blockchain

package
v0.0.0-...-36603ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

implement a simple p2p blockchain

Index

Constants

This section is empty.

Variables

View Source
var BlockChain []Block

BlockChain slice to storage Block

Functions

func CaculateBlockHash

func CaculateBlockHash(block Block) string

CaculateBlockHash 计算Block的hash值

func CaculateHash

func CaculateHash(s string) string

CaculateHash 计算string的hash值

func CreateNode

func CreateNode(port int, target string, seed int64) (string, string, error)

func GetNodes

func GetNodes() []string

func GetTokenAge

func GetTokenAge(id string) int64

func HandleStream

func HandleStream(s network.Stream)

HandleStream handler stream info

func InitNodeResource

func InitNodeResource()

func InitTokenAgeMap

func InitTokenAgeMap()

func InitVoteMap

func InitVoteMap()

func IsBlockValid

func IsBlockValid(newBlock, oldBlock Block) bool

IsBlockValid 校验区块是否合法

func MakeBasicHost

func MakeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, string, string, error)

MakeBasicHost 构建P2P网络

func NodeTokenAgeMap

func NodeTokenAgeMap() map[string]int64

func NodeVoteMap

func NodeVoteMap() map[string]int

func PickWinnerWithDpos

func PickWinnerWithDpos() (bp string, err error)

PickWinner 根据投票数量选择生成区块的节点

func PickWinnerWithPos

func PickWinnerWithPos() (bp string, err error)

PickWinnerPos 根据代币和持有时间选择生成区块的节点

func ReadData

func ReadData(id string) string

func SetTokenAge

func SetTokenAge(id string, TokenAge int64)

func Vote

func Vote(id string, ticket int)

func WriteData

func WriteData(address string, data int) (bool, error)

Types

type Block

type Block struct {
	Index     int
	Timestamp string
	BPM       int
	Hash      string
	PrevHash  string
	// contains filtered or unexported fields
}

Block struct, A block contain 以下信息: Index 索引、Timestamp(时间戳)、�BPM、Hash(自己的hash值)、PreHash(上一个块的Hash值)、validator(此区块的生产者信息)

func GenerateBlock

func GenerateBlock(oldBlock Block, BPM int, address string) (Block, error)

GenerateBlock 根据上一个区块信息,生成新的区块

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL