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