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