github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/eth/api_backend.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2015 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package eth 26 27 import ( 28 "context" 29 "math/big" 30 31 "github.com/ethereum/go-ethereum/accounts" 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/common/math" 34 "github.com/ethereum/go-ethereum/core" 35 "github.com/ethereum/go-ethereum/core/bloombits" 36 "github.com/ethereum/go-ethereum/core/rawdb" 37 "github.com/ethereum/go-ethereum/core/state" 38 "github.com/ethereum/go-ethereum/core/types" 39 "github.com/ethereum/go-ethereum/core/vm" 40 "github.com/ethereum/go-ethereum/eth/downloader" 41 "github.com/ethereum/go-ethereum/eth/gasprice" 42 "github.com/ethereum/go-ethereum/ethdb" 43 "github.com/ethereum/go-ethereum/event" 44 "github.com/ethereum/go-ethereum/params" 45 "github.com/ethereum/go-ethereum/rpc" 46 ) 47 48 //ethapi backend为完整节点实现ethapi.backend 49 type EthAPIBackend struct { 50 eth *Ethereum 51 gpo *gasprice.Oracle 52 } 53 54 //chainconfig返回活动链配置。 55 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 56 return b.eth.chainConfig 57 } 58 59 func (b *EthAPIBackend) CurrentBlock() *types.Block { 60 return b.eth.blockchain.CurrentBlock() 61 } 62 63 func (b *EthAPIBackend) SetHead(number uint64) { 64 b.eth.protocolManager.downloader.Cancel() 65 b.eth.blockchain.SetHead(number) 66 } 67 68 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 69 //只有矿工知道挂起的块 70 if blockNr == rpc.PendingBlockNumber { 71 block := b.eth.miner.PendingBlock() 72 return block.Header(), nil 73 } 74 //否则解决并返回块 75 if blockNr == rpc.LatestBlockNumber { 76 return b.eth.blockchain.CurrentBlock().Header(), nil 77 } 78 return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil 79 } 80 81 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 82 return b.eth.blockchain.GetHeaderByHash(hash), nil 83 } 84 85 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 86 //只有矿工知道挂起的块 87 if blockNr == rpc.PendingBlockNumber { 88 block := b.eth.miner.PendingBlock() 89 return block, nil 90 } 91 //否则解决并返回块 92 if blockNr == rpc.LatestBlockNumber { 93 return b.eth.blockchain.CurrentBlock(), nil 94 } 95 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 96 } 97 98 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 99 //只有矿工知道挂起状态 100 if blockNr == rpc.PendingBlockNumber { 101 block, state := b.eth.miner.Pending() 102 return state, block.Header(), nil 103 } 104 //否则,解析块号并返回其状态 105 header, err := b.HeaderByNumber(ctx, blockNr) 106 if header == nil || err != nil { 107 return nil, nil, err 108 } 109 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 110 return stateDb, header, err 111 } 112 113 func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { 114 return b.eth.blockchain.GetBlockByHash(hash), nil 115 } 116 117 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 118 if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil { 119 return rawdb.ReadReceipts(b.eth.chainDb, hash, *number), nil 120 } 121 return nil, nil 122 } 123 124 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 125 number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash) 126 if number == nil { 127 return nil, nil 128 } 129 receipts := rawdb.ReadReceipts(b.eth.chainDb, hash, *number) 130 if receipts == nil { 131 return nil, nil 132 } 133 logs := make([][]*types.Log, len(receipts)) 134 for i, receipt := range receipts { 135 logs[i] = receipt.Logs 136 } 137 return logs, nil 138 } 139 140 func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int { 141 return b.eth.blockchain.GetTdByHash(blockHash) 142 } 143 144 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) { 145 state.SetBalance(msg.From(), math.MaxBig256) 146 vmError := func() error { return nil } 147 148 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 149 return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil 150 } 151 152 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 153 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 154 } 155 156 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 157 return b.eth.BlockChain().SubscribeChainEvent(ch) 158 } 159 160 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 161 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 162 } 163 164 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 165 return b.eth.BlockChain().SubscribeLogsEvent(ch) 166 } 167 168 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 169 return b.eth.txPool.AddLocal(signedTx) 170 } 171 172 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 173 pending, err := b.eth.txPool.Pending() 174 if err != nil { 175 return nil, err 176 } 177 var txs types.Transactions 178 for _, batch := range pending { 179 txs = append(txs, batch...) 180 } 181 return txs, nil 182 } 183 184 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 185 return b.eth.txPool.Get(hash) 186 } 187 188 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 189 return b.eth.txPool.State().GetNonce(addr), nil 190 } 191 192 func (b *EthAPIBackend) Stats() (pending int, queued int) { 193 return b.eth.txPool.Stats() 194 } 195 196 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 197 return b.eth.TxPool().Content() 198 } 199 200 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 201 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 202 } 203 204 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 205 return b.eth.Downloader() 206 } 207 208 func (b *EthAPIBackend) ProtocolVersion() int { 209 return b.eth.EthVersion() 210 } 211 212 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 213 return b.gpo.SuggestPrice(ctx) 214 } 215 216 func (b *EthAPIBackend) ChainDb() ethdb.Database { 217 return b.eth.ChainDb() 218 } 219 220 func (b *EthAPIBackend) EventMux() *event.TypeMux { 221 return b.eth.EventMux() 222 } 223 224 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 225 return b.eth.AccountManager() 226 } 227 228 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 229 sections, _, _ := b.eth.bloomIndexer.Sections() 230 return params.BloomBitsBlocks, sections 231 } 232 233 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 234 for i := 0; i < bloomFilterThreads; i++ { 235 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 236 } 237 }