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

feat:snapsync for meerevm #834

Merged
merged 9 commits into from
Nov 26, 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
5 changes: 5 additions & 0 deletions consensus/model/meer_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package model
import (
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/rpc/api"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/p2p"
)

type MeerChain interface {
RegisterAPIs(apis []api.API)
GetBlockIDByTxHash(txhash *hash.Hash) uint64
SyncMode() downloader.SyncMode
Downloader() *downloader.Downloader
SyncTo(target common.Hash) error
Server() *p2p.Server
}
8 changes: 0 additions & 8 deletions core/blockchain/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,6 @@ func (b *BlockChain) ProcessBlockBySnap(sds []*SnapData) (meerdag.IBlock, error)
}
}

err := b.meerCheckConnectBlock(node)
if err != nil {
return returnFun(err)
}
_, err = b.meerConnectBlock(node)
if err != nil {
return returnFun(err)
}
err = b.dbUpdateUtxoView(view)
if err != nil {
return returnFun(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum v1.14.11 => github.com/Qitmeer/go-ethereum v1.14.11-q.2
replace github.com/ethereum/go-ethereum v1.14.11 => github.com/Qitmeer/go-ethereum v1.14.11-q.3

replace github.com/karalabe/usb v0.0.2 => github.com/dindinw/karalabe-usb v0.0.0-20230613095851-cd69a62c9c30
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a h1:LUGOJG/lF0TlnlYT
github.com/Qitmeer/crypto v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:gbGKdXSJn71Mc2xcKJHqC/waPiX0byZae67zarj83m4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a h1:O2Erw/YvYAkIqkc2uvP/WwuWf0V8S0+pjU/FKHmjFU4=
github.com/Qitmeer/crypto/cryptonight v0.0.0-20201028030128-6ed4040ca34a/go.mod h1:KiA7g46zc6dkgf/3NbEpJirY75v656WYlmSQNR1wTVk=
github.com/Qitmeer/go-ethereum v1.14.11-q.2 h1:AyPTHi9SKNjlzc4FdFAAE5ChsVnNTaG7UDG/onyhwOY=
github.com/Qitmeer/go-ethereum v1.14.11-q.2/go.mod h1:G/lOJx/OgkhxHd4/H2gVLujGhewGflf2112ENitmGGo=
github.com/Qitmeer/go-ethereum v1.14.11-q.3 h1:LLY0j/63LtvSClETOLaf7DC3ZN/oVtyj43WryJiMMWk=
github.com/Qitmeer/go-ethereum v1.14.11-q.3/go.mod h1:G/lOJx/OgkhxHd4/H2gVLujGhewGflf2112ENitmGGo=
github.com/Qitmeer/go-secp256k1 v0.5.1 h1:t3u4VMSz7LRn9Ki1lecG7tyTNwbLGILt1CwdOAA5N7M=
github.com/Qitmeer/go-secp256k1 v0.5.1/go.mod h1:AkrSQDAMwdV4+pXUQnEAPvPbUiIN4DAHMAm0ueneJB8=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
Expand Down
38 changes: 38 additions & 0 deletions meerevm/meer/meerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package meer

import (
"errors"
"fmt"
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/common/system"
"github.com/Qitmeer/qng/consensus/forks"
"github.com/Qitmeer/qng/consensus/model"
mmeer "github.com/Qitmeer/qng/consensus/model/meer"
Expand All @@ -32,10 +34,12 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"github.com/holiman/uint256"
"math/big"
"reflect"
"time"
)

const (
Expand Down Expand Up @@ -660,6 +664,40 @@ func (b *MeerChain) SyncMode() downloader.SyncMode {
return b.chain.Config().Eth.SyncMode
}

func (b *MeerChain) Downloader() *downloader.Downloader {
return b.chain.Ether().Downloader()
}

func (b *MeerChain) Server() *p2p.Server {
return b.chain.Node().Server()
}

func (b *MeerChain) SyncTo(target common.Hash) error {
mode := b.SyncMode()
quit := make(chan struct{})
err := b.Downloader().SyncQngWaitPeers(mode, target, quit)
if err != nil {
log.Info("Failed to trigger beacon sync", "err", err)
return err
}

ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
for {
select {
case <-ticker.C:
block := b.chain.Ether().BlockChain().GetBlockByHash(target)
if block != nil {
log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash())
return nil
}
}
if system.InterruptRequested(b.consensus.Interrupt()) {
return errors.New("System interrupt")
}
}
}

func (b *MeerChain) APIs() []api.API {
return append([]api.API{
{
Expand Down
16 changes: 14 additions & 2 deletions p2p/synch/snapstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type SnapStatus struct {
peid peer.ID

syncPoint meerdag.IBlock

evmCompleted bool
}

func (s *SnapStatus) IsInit() bool {
Expand Down Expand Up @@ -149,16 +151,26 @@ func (s *SnapStatus) IsCompleted() bool {
}

func (s *SnapStatus) isCompleted() bool {
return s.isPointCompleted() && s.evmCompleted
}

func (s *SnapStatus) isPointCompleted() bool {
if s.syncPoint == nil {
return false
}
return s.syncPoint.GetHash().IsEqual(s.targetBlock)
}

func (s *SnapStatus) CompleteEVM() {
s.locker.Lock()
defer s.locker.Unlock()
s.evmCompleted = true
}

func NewSnapStatus(peid peer.ID) *SnapStatus {
return &SnapStatus{
peid: peid,
locker: &sync.RWMutex{},
peid: peid,
locker: &sync.RWMutex{},
evmCompleted: false,
}
}
15 changes: 13 additions & 2 deletions p2p/synch/snapsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Qitmeer/qng/core/json"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/meerevm/meer"
"github.com/Qitmeer/qng/p2p/common"
"github.com/Qitmeer/qng/p2p/peers"
pb "github.com/Qitmeer/qng/p2p/proto/v1"
Expand Down Expand Up @@ -40,6 +41,9 @@ func (ps *PeerSync) startSnapSync() bool {
if !ps.IsSnapSync() && gs.GetTotal() < best.GraphState.GetTotal()+MaxBlockLocatorsPerMsg {
return false
}
if ps.Chain().MeerChain().Server().PeerCount() <= 0 {
return false
}
// Start syncing from the best peer if one was selected.
ps.processID++
ps.processwg.Add(1)
Expand Down Expand Up @@ -72,7 +76,7 @@ cleanup:
}
add := 0
ps.snapStatus.locker.Lock()
for !ps.snapStatus.isCompleted() {
for !ps.snapStatus.isPointCompleted() {
ret, err := ps.syncSnapStatus(bestPeer)
if err != nil {
log.Warn("Snap-sync", "err", err.Error())
Expand All @@ -93,10 +97,17 @@ cleanup:
break
}
ps.snapStatus.setSyncPoint(latest)
log.Trace("Snap-sync status update point", "point", latest.GetHash().String())
add += len(sds)
}
ps.snapStatus.locker.Unlock()

err = ps.Chain().MeerChain().SyncTo(ps.snapStatus.GetSyncPoint().GetState().GetEVMHash())
if err != nil {
log.Error(err.Error())
meer.Cleanup(ps.Chain().Consensus().Config())
} else {
ps.snapStatus.CompleteEVM()
}
ps.sy.p2p.BlockChain().EndSnapSyncing()
sp := ps.snapStatus.GetSyncPoint()
if sp != nil {
Expand Down
10 changes: 8 additions & 2 deletions p2p/synch/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,18 @@ func RegisterRPC(rpc peers.P2PRPC, basetopic string, base interface{}, handle rp
return
}
common.IngressConnectMeter.Mark(1)
pe := rpc.Peers().Get(stream.Conn().RemotePeer())
if (pe == nil || pe.ChainState() == nil) && basetopic != RPCChainState && basetopic != RPCGoodByeTopic {
log.Debug("Peer is not init, ignore the handling", "protocol", topic, "pe", stream.Conn().RemotePeer())
return
}
ctx, cancel := context.WithTimeout(rpc.Context(), RespTimeout)
defer cancel()

SetRPCStreamDeadlines(stream)

pe := rpc.Peers().Fetch(stream.Conn().RemotePeer())
if pe == nil {
pe = rpc.Peers().Fetch(stream.Conn().RemotePeer())
}
pe.UpdateAddrDir(nil, stream.Conn().RemoteMultiaddr(), stream.Conn().Stat().Direction)

log.Trace("Stream handler", "protocol", topic, "peer", pe.IDWithAddress())
Expand Down
Loading