github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/consensus/consensus.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:32</date> 10 //</624342611016028160> 11 12 13 //套餐共识实现不同的以太坊共识引擎。 14 package consensus 15 16 import ( 17 "math/big" 18 19 "github.com/ethereum/go-ethereum/common" 20 "github.com/ethereum/go-ethereum/core/state" 21 "github.com/ethereum/go-ethereum/core/types" 22 "github.com/ethereum/go-ethereum/params" 23 "github.com/ethereum/go-ethereum/rpc" 24 ) 25 26 //ChainReader定义访问本地 27 // 28 type ChainReader interface { 29 //config检索区块链的链配置。 30 Config() *params.ChainConfig 31 //当前头从本地链中检索当前头。 32 CurrentHeader() *types.Header 33 //GetHeader按哈希和数字从数据库中检索块头。 34 GetHeader(hash common.Hash, number uint64) *types.Header 35 //GetHeaderByNumber按编号从数据库中检索块头。 36 GetHeaderByNumber(number uint64) *types.Header 37 //GetHeaderByHash通过其哈希从数据库中检索块头。 38 GetHeaderByHash(hash common.Hash) *types.Header 39 //GetBlock按哈希和数字从数据库中检索块。 40 GetBlock(hash common.Hash, number uint64) *types.Block 41 } 42 43 //引擎是一个算法不可知的共识引擎。 44 type Engine interface { 45 //作者检索创建给定帐户的以太坊地址 46 //块,如果达成一致,则可能不同于标题的coinbase 47 //引擎基于签名。 48 Author(header *types.Header) (common.Address, error) 49 50 //验证标题检查标题是否符合 51 //给定发动机。可在此处选择或明确地验证密封件。 52 //通过VerifySeal方法。 53 //verifyheader(链式读卡器,header*types.header,seal bool)错误 54 VerifyHeader(chain ChainReader,header *types.Header, seal bool, interval uint64) error 55 //VerifyHeaders类似于VerifyHeader,但会验证一批头 56 //同时地。该方法返回退出通道以中止操作,并且 57 //用于检索异步验证的结果通道(顺序为 58 //输入切片)。 59 //verifyheaders(chain chainreader,headers[]*types.header,seals[]bool,blockinterval uint64)(chan<-struct,<-chan error) 60 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) 61 62 //验证叔父验证给定区块的叔父是否符合共识 63 //给定引擎的规则。 64 VerifyUncles(chain ChainReader, block *types.Block) error 65 66 //根据 67 //给定引擎的共识规则。 68 //verifyseal(链条阅读器,header*types.header)错误 69 VerifySeal(chain ChainReader, header *types.Header, genesisheader *types.Header) error 70 71 //Prepare根据 72 //特定引擎的规则。更改是以内联方式执行的。 73 Prepare(chain ChainReader, header *types.Header) error 74 75 //Finalize运行任何交易后状态修改(例如块奖励) 76 //组装最后一块。 77 //注意:块头和状态数据库可能会更新以反映 78 //在最终确定时达成共识的规则(例如集体奖励)。 79 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 80 uncles []*types.Header, receipts []*types.Receipt,dposContext *types.DposContext) (*types.Block, error) 81 82 //Seal使用本地矿工的 83 //密封顶部。 84 Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) 85 //Seal(链条阅读器,block*types.block,stop<-chan struct)(*types.block,错误) 86 87 //计算难度是难度调整算法。它又回到了困难中 88 //一个新的街区应该有。 89 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 90 91 //API返回此共识引擎提供的RPC API。 92 APIs(chain ChainReader) []rpc.API 93 94 //CLOSE终止由共识引擎维护的任何后台线程。 95 Close() error 96 } 97 98 //POW是基于工作证明的共识引擎。 99 type PoW interface { 100 Engine 101 102 //hashRate返回POW共识引擎的当前挖掘hashRate。 103 Hashrate() float64 104 } 105