github.com/theQRL/go-zond@v0.1.1/core/blockchain_reader.go (about) 1 // Copyright 2021 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 core 18 19 import ( 20 "math/big" 21 22 "github.com/theQRL/go-zond/common" 23 "github.com/theQRL/go-zond/consensus" 24 "github.com/theQRL/go-zond/core/rawdb" 25 "github.com/theQRL/go-zond/core/state" 26 "github.com/theQRL/go-zond/core/state/snapshot" 27 "github.com/theQRL/go-zond/core/types" 28 "github.com/theQRL/go-zond/core/vm" 29 "github.com/theQRL/go-zond/event" 30 "github.com/theQRL/go-zond/params" 31 "github.com/theQRL/go-zond/rlp" 32 "github.com/theQRL/go-zond/trie" 33 ) 34 35 // CurrentHeader retrieves the current head header of the canonical chain. The 36 // header is retrieved from the HeaderChain's internal cache. 37 func (bc *BlockChain) CurrentHeader() *types.Header { 38 return bc.hc.CurrentHeader() 39 } 40 41 // CurrentBlock retrieves the current head block of the canonical chain. The 42 // block is retrieved from the blockchain's internal cache. 43 func (bc *BlockChain) CurrentBlock() *types.Header { 44 return bc.currentBlock.Load() 45 } 46 47 // CurrentSnapBlock retrieves the current snap-sync head block of the canonical 48 // chain. The block is retrieved from the blockchain's internal cache. 49 func (bc *BlockChain) CurrentSnapBlock() *types.Header { 50 return bc.currentSnapBlock.Load() 51 } 52 53 // CurrentFinalBlock retrieves the current finalized block of the canonical 54 // chain. The block is retrieved from the blockchain's internal cache. 55 func (bc *BlockChain) CurrentFinalBlock() *types.Header { 56 return bc.currentFinalBlock.Load() 57 } 58 59 // CurrentSafeBlock retrieves the current safe block of the canonical 60 // chain. The block is retrieved from the blockchain's internal cache. 61 func (bc *BlockChain) CurrentSafeBlock() *types.Header { 62 return bc.currentSafeBlock.Load() 63 } 64 65 // HasHeader checks if a block header is present in the database or not, caching 66 // it if present. 67 func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool { 68 return bc.hc.HasHeader(hash, number) 69 } 70 71 // GetHeader retrieves a block header from the database by hash and number, 72 // caching it if found. 73 func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header { 74 return bc.hc.GetHeader(hash, number) 75 } 76 77 // GetHeaderByHash retrieves a block header from the database by hash, caching it if 78 // found. 79 func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header { 80 return bc.hc.GetHeaderByHash(hash) 81 } 82 83 // GetHeaderByNumber retrieves a block header from the database by number, 84 // caching it (associated with its hash) if found. 85 func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header { 86 return bc.hc.GetHeaderByNumber(number) 87 } 88 89 // GetHeadersFrom returns a contiguous segment of headers, in rlp-form, going 90 // backwards from the given number. 91 func (bc *BlockChain) GetHeadersFrom(number, count uint64) []rlp.RawValue { 92 return bc.hc.GetHeadersFrom(number, count) 93 } 94 95 // GetBody retrieves a block body (transactions and uncles) from the database by 96 // hash, caching it if found. 97 func (bc *BlockChain) GetBody(hash common.Hash) *types.Body { 98 // Short circuit if the body's already in the cache, retrieve otherwise 99 if cached, ok := bc.bodyCache.Get(hash); ok { 100 return cached 101 } 102 number := bc.hc.GetBlockNumber(hash) 103 if number == nil { 104 return nil 105 } 106 body := rawdb.ReadBody(bc.db, hash, *number) 107 if body == nil { 108 return nil 109 } 110 // Cache the found body for next time and return 111 bc.bodyCache.Add(hash, body) 112 return body 113 } 114 115 // GetBodyRLP retrieves a block body in RLP encoding from the database by hash, 116 // caching it if found. 117 func (bc *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue { 118 // Short circuit if the body's already in the cache, retrieve otherwise 119 if cached, ok := bc.bodyRLPCache.Get(hash); ok { 120 return cached 121 } 122 number := bc.hc.GetBlockNumber(hash) 123 if number == nil { 124 return nil 125 } 126 body := rawdb.ReadBodyRLP(bc.db, hash, *number) 127 if len(body) == 0 { 128 return nil 129 } 130 // Cache the found body for next time and return 131 bc.bodyRLPCache.Add(hash, body) 132 return body 133 } 134 135 // HasBlock checks if a block is fully present in the database or not. 136 func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool { 137 if bc.blockCache.Contains(hash) { 138 return true 139 } 140 if !bc.HasHeader(hash, number) { 141 return false 142 } 143 return rawdb.HasBody(bc.db, hash, number) 144 } 145 146 // HasFastBlock checks if a fast block is fully present in the database or not. 147 func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool { 148 if !bc.HasBlock(hash, number) { 149 return false 150 } 151 if bc.receiptsCache.Contains(hash) { 152 return true 153 } 154 return rawdb.HasReceipts(bc.db, hash, number) 155 } 156 157 // GetBlock retrieves a block from the database by hash and number, 158 // caching it if found. 159 func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { 160 // Short circuit if the block's already in the cache, retrieve otherwise 161 if block, ok := bc.blockCache.Get(hash); ok { 162 return block 163 } 164 block := rawdb.ReadBlock(bc.db, hash, number) 165 if block == nil { 166 return nil 167 } 168 // Cache the found block for next time and return 169 bc.blockCache.Add(block.Hash(), block) 170 return block 171 } 172 173 // GetBlockByHash retrieves a block from the database by hash, caching it if found. 174 func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { 175 number := bc.hc.GetBlockNumber(hash) 176 if number == nil { 177 return nil 178 } 179 return bc.GetBlock(hash, *number) 180 } 181 182 // GetBlockByNumber retrieves a block from the database by number, caching it 183 // (associated with its hash) if found. 184 func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { 185 hash := rawdb.ReadCanonicalHash(bc.db, number) 186 if hash == (common.Hash{}) { 187 return nil 188 } 189 return bc.GetBlock(hash, number) 190 } 191 192 // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors. 193 // [deprecated by zond/62] 194 func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) { 195 number := bc.hc.GetBlockNumber(hash) 196 if number == nil { 197 return nil 198 } 199 for i := 0; i < n; i++ { 200 block := bc.GetBlock(hash, *number) 201 if block == nil { 202 break 203 } 204 blocks = append(blocks, block) 205 hash = block.ParentHash() 206 *number-- 207 } 208 return 209 } 210 211 // GetReceiptsByHash retrieves the receipts for all transactions in a given block. 212 func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { 213 if receipts, ok := bc.receiptsCache.Get(hash); ok { 214 return receipts 215 } 216 number := rawdb.ReadHeaderNumber(bc.db, hash) 217 if number == nil { 218 return nil 219 } 220 header := bc.GetHeader(hash, *number) 221 if header == nil { 222 return nil 223 } 224 receipts := rawdb.ReadReceipts(bc.db, hash, *number, header.Time, bc.chainConfig) 225 if receipts == nil { 226 return nil 227 } 228 bc.receiptsCache.Add(hash, receipts) 229 return receipts 230 } 231 232 // GetUnclesInChain retrieves all the uncles from a given block backwards until 233 // a specific distance is reached. 234 func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header { 235 uncles := []*types.Header{} 236 for i := 0; block != nil && i < length; i++ { 237 uncles = append(uncles, block.Uncles()...) 238 block = bc.GetBlock(block.ParentHash(), block.NumberU64()-1) 239 } 240 return uncles 241 } 242 243 // GetCanonicalHash returns the canonical hash for a given block number 244 func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash { 245 return bc.hc.GetCanonicalHash(number) 246 } 247 248 // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or 249 // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the 250 // number of blocks to be individually checked before we reach the canonical chain. 251 // 252 // Note: ancestor == 0 returns the same block, 1 returns its parent and so on. 253 func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) { 254 return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical) 255 } 256 257 // GetTransactionLookup retrieves the lookup associate with the given transaction 258 // hash from the cache or database. 259 func (bc *BlockChain) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry { 260 // Short circuit if the txlookup already in the cache, retrieve otherwise 261 if lookup, exist := bc.txLookupCache.Get(hash); exist { 262 return lookup 263 } 264 tx, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(bc.db, hash) 265 if tx == nil { 266 return nil 267 } 268 lookup := &rawdb.LegacyTxLookupEntry{BlockHash: blockHash, BlockIndex: blockNumber, Index: txIndex} 269 bc.txLookupCache.Add(hash, lookup) 270 return lookup 271 } 272 273 // GetTd retrieves a block's total difficulty in the canonical chain from the 274 // database by hash and number, caching it if found. 275 func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int { 276 return bc.hc.GetTd(hash, number) 277 } 278 279 // HasState checks if state trie is fully present in the database or not. 280 func (bc *BlockChain) HasState(hash common.Hash) bool { 281 _, err := bc.stateCache.OpenTrie(hash) 282 return err == nil 283 } 284 285 // HasBlockAndState checks if a block and associated state trie is fully present 286 // in the database or not, caching it if present. 287 func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool { 288 // Check first that the block itself is known 289 block := bc.GetBlock(hash, number) 290 if block == nil { 291 return false 292 } 293 return bc.HasState(block.Root()) 294 } 295 296 // stateRecoverable checks if the specified state is recoverable. 297 // Note, this function assumes the state is not present, because 298 // state is not treated as recoverable if it's available, thus 299 // false will be returned in this case. 300 func (bc *BlockChain) stateRecoverable(root common.Hash) bool { 301 if bc.triedb.Scheme() == rawdb.HashScheme { 302 return false 303 } 304 result, _ := bc.triedb.Recoverable(root) 305 return result 306 } 307 308 // ContractCodeWithPrefix retrieves a blob of data associated with a contract 309 // hash either from ephemeral in-memory cache, or from persistent storage. 310 // 311 // If the code doesn't exist in the in-memory cache, check the storage with 312 // new code scheme. 313 func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) ([]byte, error) { 314 type codeReader interface { 315 ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error) 316 } 317 // TODO(rjl493456442) The associated account address is also required 318 // in Verkle scheme. Fix it once snap-sync is supported for Verkle. 319 return bc.stateCache.(codeReader).ContractCodeWithPrefix(common.Address{}, hash) 320 } 321 322 // State returns a new mutable state based on the current HEAD block. 323 func (bc *BlockChain) State() (*state.StateDB, error) { 324 return bc.StateAt(bc.CurrentBlock().Root) 325 } 326 327 // StateAt returns a new mutable state based on a particular point in time. 328 func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { 329 return state.New(root, bc.stateCache, bc.snaps) 330 } 331 332 // Config retrieves the chain's fork configuration. 333 func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig } 334 335 // Engine retrieves the blockchain's consensus engine. 336 func (bc *BlockChain) Engine() consensus.Engine { return bc.engine } 337 338 // Snapshots returns the blockchain snapshot tree. 339 func (bc *BlockChain) Snapshots() *snapshot.Tree { 340 return bc.snaps 341 } 342 343 // Validator returns the current validator. 344 func (bc *BlockChain) Validator() Validator { 345 return bc.validator 346 } 347 348 // Processor returns the current processor. 349 func (bc *BlockChain) Processor() Processor { 350 return bc.processor 351 } 352 353 // StateCache returns the caching database underpinning the blockchain instance. 354 func (bc *BlockChain) StateCache() state.Database { 355 return bc.stateCache 356 } 357 358 // GasLimit returns the gas limit of the current HEAD block. 359 func (bc *BlockChain) GasLimit() uint64 { 360 return bc.CurrentBlock().GasLimit 361 } 362 363 // Genesis retrieves the chain's genesis block. 364 func (bc *BlockChain) Genesis() *types.Block { 365 return bc.genesisBlock 366 } 367 368 // GetVMConfig returns the block chain VM config. 369 func (bc *BlockChain) GetVMConfig() *vm.Config { 370 return &bc.vmConfig 371 } 372 373 // SetTxLookupLimit is responsible for updating the txlookup limit to the 374 // original one stored in db if the new mismatches with the old one. 375 func (bc *BlockChain) SetTxLookupLimit(limit uint64) { 376 bc.txLookupLimit = limit 377 } 378 379 // TxLookupLimit retrieves the txlookup limit used by blockchain to prune 380 // stale transaction indices. 381 func (bc *BlockChain) TxLookupLimit() uint64 { 382 return bc.txLookupLimit 383 } 384 385 // TrieDB retrieves the low level trie database used for data storage. 386 func (bc *BlockChain) TrieDB() *trie.Database { 387 return bc.triedb 388 } 389 390 // SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent. 391 func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription { 392 return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch)) 393 } 394 395 // SubscribeChainEvent registers a subscription of ChainEvent. 396 func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription { 397 return bc.scope.Track(bc.chainFeed.Subscribe(ch)) 398 } 399 400 // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent. 401 func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription { 402 return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch)) 403 } 404 405 // SubscribeChainSideEvent registers a subscription of ChainSideEvent. 406 func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription { 407 return bc.scope.Track(bc.chainSideFeed.Subscribe(ch)) 408 } 409 410 // SubscribeLogsEvent registers a subscription of []*types.Log. 411 func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 412 return bc.scope.Track(bc.logsFeed.Subscribe(ch)) 413 } 414 415 // SubscribeBlockProcessingEvent registers a subscription of bool where true means 416 // block processing has started while false means it has stopped. 417 func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription { 418 return bc.scope.Track(bc.blockProcFeed.Subscribe(ch)) 419 }