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