github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/api_backend.go (about) 1 // Copyright 2015 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package eth 18 19 import ( 20 "context" 21 "math/big" 22 23 "errors" 24 25 "github.com/SmartMeshFoundation/Spectrum/accounts" 26 "github.com/SmartMeshFoundation/Spectrum/common" 27 "github.com/SmartMeshFoundation/Spectrum/common/math" 28 "github.com/SmartMeshFoundation/Spectrum/core" 29 "github.com/SmartMeshFoundation/Spectrum/core/bloombits" 30 "github.com/SmartMeshFoundation/Spectrum/core/state" 31 "github.com/SmartMeshFoundation/Spectrum/core/types" 32 "github.com/SmartMeshFoundation/Spectrum/core/vm" 33 "github.com/SmartMeshFoundation/Spectrum/eth/downloader" 34 "github.com/SmartMeshFoundation/Spectrum/eth/gasprice" 35 "github.com/SmartMeshFoundation/Spectrum/ethdb" 36 "github.com/SmartMeshFoundation/Spectrum/event" 37 "github.com/SmartMeshFoundation/Spectrum/log" 38 "github.com/SmartMeshFoundation/Spectrum/params" 39 "github.com/SmartMeshFoundation/Spectrum/rpc" 40 ) 41 42 // EthApiBackend implements ethapi.Backend for full nodes 43 type EthApiBackend struct { 44 eth *Ethereum 45 gpo *gasprice.Oracle 46 } 47 48 func (b *EthApiBackend) ChainConfig() *params.ChainConfig { 49 return b.eth.chainConfig 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, blockNr rpc.BlockNumber) (*types.Header, error) { 62 // Pending block is only known by the miner 63 if blockNr == rpc.PendingBlockNumber { 64 block := b.eth.miner.PendingBlock() 65 return block.Header(), nil 66 } 67 // Otherwise resolve and return the block 68 if blockNr == rpc.LatestBlockNumber { 69 return b.eth.blockchain.CurrentBlock().Header(), nil 70 } 71 return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil 72 } 73 74 func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { 75 // Pending block is only known by the miner 76 if blockNr == rpc.PendingBlockNumber { 77 block := b.eth.miner.PendingBlock() 78 return block, nil 79 } 80 // Otherwise resolve and return the block 81 if blockNr == rpc.LatestBlockNumber { 82 return b.eth.blockchain.CurrentBlock(), nil 83 } 84 return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil 85 } 86 87 // add by liangc : only for execute contract by hash 88 func (b *EthApiBackend) StateAndHeaderByHash(ctx context.Context, hash *common.Hash) (*state.StateDB, *types.Header, error) { 89 // Pending state is only known by the miner 90 // Otherwise resolve the block number and return its state 91 blk, err := b.GetBlock(ctx, *hash) 92 if err != nil { 93 return nil, nil, err 94 } 95 if blk == nil { 96 log.Debug("EthApiBackend.StateAndHeaderByHash", "err", "block_not_found", "hash", hash.Hex()) 97 return nil, nil, errors.New("block_not_found") 98 } 99 header := blk.Header() 100 log.Debug("EthApiBackend.StateAndHeaderByHash #>", "hex", hash.Hex()) 101 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 102 return stateDb, header, err 103 } 104 func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { 105 // Pending state is only known by the miner 106 if blockNr == rpc.PendingBlockNumber { 107 block, state := b.eth.miner.Pending() 108 return state, block.Header(), nil 109 } 110 // Otherwise resolve the block number and return its state 111 header, err := b.HeaderByNumber(ctx, blockNr) 112 log.Debug("EthApiBackend.StateAndHeaderByNumber #>", "number", blockNr.Int64()) 113 114 if header == nil || err != nil { 115 return nil, nil, err 116 } 117 stateDb, err := b.eth.BlockChain().StateAt(header.Root) 118 return stateDb, header, err 119 } 120 121 func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) { 122 return b.eth.blockchain.GetBlockByHash(blockHash), nil 123 } 124 125 func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) { 126 return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil 127 } 128 129 func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int { 130 return b.eth.blockchain.GetTdByHash(blockHash) 131 } 132 133 func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) { 134 state.SetBalance(msg.From(), math.MaxBig256) 135 vmError := func() error { return nil } 136 137 context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil) 138 return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil 139 } 140 141 func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 142 return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch) 143 } 144 145 func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 146 return b.eth.BlockChain().SubscribeChainEvent(ch) 147 } 148 149 func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 150 return b.eth.BlockChain().SubscribeChainHeadEvent(ch) 151 } 152 153 func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 154 return b.eth.BlockChain().SubscribeChainSideEvent(ch) 155 } 156 157 func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 158 return b.eth.BlockChain().SubscribeLogsEvent(ch) 159 } 160 161 func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { 162 return b.eth.txPool.AddLocal(signedTx) 163 } 164 165 func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) { 166 pending, err := b.eth.txPool.Pending() 167 if err != nil { 168 return nil, err 169 } 170 var txs types.Transactions 171 for _, batch := range pending { 172 txs = append(txs, batch...) 173 } 174 return txs, nil 175 } 176 177 func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction { 178 return b.eth.txPool.Get(hash) 179 } 180 181 func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { 182 return b.eth.txPool.State().GetNonce(addr), nil 183 } 184 185 func (b *EthApiBackend) Stats() (pending int, queued int) { 186 return b.eth.txPool.Stats() 187 } 188 189 func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 190 return b.eth.TxPool().Content() 191 } 192 193 func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription { 194 return b.eth.TxPool().SubscribeTxPreEvent(ch) 195 } 196 197 func (b *EthApiBackend) Downloader() *downloader.Downloader { 198 return b.eth.Downloader() 199 } 200 201 func (b *EthApiBackend) ProtocolVersion() int { 202 return b.eth.EthVersion() 203 } 204 205 func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { 206 return b.gpo.SuggestPrice(ctx) 207 } 208 209 func (b *EthApiBackend) ChainDb() ethdb.Database { 210 return b.eth.ChainDb() 211 } 212 213 func (b *EthApiBackend) EventMux() *event.TypeMux { 214 return b.eth.EventMux() 215 } 216 217 func (b *EthApiBackend) AccountManager() *accounts.Manager { 218 return b.eth.AccountManager() 219 } 220 221 func (b *EthApiBackend) BloomStatus() (uint64, uint64) { 222 sections, _, _ := b.eth.bloomIndexer.Sections() 223 return params.BloomBitsBlocks, sections 224 } 225 226 func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 227 for i := 0; i < bloomFilterThreads; i++ { 228 go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests) 229 } 230 }