github.com/palcoin-project/palcd@v1.0.0/blockchain/chain.go (about) 1 // Copyright (c) 2013-2018 The btcsuite developers 2 // Copyright (c) 2015-2018 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 "container/list" 10 "fmt" 11 "sync" 12 "time" 13 14 "github.com/palcoin-project/palcd/chaincfg" 15 "github.com/palcoin-project/palcd/chaincfg/chainhash" 16 "github.com/palcoin-project/palcd/database" 17 "github.com/palcoin-project/palcd/txscript" 18 "github.com/palcoin-project/palcd/wire" 19 "github.com/palcoin-project/palcutil" 20 ) 21 22 const ( 23 // maxOrphanBlocks is the maximum number of orphan blocks that can be 24 // queued. 25 maxOrphanBlocks = 100 26 ) 27 28 // BlockLocator is used to help locate a specific block. The algorithm for 29 // building the block locator is to add the hashes in reverse order until 30 // the genesis block is reached. In order to keep the list of locator hashes 31 // to a reasonable number of entries, first the most recent previous 12 block 32 // hashes are added, then the step is doubled each loop iteration to 33 // exponentially decrease the number of hashes as a function of the distance 34 // from the block being located. 35 // 36 // For example, assume a block chain with a side chain as depicted below: 37 // genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18 38 // \-> 16a -> 17a 39 // 40 // The block locator for block 17a would be the hashes of blocks: 41 // [17a 16a 15 14 13 12 11 10 9 8 7 6 4 genesis] 42 type BlockLocator []*chainhash.Hash 43 44 // orphanBlock represents a block that we don't yet have the parent for. It 45 // is a normal block plus an expiration time to prevent caching the orphan 46 // forever. 47 type orphanBlock struct { 48 block *palcutil.Block 49 expiration time.Time 50 } 51 52 // BestState houses information about the current best block and other info 53 // related to the state of the main chain as it exists from the point of view of 54 // the current best block. 55 // 56 // The BestSnapshot method can be used to obtain access to this information 57 // in a concurrent safe manner and the data will not be changed out from under 58 // the caller when chain state changes occur as the function name implies. 59 // However, the returned snapshot must be treated as immutable since it is 60 // shared by all callers. 61 type BestState struct { 62 Hash chainhash.Hash // The hash of the block. 63 Height int32 // The height of the block. 64 Bits uint32 // The difficulty bits of the block. 65 BlockSize uint64 // The size of the block. 66 BlockWeight uint64 // The weight of the block. 67 NumTxns uint64 // The number of txns in the block. 68 TotalTxns uint64 // The total number of txns in the chain. 69 MedianTime time.Time // Median time as per CalcPastMedianTime. 70 } 71 72 // newBestState returns a new best stats instance for the given parameters. 73 func newBestState(node *blockNode, blockSize, blockWeight, numTxns, 74 totalTxns uint64, medianTime time.Time) *BestState { 75 76 return &BestState{ 77 Hash: node.hash, 78 Height: node.height, 79 Bits: node.bits, 80 BlockSize: blockSize, 81 BlockWeight: blockWeight, 82 NumTxns: numTxns, 83 TotalTxns: totalTxns, 84 MedianTime: medianTime, 85 } 86 } 87 88 // BlockChain provides functions for working with the bitcoin block chain. 89 // It includes functionality such as rejecting duplicate blocks, ensuring blocks 90 // follow all rules, orphan handling, checkpoint handling, and best chain 91 // selection with reorganization. 92 type BlockChain struct { 93 // The following fields are set when the instance is created and can't 94 // be changed afterwards, so there is no need to protect them with a 95 // separate mutex. 96 checkpoints []chaincfg.Checkpoint 97 checkpointsByHeight map[int32]*chaincfg.Checkpoint 98 db database.DB 99 chainParams *chaincfg.Params 100 timeSource MedianTimeSource 101 sigCache *txscript.SigCache 102 indexManager IndexManager 103 hashCache *txscript.HashCache 104 105 // The following fields are calculated based upon the provided chain 106 // parameters. They are also set when the instance is created and 107 // can't be changed afterwards, so there is no need to protect them with 108 // a separate mutex. 109 minRetargetTimespan int64 // target timespan / adjustment factor 110 maxRetargetTimespan int64 // target timespan * adjustment factor 111 blocksPerRetarget int32 // target timespan / target time per block 112 113 // chainLock protects concurrent access to the vast majority of the 114 // fields in this struct below this point. 115 chainLock sync.RWMutex 116 117 // These fields are related to the memory block index. They both have 118 // their own locks, however they are often also protected by the chain 119 // lock to help prevent logic races when blocks are being processed. 120 // 121 // index houses the entire block index in memory. The block index is 122 // a tree-shaped structure. 123 // 124 // bestChain tracks the current active chain by making use of an 125 // efficient chain view into the block index. 126 index *blockIndex 127 bestChain *chainView 128 129 // These fields are related to handling of orphan blocks. They are 130 // protected by a combination of the chain lock and the orphan lock. 131 orphanLock sync.RWMutex 132 orphans map[chainhash.Hash]*orphanBlock 133 prevOrphans map[chainhash.Hash][]*orphanBlock 134 oldestOrphan *orphanBlock 135 136 // These fields are related to checkpoint handling. They are protected 137 // by the chain lock. 138 nextCheckpoint *chaincfg.Checkpoint 139 checkpointNode *blockNode 140 141 // The state is used as a fairly efficient way to cache information 142 // about the current best chain state that is returned to callers when 143 // requested. It operates on the principle of MVCC such that any time a 144 // new block becomes the best block, the state pointer is replaced with 145 // a new struct and the old state is left untouched. In this way, 146 // multiple callers can be pointing to different best chain states. 147 // This is acceptable for most callers because the state is only being 148 // queried at a specific point in time. 149 // 150 // In addition, some of the fields are stored in the database so the 151 // chain state can be quickly reconstructed on load. 152 stateLock sync.RWMutex 153 stateSnapshot *BestState 154 155 // The following caches are used to efficiently keep track of the 156 // current deployment threshold state of each rule change deployment. 157 // 158 // This information is stored in the database so it can be quickly 159 // reconstructed on load. 160 // 161 // warningCaches caches the current deployment threshold state for blocks 162 // in each of the **possible** deployments. This is used in order to 163 // detect when new unrecognized rule changes are being voted on and/or 164 // have been activated such as will be the case when older versions of 165 // the software are being used 166 // 167 // deploymentCaches caches the current deployment threshold state for 168 // blocks in each of the actively defined deployments. 169 warningCaches []thresholdStateCache 170 deploymentCaches []thresholdStateCache 171 172 // The following fields are used to determine if certain warnings have 173 // already been shown. 174 // 175 // unknownRulesWarned refers to warnings due to unknown rules being 176 // activated. 177 unknownRulesWarned bool 178 179 // The notifications field stores a slice of callbacks to be executed on 180 // certain blockchain events. 181 notificationsLock sync.RWMutex 182 notifications []NotificationCallback 183 } 184 185 // HaveBlock returns whether or not the chain instance has the block represented 186 // by the passed hash. This includes checking the various places a block can 187 // be like part of the main chain, on a side chain, or in the orphan pool. 188 // 189 // This function is safe for concurrent access. 190 func (b *BlockChain) HaveBlock(hash *chainhash.Hash) (bool, error) { 191 exists, err := b.blockExists(hash) 192 if err != nil { 193 return false, err 194 } 195 return exists || b.IsKnownOrphan(hash), nil 196 } 197 198 // IsKnownOrphan returns whether the passed hash is currently a known orphan. 199 // Keep in mind that only a limited number of orphans are held onto for a 200 // limited amount of time, so this function must not be used as an absolute 201 // way to test if a block is an orphan block. A full block (as opposed to just 202 // its hash) must be passed to ProcessBlock for that purpose. However, calling 203 // ProcessBlock with an orphan that already exists results in an error, so this 204 // function provides a mechanism for a caller to intelligently detect *recent* 205 // duplicate orphans and react accordingly. 206 // 207 // This function is safe for concurrent access. 208 func (b *BlockChain) IsKnownOrphan(hash *chainhash.Hash) bool { 209 // Protect concurrent access. Using a read lock only so multiple 210 // readers can query without blocking each other. 211 b.orphanLock.RLock() 212 _, exists := b.orphans[*hash] 213 b.orphanLock.RUnlock() 214 215 return exists 216 } 217 218 // GetOrphanRoot returns the head of the chain for the provided hash from the 219 // map of orphan blocks. 220 // 221 // This function is safe for concurrent access. 222 func (b *BlockChain) GetOrphanRoot(hash *chainhash.Hash) *chainhash.Hash { 223 // Protect concurrent access. Using a read lock only so multiple 224 // readers can query without blocking each other. 225 b.orphanLock.RLock() 226 defer b.orphanLock.RUnlock() 227 228 // Keep looping while the parent of each orphaned block is 229 // known and is an orphan itself. 230 orphanRoot := hash 231 prevHash := hash 232 for { 233 orphan, exists := b.orphans[*prevHash] 234 if !exists { 235 break 236 } 237 orphanRoot = prevHash 238 prevHash = &orphan.block.MsgBlock().Header.PrevBlock 239 } 240 241 return orphanRoot 242 } 243 244 // removeOrphanBlock removes the passed orphan block from the orphan pool and 245 // previous orphan index. 246 func (b *BlockChain) removeOrphanBlock(orphan *orphanBlock) { 247 // Protect concurrent access. 248 b.orphanLock.Lock() 249 defer b.orphanLock.Unlock() 250 251 // Remove the orphan block from the orphan pool. 252 orphanHash := orphan.block.Hash() 253 delete(b.orphans, *orphanHash) 254 255 // Remove the reference from the previous orphan index too. An indexing 256 // for loop is intentionally used over a range here as range does not 257 // reevaluate the slice on each iteration nor does it adjust the index 258 // for the modified slice. 259 prevHash := &orphan.block.MsgBlock().Header.PrevBlock 260 orphans := b.prevOrphans[*prevHash] 261 for i := 0; i < len(orphans); i++ { 262 hash := orphans[i].block.Hash() 263 if hash.IsEqual(orphanHash) { 264 copy(orphans[i:], orphans[i+1:]) 265 orphans[len(orphans)-1] = nil 266 orphans = orphans[:len(orphans)-1] 267 i-- 268 } 269 } 270 b.prevOrphans[*prevHash] = orphans 271 272 // Remove the map entry altogether if there are no longer any orphans 273 // which depend on the parent hash. 274 if len(b.prevOrphans[*prevHash]) == 0 { 275 delete(b.prevOrphans, *prevHash) 276 } 277 } 278 279 // addOrphanBlock adds the passed block (which is already determined to be 280 // an orphan prior calling this function) to the orphan pool. It lazily cleans 281 // up any expired blocks so a separate cleanup poller doesn't need to be run. 282 // It also imposes a maximum limit on the number of outstanding orphan 283 // blocks and will remove the oldest received orphan block if the limit is 284 // exceeded. 285 func (b *BlockChain) addOrphanBlock(block *palcutil.Block) { 286 // Remove expired orphan blocks. 287 for _, oBlock := range b.orphans { 288 if time.Now().After(oBlock.expiration) { 289 b.removeOrphanBlock(oBlock) 290 continue 291 } 292 293 // Update the oldest orphan block pointer so it can be discarded 294 // in case the orphan pool fills up. 295 if b.oldestOrphan == nil || oBlock.expiration.Before(b.oldestOrphan.expiration) { 296 b.oldestOrphan = oBlock 297 } 298 } 299 300 // Limit orphan blocks to prevent memory exhaustion. 301 if len(b.orphans)+1 > maxOrphanBlocks { 302 // Remove the oldest orphan to make room for the new one. 303 b.removeOrphanBlock(b.oldestOrphan) 304 b.oldestOrphan = nil 305 } 306 307 // Protect concurrent access. This is intentionally done here instead 308 // of near the top since removeOrphanBlock does its own locking and 309 // the range iterator is not invalidated by removing map entries. 310 b.orphanLock.Lock() 311 defer b.orphanLock.Unlock() 312 313 // Insert the block into the orphan map with an expiration time 314 // 1 hour from now. 315 expiration := time.Now().Add(time.Hour) 316 oBlock := &orphanBlock{ 317 block: block, 318 expiration: expiration, 319 } 320 b.orphans[*block.Hash()] = oBlock 321 322 // Add to previous hash lookup index for faster dependency lookups. 323 prevHash := &block.MsgBlock().Header.PrevBlock 324 b.prevOrphans[*prevHash] = append(b.prevOrphans[*prevHash], oBlock) 325 } 326 327 // SequenceLock represents the converted relative lock-time in seconds, and 328 // absolute block-height for a transaction input's relative lock-times. 329 // According to SequenceLock, after the referenced input has been confirmed 330 // within a block, a transaction spending that input can be included into a 331 // block either after 'seconds' (according to past median time), or once the 332 // 'BlockHeight' has been reached. 333 type SequenceLock struct { 334 Seconds int64 335 BlockHeight int32 336 } 337 338 // CalcSequenceLock computes a relative lock-time SequenceLock for the passed 339 // transaction using the passed UtxoViewpoint to obtain the past median time 340 // for blocks in which the referenced inputs of the transactions were included 341 // within. The generated SequenceLock lock can be used in conjunction with a 342 // block height, and adjusted median block time to determine if all the inputs 343 // referenced within a transaction have reached sufficient maturity allowing 344 // the candidate transaction to be included in a block. 345 // 346 // This function is safe for concurrent access. 347 func (b *BlockChain) CalcSequenceLock(tx *palcutil.Tx, utxoView *UtxoViewpoint, mempool bool) (*SequenceLock, error) { 348 b.chainLock.Lock() 349 defer b.chainLock.Unlock() 350 351 return b.calcSequenceLock(b.bestChain.Tip(), tx, utxoView, mempool) 352 } 353 354 // calcSequenceLock computes the relative lock-times for the passed 355 // transaction. See the exported version, CalcSequenceLock for further details. 356 // 357 // This function MUST be called with the chain state lock held (for writes). 358 func (b *BlockChain) calcSequenceLock(node *blockNode, tx *palcutil.Tx, utxoView *UtxoViewpoint, mempool bool) (*SequenceLock, error) { 359 // A value of -1 for each relative lock type represents a relative time 360 // lock value that will allow a transaction to be included in a block 361 // at any given height or time. This value is returned as the relative 362 // lock time in the case that BIP 68 is disabled, or has not yet been 363 // activated. 364 sequenceLock := &SequenceLock{Seconds: -1, BlockHeight: -1} 365 366 // The sequence locks semantics are always active for transactions 367 // within the mempool. 368 csvSoftforkActive := mempool 369 370 // If we're performing block validation, then we need to query the BIP9 371 // state. 372 if !csvSoftforkActive { 373 // Obtain the latest BIP9 version bits state for the 374 // CSV-package soft-fork deployment. The adherence of sequence 375 // locks depends on the current soft-fork state. 376 csvState, err := b.deploymentState(node.parent, chaincfg.DeploymentCSV) 377 if err != nil { 378 return nil, err 379 } 380 csvSoftforkActive = csvState == ThresholdActive 381 } 382 383 // If the transaction's version is less than 2, and BIP 68 has not yet 384 // been activated then sequence locks are disabled. Additionally, 385 // sequence locks don't apply to coinbase transactions Therefore, we 386 // return sequence lock values of -1 indicating that this transaction 387 // can be included within a block at any given height or time. 388 mTx := tx.MsgTx() 389 sequenceLockActive := mTx.Version >= 2 && csvSoftforkActive 390 if !sequenceLockActive || IsCoinBase(tx) { 391 return sequenceLock, nil 392 } 393 394 // Grab the next height from the PoV of the passed blockNode to use for 395 // inputs present in the mempool. 396 nextHeight := node.height + 1 397 398 for txInIndex, txIn := range mTx.TxIn { 399 utxo := utxoView.LookupEntry(txIn.PreviousOutPoint) 400 if utxo == nil { 401 str := fmt.Sprintf("output %v referenced from "+ 402 "transaction %s:%d either does not exist or "+ 403 "has already been spent", txIn.PreviousOutPoint, 404 tx.Hash(), txInIndex) 405 return sequenceLock, ruleError(ErrMissingTxOut, str) 406 } 407 408 // If the input height is set to the mempool height, then we 409 // assume the transaction makes it into the next block when 410 // evaluating its sequence blocks. 411 inputHeight := utxo.BlockHeight() 412 if inputHeight == 0x7fffffff { 413 inputHeight = nextHeight 414 } 415 416 // Given a sequence number, we apply the relative time lock 417 // mask in order to obtain the time lock delta required before 418 // this input can be spent. 419 sequenceNum := txIn.Sequence 420 relativeLock := int64(sequenceNum & wire.SequenceLockTimeMask) 421 422 switch { 423 // Relative time locks are disabled for this input, so we can 424 // skip any further calculation. 425 case sequenceNum&wire.SequenceLockTimeDisabled == wire.SequenceLockTimeDisabled: 426 continue 427 case sequenceNum&wire.SequenceLockTimeIsSeconds == wire.SequenceLockTimeIsSeconds: 428 // This input requires a relative time lock expressed 429 // in seconds before it can be spent. Therefore, we 430 // need to query for the block prior to the one in 431 // which this input was included within so we can 432 // compute the past median time for the block prior to 433 // the one which included this referenced output. 434 prevInputHeight := inputHeight - 1 435 if prevInputHeight < 0 { 436 prevInputHeight = 0 437 } 438 blockNode := node.Ancestor(prevInputHeight) 439 medianTime := blockNode.CalcPastMedianTime() 440 441 // Time based relative time-locks as defined by BIP 68 442 // have a time granularity of RelativeLockSeconds, so 443 // we shift left by this amount to convert to the 444 // proper relative time-lock. We also subtract one from 445 // the relative lock to maintain the original lockTime 446 // semantics. 447 timeLockSeconds := (relativeLock << wire.SequenceLockTimeGranularity) - 1 448 timeLock := medianTime.Unix() + timeLockSeconds 449 if timeLock > sequenceLock.Seconds { 450 sequenceLock.Seconds = timeLock 451 } 452 default: 453 // The relative lock-time for this input is expressed 454 // in blocks so we calculate the relative offset from 455 // the input's height as its converted absolute 456 // lock-time. We subtract one from the relative lock in 457 // order to maintain the original lockTime semantics. 458 blockHeight := inputHeight + int32(relativeLock-1) 459 if blockHeight > sequenceLock.BlockHeight { 460 sequenceLock.BlockHeight = blockHeight 461 } 462 } 463 } 464 465 return sequenceLock, nil 466 } 467 468 // LockTimeToSequence converts the passed relative locktime to a sequence 469 // number in accordance to BIP-68. 470 // See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki 471 // * (Compatibility) 472 func LockTimeToSequence(isSeconds bool, locktime uint32) uint32 { 473 // If we're expressing the relative lock time in blocks, then the 474 // corresponding sequence number is simply the desired input age. 475 if !isSeconds { 476 return locktime 477 } 478 479 // Set the 22nd bit which indicates the lock time is in seconds, then 480 // shift the locktime over by 9 since the time granularity is in 481 // 512-second intervals (2^9). This results in a max lock-time of 482 // 33,553,920 seconds, or 1.1 years. 483 return wire.SequenceLockTimeIsSeconds | 484 locktime>>wire.SequenceLockTimeGranularity 485 } 486 487 // getReorganizeNodes finds the fork point between the main chain and the passed 488 // node and returns a list of block nodes that would need to be detached from 489 // the main chain and a list of block nodes that would need to be attached to 490 // the fork point (which will be the end of the main chain after detaching the 491 // returned list of block nodes) in order to reorganize the chain such that the 492 // passed node is the new end of the main chain. The lists will be empty if the 493 // passed node is not on a side chain. 494 // 495 // This function may modify node statuses in the block index without flushing. 496 // 497 // This function MUST be called with the chain state lock held (for reads). 498 func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List) { 499 attachNodes := list.New() 500 detachNodes := list.New() 501 502 // Do not reorganize to a known invalid chain. Ancestors deeper than the 503 // direct parent are checked below but this is a quick check before doing 504 // more unnecessary work. 505 if b.index.NodeStatus(node.parent).KnownInvalid() { 506 b.index.SetStatusFlags(node, statusInvalidAncestor) 507 return detachNodes, attachNodes 508 } 509 510 // Find the fork point (if any) adding each block to the list of nodes 511 // to attach to the main tree. Push them onto the list in reverse order 512 // so they are attached in the appropriate order when iterating the list 513 // later. 514 forkNode := b.bestChain.FindFork(node) 515 invalidChain := false 516 for n := node; n != nil && n != forkNode; n = n.parent { 517 if b.index.NodeStatus(n).KnownInvalid() { 518 invalidChain = true 519 break 520 } 521 attachNodes.PushFront(n) 522 } 523 524 // If any of the node's ancestors are invalid, unwind attachNodes, marking 525 // each one as invalid for future reference. 526 if invalidChain { 527 var next *list.Element 528 for e := attachNodes.Front(); e != nil; e = next { 529 next = e.Next() 530 n := attachNodes.Remove(e).(*blockNode) 531 b.index.SetStatusFlags(n, statusInvalidAncestor) 532 } 533 return detachNodes, attachNodes 534 } 535 536 // Start from the end of the main chain and work backwards until the 537 // common ancestor adding each block to the list of nodes to detach from 538 // the main chain. 539 for n := b.bestChain.Tip(); n != nil && n != forkNode; n = n.parent { 540 detachNodes.PushBack(n) 541 } 542 543 return detachNodes, attachNodes 544 } 545 546 // connectBlock handles connecting the passed node/block to the end of the main 547 // (best) chain. 548 // 549 // This passed utxo view must have all referenced txos the block spends marked 550 // as spent and all of the new txos the block creates added to it. In addition, 551 // the passed stxos slice must be populated with all of the information for the 552 // spent txos. This approach is used because the connection validation that 553 // must happen prior to calling this function requires the same details, so 554 // it would be inefficient to repeat it. 555 // 556 // This function MUST be called with the chain state lock held (for writes). 557 func (b *BlockChain) connectBlock(node *blockNode, block *palcutil.Block, 558 view *UtxoViewpoint, stxos []SpentTxOut) error { 559 560 // Make sure it's extending the end of the best chain. 561 prevHash := &block.MsgBlock().Header.PrevBlock 562 if !prevHash.IsEqual(&b.bestChain.Tip().hash) { 563 return AssertError("connectBlock must be called with a block " + 564 "that extends the main chain") 565 } 566 567 // Sanity check the correct number of stxos are provided. 568 if len(stxos) != countSpentOutputs(block) { 569 return AssertError("connectBlock called with inconsistent " + 570 "spent transaction out information") 571 } 572 573 // No warnings about unknown rules until the chain is current. 574 if b.isCurrent() { 575 // Warn if any unknown new rules are either about to activate or 576 // have already been activated. 577 if err := b.warnUnknownRuleActivations(node); err != nil { 578 return err 579 } 580 } 581 582 // Write any block status changes to DB before updating best state. 583 err := b.index.flushToDB() 584 if err != nil { 585 return err 586 } 587 588 // Generate a new best state snapshot that will be used to update the 589 // database and later memory if all database updates are successful. 590 b.stateLock.RLock() 591 curTotalTxns := b.stateSnapshot.TotalTxns 592 b.stateLock.RUnlock() 593 numTxns := uint64(len(block.MsgBlock().Transactions)) 594 blockSize := uint64(block.MsgBlock().SerializeSize()) 595 blockWeight := uint64(GetBlockWeight(block)) 596 state := newBestState(node, blockSize, blockWeight, numTxns, 597 curTotalTxns+numTxns, node.CalcPastMedianTime()) 598 599 // Atomically insert info into the database. 600 err = b.db.Update(func(dbTx database.Tx) error { 601 // Update best block state. 602 err := dbPutBestState(dbTx, state, node.workSum) 603 if err != nil { 604 return err 605 } 606 607 // Add the block hash and height to the block index which tracks 608 // the main chain. 609 err = dbPutBlockIndex(dbTx, block.Hash(), node.height) 610 if err != nil { 611 return err 612 } 613 614 // Update the utxo set using the state of the utxo view. This 615 // entails removing all of the utxos spent and adding the new 616 // ones created by the block. 617 err = dbPutUtxoView(dbTx, view) 618 if err != nil { 619 return err 620 } 621 622 // Update the transaction spend journal by adding a record for 623 // the block that contains all txos spent by it. 624 err = dbPutSpendJournalEntry(dbTx, block.Hash(), stxos) 625 if err != nil { 626 return err 627 } 628 629 // Allow the index manager to call each of the currently active 630 // optional indexes with the block being connected so they can 631 // update themselves accordingly. 632 if b.indexManager != nil { 633 err := b.indexManager.ConnectBlock(dbTx, block, stxos) 634 if err != nil { 635 return err 636 } 637 } 638 639 return nil 640 }) 641 if err != nil { 642 return err 643 } 644 645 // Prune fully spent entries and mark all entries in the view unmodified 646 // now that the modifications have been committed to the database. 647 view.commit() 648 649 // This node is now the end of the best chain. 650 b.bestChain.SetTip(node) 651 652 // Update the state for the best block. Notice how this replaces the 653 // entire struct instead of updating the existing one. This effectively 654 // allows the old version to act as a snapshot which callers can use 655 // freely without needing to hold a lock for the duration. See the 656 // comments on the state variable for more details. 657 b.stateLock.Lock() 658 b.stateSnapshot = state 659 b.stateLock.Unlock() 660 661 // Notify the caller that the block was connected to the main chain. 662 // The caller would typically want to react with actions such as 663 // updating wallets. 664 b.chainLock.Unlock() 665 b.sendNotification(NTBlockConnected, block) 666 b.chainLock.Lock() 667 668 return nil 669 } 670 671 // disconnectBlock handles disconnecting the passed node/block from the end of 672 // the main (best) chain. 673 // 674 // This function MUST be called with the chain state lock held (for writes). 675 func (b *BlockChain) disconnectBlock(node *blockNode, block *palcutil.Block, view *UtxoViewpoint) error { 676 // Make sure the node being disconnected is the end of the best chain. 677 if !node.hash.IsEqual(&b.bestChain.Tip().hash) { 678 return AssertError("disconnectBlock must be called with the " + 679 "block at the end of the main chain") 680 } 681 682 // Load the previous block since some details for it are needed below. 683 prevNode := node.parent 684 var prevBlock *palcutil.Block 685 err := b.db.View(func(dbTx database.Tx) error { 686 var err error 687 prevBlock, err = dbFetchBlockByNode(dbTx, prevNode) 688 return err 689 }) 690 if err != nil { 691 return err 692 } 693 694 // Write any block status changes to DB before updating best state. 695 err = b.index.flushToDB() 696 if err != nil { 697 return err 698 } 699 700 // Generate a new best state snapshot that will be used to update the 701 // database and later memory if all database updates are successful. 702 b.stateLock.RLock() 703 curTotalTxns := b.stateSnapshot.TotalTxns 704 b.stateLock.RUnlock() 705 numTxns := uint64(len(prevBlock.MsgBlock().Transactions)) 706 blockSize := uint64(prevBlock.MsgBlock().SerializeSize()) 707 blockWeight := uint64(GetBlockWeight(prevBlock)) 708 newTotalTxns := curTotalTxns - uint64(len(block.MsgBlock().Transactions)) 709 state := newBestState(prevNode, blockSize, blockWeight, numTxns, 710 newTotalTxns, prevNode.CalcPastMedianTime()) 711 712 err = b.db.Update(func(dbTx database.Tx) error { 713 // Update best block state. 714 err := dbPutBestState(dbTx, state, node.workSum) 715 if err != nil { 716 return err 717 } 718 719 // Remove the block hash and height from the block index which 720 // tracks the main chain. 721 err = dbRemoveBlockIndex(dbTx, block.Hash(), node.height) 722 if err != nil { 723 return err 724 } 725 726 // Update the utxo set using the state of the utxo view. This 727 // entails restoring all of the utxos spent and removing the new 728 // ones created by the block. 729 err = dbPutUtxoView(dbTx, view) 730 if err != nil { 731 return err 732 } 733 734 // Before we delete the spend journal entry for this back, 735 // we'll fetch it as is so the indexers can utilize if needed. 736 stxos, err := dbFetchSpendJournalEntry(dbTx, block) 737 if err != nil { 738 return err 739 } 740 741 // Update the transaction spend journal by removing the record 742 // that contains all txos spent by the block. 743 err = dbRemoveSpendJournalEntry(dbTx, block.Hash()) 744 if err != nil { 745 return err 746 } 747 748 // Allow the index manager to call each of the currently active 749 // optional indexes with the block being disconnected so they 750 // can update themselves accordingly. 751 if b.indexManager != nil { 752 err := b.indexManager.DisconnectBlock(dbTx, block, stxos) 753 if err != nil { 754 return err 755 } 756 } 757 758 return nil 759 }) 760 if err != nil { 761 return err 762 } 763 764 // Prune fully spent entries and mark all entries in the view unmodified 765 // now that the modifications have been committed to the database. 766 view.commit() 767 768 // This node's parent is now the end of the best chain. 769 b.bestChain.SetTip(node.parent) 770 771 // Update the state for the best block. Notice how this replaces the 772 // entire struct instead of updating the existing one. This effectively 773 // allows the old version to act as a snapshot which callers can use 774 // freely without needing to hold a lock for the duration. See the 775 // comments on the state variable for more details. 776 b.stateLock.Lock() 777 b.stateSnapshot = state 778 b.stateLock.Unlock() 779 780 // Notify the caller that the block was disconnected from the main 781 // chain. The caller would typically want to react with actions such as 782 // updating wallets. 783 b.chainLock.Unlock() 784 b.sendNotification(NTBlockDisconnected, block) 785 b.chainLock.Lock() 786 787 return nil 788 } 789 790 // countSpentOutputs returns the number of utxos the passed block spends. 791 func countSpentOutputs(block *palcutil.Block) int { 792 // Exclude the coinbase transaction since it can't spend anything. 793 var numSpent int 794 for _, tx := range block.Transactions()[1:] { 795 numSpent += len(tx.MsgTx().TxIn) 796 } 797 return numSpent 798 } 799 800 // reorganizeChain reorganizes the block chain by disconnecting the nodes in the 801 // detachNodes list and connecting the nodes in the attach list. It expects 802 // that the lists are already in the correct order and are in sync with the 803 // end of the current best chain. Specifically, nodes that are being 804 // disconnected must be in reverse order (think of popping them off the end of 805 // the chain) and nodes the are being attached must be in forwards order 806 // (think pushing them onto the end of the chain). 807 // 808 // This function may modify node statuses in the block index without flushing. 809 // 810 // This function MUST be called with the chain state lock held (for writes). 811 func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error { 812 // Nothing to do if no reorganize nodes were provided. 813 if detachNodes.Len() == 0 && attachNodes.Len() == 0 { 814 return nil 815 } 816 817 // Ensure the provided nodes match the current best chain. 818 tip := b.bestChain.Tip() 819 if detachNodes.Len() != 0 { 820 firstDetachNode := detachNodes.Front().Value.(*blockNode) 821 if firstDetachNode.hash != tip.hash { 822 return AssertError(fmt.Sprintf("reorganize nodes to detach are "+ 823 "not for the current best chain -- first detach node %v, "+ 824 "current chain %v", &firstDetachNode.hash, &tip.hash)) 825 } 826 } 827 828 // Ensure the provided nodes are for the same fork point. 829 if attachNodes.Len() != 0 && detachNodes.Len() != 0 { 830 firstAttachNode := attachNodes.Front().Value.(*blockNode) 831 lastDetachNode := detachNodes.Back().Value.(*blockNode) 832 if firstAttachNode.parent.hash != lastDetachNode.parent.hash { 833 return AssertError(fmt.Sprintf("reorganize nodes do not have the "+ 834 "same fork point -- first attach parent %v, last detach "+ 835 "parent %v", &firstAttachNode.parent.hash, 836 &lastDetachNode.parent.hash)) 837 } 838 } 839 840 // Track the old and new best chains heads. 841 oldBest := tip 842 newBest := tip 843 844 // All of the blocks to detach and related spend journal entries needed 845 // to unspend transaction outputs in the blocks being disconnected must 846 // be loaded from the database during the reorg check phase below and 847 // then they are needed again when doing the actual database updates. 848 // Rather than doing two loads, cache the loaded data into these slices. 849 detachBlocks := make([]*palcutil.Block, 0, detachNodes.Len()) 850 detachSpentTxOuts := make([][]SpentTxOut, 0, detachNodes.Len()) 851 attachBlocks := make([]*palcutil.Block, 0, attachNodes.Len()) 852 853 // Disconnect all of the blocks back to the point of the fork. This 854 // entails loading the blocks and their associated spent txos from the 855 // database and using that information to unspend all of the spent txos 856 // and remove the utxos created by the blocks. 857 view := NewUtxoViewpoint() 858 view.SetBestHash(&oldBest.hash) 859 for e := detachNodes.Front(); e != nil; e = e.Next() { 860 n := e.Value.(*blockNode) 861 var block *palcutil.Block 862 err := b.db.View(func(dbTx database.Tx) error { 863 var err error 864 block, err = dbFetchBlockByNode(dbTx, n) 865 return err 866 }) 867 if err != nil { 868 return err 869 } 870 if n.hash != *block.Hash() { 871 return AssertError(fmt.Sprintf("detach block node hash %v (height "+ 872 "%v) does not match previous parent block hash %v", &n.hash, 873 n.height, block.Hash())) 874 } 875 876 // Load all of the utxos referenced by the block that aren't 877 // already in the view. 878 err = view.fetchInputUtxos(b.db, block) 879 if err != nil { 880 return err 881 } 882 883 // Load all of the spent txos for the block from the spend 884 // journal. 885 var stxos []SpentTxOut 886 err = b.db.View(func(dbTx database.Tx) error { 887 stxos, err = dbFetchSpendJournalEntry(dbTx, block) 888 return err 889 }) 890 if err != nil { 891 return err 892 } 893 894 // Store the loaded block and spend journal entry for later. 895 detachBlocks = append(detachBlocks, block) 896 detachSpentTxOuts = append(detachSpentTxOuts, stxos) 897 898 err = view.disconnectTransactions(b.db, block, stxos) 899 if err != nil { 900 return err 901 } 902 903 newBest = n.parent 904 } 905 906 // Set the fork point only if there are nodes to attach since otherwise 907 // blocks are only being disconnected and thus there is no fork point. 908 var forkNode *blockNode 909 if attachNodes.Len() > 0 { 910 forkNode = newBest 911 } 912 913 // Perform several checks to verify each block that needs to be attached 914 // to the main chain can be connected without violating any rules and 915 // without actually connecting the block. 916 // 917 // NOTE: These checks could be done directly when connecting a block, 918 // however the downside to that approach is that if any of these checks 919 // fail after disconnecting some blocks or attaching others, all of the 920 // operations have to be rolled back to get the chain back into the 921 // state it was before the rule violation (or other failure). There are 922 // at least a couple of ways accomplish that rollback, but both involve 923 // tweaking the chain and/or database. This approach catches these 924 // issues before ever modifying the chain. 925 for e := attachNodes.Front(); e != nil; e = e.Next() { 926 n := e.Value.(*blockNode) 927 928 var block *palcutil.Block 929 err := b.db.View(func(dbTx database.Tx) error { 930 var err error 931 block, err = dbFetchBlockByNode(dbTx, n) 932 return err 933 }) 934 if err != nil { 935 return err 936 } 937 938 // Store the loaded block for later. 939 attachBlocks = append(attachBlocks, block) 940 941 // Skip checks if node has already been fully validated. Although 942 // checkConnectBlock gets skipped, we still need to update the UTXO 943 // view. 944 if b.index.NodeStatus(n).KnownValid() { 945 err = view.fetchInputUtxos(b.db, block) 946 if err != nil { 947 return err 948 } 949 err = view.connectTransactions(block, nil) 950 if err != nil { 951 return err 952 } 953 954 newBest = n 955 continue 956 } 957 958 // Notice the spent txout details are not requested here and 959 // thus will not be generated. This is done because the state 960 // is not being immediately written to the database, so it is 961 // not needed. 962 // 963 // In the case the block is determined to be invalid due to a 964 // rule violation, mark it as invalid and mark all of its 965 // descendants as having an invalid ancestor. 966 err = b.checkConnectBlock(n, block, view, nil) 967 if err != nil { 968 if _, ok := err.(RuleError); ok { 969 b.index.SetStatusFlags(n, statusValidateFailed) 970 for de := e.Next(); de != nil; de = de.Next() { 971 dn := de.Value.(*blockNode) 972 b.index.SetStatusFlags(dn, statusInvalidAncestor) 973 } 974 } 975 return err 976 } 977 b.index.SetStatusFlags(n, statusValid) 978 979 newBest = n 980 } 981 982 // Reset the view for the actual connection code below. This is 983 // required because the view was previously modified when checking if 984 // the reorg would be successful and the connection code requires the 985 // view to be valid from the viewpoint of each block being connected or 986 // disconnected. 987 view = NewUtxoViewpoint() 988 view.SetBestHash(&b.bestChain.Tip().hash) 989 990 // Disconnect blocks from the main chain. 991 for i, e := 0, detachNodes.Front(); e != nil; i, e = i+1, e.Next() { 992 n := e.Value.(*blockNode) 993 block := detachBlocks[i] 994 995 // Load all of the utxos referenced by the block that aren't 996 // already in the view. 997 err := view.fetchInputUtxos(b.db, block) 998 if err != nil { 999 return err 1000 } 1001 1002 // Update the view to unspend all of the spent txos and remove 1003 // the utxos created by the block. 1004 err = view.disconnectTransactions(b.db, block, 1005 detachSpentTxOuts[i]) 1006 if err != nil { 1007 return err 1008 } 1009 1010 // Update the database and chain state. 1011 err = b.disconnectBlock(n, block, view) 1012 if err != nil { 1013 return err 1014 } 1015 } 1016 1017 // Connect the new best chain blocks. 1018 for i, e := 0, attachNodes.Front(); e != nil; i, e = i+1, e.Next() { 1019 n := e.Value.(*blockNode) 1020 block := attachBlocks[i] 1021 1022 // Load all of the utxos referenced by the block that aren't 1023 // already in the view. 1024 err := view.fetchInputUtxos(b.db, block) 1025 if err != nil { 1026 return err 1027 } 1028 1029 // Update the view to mark all utxos referenced by the block 1030 // as spent and add all transactions being created by this block 1031 // to it. Also, provide an stxo slice so the spent txout 1032 // details are generated. 1033 stxos := make([]SpentTxOut, 0, countSpentOutputs(block)) 1034 err = view.connectTransactions(block, &stxos) 1035 if err != nil { 1036 return err 1037 } 1038 1039 // Update the database and chain state. 1040 err = b.connectBlock(n, block, view, stxos) 1041 if err != nil { 1042 return err 1043 } 1044 } 1045 1046 // Log the point where the chain forked and old and new best chain 1047 // heads. 1048 if forkNode != nil { 1049 log.Infof("REORGANIZE: Chain forks at %v (height %v)", forkNode.hash, 1050 forkNode.height) 1051 } 1052 log.Infof("REORGANIZE: Old best chain head was %v (height %v)", 1053 &oldBest.hash, oldBest.height) 1054 log.Infof("REORGANIZE: New best chain head is %v (height %v)", 1055 newBest.hash, newBest.height) 1056 1057 return nil 1058 } 1059 1060 // connectBestChain handles connecting the passed block to the chain while 1061 // respecting proper chain selection according to the chain with the most 1062 // proof of work. In the typical case, the new block simply extends the main 1063 // chain. However, it may also be extending (or creating) a side chain (fork) 1064 // which may or may not end up becoming the main chain depending on which fork 1065 // cumulatively has the most proof of work. It returns whether or not the block 1066 // ended up on the main chain (either due to extending the main chain or causing 1067 // a reorganization to become the main chain). 1068 // 1069 // The flags modify the behavior of this function as follows: 1070 // - BFFastAdd: Avoids several expensive transaction validation operations. 1071 // This is useful when using checkpoints. 1072 // 1073 // This function MUST be called with the chain state lock held (for writes). 1074 func (b *BlockChain) connectBestChain(node *blockNode, block *palcutil.Block, flags BehaviorFlags) (bool, error) { 1075 fastAdd := flags&BFFastAdd == BFFastAdd 1076 1077 flushIndexState := func() { 1078 // Intentionally ignore errors writing updated node status to DB. If 1079 // it fails to write, it's not the end of the world. If the block is 1080 // valid, we flush in connectBlock and if the block is invalid, the 1081 // worst that can happen is we revalidate the block after a restart. 1082 if writeErr := b.index.flushToDB(); writeErr != nil { 1083 log.Warnf("Error flushing block index changes to disk: %v", 1084 writeErr) 1085 } 1086 } 1087 1088 // We are extending the main (best) chain with a new block. This is the 1089 // most common case. 1090 parentHash := &block.MsgBlock().Header.PrevBlock 1091 if parentHash.IsEqual(&b.bestChain.Tip().hash) { 1092 // Skip checks if node has already been fully validated. 1093 fastAdd = fastAdd || b.index.NodeStatus(node).KnownValid() 1094 1095 // Perform several checks to verify the block can be connected 1096 // to the main chain without violating any rules and without 1097 // actually connecting the block. 1098 view := NewUtxoViewpoint() 1099 view.SetBestHash(parentHash) 1100 stxos := make([]SpentTxOut, 0, countSpentOutputs(block)) 1101 if !fastAdd { 1102 err := b.checkConnectBlock(node, block, view, &stxos) 1103 if err == nil { 1104 b.index.SetStatusFlags(node, statusValid) 1105 } else if _, ok := err.(RuleError); ok { 1106 b.index.SetStatusFlags(node, statusValidateFailed) 1107 } else { 1108 return false, err 1109 } 1110 1111 flushIndexState() 1112 1113 if err != nil { 1114 return false, err 1115 } 1116 } 1117 1118 // In the fast add case the code to check the block connection 1119 // was skipped, so the utxo view needs to load the referenced 1120 // utxos, spend them, and add the new utxos being created by 1121 // this block. 1122 if fastAdd { 1123 err := view.fetchInputUtxos(b.db, block) 1124 if err != nil { 1125 return false, err 1126 } 1127 err = view.connectTransactions(block, &stxos) 1128 if err != nil { 1129 return false, err 1130 } 1131 } 1132 1133 // Connect the block to the main chain. 1134 err := b.connectBlock(node, block, view, stxos) 1135 if err != nil { 1136 // If we got hit with a rule error, then we'll mark 1137 // that status of the block as invalid and flush the 1138 // index state to disk before returning with the error. 1139 if _, ok := err.(RuleError); ok { 1140 b.index.SetStatusFlags( 1141 node, statusValidateFailed, 1142 ) 1143 } 1144 1145 flushIndexState() 1146 1147 return false, err 1148 } 1149 1150 // If this is fast add, or this block node isn't yet marked as 1151 // valid, then we'll update its status and flush the state to 1152 // disk again. 1153 if fastAdd || !b.index.NodeStatus(node).KnownValid() { 1154 b.index.SetStatusFlags(node, statusValid) 1155 flushIndexState() 1156 } 1157 1158 return true, nil 1159 } 1160 if fastAdd { 1161 log.Warnf("fastAdd set in the side chain case? %v\n", 1162 block.Hash()) 1163 } 1164 1165 // We're extending (or creating) a side chain, but the cumulative 1166 // work for this new side chain is not enough to make it the new chain. 1167 if node.workSum.Cmp(b.bestChain.Tip().workSum) <= 0 { 1168 // Log information about how the block is forking the chain. 1169 fork := b.bestChain.FindFork(node) 1170 if fork.hash.IsEqual(parentHash) { 1171 log.Infof("FORK: Block %v forks the chain at height %d"+ 1172 "/block %v, but does not cause a reorganize", 1173 node.hash, fork.height, fork.hash) 1174 } else { 1175 log.Infof("EXTEND FORK: Block %v extends a side chain "+ 1176 "which forks the chain at height %d/block %v", 1177 node.hash, fork.height, fork.hash) 1178 } 1179 1180 return false, nil 1181 } 1182 1183 // We're extending (or creating) a side chain and the cumulative work 1184 // for this new side chain is more than the old best chain, so this side 1185 // chain needs to become the main chain. In order to accomplish that, 1186 // find the common ancestor of both sides of the fork, disconnect the 1187 // blocks that form the (now) old fork from the main chain, and attach 1188 // the blocks that form the new chain to the main chain starting at the 1189 // common ancenstor (the point where the chain forked). 1190 detachNodes, attachNodes := b.getReorganizeNodes(node) 1191 1192 // Reorganize the chain. 1193 log.Infof("REORGANIZE: Block %v is causing a reorganize.", node.hash) 1194 err := b.reorganizeChain(detachNodes, attachNodes) 1195 1196 // Either getReorganizeNodes or reorganizeChain could have made unsaved 1197 // changes to the block index, so flush regardless of whether there was an 1198 // error. The index would only be dirty if the block failed to connect, so 1199 // we can ignore any errors writing. 1200 if writeErr := b.index.flushToDB(); writeErr != nil { 1201 log.Warnf("Error flushing block index changes to disk: %v", writeErr) 1202 } 1203 1204 return err == nil, err 1205 } 1206 1207 // isCurrent returns whether or not the chain believes it is current. Several 1208 // factors are used to guess, but the key factors that allow the chain to 1209 // believe it is current are: 1210 // - Latest block height is after the latest checkpoint (if enabled) 1211 // - Latest block has a timestamp newer than 24 hours ago 1212 // 1213 // This function MUST be called with the chain state lock held (for reads). 1214 func (b *BlockChain) isCurrent() bool { 1215 // Not current if the latest main (best) chain height is before the 1216 // latest known good checkpoint (when checkpoints are enabled). 1217 checkpoint := b.LatestCheckpoint() 1218 if checkpoint != nil && b.bestChain.Tip().height < checkpoint.Height { 1219 return false 1220 } 1221 1222 // Not current if the latest best block has a timestamp before 24 hours 1223 // ago. 1224 // 1225 // The chain appears to be current if none of the checks reported 1226 // otherwise. 1227 minus24Hours := b.timeSource.AdjustedTime().Add(-24 * time.Hour).Unix() 1228 return b.bestChain.Tip().timestamp >= minus24Hours 1229 } 1230 1231 // IsCurrent returns whether or not the chain believes it is current. Several 1232 // factors are used to guess, but the key factors that allow the chain to 1233 // believe it is current are: 1234 // - Latest block height is after the latest checkpoint (if enabled) 1235 // - Latest block has a timestamp newer than 24 hours ago 1236 // 1237 // This function is safe for concurrent access. 1238 func (b *BlockChain) IsCurrent() bool { 1239 b.chainLock.RLock() 1240 defer b.chainLock.RUnlock() 1241 1242 return b.isCurrent() 1243 } 1244 1245 // BestSnapshot returns information about the current best chain block and 1246 // related state as of the current point in time. The returned instance must be 1247 // treated as immutable since it is shared by all callers. 1248 // 1249 // This function is safe for concurrent access. 1250 func (b *BlockChain) BestSnapshot() *BestState { 1251 b.stateLock.RLock() 1252 snapshot := b.stateSnapshot 1253 b.stateLock.RUnlock() 1254 return snapshot 1255 } 1256 1257 // HeaderByHash returns the block header identified by the given hash or an 1258 // error if it doesn't exist. Note that this will return headers from both the 1259 // main and side chains. 1260 func (b *BlockChain) HeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error) { 1261 node := b.index.LookupNode(hash) 1262 if node == nil { 1263 err := fmt.Errorf("block %s is not known", hash) 1264 return wire.BlockHeader{}, err 1265 } 1266 1267 return node.Header(), nil 1268 } 1269 1270 // MainChainHasBlock returns whether or not the block with the given hash is in 1271 // the main chain. 1272 // 1273 // This function is safe for concurrent access. 1274 func (b *BlockChain) MainChainHasBlock(hash *chainhash.Hash) bool { 1275 node := b.index.LookupNode(hash) 1276 return node != nil && b.bestChain.Contains(node) 1277 } 1278 1279 // BlockLocatorFromHash returns a block locator for the passed block hash. 1280 // See BlockLocator for details on the algorithm used to create a block locator. 1281 // 1282 // In addition to the general algorithm referenced above, this function will 1283 // return the block locator for the latest known tip of the main (best) chain if 1284 // the passed hash is not currently known. 1285 // 1286 // This function is safe for concurrent access. 1287 func (b *BlockChain) BlockLocatorFromHash(hash *chainhash.Hash) BlockLocator { 1288 b.chainLock.RLock() 1289 node := b.index.LookupNode(hash) 1290 locator := b.bestChain.blockLocator(node) 1291 b.chainLock.RUnlock() 1292 return locator 1293 } 1294 1295 // LatestBlockLocator returns a block locator for the latest known tip of the 1296 // main (best) chain. 1297 // 1298 // This function is safe for concurrent access. 1299 func (b *BlockChain) LatestBlockLocator() (BlockLocator, error) { 1300 b.chainLock.RLock() 1301 locator := b.bestChain.BlockLocator(nil) 1302 b.chainLock.RUnlock() 1303 return locator, nil 1304 } 1305 1306 // BlockHeightByHash returns the height of the block with the given hash in the 1307 // main chain. 1308 // 1309 // This function is safe for concurrent access. 1310 func (b *BlockChain) BlockHeightByHash(hash *chainhash.Hash) (int32, error) { 1311 node := b.index.LookupNode(hash) 1312 if node == nil || !b.bestChain.Contains(node) { 1313 str := fmt.Sprintf("block %s is not in the main chain", hash) 1314 return 0, errNotInMainChain(str) 1315 } 1316 1317 return node.height, nil 1318 } 1319 1320 // BlockHashByHeight returns the hash of the block at the given height in the 1321 // main chain. 1322 // 1323 // This function is safe for concurrent access. 1324 func (b *BlockChain) BlockHashByHeight(blockHeight int32) (*chainhash.Hash, error) { 1325 node := b.bestChain.NodeByHeight(blockHeight) 1326 if node == nil { 1327 str := fmt.Sprintf("no block at height %d exists", blockHeight) 1328 return nil, errNotInMainChain(str) 1329 1330 } 1331 1332 return &node.hash, nil 1333 } 1334 1335 // HeightRange returns a range of block hashes for the given start and end 1336 // heights. It is inclusive of the start height and exclusive of the end 1337 // height. The end height will be limited to the current main chain height. 1338 // 1339 // This function is safe for concurrent access. 1340 func (b *BlockChain) HeightRange(startHeight, endHeight int32) ([]chainhash.Hash, error) { 1341 // Ensure requested heights are sane. 1342 if startHeight < 0 { 1343 return nil, fmt.Errorf("start height of fetch range must not "+ 1344 "be less than zero - got %d", startHeight) 1345 } 1346 if endHeight < startHeight { 1347 return nil, fmt.Errorf("end height of fetch range must not "+ 1348 "be less than the start height - got start %d, end %d", 1349 startHeight, endHeight) 1350 } 1351 1352 // There is nothing to do when the start and end heights are the same, 1353 // so return now to avoid the chain view lock. 1354 if startHeight == endHeight { 1355 return nil, nil 1356 } 1357 1358 // Grab a lock on the chain view to prevent it from changing due to a 1359 // reorg while building the hashes. 1360 b.bestChain.mtx.Lock() 1361 defer b.bestChain.mtx.Unlock() 1362 1363 // When the requested start height is after the most recent best chain 1364 // height, there is nothing to do. 1365 latestHeight := b.bestChain.tip().height 1366 if startHeight > latestHeight { 1367 return nil, nil 1368 } 1369 1370 // Limit the ending height to the latest height of the chain. 1371 if endHeight > latestHeight+1 { 1372 endHeight = latestHeight + 1 1373 } 1374 1375 // Fetch as many as are available within the specified range. 1376 hashes := make([]chainhash.Hash, 0, endHeight-startHeight) 1377 for i := startHeight; i < endHeight; i++ { 1378 hashes = append(hashes, b.bestChain.nodeByHeight(i).hash) 1379 } 1380 return hashes, nil 1381 } 1382 1383 // HeightToHashRange returns a range of block hashes for the given start height 1384 // and end hash, inclusive on both ends. The hashes are for all blocks that are 1385 // ancestors of endHash with height greater than or equal to startHeight. The 1386 // end hash must belong to a block that is known to be valid. 1387 // 1388 // This function is safe for concurrent access. 1389 func (b *BlockChain) HeightToHashRange(startHeight int32, 1390 endHash *chainhash.Hash, maxResults int) ([]chainhash.Hash, error) { 1391 1392 endNode := b.index.LookupNode(endHash) 1393 if endNode == nil { 1394 return nil, fmt.Errorf("no known block header with hash %v", endHash) 1395 } 1396 if !b.index.NodeStatus(endNode).KnownValid() { 1397 return nil, fmt.Errorf("block %v is not yet validated", endHash) 1398 } 1399 endHeight := endNode.height 1400 1401 if startHeight < 0 { 1402 return nil, fmt.Errorf("start height (%d) is below 0", startHeight) 1403 } 1404 if startHeight > endHeight { 1405 return nil, fmt.Errorf("start height (%d) is past end height (%d)", 1406 startHeight, endHeight) 1407 } 1408 1409 resultsLength := int(endHeight - startHeight + 1) 1410 if resultsLength > maxResults { 1411 return nil, fmt.Errorf("number of results (%d) would exceed max (%d)", 1412 resultsLength, maxResults) 1413 } 1414 1415 // Walk backwards from endHeight to startHeight, collecting block hashes. 1416 node := endNode 1417 hashes := make([]chainhash.Hash, resultsLength) 1418 for i := resultsLength - 1; i >= 0; i-- { 1419 hashes[i] = node.hash 1420 node = node.parent 1421 } 1422 return hashes, nil 1423 } 1424 1425 // IntervalBlockHashes returns hashes for all blocks that are ancestors of 1426 // endHash where the block height is a positive multiple of interval. 1427 // 1428 // This function is safe for concurrent access. 1429 func (b *BlockChain) IntervalBlockHashes(endHash *chainhash.Hash, interval int, 1430 ) ([]chainhash.Hash, error) { 1431 1432 endNode := b.index.LookupNode(endHash) 1433 if endNode == nil { 1434 return nil, fmt.Errorf("no known block header with hash %v", endHash) 1435 } 1436 if !b.index.NodeStatus(endNode).KnownValid() { 1437 return nil, fmt.Errorf("block %v is not yet validated", endHash) 1438 } 1439 endHeight := endNode.height 1440 1441 resultsLength := int(endHeight) / interval 1442 hashes := make([]chainhash.Hash, resultsLength) 1443 1444 b.bestChain.mtx.Lock() 1445 defer b.bestChain.mtx.Unlock() 1446 1447 blockNode := endNode 1448 for index := int(endHeight) / interval; index > 0; index-- { 1449 // Use the bestChain chainView for faster lookups once lookup intersects 1450 // the best chain. 1451 blockHeight := int32(index * interval) 1452 if b.bestChain.contains(blockNode) { 1453 blockNode = b.bestChain.nodeByHeight(blockHeight) 1454 } else { 1455 blockNode = blockNode.Ancestor(blockHeight) 1456 } 1457 1458 hashes[index-1] = blockNode.hash 1459 } 1460 1461 return hashes, nil 1462 } 1463 1464 // locateInventory returns the node of the block after the first known block in 1465 // the locator along with the number of subsequent nodes needed to either reach 1466 // the provided stop hash or the provided max number of entries. 1467 // 1468 // In addition, there are two special cases: 1469 // 1470 // - When no locators are provided, the stop hash is treated as a request for 1471 // that block, so it will either return the node associated with the stop hash 1472 // if it is known, or nil if it is unknown 1473 // - When locators are provided, but none of them are known, nodes starting 1474 // after the genesis block will be returned 1475 // 1476 // This is primarily a helper function for the locateBlocks and locateHeaders 1477 // functions. 1478 // 1479 // This function MUST be called with the chain state lock held (for reads). 1480 func (b *BlockChain) locateInventory(locator BlockLocator, hashStop *chainhash.Hash, maxEntries uint32) (*blockNode, uint32) { 1481 // There are no block locators so a specific block is being requested 1482 // as identified by the stop hash. 1483 stopNode := b.index.LookupNode(hashStop) 1484 if len(locator) == 0 { 1485 if stopNode == nil { 1486 // No blocks with the stop hash were found so there is 1487 // nothing to do. 1488 return nil, 0 1489 } 1490 return stopNode, 1 1491 } 1492 1493 // Find the most recent locator block hash in the main chain. In the 1494 // case none of the hashes in the locator are in the main chain, fall 1495 // back to the genesis block. 1496 startNode := b.bestChain.Genesis() 1497 for _, hash := range locator { 1498 node := b.index.LookupNode(hash) 1499 if node != nil && b.bestChain.Contains(node) { 1500 startNode = node 1501 break 1502 } 1503 } 1504 1505 // Start at the block after the most recently known block. When there 1506 // is no next block it means the most recently known block is the tip of 1507 // the best chain, so there is nothing more to do. 1508 startNode = b.bestChain.Next(startNode) 1509 if startNode == nil { 1510 return nil, 0 1511 } 1512 1513 // Calculate how many entries are needed. 1514 total := uint32((b.bestChain.Tip().height - startNode.height) + 1) 1515 if stopNode != nil && b.bestChain.Contains(stopNode) && 1516 stopNode.height >= startNode.height { 1517 1518 total = uint32((stopNode.height - startNode.height) + 1) 1519 } 1520 if total > maxEntries { 1521 total = maxEntries 1522 } 1523 1524 return startNode, total 1525 } 1526 1527 // locateBlocks returns the hashes of the blocks after the first known block in 1528 // the locator until the provided stop hash is reached, or up to the provided 1529 // max number of block hashes. 1530 // 1531 // See the comment on the exported function for more details on special cases. 1532 // 1533 // This function MUST be called with the chain state lock held (for reads). 1534 func (b *BlockChain) locateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash { 1535 // Find the node after the first known block in the locator and the 1536 // total number of nodes after it needed while respecting the stop hash 1537 // and max entries. 1538 node, total := b.locateInventory(locator, hashStop, maxHashes) 1539 if total == 0 { 1540 return nil 1541 } 1542 1543 // Populate and return the found hashes. 1544 hashes := make([]chainhash.Hash, 0, total) 1545 for i := uint32(0); i < total; i++ { 1546 hashes = append(hashes, node.hash) 1547 node = b.bestChain.Next(node) 1548 } 1549 return hashes 1550 } 1551 1552 // LocateBlocks returns the hashes of the blocks after the first known block in 1553 // the locator until the provided stop hash is reached, or up to the provided 1554 // max number of block hashes. 1555 // 1556 // In addition, there are two special cases: 1557 // 1558 // - When no locators are provided, the stop hash is treated as a request for 1559 // that block, so it will either return the stop hash itself if it is known, 1560 // or nil if it is unknown 1561 // - When locators are provided, but none of them are known, hashes starting 1562 // after the genesis block will be returned 1563 // 1564 // This function is safe for concurrent access. 1565 func (b *BlockChain) LocateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash { 1566 b.chainLock.RLock() 1567 hashes := b.locateBlocks(locator, hashStop, maxHashes) 1568 b.chainLock.RUnlock() 1569 return hashes 1570 } 1571 1572 // locateHeaders returns the headers of the blocks after the first known block 1573 // in the locator until the provided stop hash is reached, or up to the provided 1574 // max number of block headers. 1575 // 1576 // See the comment on the exported function for more details on special cases. 1577 // 1578 // This function MUST be called with the chain state lock held (for reads). 1579 func (b *BlockChain) locateHeaders(locator BlockLocator, hashStop *chainhash.Hash, maxHeaders uint32) []wire.BlockHeader { 1580 // Find the node after the first known block in the locator and the 1581 // total number of nodes after it needed while respecting the stop hash 1582 // and max entries. 1583 node, total := b.locateInventory(locator, hashStop, maxHeaders) 1584 if total == 0 { 1585 return nil 1586 } 1587 1588 // Populate and return the found headers. 1589 headers := make([]wire.BlockHeader, 0, total) 1590 for i := uint32(0); i < total; i++ { 1591 headers = append(headers, node.Header()) 1592 node = b.bestChain.Next(node) 1593 } 1594 return headers 1595 } 1596 1597 // LocateHeaders returns the headers of the blocks after the first known block 1598 // in the locator until the provided stop hash is reached, or up to a max of 1599 // wire.MaxBlockHeadersPerMsg headers. 1600 // 1601 // In addition, there are two special cases: 1602 // 1603 // - When no locators are provided, the stop hash is treated as a request for 1604 // that header, so it will either return the header for the stop hash itself 1605 // if it is known, or nil if it is unknown 1606 // - When locators are provided, but none of them are known, headers starting 1607 // after the genesis block will be returned 1608 // 1609 // This function is safe for concurrent access. 1610 func (b *BlockChain) LocateHeaders(locator BlockLocator, hashStop *chainhash.Hash) []wire.BlockHeader { 1611 b.chainLock.RLock() 1612 headers := b.locateHeaders(locator, hashStop, wire.MaxBlockHeadersPerMsg) 1613 b.chainLock.RUnlock() 1614 return headers 1615 } 1616 1617 // IndexManager provides a generic interface that the is called when blocks are 1618 // connected and disconnected to and from the tip of the main chain for the 1619 // purpose of supporting optional indexes. 1620 type IndexManager interface { 1621 // Init is invoked during chain initialize in order to allow the index 1622 // manager to initialize itself and any indexes it is managing. The 1623 // channel parameter specifies a channel the caller can close to signal 1624 // that the process should be interrupted. It can be nil if that 1625 // behavior is not desired. 1626 Init(*BlockChain, <-chan struct{}) error 1627 1628 // ConnectBlock is invoked when a new block has been connected to the 1629 // main chain. The set of output spent within a block is also passed in 1630 // so indexers can access the previous output scripts input spent if 1631 // required. 1632 ConnectBlock(database.Tx, *palcutil.Block, []SpentTxOut) error 1633 1634 // DisconnectBlock is invoked when a block has been disconnected from 1635 // the main chain. The set of outputs scripts that were spent within 1636 // this block is also returned so indexers can clean up the prior index 1637 // state for this block. 1638 DisconnectBlock(database.Tx, *palcutil.Block, []SpentTxOut) error 1639 } 1640 1641 // Config is a descriptor which specifies the blockchain instance configuration. 1642 type Config struct { 1643 // DB defines the database which houses the blocks and will be used to 1644 // store all metadata created by this package such as the utxo set. 1645 // 1646 // This field is required. 1647 DB database.DB 1648 1649 // Interrupt specifies a channel the caller can close to signal that 1650 // long running operations, such as catching up indexes or performing 1651 // database migrations, should be interrupted. 1652 // 1653 // This field can be nil if the caller does not desire the behavior. 1654 Interrupt <-chan struct{} 1655 1656 // ChainParams identifies which chain parameters the chain is associated 1657 // with. 1658 // 1659 // This field is required. 1660 ChainParams *chaincfg.Params 1661 1662 // Checkpoints hold caller-defined checkpoints that should be added to 1663 // the default checkpoints in ChainParams. Checkpoints must be sorted 1664 // by height. 1665 // 1666 // This field can be nil if the caller does not wish to specify any 1667 // checkpoints. 1668 Checkpoints []chaincfg.Checkpoint 1669 1670 // TimeSource defines the median time source to use for things such as 1671 // block processing and determining whether or not the chain is current. 1672 // 1673 // The caller is expected to keep a reference to the time source as well 1674 // and add time samples from other peers on the network so the local 1675 // time is adjusted to be in agreement with other peers. 1676 TimeSource MedianTimeSource 1677 1678 // SigCache defines a signature cache to use when when validating 1679 // signatures. This is typically most useful when individual 1680 // transactions are already being validated prior to their inclusion in 1681 // a block such as what is usually done via a transaction memory pool. 1682 // 1683 // This field can be nil if the caller is not interested in using a 1684 // signature cache. 1685 SigCache *txscript.SigCache 1686 1687 // IndexManager defines an index manager to use when initializing the 1688 // chain and connecting and disconnecting blocks. 1689 // 1690 // This field can be nil if the caller does not wish to make use of an 1691 // index manager. 1692 IndexManager IndexManager 1693 1694 // HashCache defines a transaction hash mid-state cache to use when 1695 // validating transactions. This cache has the potential to greatly 1696 // speed up transaction validation as re-using the pre-calculated 1697 // mid-state eliminates the O(N^2) validation complexity due to the 1698 // SigHashAll flag. 1699 // 1700 // This field can be nil if the caller is not interested in using a 1701 // signature cache. 1702 HashCache *txscript.HashCache 1703 } 1704 1705 // New returns a BlockChain instance using the provided configuration details. 1706 func New(config *Config) (*BlockChain, error) { 1707 // Enforce required config fields. 1708 if config.DB == nil { 1709 return nil, AssertError("blockchain.New database is nil") 1710 } 1711 if config.ChainParams == nil { 1712 return nil, AssertError("blockchain.New chain parameters nil") 1713 } 1714 if config.TimeSource == nil { 1715 return nil, AssertError("blockchain.New timesource is nil") 1716 } 1717 1718 // Generate a checkpoint by height map from the provided checkpoints 1719 // and assert the provided checkpoints are sorted by height as required. 1720 var checkpointsByHeight map[int32]*chaincfg.Checkpoint 1721 var prevCheckpointHeight int32 1722 if len(config.Checkpoints) > 0 { 1723 checkpointsByHeight = make(map[int32]*chaincfg.Checkpoint) 1724 for i := range config.Checkpoints { 1725 checkpoint := &config.Checkpoints[i] 1726 if checkpoint.Height <= prevCheckpointHeight { 1727 return nil, AssertError("blockchain.New " + 1728 "checkpoints are not sorted by height") 1729 } 1730 1731 checkpointsByHeight[checkpoint.Height] = checkpoint 1732 prevCheckpointHeight = checkpoint.Height 1733 } 1734 } 1735 1736 params := config.ChainParams 1737 targetTimespan := int64(params.TargetTimespan / time.Second) 1738 targetTimePerBlock := int64(params.TargetTimePerBlock / time.Second) 1739 adjustmentFactor := params.RetargetAdjustmentFactor 1740 b := BlockChain{ 1741 checkpoints: config.Checkpoints, 1742 checkpointsByHeight: checkpointsByHeight, 1743 db: config.DB, 1744 chainParams: params, 1745 timeSource: config.TimeSource, 1746 sigCache: config.SigCache, 1747 indexManager: config.IndexManager, 1748 minRetargetTimespan: targetTimespan / adjustmentFactor, 1749 maxRetargetTimespan: targetTimespan * adjustmentFactor, 1750 blocksPerRetarget: int32(targetTimespan / targetTimePerBlock), 1751 index: newBlockIndex(config.DB, params), 1752 hashCache: config.HashCache, 1753 bestChain: newChainView(nil), 1754 orphans: make(map[chainhash.Hash]*orphanBlock), 1755 prevOrphans: make(map[chainhash.Hash][]*orphanBlock), 1756 warningCaches: newThresholdCaches(vbNumBits), 1757 deploymentCaches: newThresholdCaches(chaincfg.DefinedDeployments), 1758 } 1759 1760 // Initialize the chain state from the passed database. When the db 1761 // does not yet contain any chain state, both it and the chain state 1762 // will be initialized to contain only the genesis block. 1763 if err := b.initChainState(); err != nil { 1764 return nil, err 1765 } 1766 1767 // Perform any upgrades to the various chain-specific buckets as needed. 1768 if err := b.maybeUpgradeDbBuckets(config.Interrupt); err != nil { 1769 return nil, err 1770 } 1771 1772 // Initialize and catch up all of the currently active optional indexes 1773 // as needed. 1774 if config.IndexManager != nil { 1775 err := config.IndexManager.Init(&b, config.Interrupt) 1776 if err != nil { 1777 return nil, err 1778 } 1779 } 1780 1781 // Initialize rule change threshold state caches. 1782 if err := b.initThresholdCaches(); err != nil { 1783 return nil, err 1784 } 1785 1786 bestNode := b.bestChain.Tip() 1787 log.Infof("Chain state (height %d, hash %v, totaltx %d, work %v)", 1788 bestNode.height, bestNode.hash, b.stateSnapshot.TotalTxns, 1789 bestNode.workSum) 1790 1791 return &b, nil 1792 }