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