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