github.com/bhs-gq/quorum-hotstuff@v21.1.0+incompatible/light/lightchain.go (about) 1 // Copyright 2016 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 light implements on-demand retrieval capable state and chain objects 18 // for the Ethereum Light Client. 19 package light 20 21 import ( 22 "context" 23 "errors" 24 "math/big" 25 "sync" 26 "sync/atomic" 27 "time" 28 29 "github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto" 30 31 "github.com/ethereum/go-ethereum/common" 32 "github.com/ethereum/go-ethereum/consensus" 33 "github.com/ethereum/go-ethereum/core" 34 "github.com/ethereum/go-ethereum/core/rawdb" 35 "github.com/ethereum/go-ethereum/core/state" 36 "github.com/ethereum/go-ethereum/core/types" 37 "github.com/ethereum/go-ethereum/ethdb" 38 "github.com/ethereum/go-ethereum/event" 39 "github.com/ethereum/go-ethereum/log" 40 "github.com/ethereum/go-ethereum/params" 41 "github.com/ethereum/go-ethereum/rlp" 42 lru "github.com/hashicorp/golang-lru" 43 ) 44 45 var ( 46 bodyCacheLimit = 256 47 blockCacheLimit = 256 48 ) 49 50 // LightChain represents a canonical chain that by default only handles block 51 // headers, downloading block bodies and receipts on demand through an ODR 52 // interface. It only does header validation during chain insertion. 53 type LightChain struct { 54 hc *core.HeaderChain 55 indexerConfig *IndexerConfig 56 chainDb ethdb.Database 57 engine consensus.Engine 58 odr OdrBackend 59 chainFeed event.Feed 60 chainSideFeed event.Feed 61 chainHeadFeed event.Feed 62 scope event.SubscriptionScope 63 genesisBlock *types.Block 64 65 bodyCache *lru.Cache // Cache for the most recent block bodies 66 bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format 67 blockCache *lru.Cache // Cache for the most recent entire blocks 68 69 chainmu sync.RWMutex // protects header inserts 70 quit chan struct{} 71 wg sync.WaitGroup 72 73 // Atomic boolean switches: 74 running int32 // whether LightChain is running or stopped 75 procInterrupt int32 // interrupts chain insert 76 disableCheckFreq int32 // disables header verification 77 78 // Quorum 79 isMultitenant bool 80 } 81 82 // NewLightChain returns a fully initialised light chain using information 83 // available in the database. It initialises the default Ethereum header 84 // validator. 85 func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.Engine, checkpoint *params.TrustedCheckpoint) (*LightChain, error) { 86 bodyCache, _ := lru.New(bodyCacheLimit) 87 bodyRLPCache, _ := lru.New(bodyCacheLimit) 88 blockCache, _ := lru.New(blockCacheLimit) 89 90 bc := &LightChain{ 91 chainDb: odr.Database(), 92 indexerConfig: odr.IndexerConfig(), 93 odr: odr, 94 quit: make(chan struct{}), 95 bodyCache: bodyCache, 96 bodyRLPCache: bodyRLPCache, 97 blockCache: blockCache, 98 engine: engine, 99 } 100 var err error 101 bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.engine, bc.getProcInterrupt) 102 if err != nil { 103 return nil, err 104 } 105 bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0) 106 if bc.genesisBlock == nil { 107 return nil, core.ErrNoGenesis 108 } 109 if checkpoint != nil { 110 bc.AddTrustedCheckpoint(checkpoint) 111 } 112 if err := bc.loadLastState(); err != nil { 113 return nil, err 114 } 115 // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain 116 for hash := range core.BadHashes { 117 if header := bc.GetHeaderByHash(hash); header != nil { 118 log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash) 119 bc.SetHead(header.Number.Uint64() - 1) 120 log.Error("Chain rewind was successful, resuming normal operation") 121 } 122 } 123 return bc, nil 124 } 125 126 func NewMultitenantLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.Engine, checkpoint *params.TrustedCheckpoint) (*LightChain, error) { 127 bc, err := NewLightChain(odr, config, engine, checkpoint) 128 if err != nil { 129 return nil, err 130 } 131 bc.isMultitenant = true 132 return bc, nil 133 } 134 135 // AddTrustedCheckpoint adds a trusted checkpoint to the blockchain 136 func (lc *LightChain) AddTrustedCheckpoint(cp *params.TrustedCheckpoint) { 137 if lc.odr.ChtIndexer() != nil { 138 StoreChtRoot(lc.chainDb, cp.SectionIndex, cp.SectionHead, cp.CHTRoot) 139 lc.odr.ChtIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead) 140 } 141 if lc.odr.BloomTrieIndexer() != nil { 142 StoreBloomTrieRoot(lc.chainDb, cp.SectionIndex, cp.SectionHead, cp.BloomRoot) 143 lc.odr.BloomTrieIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead) 144 } 145 if lc.odr.BloomIndexer() != nil { 146 lc.odr.BloomIndexer().AddCheckpoint(cp.SectionIndex, cp.SectionHead) 147 } 148 log.Info("Added trusted checkpoint", "block", (cp.SectionIndex+1)*lc.indexerConfig.ChtSize-1, "hash", cp.SectionHead) 149 } 150 151 func (lc *LightChain) getProcInterrupt() bool { 152 return atomic.LoadInt32(&lc.procInterrupt) == 1 153 } 154 155 // Odr returns the ODR backend of the chain 156 func (lc *LightChain) Odr() OdrBackend { 157 return lc.odr 158 } 159 160 // loadLastState loads the last known chain state from the database. This method 161 // assumes that the chain manager mutex is held. 162 func (lc *LightChain) loadLastState() error { 163 if head := rawdb.ReadHeadHeaderHash(lc.chainDb); head == (common.Hash{}) { 164 // Corrupt or empty database, init from scratch 165 lc.Reset() 166 } else { 167 if header := lc.GetHeaderByHash(head); header != nil { 168 lc.hc.SetCurrentHeader(header) 169 } 170 } 171 172 // Issue a status log and return 173 header := lc.hc.CurrentHeader() 174 headerTd := lc.GetTd(header.Hash(), header.Number.Uint64()) 175 log.Info("Loaded most recent local header", "number", header.Number, "hash", header.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(header.Time), 0))) 176 177 return nil 178 } 179 180 // SetHead rewinds the local chain to a new head. Everything above the new 181 // head will be deleted and the new one set. 182 func (lc *LightChain) SetHead(head uint64) error { 183 lc.chainmu.Lock() 184 defer lc.chainmu.Unlock() 185 186 lc.hc.SetHead(head, nil, nil) 187 return lc.loadLastState() 188 } 189 190 // GasLimit returns the gas limit of the current HEAD block. 191 func (lc *LightChain) GasLimit() uint64 { 192 return lc.hc.CurrentHeader().GasLimit 193 } 194 195 // Reset purges the entire blockchain, restoring it to its genesis state. 196 func (lc *LightChain) Reset() { 197 lc.ResetWithGenesisBlock(lc.genesisBlock) 198 } 199 200 // ResetWithGenesisBlock purges the entire blockchain, restoring it to the 201 // specified genesis state. 202 func (lc *LightChain) ResetWithGenesisBlock(genesis *types.Block) { 203 // Dump the entire block chain and purge the caches 204 lc.SetHead(0) 205 206 lc.chainmu.Lock() 207 defer lc.chainmu.Unlock() 208 209 // Prepare the genesis block and reinitialise the chain 210 rawdb.WriteTd(lc.chainDb, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()) 211 rawdb.WriteBlock(lc.chainDb, genesis) 212 213 lc.genesisBlock = genesis 214 lc.hc.SetGenesis(lc.genesisBlock.Header()) 215 lc.hc.SetCurrentHeader(lc.genesisBlock.Header()) 216 } 217 218 // Accessors 219 220 // Engine retrieves the light chain's consensus engine. 221 func (lc *LightChain) Engine() consensus.Engine { return lc.engine } 222 223 // Genesis returns the genesis block 224 func (lc *LightChain) Genesis() *types.Block { 225 return lc.genesisBlock 226 } 227 228 func (lc *LightChain) StateCache() (state.Database, state.Database) { 229 panic("not implemented") 230 } 231 232 // GetBody retrieves a block body (transactions and uncles) from the database 233 // or ODR service by hash, caching it if found. 234 func (lc *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error) { 235 // Short circuit if the body's already in the cache, retrieve otherwise 236 if cached, ok := lc.bodyCache.Get(hash); ok { 237 body := cached.(*types.Body) 238 return body, nil 239 } 240 number := lc.hc.GetBlockNumber(hash) 241 if number == nil { 242 return nil, errors.New("unknown block") 243 } 244 body, err := GetBody(ctx, lc.odr, hash, *number) 245 if err != nil { 246 return nil, err 247 } 248 // Cache the found body for next time and return 249 lc.bodyCache.Add(hash, body) 250 return body, nil 251 } 252 253 // GetBodyRLP retrieves a block body in RLP encoding from the database or 254 // ODR service by hash, caching it if found. 255 func (lc *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error) { 256 // Short circuit if the body's already in the cache, retrieve otherwise 257 if cached, ok := lc.bodyRLPCache.Get(hash); ok { 258 return cached.(rlp.RawValue), nil 259 } 260 number := lc.hc.GetBlockNumber(hash) 261 if number == nil { 262 return nil, errors.New("unknown block") 263 } 264 body, err := GetBodyRLP(ctx, lc.odr, hash, *number) 265 if err != nil { 266 return nil, err 267 } 268 // Cache the found body for next time and return 269 lc.bodyRLPCache.Add(hash, body) 270 return body, nil 271 } 272 273 // HasBlock checks if a block is fully present in the database or not, caching 274 // it if present. 275 func (lc *LightChain) HasBlock(hash common.Hash, number uint64) bool { 276 blk, _ := lc.GetBlock(NoOdr, hash, number) 277 return blk != nil 278 } 279 280 // GetBlock retrieves a block from the database or ODR service by hash and number, 281 // caching it if found. 282 func (lc *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*types.Block, error) { 283 // Short circuit if the block's already in the cache, retrieve otherwise 284 if block, ok := lc.blockCache.Get(hash); ok { 285 return block.(*types.Block), nil 286 } 287 block, err := GetBlock(ctx, lc.odr, hash, number) 288 if err != nil { 289 return nil, err 290 } 291 // Cache the found block for next time and return 292 lc.blockCache.Add(block.Hash(), block) 293 return block, nil 294 } 295 296 // GetBlockByHash retrieves a block from the database or ODR service by hash, 297 // caching it if found. 298 func (lc *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { 299 number := lc.hc.GetBlockNumber(hash) 300 if number == nil { 301 return nil, errors.New("unknown block") 302 } 303 return lc.GetBlock(ctx, hash, *number) 304 } 305 306 // GetBlockByNumber retrieves a block from the database or ODR service by 307 // number, caching it (associated with its hash) if found. 308 func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) { 309 hash, err := GetCanonicalHash(ctx, lc.odr, number) 310 if hash == (common.Hash{}) || err != nil { 311 return nil, err 312 } 313 return lc.GetBlock(ctx, hash, number) 314 } 315 316 // Stop stops the blockchain service. If any imports are currently in progress 317 // it will abort them using the procInterrupt. 318 func (lc *LightChain) Stop() { 319 if !atomic.CompareAndSwapInt32(&lc.running, 0, 1) { 320 return 321 } 322 close(lc.quit) 323 atomic.StoreInt32(&lc.procInterrupt, 1) 324 325 lc.wg.Wait() 326 log.Info("Blockchain manager stopped") 327 } 328 329 // Rollback is designed to remove a chain of links from the database that aren't 330 // certain enough to be valid. 331 func (lc *LightChain) Rollback(chain []common.Hash) { 332 lc.chainmu.Lock() 333 defer lc.chainmu.Unlock() 334 335 for i := len(chain) - 1; i >= 0; i-- { 336 hash := chain[i] 337 338 if head := lc.hc.CurrentHeader(); head.Hash() == hash { 339 lc.hc.SetCurrentHeader(lc.GetHeader(head.ParentHash, head.Number.Uint64()-1)) 340 } 341 } 342 } 343 344 // postChainEvents iterates over the events generated by a chain insertion and 345 // posts them into the event feed. 346 func (lc *LightChain) postChainEvents(events []interface{}) { 347 for _, event := range events { 348 switch ev := event.(type) { 349 case core.ChainEvent: 350 if lc.CurrentHeader().Hash() == ev.Hash { 351 lc.chainHeadFeed.Send(core.ChainHeadEvent{Block: ev.Block}) 352 } 353 lc.chainFeed.Send(ev) 354 case core.ChainSideEvent: 355 lc.chainSideFeed.Send(ev) 356 } 357 } 358 } 359 360 // InsertHeaderChain attempts to insert the given header chain in to the local 361 // chain, possibly creating a reorg. If an error is returned, it will return the 362 // index number of the failing header as well an error describing what went wrong. 363 // 364 // The verify parameter can be used to fine tune whether nonce verification 365 // should be done or not. The reason behind the optional check is because some 366 // of the header retrieval mechanisms already need to verfy nonces, as well as 367 // because nonces can be verified sparsely, not needing to check each. 368 // 369 // In the case of a light chain, InsertHeaderChain also creates and posts light 370 // chain events when necessary. 371 func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { 372 if atomic.LoadInt32(&lc.disableCheckFreq) == 1 { 373 checkFreq = 0 374 } 375 start := time.Now() 376 if i, err := lc.hc.ValidateHeaderChain(chain, checkFreq); err != nil { 377 return i, err 378 } 379 380 // Make sure only one thread manipulates the chain at once 381 lc.chainmu.Lock() 382 defer lc.chainmu.Unlock() 383 384 lc.wg.Add(1) 385 defer lc.wg.Done() 386 387 var events []interface{} 388 whFunc := func(header *types.Header) error { 389 status, err := lc.hc.WriteHeader(header) 390 391 switch status { 392 case core.CanonStatTy: 393 log.Debug("Inserted new header", "number", header.Number, "hash", header.Hash()) 394 events = append(events, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: header.Hash()}) 395 396 case core.SideStatTy: 397 log.Debug("Inserted forked header", "number", header.Number, "hash", header.Hash()) 398 events = append(events, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)}) 399 } 400 return err 401 } 402 i, err := lc.hc.InsertHeaderChain(chain, whFunc, start) 403 lc.postChainEvents(events) 404 return i, err 405 } 406 407 // CurrentHeader retrieves the current head header of the canonical chain. The 408 // header is retrieved from the HeaderChain's internal cache. 409 func (lc *LightChain) CurrentHeader() *types.Header { 410 return lc.hc.CurrentHeader() 411 } 412 413 // GetTd retrieves a block's total difficulty in the canonical chain from the 414 // database by hash and number, caching it if found. 415 func (lc *LightChain) GetTd(hash common.Hash, number uint64) *big.Int { 416 return lc.hc.GetTd(hash, number) 417 } 418 419 // GetTdByHash retrieves a block's total difficulty in the canonical chain from the 420 // database by hash, caching it if found. 421 func (lc *LightChain) GetTdByHash(hash common.Hash) *big.Int { 422 return lc.hc.GetTdByHash(hash) 423 } 424 425 // GetHeader retrieves a block header from the database by hash and number, 426 // caching it if found. 427 func (lc *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header { 428 return lc.hc.GetHeader(hash, number) 429 } 430 431 // GetHeaderByHash retrieves a block header from the database by hash, caching it if 432 // found. 433 func (lc *LightChain) GetHeaderByHash(hash common.Hash) *types.Header { 434 return lc.hc.GetHeaderByHash(hash) 435 } 436 437 // HasHeader checks if a block header is present in the database or not, caching 438 // it if present. 439 func (lc *LightChain) HasHeader(hash common.Hash, number uint64) bool { 440 return lc.hc.HasHeader(hash, number) 441 } 442 443 // GetCanonicalHash returns the canonical hash for a given block number 444 func (bc *LightChain) GetCanonicalHash(number uint64) common.Hash { 445 return bc.hc.GetCanonicalHash(number) 446 } 447 448 // GetBlockHashesFromHash retrieves a number of block hashes starting at a given 449 // hash, fetching towards the genesis block. 450 func (lc *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { 451 return lc.hc.GetBlockHashesFromHash(hash, max) 452 } 453 454 // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or 455 // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the 456 // number of blocks to be individually checked before we reach the canonical chain. 457 // 458 // Note: ancestor == 0 returns the same block, 1 returns its parent and so on. 459 func (lc *LightChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) { 460 return lc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical) 461 } 462 463 // GetHeaderByNumber retrieves a block header from the database by number, 464 // caching it (associated with its hash) if found. 465 func (lc *LightChain) GetHeaderByNumber(number uint64) *types.Header { 466 return lc.hc.GetHeaderByNumber(number) 467 } 468 469 // GetHeaderByNumberOdr retrieves a block header from the database or network 470 // by number, caching it (associated with its hash) if found. 471 func (lc *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.Header, error) { 472 if header := lc.hc.GetHeaderByNumber(number); header != nil { 473 return header, nil 474 } 475 return GetHeaderByNumber(ctx, lc.odr, number) 476 } 477 478 // Config retrieves the header chain's chain configuration. 479 func (lc *LightChain) Config() *params.ChainConfig { return lc.hc.Config() } 480 481 // SyncCheckpoint fetches the checkpoint point block header according to 482 // the checkpoint provided by the remote peer. 483 // 484 // Note if we are running the clique, fetches the last epoch snapshot header 485 // which covered by checkpoint. 486 func (lc *LightChain) SyncCheckpoint(ctx context.Context, checkpoint *params.TrustedCheckpoint) bool { 487 // Ensure the remote checkpoint head is ahead of us 488 head := lc.CurrentHeader().Number.Uint64() 489 490 latest := (checkpoint.SectionIndex+1)*lc.indexerConfig.ChtSize - 1 491 if clique := lc.hc.Config().Clique; clique != nil { 492 latest -= latest % clique.Epoch // epoch snapshot for clique 493 } 494 if head >= latest { 495 return true 496 } 497 // Retrieve the latest useful header and update to it 498 if header, err := GetHeaderByNumber(ctx, lc.odr, latest); header != nil && err == nil { 499 lc.chainmu.Lock() 500 defer lc.chainmu.Unlock() 501 502 // Ensure the chain didn't move past the latest block while retrieving it 503 if lc.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() { 504 log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash(), "age", common.PrettyAge(time.Unix(int64(header.Time), 0))) 505 lc.hc.SetCurrentHeader(header) 506 } 507 return true 508 } 509 return false 510 } 511 512 // LockChain locks the chain mutex for reading so that multiple canonical hashes can be 513 // retrieved while it is guaranteed that they belong to the same version of the chain 514 func (lc *LightChain) LockChain() { 515 lc.chainmu.RLock() 516 } 517 518 // UnlockChain unlocks the chain mutex 519 func (lc *LightChain) UnlockChain() { 520 lc.chainmu.RUnlock() 521 } 522 523 // SubscribeChainEvent registers a subscription of ChainEvent. 524 func (lc *LightChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 525 return lc.scope.Track(lc.chainFeed.Subscribe(ch)) 526 } 527 528 // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent. 529 func (lc *LightChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 530 return lc.scope.Track(lc.chainHeadFeed.Subscribe(ch)) 531 } 532 533 // SubscribeChainSideEvent registers a subscription of ChainSideEvent. 534 func (lc *LightChain) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription { 535 return lc.scope.Track(lc.chainSideFeed.Subscribe(ch)) 536 } 537 538 // SubscribeLogsEvent implements the interface of filters.Backend 539 // LightChain does not send logs events, so return an empty subscription. 540 func (lc *LightChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 541 return lc.scope.Track(new(event.Feed).Subscribe(ch)) 542 } 543 544 // SubscribeRemovedLogsEvent implements the interface of filters.Backend 545 // LightChain does not send core.RemovedLogsEvent, so return an empty subscription. 546 func (lc *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 547 return lc.scope.Track(new(event.Feed).Subscribe(ch)) 548 } 549 550 // DisableCheckFreq disables header validation. This is used for ultralight mode. 551 func (lc *LightChain) DisableCheckFreq() { 552 atomic.StoreInt32(&lc.disableCheckFreq, 1) 553 } 554 555 // EnableCheckFreq enables header validation. 556 func (lc *LightChain) EnableCheckFreq() { 557 atomic.StoreInt32(&lc.disableCheckFreq, 0) 558 } 559 560 func (lc *LightChain) SupportsMultitenancy(context.Context) (*proto.PreAuthenticatedAuthenticationToken, bool) { 561 return nil, lc.isMultitenant 562 }