Skip to content

Commit 20d6eca

Browse files
authored
Merge pull request #606 from lochjin/dev1.2
Accelerate the efficiency of block synchronization
2 parents caf3d29 + 16039cd commit 20d6eca

File tree

15 files changed

+1340
-957
lines changed

15 files changed

+1340
-957
lines changed

consensus/model/notify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// and rpc server.
1414
type Notify interface {
1515
AnnounceNewTransactions(newTxs []*types.TxDesc, filters []peer.ID)
16-
RelayInventory(data interface{}, filters []peer.ID)
16+
RelayInventory(block *types.SerializedBlock, flags uint32, source *peer.ID)
1717
BroadcastMessage(data interface{})
1818
TransactionConfirmed(tx *types.Tx)
1919
AddRebroadcastInventory(newTxs []*types.TxDesc)

core/blockchain/behaviorflags.go

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const (
2121
// Add block from RPC interface
2222
BFRPCAdd
2323

24+
// Add block from broadcast interface
25+
BFBroadcast
26+
2427
// BFNone is a convenience value to specifically indicate no flags.
2528
BFNone BehaviorFlags = 0
2629
)

core/blockchain/notifications.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Qitmeer/qng/common/hash"
1212
"github.com/Qitmeer/qng/core/event"
1313
"github.com/Qitmeer/qng/core/types"
14+
"github.com/libp2p/go-libp2p/core/peer"
1415
"time"
1516
)
1617

@@ -69,7 +70,8 @@ type BlockAcceptedNotifyData struct {
6970
Block *types.SerializedBlock
7071
Height uint64
7172

72-
Flags BehaviorFlags
73+
Flags BehaviorFlags
74+
Source *peer.ID
7375
}
7476

7577
// ReorganizationNotifyData is the structure for data indicating information

core/blockchain/orphan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (b *BlockChain) processOrphans(flags BehaviorFlags) error {
258258
continue
259259
}
260260
b.RemoveOrphanBlock(cur)
261-
b.maybeAcceptBlock(cur.block, flags)
261+
b.maybeAcceptBlock(cur.block, flags, nil)
262262
}
263263
return nil
264264
}

core/blockchain/process.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package blockchain
88
import (
99
"container/list"
1010
"fmt"
11+
"github.com/libp2p/go-libp2p/core/peer"
1112
"time"
1213

1314
"github.com/Qitmeer/qng/common/hash"
@@ -36,12 +37,12 @@ import (
3637
//
3738
// This function is safe for concurrent access.
3839
// return IsOrphan,error
39-
func (b *BlockChain) ProcessBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, bool, error) {
40+
func (b *BlockChain) ProcessBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, bool, error) {
4041
if b.IsShutdown() {
4142
return nil, false, fmt.Errorf("block chain is shutdown")
4243
}
4344
block.Reset()
44-
msg := processMsg{block: block, flags: flags, result: make(chan *processResult)}
45+
msg := processMsg{block: block, flags: flags, result: make(chan *processResult), source: source}
4546
b.msgChan <- &msg
4647
result := <-msg.result
4748
return result.block, result.isOrphan, result.err
@@ -54,7 +55,7 @@ out:
5455
select {
5556
case msg := <-b.msgChan:
5657
start := time.Now()
57-
ib, isOrphan, err := b.processBlock(msg.block, msg.flags)
58+
ib, isOrphan, err := b.processBlock(msg.block, msg.flags, msg.source)
5859
blockProcessTimer.Update(time.Since(start))
5960
msg.result <- &processResult{isOrphan: isOrphan, err: err, block: ib}
6061
case <-b.quit:
@@ -75,14 +76,14 @@ cleanup:
7576
log.Trace("BlockChain handler done")
7677
}
7778

78-
func (b *BlockChain) processBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, bool, error) {
79+
func (b *BlockChain) processBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, bool, error) {
7980
isorphan, err := b.preProcessBlock(block, flags)
8081
if err != nil || isorphan {
8182
return nil, isorphan, err
8283
}
8384
// The block has passed all context independent checks and appears sane
8485
// enough to potentially accept it into the block chain.
85-
ib, err := b.maybeAcceptBlock(block, flags)
86+
ib, err := b.maybeAcceptBlock(block, flags, source)
8687
if err != nil {
8788
return nil, false, err
8889
}
@@ -193,7 +194,7 @@ func (b *BlockChain) preProcessBlock(block *types.SerializedBlock, flags Behavio
193194
// their documentation for how the flags modify their behavior.
194195
//
195196
// This function MUST be called with the chain state lock held (for writes).
196-
func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, error) {
197+
func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags, source *peer.ID) (meerdag.IBlock, error) {
197198
if onEnd := l.LogAndMeasureExecutionTime(log, "BlockChain.maybeAcceptBlock"); onEnd != nil {
198199
defer onEnd()
199200
}
@@ -293,6 +294,7 @@ func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags Behavi
293294
Block: block,
294295
Flags: flags,
295296
Height: uint64(ib.GetHeight()),
297+
Source: source,
296298
})
297299
if b.Acct != nil {
298300
err = b.Acct.Commit()
@@ -304,7 +306,7 @@ func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags Behavi
304306
}
305307

306308
func (b *BlockChain) FastAcceptBlock(block *types.SerializedBlock, flags BehaviorFlags) (meerdag.IBlock, error) {
307-
return b.maybeAcceptBlock(block, flags)
309+
return b.maybeAcceptBlock(block, flags, nil)
308310
}
309311

310312
// connectBestChain handles connecting the passed block to the chain while
@@ -788,6 +790,7 @@ type processMsg struct {
788790
block *types.SerializedBlock
789791
flags BehaviorFlags
790792
result chan *processResult
793+
source *peer.ID
791794
}
792795

793796
type processResult struct {

core/protocol/protocol.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ const (
2121
// Support continue block sync for DAG search
2222
ConSyncDAGProtocolVersion uint32 = 44
2323

24+
// Support continue block sync for DAG search
25+
BroadcastblockProtocolVersion uint32 = 45
26+
2427
// ProtocolVersion is the latest protocol version this package supports.
25-
ProtocolVersion uint32 = ConSyncDAGProtocolVersion
28+
ProtocolVersion uint32 = BroadcastblockProtocolVersion
2629
)
2730

2831
// Network represents which qitmeer network a message belongs to.

meerdag/dagsync.go

+21
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ func (ds *DAGSync) getBlockChainFromMain(point IBlock, maxHashes uint) []*hash.H
184184
break
185185
}
186186
}
187+
rlen := len(result)
188+
if uint(rlen) < maxHashes &&
189+
rlen > 0 &&
190+
result[rlen-1].IsEqual(mainTip.GetHash()) {
191+
pdb, ok := ds.bd.GetInstance().(*Phantom)
192+
if ok {
193+
if !pdb.GetDiffAnticone().IsEmpty() {
194+
da := pdb.GetDiffAnticone().SortList(false)
195+
for _, id := range da {
196+
block := ds.bd.getBlockById(id)
197+
if block == nil {
198+
continue
199+
}
200+
result = append(result, block.GetHash())
201+
if uint(len(result)) >= maxHashes {
202+
break
203+
}
204+
}
205+
}
206+
}
207+
}
187208
return result
188209
}
189210

0 commit comments

Comments
 (0)