mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00

* [DEV-134] Implement Continuous Integration Squashed commit: [5e41d830] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [4c95e293] [DEV-134] Made golint ignore the vendor directory. [21736dbc] [DEV-134] Renamed ExampleBlockChain_ProcessBlock to ExampleBlockDAG_ProcessBlock to satisfy go vet. [beea6486] [DEV-134] Removed pushing the built docker to a remove repository. That's unnecessary at this stage. [bee911ed] [DEV-134] Made all precompilation checks run on everything instead of only the root dir. [585f92ae] [DEV-134] Added "github.com/pkg/errors" to dep. [5f02f570] [DEV-134] -vendor-only is written with only one hyphen. [3eee7f95] [DEV-134] go vet instead of go tool vet. [0c2d4343] [DEV-134] Split all the pre-compile checks to separate lines to be able to tell which of them is failing. [780519c8] [DEV-134] Ran gofmt on everything. [8247146b] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [bff68aa3] [DEV-134] Gave executable permission to deploy.sh [638a99d9] [DEV-134] Added jenkinsfile and deploy script. * [DEV-134] Added a robust testing script. * [DEV-134] Fixed a bash-ism. * [DEV-134] Disabled testing with coverage for now. * [DEV-134] Disabled golint and removed removing debug symbols. * [DEV-134] Disabled aligncheck. * [DEV-134] Disabled structcheck and varcheck. * [DEV-134] Added "don't inline functions" to compiler flags for testing. * [DEV-134] Made build fail if gofmt prints out anything. * [DEV-134] Fixed misleading comment. * [DEV-134] Added comments to test.sh. * [DEV-134] Renamed tm to measure_runtime and removed do_ prefixes from functions. * [DEV-134] Fixed gofmt line in build script. * [DEV-134] Fixed gofmt some more. * [DEV-134] Fixed gofmt not actually failing due to logical or.
106 lines
3.2 KiB
Go
106 lines
3.2 KiB
Go
package blockdag
|
|
|
|
import (
|
|
"github.com/daglabs/btcd/dagconfig/daghash"
|
|
)
|
|
|
|
// phantom calculates and returns the block's blue set, selected parent and blue score.
|
|
// Chain start is determined by going down the DAG through the selected path
|
|
// (follow the selected parent of each block) k + 1 steps.
|
|
// The blue set of a block are all blue blocks in its past.
|
|
// To optimize memory usage, for each block we are storing only the blue blocks in
|
|
// its selected parent's anticone that are in the future of the chain start
|
|
// as well as the selected parent itself - the rest of the
|
|
// blue set can be restored by traversing the selected parent chain and combining
|
|
// the .blues of all blocks in it.
|
|
// The blue score is the total number of blocks in this block's blue set
|
|
// of the selected parent. (the blue score of the genesis block is defined as 0)
|
|
// The selected parent is chosen by determining which block's parent will give this block the highest blue score.
|
|
func phantom(block *blockNode, k uint32) (blues []*blockNode, selectedParent *blockNode, score uint64) {
|
|
bestScore := uint64(0)
|
|
var bestParent *blockNode
|
|
var bestBlues []*blockNode
|
|
var bestHash *daghash.Hash
|
|
for _, parent := range block.parents {
|
|
chainStart := digToChainStart(parent, k)
|
|
candidates := blueCandidates(chainStart)
|
|
blues := traverseCandidates(block, candidates, parent)
|
|
score := uint64(len(blues)) + parent.blueScore
|
|
|
|
if score > bestScore || (score == bestScore && (bestHash == nil || daghash.Less(&parent.hash, bestHash))) {
|
|
bestScore = score
|
|
bestBlues = blues
|
|
bestParent = parent
|
|
bestHash = &parent.hash
|
|
}
|
|
}
|
|
|
|
return bestBlues, bestParent, bestScore
|
|
}
|
|
|
|
// digToChainStart digs through the selected path and returns the block in depth k+1
|
|
func digToChainStart(parent *blockNode, k uint32) *blockNode {
|
|
current := parent
|
|
|
|
for i := uint32(0); i < k; i++ {
|
|
if current.isGenesis() {
|
|
break
|
|
}
|
|
current = current.selectedParent
|
|
}
|
|
|
|
return current
|
|
}
|
|
|
|
func blueCandidates(chainStart *blockNode) blockSet {
|
|
candidates := newSet()
|
|
candidates.add(chainStart)
|
|
|
|
queue := []*blockNode{chainStart}
|
|
for len(queue) > 0 {
|
|
var current *blockNode
|
|
current, queue = queue[0], queue[1:]
|
|
|
|
children := current.children
|
|
for _, child := range children {
|
|
if !candidates.contains(child) {
|
|
candidates.add(child)
|
|
queue = append(queue, child)
|
|
}
|
|
}
|
|
}
|
|
|
|
return candidates
|
|
}
|
|
|
|
//traverseCandidates returns all the blocks that are in the future of the chain start and in the anticone of the selected parent
|
|
func traverseCandidates(newBlock *blockNode, candidates blockSet, selectedParent *blockNode) []*blockNode {
|
|
blues := []*blockNode{}
|
|
selectedParentPast := newSet()
|
|
queue := NewHeap()
|
|
visited := newSet()
|
|
|
|
for _, parent := range newBlock.parents {
|
|
queue.Push(parent)
|
|
}
|
|
|
|
for queue.Len() > 0 {
|
|
current := queue.pop()
|
|
if candidates.contains(current) {
|
|
if current == selectedParent || selectedParentPast.anyChildInSet(current) {
|
|
selectedParentPast.add(current)
|
|
} else {
|
|
blues = append(blues, current)
|
|
}
|
|
for _, parent := range current.parents {
|
|
if !visited.contains(parent) {
|
|
visited.add(parent)
|
|
queue.Push(parent)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return append(blues, selectedParent)
|
|
}
|