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