mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-02-21 11:17:05 +00:00
Compare commits
3 Commits
v1sectimeo
...
ghostdagRe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16a83f9bac | ||
|
|
7d20ee6b58 | ||
|
|
1ab05c3fbc |
@@ -1,9 +1,8 @@
|
||||
package ghostdag2
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/kaspanet/kaspad/util/difficulty"
|
||||
"sort"
|
||||
|
||||
"math/big"
|
||||
|
||||
@@ -19,7 +18,7 @@ type ghostdagHelper struct {
|
||||
headerStore model.BlockHeaderStore
|
||||
}
|
||||
|
||||
// New creates a new instance of this alternative ghostdag impl
|
||||
// New creates a new instance of this alternative GHOSTDAG impl
|
||||
func New(
|
||||
databaseContext model.DBReader,
|
||||
dagTopologyManager model.DAGTopologyManager,
|
||||
@@ -36,53 +35,60 @@ func New(
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------- */
|
||||
|
||||
func (gh *ghostdagHelper) GHOSTDAG(stagingArea *model.StagingArea, blockCandidate *externalapi.DomainHash) error {
|
||||
myWork := new(big.Int)
|
||||
maxWork := new(big.Int)
|
||||
var myScore uint64
|
||||
var spScore uint64
|
||||
/* find the selectedParent */
|
||||
blockParents, err := gh.dagTopologyManager.Parents(stagingArea, blockCandidate)
|
||||
var blueScore uint64 = 0
|
||||
var blueWork = new(big.Int)
|
||||
blueWork.SetUint64(0)
|
||||
var selectedParent *externalapi.DomainHash = nil
|
||||
var mergeSetBlues = make([]*externalapi.DomainHash, 0)
|
||||
var mergeSetReds = make([]*externalapi.DomainHash, 0)
|
||||
|
||||
parents, err := gh.dagTopologyManager.Parents(stagingArea, blockCandidate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var selectedParent = blockParents[0]
|
||||
for _, parent := range blockParents {
|
||||
blockData, err := gh.dataStore.Get(gh.dbAccess, stagingArea, parent)
|
||||
// If genesis:
|
||||
if len(parents) == 0 {
|
||||
blockGHOSTDAGData := model.NewBlockGHOSTDAGData(blueScore, blueWork, selectedParent, mergeSetBlues, mergeSetReds, nil)
|
||||
gh.dataStore.Stage(stagingArea, blockCandidate, blockGHOSTDAGData)
|
||||
return nil
|
||||
}
|
||||
|
||||
maxBlueWork := new(big.Int)
|
||||
maxBlueWork.SetUint64(0)
|
||||
maxBlueScore := uint64(0)
|
||||
selectedParent = parents[0]
|
||||
// Find the selected parent.
|
||||
for _, parent := range parents {
|
||||
parentBlockData, err := gh.dataStore.Get(gh.dbAccess, stagingArea, parent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
blockWork := blockData.BlueWork()
|
||||
blockScore := blockData.BlueScore()
|
||||
if blockWork.Cmp(maxWork) == 1 {
|
||||
parentBlueWork := parentBlockData.BlueWork()
|
||||
switch parentBlueWork.Cmp(maxBlueWork) {
|
||||
case 0:
|
||||
if isMoreHash(parent, selectedParent) {
|
||||
selectedParent = parent
|
||||
maxBlueWork = parentBlueWork
|
||||
maxBlueScore = parentBlockData.BlueScore()
|
||||
}
|
||||
case 1:
|
||||
selectedParent = parent
|
||||
maxWork = blockWork
|
||||
spScore = blockScore
|
||||
}
|
||||
if blockWork.Cmp(maxWork) == 0 && ismoreHash(parent, selectedParent) {
|
||||
selectedParent = parent
|
||||
maxWork = blockWork
|
||||
spScore = blockScore
|
||||
maxBlueWork = parentBlueWork
|
||||
maxBlueScore = parentBlockData.BlueScore()
|
||||
}
|
||||
}
|
||||
myWork.Set(maxWork)
|
||||
myScore = spScore
|
||||
|
||||
/* Goal: iterate blockCandidate's mergeSet and divide it to : blue, blues, reds. */
|
||||
var mergeSetBlues = make([]*externalapi.DomainHash, 0)
|
||||
var mergeSetReds = make([]*externalapi.DomainHash, 0)
|
||||
var blueSet = make([]*externalapi.DomainHash, 0)
|
||||
blueWork.Set(maxBlueWork)
|
||||
blueScore = maxBlueScore
|
||||
|
||||
blueSet := make([]*externalapi.DomainHash, 0)
|
||||
blueSet = append(blueSet, selectedParent)
|
||||
mergeSetBlues = append(mergeSetBlues, selectedParent)
|
||||
|
||||
mergeSetArr, err := gh.findMergeSet(stagingArea, blockParents, selectedParent)
|
||||
mergeSet, err := gh.findMergeSet(stagingArea, parents, selectedParent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = gh.sortByBlueWork(stagingArea, mergeSetArr)
|
||||
err = gh.sortByBlueWork(stagingArea, mergeSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -90,41 +96,36 @@ func (gh *ghostdagHelper) GHOSTDAG(stagingArea *model.StagingArea, blockCandidat
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, mergeSetBlock := range mergeSetArr {
|
||||
// Iterate on mergeSet and divide it to mergeSetBlues and mergeSetReds.
|
||||
for _, mergeSetBlock := range mergeSet {
|
||||
if mergeSetBlock.Equal(selectedParent) {
|
||||
if !contains(selectedParent, mergeSetBlues) {
|
||||
mergeSetBlues = append(mergeSetBlues, selectedParent)
|
||||
blueSet = append(blueSet, selectedParent)
|
||||
}
|
||||
continue
|
||||
}
|
||||
err := gh.divideBlueRed(stagingArea, selectedParent, mergeSetBlock, &mergeSetBlues, &mergeSetReds, &blueSet)
|
||||
err := gh.divideToBlueAndRed(stagingArea, mergeSetBlock, &mergeSetBlues, &mergeSetReds, &blueSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
myScore += uint64(len(mergeSetBlues))
|
||||
blueScore += uint64(len(mergeSetBlues))
|
||||
|
||||
// We add up all the *work*(not blueWork) that all our blues and selected parent did
|
||||
// Calculation of blue work
|
||||
for _, blue := range mergeSetBlues {
|
||||
header, err := gh.headerStore.BlockHeader(gh.dbAccess, stagingArea, blue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
myWork.Add(myWork, difficulty.CalcWork(header.Bits()))
|
||||
blueWork.Add(blueWork, difficulty.CalcWork(header.Bits()))
|
||||
}
|
||||
|
||||
e := model.NewBlockGHOSTDAGData(myScore, myWork, selectedParent, mergeSetBlues, mergeSetReds, nil)
|
||||
gh.dataStore.Stage(stagingArea, blockCandidate, e)
|
||||
blockGHOSTDAGData := model.NewBlockGHOSTDAGData(blueScore, blueWork, selectedParent, mergeSetBlues, mergeSetReds, nil)
|
||||
gh.dataStore.Stage(stagingArea, blockCandidate, blockGHOSTDAGData)
|
||||
return nil
|
||||
}
|
||||
|
||||
/* --------isMoreHash(w, selectedParent)----------------*/
|
||||
func ismoreHash(parent *externalapi.DomainHash, selectedParent *externalapi.DomainHash) bool {
|
||||
func isMoreHash(parent *externalapi.DomainHash, selectedParent *externalapi.DomainHash) bool {
|
||||
parentByteArray := parent.ByteArray()
|
||||
selectedParentByteArray := selectedParent.ByteArray()
|
||||
//Check if parentHash is more then selectedParentHash
|
||||
|
||||
for i := 0; i < len(parentByteArray); i++ {
|
||||
switch {
|
||||
case parentByteArray[i] < selectedParentByteArray[i]:
|
||||
@@ -136,46 +137,39 @@ func ismoreHash(parent *externalapi.DomainHash, selectedParent *externalapi.Doma
|
||||
return false
|
||||
}
|
||||
|
||||
/* 1. blue = selectedParent.blue + blues
|
||||
2. not connected to at most K blocks (from the blue group)
|
||||
3. for each block at blue , check if not destroy
|
||||
*/
|
||||
|
||||
/* ---------------divideBluesReds--------------------- */
|
||||
func (gh *ghostdagHelper) divideBlueRed(stagingArea *model.StagingArea,
|
||||
selectedParent *externalapi.DomainHash, desiredBlock *externalapi.DomainHash,
|
||||
func (gh *ghostdagHelper) divideToBlueAndRed(stagingArea *model.StagingArea, blockToCheck *externalapi.DomainHash,
|
||||
blues *[]*externalapi.DomainHash, reds *[]*externalapi.DomainHash, blueSet *[]*externalapi.DomainHash) error {
|
||||
|
||||
var k = int(gh.k)
|
||||
counter := 0
|
||||
|
||||
var suspectsBlues = make([]*externalapi.DomainHash, 0)
|
||||
anticoneBlocksCounter := 0
|
||||
anticoneBlues := make([]*externalapi.DomainHash, 0)
|
||||
isMergeBlue := true
|
||||
|
||||
//check that not-connected to at most k.
|
||||
for _, block := range *blueSet {
|
||||
isAnticone, err := gh.isAnticone(stagingArea, block, desiredBlock)
|
||||
for _, blueblock := range *blueSet {
|
||||
isAnticone, err := gh.isAnticone(stagingArea, blueblock, blockToCheck)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isAnticone {
|
||||
counter++
|
||||
suspectsBlues = append(suspectsBlues, block)
|
||||
anticoneBlocksCounter++
|
||||
anticoneBlues = append(anticoneBlues, blueblock)
|
||||
}
|
||||
if counter > k {
|
||||
if anticoneBlocksCounter > k {
|
||||
isMergeBlue = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isMergeBlue {
|
||||
if !contains(desiredBlock, *reds) {
|
||||
*reds = append(*reds, desiredBlock)
|
||||
if !contains(blockToCheck, *reds) {
|
||||
*reds = append(*reds, blockToCheck)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// check that the k-cluster of each blue is still valid.
|
||||
for _, blue := range suspectsBlues {
|
||||
isDestroyed, err := gh.checkIfDestroy(stagingArea, blue, blueSet)
|
||||
// check that the k-cluster of each anticone blue block is still valid.
|
||||
for _, blueBlock := range anticoneBlues {
|
||||
isDestroyed, err := gh.checkIfDestroy(stagingArea, blueBlock, blueSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -185,39 +179,37 @@ func (gh *ghostdagHelper) divideBlueRed(stagingArea *model.StagingArea,
|
||||
}
|
||||
}
|
||||
if !isMergeBlue {
|
||||
if !contains(desiredBlock, *reds) {
|
||||
*reds = append(*reds, desiredBlock)
|
||||
if !contains(blockToCheck, *reds) {
|
||||
*reds = append(*reds, blockToCheck)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !contains(desiredBlock, *blues) {
|
||||
*blues = append(*blues, desiredBlock)
|
||||
if !contains(blockToCheck, *blues) {
|
||||
*blues = append(*blues, blockToCheck)
|
||||
}
|
||||
if !contains(desiredBlock, *blueSet) {
|
||||
*blueSet = append(*blueSet, desiredBlock)
|
||||
if !contains(blockToCheck, *blueSet) {
|
||||
*blueSet = append(*blueSet, blockToCheck)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------isAnticone-------------------------- */
|
||||
func (gh *ghostdagHelper) isAnticone(stagingArea *model.StagingArea, blockA, blockB *externalapi.DomainHash) (bool, error) {
|
||||
isAAncestorOfAB, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, blockA, blockB)
|
||||
isBlockAAncestorOfBlockB, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, blockA, blockB)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
isBAncestorOfA, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, blockB, blockA)
|
||||
isBlockBAncestorOfBlockA, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, blockB, blockA)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return !isAAncestorOfAB && !isBAncestorOfA, nil
|
||||
|
||||
return !isBlockAAncestorOfBlockB && !isBlockBAncestorOfBlockA, nil
|
||||
}
|
||||
|
||||
/* ----------------validateKCluster------------------- */
|
||||
func (gh *ghostdagHelper) validateKCluster(stagingArea *model.StagingArea, chain *externalapi.DomainHash,
|
||||
checkedBlock *externalapi.DomainHash, counter *int, blueSet *[]*externalapi.DomainHash) (bool, error) {
|
||||
func (gh *ghostdagHelper) validateKCluster(stagingArea *model.StagingArea, chain *externalapi.DomainHash, checkedBlock *externalapi.DomainHash,
|
||||
counter *int, blueSet *[]*externalapi.DomainHash) (bool, error) {
|
||||
|
||||
var k = int(gh.k)
|
||||
k := int(gh.k)
|
||||
isAnticone, err := gh.isAnticone(stagingArea, chain, checkedBlock)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -236,6 +228,7 @@ func (gh *ghostdagHelper) validateKCluster(stagingArea *model.StagingArea, chain
|
||||
*counter++
|
||||
return true, nil
|
||||
}
|
||||
|
||||
isAncestorOf, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, checkedBlock, chain)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -251,27 +244,24 @@ func (gh *ghostdagHelper) validateKCluster(stagingArea *model.StagingArea, chain
|
||||
} else {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
/*----------------contains-------------------------- */
|
||||
func contains(item *externalapi.DomainHash, items []*externalapi.DomainHash) bool {
|
||||
for _, r := range items {
|
||||
if r.Equal(item) {
|
||||
func contains(desiredItem *externalapi.DomainHash, items []*externalapi.DomainHash) bool {
|
||||
for _, item := range items {
|
||||
if item.Equal(desiredItem) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/* ----------------checkIfDestroy------------------- */
|
||||
/* find number of not-connected in his blue*/
|
||||
// find number of not-connected in blueSet
|
||||
func (gh *ghostdagHelper) checkIfDestroy(stagingArea *model.StagingArea, blockBlue *externalapi.DomainHash,
|
||||
blueSet *[]*externalapi.DomainHash) (bool, error) {
|
||||
|
||||
// Goal: check that the K-cluster of each block in the blueSet is not destroyed when adding the block to the mergeSet.
|
||||
var k = int(gh.k)
|
||||
// check that the K-cluster of each block in the blueSet is not destroyed when adding the block to the mergeSet.
|
||||
k := int(gh.k)
|
||||
counter := 0
|
||||
for _, blue := range *blueSet {
|
||||
isAnticone, err := gh.isAnticone(stagingArea, blue, blockBlue)
|
||||
@@ -288,51 +278,45 @@ func (gh *ghostdagHelper) checkIfDestroy(stagingArea *model.StagingArea, blockBl
|
||||
return false, nil
|
||||
}
|
||||
|
||||
/* ----------------findMergeSet------------------- */
|
||||
func (gh *ghostdagHelper) findMergeSet(stagingArea *model.StagingArea, parents []*externalapi.DomainHash,
|
||||
selectedParent *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
|
||||
|
||||
allMergeSet := make([]*externalapi.DomainHash, 0)
|
||||
blockQueue := make([]*externalapi.DomainHash, 0)
|
||||
mergeSet := make([]*externalapi.DomainHash, 0)
|
||||
blocksQueue := make([]*externalapi.DomainHash, 0)
|
||||
for _, parent := range parents {
|
||||
if !contains(parent, blockQueue) {
|
||||
blockQueue = append(blockQueue, parent)
|
||||
if !contains(parent, blocksQueue) {
|
||||
blocksQueue = append(blocksQueue, parent)
|
||||
}
|
||||
|
||||
}
|
||||
for len(blockQueue) > 0 {
|
||||
block := blockQueue[0]
|
||||
blockQueue = blockQueue[1:]
|
||||
for len(blocksQueue) > 0 {
|
||||
block := blocksQueue[0]
|
||||
blocksQueue = blocksQueue[1:]
|
||||
if selectedParent.Equal(block) {
|
||||
if !contains(block, allMergeSet) {
|
||||
allMergeSet = append(allMergeSet, block)
|
||||
if !contains(block, mergeSet) {
|
||||
mergeSet = append(mergeSet, block)
|
||||
}
|
||||
continue
|
||||
}
|
||||
isancestorOf, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, block, selectedParent)
|
||||
isAncestorOf, err := gh.dagTopologyManager.IsAncestorOf(stagingArea, block, selectedParent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isancestorOf {
|
||||
if isAncestorOf {
|
||||
continue
|
||||
}
|
||||
if !contains(block, allMergeSet) {
|
||||
allMergeSet = append(allMergeSet, block)
|
||||
if !contains(block, mergeSet) {
|
||||
mergeSet = append(mergeSet, block)
|
||||
}
|
||||
err = gh.insertParent(stagingArea, block, &blockQueue)
|
||||
err = gh.insertParent(stagingArea, block, &blocksQueue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
return allMergeSet, nil
|
||||
return mergeSet, nil
|
||||
}
|
||||
|
||||
/* ----------------insertParent------------------- */
|
||||
/* Insert all parents to the queue*/
|
||||
func (gh *ghostdagHelper) insertParent(stagingArea *model.StagingArea, child *externalapi.DomainHash,
|
||||
queue *[]*externalapi.DomainHash) error {
|
||||
|
||||
// Insert all parents to the queue
|
||||
func (gh *ghostdagHelper) insertParent(stagingArea *model.StagingArea, child *externalapi.DomainHash, queue *[]*externalapi.DomainHash) error {
|
||||
parents, err := gh.dagTopologyManager.Parents(stagingArea, child)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -346,8 +330,9 @@ func (gh *ghostdagHelper) insertParent(stagingArea *model.StagingArea, child *ex
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ----------------findBlueSet------------------- */
|
||||
func (gh *ghostdagHelper) findBlueSet(stagingArea *model.StagingArea, blueSet *[]*externalapi.DomainHash, selectedParent *externalapi.DomainHash) error {
|
||||
func (gh *ghostdagHelper) findBlueSet(stagingArea *model.StagingArea, blueSet *[]*externalapi.DomainHash,
|
||||
selectedParent *externalapi.DomainHash) error {
|
||||
|
||||
for selectedParent != nil {
|
||||
if !contains(selectedParent, *blueSet) {
|
||||
*blueSet = append(*blueSet, selectedParent)
|
||||
@@ -368,44 +353,41 @@ func (gh *ghostdagHelper) findBlueSet(stagingArea *model.StagingArea, blueSet *[
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ----------------sortByBlueScore------------------- */
|
||||
func (gh *ghostdagHelper) sortByBlueWork(stagingArea *model.StagingArea, arr []*externalapi.DomainHash) error {
|
||||
|
||||
var err error = nil
|
||||
var err error
|
||||
sort.Slice(arr, func(i, j int) bool {
|
||||
|
||||
blockLeft, error := gh.dataStore.Get(gh.dbAccess, stagingArea, arr[i])
|
||||
if error != nil {
|
||||
err = error
|
||||
blockLeft, err := gh.dataStore.Get(gh.dbAccess, stagingArea, arr[i])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
blockRight, error := gh.dataStore.Get(gh.dbAccess, stagingArea, arr[j])
|
||||
if error != nil {
|
||||
err = error
|
||||
blockRight, err := gh.dataStore.Get(gh.dbAccess, stagingArea, arr[j])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if blockLeft.BlueWork().Cmp(blockRight.BlueWork()) == -1 {
|
||||
return true
|
||||
}
|
||||
if blockLeft.BlueWork().Cmp(blockRight.BlueWork()) == 0 {
|
||||
return ismoreHash(arr[j], arr[i])
|
||||
return isMoreHash(arr[j], arr[i])
|
||||
}
|
||||
return false
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
/* --------------------------------------------- */
|
||||
|
||||
func (gh *ghostdagHelper) BlockData(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (*model.BlockGHOSTDAGData, error) {
|
||||
return gh.dataStore.Get(gh.dbAccess, stagingArea, blockHash)
|
||||
}
|
||||
func (gh *ghostdagHelper) ChooseSelectedParent(stagingArea *model.StagingArea, blockHashes ...*externalapi.DomainHash) (*externalapi.DomainHash, error) {
|
||||
|
||||
func (gh *ghostdagHelper) ChooseSelectedParent(*model.StagingArea, ...*externalapi.DomainHash) (*externalapi.DomainHash, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (gh *ghostdagHelper) Less(blockHashA *externalapi.DomainHash, ghostdagDataA *model.BlockGHOSTDAGData, blockHashB *externalapi.DomainHash, ghostdagDataB *model.BlockGHOSTDAGData) bool {
|
||||
func (gh *ghostdagHelper) Less(*externalapi.DomainHash, *model.BlockGHOSTDAGData, *externalapi.DomainHash, *model.BlockGHOSTDAGData) bool {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (gh *ghostdagHelper) GetSortedMergeSet(*model.StagingArea, *externalapi.DomainHash) ([]*externalapi.DomainHash, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user