github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/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 "math/big" 22 23 "github.com/FusionFoundation/efsn/accounts" 24 "github.com/FusionFoundation/efsn/common" 25 "github.com/FusionFoundation/efsn/common/math" 26 "github.com/FusionFoundation/efsn/core" 27 "github.com/FusionFoundation/efsn/core/bloombits" 28 "github.com/FusionFoundation/efsn/core/rawdb" 29 "github.com/FusionFoundation/efsn/core/state" 30 "github.com/FusionFoundation/efsn/core/types" 31 "github.com/FusionFoundation/efsn/core/vm" 32 "github.com/FusionFoundation/efsn/eth/downloader" 33 "github.com/FusionFoundation/efsn/eth/gasprice" 34 "github.com/FusionFoundation/efsn/ethdb" 35 "github.com/FusionFoundation/efsn/event" 36 "github.com/FusionFoundation/efsn/params" 37 "github.com/FusionFoundation/efsn/rpc" 38 ) 39 40 // EthAPIBackend implements ethapi.Backend for full nodes 41 type EthAPIBackend struct { 42 eth *Ethereum 43 gpo *gasprice.Oracle 44 } 45 46 // ChainConfig returns the active chain configuration. 47 func (b *EthAPIBackend) ChainConfig() *params.ChainConfig { 48 return b.eth.chainConfig 49 } 50 51 func (b *EthAPIBackend) CurrentBlock() *types.Block { 52 return b.eth.blockchain.CurrentBlock() 53 } 54 55 func (b *EthAPIBackend) SetHead(number uint64) { 56 b.eth.protocolManager.downloader.Cancel() 57 b.eth.blockchain.SetHead(number) 58 } 59 60 func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 61 // Pending block is only known by the miner 62 if blockNr == rpc.PendingBlockNumber { 63 block := b.eth.miner.PendingBlock() 64 return block.Header(), nil 65 } 66 // Otherwise resolve and return the block 67 if blockNr == rpc.LatestBlockNumber { 68 return b.eth.blockchain.CurrentBlock().Header(), nil 69 } 70 return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil 71 } 72 73 func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 74 return b.eth.blockchain.GetHeaderByHash(hash), nil 75 } 76 77 func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 78 // Pending block is only known by the miner 79 if blockNr == rpc.PendingBlockNumber { 80 block := b.eth.miner.PendingBlock() 81 if block != nil { 82 return block, nil 83 } 84 return b.eth.blockchain.CurrentBlock(), nil 85 } 86 // Otherwise resolve and return the block 87 if blockNr == rpc.LatestBlockNumber { 88 return b.eth.blockchain.CurrentBlock(), nil 89 } 90 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 91 } 92 93 func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 94 // Pending state is only known by the miner 95 if blockNr == rpc.PendingBlockNumber { 96 block, state := b.eth.miner.Pending() 97 return state, block.Header(), nil 98 } 99 // Otherwise resolve the block number and return its state 100 header, err := b.HeaderByNumber(ctx, blockNr) 101 if header == nil || err != nil { 102 return nil, nil, err 103 } 104 stateDb, err := b.eth.BlockChain().StateAt(header.Root, header.MixDigest) 105 return stateDb, header, err 106 } 107 108 func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { 109 return b.eth.blockchain.GetBlockByHash(hash), nil 110 } 111 112 func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 113 if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil { 114 return rawdb.ReadReceipts(b.eth.chainDb, hash, *number), nil 115 } 116 return nil, nil 117 } 118 119 func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { 120 number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash) 121 if number == nil { 122 return nil, nil 123 } 124 receipts := rawdb.ReadReceipts(b.eth.chainDb, hash, *number) 125 if receipts == nil { 126 return nil, nil 127 } 128 logs := make([][]*types.Log, len(receipts)) 129 for i, receipt := range receipts { 130 logs[i] = receipt.Logs 131 } 132 return logs, nil 133 } 134 135 func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int { 136 return b.eth.blockchain.GetTdByHash(blockHash) 137 } 138 139 func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) { 140 state.SetBalance(msg.From(), common.SystemAssetID, math.MaxBig256) 141 vmError := func() error { return nil } 142 143 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 144 return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil 145 } 146 147 func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 148 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 149 } 150 151 func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 152 return b.eth.BlockChain().SubscribeChainEvent(ch) 153 } 154 155 func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 156 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 157 } 158 159 func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 160 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 161 } 162 163 func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 164 return b.eth.BlockChain().SubscribeLogsEvent(ch) 165 } 166 167 func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 168 return b.eth.txPool.AddLocal(signedTx) 169 } 170 171 func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) { 172 pending, err := b.eth.txPool.Pending() 173 if err != nil { 174 return nil, err 175 } 176 var txs types.Transactions 177 for _, batch := range pending { 178 txs = append(txs, batch...) 179 } 180 return txs, nil 181 } 182 183 func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 184 return b.eth.txPool.Get(hash) 185 } 186 187 func (b *EthAPIBackend) GetPoolTransactionByPredicate(predicate func(*types.Transaction) bool) *types.Transaction { 188 return b.eth.txPool.GetByPredicate(predicate) 189 } 190 191 func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 192 return b.eth.txPool.State().GetNonce(addr), nil 193 } 194 195 func (b *EthAPIBackend) Stats() (pending int, queued int) { 196 return b.eth.txPool.Stats() 197 } 198 199 func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 200 return b.eth.TxPool().Content() 201 } 202 203 func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 204 return b.eth.TxPool().SubscribeNewTxsEvent(ch) 205 } 206 207 func (b *EthAPIBackend) Downloader() *downloader.Downloader { 208 return b.eth.Downloader() 209 } 210 211 func (b *EthAPIBackend) ProtocolVersion() int { 212 return b.eth.EthVersion() 213 } 214 215 func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 216 return b.gpo.SuggestPrice(ctx) 217 } 218 219 func (b *EthAPIBackend) ChainDb() ethdb.Database { 220 return b.eth.ChainDb() 221 } 222 223 func (b *EthAPIBackend) EventMux() *event.TypeMux { 224 return b.eth.EventMux() 225 } 226 227 func (b *EthAPIBackend) AccountManager() *accounts.Manager { 228 return b.eth.AccountManager() 229 } 230 231 func (b *EthAPIBackend) BloomStatus() (uint64, uint64) { 232 sections, _, _ := b.eth.bloomIndexer.Sections() 233 return params.BloomBitsBlocks, sections 234 } 235 236 func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 237 for i := 0; i < bloomFilterThreads; i++ { 238 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 239 } 240 } 241 242 func (b *EthAPIBackend) IsMining() bool { 243 return b.eth.IsMining() 244 } 245 246 func (b *EthAPIBackend) Coinbase() (common.Address, error) { 247 return b.eth.Etherbase() 248 }