Skip to content

Commit

Permalink
Merge pull request #292 from lochjin/main
Browse files Browse the repository at this point in the history
P2P:optimize dag sync
  • Loading branch information
dindinw authored Nov 19, 2022
2 parents fadf255 + 9dff158 commit 6f52821
Showing 1 changed file with 42 additions and 52 deletions.
94 changes: 42 additions & 52 deletions meerdag/dagsync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meerdag

import (
"container/list"
"github.com/Qitmeer/qng/common/hash"
"sync"
)
Expand Down Expand Up @@ -110,70 +111,59 @@ func (ds *DAGSync) GetMainLocator(point *hash.Hash) []*hash.Hash {
endBlock = ds.bd.getGenesis()
}
startBlock := ds.bd.getMainChainTip()
dist := startBlock.GetHeight() - endBlock.GetHeight()
locator := []*hash.Hash{}
locator := list.New()
cur := startBlock
if dist <= MaxMainLocatorNum {
for cur.GetID() != endBlock.GetID() {
if cur.GetID() == 0 {
break
}
locator = append(locator, cur.GetHash())
if cur.GetMainParent() == MaxId {
break
}
cur = ds.bd.getBlockById(cur.GetMainParent())
if cur == nil {
break
}
const DefaultMainLocatorNum = 10
for i := 0; i < DefaultMainLocatorNum; i++ {
if cur.GetID() == 0 ||
cur.GetMainParent() == MaxId ||
cur.GetID() <= endBlock.GetID() {
break
}
locator.PushFront(cur.GetHash())
cur = ds.bd.getBlockById(cur.GetMainParent())
if cur == nil {
break
}
} else {
const DefaultMainLocatorNum = 10
deep := uint(1)
for len(locator) < MaxMainLocatorNum {
if cur.GetID() == 0 {
}
if cur.GetID() > endBlock.GetID() {
halfStart := cur.GetID()
halfEnd := endBlock.GetID()
hlocator := []*hash.Hash{}
for locator.Len()+len(hlocator)+1 < MaxMainLocatorNum {
//for {
nextID := (halfStart - halfEnd) / 2
if nextID <= 0 {
break
}
if len(locator) < DefaultMainLocatorNum {
locator = append(locator, cur.GetHash())
} else {
height := uint(0)
if startBlock.GetHeight()-DefaultMainLocatorNum >= deep {
height = startBlock.GetHeight() - DefaultMainLocatorNum - deep
}
if cur.GetHeight() <= height {
locator = append(locator, cur.GetHash())
deep *= 2
}
}

if cur.GetMainParent() == MaxId {
nextID += halfEnd
if nextID == halfStart ||
nextID == halfEnd {
break
}

next := ds.bd.getBlockById(cur.GetMainParent())
if next.GetID() == endBlock.GetID() {
break
if !ds.bd.isOnMainChain(nextID) {
halfEnd++
continue
}
cur = next
if cur == nil {
break
ib := ds.bd.getBlockById(nextID)
if ib == nil {
halfEnd++
continue
}
hlocator = append(hlocator, ib.GetHash())
halfEnd = nextID
}
}
locator = append(locator, endBlock.GetHash())
if len(locator) >= 2 {
tempL := locator
locator = []*hash.Hash{}
for i := len(tempL) - 1; i >= 0; i-- {
if len(locator) >= MaxMainLocatorNum {
break
if len(hlocator) > 0 {
for i := len(hlocator) - 1; i >= 0; i-- {
locator.PushFront(hlocator[i])
}
locator = append(locator, tempL[i])
}
}

return locator
result := []*hash.Hash{endBlock.GetHash()}
for i := locator.Front(); i != nil; i = i.Next() {
result = append(result, i.Value.(*hash.Hash))
}
return result
}

func (ds *DAGSync) getBlockChainFromMain(point IBlock, maxHashes uint) []*hash.Hash {
Expand Down

0 comments on commit 6f52821

Please sign in to comment.