gitee.com/liu-zhao234568/cntest@v1.0.0/eth/api_backend.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package eth 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 24 "gitee.com/liu-zhao234568/cntest/accounts" 25 "gitee.com/liu-zhao234568/cntest/common" 26 "gitee.com/liu-zhao234568/cntest/consensus" 27 "gitee.com/liu-zhao234568/cntest/core" 28 "gitee.com/liu-zhao234568/cntest/core/bloombits" 29 "gitee.com/liu-zhao234568/cntest/core/rawdb" 30 "gitee.com/liu-zhao234568/cntest/core/state" 31 "gitee.com/liu-zhao234568/cntest/core/types" 32 "gitee.com/liu-zhao234568/cntest/core/vm" 33 "gitee.com/liu-zhao234568/cntest/eth/downloader" 34 "gitee.com/liu-zhao234568/cntest/eth/gasprice" 35 "gitee.com/liu-zhao234568/cntest/ethdb" 36 "gitee.com/liu-zhao234568/cntest/event" 37 "gitee.com/liu-zhao234568/cntest/miner" 38 "gitee.com/liu-zhao234568/cntest/params" 39 "gitee.com/liu-zhao234568/cntest/rpc" 40 ) 41 42 // EthAPIBackend implements ethapi.Backend for full nodes 43 type EthAPIBackend struct { 44 extRPCEnabled bool 45 allowUnprotectedTxs bool 46 eth *Ethereum 47 gpo *gasprice.Oracle 48 } 49 50 // ChainConfig returns the active chain configuration. 51 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 52 return b.eth.blockchain.Config() 53 } 54 55 func (b *EthAPIBackend) CurrentBlock() *types.Block { 56 return b.eth.blockchain.CurrentBlock() 57 } 58 59 func (b *EthAPIBackend) SetHead(number uint64) { 60 b.eth.handler.downloader.Cancel() 61 b.eth.blockchain.SetHead(number) 62 } 63 64 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 65 // Pending block is only known by the miner 66 if number == rpc.PendingBlockNumber { 67 block := b.eth.miner.PendingBlock() 68 return block.Header(), nil 69 } 70 // Otherwise resolve and return the block 71 if number == rpc.LatestBlockNumber { 72 return b.eth.blockchain.CurrentBlock().Header(), nil 73 } 74 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 75 } 76 77 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 78 if blockNr, ok := blockNrOrHash.Number(); ok { 79 return b.HeaderByNumber(ctx, blockNr) 80 } 81 if hash, ok := blockNrOrHash.Hash(); ok { 82 header := b.eth.blockchain.GetHeaderByHash(hash) 83 if header == nil { 84 return nil, errors.New("header for hash not found") 85 } 86 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 87 return nil, errors.New("hash is not currently canonical") 88 } 89 return header, nil 90 } 91 return nil, errors.New("invalid arguments; neither block nor hash specified") 92 } 93 94 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 95 return b.eth.blockchain.GetHeaderByHash(hash), nil 96 } 97 98 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 99 // Pending block is only known by the miner 100 if number == rpc.PendingBlockNumber { 101 block := b.eth.miner.PendingBlock() 102 return block, nil 103 } 104 // Otherwise resolve and return the block 105 if number == rpc.LatestBlockNumber { 106 return b.eth.blockchain.CurrentBlock(), nil 107 } 108 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 109 } 110 111 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 112 return b.eth.blockchain.GetBlockByHash(hash), nil 113 } 114 115 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 116 if blockNr, ok := blockNrOrHash.Number(); ok { 117 return b.BlockByNumber(ctx, blockNr) 118 } 119 if hash, ok := blockNrOrHash.Hash(); ok { 120 header := b.eth.blockchain.GetHeaderByHash(hash) 121 if header == nil { 122 return nil, errors.New("header for hash not found") 123 } 124 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 125 return nil, errors.New("hash is not currently canonical") 126 } 127 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 128 if block == nil { 129 return nil, errors.New("header found, but block body is missing") 130 } 131 return block, nil 132 } 133 return nil, errors.New("invalid arguments; neither block nor hash specified") 134 } 135 136 func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 137 return b.eth.miner.PendingBlockAndReceipts() 138 } 139 140 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 141 // Pending state is only known by the miner 142 if number == rpc.PendingBlockNumber { 143 block, state := b.eth.miner.Pending() 144 return state, block.Header(), nil 145 } 146 // Otherwise resolve the block number and return its state 147 header, err := b.HeaderByNumber(ctx, number) 148 if err != nil { 149 return nil, nil, err 150 } 151 if header == nil { 152 return nil, nil, errors.New("header not found") 153 } 154 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 155 return stateDb, header, err 156 } 157 158 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 159 if blockNr, ok := blockNrOrHash.Number(); ok { 160 return b.StateAndHeaderByNumber(ctx, blockNr) 161 } 162 if hash, ok := blockNrOrHash.Hash(); ok { 163 header, err := b.HeaderByHash(ctx, hash) 164 if err != nil { 165 return nil, nil, err 166 } 167 if header == nil { 168 return nil, nil, errors.New("header for hash not found") 169 } 170 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 171 return nil, nil, errors.New("hash is not currently canonical") 172 } 173 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 174 return stateDb, header, err 175 } 176 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 177 } 178 179 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 180 return b.eth.blockchain.GetReceiptsByHash(hash), nil 181 } 182 183 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 184 receipts := b.eth.blockchain.GetReceiptsByHash(hash) 185 if receipts == nil { 186 return nil, nil 187 } 188 logs := make([][]*types.Log, len(receipts)) 189 for i, receipt := range receipts { 190 logs[i] = receipt.Logs 191 } 192 return logs, nil 193 } 194 195 func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 196 return b.eth.blockchain.GetTdByHash(hash) 197 } 198 199 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) { 200 vmError := func() error { return nil } 201 if vmConfig == nil { 202 vmConfig = b.eth.blockchain.GetVMConfig() 203 } 204 txContext := core.NewEVMTxContext(msg) 205 context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) 206 return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil 207 } 208 209 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 210 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 211 } 212 213 func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 214 return b.eth.miner.SubscribePendingLogs(ch) 215 } 216 217 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 218 return b.eth.BlockChain().SubscribeChainEvent(ch) 219 } 220 221 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 222 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 223 } 224 225 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 226 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 227 } 228 229 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 230 return b.eth.BlockChain().SubscribeLogsEvent(ch) 231 } 232 233 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 234 return b.eth.txPool.AddLocal(signedTx) 235 } 236 237 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 238 pending, err := b.eth.txPool.Pending(false) 239 if err != nil { 240 return nil, err 241 } 242 var txs types.Transactions 243 for _, batch := range pending { 244 txs = append(txs, batch...) 245 } 246 return txs, nil 247 } 248 249 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 250 return b.eth.txPool.Get(hash) 251 } 252 253 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 254 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) 255 return tx, blockHash, blockNumber, index, nil 256 } 257 258 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 259 return b.eth.txPool.Nonce(addr), nil 260 } 261 262 func (b *EthAPIBackend) Stats() (pending int, queued int) { 263 return b.eth.txPool.Stats() 264 } 265 266 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 267 return b.eth.TxPool().Content() 268 } 269 270 func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) { 271 return b.eth.TxPool().ContentFrom(addr) 272 } 273 274 func (b *EthAPIBackend) TxPool() *core.TxPool { 275 return b.eth.TxPool() 276 } 277 278 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 279 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 280 } 281 282 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 283 return b.eth.Downloader() 284 } 285 286 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 287 return b.gpo.SuggestTipCap(ctx) 288 } 289 290 func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock rpc.BlockNumber, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { 291 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 292 } 293 294 func (b *EthAPIBackend) ChainDb() ethdb.Database { 295 return b.eth.ChainDb() 296 } 297 298 func (b *EthAPIBackend) EventMux() *event.TypeMux { 299 return b.eth.EventMux() 300 } 301 302 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 303 return b.eth.AccountManager() 304 } 305 306 func (b *EthAPIBackend) ExtRPCEnabled() bool { 307 return b.extRPCEnabled 308 } 309 310 func (b *EthAPIBackend) UnprotectedAllowed() bool { 311 return b.allowUnprotectedTxs 312 } 313 314 func (b *EthAPIBackend) RPCGasCap() uint64 { 315 return b.eth.config.RPCGasCap 316 } 317 318 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 319 return b.eth.config.RPCTxFeeCap 320 } 321 322 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 323 sections, _, _ := b.eth.bloomIndexer.Sections() 324 return params.BloomBitsBlocks, sections 325 } 326 327 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 328 for i := 0; i < bloomFilterThreads; i++ { 329 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 330 } 331 } 332 333 func (b *EthAPIBackend) Engine() consensus.Engine { 334 return b.eth.engine 335 } 336 337 func (b *EthAPIBackend) CurrentHeader() *types.Header { 338 return b.eth.blockchain.CurrentHeader() 339 } 340 341 func (b *EthAPIBackend) Miner() *miner.Miner { 342 return b.eth.Miner() 343 } 344 345 func (b *EthAPIBackend) StartMining(threads int) error { 346 return b.eth.StartMining(threads) 347 } 348 349 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error) { 350 return b.eth.stateAtBlock(block, reexec, base, checkLive) 351 } 352 353 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) { 354 return b.eth.stateAtTransaction(block, txIndex, reexec) 355 }