github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/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 19:16:34</date> 10 //</624450074675515392> 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 //当前头从本地链中检索当前头。 33 CurrentHeader() *types.Header 34 35 //GetHeader按哈希和数字从数据库中检索块头。 36 GetHeader(hash common.Hash, number uint64) *types.Header 37 38 //GetHeaderByNumber按编号从数据库中检索块头。 39 GetHeaderByNumber(number uint64) *types.Header 40 41 //GetHeaderByHash通过其哈希从数据库中检索块头。 42 GetHeaderByHash(hash common.Hash) *types.Header 43 44 //GetBlock按哈希和数字从数据库中检索块。 45 GetBlock(hash common.Hash, number uint64) *types.Block 46 } 47 48 //引擎是一个算法不可知的共识引擎。 49 type Engine interface { 50 //作者检索创建给定帐户的以太坊地址 51 //块,如果达成一致,则可能不同于标题的coinbase 52 //引擎基于签名。 53 Author(header *types.Header) (common.Address, error) 54 55 //验证标题检查标题是否符合 56 //给定发动机。可在此处选择或明确地验证密封件。 57 //通过VerifySeal方法。 58 VerifyHeader(chain ChainReader, header *types.Header, seal bool) error 59 60 //VerifyHeaders类似于VerifyHeader,但会验证一批头 61 //同时地。该方法返回退出通道以中止操作,并且 62 //用于检索异步验证的结果通道(顺序为 63 //输入切片)。 64 VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) 65 66 //验证叔父验证给定区块的叔父是否符合共识 67 //给定引擎的规则。 68 VerifyUncles(chain ChainReader, block *types.Block) error 69 70 //根据 71 //给定引擎的共识规则。 72 VerifySeal(chain ChainReader, header *types.Header) error 73 74 //Prepare根据 75 //特定引擎的规则。更改是以内联方式执行的。 76 Prepare(chain ChainReader, header *types.Header) error 77 78 //Finalize运行任何交易后状态修改(例如块奖励) 79 //组装最后一块。 80 //注意:块头和状态数据库可能会更新以反映 81 //在最终确定时达成共识的规则(例如集体奖励)。 82 Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, 83 uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) 84 85 //Seal为给定的输入块生成新的密封请求并推动 86 //将结果输入给定的通道。 87 // 88 //注意,该方法立即返回并将结果异步发送。更多 89 //根据共识算法,还可以返回一个以上的结果。 90 Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error 91 92 //sealHash返回块在被密封之前的哈希。 93 SealHash(header *types.Header) common.Hash 94 95 //计算难度是难度调整算法。它又回到了困难中 96 //一个新的街区应该有。 97 CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int 98 99 //API返回此共识引擎提供的RPC API。 100 APIs(chain ChainReader) []rpc.API 101 102 //CLOSE终止由共识引擎维护的任何后台线程。 103 Close() error 104 } 105 106 //POW是基于工作证明的共识引擎。 107 type PoW interface { 108 Engine 109 110 //hashRate返回POW共识引擎的当前挖掘hashRate。 111 Hashrate() float64 112 } 113