Skip to content

Commit 7ddcc15

Browse files
committed
eth: added SynchroniseWith to synchronize with a given peer
1 parent a4f0f8f commit 7ddcc15

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

eth/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ func (api *PrivateAdminAPI) EtcdDeleteWork() error {
318318
return metaapi.EtcdDeleteWork()
319319
}
320320

321+
// Synchronize with the given peer
322+
func (api *PrivateAdminAPI) SynchroniseWith(id enode.ID) error {
323+
return api.eth.handler.SynchroniseWith(id)
324+
}
325+
321326
// PublicDebugAPI is the collection of Ethereum full node APIs exposed
322327
// over the public debugging endpoint.
323328
type PublicDebugAPI struct {

eth/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,16 @@ func (h *handler) RequestEtcdAddMember(id enode.ID) error {
552552
return ethereum.NotFound
553553
}
554554
}
555+
556+
func (h *handler) SynchroniseWith(id enode.ID) error {
557+
if p := h.peers.peer(id.String()); p != nil {
558+
if op, err := h.chainSync.peerSyncOp(p.Peer); err != nil {
559+
return err
560+
} else if op != nil {
561+
return h.doSync(op)
562+
}
563+
return nil
564+
} else {
565+
return ethereum.NotFound
566+
}
567+
}

eth/sync.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package eth
1818

1919
import (
20+
"errors"
2021
"math/big"
2122
"math/rand"
2223
"sync/atomic"
@@ -271,6 +272,27 @@ func (cs *chainSyncer) nextSyncOp() *chainSyncOp {
271272
return op
272273
}
273274

275+
func (cs *chainSyncer) peerSyncOp(p *eth.Peer) (*chainSyncOp, error) {
276+
if cs.doneCh != nil {
277+
// Sync already running
278+
return nil, errors.New("sync already in progress")
279+
} else if metaminer.AmPartner() && !metaminer.IsPartner(p.ID()) {
280+
return nil, errors.New("not a miner")
281+
}
282+
283+
mode, ourTD := cs.modeAndLocalHead()
284+
if mode == downloader.FastSync && atomic.LoadUint32(&cs.handler.snapSync) == 1 {
285+
// Fast sync via the snap protocol
286+
mode = downloader.SnapSync
287+
}
288+
op := peerToSyncOp(mode, p)
289+
if op.td.Cmp(ourTD) <= 0 {
290+
// We're in sync.
291+
return nil, nil
292+
}
293+
return op, nil
294+
}
295+
274296
func peerToSyncOp(mode downloader.SyncMode, p *eth.Peer) *chainSyncOp {
275297
peerHead, peerTD := p.Head()
276298
return &chainSyncOp{mode: mode, peer: p, td: peerTD, head: peerHead}

0 commit comments

Comments
 (0)