Skip to content

Commit

Permalink
eth: added SynchroniseWith to synchronize with a given peer
Browse files Browse the repository at this point in the history
  • Loading branch information
sadoci committed Nov 9, 2021
1 parent a4f0f8f commit 7ddcc15
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (api *PrivateAdminAPI) EtcdDeleteWork() error {
return metaapi.EtcdDeleteWork()
}

// Synchronize with the given peer
func (api *PrivateAdminAPI) SynchroniseWith(id enode.ID) error {
return api.eth.handler.SynchroniseWith(id)
}

// PublicDebugAPI is the collection of Ethereum full node APIs exposed
// over the public debugging endpoint.
type PublicDebugAPI struct {
Expand Down
13 changes: 13 additions & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,16 @@ func (h *handler) RequestEtcdAddMember(id enode.ID) error {
return ethereum.NotFound
}
}

func (h *handler) SynchroniseWith(id enode.ID) error {
if p := h.peers.peer(id.String()); p != nil {
if op, err := h.chainSync.peerSyncOp(p.Peer); err != nil {
return err
} else if op != nil {
return h.doSync(op)
}
return nil
} else {
return ethereum.NotFound
}
}
22 changes: 22 additions & 0 deletions eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package eth

import (
"errors"
"math/big"
"math/rand"
"sync/atomic"
Expand Down Expand Up @@ -271,6 +272,27 @@ func (cs *chainSyncer) nextSyncOp() *chainSyncOp {
return op
}

func (cs *chainSyncer) peerSyncOp(p *eth.Peer) (*chainSyncOp, error) {
if cs.doneCh != nil {
// Sync already running
return nil, errors.New("sync already in progress")
} else if metaminer.AmPartner() && !metaminer.IsPartner(p.ID()) {
return nil, errors.New("not a miner")
}

mode, ourTD := cs.modeAndLocalHead()
if mode == downloader.FastSync && atomic.LoadUint32(&cs.handler.snapSync) == 1 {
// Fast sync via the snap protocol
mode = downloader.SnapSync
}
op := peerToSyncOp(mode, p)
if op.td.Cmp(ourTD) <= 0 {
// We're in sync.
return nil, nil
}
return op, nil
}

func peerToSyncOp(mode downloader.SyncMode, p *eth.Peer) *chainSyncOp {
peerHead, peerTD := p.Head()
return &chainSyncOp{mode: mode, peer: p, td: peerTD, head: peerHead}
Expand Down

0 comments on commit 7ddcc15

Please sign in to comment.