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