Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accelerate the efficiency of block synchronization #606

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consensus/model/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// and rpc server.
type Notify interface {
AnnounceNewTransactions(newTxs []*types.TxDesc, filters []peer.ID)
RelayInventory(data interface{}, filters []peer.ID)
RelayInventory(block *types.SerializedBlock, flags uint32, source *peer.ID)
BroadcastMessage(data interface{})
TransactionConfirmed(tx *types.Tx)
AddRebroadcastInventory(newTxs []*types.TxDesc)
Expand Down
3 changes: 3 additions & 0 deletions core/blockchain/behaviorflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const (
// Add block from RPC interface
BFRPCAdd

// Add block from broadcast interface
BFBroadcast

// BFNone is a convenience value to specifically indicate no flags.
BFNone BehaviorFlags = 0
)
Expand Down
4 changes: 3 additions & 1 deletion core/blockchain/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/core/event"
"github.com/Qitmeer/qng/core/types"
"github.com/libp2p/go-libp2p/core/peer"
"time"
)

Expand Down Expand Up @@ -69,7 +70,8 @@ type BlockAcceptedNotifyData struct {
Block *types.SerializedBlock
Height uint64

Flags BehaviorFlags
Flags BehaviorFlags
Source *peer.ID
}

// ReorganizationNotifyData is the structure for data indicating information
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain/orphan.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (b *BlockChain) processOrphans(flags BehaviorFlags) error {
continue
}
b.RemoveOrphanBlock(cur)
b.maybeAcceptBlock(cur.block, flags)
b.maybeAcceptBlock(cur.block, flags, nil)
}
return nil
}
Expand Down
17 changes: 10 additions & 7 deletions core/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package blockchain
import (
"container/list"
"fmt"
"github.com/libp2p/go-libp2p/core/peer"
"time"

"github.com/Qitmeer/qng/common/hash"
Expand Down Expand Up @@ -36,12 +37,12 @@ import (
//
// This function is safe for concurrent access.
// return IsOrphan,error
func (b *BlockChain) ProcessBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, bool, error) {
func (b *BlockChain) ProcessBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, bool, error) {
if b.IsShutdown() {
return nil, false, fmt.Errorf("block chain is shutdown")
}
block.Reset()
msg := processMsg{block: block, flags: flags, result: make(chan *processResult)}
msg := processMsg{block: block, flags: flags, result: make(chan *processResult), source: source}
b.msgChan <- &msg
result := <-msg.result
return result.block, result.isOrphan, result.err
Expand All @@ -54,7 +55,7 @@ out:
select {
case msg := <-b.msgChan:
start := time.Now()
ib, isOrphan, err := b.processBlock(msg.block, msg.flags)
ib, isOrphan, err := b.processBlock(msg.block, msg.flags, msg.source)
blockProcessTimer.Update(time.Since(start))
msg.result <- &processResult{isOrphan: isOrphan, err: err, block: ib}
case <-b.quit:
Expand All @@ -75,14 +76,14 @@ cleanup:
log.Trace("BlockChain handler done")
}

func (b *BlockChain) processBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, bool, error) {
func (b *BlockChain) processBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, bool, error) {
isorphan, err := b.preProcessBlock(block, flags)
if err != nil || isorphan {
return nil, isorphan, err
}
// The block has passed all context independent checks and appears sane
// enough to potentially accept it into the block chain.
ib, err := b.maybeAcceptBlock(block, flags)
ib, err := b.maybeAcceptBlock(block, flags, source)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func (b *BlockChain) preProcessBlock(block *types.SerializedBlock, flags Behavio
// their documentation for how the flags modify their behavior.
//
// This function MUST be called with the chain state lock held (for writes).
func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, error) {
func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, error) {
if onEnd := l.LogAndMeasureExecutionTime(log, "BlockChain.maybeAcceptBlock"); onEnd != nil {
defer onEnd()
}
Expand Down Expand Up @@ -293,6 +294,7 @@ func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags Behavi
Block: block,
Flags: flags,
Height: uint64(ib.GetHeight()),
Source: source,
})
if b.Acct != nil {
err = b.Acct.Commit()
Expand All @@ -304,7 +306,7 @@ func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags Behavi
}

func (b *BlockChain) FastAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, error) {
return b.maybeAcceptBlock(block, flags)
return b.maybeAcceptBlock(block, flags, nil)
}

// connectBestChain handles connecting the passed block to the chain while
Expand Down Expand Up @@ -788,6 +790,7 @@ type processMsg struct {
block *types.SerializedBlock
flags BehaviorFlags
result chan *processResult
source *peer.ID
}

type processResult struct {
Expand Down
5 changes: 4 additions & 1 deletion core/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ const (
// Support continue block sync for DAG search
ConSyncDAGProtocolVersion uint32 = 44

// Support continue block sync for DAG search
BroadcastblockProtocolVersion uint32 = 45

// ProtocolVersion is the latest protocol version this package supports.
ProtocolVersion uint32 = ConSyncDAGProtocolVersion
ProtocolVersion uint32 = BroadcastblockProtocolVersion
)

// Network represents which qitmeer network a message belongs to.
Expand Down
21 changes: 21 additions & 0 deletions meerdag/dagsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ func (ds *DAGSync) getBlockChainFromMain(point IBlock, maxHashes uint) []*hash.H
break
}
}
rlen := len(result)
if uint(rlen) < maxHashes &&
rlen > 0 &&
result[rlen-1].IsEqual(mainTip.GetHash()) {
pdb, ok := ds.bd.GetInstance().(*Phantom)
if ok {
if !pdb.GetDiffAnticone().IsEmpty() {
da := pdb.GetDiffAnticone().SortList(false)
for _, id := range da {
block := ds.bd.getBlockById(id)
if block == nil {
continue
}
result = append(result, block.GetHash())
if uint(len(result)) >= maxHashes {
break
}
}
}
}
}
return result
}

Expand Down
Loading
Loading