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