mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-14 21:40:11 +00:00

* [NOD-540] Implement reachability (#545) * [NOD-540] Begin implementing reachability. * [NOD-540] Finish implementing reachability. * [NOD-540] Implement TestIsFutureBlock. * [NOD-540] Implement TestInsertFutureBlock. * [NOD-540] Add comments. * [NOD-540] Add comment for interval in blockNode. * [NOD-540] Updated comments over insertFutureBlock and isFutureBlock. * [NOD-540] Implement interval splitting methods. * [NOD-540] Begin implementing tree manipulation in blockNode. * [NOD-540] Implement countSubtreesUp. * [NOD-540] Add a comment explaining an impossible condition. * [NOD-540] Implement applyIntervalDown. * [NOD-540] Moved the reachability tree stuff into reachability.go. * [NOD-540] Add some comments. * [NOD-540] Add more comments, implement isInPast. * [NOD-540] Fix comments. * [NOD-540] Implement TestSplitFraction. * [NOD-540] Implement TestSplitExact. * [NOD-540] Implement TestSplit. * [NOD-540] Add comments to structs. * [NOD-540] Implement TestAddTreeChild. * [NOD-540] Fix a comment. * [NOD-540] Rename isInPast to isAncestorOf. * [NOD-540] Rename futureBlocks to futureCoveringSet. * [NOD-540] Rename isFutureBlock to isInFuture. * [NOD-540] move reachabilityInterval to the top of reachability.go. * [NOD-540] Change "s.t." to "such that" in a comment. * [NOD-540] Fix indentation. * [NOD-540] Fix a potential bug involving float inaccuracy. * [NOD-540] Wrote a more descriptive error message. * [NOD-540] Fix error messsage. * [NOD-540] Fix the recursive countSubtreesUp. * [NOD-540] Rename countSubtreesUp to countSubtrees and applyIntervalDown to propagateInterval. * [NOD-540] Implement updating reachability for a valid new block. * [NOD-540] Implement a disk storage for reachability data. * [NOD-540] Fix not all tree nodes being written to the database. * [NOD-540] Implement serialization for reachabilityData. * [NOD-540] Implement some deserialization for reachabilityData. * [NOD-540] Implement restoring the reachabilityStore on node restart. * [NOD-540] Made interval and remainingInterval pointers. * [NOD-540] Rename setTreeInterval to setInterval. * [NOD-540] Rename reindexTreeIntervals to reindexIntervals and fixed the comment above it. * [NOD-540] Expand the comment above reindexIntervals. * [NOD-540] Fix comment above countSubtrees. * [NOD-540] Fix comment above countSubtrees some more. * [NOD-540] Fix comment above split. * [NOD-540] Fix comment above isAncestorOf. * [NOD-540] Fix comment above reachabilityTreeNode. * [NOD-540] Fix weird condition in addTreeChild. * [NOD-540] Rename addTreeChild to addChild. * [NOD-540] Fix weird condition in splitFraction. * [NOD-540] Reverse the lines in reachabilityTreeNode.String(). * [NOD-540] Renamed f to fraction and x to size. * [NOD-540] Fix comment above bisect. * [NOD-540] Implement rtn.isAncestorOf(). * [NOD-540] Use treeNode isAncestorOf instead of treeInterval isAncestorOf. * [NOD-540] Use newReachabilityInterval instead of struct initialization. * [NOD-540] Make reachabilityTreeNode.String() use strings.Join. * [NOD-540] Use sync.RWMutex instead of locks.PriorityMutex. * [NOD-540] Rename thisTreeNode to newTreeNode. * [NOD-540] Rename setTreeNode to addTreeNode. * [NOD-540] Extracted selectedParentAnticone to a separate function. * [NOD-540] Rename node to this. * [NOD-540] Move updateReachability and isAncestorOf from dag.go to reachability.go. * [NOD-540] Add whitespace after multiline function signatures in reachability.go. * [NOD-540] Make splitFraction return an error on empty interval. * [NOD-540] Add a comment about rounding to splitFraction. * [NOD-540] Replace sneaky tabs with spaces. * [NOD-540] Rename split to splitExponential. * [NOD-540] Extract exponentialFractions to a separate function. * [NOD-540] Rename bisect to findIndex. * [NOD-540] Add call to reachabilityStore.clearDirtyEntries at the end of saveChangesFromBlock. * [NOD-540] Explain the dirty hack in reachabilityStore.init(). * [NOD-540] Split the function signature for deserializeReachabilityData to two lines. * [NOD-540] Add a comment about float precision loss to exponentialFractions. * [NOD-540] Corrected a comment about float precision loss to exponentialFractions. * [NOD-540] Fixed a comment about float precision loss to exponentialFractions some more. * [NOD-540] Added further comments above futureCoveringBlockSet. * [NOD-540] Rename addTreeNode to setTreeNode. * [NOD-540] Rename splitExponential to splitWithExponentialBias. * [NOD-540] Fix object references in reachabilityData deserialization (#563) * [NOD-540] Fix broken references in deserialization. * [NOD-540] Fix broken references in futureCoveringSet deserialization. Also add comments. * [NOD-540] Don't deserialize on the first pass in reachabilityStore.init(). * [NOD-540] Remove redundant assignment to loaded[hash]. * [NOD-540] Use NewHash instead of SetBytes. Rename data to destination. * [NOD-540] Preallocate futureCoveringSet. * [NOD-541] Implement GHOSTDAG (#560) * [NOD-541] Implement GHOSTDAG * [NOD-541] Replace the old PHANTOM variant with GHOSTDAG * [NOD-541] Move dag.updateReachability to the top of dag.applyDAGChanges to update reachability before the virtual block is updated * [NOD-541] Fix blueAnticoneSize * [NOD-541] Initialize node.bluesAnticoneSizes * [NOD-541] Fix pastUTXO and applyBlueBlocks blues order * [NOD-541] Add serialization logic to node.bluesAnticoneSizes * [NOD-541] Fix GHOSTDAG to not count the new block and the blue candidates anticone, add selected parent to blues, and save to node.bluesAnticoneSizes properly * [NOD-541] Fix test names in inner strings * [NOD-541] Writing TestGHOSTDAG * [NOD-541] In blueAnticoneSize change node->current * [NOD-541] name ghostdag return values * [NOD-541] fix ghostdag to return slice * [NOD-541] Split k-cluster violation rules * [NOD-541] Add missing space * [NOD-541] Add comment to ghostdag * [NOD-541] In selectedParentAnticone rename past->selectedParentPast * [NOD-541] Fix misrefernces to TestChainUpdates * [NOD-541] Fix ghostdag comment * [NOD-541] Make PrepareBlockForTest in blockdag package * [NOD-541] Make PrepareBlockForTest in blockdag package * [NOD-541] Assign to selectedParentAnticone[i] instead of appending * [NOD-541] Remove redundant forceTransactions arguments from PrepareBlockForTEST * [NOD-541] Add non-selected parents to anticoneHeap * [NOD-541] add test for ghostdag * [NOD-541] Add comments * [NOD-541] Use adjusted time for initializing blockNode * [NOD-541] Rename isAncestorOf -> isAncestorOfBlueCandidate * [NOD-541] Remove params from PrepareBlockForTest * [NOD-541] Fix TestChainHeight * [NOD-541] Remove recursive lock * [NOD-541] Fix TestTxIndexConnectBlock * [NOD-541] Fix TestBlueBlockWindow * [NOD-541] Put prepareAndProcessBlock in common_test.go * [NOD-541] Fix TestConfirmations * [NOD-541] Fix TestAcceptingBlock * [NOD-541] Fix TestDifficulty * [NOD-541] Fix TestVirtualBlock * [NOD-541] Fix TestSelectedPath * [NOD-541] Fix TestChainUpdates * [NOD-541] Shorten TestDifficulty test time * [NOD-541] Make PrepareBlockForTest use minimal valid block time * [NOD-541] Remove TODO comment * [NOD-541] Move blockdag related mining functions to mining.go * [NOD-541] Use NextBlockCoinbaseTransaction instead of NextBlockCoinbaseTransactionNoLock in NextCoinbaseFromAddress * [NOD-541] Remove useMinimalTime from BlockForMining * [NOD-541] Make MedianAdjustedTime a *BlockDAG method * [NOD-541] Fix ghostdag to use anticone slice instead of heap * [NOD-541] Fix NewBlockTemplate locks * [NOD-541] Fix ghostdag comments * [NOD-541] Convert MedianAdjustedTime to NextBlockTime * [NOD-541] Fix ghostdag comment * [NOD-541] Fix TestGHOSTDAG comment * [NOD-541] Add comment before sanity check * [NOD-541] Explicitly initialize .blues in ghostdag * [NOD-541] Rename *blockNode.lessThan to *blockNode.less * [NOD-541] Remove redundant check if block != chainBlock * [NOD-541] Fix comment * [NOD-541] Fix comment * [NOD-497] Add comment; General refactoring * [NOD-497] General refactoring. * [NOD-497] Use isAncestor of the tree rather than the node * [NOD-497] Remove reachability mutex lock as it is redundant (dag lock is held so no need); General refactoring. * [NOD-497] Update comment * [NOD-497] Undo test blocktimestamp * [NOD-497] Update comments; Use BlockNode.less for blockset; * [NOD-497] Change processBlock to return boolean and not the delay duration (merge conflict) * [NOD-497] Undo change for bluest to use less; Change blocknode less to use daghash.Less Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com> Co-authored-by: Dan Aharoni <dereeno@protonmail.com>
176 lines
6.6 KiB
Go
176 lines
6.6 KiB
Go
package blockdag
|
||
|
||
import (
|
||
"github.com/pkg/errors"
|
||
"sort"
|
||
)
|
||
|
||
// ghostdag runs the GHOSTDAG protocol and updates newNode.blues,
|
||
// newNode.selectedParent and newNode.bluesAnticoneSizes accordingly.
|
||
// The function updates newNode.blues by iterating over the blocks in
|
||
// the anticone of newNode.selectedParent (which is the parent with the
|
||
// highest blue score) and adds any block to newNode.blues if by adding
|
||
// it to newNode.blues these conditions will not be violated:
|
||
//
|
||
// 1) |anticone-of-candidate-block ∩ blue-set-of-newNode| ≤ K
|
||
//
|
||
// 2) For every blue block in blue-set-of-newNode:
|
||
// |(anticone-of-blue-block ∩ blue-set-newNode) ∪ {candidate-block}| ≤ K.
|
||
// We validate this condition by maintaining a map bluesAnticoneSizes for
|
||
// each block which holds all the blue anticone sizes that were affected by
|
||
// the new added blue blocks.
|
||
// So to find out what is |anticone-of-blue ∩ blue-set-of-newNode| we just iterate in
|
||
// the selected parent chain of newNode until we find an existing entry in
|
||
// bluesAnticoneSizes.
|
||
//
|
||
// For further details see the article https://eprint.iacr.org/2018/104.pdf
|
||
func (dag *BlockDAG) ghostdag(newNode *blockNode) (selectedParentAnticone []*blockNode, err error) {
|
||
newNode.selectedParent = newNode.parents.bluest()
|
||
newNode.bluesAnticoneSizes[*newNode.selectedParent.hash] = 0
|
||
newNode.blues = []*blockNode{newNode.selectedParent}
|
||
selectedParentAnticone, err = dag.selectedParentAnticone(newNode)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
sort.Slice(selectedParentAnticone, func(i, j int) bool {
|
||
return selectedParentAnticone[i].less(selectedParentAnticone[j])
|
||
})
|
||
|
||
for _, blueCandidate := range selectedParentAnticone {
|
||
candidateBluesAnticoneSizes := make(map[*blockNode]uint32)
|
||
var candidateAnticoneSize uint32
|
||
possiblyBlue := true
|
||
|
||
// Iterate over all blocks in the blue set of newNode that are not in the past
|
||
// of blueCandidate, and check for each one of them if blueCandidate potentially
|
||
// enlarges their blue anticone to be over K, or that they enlarge the blue anticone
|
||
// of blueCandidate to be over K.
|
||
for chainBlock := newNode; possiblyBlue; chainBlock = chainBlock.selectedParent {
|
||
// If blueCandidate is in the future of chainBlock, it means
|
||
// that all remaining blues are in the past of chainBlock and thus
|
||
// in the past of blueCandidate. In this case we know for sure that
|
||
// the anticone of blueCandidate will not exceed K, and we can mark
|
||
// it as blue.
|
||
//
|
||
// newNode is always in the future of blueCandidate, so there's
|
||
// no point in checking it.
|
||
if chainBlock != newNode {
|
||
if isAncestorOfBlueCandidate, err := dag.isAncestorOf(chainBlock, blueCandidate); err != nil {
|
||
return nil, err
|
||
} else if isAncestorOfBlueCandidate {
|
||
break
|
||
}
|
||
}
|
||
|
||
for _, block := range chainBlock.blues {
|
||
// Skip blocks that exist in the past of blueCandidate.
|
||
if isAncestorOfBlueCandidate, err := dag.isAncestorOf(block, blueCandidate); err != nil {
|
||
return nil, err
|
||
} else if isAncestorOfBlueCandidate {
|
||
continue
|
||
}
|
||
|
||
candidateBluesAnticoneSizes[block], err = dag.blueAnticoneSize(block, newNode)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
candidateAnticoneSize++
|
||
|
||
if candidateAnticoneSize > dag.dagParams.K {
|
||
// k-cluster violation: The candidate's blue anticone exceeded k
|
||
possiblyBlue = false
|
||
break
|
||
}
|
||
|
||
if candidateBluesAnticoneSizes[block] == dag.dagParams.K {
|
||
// k-cluster violation: A block in candidate's blue anticone already
|
||
// has k blue blocks in its own anticone
|
||
possiblyBlue = false
|
||
break
|
||
}
|
||
|
||
// This is a sanity check that validates that a blue
|
||
// block's blue anticone is not already larger than K.
|
||
if candidateBluesAnticoneSizes[block] > dag.dagParams.K {
|
||
return nil, errors.New("found blue anticone size larger than k")
|
||
}
|
||
}
|
||
}
|
||
|
||
if possiblyBlue {
|
||
// No k-cluster violation found, we can now set the candidate block as blue
|
||
newNode.blues = append(newNode.blues, blueCandidate)
|
||
newNode.bluesAnticoneSizes[*blueCandidate.hash] = candidateAnticoneSize
|
||
for blue, blueAnticoneSize := range candidateBluesAnticoneSizes {
|
||
newNode.bluesAnticoneSizes[*blue.hash] = blueAnticoneSize + 1
|
||
}
|
||
|
||
// The maximum length of node.blues can be K+1 because
|
||
// it contains the selected parent.
|
||
if uint32(len(newNode.blues)) == dag.dagParams.K+1 {
|
||
break
|
||
}
|
||
}
|
||
}
|
||
|
||
newNode.blueScore = newNode.selectedParent.blueScore + uint64(len(newNode.blues))
|
||
return selectedParentAnticone, nil
|
||
}
|
||
|
||
// selectedParentAnticone returns the blocks in the anticone of the selected parent of the given node.
|
||
// The function work as follows.
|
||
// We start by adding all parents of the node (other than the selected parent) to a process queue.
|
||
// For each node in the queue:
|
||
// we check whether it is in the past of the selected parent.
|
||
// If not, we add the node to the resulting anticone-set and queue it for processing.
|
||
func (dag *BlockDAG) selectedParentAnticone(node *blockNode) ([]*blockNode, error) {
|
||
anticoneSet := newSet()
|
||
var anticoneSlice []*blockNode
|
||
selectedParentPast := newSet()
|
||
var queue []*blockNode
|
||
// Queueing all parents (other than the selected parent itself) for processing.
|
||
for _, parent := range node.parents {
|
||
if parent == node.selectedParent {
|
||
continue
|
||
}
|
||
anticoneSet.add(parent)
|
||
anticoneSlice = append(anticoneSlice, parent)
|
||
queue = append(queue, parent)
|
||
}
|
||
for len(queue) > 0 {
|
||
var current *blockNode
|
||
current, queue = queue[0], queue[1:]
|
||
// For each parent of a the current node we check whether it is in the past of the selected parent. If not,
|
||
// we add the it to the resulting anticone-set and queue it for further processing.
|
||
for _, parent := range current.parents {
|
||
if anticoneSet.contains(parent) || selectedParentPast.contains(parent) {
|
||
continue
|
||
}
|
||
isAncestorOfSelectedParent, err := dag.isAncestorOf(parent, node.selectedParent)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if isAncestorOfSelectedParent {
|
||
selectedParentPast.add(parent)
|
||
continue
|
||
}
|
||
anticoneSet.add(parent)
|
||
anticoneSlice = append(anticoneSlice, parent)
|
||
queue = append(queue, parent)
|
||
}
|
||
}
|
||
return anticoneSlice, nil
|
||
}
|
||
|
||
// blueAnticoneSize returns the blue anticone size of 'block' from the worldview of 'context'.
|
||
// Expects 'block' to be in the blue set of 'context'
|
||
func (dag *BlockDAG) blueAnticoneSize(block, context *blockNode) (uint32, error) {
|
||
for current := context; current != nil; current = current.selectedParent {
|
||
if blueAnticoneSize, ok := current.bluesAnticoneSizes[*block.hash]; ok {
|
||
return blueAnticoneSize, nil
|
||
}
|
||
}
|
||
return 0, errors.Errorf("block %s is not in blue set of %s", block.hash, context.hash)
|
||
}
|