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