mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-1477] Add selected parent to merge set (#967)
* [NOD-1477] Add selected parent to merge set * [NOD-1469] Init BluesAnticoneSizes * [NOD-1477] Undo changes in hash comparison
This commit is contained in:
parent
be56fb7e8b
commit
a132f55302
@ -2,6 +2,7 @@ package ghostdagmanager
|
||||
|
||||
import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||
)
|
||||
|
||||
func (gm *ghostdagManager) findSelectedParent(parentHashes []*externalapi.DomainHash) (*externalapi.DomainHash, error) {
|
||||
@ -45,7 +46,7 @@ func (gm *ghostdagManager) ChooseSelectedParent(blockHashA *externalapi.DomainHa
|
||||
blockABlueScore := blockAGHOSTDAGData.BlueScore
|
||||
blockBBlueScore := blockBGHOSTDAGData.BlueScore
|
||||
if blockABlueScore == blockBBlueScore {
|
||||
if hashesLess(blockHashA, blockHashB) {
|
||||
if hashes.Less(blockHashA, blockHashB) {
|
||||
return blockHashB, nil
|
||||
}
|
||||
return blockHashA, nil
|
||||
@ -55,16 +56,3 @@ func (gm *ghostdagManager) ChooseSelectedParent(blockHashA *externalapi.DomainHa
|
||||
}
|
||||
return blockHashA, nil
|
||||
}
|
||||
|
||||
func hashesLess(a, b *externalapi.DomainHash) bool {
|
||||
// We compare the hashes backwards because Hash is stored as a little endian byte array.
|
||||
for i := len(a) - 1; i >= 0; i-- {
|
||||
switch {
|
||||
case a[i] < b[i]:
|
||||
return true
|
||||
case a[i] > b[i]:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -39,13 +39,17 @@ func (gm *ghostdagManager) GHOSTDAG(blockHash *externalapi.DomainHash) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newBlockData.SelectedParent = selectedParent
|
||||
mergeSet, err := gm.mergeSet(newBlockData.SelectedParent, blockParents)
|
||||
newBlockData.MergeSetBlues = append(newBlockData.MergeSetBlues, selectedParent)
|
||||
newBlockData.BluesAnticoneSizes[*selectedParent] = 0
|
||||
|
||||
mergeSetWithoutSelectedParent, err := gm.mergeSetWithoutSelectedParent(newBlockData.SelectedParent, blockParents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, blueCandidate := range mergeSet {
|
||||
for _, blueCandidate := range mergeSetWithoutSelectedParent {
|
||||
isBlue, candidateAnticoneSize, candidateBluesAnticoneSizes, err := gm.checkBlueCandidate(newBlockData, blueCandidate)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -101,7 +105,7 @@ func (gm *ghostdagManager) checkBlueCandidate(newBlockData *model.BlockGHOSTDAGD
|
||||
isBlue, isRed, err := gm.checkBlueCandidateWithChainBlock(newBlockData, chainBlock, blueCandidate, candidateBluesAnticoneSizes,
|
||||
&candidateAnticoneSize)
|
||||
if err != nil {
|
||||
return false, 0, nil, nil
|
||||
return false, 0, nil, err
|
||||
}
|
||||
|
||||
if isBlue {
|
||||
@ -154,7 +158,7 @@ func (gm *ghostdagManager) checkBlueCandidateWithChainBlock(newBlockData *model.
|
||||
// Skip blocks that exist in the past of blueCandidate.
|
||||
isAncestorOfBlueCandidate, err := gm.dagTopologyManager.IsAncestorOf(block, blueCandidate)
|
||||
if err != nil {
|
||||
return false, false, nil
|
||||
return false, false, err
|
||||
}
|
||||
|
||||
if isAncestorOfBlueCandidate {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func (gm *ghostdagManager) mergeSet(selecteParent *externalapi.DomainHash,
|
||||
func (gm *ghostdagManager) mergeSetWithoutSelectedParent(selecteParent *externalapi.DomainHash,
|
||||
blockParents []*externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
|
||||
|
||||
mergeSetMap := make(map[externalapi.DomainHash]struct{}, gm.k)
|
||||
|
Loading…
x
Reference in New Issue
Block a user