From a77374ec26a87a74af970690cca7553e16ead6ff Mon Sep 17 00:00:00 2001 From: Jin Date: Sun, 22 Dec 2024 08:48:14 +0800 Subject: [PATCH 1/5] fix:cannot found when no more than one peer --- p2p/synch/peersync.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/p2p/synch/peersync.go b/p2p/synch/peersync.go index 3f1ed704..1182ca7c 100644 --- a/p2p/synch/peersync.go +++ b/p2p/synch/peersync.go @@ -399,12 +399,6 @@ func (ps *PeerSync) getBestPeer(snap bool, exclude map[peer.ID]struct{}) *peers. var bestPeer *peers.Peer equalPeers := []*peers.Peer{} for _, sp := range ps.sy.peers.CanSyncPeers() { - if len(exclude) > 0 { - _, ok := exclude[sp.GetID()] - if ok { - continue - } - } if snap { if !isValidSnapPeer(sp) { continue @@ -445,6 +439,21 @@ func (ps *PeerSync) getBestPeer(snap bool, exclude map[peer.ID]struct{}) *peers. if len(equalPeers) == 1 { return equalPeers[0] } + if len(exclude) > 0 { + filter := []*peers.Peer{} + for _, sp := range equalPeers { + _, ok := exclude[sp.GetID()] + if ok { + continue + } + filter = append(filter, sp) + } + if len(filter) == 1 { + return filter[0] + } else if len(filter) > 1 { + equalPeers = filter + } + } index := int(rand.Int63n(int64(len(equalPeers)))) if index >= len(equalPeers) { From d7e21b9a6f883ede30962b1c6a2c24e92201e91c Mon Sep 17 00:00:00 2001 From: Jin Date: Sun, 22 Dec 2024 23:00:36 +0800 Subject: [PATCH 2/5] fix:meerchain supports being interrupted during init --- meerevm/meer/meerchain.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/meerevm/meer/meerchain.go b/meerevm/meer/meerchain.go index 1140af89..c0ea1e78 100644 --- a/meerevm/meer/meerchain.go +++ b/meerevm/meer/meerchain.go @@ -424,6 +424,9 @@ func (b *MeerChain) prepareEnvironment(state model.BlockState) (*types.Header, e list := []model.BlockState{state} startState := b.consensus.BlockChain().GetBlockState(state.GetOrder() - 1) for startState != nil && startState.GetEVMNumber() >= curBlockHeader.Number.Uint64() { + if system.InterruptRequested(b.consensus.Interrupt()) { + return nil, getError("shutdown interrupt") + } if startState.GetEVMNumber() == curBlockHeader.Number.Uint64() && startState.GetEVMHash() == curBlockHeader.Hash() { curBlockState = startState @@ -440,6 +443,9 @@ func (b *MeerChain) prepareEnvironment(state model.BlockState) (*types.Header, e } log.Info("Find cur block state", "state.order", curBlockState.GetOrder(), "evm.Number", curBlockState.GetEVMNumber()) for i := len(list) - 1; i >= 0; i-- { + if system.InterruptRequested(b.consensus.Interrupt()) { + return nil, getError("shutdown interrupt") + } if list[i].GetStatus().KnownInvalid() { continue } From 9e6065a54656d561bc0815870789d7aaca7a8910 Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 23 Dec 2024 09:41:01 +0800 Subject: [PATCH 3/5] fix:meerevm connection deadlock --- p2p/synch/meerevm.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/p2p/synch/meerevm.go b/p2p/synch/meerevm.go index c6994ba9..c984229b 100644 --- a/p2p/synch/meerevm.go +++ b/p2p/synch/meerevm.go @@ -55,10 +55,14 @@ func (s *Sync) establishMeerConnection(pe *peers.Peer) error { return err } pe.SetMeerConn(true) - defer pe.SetMeerConn(false) - - _, err = s.p2p.MeerServer().Connect(qc, dest) + go func() { + _, err := s.p2p.MeerServer().Connect(qc, dest) + if err != nil { + log.Error(err.Error()) + } + pe.SetMeerConn(false) + }() return err } From 8294c10985bcc5bba324d3157082db48231a5b82 Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 23 Dec 2024 09:52:19 +0800 Subject: [PATCH 4/5] fix:meerevm continue snap-sync when reboot --- core/blockchain/blockchain.go | 14 ++++++++------ p2p/synch/peersync.go | 2 ++ p2p/synch/snapstatus.go | 2 +- p2p/synch/snapsync.go | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/blockchain/blockchain.go b/core/blockchain/blockchain.go index 0eb7c451..bf590848 100644 --- a/core/blockchain/blockchain.go +++ b/core/blockchain/blockchain.go @@ -357,13 +357,15 @@ func (b *BlockChain) Start() error { b.wg.Add(1) go b.handler() - // prepare evm env - mainTip := b.bd.GetMainChainTip() - evmHead, err := b.meerChain.PrepareEnvironment(mainTip.GetState()) - if err != nil { - return err + if !b.IsSnapSyncing() { + // prepare evm env + mainTip := b.bd.GetMainChainTip() + evmHead, err := b.meerChain.PrepareEnvironment(mainTip.GetState()) + if err != nil { + return err + } + log.Info("prepare evm environment", "mainTipOrder", mainTip.GetOrder(), "mainTipHash", mainTip.GetHash().String(), "hash", evmHead.Hash().String(), "number", evmHead.Number.Uint64(), "root", evmHead.Root.String()) } - log.Info("prepare evm environment", "mainTipOrder", mainTip.GetOrder(), "mainTipHash", mainTip.GetHash().String(), "hash", evmHead.Hash().String(), "number", evmHead.Number.Uint64(), "root", evmHead.Root.String()) return b.DB().Snapshot() } diff --git a/p2p/synch/peersync.go b/p2p/synch/peersync.go index 1182ca7c..0bf8eeff 100644 --- a/p2p/synch/peersync.go +++ b/p2p/synch/peersync.go @@ -80,6 +80,7 @@ func (ps *PeerSync) Stop() error { ps.wg.Wait() log.Info("P2P PeerSync Stoped") + ps.saveSnapSync() return nil } @@ -765,5 +766,6 @@ func NewPeerSync(sy *Sync) *PeerSync { interrupt: make(chan struct{}), lastBlockID: meerdag.MaxId, } + peerSync.loadSnapSync() return peerSync } diff --git a/p2p/synch/snapstatus.go b/p2p/synch/snapstatus.go index e7e40703..0007f5f6 100644 --- a/p2p/synch/snapstatus.go +++ b/p2p/synch/snapstatus.go @@ -255,7 +255,7 @@ func (ss *SnapStatus) Decode(data []byte) error { if err != nil { return err } - ss.peid, err = peer.Decode(string(peid)) + ss.peid, err = peer.IDFromBytes(peid) if err != nil { return err } diff --git a/p2p/synch/snapsync.go b/p2p/synch/snapsync.go index 03e17de7..51a82d1d 100644 --- a/p2p/synch/snapsync.go +++ b/p2p/synch/snapsync.go @@ -42,6 +42,7 @@ func (ps *PeerSync) loadSnapSync() { } snapStatus.syncPoint = ps.Chain().BlockDAG().GetBlockById(uint(snapStatus.PointID)) if snapStatus.syncPoint == nil { + log.Error("Can't find snap status point", "id", snapStatus.PointID) return } ps.snapStatus = snapStatus @@ -51,7 +52,6 @@ func (ps *PeerSync) loadSnapSync() { log.Info("End snap-sync", "err", err.Error()) return } - } func (ps *PeerSync) saveSnapSync() { From 5b978004c449aa28060f32514b5d3f3d4c5ab5d1 Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 23 Dec 2024 10:02:27 +0800 Subject: [PATCH 5/5] fix:meerevm stream close error --- p2p/synch/meerevm.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/p2p/synch/meerevm.go b/p2p/synch/meerevm.go index c984229b..5472cc20 100644 --- a/p2p/synch/meerevm.go +++ b/p2p/synch/meerevm.go @@ -47,7 +47,6 @@ func (s *Sync) establishMeerConnection(pe *peers.Peer) error { if err != nil { return common.NewErrorStr(common.ErrStreamBase, fmt.Sprintf("open stream on topic %v failed", topic)).ToError() } - defer stream.Close() common.EgressConnectMeter.Mark(1) qc, err := NewMeerConn(stream) @@ -62,6 +61,10 @@ func (s *Sync) establishMeerConnection(pe *peers.Peer) error { log.Error(err.Error()) } pe.SetMeerConn(false) + err = stream.Close() + if err != nil { + log.Error("Close meer conn", "err", err.Error()) + } }() return err }