github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/core/headerchain.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-wtc 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-wtc 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 crand "crypto/rand" 21 "errors" 22 "fmt" 23 "math" 24 "math/big" 25 mrand "math/rand" 26 "time" 27 28 "github.com/wtc/go-wtc/common" 29 "github.com/wtc/go-wtc/consensus" 30 "github.com/wtc/go-wtc/core/state" 31 "github.com/wtc/go-wtc/core/types" 32 "github.com/wtc/go-wtc/wtcdb" 33 "github.com/wtc/go-wtc/log" 34 "github.com/wtc/go-wtc/params" 35 "github.com/hashicorp/golang-lru" 36 ) 37 38 const ( 39 headerCacheLimit = 512 40 tdCacheLimit = 1024 41 numberCacheLimit = 2048 42 ) 43 44 // HeaderChain implements the basic block header chain logic that is shared by 45 // core.BlockChain and light.LightChain. It is not usable in itself, only as 46 // a part of either structure. 47 // It is not thread safe either, the encapsulating chain structures should do 48 // the necessary mutex locking/unlocking. 49 type HeaderChain struct { 50 config *params.ChainConfig 51 52 chainDb wtcdb.Database 53 genesisHeader *types.Header 54 55 currentHeader *types.Header // Current head of the header chain (may be above the block chain!) 56 currentHeaderHash common.Hash // Hash of the current head of the header chain (prevent recomputing all the time) 57 58 stateCache state.Database // State database to reuse between imports (contains state cache) 59 headerCache *lru.Cache // Cache for the most recent block headers 60 tdCache *lru.Cache // Cache for the most recent block total difficulties 61 numberCache *lru.Cache // Cache for the most recent block numbers 62 63 procInterrupt func() bool 64 65 rand *mrand.Rand 66 engine consensus.Engine 67 } 68 69 // NewHeaderChain creates a new HeaderChain structure. 70 // getValidator should return the parent's validator 71 // procInterrupt points to the parent's interrupt semaphore 72 // wg points to the parent's shutdown wait group 73 func NewHeaderChain(chainDb wtcdb.Database, config *params.ChainConfig, engine consensus.Engine, procInterrupt func() bool) (*HeaderChain, error) { 74 headerCache, _ := lru.New(headerCacheLimit) 75 tdCache, _ := lru.New(tdCacheLimit) 76 numberCache, _ := lru.New(numberCacheLimit) 77 78 // Seed a fast but crypto originating random generator 79 seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) 80 if err != nil { 81 return nil, err 82 } 83 84 hc := &HeaderChain{ 85 config: config, 86 chainDb: chainDb, 87 stateCache: state.NewDatabase(chainDb), 88 headerCache: headerCache, 89 tdCache: tdCache, 90 numberCache: numberCache, 91 procInterrupt: procInterrupt, 92 rand: mrand.New(mrand.NewSource(seed.Int64())), 93 engine: engine, 94 } 95 96 hc.genesisHeader = hc.GetHeaderByNumber(0) 97 if hc.genesisHeader == nil { 98 return nil, ErrNoGenesis 99 } 100 101 hc.currentHeader = hc.genesisHeader 102 if head := GetHeadBlockHash(chainDb); head != (common.Hash{}) { 103 if chead := hc.GetHeaderByHash(head); chead != nil { 104 hc.currentHeader = chead 105 } 106 } 107 hc.currentHeaderHash = hc.currentHeader.Hash() 108 109 return hc, nil 110 } 111 112 // GetBlockNumber retrieves the block number belonging to the given hash 113 // from the cache or database 114 func (hc *HeaderChain) GetBlockNumber(hash common.Hash) uint64 { 115 if cached, ok := hc.numberCache.Get(hash); ok { 116 return cached.(uint64) 117 } 118 number := GetBlockNumber(hc.chainDb, hash) 119 if number != missingNumber { 120 hc.numberCache.Add(hash, number) 121 } 122 return number 123 } 124 125 // WriteHeader writes a header into the local chain, given that its parent is 126 // already known. If the total difficulty of the newly inserted header becomes 127 // greater than the current known TD, the canonical chain is re-routed. 128 // 129 // Note: This method is not concurrent-safe with inserting blocks simultaneously 130 // into the chain, as side effects caused by reorganisations cannot be emulated 131 // without the real blocks. Hence, writing headers directly should only be done 132 // in two scenarios: pure-header mode of operation (light clients), or properly 133 // separated header/block phases (non-archive clients). 134 func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) { 135 // Cache some values to prevent constant recalculation 136 var ( 137 hash = header.Hash() 138 number = header.Number.Uint64() 139 ) 140 // Calculate the total difficulty of the header 141 ptd := hc.GetTd(header.ParentHash, number-1) 142 if ptd == nil { 143 return NonStatTy, consensus.ErrUnknownAncestor 144 } 145 localTd := hc.GetTd(hc.currentHeaderHash, hc.currentHeader.Number.Uint64()) 146 externTd := new(big.Int).Add(header.Difficulty, ptd) 147 148 // Irrelevant of the canonical status, write the td and header to the database 149 if err := hc.WriteTd(hash, number, externTd); err != nil { 150 log.Crit("Failed to write header total difficulty", "err", err) 151 } 152 if err := WriteHeader(hc.chainDb, header); err != nil { 153 log.Crit("Failed to write header content", "err", err) 154 } 155 // If the total difficulty is higher than our known, add it to the canonical chain 156 // Second clause in the if statement reduces the vulnerability to selfish mining. 157 // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf 158 if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { 159 // Delete any canonical number assignments above the new head 160 for i := number + 1; ; i++ { 161 hash := GetCanonicalHash(hc.chainDb, i) 162 if hash == (common.Hash{}) { 163 break 164 } 165 DeleteCanonicalHash(hc.chainDb, i) 166 } 167 // Overwrite any stale canonical number assignments 168 var ( 169 headHash = header.ParentHash 170 headNumber = header.Number.Uint64() - 1 171 headHeader = hc.GetHeader(headHash, headNumber) 172 ) 173 for GetCanonicalHash(hc.chainDb, headNumber) != headHash { 174 WriteCanonicalHash(hc.chainDb, headHash, headNumber) 175 176 headHash = headHeader.ParentHash 177 headNumber = headHeader.Number.Uint64() - 1 178 headHeader = hc.GetHeader(headHash, headNumber) 179 } 180 // Extend the canonical chain with the new header 181 if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil { 182 log.Crit("Failed to insert header number", "err", err) 183 } 184 if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil { 185 log.Crit("Failed to insert head header hash", "err", err) 186 } 187 hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header) 188 189 status = CanonStatTy 190 } else { 191 status = SideStatTy 192 } 193 194 hc.headerCache.Add(hash, header) 195 hc.numberCache.Add(hash, number) 196 197 return 198 } 199 200 // WhCallback is a callback function for inserting individual headers. 201 // A callback is used for two reasons: first, in a LightChain, status should be 202 // processed and light chain events sent, while in a BlockChain this is not 203 // necessary since chain events are sent after inserting blocks. Second, the 204 // header writes should be protected by the parent chain mutex individually. 205 type WhCallback func(*types.Header) error 206 207 func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int) (int, error) { 208 // Do a sanity check that the provided chain is actually ordered and linked 209 for i := 1; i < len(chain); i++ { 210 if chain[i].Number.Uint64() != chain[i-1].Number.Uint64()+1 || chain[i].ParentHash != chain[i-1].Hash() { 211 // Chain broke ancestry, log a messge (programming error) and skip insertion 212 log.Error("Non contiguous header insert", "number", chain[i].Number, "hash", chain[i].Hash(), 213 "parent", chain[i].ParentHash, "prevnumber", chain[i-1].Number, "prevhash", chain[i-1].Hash()) 214 215 return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, chain[i-1].Number, 216 chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4]) 217 } 218 } 219 220 // Generate the list of seal verification requests, and start the parallel verifier 221 seals := make([]bool, len(chain)) 222 for i := 0; i < len(seals)/checkFreq; i++ { 223 index := i*checkFreq + hc.rand.Intn(checkFreq) 224 if index >= len(seals) { 225 index = len(seals) - 1 226 } 227 seals[index] = true 228 } 229 seals[len(seals)-1] = true // Last should always be verified to avoid junk 230 231 abort, results := hc.engine.VerifyHeaders(hc, chain, seals) 232 defer close(abort) 233 234 // Iterate over the headers and ensure they all check out 235 for i, header := range chain { 236 // If the chain is terminating, stop processing blocks 237 if hc.procInterrupt() { 238 log.Debug("Premature abort during headers verification") 239 return 0, errors.New("aborted") 240 } 241 // If the header is a banned one, straight out abort 242 if BadHashes[header.Hash()] { 243 return i, ErrBlacklistedHash 244 } 245 // Otherwise wait for headers checks and ensure they pass 246 if err := <-results; err != nil { 247 return i, err 248 } 249 } 250 251 return 0, nil 252 } 253 254 // InsertHeaderChain attempts to insert the given header chain in to the local 255 // chain, possibly creating a reorg. If an error is returned, it will return the 256 // index number of the failing header as well an error describing what went wrong. 257 // 258 // The verify parameter can be used to fine tune whether nonce verification 259 // should be done or not. The reason behind the optional check is because some 260 // of the header retrieval mechanisms already need to verfy nonces, as well as 261 // because nonces can be verified sparsely, not needing to check each. 262 func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCallback, start time.Time) (int, error) { 263 // Collect some import statistics to report on 264 stats := struct{ processed, ignored int }{} 265 // All headers passed verification, import them into the database 266 for i, header := range chain { 267 // Short circuit insertion if shutting down 268 if hc.procInterrupt() { 269 log.Debug("Premature abort during headers import") 270 return i, errors.New("aborted") 271 } 272 // If the header's already known, skip it, otherwise store 273 if hc.HasHeader(header.Hash(), header.Number.Uint64()) { 274 stats.ignored++ 275 continue 276 } 277 if err := writeHeader(header); err != nil { 278 return i, err 279 } 280 stats.processed++ 281 } 282 // Report some public statistics so the user has a clue what's going on 283 last := chain[len(chain)-1] 284 log.Info("Imported new block headers", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)), 285 "number", last.Number, "hash", last.Hash(), "ignored", stats.ignored) 286 287 return 0, nil 288 } 289 290 // GetBlockHashesFromHash retrieves a number of block hashes starting at a given 291 // hash, fetching towards the genesis block. 292 func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { 293 // Get the origin header from which to fetch 294 header := hc.GetHeaderByHash(hash) 295 if header == nil { 296 return nil 297 } 298 // Iterate the headers until enough is collected or the genesis reached 299 chain := make([]common.Hash, 0, max) 300 for i := uint64(0); i < max; i++ { 301 next := header.ParentHash 302 if header = hc.GetHeader(next, header.Number.Uint64()-1); header == nil { 303 break 304 } 305 chain = append(chain, next) 306 if header.Number.Sign() == 0 { 307 break 308 } 309 } 310 return chain 311 } 312 313 // GetTd retrieves a block's total difficulty in the canonical chain from the 314 // database by hash and number, caching it if found. 315 func (hc *HeaderChain) GetTd(hash common.Hash, number uint64) *big.Int { 316 // Short circuit if the td's already in the cache, retrieve otherwise 317 if cached, ok := hc.tdCache.Get(hash); ok { 318 return cached.(*big.Int) 319 } 320 td := GetTd(hc.chainDb, hash, number) 321 if td == nil { 322 return nil 323 } 324 // Cache the found body for next time and return 325 hc.tdCache.Add(hash, td) 326 return td 327 } 328 329 // GetTdByHash retrieves a block's total difficulty in the canonical chain from the 330 // database by hash, caching it if found. 331 func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int { 332 return hc.GetTd(hash, hc.GetBlockNumber(hash)) 333 } 334 335 // WriteTd stores a block's total difficulty into the database, also caching it 336 // along the way. 337 func (hc *HeaderChain) WriteTd(hash common.Hash, number uint64, td *big.Int) error { 338 if err := WriteTd(hc.chainDb, hash, number, td); err != nil { 339 return err 340 } 341 hc.tdCache.Add(hash, new(big.Int).Set(td)) 342 return nil 343 } 344 345 // GetHeader retrieves a block header from the database by hash and number, 346 // caching it if found. 347 func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header { 348 // Short circuit if the header's already in the cache, retrieve otherwise 349 if header, ok := hc.headerCache.Get(hash); ok { 350 return header.(*types.Header) 351 } 352 header := GetHeader(hc.chainDb, hash, number) 353 if header == nil { 354 return nil 355 } 356 // Cache the found header for next time and return 357 hc.headerCache.Add(hash, header) 358 return header 359 } 360 361 // GetHeaderByHash retrieves a block header from the database by hash, caching it if 362 // found. 363 func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header { 364 return hc.GetHeader(hash, hc.GetBlockNumber(hash)) 365 } 366 367 // HasHeader checks if a block header is present in the database or not. 368 func (hc *HeaderChain) HasHeader(hash common.Hash, number uint64) bool { 369 if hc.numberCache.Contains(hash) || hc.headerCache.Contains(hash) { 370 return true 371 } 372 ok, _ := hc.chainDb.Has(headerKey(hash, number)) 373 return ok 374 } 375 376 // GetHeaderByNumber retrieves a block header from the database by number, 377 // caching it (associated with its hash) if found. 378 func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header { 379 hash := GetCanonicalHash(hc.chainDb, number) 380 if hash == (common.Hash{}) { 381 return nil 382 } 383 return hc.GetHeader(hash, number) 384 } 385 386 // CurrentHeader retrieves the current head header of the canonical chain. The 387 // header is retrieved from the HeaderChain's internal cache. 388 func (hc *HeaderChain) CurrentHeader() *types.Header { 389 return hc.currentHeader 390 } 391 392 // SetCurrentHeader sets the current head header of the canonical chain. 393 func (hc *HeaderChain) SetCurrentHeader(head *types.Header) { 394 if err := WriteHeadHeaderHash(hc.chainDb, head.Hash()); err != nil { 395 log.Crit("Failed to insert head header hash", "err", err) 396 } 397 hc.currentHeader = head 398 hc.currentHeaderHash = head.Hash() 399 } 400 401 // DeleteCallback is a callback function that is called by SetHead before 402 // each header is deleted. 403 type DeleteCallback func(common.Hash, uint64) 404 405 // SetHead rewinds the local chain to a new head. Everything above the new head 406 // will be deleted and the new one set. 407 func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) { 408 height := uint64(0) 409 if hc.currentHeader != nil { 410 height = hc.currentHeader.Number.Uint64() 411 } 412 413 for hc.currentHeader != nil && hc.currentHeader.Number.Uint64() > head { 414 hash := hc.currentHeader.Hash() 415 num := hc.currentHeader.Number.Uint64() 416 if delFn != nil { 417 delFn(hash, num) 418 } 419 DeleteHeader(hc.chainDb, hash, num) 420 DeleteTd(hc.chainDb, hash, num) 421 hc.currentHeader = hc.GetHeader(hc.currentHeader.ParentHash, hc.currentHeader.Number.Uint64()-1) 422 } 423 // Roll back the canonical chain numbering 424 for i := height; i > head; i-- { 425 DeleteCanonicalHash(hc.chainDb, i) 426 } 427 // Clear out any stale content from the caches 428 hc.headerCache.Purge() 429 hc.tdCache.Purge() 430 hc.numberCache.Purge() 431 432 if hc.currentHeader == nil { 433 hc.currentHeader = hc.genesisHeader 434 } 435 hc.currentHeaderHash = hc.currentHeader.Hash() 436 437 if err := WriteHeadHeaderHash(hc.chainDb, hc.currentHeaderHash); err != nil { 438 log.Crit("Failed to reset head header hash", "err", err) 439 } 440 } 441 442 // SetGenesis sets a new genesis block header for the chain 443 func (hc *HeaderChain) SetGenesis(head *types.Header) { 444 hc.genesisHeader = head 445 } 446 447 // Config retrieves the header chain's chain configuration. 448 func (hc *HeaderChain) Config() *params.ChainConfig { return hc.config } 449 450 // Engine retrieves the header chain's consensus engine. 451 func (hc *HeaderChain) Engine() consensus.Engine { return hc.engine } 452 453 // GetBlock implements consensus.ChainReader, and returns nil for every input as 454 // a header chain does not have blocks available for retrieval. 455 func (hc *HeaderChain) GetBlock(hash common.Hash, number uint64) *types.Block { 456 return nil 457 } 458 459 func (hc *HeaderChain) GetBalanceAndCoinAgeByHeaderHash(addr common.Address) (*big.Int, *big.Int, *big.Int, *big.Int) { 460 461 s,_,Number,Time :=hc.State() 462 return s.GetBalance(addr),s.GetCoinAge(addr,Number,Time),Number,Time 463 464 } 465 466 func (hc *HeaderChain) State() (*state.StateDB, error, *big.Int, *big.Int) { 467 s,e := hc.StateAt(hc.currentHeader.Root) 468 return s,e, hc.currentHeader.Number, hc.currentHeader.Time 469 } 470 471 func (hc *HeaderChain) StateAt(root common.Hash) (*state.StateDB, error) { 472 return state.New(root, hc.stateCache) 473 }