Skip to content

Commit

Permalink
Merge pull request #251 from lochjin/main
Browse files Browse the repository at this point in the history
GHOSTDAG test
  • Loading branch information
dindinw authored Oct 25, 2022
2 parents 7d05612 + 13864d4 commit df00867
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
30 changes: 28 additions & 2 deletions meerdag/ghostdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,36 @@ func (gd *GhostDAG) GetBlueSet() *IdSet {
return blueSet
}

func (gd *GhostDAG) UpdateOrders() {
// It is only used to simulate the tags of all sequences, and the algorithm itself is very inefficient
func (gd *GhostDAG) UpdateOrders() error {
if gd.virtualBlock.IsOrdered() {
return
return nil
}
mainChains := []IBlock{}
for cur := IBlock(gd.virtualBlock); cur != nil; cur = gd.bd.getBlockById(cur.GetMainParent()) {
mainChains = append(mainChains, cur)
}
curOrder := uint(0)
for i := len(mainChains) - 1; i >= 0; i-- {
sms, err := gd.algorithm.GetSortedMergeSet(nil, mainChains[i].GetHash())
if err != nil {
return err
}
for _, v := range sms {
block := gd.bd.getBlock(v)
if block.GetID() == mainChains[i].GetMainParent() {
continue
}
block.SetOrder(curOrder)
gd.bd.commitOrder[curOrder] = block.GetID()
curOrder++
}
mainChains[i].SetOrder(curOrder)
gd.bd.commitOrder[curOrder] = mainChains[i].GetID()
curOrder++

}
return gd.bd.commit()
}

//---------------
Expand Down
102 changes: 102 additions & 0 deletions meerdag/ghostdag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import (
"testing"
)

func TestGhostDAGBlueSetFig1(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig1-blocks")
if ibd == nil {
t.FailNow()
}
ph := ibd.(*GhostDAG)
//
blueSet := ph.GetBlueSet()
fmt.Println("Fig1 blue set:")
printBlockSetTag(blueSet)
if !processResult(blueSet, changeToIDList(testData.GD_BlueSetFig1.Output)) {
t.FailNow()
}
}

func TestGhostDAGBlueSetFig2(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig2-blocks")
if ibd == nil {
Expand All @@ -15,4 +30,91 @@ func TestGhostDAGBlueSetFig2(t *testing.T) {
blueSet := ph.GetBlueSet()
fmt.Println("Fig2 blue set:")
printBlockSetTag(blueSet)
if !processResult(blueSet, changeToIDList(testData.GD_BlueSetFig2.Output)) {
t.FailNow()
}
}

func TestGhostDAGBlueSetFig4(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig4-blocks")
if ibd == nil {
t.FailNow()
}
ph := ibd.(*GhostDAG)
//
blueSet := ph.GetBlueSet()
fmt.Println("Fig4 blue set:")
printBlockSetTag(blueSet)
if !processResult(blueSet, changeToIDList(testData.GD_BlueSetFig4.Output)) {
t.FailNow()
}
}

func TestGhostDAGOrderFig1(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig1-blocks")
if ibd == nil {
t.FailNow()
}
ph := ibd.(*GhostDAG)
order := []uint{}
var i uint
err := ph.UpdateOrders()
if err != nil {
t.Fatal(err)
}
for i = 0; i < bd.GetBlockTotal(); i++ {
order = append(order, bd.getBlockByOrder(uint(i)).GetID())
}
fmt.Printf("The Fig.1 Order: ")
printBlockChainTag(order)

if !processResult(order, changeToIDList(testData.GD_OrderFig1.Output)) {
t.FailNow()
}
}

func TestGhostDAGOrderFig2(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig2-blocks")
if ibd == nil {
t.FailNow()
}
ph := ibd.(*GhostDAG)
order := []uint{}
var i uint
err := ph.UpdateOrders()
if err != nil {
t.Fatal(err)
}
for i = 0; i < bd.GetBlockTotal(); i++ {
order = append(order, bd.getBlockByOrder(uint(i)).GetID())
}
fmt.Printf("The Fig.2 Order: ")
printBlockChainTag(order)

if !processResult(order, changeToIDList(testData.GD_OrderFig2.Output)) {
t.FailNow()
}
}

func TestGhostDAGOrderFig4(t *testing.T) {
ibd := InitBlockDAG(GHOSTDAG, "PH_fig4-blocks")
if ibd == nil {
t.FailNow()
}
ph := ibd.(*GhostDAG)
order := []uint{}
var i uint
err := ph.UpdateOrders()
if err != nil {
t.Fatal(err)
}
for i = 0; i < bd.GetBlockTotal(); i++ {
order = append(order, bd.getBlockByOrder(uint(i)).GetID())
}
fmt.Printf("The Fig.4 Order: ")
printBlockChainTag(order)

if !processResult(order, changeToIDList(testData.GD_OrderFig4.Output)) {
t.FailNow()
}
}
6 changes: 6 additions & 0 deletions meerdag/meerdag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type TestData struct {
PH_MPConcurrency TestInOutData2
PH_BConcurrency TestInOutData2
PH_MainChainTip []TestInOutData3
GD_BlueSetFig1 TestInOutData
GD_BlueSetFig2 TestInOutData
GD_BlueSetFig4 TestInOutData
GD_OrderFig1 TestInOutData
GD_OrderFig2 TestInOutData
GD_OrderFig4 TestInOutData
}

// Load some data that phantom test need,it can use to build the dag ;This is the
Expand Down
20 changes: 19 additions & 1 deletion meerdag/testData.json
Original file line number Diff line number Diff line change
Expand Up @@ -602,5 +602,23 @@
],
"out": false
}
]
],
"GD_BlueSetFig1":{
"out":["I","D","E","C","Genesis","M","K","H"]
},
"GD_BlueSetFig2":{
"out":["J","F","D","B","C","A","I","G"]
},
"GD_BlueSetFig4":{
"out":["F","D","B","C","R","O","P","I","Genesis","M","K","J"]
},
"GD_OrderFig1":{
"out":["Genesis","D","E","C","H","B","I","K","F","M","L","J"]
},
"GD_OrderFig2":{
"out":["A","B","C","F","D","I","E","H","G","K","J"]
},
"GD_OrderFig4":{
"out":["Genesis","B","C","F","D","J","E","I","K","O","H","N","P","R","L","Q","S","T","U","M"]
}
}

0 comments on commit df00867

Please sign in to comment.