github.com/openethereum/go-ethereum@v1.9.7/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 "github.com/ethereum/go-ethereum/accounts" 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/common/math" 27 "github.com/ethereum/go-ethereum/core" 28 "github.com/ethereum/go-ethereum/core/bloombits" 29 "github.com/ethereum/go-ethereum/core/rawdb" 30 "github.com/ethereum/go-ethereum/core/state" 31 "github.com/ethereum/go-ethereum/core/types" 32 "github.com/ethereum/go-ethereum/core/vm" 33 "github.com/ethereum/go-ethereum/eth/downloader" 34 "github.com/ethereum/go-ethereum/eth/gasprice" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/event" 37 "github.com/ethereum/go-ethereum/params" 38 "github.com/ethereum/go-ethereum/rpc" 39 ) 40 41 // EthAPIBackend implements ethapi.Backend for full nodes 42 type EthAPIBackend struct { 43 extRPCEnabled bool 44 eth *Ethereum 45 gpo *gasprice.Oracle 46 } 47 48 // ChainConfig returns the active chain configuration. 49 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 50 return b.eth.blockchain.Config() 51 } 52 53 func (b *EthAPIBackend) CurrentBlock() *types.Block { 54 return b.eth.blockchain.CurrentBlock() 55 } 56 57 func (b *EthAPIBackend) SetHead(number uint64) { 58 b.eth.protocolManager.downloader.Cancel() 59 b.eth.blockchain.SetHead(number) 60 } 61 62 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 63 // Pending block is only known by the miner 64 if number == rpc.PendingBlockNumber { 65 block := b.eth.miner.PendingBlock() 66 return block.Header(), nil 67 } 68 // Otherwise resolve and return the block 69 if number == rpc.LatestBlockNumber { 70 return b.eth.blockchain.CurrentBlock().Header(), nil 71 } 72 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 73 } 74 75 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 76 if blockNr, ok := blockNrOrHash.Number(); ok { 77 return b.HeaderByNumber(ctx, blockNr) 78 } 79 if hash, ok := blockNrOrHash.Hash(); ok { 80 header := b.eth.blockchain.GetHeaderByHash(hash) 81 if header == nil { 82 return nil, errors.New("header for hash not found") 83 } 84 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 85 return nil, errors.New("hash is not currently canonical") 86 } 87 return header, nil 88 } 89 return nil, errors.New("invalid arguments; neither block nor hash specified") 90 } 91 92 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 93 return b.eth.blockchain.GetHeaderByHash(hash), nil 94 } 95 96 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 97 // Pending block is only known by the miner 98 if number == rpc.PendingBlockNumber { 99 block := b.eth.miner.PendingBlock() 100 return block, nil 101 } 102 // Otherwise resolve and return the block 103 if number == rpc.LatestBlockNumber { 104 return b.eth.blockchain.CurrentBlock(), nil 105 } 106 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 107 } 108 109 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 110 return b.eth.blockchain.GetBlockByHash(hash), nil 111 } 112 113 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 114 if blockNr, ok := blockNrOrHash.Number(); ok { 115 return b.BlockByNumber(ctx, blockNr) 116 } 117 if hash, ok := blockNrOrHash.Hash(); ok { 118 header := b.eth.blockchain.GetHeaderByHash(hash) 119 if header == nil { 120 return nil, errors.New("header for hash not found") 121 } 122 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 123 return nil, errors.New("hash is not currently canonical") 124 } 125 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 126 if block == nil { 127 return nil, errors.New("header found, but block body is missing") 128 } 129 return block, nil 130 } 131 return nil, errors.New("invalid arguments; neither block nor hash specified") 132 } 133 134 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 135 // Pending state is only known by the miner 136 if number == rpc.PendingBlockNumber { 137 block, state := b.eth.miner.Pending() 138 return state, block.Header(), nil 139 } 140 // Otherwise resolve the block number and return its state 141 header, err := b.HeaderByNumber(ctx, number) 142 if err != nil { 143 return nil, nil, err 144 } 145 if header == nil { 146 return nil, nil, errors.New("header not found") 147 } 148 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 149 return stateDb, header, err 150 } 151 152 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 153 if blockNr, ok := blockNrOrHash.Number(); ok { 154 return b.StateAndHeaderByNumber(ctx, blockNr) 155 } 156 if hash, ok := blockNrOrHash.Hash(); ok { 157 header, err := b.HeaderByHash(ctx, hash) 158 if err != nil { 159 return nil, nil, err 160 } 161 if header == nil { 162 return nil, nil, errors.New("header for hash not found") 163 } 164 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 165 return nil, nil, errors.New("hash is not currently canonical") 166 } 167 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 168 return stateDb, header, err 169 } 170 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 171 } 172 173 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 174 return b.eth.blockchain.GetReceiptsByHash(hash), nil 175 } 176 177 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 178 receipts := b.eth.blockchain.GetReceiptsByHash(hash) 179 if receipts == nil { 180 return nil, nil 181 } 182 logs := make([][]*types.Log, len(receipts)) 183 for i, receipt := range receipts { 184 logs[i] = receipt.Logs 185 } 186 return logs, nil 187 } 188 189 func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int { 190 return b.eth.blockchain.GetTdByHash(blockHash) 191 } 192 193 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) { 194 state.SetBalance(msg.From(), math.MaxBig256) 195 vmError := func() error { return nil } 196 197 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 198 return vm.NewEVM(context, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil 199 } 200 201 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 202 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 203 } 204 205 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 206 return b.eth.BlockChain().SubscribeChainEvent(ch) 207 } 208 209 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 210 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 211 } 212 213 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 214 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 215 } 216 217 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 218 return b.eth.BlockChain().SubscribeLogsEvent(ch) 219 } 220 221 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 222 return b.eth.txPool.AddLocal(signedTx) 223 } 224 225 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 226 pending, err := b.eth.txPool.Pending() 227 if err != nil { 228 return nil, err 229 } 230 var txs types.Transactions 231 for _, batch := range pending { 232 txs = append(txs, batch...) 233 } 234 return txs, nil 235 } 236 237 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 238 return b.eth.txPool.Get(hash) 239 } 240 241 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 242 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) 243 return tx, blockHash, blockNumber, index, nil 244 } 245 246 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 247 return b.eth.txPool.Nonce(addr), nil 248 } 249 250 func (b *EthAPIBackend) Stats() (pending int, queued int) { 251 return b.eth.txPool.Stats() 252 } 253 254 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 255 return b.eth.TxPool().Content() 256 } 257 258 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 259 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 260 } 261 262 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 263 return b.eth.Downloader() 264 } 265 266 func (b *EthAPIBackend) ProtocolVersion() int { 267 return b.eth.EthVersion() 268 } 269 270 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 271 return b.gpo.SuggestPrice(ctx) 272 } 273 274 func (b *EthAPIBackend) ChainDb() ethdb.Database { 275 return b.eth.ChainDb() 276 } 277 278 func (b *EthAPIBackend) EventMux() *event.TypeMux { 279 return b.eth.EventMux() 280 } 281 282 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 283 return b.eth.AccountManager() 284 } 285 286 func (b *EthAPIBackend) ExtRPCEnabled() bool { 287 return b.extRPCEnabled 288 } 289 290 func (b *EthAPIBackend) RPCGasCap() *big.Int { 291 return b.eth.config.RPCGasCap 292 } 293 294 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 295 sections, _, _ := b.eth.bloomIndexer.Sections() 296 return params.BloomBitsBlocks, sections 297 } 298 299 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 300 for i := 0; i < bloomFilterThreads; i++ { 301 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 302 } 303 }