github.com/decred/dcrd/blockchain@v1.2.1/chain.go (about) 1 // Copyright (c) 2013-2016 The btcsuite developers 2 // Copyright (c) 2015-2019 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package blockchain 7 8 import ( 9 "fmt" 10 "math/big" 11 "sync" 12 "time" 13 14 "github.com/decred/dcrd/blockchain/stake" 15 "github.com/decred/dcrd/blockchain/standalone" 16 "github.com/decred/dcrd/chaincfg" 17 "github.com/decred/dcrd/chaincfg/chainhash" 18 "github.com/decred/dcrd/database" 19 "github.com/decred/dcrd/dcrutil" 20 "github.com/decred/dcrd/txscript" 21 "github.com/decred/dcrd/wire" 22 ) 23 24 const ( 25 // maxOrphanBlocks is the maximum number of orphan blocks that can be 26 // queued. 27 maxOrphanBlocks = 500 28 29 // minMemoryNodes is the minimum number of consecutive nodes needed 30 // in memory in order to perform all necessary validation. It is used 31 // to determine when it's safe to prune nodes from memory without 32 // causing constant dynamic reloading. This value should be larger than 33 // that for minMemoryStakeNodes. 34 minMemoryNodes = 2880 35 36 // minMemoryStakeNodes is the maximum height to keep stake nodes 37 // in memory for in their respective nodes. Beyond this height, 38 // they will need to be manually recalculated. This value should 39 // be at least the stake retarget interval. 40 minMemoryStakeNodes = 288 41 42 // mainchainBlockCacheSize is the number of mainchain blocks to 43 // keep in memory, by height from the tip of the mainchain. 44 mainchainBlockCacheSize = 12 45 ) 46 47 // panicf is a convenience function that formats according to the given format 48 // specifier and arguments and then logs the result at the critical level and 49 // panics with it. 50 func panicf(format string, args ...interface{}) { 51 str := fmt.Sprintf(format, args...) 52 log.Critical(str) 53 panic(str) 54 } 55 56 // BlockLocator is used to help locate a specific block. The algorithm for 57 // building the block locator is to add the hashes in reverse order until 58 // the genesis block is reached. In order to keep the list of locator hashes 59 // to a reasonable number of entries, first the most recent previous 12 block 60 // hashes are added, then the step is doubled each loop iteration to 61 // exponentially decrease the number of hashes as a function of the distance 62 // from the block being located. 63 // 64 // For example, assume a block chain with a side chain as depicted below: 65 // genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18 66 // \-> 16a -> 17a 67 // 68 // The block locator for block 17a would be the hashes of blocks: 69 // [17a 16a 15 14 13 12 11 10 9 8 7 6 4 genesis] 70 type BlockLocator []*chainhash.Hash 71 72 // orphanBlock represents a block that we don't yet have the parent for. It 73 // is a normal block plus an expiration time to prevent caching the orphan 74 // forever. 75 type orphanBlock struct { 76 block *dcrutil.Block 77 expiration time.Time 78 } 79 80 // BestState houses information about the current best block and other info 81 // related to the state of the main chain as it exists from the point of view of 82 // the current best block. 83 // 84 // The BestSnapshot method can be used to obtain access to this information 85 // in a concurrent safe manner and the data will not be changed out from under 86 // the caller when chain state changes occur as the function name implies. 87 // However, the returned snapshot must be treated as immutable since it is 88 // shared by all callers. 89 type BestState struct { 90 Hash chainhash.Hash // The hash of the block. 91 PrevHash chainhash.Hash // The previous block hash. 92 Height int64 // The height of the block. 93 Bits uint32 // The difficulty bits of the block. 94 NextPoolSize uint32 // The ticket pool size. 95 NextStakeDiff int64 // The next stake difficulty. 96 BlockSize uint64 // The size of the block. 97 NumTxns uint64 // The number of txns in the block. 98 TotalTxns uint64 // The total number of txns in the chain. 99 MedianTime time.Time // Median time as per CalcPastMedianTime. 100 TotalSubsidy int64 // The total subsidy for the chain. 101 NextWinningTickets []chainhash.Hash // The eligible tickets to vote on the next block. 102 MissedTickets []chainhash.Hash // The missed tickets set to be revoked. 103 NextFinalState [6]byte // The calculated state of the lottery for the next block. 104 } 105 106 // newBestState returns a new best stats instance for the given parameters. 107 func newBestState(node *blockNode, blockSize, numTxns, totalTxns uint64, 108 medianTime time.Time, totalSubsidy int64, nextPoolSize uint32, 109 nextStakeDiff int64, nextWinners, missed []chainhash.Hash, 110 nextFinalState [6]byte) *BestState { 111 prevHash := *zeroHash 112 if node.parent != nil { 113 prevHash = node.parent.hash 114 } 115 return &BestState{ 116 Hash: node.hash, 117 PrevHash: prevHash, 118 Height: node.height, 119 Bits: node.bits, 120 NextPoolSize: nextPoolSize, 121 NextStakeDiff: nextStakeDiff, 122 BlockSize: blockSize, 123 NumTxns: numTxns, 124 TotalTxns: totalTxns, 125 MedianTime: medianTime, 126 TotalSubsidy: totalSubsidy, 127 NextWinningTickets: nextWinners, 128 MissedTickets: missed, 129 NextFinalState: nextFinalState, 130 } 131 } 132 133 // BlockChain provides functions for working with the Decred block chain. 134 // It includes functionality such as rejecting duplicate blocks, ensuring blocks 135 // follow all rules, orphan handling, checkpoint handling, and best chain 136 // selection with reorganization. 137 type BlockChain struct { 138 // The following fields are set when the instance is created and can't 139 // be changed afterwards, so there is no need to protect them with a 140 // separate mutex. 141 checkpointsByHeight map[int64]*chaincfg.Checkpoint 142 deploymentVers map[string]uint32 143 db database.DB 144 dbInfo *databaseInfo 145 chainParams *chaincfg.Params 146 timeSource MedianTimeSource 147 notifications NotificationCallback 148 sigCache *txscript.SigCache 149 indexManager IndexManager 150 interrupt <-chan struct{} 151 152 // subsidyCache is the cache that provides quick lookup of subsidy 153 // values. 154 subsidyCache *standalone.SubsidyCache 155 156 // chainLock protects concurrent access to the vast majority of the 157 // fields in this struct below this point. 158 chainLock sync.RWMutex 159 160 // These fields are configuration parameters that can be toggled at 161 // runtime. They are protected by the chain lock. 162 noVerify bool 163 noCheckpoints bool 164 165 // These fields are related to the memory block index. They both have 166 // their own locks, however they are often also protected by the chain 167 // lock to help prevent logic races when blocks are being processed. 168 // 169 // index houses the entire block index in memory. The block index is 170 // a tree-shaped structure. 171 // 172 // bestChain tracks the current active chain by making use of an 173 // efficient chain view into the block index. 174 index *blockIndex 175 bestChain *chainView 176 177 // These fields are related to handling of orphan blocks. They are 178 // protected by a combination of the chain lock and the orphan lock. 179 orphanLock sync.RWMutex 180 orphans map[chainhash.Hash]*orphanBlock 181 prevOrphans map[chainhash.Hash][]*orphanBlock 182 oldestOrphan *orphanBlock 183 184 // The block cache for mainchain blocks, to facilitate faster 185 // reorganizations. 186 mainchainBlockCacheLock sync.RWMutex 187 mainchainBlockCache map[chainhash.Hash]*dcrutil.Block 188 mainchainBlockCacheSize int 189 190 // These fields house a cached view that represents a block that votes 191 // against its parent and therefore contains all changes as a result 192 // of disconnecting all regular transactions in its parent. It is only 193 // lazily updated to the current tip when fetching a utxo view via the 194 // FetchUtxoView function with the flag indicating the block votes against 195 // the parent set. 196 disapprovedViewLock sync.Mutex 197 disapprovedView *UtxoViewpoint 198 199 // These fields are related to checkpoint handling. They are protected 200 // by the chain lock. 201 nextCheckpoint *chaincfg.Checkpoint 202 checkpointNode *blockNode 203 204 // The state is used as a fairly efficient way to cache information 205 // about the current best chain state that is returned to callers when 206 // requested. It operates on the principle of MVCC such that any time a 207 // new block becomes the best block, the state pointer is replaced with 208 // a new struct and the old state is left untouched. In this way, 209 // multiple callers can be pointing to different best chain states. 210 // This is acceptable for most callers because the state is only being 211 // queried at a specific point in time. 212 // 213 // In addition, some of the fields are stored in the database so the 214 // chain state can be quickly reconstructed on load. 215 stateLock sync.RWMutex 216 stateSnapshot *BestState 217 218 // The following caches are used to efficiently keep track of the 219 // current deployment threshold state of each rule change deployment. 220 // 221 // This information is stored in the database so it can be quickly 222 // reconstructed on load. 223 // 224 // deploymentCaches caches the current deployment threshold state for 225 // blocks in each of the actively defined deployments. 226 deploymentCaches map[uint32][]thresholdStateCache 227 228 // pruner is the automatic pruner for block nodes and stake nodes, 229 // so that the memory may be restored by the garbage collector if 230 // it is unlikely to be referenced in the future. 231 pruner *chainPruner 232 233 // The following maps are various caches for the stake version/voting 234 // system. The goal of these is to reduce disk access to load blocks 235 // from disk. Measurements indicate that it is slightly more expensive 236 // so setup the cache (<10%) vs doing a straight chain walk. Every 237 // other subsequent call is >10x faster. 238 isVoterMajorityVersionCache map[[stakeMajorityCacheKeySize]byte]bool 239 isStakeMajorityVersionCache map[[stakeMajorityCacheKeySize]byte]bool 240 calcPriorStakeVersionCache map[[chainhash.HashSize]byte]uint32 241 calcVoterVersionIntervalCache map[[chainhash.HashSize]byte]uint32 242 calcStakeVersionCache map[[chainhash.HashSize]byte]uint32 243 } 244 245 const ( 246 // stakeMajorityCacheKeySize is comprised of the stake version and the 247 // hash size. The stake version is a little endian uint32, hence we 248 // add 4 to the overall size. 249 stakeMajorityCacheKeySize = 4 + chainhash.HashSize 250 ) 251 252 // StakeVersions is a condensed form of a dcrutil.Block that is used to prevent 253 // using gigabytes of memory. 254 type StakeVersions struct { 255 Hash chainhash.Hash 256 Height int64 257 BlockVersion int32 258 StakeVersion uint32 259 Votes []stake.VoteVersionTuple 260 } 261 262 // GetStakeVersions returns a cooked array of StakeVersions. We do this in 263 // order to not bloat memory by returning raw blocks. 264 func (b *BlockChain) GetStakeVersions(hash *chainhash.Hash, count int32) ([]StakeVersions, error) { 265 // NOTE: The requirement for the node being fully validated here is strictly 266 // stronger than what is actually required. In reality, all that is needed 267 // is for the block data for the node and all of its ancestors to be 268 // available, but there is not currently any tracking to be able to 269 // efficiently determine that state. 270 startNode := b.index.LookupNode(hash) 271 if startNode == nil || !b.index.NodeStatus(startNode).KnownValid() { 272 return nil, fmt.Errorf("block %s is not known", hash) 273 } 274 275 // Nothing to do if no count requested. 276 if count == 0 { 277 return nil, nil 278 } 279 280 if count < 0 { 281 return nil, fmt.Errorf("count must not be less than zero - "+ 282 "got %d", count) 283 } 284 285 // Limit the requested count to the max possible for the requested block. 286 if count > int32(startNode.height+1) { 287 count = int32(startNode.height + 1) 288 } 289 290 result := make([]StakeVersions, 0, count) 291 prevNode := startNode 292 for i := int32(0); prevNode != nil && i < count; i++ { 293 sv := StakeVersions{ 294 Hash: prevNode.hash, 295 Height: prevNode.height, 296 BlockVersion: prevNode.blockVersion, 297 StakeVersion: prevNode.stakeVersion, 298 Votes: prevNode.votes, 299 } 300 301 result = append(result, sv) 302 303 prevNode = prevNode.parent 304 } 305 306 return result, nil 307 } 308 309 // VoteInfo represents information on agendas and their respective states for 310 // a consensus deployment. 311 type VoteInfo struct { 312 Agendas []chaincfg.ConsensusDeployment 313 AgendaStatus []ThresholdStateTuple 314 } 315 316 // GetVoteInfo returns information on consensus deployment agendas 317 // and their respective states at the provided hash, for the provided 318 // deployment version. 319 func (b *BlockChain) GetVoteInfo(hash *chainhash.Hash, version uint32) (*VoteInfo, error) { 320 deployments, ok := b.chainParams.Deployments[version] 321 if !ok { 322 return nil, VoteVersionError(version) 323 } 324 325 vi := VoteInfo{ 326 Agendas: make([]chaincfg.ConsensusDeployment, 327 0, len(deployments)), 328 AgendaStatus: make([]ThresholdStateTuple, 0, len(deployments)), 329 } 330 for _, deployment := range deployments { 331 vi.Agendas = append(vi.Agendas, deployment) 332 status, err := b.NextThresholdState(hash, version, deployment.Vote.Id) 333 if err != nil { 334 return nil, err 335 } 336 vi.AgendaStatus = append(vi.AgendaStatus, status) 337 } 338 339 return &vi, nil 340 } 341 342 // DisableVerify provides a mechanism to disable transaction script validation 343 // which you DO NOT want to do in production as it could allow double spends 344 // and other undesirable things. It is provided only for debug purposes since 345 // script validation is extremely intensive and when debugging it is sometimes 346 // nice to quickly get the chain. 347 // 348 // This function is safe for concurrent access. 349 func (b *BlockChain) DisableVerify(disable bool) { 350 b.chainLock.Lock() 351 b.noVerify = disable 352 b.chainLock.Unlock() 353 } 354 355 // TotalSubsidy returns the total subsidy mined so far in the best chain. 356 // 357 // This function is safe for concurrent access. 358 func (b *BlockChain) TotalSubsidy() int64 { 359 b.chainLock.RLock() 360 ts := b.BestSnapshot().TotalSubsidy 361 b.chainLock.RUnlock() 362 363 return ts 364 } 365 366 // FetchSubsidyCache returns the current subsidy cache from the blockchain. 367 // 368 // This function is safe for concurrent access. 369 func (b *BlockChain) FetchSubsidyCache() *SubsidyCache { 370 return b.subsidyCache 371 } 372 373 // HaveBlock returns whether or not the chain instance has the block represented 374 // by the passed hash. This includes checking the various places a block can 375 // be like part of the main chain, on a side chain, or in the orphan pool. 376 // 377 // This function is safe for concurrent access. 378 func (b *BlockChain) HaveBlock(hash *chainhash.Hash) (bool, error) { 379 return b.index.HaveBlock(hash) || b.IsKnownOrphan(hash), nil 380 } 381 382 // ChainWork returns the total work up to and including the block of the 383 // provided block hash. 384 func (b *BlockChain) ChainWork(hash *chainhash.Hash) (*big.Int, error) { 385 node := b.index.LookupNode(hash) 386 if node == nil { 387 return nil, fmt.Errorf("block %s is not known", hash) 388 } 389 390 return node.workSum, nil 391 } 392 393 // IsKnownOrphan returns whether the passed hash is currently a known orphan. 394 // Keep in mind that only a limited number of orphans are held onto for a 395 // limited amount of time, so this function must not be used as an absolute 396 // way to test if a block is an orphan block. A full block (as opposed to just 397 // its hash) must be passed to ProcessBlock for that purpose. However, calling 398 // ProcessBlock with an orphan that already exists results in an error, so this 399 // function provides a mechanism for a caller to intelligently detect *recent* 400 // duplicate orphans and react accordingly. 401 // 402 // This function is safe for concurrent access. 403 func (b *BlockChain) IsKnownOrphan(hash *chainhash.Hash) bool { 404 // Protect concurrent access. Using a read lock only so multiple 405 // readers can query without blocking each other. 406 b.orphanLock.RLock() 407 _, exists := b.orphans[*hash] 408 b.orphanLock.RUnlock() 409 410 return exists 411 } 412 413 // GetOrphanRoot returns the head of the chain for the provided hash from the 414 // map of orphan blocks. 415 // 416 // This function is safe for concurrent access. 417 func (b *BlockChain) GetOrphanRoot(hash *chainhash.Hash) *chainhash.Hash { 418 // Protect concurrent access. Using a read lock only so multiple 419 // readers can query without blocking each other. 420 b.orphanLock.RLock() 421 defer b.orphanLock.RUnlock() 422 423 // Keep looping while the parent of each orphaned block is 424 // known and is an orphan itself. 425 orphanRoot := hash 426 prevHash := hash 427 for { 428 orphan, exists := b.orphans[*prevHash] 429 if !exists { 430 break 431 } 432 orphanRoot = prevHash 433 prevHash = &orphan.block.MsgBlock().Header.PrevBlock 434 } 435 436 return orphanRoot 437 } 438 439 // removeOrphanBlock removes the passed orphan block from the orphan pool and 440 // previous orphan index. 441 func (b *BlockChain) removeOrphanBlock(orphan *orphanBlock) { 442 // Protect concurrent access. 443 b.orphanLock.Lock() 444 defer b.orphanLock.Unlock() 445 446 // Remove the orphan block from the orphan pool. 447 orphanHash := orphan.block.Hash() 448 delete(b.orphans, *orphanHash) 449 450 // Remove the reference from the previous orphan index too. An indexing 451 // for loop is intentionally used over a range here as range does not 452 // reevaluate the slice on each iteration nor does it adjust the index 453 // for the modified slice. 454 prevHash := &orphan.block.MsgBlock().Header.PrevBlock 455 orphans := b.prevOrphans[*prevHash] 456 for i := 0; i < len(orphans); i++ { 457 hash := orphans[i].block.Hash() 458 if hash.IsEqual(orphanHash) { 459 copy(orphans[i:], orphans[i+1:]) 460 orphans[len(orphans)-1] = nil 461 orphans = orphans[:len(orphans)-1] 462 i-- 463 } 464 } 465 b.prevOrphans[*prevHash] = orphans 466 467 // Remove the map entry altogether if there are no longer any orphans 468 // which depend on the parent hash. 469 if len(b.prevOrphans[*prevHash]) == 0 { 470 delete(b.prevOrphans, *prevHash) 471 } 472 } 473 474 // addOrphanBlock adds the passed block (which is already determined to be 475 // an orphan prior calling this function) to the orphan pool. It lazily cleans 476 // up any expired blocks so a separate cleanup poller doesn't need to be run. 477 // It also imposes a maximum limit on the number of outstanding orphan 478 // blocks and will remove the oldest received orphan block if the limit is 479 // exceeded. 480 func (b *BlockChain) addOrphanBlock(block *dcrutil.Block) { 481 // Remove expired orphan blocks. 482 for _, oBlock := range b.orphans { 483 if time.Now().After(oBlock.expiration) { 484 b.removeOrphanBlock(oBlock) 485 continue 486 } 487 488 // Update the oldest orphan block pointer so it can be discarded 489 // in case the orphan pool fills up. 490 if b.oldestOrphan == nil || 491 oBlock.expiration.Before(b.oldestOrphan.expiration) { 492 b.oldestOrphan = oBlock 493 } 494 } 495 496 // Limit orphan blocks to prevent memory exhaustion. 497 if len(b.orphans)+1 > maxOrphanBlocks { 498 // Remove the oldest orphan to make room for the new one. 499 b.removeOrphanBlock(b.oldestOrphan) 500 b.oldestOrphan = nil 501 } 502 503 // Protect concurrent access. This is intentionally done here instead 504 // of near the top since removeOrphanBlock does its own locking and 505 // the range iterator is not invalidated by removing map entries. 506 b.orphanLock.Lock() 507 defer b.orphanLock.Unlock() 508 509 // Insert the block into the orphan map with an expiration time 510 // 1 hour from now. 511 expiration := time.Now().Add(time.Hour) 512 oBlock := &orphanBlock{ 513 block: block, 514 expiration: expiration, 515 } 516 b.orphans[*block.Hash()] = oBlock 517 518 // Add to previous hash lookup index for faster dependency lookups. 519 prevHash := &block.MsgBlock().Header.PrevBlock 520 b.prevOrphans[*prevHash] = append(b.prevOrphans[*prevHash], oBlock) 521 } 522 523 // TipGeneration returns the entire generation of blocks stemming from the 524 // parent of the current tip. 525 // 526 // The function is safe for concurrent access. 527 func (b *BlockChain) TipGeneration() ([]chainhash.Hash, error) { 528 b.chainLock.Lock() 529 b.index.RLock() 530 nodes := b.index.chainTips[b.bestChain.Tip().height] 531 nodeHashes := make([]chainhash.Hash, len(nodes)) 532 for i, n := range nodes { 533 nodeHashes[i] = n.hash 534 } 535 b.index.RUnlock() 536 b.chainLock.Unlock() 537 return nodeHashes, nil 538 } 539 540 // fetchMainChainBlockByNode returns the block from the main chain associated 541 // with the given node. It first attempts to use cache and then falls back to 542 // loading it from the database. 543 // 544 // An error is returned if the block is either not found or not in the main 545 // chain. 546 // 547 // This function MUST be called with the chain lock held (for reads). 548 func (b *BlockChain) fetchMainChainBlockByNode(node *blockNode) (*dcrutil.Block, error) { 549 // Ensure the block is in the main chain. 550 if !b.bestChain.Contains(node) { 551 str := fmt.Sprintf("block %s is not in the main chain", node.hash) 552 return nil, errNotInMainChain(str) 553 } 554 555 b.mainchainBlockCacheLock.RLock() 556 block, ok := b.mainchainBlockCache[node.hash] 557 b.mainchainBlockCacheLock.RUnlock() 558 if ok { 559 return block, nil 560 } 561 562 // Load the block from the database. 563 err := b.db.View(func(dbTx database.Tx) error { 564 var err error 565 block, err = dbFetchBlockByNode(dbTx, node) 566 return err 567 }) 568 return block, err 569 } 570 571 // fetchBlockByNode returns the block associated with the given node all known 572 // sources such as the internal caches and the database. This function returns 573 // blocks regardless or whether or not they are part of the main chain. 574 // 575 // This function is safe for concurrent access. 576 func (b *BlockChain) fetchBlockByNode(node *blockNode) (*dcrutil.Block, error) { 577 // Check main chain cache. 578 b.mainchainBlockCacheLock.RLock() 579 block, ok := b.mainchainBlockCache[node.hash] 580 b.mainchainBlockCacheLock.RUnlock() 581 if ok { 582 return block, nil 583 } 584 585 // Check orphan cache. 586 b.orphanLock.RLock() 587 orphan, existsOrphans := b.orphans[node.hash] 588 b.orphanLock.RUnlock() 589 if existsOrphans { 590 return orphan.block, nil 591 } 592 593 // Load the block from the database. 594 err := b.db.View(func(dbTx database.Tx) error { 595 var err error 596 block, err = dbFetchBlockByNode(dbTx, node) 597 return err 598 }) 599 return block, err 600 } 601 602 // pruneStakeNodes removes references to old stake nodes which should no 603 // longer be held in memory so as to keep the maximum memory usage down. 604 // It proceeds from the bestNode back to the determined minimum height node, 605 // finds all the relevant children, and then drops the the stake nodes from 606 // them by assigning nil and allowing the memory to be recovered by GC. 607 // 608 // This function MUST be called with the chain state lock held (for writes). 609 func (b *BlockChain) pruneStakeNodes() { 610 // Find the height to prune to. 611 pruneToNode := b.bestChain.Tip() 612 for i := int64(0); i < minMemoryStakeNodes-1 && pruneToNode != nil; i++ { 613 pruneToNode = pruneToNode.parent 614 } 615 616 // Nothing to do if there are not enough nodes. 617 if pruneToNode == nil || pruneToNode.parent == nil { 618 return 619 } 620 621 // Push the nodes to delete on a list. This will typically end up being 622 // a single node since pruning is currently done just before each new 623 // node is created. However, that might be tuned later to only prune at 624 // intervals, so the code needs to account for the possibility of 625 // multiple nodes. 626 var deleteNodes []*blockNode 627 for node := pruneToNode.parent; node != nil; node = node.parent { 628 deleteNodes = append(deleteNodes, node) 629 } 630 631 // Loop through each node to prune in reverse, unlink its children, remove 632 // it from the dependency index, and remove it from the node index. 633 for i := len(deleteNodes) - 1; i >= 0; i-- { 634 node := deleteNodes[i] 635 636 // Do not attempt to prune if the node should already have been pruned, 637 // for example if you're adding an old side chain block. 638 if node.height > b.bestChain.Tip().height-minMemoryNodes { 639 node.stakeNode = nil 640 node.newTickets = nil 641 node.ticketsVoted = nil 642 node.ticketsRevoked = nil 643 } 644 } 645 } 646 647 // BestPrevHash returns the hash of the previous block of the block at HEAD. 648 // 649 // This function is safe for concurrent access. 650 func (b *BlockChain) BestPrevHash() chainhash.Hash { 651 b.chainLock.Lock() 652 defer b.chainLock.Unlock() 653 654 var prevHash chainhash.Hash 655 tip := b.bestChain.Tip() 656 if tip.parent != nil { 657 prevHash = tip.parent.hash 658 } 659 return prevHash 660 } 661 662 // isMajorityVersion determines if a previous number of blocks in the chain 663 // starting with startNode are at least the minimum passed version. 664 // 665 // This function MUST be called with the chain state lock held (for reads). 666 func (b *BlockChain) isMajorityVersion(minVer int32, startNode *blockNode, numRequired uint64) bool { 667 numFound := uint64(0) 668 iterNode := startNode 669 for i := uint64(0); i < b.chainParams.BlockUpgradeNumToCheck && 670 numFound < numRequired && iterNode != nil; i++ { 671 672 // This node has a version that is at least the minimum version. 673 if iterNode.blockVersion >= minVer { 674 numFound++ 675 } 676 677 iterNode = iterNode.parent 678 } 679 680 return numFound >= numRequired 681 } 682 683 // pushMainChainBlockCache pushes a block onto the main chain block cache, 684 // and removes any old blocks from the cache that might be present. 685 func (b *BlockChain) pushMainChainBlockCache(block *dcrutil.Block) { 686 curHeight := block.Height() 687 curHash := block.Hash() 688 b.mainchainBlockCacheLock.Lock() 689 b.mainchainBlockCache[*curHash] = block 690 for hash, bl := range b.mainchainBlockCache { 691 if bl.Height() <= curHeight-int64(b.mainchainBlockCacheSize) { 692 delete(b.mainchainBlockCache, hash) 693 } 694 } 695 b.mainchainBlockCacheLock.Unlock() 696 } 697 698 // connectBlock handles connecting the passed node/block to the end of the main 699 // (best) chain. 700 // 701 // This passed utxo view must have all referenced txos the block spends marked 702 // as spent and all of the new txos the block creates added to it. In addition, 703 // the passed stxos slice must be populated with all of the information for the 704 // spent txos. This approach is used because the connection validation that 705 // must happen prior to calling this function requires the same details, so 706 // it would be inefficient to repeat it. 707 // 708 // This function MUST be called with the chain state lock held (for writes). 709 func (b *BlockChain) connectBlock(node *blockNode, block, parent *dcrutil.Block, view *UtxoViewpoint, stxos []spentTxOut) error { 710 // Make sure it's extending the end of the best chain. 711 prevHash := block.MsgBlock().Header.PrevBlock 712 tip := b.bestChain.Tip() 713 if prevHash != tip.hash { 714 panicf("block %v (height %v) connects to block %v instead of "+ 715 "extending the best chain (hash %v, height %v)", node.hash, 716 node.height, prevHash, tip.hash, tip.height) 717 } 718 719 // Sanity check the correct number of stxos are provided. 720 if len(stxos) != countSpentOutputs(block) { 721 panicf("provided %v stxos for block %v (height %v) which spends %v "+ 722 "outputs", len(stxos), node.hash, node.height, 723 countSpentOutputs(block)) 724 } 725 726 // Write any modified block index entries to the database before 727 // updating the best state. 728 if err := b.flushBlockIndex(); err != nil { 729 return err 730 } 731 732 // Get the stake node for this node, filling in any data that 733 // may have yet to have been filled in. In all cases this 734 // should simply give a pointer to data already prepared, but 735 // run this anyway to be safe. 736 stakeNode, err := b.fetchStakeNode(node) 737 if err != nil { 738 return err 739 } 740 741 // Calculate the next stake difficulty. 742 nextStakeDiff, err := b.calcNextRequiredStakeDifficulty(node) 743 if err != nil { 744 return err 745 } 746 747 // Generate a new best state snapshot that will be used to update the 748 // database and later memory if all database updates are successful. 749 b.stateLock.RLock() 750 curTotalTxns := b.stateSnapshot.TotalTxns 751 curTotalSubsidy := b.stateSnapshot.TotalSubsidy 752 b.stateLock.RUnlock() 753 subsidy := calculateAddedSubsidy(block, parent) 754 numTxns := uint64(len(block.Transactions()) + len(block.STransactions())) 755 blockSize := uint64(block.MsgBlock().Header.Size) 756 state := newBestState(node, blockSize, numTxns, curTotalTxns+numTxns, 757 node.CalcPastMedianTime(), curTotalSubsidy+subsidy, 758 uint32(node.stakeNode.PoolSize()), nextStakeDiff, 759 node.stakeNode.Winners(), node.stakeNode.MissedTickets(), 760 node.stakeNode.FinalState()) 761 762 // Atomically insert info into the database. 763 err = b.db.Update(func(dbTx database.Tx) error { 764 // Update best block state. 765 err := dbPutBestState(dbTx, state, node.workSum) 766 if err != nil { 767 return err 768 } 769 770 // Update the utxo set using the state of the utxo view. This 771 // entails removing all of the utxos spent and adding the new 772 // ones created by the block. 773 err = dbPutUtxoView(dbTx, view) 774 if err != nil { 775 return err 776 } 777 778 // Update the transaction spend journal by adding a record for 779 // the block that contains all txos spent by it. 780 err = dbPutSpendJournalEntry(dbTx, block.Hash(), stxos) 781 if err != nil { 782 return err 783 } 784 785 // Insert the block into the stake database. 786 err = stake.WriteConnectedBestNode(dbTx, stakeNode, node.hash) 787 if err != nil { 788 return err 789 } 790 791 // Allow the index manager to call each of the currently active 792 // optional indexes with the block being connected so they can 793 // update themselves accordingly. 794 if b.indexManager != nil { 795 err := b.indexManager.ConnectBlock(dbTx, block, parent, view) 796 if err != nil { 797 return err 798 } 799 } 800 801 return nil 802 }) 803 if err != nil { 804 return err 805 } 806 807 // Prune fully spent entries and mark all entries in the view unmodified 808 // now that the modifications have been committed to the database. 809 view.commit() 810 811 // This node is now the end of the best chain. 812 b.bestChain.SetTip(node) 813 814 // Update the state for the best block. Notice how this replaces the 815 // entire struct instead of updating the existing one. This effectively 816 // allows the old version to act as a snapshot which callers can use 817 // freely without needing to hold a lock for the duration. See the 818 // comments on the state variable for more details. 819 b.stateLock.Lock() 820 b.stateSnapshot = state 821 b.stateLock.Unlock() 822 823 // Assemble the current block and the parent into a slice. 824 blockAndParent := []*dcrutil.Block{block, parent} 825 826 // Notify the caller that the block was connected to the main chain. 827 // The caller would typically want to react with actions such as 828 // updating wallets. 829 b.chainLock.Unlock() 830 b.sendNotification(NTBlockConnected, blockAndParent) 831 b.chainLock.Lock() 832 833 // Send stake notifications about the new block. 834 if node.height >= b.chainParams.StakeEnabledHeight { 835 nextStakeDiff, err := b.calcNextRequiredStakeDifficulty(node) 836 if err != nil { 837 return err 838 } 839 840 // Notify of spent and missed tickets 841 b.sendNotification(NTSpentAndMissedTickets, 842 &TicketNotificationsData{ 843 Hash: node.hash, 844 Height: node.height, 845 StakeDifficulty: nextStakeDiff, 846 TicketsSpent: node.stakeNode.SpentByBlock(), 847 TicketsMissed: node.stakeNode.MissedByBlock(), 848 TicketsNew: []chainhash.Hash{}, 849 }) 850 // Notify of new tickets 851 b.sendNotification(NTNewTickets, 852 &TicketNotificationsData{ 853 Hash: node.hash, 854 Height: node.height, 855 StakeDifficulty: nextStakeDiff, 856 TicketsSpent: []chainhash.Hash{}, 857 TicketsMissed: []chainhash.Hash{}, 858 TicketsNew: node.stakeNode.NewTickets(), 859 }) 860 } 861 862 // Optimization: Before checkpoints, immediately dump the parent's stake 863 // node because we no longer need it. 864 if node.height < b.chainParams.LatestCheckpointHeight() { 865 parent := b.bestChain.Tip().parent 866 parent.stakeNode = nil 867 parent.newTickets = nil 868 parent.ticketsVoted = nil 869 parent.ticketsRevoked = nil 870 } 871 872 b.pushMainChainBlockCache(block) 873 874 return nil 875 } 876 877 // dropMainChainBlockCache drops a block from the main chain block cache. 878 func (b *BlockChain) dropMainChainBlockCache(block *dcrutil.Block) { 879 curHash := block.Hash() 880 b.mainchainBlockCacheLock.Lock() 881 delete(b.mainchainBlockCache, *curHash) 882 b.mainchainBlockCacheLock.Unlock() 883 } 884 885 // disconnectBlock handles disconnecting the passed node/block from the end of 886 // the main (best) chain. 887 // 888 // This function MUST be called with the chain state lock held (for writes). 889 func (b *BlockChain) disconnectBlock(node *blockNode, block, parent *dcrutil.Block, view *UtxoViewpoint) error { 890 // Make sure the node being disconnected is the end of the best chain. 891 tip := b.bestChain.Tip() 892 if node.hash != tip.hash { 893 panicf("block %v (height %v) is not the end of the best chain "+ 894 "(hash %v, height %v)", node.hash, node.height, tip.hash, 895 tip.height) 896 } 897 898 // Write any modified block index entries to the database before 899 // updating the best state. 900 if err := b.flushBlockIndex(); err != nil { 901 return err 902 } 903 904 // Prepare the information required to update the stake database 905 // contents. 906 childStakeNode, err := b.fetchStakeNode(node) 907 if err != nil { 908 return err 909 } 910 parentStakeNode, err := b.fetchStakeNode(node.parent) 911 if err != nil { 912 return err 913 } 914 915 // Generate a new best state snapshot that will be used to update the 916 // database and later memory if all database updates are successful. 917 b.stateLock.RLock() 918 curTotalTxns := b.stateSnapshot.TotalTxns 919 curTotalSubsidy := b.stateSnapshot.TotalSubsidy 920 b.stateLock.RUnlock() 921 parentBlockSize := uint64(parent.MsgBlock().Header.Size) 922 numParentTxns := uint64(len(parent.Transactions()) + len(parent.STransactions())) 923 numBlockTxns := uint64(len(block.Transactions()) + len(block.STransactions())) 924 newTotalTxns := curTotalTxns - numBlockTxns 925 subsidy := calculateAddedSubsidy(block, parent) 926 newTotalSubsidy := curTotalSubsidy - subsidy 927 prevNode := node.parent 928 state := newBestState(prevNode, parentBlockSize, numParentTxns, 929 newTotalTxns, prevNode.CalcPastMedianTime(), newTotalSubsidy, 930 uint32(prevNode.stakeNode.PoolSize()), node.sbits, 931 prevNode.stakeNode.Winners(), prevNode.stakeNode.MissedTickets(), 932 prevNode.stakeNode.FinalState()) 933 934 err = b.db.Update(func(dbTx database.Tx) error { 935 // Update best block state. 936 err := dbPutBestState(dbTx, state, node.workSum) 937 if err != nil { 938 return err 939 } 940 941 // Update the utxo set using the state of the utxo view. This 942 // entails restoring all of the utxos spent and removing the new 943 // ones created by the block. 944 err = dbPutUtxoView(dbTx, view) 945 if err != nil { 946 return err 947 } 948 949 // Update the transaction spend journal by removing the record 950 // that contains all txos spent by the block . 951 err = dbRemoveSpendJournalEntry(dbTx, block.Hash()) 952 if err != nil { 953 return err 954 } 955 956 err = stake.WriteDisconnectedBestNode(dbTx, parentStakeNode, 957 node.parent.hash, childStakeNode.UndoData()) 958 if err != nil { 959 return err 960 } 961 962 // Allow the index manager to call each of the currently active 963 // optional indexes with the block being disconnected so they 964 // can update themselves accordingly. 965 if b.indexManager != nil { 966 err := b.indexManager.DisconnectBlock(dbTx, block, parent, view) 967 if err != nil { 968 return err 969 } 970 } 971 972 return nil 973 }) 974 if err != nil { 975 return err 976 } 977 978 // Prune fully spent entries and mark all entries in the view unmodified 979 // now that the modifications have been committed to the database. 980 view.commit() 981 982 // This node's parent is now the end of the best chain. 983 b.bestChain.SetTip(node.parent) 984 985 // Update the state for the best block. Notice how this replaces the 986 // entire struct instead of updating the existing one. This effectively 987 // allows the old version to act as a snapshot which callers can use 988 // freely without needing to hold a lock for the duration. See the 989 // comments on the state variable for more details. 990 b.stateLock.Lock() 991 b.stateSnapshot = state 992 b.stateLock.Unlock() 993 994 // Assemble the current block and the parent into a slice. 995 blockAndParent := []*dcrutil.Block{block, parent} 996 997 // Notify the caller that the block was disconnected from the main 998 // chain. The caller would typically want to react with actions such as 999 // updating wallets. 1000 b.chainLock.Unlock() 1001 b.sendNotification(NTBlockDisconnected, blockAndParent) 1002 b.chainLock.Lock() 1003 1004 b.dropMainChainBlockCache(block) 1005 1006 return nil 1007 } 1008 1009 // countSpentRegularOutputs returns the number of utxos the regular transactions 1010 // in the passed block spend. 1011 func countSpentRegularOutputs(block *dcrutil.Block) int { 1012 // Skip the coinbase since it has no inputs. 1013 var numSpent int 1014 for _, tx := range block.MsgBlock().Transactions[1:] { 1015 numSpent += len(tx.TxIn) 1016 } 1017 return numSpent 1018 } 1019 1020 // countSpentStakeOutputs returns the number of utxos the stake transactions in 1021 // the passed block spend. 1022 func countSpentStakeOutputs(block *dcrutil.Block) int { 1023 var numSpent int 1024 for _, stx := range block.MsgBlock().STransactions { 1025 // Exclude the vote stakebase since it has no input. 1026 if stake.IsSSGen(stx) { 1027 numSpent++ 1028 continue 1029 } 1030 numSpent += len(stx.TxIn) 1031 } 1032 return numSpent 1033 } 1034 1035 // countSpentOutputs returns the number of utxos the passed block spends. 1036 func countSpentOutputs(block *dcrutil.Block) int { 1037 return countSpentRegularOutputs(block) + countSpentStakeOutputs(block) 1038 } 1039 1040 // reorganizeChainInternal attempts to reorganize the block chain to the 1041 // provided tip without attempting to undo failed reorgs. 1042 // 1043 // Since reorganizing to a new chain tip might involve validating blocks that 1044 // have not previously been validated, or attempting to reorganize to a branch 1045 // that is already known to be invalid, it possible for the reorganize to fail. 1046 // When that is the case, this function will return the error without attempting 1047 // to undo what has already been reorganized to that point. That means the best 1048 // chain tip will be set to some intermediate block along the reorg path and 1049 // will not actually be the best chain. This is acceptable because this 1050 // function is only intended to be called from the reorganizeChain function 1051 // which handles reorg failures by reorganizing back to the known good best 1052 // chain tip. 1053 // 1054 // A reorg entails disconnecting all blocks from the current best chain tip back 1055 // to the fork point between it and the provided target tip in reverse order 1056 // (think popping them off the end of the chain) and then connecting the blocks 1057 // on the new branch in forwards order (think pushing them onto the end of the 1058 // chain). 1059 // 1060 // This function may modify the validation state of nodes in the block index 1061 // without flushing in the case the chain is not able to reorganize due to a 1062 // block failing to connect. 1063 // 1064 // This function MUST be called with the chain state lock held (for writes). 1065 func (b *BlockChain) reorganizeChainInternal(targetTip *blockNode) error { 1066 // Find the fork point adding each block to a slice of blocks to attach 1067 // below once the current best chain has been disconnected. They are added 1068 // to the slice from back to front so that so they are attached in the 1069 // appropriate order when iterating the slice later. 1070 // 1071 // In the case a known invalid block is detected while constructing this 1072 // list, mark all of its descendants as having an invalid ancestor and 1073 // prevent the reorganize. 1074 fork := b.bestChain.FindFork(targetTip) 1075 attachNodes := make([]*blockNode, targetTip.height-fork.height) 1076 for n := targetTip; n != nil && n != fork; n = n.parent { 1077 if b.index.NodeStatus(n).KnownInvalid() { 1078 for _, dn := range attachNodes[n.height-fork.height:] { 1079 b.index.SetStatusFlags(dn, statusInvalidAncestor) 1080 } 1081 1082 str := fmt.Sprintf("block %s is known to be invalid or a "+ 1083 "descendant of an invalid block", n.hash) 1084 return ruleError(ErrKnownInvalidBlock, str) 1085 } 1086 1087 attachNodes[n.height-fork.height-1] = n 1088 } 1089 1090 // Disconnect all of the blocks back to the point of the fork. This entails 1091 // loading the blocks and their associated spent txos from the database and 1092 // using that information to unspend all of the spent txos and remove the 1093 // utxos created by the blocks. In addition, if a block votes against its 1094 // parent, the regular transactions are reconnected. 1095 tip := b.bestChain.Tip() 1096 view := NewUtxoViewpoint() 1097 view.SetBestHash(&tip.hash) 1098 var nextBlockToDetach *dcrutil.Block 1099 for tip != nil && tip != fork { 1100 // Grab the block to detach based on the node. Use the fact that the 1101 // blocks are being detached in reverse order, so the parent of the 1102 // current block being detached is the next one being detached. 1103 n := tip 1104 block := nextBlockToDetach 1105 if block == nil { 1106 var err error 1107 block, err = b.fetchMainChainBlockByNode(n) 1108 if err != nil { 1109 return err 1110 } 1111 } 1112 if n.hash != *block.Hash() { 1113 panicf("detach block node hash %v (height %v) does not match "+ 1114 "previous parent block hash %v", &n.hash, n.height, 1115 block.Hash()) 1116 } 1117 1118 // Grab the parent of the current block and also save a reference to it 1119 // as the next block to detach so it doesn't need to be loaded again on 1120 // the next iteration. 1121 parent, err := b.fetchMainChainBlockByNode(n.parent) 1122 if err != nil { 1123 return err 1124 } 1125 nextBlockToDetach = parent 1126 1127 // Load all of the spent txos for the block from the spend journal. 1128 var stxos []spentTxOut 1129 err = b.db.View(func(dbTx database.Tx) error { 1130 stxos, err = dbFetchSpendJournalEntry(dbTx, block) 1131 return err 1132 }) 1133 if err != nil { 1134 return err 1135 } 1136 1137 // Update the view to unspend all of the spent txos and remove the utxos 1138 // created by the block. Also, if the block votes against its parent, 1139 // reconnect all of the regular transactions. 1140 err = view.disconnectBlock(b.db, block, parent, stxos) 1141 if err != nil { 1142 return err 1143 } 1144 1145 // Update the database and chain state. 1146 err = b.disconnectBlock(n, block, parent, view) 1147 if err != nil { 1148 return err 1149 } 1150 1151 tip = n.parent 1152 } 1153 1154 // Load the fork block if there are blocks to attach and its not already 1155 // loaded which will be the case if no nodes were detached. The fork block 1156 // is used as the parent to the first node to be attached below. 1157 forkBlock := nextBlockToDetach 1158 if len(attachNodes) > 0 && forkBlock == nil { 1159 var err error 1160 forkBlock, err = b.fetchMainChainBlockByNode(tip) 1161 if err != nil { 1162 return err 1163 } 1164 } 1165 1166 // Attempt to connect each block that needs to be attached to the main 1167 // chain. This entails performing several checks to verify each block can 1168 // be connected without violating any consensus rules and updating the 1169 // relevant information related to the current chain state. 1170 var prevBlockAttached *dcrutil.Block 1171 for i, n := range attachNodes { 1172 // Grab the block to attach based on the node. Use the fact that the 1173 // parent of the block is either the fork point for the first node being 1174 // attached or the previous one that was attached for subsequent blocks 1175 // to optimize. 1176 block, err := b.fetchBlockByNode(n) 1177 if err != nil { 1178 return err 1179 } 1180 parent := forkBlock 1181 if i > 0 { 1182 parent = prevBlockAttached 1183 } 1184 if n.parent.hash != *parent.Hash() { 1185 panicf("attach block node hash %v (height %v) parent hash %v does "+ 1186 "not match previous parent block hash %v", &n.hash, n.height, 1187 &n.parent.hash, parent.Hash()) 1188 } 1189 1190 // Store the loaded block as parent of next iteration. 1191 prevBlockAttached = block 1192 1193 // Skip validation if the block is already known to be valid. However, 1194 // the utxo view still needs to be updated and the stxos are still 1195 // needed. 1196 stxos := make([]spentTxOut, 0, countSpentOutputs(block)) 1197 if b.index.NodeStatus(n).KnownValid() { 1198 // Update the view to mark all utxos referenced by the block as 1199 // spent and add all transactions being created by this block to it. 1200 // In the case the block votes against the parent, also disconnect 1201 // all of the regular transactions in the parent block. Finally, 1202 // provide an stxo slice so the spent txout details are generated. 1203 err := view.connectBlock(b.db, block, parent, &stxos) 1204 if err != nil { 1205 return err 1206 } 1207 } else { 1208 // In the case the block is determined to be invalid due to a rule 1209 // violation, mark it as invalid and mark all of its descendants as 1210 // having an invalid ancestor. 1211 err = b.checkConnectBlock(n, block, parent, view, &stxos) 1212 if err != nil { 1213 if _, ok := err.(RuleError); ok { 1214 b.index.SetStatusFlags(n, statusValidateFailed) 1215 for _, dn := range attachNodes[i+1:] { 1216 b.index.SetStatusFlags(dn, statusInvalidAncestor) 1217 } 1218 } 1219 return err 1220 } 1221 b.index.SetStatusFlags(n, statusValid) 1222 } 1223 1224 // Update the database and chain state. 1225 err = b.connectBlock(n, block, parent, view, stxos) 1226 if err != nil { 1227 return err 1228 } 1229 1230 tip = n 1231 } 1232 1233 return nil 1234 } 1235 1236 // reorganizeChain attempts to reorganize the block chain to the provided tip. 1237 // The tip must have already been determined to be on another branch by the 1238 // caller. Upon return, the chain will be fully reorganized to the provided tip 1239 // or an appropriate error will be returned and the chain will remain at the 1240 // same tip it was prior to calling this function. 1241 // 1242 // Reorganizing the chain entails disconnecting all blocks from the current best 1243 // chain tip back to the fork point between it and the provided target tip in 1244 // reverse order (think popping them off the end of the chain) and then 1245 // connecting the blocks on the new branch in forwards order (think pushing them 1246 // onto the end of the chain). 1247 // 1248 // This function may modify the validation state of nodes in the block index 1249 // without flushing in the case the chain is not able to reorganize due to a 1250 // block failing to connect. 1251 // 1252 // This function MUST be called with the chain state lock held (for writes). 1253 func (b *BlockChain) reorganizeChain(targetTip *blockNode) error { 1254 // Nothing to do if there is no target tip or the target tip is already the 1255 // current tip. 1256 if targetTip == nil { 1257 return nil 1258 } 1259 origTip := b.bestChain.Tip() 1260 if origTip == targetTip { 1261 return nil 1262 } 1263 1264 // Send a notification announcing the start of the chain reorganization. 1265 b.chainLock.Unlock() 1266 b.sendNotification(NTChainReorgStarted, nil) 1267 b.chainLock.Lock() 1268 1269 defer func() { 1270 // Send a notification announcing the end of the chain reorganization. 1271 b.chainLock.Unlock() 1272 b.sendNotification(NTChainReorgDone, nil) 1273 b.chainLock.Lock() 1274 }() 1275 1276 // Attempt to reorganize to the chain to the new tip. In the case it fails, 1277 // reorganize back to the original tip. There is no way to recover if the 1278 // chain fails to reorganize back to the original tip since something is 1279 // very wrong if a chain tip that was already known to be valid fails to 1280 // reconnect. 1281 // 1282 // NOTE: The failure handling makes an assumption that a block in the path 1283 // between the fork point and original tip are not somehow invalidated in 1284 // between the point a reorged chain fails to connect and the reorg back to 1285 // the original tip. That is a safe assumption with the current code due to 1286 // all modifications which mark blocks invalid being performed under the 1287 // chain lock, however, this will need to be reworked if that assumption is 1288 // violated. 1289 fork := b.bestChain.FindFork(targetTip) 1290 reorgErr := b.reorganizeChainInternal(targetTip) 1291 if reorgErr != nil { 1292 if err := b.reorganizeChainInternal(origTip); err != nil { 1293 panicf("failed to reorganize back to known good chain tip %s "+ 1294 "(height %d): %v -- probable database corruption", origTip.hash, 1295 origTip.height, err) 1296 } 1297 1298 return reorgErr 1299 } 1300 1301 // Send a notification that a blockchain reorganization took place. 1302 reorgData := &ReorganizationNtfnsData{origTip.hash, origTip.height, 1303 targetTip.hash, targetTip.height} 1304 b.chainLock.Unlock() 1305 b.sendNotification(NTReorganization, reorgData) 1306 b.chainLock.Lock() 1307 1308 // Log the point where the chain forked and old and new best chain tips. 1309 if fork != nil { 1310 log.Infof("REORGANIZE: Chain forks at %v (height %v)", fork.hash, 1311 fork.height) 1312 } 1313 log.Infof("REORGANIZE: Old best chain tip was %v (height %v)", 1314 &origTip.hash, origTip.height) 1315 log.Infof("REORGANIZE: New best chain tip is %v (height %v)", 1316 targetTip.hash, targetTip.height) 1317 1318 return nil 1319 } 1320 1321 // forceHeadReorganization forces a reorganization of the block chain to the 1322 // block hash requested, so long as it matches up with the current organization 1323 // of the best chain. 1324 // 1325 // This function may modify the validation state of nodes in the block index 1326 // without flushing. 1327 // 1328 // This function MUST be called with the chain state lock held (for writes). 1329 func (b *BlockChain) forceHeadReorganization(formerBest chainhash.Hash, newBest chainhash.Hash) error { 1330 if formerBest.IsEqual(&newBest) { 1331 return fmt.Errorf("can't reorganize to the same block") 1332 } 1333 formerBestNode := b.bestChain.Tip() 1334 1335 // We can't reorganize the chain unless our head block matches up with 1336 // b.bestChain. 1337 if !formerBestNode.hash.IsEqual(&formerBest) { 1338 return ruleError(ErrForceReorgWrongChain, "tried to force reorg "+ 1339 "on wrong chain") 1340 } 1341 1342 // Child to reorganize to is missing. 1343 newBestNode := b.index.LookupNode(&newBest) 1344 if newBestNode == nil || newBestNode.parent != formerBestNode.parent { 1345 return ruleError(ErrForceReorgMissingChild, "missing child of "+ 1346 "common parent for forced reorg") 1347 } 1348 1349 // Don't allow a reorganize to a known invalid chain. 1350 newBestNodeStatus := b.index.NodeStatus(newBestNode) 1351 if newBestNodeStatus.KnownInvalid() { 1352 return ruleError(ErrKnownInvalidBlock, "block is known to be invalid") 1353 } 1354 1355 // Reorganize the chain and flush any potential unsaved changes to the 1356 // block index to the database. It is safe to ignore any flushing 1357 // errors here as the only time the index will be modified is if the 1358 // block failed to connect. 1359 err := b.reorganizeChain(newBestNode) 1360 b.flushBlockIndexWarnOnly() 1361 return err 1362 } 1363 1364 // ForceHeadReorganization forces a reorganization of the block chain to the 1365 // block hash requested, so long as it matches up with the current organization 1366 // of the best chain. 1367 // 1368 // This function is safe for concurrent access. 1369 func (b *BlockChain) ForceHeadReorganization(formerBest chainhash.Hash, newBest chainhash.Hash) error { 1370 b.chainLock.Lock() 1371 err := b.forceHeadReorganization(formerBest, newBest) 1372 b.chainLock.Unlock() 1373 return err 1374 } 1375 1376 // flushBlockIndex populates any ticket data that has been pruned from modified 1377 // block nodes, writes those nodes to the database and clears the set of 1378 // modified nodes if it succeeds. 1379 func (b *BlockChain) flushBlockIndex() error { 1380 b.index.RLock() 1381 for node := range b.index.modified { 1382 if err := b.maybeFetchTicketInfo(node); err != nil { 1383 b.index.RUnlock() 1384 return err 1385 } 1386 } 1387 b.index.RUnlock() 1388 1389 return b.index.flush() 1390 } 1391 1392 // flushBlockIndexWarnOnly attempts to flush and modified block index nodes to 1393 // the database and will log a warning if it fails. 1394 // 1395 // NOTE: This MUST only be used in the specific circumstances where failure to 1396 // flush only results in a worst case scenario of requiring one or more blocks 1397 // to be validated again. All other cases must directly call the function on 1398 // the block index and check the error return accordingly. 1399 func (b *BlockChain) flushBlockIndexWarnOnly() { 1400 if err := b.flushBlockIndex(); err != nil { 1401 log.Warnf("Unable to flush block index changes to db: %v", err) 1402 } 1403 } 1404 1405 // connectBestChain handles connecting the passed block to the chain while 1406 // respecting proper chain selection according to the chain with the most 1407 // proof of work. In the typical case, the new block simply extends the main 1408 // chain. However, it may also be extending (or creating) a side chain (fork) 1409 // which may or may not end up becoming the main chain depending on which fork 1410 // cumulatively has the most proof of work. It returns the resulting fork 1411 // length, that is to say the number of blocks to the fork point from the main 1412 // chain, which will be zero if the block ends up on the main chain (either 1413 // due to extending the main chain or causing a reorganization to become the 1414 // main chain). 1415 // 1416 // The flags modify the behavior of this function as follows: 1417 // - BFFastAdd: Avoids several expensive transaction validation operations. 1418 // This is useful when using checkpoints. 1419 // 1420 // This function MUST be called with the chain state lock held (for writes). 1421 func (b *BlockChain) connectBestChain(node *blockNode, block, parent *dcrutil.Block, flags BehaviorFlags) (int64, error) { 1422 fastAdd := flags&BFFastAdd == BFFastAdd 1423 1424 // Ensure the passed parent is actually the parent of the block. 1425 if *parent.Hash() != node.parent.hash { 1426 panicf("parent block %v (height %v) does not match expected parent %v "+ 1427 "(height %v)", parent.Hash(), parent.MsgBlock().Header.Height, 1428 node.parent.hash, node.height-1) 1429 } 1430 1431 // We are extending the main (best) chain with a new block. This is the 1432 // most common case. 1433 parentHash := &block.MsgBlock().Header.PrevBlock 1434 tip := b.bestChain.Tip() 1435 if *parentHash == tip.hash { 1436 // Skip expensive checks if the block has already been fully 1437 // validated. 1438 isKnownValid := b.index.NodeStatus(node).KnownValid() 1439 fastAdd = fastAdd || isKnownValid 1440 1441 // Perform several checks to verify the block can be connected 1442 // to the main chain without violating any rules and without 1443 // actually connecting the block. 1444 // 1445 // Also, set the applicable status result in the block index, 1446 // and flush the status changes to the database. It is safe to 1447 // ignore any errors when flushing here as the changes will be 1448 // flushed when a valid block is connected, and the worst case 1449 // scenario if a block a invalid is it would need to be 1450 // revalidated after a restart. 1451 view := NewUtxoViewpoint() 1452 view.SetBestHash(parentHash) 1453 var stxos []spentTxOut 1454 if !fastAdd { 1455 err := b.checkConnectBlock(node, block, parent, view, 1456 &stxos) 1457 if err != nil { 1458 if _, ok := err.(RuleError); ok { 1459 b.index.SetStatusFlags(node, statusValidateFailed) 1460 b.flushBlockIndexWarnOnly() 1461 } 1462 return 0, err 1463 } 1464 } 1465 if !isKnownValid { 1466 b.index.SetStatusFlags(node, statusValid) 1467 b.flushBlockIndexWarnOnly() 1468 } 1469 1470 // In the fast add case the code to check the block connection 1471 // was skipped, so the utxo view needs to load the referenced 1472 // utxos, spend them, and add the new utxos being created by 1473 // this block. Also, in the case the the block votes against 1474 // the parent, its regular transaction tree must be 1475 // disconnected. 1476 if fastAdd { 1477 err := view.connectBlock(b.db, block, parent, &stxos) 1478 if err != nil { 1479 return 0, err 1480 } 1481 } 1482 1483 // Connect the block to the main chain. 1484 err := b.connectBlock(node, block, parent, view, stxos) 1485 if err != nil { 1486 return 0, err 1487 } 1488 1489 validateStr := "validating" 1490 if !voteBitsApproveParent(node.voteBits) { 1491 validateStr = "invalidating" 1492 } 1493 1494 log.Debugf("Block %v (height %v) connected to the main chain, "+ 1495 "%v the previous block", node.hash, node.height, 1496 validateStr) 1497 1498 // The fork length is zero since the block is now the tip of the 1499 // best chain. 1500 return 0, nil 1501 } 1502 if fastAdd { 1503 log.Warnf("fastAdd set in the side chain case? %v\n", 1504 block.Hash()) 1505 } 1506 1507 // We're extending (or creating) a side chain, but the cumulative 1508 // work for this new side chain is not enough to make it the new chain. 1509 if node.workSum.Cmp(tip.workSum) <= 0 { 1510 // Log information about how the block is forking the chain. 1511 fork := b.bestChain.FindFork(node) 1512 if fork.hash == *parentHash { 1513 log.Infof("FORK: Block %v (height %v) forks the chain at height "+ 1514 "%d/block %v, but does not cause a reorganize", 1515 node.hash, node.height, fork.height, fork.hash) 1516 } else { 1517 log.Infof("EXTEND FORK: Block %v (height %v) extends a side chain "+ 1518 "which forks the chain at height %d/block %v", node.hash, 1519 node.height, fork.height, fork.hash) 1520 } 1521 1522 forkLen := node.height - fork.height 1523 return forkLen, nil 1524 } 1525 1526 // We're extending (or creating) a side chain and the cumulative work 1527 // for this new side chain is more than the old best chain, so this side 1528 // chain needs to become the main chain. In order to accomplish that, 1529 // find the common ancestor of both sides of the fork, disconnect the 1530 // blocks that form the (now) old fork from the main chain, and attach 1531 // the blocks that form the new chain to the main chain starting at the 1532 // common ancestor (the point where the chain forked). 1533 // 1534 // Reorganize the chain and flush any potential unsaved changes to the 1535 // block index to the database. It is safe to ignore any flushing 1536 // errors here as the only time the index will be modified is if the 1537 // block failed to connect. 1538 log.Infof("REORGANIZE: Block %v is causing a reorganize.", node.hash) 1539 err := b.reorganizeChain(node) 1540 b.flushBlockIndexWarnOnly() 1541 if err != nil { 1542 return 0, err 1543 } 1544 1545 // The fork length is zero since the block is now the tip of the best 1546 // chain. 1547 return 0, nil 1548 } 1549 1550 // isCurrent returns whether or not the chain believes it is current. Several 1551 // factors are used to guess, but the key factors that allow the chain to 1552 // believe it is current are: 1553 // - Latest block height is after the latest checkpoint (if enabled) 1554 // - Latest block has a timestamp newer than 24 hours ago 1555 // 1556 // This function MUST be called with the chain state lock held (for reads). 1557 func (b *BlockChain) isCurrent() bool { 1558 // Not current if the latest main (best) chain height is before the 1559 // latest known good checkpoint (when checkpoints are enabled). 1560 tip := b.bestChain.Tip() 1561 checkpoint := b.latestCheckpoint() 1562 if checkpoint != nil && tip.height < checkpoint.Height { 1563 return false 1564 } 1565 1566 // Not current if the latest best block has a timestamp before 24 hours 1567 // ago. 1568 // 1569 // The chain appears to be current if none of the checks reported 1570 // otherwise. 1571 minus24Hours := b.timeSource.AdjustedTime().Add(-24 * time.Hour).Unix() 1572 return tip.timestamp >= minus24Hours 1573 } 1574 1575 // IsCurrent returns whether or not the chain believes it is current. Several 1576 // factors are used to guess, but the key factors that allow the chain to 1577 // believe it is current are: 1578 // - Latest block height is after the latest checkpoint (if enabled) 1579 // - Latest block has a timestamp newer than 24 hours ago 1580 // 1581 // This function is safe for concurrent access. 1582 func (b *BlockChain) IsCurrent() bool { 1583 b.chainLock.RLock() 1584 defer b.chainLock.RUnlock() 1585 1586 return b.isCurrent() 1587 } 1588 1589 // BestSnapshot returns information about the current best chain block and 1590 // related state as of the current point in time. The returned instance must be 1591 // treated as immutable since it is shared by all callers. 1592 // 1593 // This function is safe for concurrent access. 1594 func (b *BlockChain) BestSnapshot() *BestState { 1595 b.stateLock.RLock() 1596 snapshot := b.stateSnapshot 1597 b.stateLock.RUnlock() 1598 return snapshot 1599 } 1600 1601 // MaximumBlockSize returns the maximum permitted block size for the block 1602 // AFTER the given node. 1603 // 1604 // This function MUST be called with the chain state lock held (for reads). 1605 func (b *BlockChain) maxBlockSize(prevNode *blockNode) (int64, error) { 1606 // Determine the correct deployment version for the block size consensus 1607 // vote or treat it as active when voting is not enabled for the current 1608 // network. 1609 const deploymentID = chaincfg.VoteIDMaxBlockSize 1610 deploymentVer, ok := b.deploymentVers[deploymentID] 1611 if !ok { 1612 return int64(b.chainParams.MaximumBlockSizes[0]), nil 1613 } 1614 1615 // Return the larger block size if the stake vote for the max block size 1616 // increase agenda is active. 1617 // 1618 // NOTE: The choice field of the return threshold state is not examined 1619 // here because there is only one possible choice that can be active 1620 // for the agenda, which is yes, so there is no need to check it. 1621 maxSize := int64(b.chainParams.MaximumBlockSizes[0]) 1622 state, err := b.deploymentState(prevNode, deploymentVer, deploymentID) 1623 if err != nil { 1624 return maxSize, err 1625 } 1626 if state.State == ThresholdActive { 1627 return int64(b.chainParams.MaximumBlockSizes[1]), nil 1628 } 1629 1630 // The max block size is not changed in any other cases. 1631 return maxSize, nil 1632 } 1633 1634 // MaxBlockSize returns the maximum permitted block size for the block AFTER 1635 // the end of the current best chain. 1636 // 1637 // This function is safe for concurrent access. 1638 func (b *BlockChain) MaxBlockSize() (int64, error) { 1639 b.chainLock.Lock() 1640 maxSize, err := b.maxBlockSize(b.bestChain.Tip()) 1641 b.chainLock.Unlock() 1642 return maxSize, err 1643 } 1644 1645 // HeaderByHash returns the block header identified by the given hash or an 1646 // error if it doesn't exist. Note that this will return headers from both the 1647 // main chain and any side chains. 1648 // 1649 // This function is safe for concurrent access. 1650 func (b *BlockChain) HeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error) { 1651 node := b.index.LookupNode(hash) 1652 if node == nil { 1653 return wire.BlockHeader{}, fmt.Errorf("block %s is not known", hash) 1654 } 1655 1656 return node.Header(), nil 1657 } 1658 1659 // HeaderByHeight returns the block header at the given height in the main 1660 // chain. 1661 // 1662 // This function is safe for concurrent access. 1663 func (b *BlockChain) HeaderByHeight(height int64) (wire.BlockHeader, error) { 1664 node := b.bestChain.NodeByHeight(height) 1665 if node == nil { 1666 str := fmt.Sprintf("no block at height %d exists", height) 1667 return wire.BlockHeader{}, errNotInMainChain(str) 1668 } 1669 1670 return node.Header(), nil 1671 } 1672 1673 // BlockByHash searches the internal chain block stores and the database in an 1674 // attempt to find the requested block and returns it. This function returns 1675 // blocks regardless of whether or not they are part of the main chain. 1676 // 1677 // This function is safe for concurrent access. 1678 func (b *BlockChain) BlockByHash(hash *chainhash.Hash) (*dcrutil.Block, error) { 1679 node := b.index.LookupNode(hash) 1680 if node == nil || !b.index.NodeStatus(node).HaveData() { 1681 return nil, fmt.Errorf("block %s is not known", hash) 1682 } 1683 1684 // Return the block from either cache or the database. 1685 return b.fetchBlockByNode(node) 1686 } 1687 1688 // BlockByHeight returns the block at the given height in the main chain. 1689 // 1690 // This function is safe for concurrent access. 1691 func (b *BlockChain) BlockByHeight(height int64) (*dcrutil.Block, error) { 1692 // Lookup the block height in the best chain. 1693 node := b.bestChain.NodeByHeight(height) 1694 if node == nil { 1695 str := fmt.Sprintf("no block at height %d exists", height) 1696 return nil, errNotInMainChain(str) 1697 } 1698 1699 // Return the block from either cache or the database. Note that this is 1700 // not using fetchMainChainBlockByNode since the main chain check has 1701 // already been done. 1702 return b.fetchBlockByNode(node) 1703 } 1704 1705 // MainChainHasBlock returns whether or not the block with the given hash is in 1706 // the main chain. 1707 // 1708 // This function is safe for concurrent access. 1709 func (b *BlockChain) MainChainHasBlock(hash *chainhash.Hash) bool { 1710 node := b.index.LookupNode(hash) 1711 return node != nil && b.bestChain.Contains(node) 1712 } 1713 1714 // BlockHeightByHash returns the height of the block with the given hash in the 1715 // main chain. 1716 // 1717 // This function is safe for concurrent access. 1718 func (b *BlockChain) BlockHeightByHash(hash *chainhash.Hash) (int64, error) { 1719 node := b.index.LookupNode(hash) 1720 if node == nil || !b.bestChain.Contains(node) { 1721 str := fmt.Sprintf("block %s is not in the main chain", hash) 1722 return 0, errNotInMainChain(str) 1723 } 1724 1725 return node.height, nil 1726 } 1727 1728 // BlockHashByHeight returns the hash of the block at the given height in the 1729 // main chain. 1730 // 1731 // This function is safe for concurrent access. 1732 func (b *BlockChain) BlockHashByHeight(height int64) (*chainhash.Hash, error) { 1733 node := b.bestChain.NodeByHeight(height) 1734 if node == nil { 1735 str := fmt.Sprintf("no block at height %d exists", height) 1736 return nil, errNotInMainChain(str) 1737 } 1738 1739 return &node.hash, nil 1740 } 1741 1742 // HeightRange returns a range of block hashes for the given start and end 1743 // heights. It is inclusive of the start height and exclusive of the end 1744 // height. In other words, it is the half open range [startHeight, endHeight). 1745 // 1746 // The end height will be limited to the current main chain height. 1747 // 1748 // This function is safe for concurrent access. 1749 func (b *BlockChain) HeightRange(startHeight, endHeight int64) ([]chainhash.Hash, error) { 1750 // Ensure requested heights are sane. 1751 if startHeight < 0 { 1752 return nil, fmt.Errorf("start height of fetch range must not "+ 1753 "be less than zero - got %d", startHeight) 1754 } 1755 if endHeight < startHeight { 1756 return nil, fmt.Errorf("end height of fetch range must not "+ 1757 "be less than the start height - got start %d, end %d", 1758 startHeight, endHeight) 1759 } 1760 1761 // There is nothing to do when the start and end heights are the same, 1762 // so return now to avoid extra work. 1763 if startHeight == endHeight { 1764 return nil, nil 1765 } 1766 1767 // When the requested start height is after the most recent best chain 1768 // height, there is nothing to do. 1769 latestHeight := b.bestChain.Tip().height 1770 if startHeight > latestHeight { 1771 return nil, nil 1772 } 1773 1774 // Limit the ending height to the latest height of the chain. 1775 if endHeight > latestHeight+1 { 1776 endHeight = latestHeight + 1 1777 } 1778 1779 // Fetch as many as are available within the specified range. 1780 hashes := make([]chainhash.Hash, endHeight-startHeight) 1781 iterNode := b.bestChain.NodeByHeight(endHeight - 1) 1782 for i := startHeight; i < endHeight; i++ { 1783 // Since the desired result is from the starting node to the 1784 // ending node in forward order, but they are iterated in 1785 // reverse, add them in reverse order. 1786 hashes[endHeight-i-1] = iterNode.hash 1787 iterNode = iterNode.parent 1788 } 1789 return hashes, nil 1790 } 1791 1792 // locateInventory returns the node of the block after the first known block in 1793 // the locator along with the number of subsequent nodes needed to either reach 1794 // the provided stop hash or the provided max number of entries. 1795 // 1796 // In addition, there are two special cases: 1797 // 1798 // - When no locators are provided, the stop hash is treated as a request for 1799 // that block, so it will either return the node associated with the stop hash 1800 // if it is known, or nil if it is unknown 1801 // - When locators are provided, but none of them are known, nodes starting 1802 // after the genesis block will be returned 1803 // 1804 // This is primarily a helper function for the locateBlocks and locateHeaders 1805 // functions. 1806 // 1807 // This function MUST be called with the chain state lock held (for reads). 1808 func (b *BlockChain) locateInventory(locator BlockLocator, hashStop *chainhash.Hash, maxEntries uint32) (*blockNode, uint32) { 1809 // There are no block locators so a specific block is being requested 1810 // as identified by the stop hash. 1811 stopNode := b.index.LookupNode(hashStop) 1812 if len(locator) == 0 { 1813 if stopNode == nil { 1814 // No blocks with the stop hash were found so there is 1815 // nothing to do. 1816 return nil, 0 1817 } 1818 return stopNode, 1 1819 } 1820 1821 // Find the most recent locator block hash in the main chain. In the 1822 // case none of the hashes in the locator are in the main chain, fall 1823 // back to the genesis block. 1824 startNode := b.bestChain.Genesis() 1825 for _, hash := range locator { 1826 node := b.index.LookupNode(hash) 1827 if node != nil && b.bestChain.Contains(node) { 1828 startNode = node 1829 break 1830 } 1831 } 1832 1833 // Start at the block after the most recently known block. When there 1834 // is no next block it means the most recently known block is the tip of 1835 // the best chain, so there is nothing more to do. 1836 startNode = b.bestChain.Next(startNode) 1837 if startNode == nil { 1838 return nil, 0 1839 } 1840 1841 // Calculate how many entries are needed. 1842 total := uint32((b.bestChain.Tip().height - startNode.height) + 1) 1843 if stopNode != nil && b.bestChain.Contains(stopNode) && 1844 stopNode.height >= startNode.height { 1845 1846 total = uint32((stopNode.height - startNode.height) + 1) 1847 } 1848 if total > maxEntries { 1849 total = maxEntries 1850 } 1851 1852 return startNode, total 1853 } 1854 1855 // locateBlocks returns the hashes of the blocks after the first known block in 1856 // the locator until the provided stop hash is reached, or up to the provided 1857 // max number of block hashes. 1858 // 1859 // See the comment on the exported function for more details on special cases. 1860 // 1861 // This function MUST be called with the chain state lock held (for reads). 1862 func (b *BlockChain) locateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash { 1863 // Find the node after the first known block in the locator and the 1864 // total number of nodes after it needed while respecting the stop hash 1865 // and max entries. 1866 node, total := b.locateInventory(locator, hashStop, maxHashes) 1867 if total == 0 { 1868 return nil 1869 } 1870 1871 // Populate and return the found hashes. 1872 hashes := make([]chainhash.Hash, 0, total) 1873 for i := uint32(0); i < total; i++ { 1874 hashes = append(hashes, node.hash) 1875 node = b.bestChain.Next(node) 1876 } 1877 return hashes 1878 } 1879 1880 // LocateBlocks returns the hashes of the blocks after the first known block in 1881 // the locator until the provided stop hash is reached, or up to the provided 1882 // max number of block hashes. 1883 // 1884 // In addition, there are two special cases: 1885 // 1886 // - When no locators are provided, the stop hash is treated as a request for 1887 // that block, so it will either return the stop hash itself if it is known, 1888 // or nil if it is unknown 1889 // - When locators are provided, but none of them are known, hashes starting 1890 // after the genesis block will be returned 1891 // 1892 // This function is safe for concurrent access. 1893 func (b *BlockChain) LocateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash { 1894 b.chainLock.RLock() 1895 hashes := b.locateBlocks(locator, hashStop, maxHashes) 1896 b.chainLock.RUnlock() 1897 return hashes 1898 } 1899 1900 // locateHeaders returns the headers of the blocks after the first known block 1901 // in the locator until the provided stop hash is reached, or up to the provided 1902 // max number of block headers. 1903 // 1904 // See the comment on the exported function for more details on special cases. 1905 // 1906 // This function MUST be called with the chain state lock held (for reads). 1907 func (b *BlockChain) locateHeaders(locator BlockLocator, hashStop *chainhash.Hash, maxHeaders uint32) []wire.BlockHeader { 1908 // Find the node after the first known block in the locator and the 1909 // total number of nodes after it needed while respecting the stop hash 1910 // and max entries. 1911 node, total := b.locateInventory(locator, hashStop, maxHeaders) 1912 if total == 0 { 1913 return nil 1914 } 1915 1916 // Populate and return the found headers. 1917 headers := make([]wire.BlockHeader, 0, total) 1918 for i := uint32(0); i < total; i++ { 1919 headers = append(headers, node.Header()) 1920 node = b.bestChain.Next(node) 1921 } 1922 return headers 1923 } 1924 1925 // LocateHeaders returns the headers of the blocks after the first known block 1926 // in the locator until the provided stop hash is reached, or up to a max of 1927 // wire.MaxBlockHeadersPerMsg headers. 1928 // 1929 // In addition, there are two special cases: 1930 // 1931 // - When no locators are provided, the stop hash is treated as a request for 1932 // that header, so it will either return the header for the stop hash itself 1933 // if it is known, or nil if it is unknown 1934 // - When locators are provided, but none of them are known, headers starting 1935 // after the genesis block will be returned 1936 // 1937 // This function is safe for concurrent access. 1938 func (b *BlockChain) LocateHeaders(locator BlockLocator, hashStop *chainhash.Hash) []wire.BlockHeader { 1939 b.chainLock.RLock() 1940 headers := b.locateHeaders(locator, hashStop, wire.MaxBlockHeadersPerMsg) 1941 b.chainLock.RUnlock() 1942 return headers 1943 } 1944 1945 // BlockLocatorFromHash returns a block locator for the passed block hash. 1946 // See BlockLocator for details on the algorithm used to create a block locator. 1947 // 1948 // In addition to the general algorithm referenced above, this function will 1949 // return the block locator for the latest known tip of the main (best) chain if 1950 // the passed hash is not currently known. 1951 // 1952 // This function is safe for concurrent access. 1953 func (b *BlockChain) BlockLocatorFromHash(hash *chainhash.Hash) BlockLocator { 1954 b.chainLock.RLock() 1955 node := b.index.LookupNode(hash) 1956 locator := b.bestChain.BlockLocator(node) 1957 b.chainLock.RUnlock() 1958 return locator 1959 } 1960 1961 // LatestBlockLocator returns a block locator for the latest known tip of the 1962 // main (best) chain. 1963 // 1964 // This function is safe for concurrent access. 1965 func (b *BlockChain) LatestBlockLocator() (BlockLocator, error) { 1966 b.chainLock.RLock() 1967 locator := b.bestChain.BlockLocator(nil) 1968 b.chainLock.RUnlock() 1969 return locator, nil 1970 } 1971 1972 // extractDeploymentIDVersions returns a map of all deployment IDs within the 1973 // provided params to the deployment version for which they are defined. An 1974 // error is returned if a duplicate ID is encountered. 1975 func extractDeploymentIDVersions(params *chaincfg.Params) (map[string]uint32, error) { 1976 // Generate a deployment ID to version map from the provided params. 1977 deploymentVers := make(map[string]uint32) 1978 for version, deployments := range params.Deployments { 1979 for _, deployment := range deployments { 1980 id := deployment.Vote.Id 1981 if _, ok := deploymentVers[id]; ok { 1982 return nil, DuplicateDeploymentError(id) 1983 } 1984 deploymentVers[id] = version 1985 } 1986 } 1987 1988 return deploymentVers, nil 1989 } 1990 1991 // IndexManager provides a generic interface that the is called when blocks are 1992 // connected and disconnected to and from the tip of the main chain for the 1993 // purpose of supporting optional indexes. 1994 type IndexManager interface { 1995 // Init is invoked during chain initialize in order to allow the index 1996 // manager to initialize itself and any indexes it is managing. The 1997 // channel parameter specifies a channel the caller can close to signal 1998 // that the process should be interrupted. It can be nil if that 1999 // behavior is not desired. 2000 Init(*BlockChain, <-chan struct{}) error 2001 2002 // ConnectBlock is invoked when a new block has been connected to the 2003 // main chain. 2004 ConnectBlock(database.Tx, *dcrutil.Block, *dcrutil.Block, *UtxoViewpoint) error 2005 2006 // DisconnectBlock is invoked when a block has been disconnected from 2007 // the main chain. 2008 DisconnectBlock(database.Tx, *dcrutil.Block, *dcrutil.Block, *UtxoViewpoint) error 2009 } 2010 2011 // Config is a descriptor which specifies the blockchain instance configuration. 2012 type Config struct { 2013 // DB defines the database which houses the blocks and will be used to 2014 // store all metadata created by this package such as the utxo set. 2015 // 2016 // This field is required. 2017 DB database.DB 2018 2019 // Interrupt specifies a channel the caller can close to signal that 2020 // long running operations, such as catching up indexes or performing 2021 // database migrations, should be interrupted. 2022 // 2023 // This field can be nil if the caller does not desire the behavior. 2024 Interrupt <-chan struct{} 2025 2026 // ChainParams identifies which chain parameters the chain is associated 2027 // with. 2028 // 2029 // This field is required. 2030 ChainParams *chaincfg.Params 2031 2032 // TimeSource defines the median time source to use for things such as 2033 // block processing and determining whether or not the chain is current. 2034 // 2035 // The caller is expected to keep a reference to the time source as well 2036 // and add time samples from other peers on the network so the local 2037 // time is adjusted to be in agreement with other peers. 2038 TimeSource MedianTimeSource 2039 2040 // Notifications defines a callback to which notifications will be sent 2041 // when various events take place. See the documentation for 2042 // Notification and NotificationType for details on the types and 2043 // contents of notifications. 2044 // 2045 // This field can be nil if the caller is not interested in receiving 2046 // notifications. 2047 Notifications NotificationCallback 2048 2049 // SigCache defines a signature cache to use when when validating 2050 // signatures. This is typically most useful when individual 2051 // transactions are already being validated prior to their inclusion in 2052 // a block such as what is usually done via a transaction memory pool. 2053 // 2054 // This field can be nil if the caller is not interested in using a 2055 // signature cache. 2056 SigCache *txscript.SigCache 2057 2058 // IndexManager defines an index manager to use when initializing the 2059 // chain and connecting and disconnecting blocks. 2060 // 2061 // This field can be nil if the caller does not wish to make use of an 2062 // index manager. 2063 IndexManager IndexManager 2064 } 2065 2066 // New returns a BlockChain instance using the provided configuration details. 2067 func New(config *Config) (*BlockChain, error) { 2068 // Enforce required config fields. 2069 if config.DB == nil { 2070 return nil, AssertError("blockchain.New database is nil") 2071 } 2072 if config.ChainParams == nil { 2073 return nil, AssertError("blockchain.New chain parameters nil") 2074 } 2075 2076 // Generate a checkpoint by height map from the provided checkpoints. 2077 params := config.ChainParams 2078 var checkpointsByHeight map[int64]*chaincfg.Checkpoint 2079 if len(params.Checkpoints) > 0 { 2080 checkpointsByHeight = make(map[int64]*chaincfg.Checkpoint) 2081 for i := range params.Checkpoints { 2082 checkpoint := ¶ms.Checkpoints[i] 2083 checkpointsByHeight[checkpoint.Height] = checkpoint 2084 } 2085 } 2086 2087 // Generate a deployment ID to version map from the provided params. 2088 deploymentVers, err := extractDeploymentIDVersions(params) 2089 if err != nil { 2090 return nil, err 2091 } 2092 2093 b := BlockChain{ 2094 checkpointsByHeight: checkpointsByHeight, 2095 deploymentVers: deploymentVers, 2096 db: config.DB, 2097 chainParams: params, 2098 timeSource: config.TimeSource, 2099 notifications: config.Notifications, 2100 sigCache: config.SigCache, 2101 indexManager: config.IndexManager, 2102 interrupt: config.Interrupt, 2103 index: newBlockIndex(config.DB), 2104 bestChain: newChainView(nil), 2105 orphans: make(map[chainhash.Hash]*orphanBlock), 2106 prevOrphans: make(map[chainhash.Hash][]*orphanBlock), 2107 mainchainBlockCache: make(map[chainhash.Hash]*dcrutil.Block), 2108 mainchainBlockCacheSize: mainchainBlockCacheSize, 2109 deploymentCaches: newThresholdCaches(params), 2110 isVoterMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), 2111 isStakeMajorityVersionCache: make(map[[stakeMajorityCacheKeySize]byte]bool), 2112 calcPriorStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), 2113 calcVoterVersionIntervalCache: make(map[[chainhash.HashSize]byte]uint32), 2114 calcStakeVersionCache: make(map[[chainhash.HashSize]byte]uint32), 2115 } 2116 2117 // Initialize the chain state from the passed database. When the db 2118 // does not yet contain any chain state, both it and the chain state 2119 // will be initialized to contain only the genesis block. 2120 if err := b.initChainState(); err != nil { 2121 return nil, err 2122 } 2123 2124 // Initialize and catch up all of the currently active optional indexes 2125 // as needed. 2126 if config.IndexManager != nil { 2127 err := config.IndexManager.Init(&b, config.Interrupt) 2128 if err != nil { 2129 return nil, err 2130 } 2131 } 2132 2133 b.subsidyCache = standalone.NewSubsidyCache(&subsidyParams{b.chainParams}) 2134 b.pruner = newChainPruner(&b) 2135 2136 // The version 5 database upgrade requires a full reindex. Perform, or 2137 // resume, the reindex as needed. 2138 if err := b.maybeFinishV5Upgrade(); err != nil { 2139 return nil, err 2140 } 2141 2142 log.Infof("Blockchain database version info: chain: %d, compression: "+ 2143 "%d, block index: %d", b.dbInfo.version, b.dbInfo.compVer, 2144 b.dbInfo.bidxVer) 2145 2146 tip := b.bestChain.Tip() 2147 log.Infof("Chain state: height %d, hash %v, total transactions %d, "+ 2148 "work %v, stake version %v", tip.height, tip.hash, 2149 b.stateSnapshot.TotalTxns, tip.workSum, 0) 2150 2151 return &b, nil 2152 }