github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/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 "fmt" 23 "math/big" 24 "time" 25 26 electroneum "github.com/electroneum/electroneum-sc" 27 "github.com/electroneum/electroneum-sc/accounts" 28 "github.com/electroneum/electroneum-sc/common" 29 "github.com/electroneum/electroneum-sc/consensus" 30 "github.com/electroneum/electroneum-sc/core" 31 "github.com/electroneum/electroneum-sc/core/bloombits" 32 "github.com/electroneum/electroneum-sc/core/rawdb" 33 "github.com/electroneum/electroneum-sc/core/state" 34 "github.com/electroneum/electroneum-sc/core/types" 35 "github.com/electroneum/electroneum-sc/core/vm" 36 "github.com/electroneum/electroneum-sc/eth/gasprice" 37 "github.com/electroneum/electroneum-sc/ethdb" 38 "github.com/electroneum/electroneum-sc/event" 39 "github.com/electroneum/electroneum-sc/miner" 40 "github.com/electroneum/electroneum-sc/params" 41 "github.com/electroneum/electroneum-sc/rpc" 42 ) 43 44 // EthAPIBackend implements ethapi.Backend for full nodes 45 type EthAPIBackend struct { 46 extRPCEnabled bool 47 allowUnprotectedTxs bool 48 eth *Ethereum 49 gpo *gasprice.Oracle 50 } 51 52 // ChainConfig returns the active chain configuration. 53 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 54 return b.eth.blockchain.Config() 55 } 56 57 func (b *EthAPIBackend) CurrentBlock() *types.Block { 58 return b.eth.blockchain.CurrentBlock() 59 } 60 61 func (b *EthAPIBackend) SetHead(number uint64) { 62 b.eth.handler.downloader.Cancel() 63 b.eth.blockchain.SetHead(number) 64 } 65 66 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 67 // Pending block is only known by the miner 68 if number == rpc.PendingBlockNumber { 69 block := b.eth.miner.PendingBlock() 70 return block.Header(), nil 71 } 72 // Otherwise resolve and return the block 73 if number == rpc.LatestBlockNumber { 74 return b.eth.blockchain.CurrentBlock().Header(), nil 75 } 76 if number == rpc.FinalizedBlockNumber { 77 return b.eth.blockchain.CurrentFinalizedBlock().Header(), nil 78 } 79 return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil 80 } 81 82 func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { 83 if blockNr, ok := blockNrOrHash.Number(); ok { 84 return b.HeaderByNumber(ctx, blockNr) 85 } 86 if hash, ok := blockNrOrHash.Hash(); ok { 87 header := b.eth.blockchain.GetHeaderByHash(hash) 88 if header == nil { 89 return nil, errors.New("header for hash not found") 90 } 91 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 92 return nil, errors.New("hash is not currently canonical") 93 } 94 return header, nil 95 } 96 return nil, errors.New("invalid arguments; neither block nor hash specified") 97 } 98 99 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 100 return b.eth.blockchain.GetHeaderByHash(hash), nil 101 } 102 103 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 104 // Pending block is only known by the miner 105 if number == rpc.PendingBlockNumber { 106 block := b.eth.miner.PendingBlock() 107 return block, nil 108 } 109 // Otherwise resolve and return the block 110 if number == rpc.LatestBlockNumber { 111 return b.eth.blockchain.CurrentBlock(), nil 112 } 113 if number == rpc.FinalizedBlockNumber { 114 return b.eth.blockchain.CurrentFinalizedBlock(), nil 115 } 116 return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil 117 } 118 119 func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 120 return b.eth.blockchain.GetBlockByHash(hash), nil 121 } 122 123 func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { 124 if blockNr, ok := blockNrOrHash.Number(); ok { 125 return b.BlockByNumber(ctx, blockNr) 126 } 127 if hash, ok := blockNrOrHash.Hash(); ok { 128 header := b.eth.blockchain.GetHeaderByHash(hash) 129 if header == nil { 130 return nil, errors.New("header for hash not found") 131 } 132 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 133 return nil, errors.New("hash is not currently canonical") 134 } 135 block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) 136 if block == nil { 137 return nil, errors.New("header found, but block body is missing") 138 } 139 return block, nil 140 } 141 return nil, errors.New("invalid arguments; neither block nor hash specified") 142 } 143 144 func (b *EthAPIBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 145 return b.eth.miner.PendingBlockAndReceipts() 146 } 147 148 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 149 // Pending state is only known by the miner 150 if number == rpc.PendingBlockNumber { 151 block, state := b.eth.miner.Pending() 152 return state, block.Header(), nil 153 } 154 // Otherwise resolve the block number and return its state 155 header, err := b.HeaderByNumber(ctx, number) 156 if err != nil { 157 return nil, nil, err 158 } 159 if header == nil { 160 return nil, nil, errors.New("header not found") 161 } 162 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 163 return stateDb, header, err 164 } 165 166 func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { 167 if blockNr, ok := blockNrOrHash.Number(); ok { 168 return b.StateAndHeaderByNumber(ctx, blockNr) 169 } 170 if hash, ok := blockNrOrHash.Hash(); ok { 171 header, err := b.HeaderByHash(ctx, hash) 172 if err != nil { 173 return nil, nil, err 174 } 175 if header == nil { 176 return nil, nil, errors.New("header for hash not found") 177 } 178 if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { 179 return nil, nil, errors.New("hash is not currently canonical") 180 } 181 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 182 return stateDb, header, err 183 } 184 return nil, nil, errors.New("invalid arguments; neither block nor hash specified") 185 } 186 187 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 188 return b.eth.blockchain.GetReceiptsByHash(hash), nil 189 } 190 191 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 192 db := b.eth.ChainDb() 193 number := rawdb.ReadHeaderNumber(db, hash) 194 if number == nil { 195 return nil, fmt.Errorf("failed to get block number for hash %#x", hash) 196 } 197 logs := rawdb.ReadLogs(db, hash, *number, b.eth.blockchain.Config()) 198 if logs == nil { 199 return nil, fmt.Errorf("failed to get logs for block #%d (0x%s)", *number, hash.TerminalString()) 200 } 201 return logs, nil 202 } 203 204 func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int { 205 if header := b.eth.blockchain.GetHeaderByHash(hash); header != nil { 206 return b.eth.blockchain.GetTd(hash, header.Number.Uint64()) 207 } 208 return nil 209 } 210 211 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) { 212 vmError := func() error { return nil } 213 if vmConfig == nil { 214 vmConfig = b.eth.blockchain.GetVMConfig() 215 } 216 txContext := core.NewEVMTxContext(msg) 217 context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil) 218 return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil 219 } 220 221 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 222 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 223 } 224 225 func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { 226 return b.eth.miner.SubscribePendingLogs(ch) 227 } 228 229 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 230 return b.eth.BlockChain().SubscribeChainEvent(ch) 231 } 232 233 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 234 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 235 } 236 237 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 238 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 239 } 240 241 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 242 return b.eth.BlockChain().SubscribeLogsEvent(ch) 243 } 244 245 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 246 return b.eth.txPool.AddLocal(signedTx) 247 } 248 249 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 250 pending := b.eth.txPool.Pending(false) 251 var txs types.Transactions 252 for _, batch := range pending { 253 txs = append(txs, batch...) 254 } 255 return txs, nil 256 } 257 258 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 259 return b.eth.txPool.Get(hash) 260 } 261 262 func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { 263 tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) 264 return tx, blockHash, blockNumber, index, nil 265 } 266 267 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 268 return b.eth.txPool.Nonce(addr), nil 269 } 270 271 func (b *EthAPIBackend) Stats() (pending int, queued int) { 272 return b.eth.txPool.Stats() 273 } 274 275 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 276 return b.eth.TxPool().Content() 277 } 278 279 func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) { 280 return b.eth.TxPool().ContentFrom(addr) 281 } 282 283 func (b *EthAPIBackend) TxPool() *core.TxPool { 284 return b.eth.TxPool() 285 } 286 287 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 288 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 289 } 290 291 func (b *EthAPIBackend) SyncProgress() electroneum.SyncProgress { 292 return b.eth.Downloader().Progress() 293 } 294 295 func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 296 return b.gpo.SuggestTipCap(ctx) 297 } 298 299 func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { 300 return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) 301 } 302 303 func (b *EthAPIBackend) ChainDb() ethdb.Database { 304 return b.eth.ChainDb() 305 } 306 307 func (b *EthAPIBackend) EventMux() *event.TypeMux { 308 return b.eth.EventMux() 309 } 310 311 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 312 return b.eth.AccountManager() 313 } 314 315 func (b *EthAPIBackend) ExtRPCEnabled() bool { 316 return b.extRPCEnabled 317 } 318 319 func (b *EthAPIBackend) UnprotectedAllowed() bool { 320 return b.allowUnprotectedTxs 321 } 322 323 func (b *EthAPIBackend) RPCGasCap() uint64 { 324 return b.eth.config.RPCGasCap 325 } 326 327 func (b *EthAPIBackend) RPCEVMTimeout() time.Duration { 328 return b.eth.config.RPCEVMTimeout 329 } 330 331 func (b *EthAPIBackend) RPCTxFeeCap() float64 { 332 return b.eth.config.RPCTxFeeCap 333 } 334 335 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 336 sections, _, _ := b.eth.bloomIndexer.Sections() 337 return params.BloomBitsBlocks, sections 338 } 339 340 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 341 for i := 0; i < bloomFilterThreads; i++ { 342 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 343 } 344 } 345 346 func (b *EthAPIBackend) Engine() consensus.Engine { 347 return b.eth.engine 348 } 349 350 func (b *EthAPIBackend) CurrentHeader() *types.Header { 351 return b.eth.blockchain.CurrentHeader() 352 } 353 354 func (b *EthAPIBackend) Miner() *miner.Miner { 355 return b.eth.Miner() 356 } 357 358 func (b *EthAPIBackend) StartMining(threads int) error { 359 return b.eth.StartMining(threads) 360 } 361 362 func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive, preferDisk bool) (*state.StateDB, error) { 363 return b.eth.StateAtBlock(block, reexec, base, checkLive, preferDisk) 364 } 365 366 func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) { 367 return b.eth.stateAtTransaction(block, txIndex, reexec) 368 }