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