dpos

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 27 Imported by: 0

README

Implement a simple blockchain use dpos algorithm

Architecture Design

  • Create a P2P Conn-pool
  • BlockChain Generate
  • Node Manage And Vote
  • Pick Node
  • Write Block On Blockchain

Build

go build -o build/dpos main/dpos.go

RUN

git clone git@github.com:csunny/dpos.git

cd dpos    // 切换到源码路径下
go build main/dpos.go

connect multi peer

./dpos new --port 3000 --secio

Vote

./dpos vote -name QmaxEdbKW4x9mP2vX15zL9fyEsp9b9yV48zwtdrpYddfxe -v 30

Document

Doc

Licence

MIT

Documentation

Index

Constants

View Source
const (
	// DefaultVote 节点默认的票数
	DefaultVote = 10
	// FileName 节点信息保存配置文件
	FileName = "config.ini"
)
View Source
const BPCount = 5

BPCount 区块生产者的数量

Variables

View Source
var BlockChain []Block

BlockChain slice to storage Block

View Source
var NewNode = cli.Command{
	Name:  "new",
	Usage: "add a new node to p2p network",
	Flags: []cli.Flag{
		cli.IntFlag{
			Name:  "port",
			Value: 3000,
			Usage: "新创建的节点端口号",
		},
		cli.StringFlag{
			Name:  "target",
			Value: "",
			Usage: "目标节点",
		},
		cli.BoolFlag{
			Name:  "secio",
			Usage: "是否打开secio",
		},
		cli.Int64Flag{
			Name:  "seed",
			Value: 0,
			Usage: "生产随机数",
		},
	},
	Action: func(context *cli.Context) error {
		if err := Run(context); err != nil {
			return err
		}
		return nil
	},
}

NewNode 创建新的节点加入到P2P网络

Functions

func CaculateBlockHash

func CaculateBlockHash(block Block) string

CaculateBlockHash 计算Block的hash值

func CaculateHash

func CaculateHash(s string) string

CaculateHash 计算string的hash值

func HandleStream

func HandleStream(s network.Stream)

HandleStream handler stream info

func IsBlockValid

func IsBlockValid(newBlock, oldBlock Block) bool

IsBlockValid 校验区块是否合法

func MakeBasicHost

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

MakeBasicHost 构建P2P网络

func PickWinner

func PickWinner() (bp string)

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

func Run

func Run(ctx *cli.Context) error

Run 函数

func SavePeer

func SavePeer(name string)

SavePeer 将加入到网络中的节点信息保存到配置文件中,方便后续投票与选择

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 根据上一个区块信息,生成新的区块

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator 定义节点信息

Directories

Path Synopsis
* Tools package.
* Tools package.

Jump to

Keyboard shortcuts

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