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