mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-08-22 18:43:15 +00:00

* [NOD-1190] Move non-processBlock stuff out of process.go. * [NOD-1190] Move everything out of accept.go. * [NOD-1190] Move all processBlock functions to process.go. * [NOD-1190] Move orphan stuff to orphan.go. * [NOD-1190] Remove thresholdstate stuff. * [NOD-1190] Move isSynced to sync_rate.go. * [NOD-1190] Move delayed block stuff to delayed_blocks.go. * [NOD-1190] Rename orphans.go to orphaned_blocks.go. * [NOD-1190] Move non-BlockDAG structs out of dag.go. * [NOD-1190] Remove unused fields. * [NOD-1190] Fixup BlockDAG.New a bit. * [NOD-1190] Move sequence lock stuff to sequence_lock.go * [NOD-1190] Move some multiset stuff out of dag.go. * [NOD-1190] Move finality stuff out of dag.go. * [NOD-1190] Move blocklocator stuff out of dag.go. * [NOD-1190] Move confirmation stuff out of dag.go. * [NOD-1190] Move utxo and selected parent chain stuff out of dag.go. * [NOD-1190] Move BlockDAG lock functions to the beginning of dag.go. * [NOD-1190] Move verifyAndBuildUTXO out of process.go. * [NOD-1190] Extract handleProcessBlockError to a function. * [NOD-1190] Remove daglock unlock in notifyBlockAccepted. * [NOD-1190] Extract checkDuplicateBlock to a method. * [NOD-1190] Fix merge errors. * [NOD-1190] Remove unused parameter from CalcSequenceLock. * [NOD-1190] Extract processBlock contents into functions. * [NOD-1190] Fix parent delayed blocks not marking their children as delayed * [NOD-1190] Fix TestProcessDelayedBlocks. * [NOD-1190] Extract stuff in maybeAcceptBlock to separate functions. * [NOD-1190] Rename handleProcessBlockError to handleConnectBlockError. * [NOD-1190] Remove some comments. * [NOD-1190] Use lowercase in error messages. * [NOD-1190] Rename createNewBlockNode to createBlockNodeFromBlock. * [NOD-1190] Rename orphaned_blocks.go to orpan_blocks.go. * [NOD-1190] Extract validateUTXOCommitment to a separate function. * [NOD-1190] Fix a bug in validateUTXOCommitment. * [NOD-1190] Rename checkBlockTxsFinalized to checkBlockTransactionsFinalized. * [NOD-1190] Add a comment over createBlockNodeFromBlock. * [NOD-1190] Fold validateAllTxsFinalized into checkBlockTransactionsFinalized. * [NOD-1190] Return parents from checkBlockParents. * [NOD-1190] Remove the processBlock prefix from the functions that had it. * [NOD-1190] Begin extracting functions out of checkTransactionSanity. * [NOD-1190] Finish extracting functions out of checkTransactionSanity. * [NOD-1190] Remove an unused parameter. * [NOD-1190] Fix merge errors. * [NOD-1190] Added an explanation as to why we change the nonce in TestProcessDelayedBlocks. * [NOD-1190] Fix a comment. * [NOD-1190] Fix a comment. * [NOD-1190] Fix a typo. * [NOD-1190] Replace checkBlockParents with handleLookupParentNodesError.
178 lines
4.9 KiB
Go
178 lines
4.9 KiB
Go
// Copyright (c) 2015-2017 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package blockdag
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/kaspanet/kaspad/dbaccess"
|
|
"github.com/kaspanet/kaspad/util"
|
|
"sync"
|
|
|
|
"github.com/kaspanet/kaspad/dagconfig"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
)
|
|
|
|
// blockIndex provides facilities for keeping track of an in-memory index of the
|
|
// block DAG.
|
|
type blockIndex struct {
|
|
// The following fields are set when the instance is created and can't
|
|
// be changed afterwards, so there is no need to protect them with a
|
|
// separate mutex.
|
|
dagParams *dagconfig.Params
|
|
|
|
sync.RWMutex
|
|
index map[daghash.Hash]*blockNode
|
|
dirty map[*blockNode]struct{}
|
|
}
|
|
|
|
// newBlockIndex returns a new empty instance of a block index. The index will
|
|
// be dynamically populated as block nodes are loaded from the database and
|
|
// manually added.
|
|
func newBlockIndex(dagParams *dagconfig.Params) *blockIndex {
|
|
return &blockIndex{
|
|
dagParams: dagParams,
|
|
index: make(map[daghash.Hash]*blockNode),
|
|
dirty: make(map[*blockNode]struct{}),
|
|
}
|
|
}
|
|
|
|
// HaveBlock returns whether or not the block index contains the provided hash.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) HaveBlock(hash *daghash.Hash) bool {
|
|
bi.RLock()
|
|
defer bi.RUnlock()
|
|
_, hasBlock := bi.index[*hash]
|
|
return hasBlock
|
|
}
|
|
|
|
// LookupNode returns the block node identified by the provided hash. It will
|
|
// return nil if there is no entry for the hash.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) LookupNode(hash *daghash.Hash) (*blockNode, bool) {
|
|
bi.RLock()
|
|
defer bi.RUnlock()
|
|
node, ok := bi.index[*hash]
|
|
return node, ok
|
|
}
|
|
|
|
// AddNode adds the provided node to the block index and marks it as dirty.
|
|
// Duplicate entries are not checked so it is up to caller to avoid adding them.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) AddNode(node *blockNode) {
|
|
bi.Lock()
|
|
defer bi.Unlock()
|
|
bi.addNode(node)
|
|
bi.dirty[node] = struct{}{}
|
|
}
|
|
|
|
// addNode adds the provided node to the block index, but does not mark it as
|
|
// dirty. This can be used while initializing the block index.
|
|
//
|
|
// This function is NOT safe for concurrent access.
|
|
func (bi *blockIndex) addNode(node *blockNode) {
|
|
bi.index[*node.hash] = node
|
|
}
|
|
|
|
// NodeStatus provides concurrent-safe access to the status field of a node.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) NodeStatus(node *blockNode) blockStatus {
|
|
bi.RLock()
|
|
defer bi.RUnlock()
|
|
status := node.status
|
|
return status
|
|
}
|
|
|
|
// SetStatusFlags flips the provided status flags on the block node to on,
|
|
// regardless of whether they were on or off previously. This does not unset any
|
|
// flags currently on.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) SetStatusFlags(node *blockNode, flags blockStatus) {
|
|
bi.Lock()
|
|
defer bi.Unlock()
|
|
node.status |= flags
|
|
bi.dirty[node] = struct{}{}
|
|
}
|
|
|
|
// UnsetStatusFlags flips the provided status flags on the block node to off,
|
|
// regardless of whether they were on or off previously.
|
|
//
|
|
// This function is safe for concurrent access.
|
|
func (bi *blockIndex) UnsetStatusFlags(node *blockNode, flags blockStatus) {
|
|
bi.Lock()
|
|
defer bi.Unlock()
|
|
node.status &^= flags
|
|
bi.dirty[node] = struct{}{}
|
|
}
|
|
|
|
// flushToDB writes all dirty block nodes to the database.
|
|
func (bi *blockIndex) flushToDB(dbContext *dbaccess.TxContext) error {
|
|
bi.Lock()
|
|
defer bi.Unlock()
|
|
if len(bi.dirty) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for node := range bi.dirty {
|
|
serializedBlockNode, err := serializeBlockNode(node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
key := blockIndexKey(node.hash, node.blueScore)
|
|
err = dbaccess.StoreIndexBlock(dbContext, key, serializedBlockNode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (bi *blockIndex) clearDirtyEntries() {
|
|
bi.dirty = make(map[*blockNode]struct{})
|
|
}
|
|
|
|
func (dag *BlockDAG) addNodeToIndexWithInvalidAncestor(block *util.Block) error {
|
|
blockHeader := &block.MsgBlock().Header
|
|
newNode, _ := dag.newBlockNode(blockHeader, newBlockSet())
|
|
newNode.status = statusInvalidAncestor
|
|
dag.index.AddNode(newNode)
|
|
|
|
dbTx, err := dag.databaseContext.NewTx()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dbTx.RollbackUnlessClosed()
|
|
err = dag.index.flushToDB(dbTx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return dbTx.Commit()
|
|
}
|
|
|
|
func lookupParentNodes(block *util.Block, dag *BlockDAG) (blockSet, error) {
|
|
header := block.MsgBlock().Header
|
|
parentHashes := header.ParentHashes
|
|
|
|
nodes := newBlockSet()
|
|
for _, parentHash := range parentHashes {
|
|
node, ok := dag.index.LookupNode(parentHash)
|
|
if !ok {
|
|
str := fmt.Sprintf("parent block %s is unknown", parentHash)
|
|
return nil, ruleError(ErrParentBlockUnknown, str)
|
|
} else if dag.index.NodeStatus(node).KnownInvalid() {
|
|
str := fmt.Sprintf("parent block %s is known to be invalid", parentHash)
|
|
return nil, ruleError(ErrInvalidAncestorBlock, str)
|
|
}
|
|
|
|
nodes.add(node)
|
|
}
|
|
|
|
return nodes, nil
|
|
}
|