aboutsummaryrefslogtreecommitdiffstats
path: root/eth/downloader
diff options
context:
space:
mode:
authorzsfelfoldi <zsfelfoldi@gmail.com>2016-01-14 02:35:48 +0800
committerFelix Lange <fjl@twurst.com>2016-11-09 09:12:53 +0800
commit7db7109a5b53c339f00e9c05ac826b3dbd1f98e1 (patch)
treec447d9816c4490e71cb2a8d2420d82f938d8757a /eth/downloader
parent9f8d192991c4f68fa14c91366722bbca601da117 (diff)
downloadgo-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.gz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.bz2
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.lz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.xz
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.tar.zst
go-tangerine-7db7109a5b53c339f00e9c05ac826b3dbd1f98e1.zip
cmd, eth: added light client and light server modes
Diffstat (limited to 'eth/downloader')
-rw-r--r--eth/downloader/downloader.go27
-rw-r--r--eth/downloader/downloader_test.go2
2 files changed, 22 insertions, 7 deletions
diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go
index da20e48a2..b1f4b8169 100644
--- a/eth/downloader/downloader.go
+++ b/eth/downloader/downloader.go
@@ -164,13 +164,13 @@ type Downloader struct {
}
// New creates a new downloader to fetch hashes and blocks from remote peers.
-func New(stateDb ethdb.Database, mux *event.TypeMux, hasHeader headerCheckFn, hasBlockAndState blockAndStateCheckFn,
+func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, hasHeader headerCheckFn, hasBlockAndState blockAndStateCheckFn,
getHeader headerRetrievalFn, getBlock blockRetrievalFn, headHeader headHeaderRetrievalFn, headBlock headBlockRetrievalFn,
headFastBlock headFastBlockRetrievalFn, commitHeadBlock headBlockCommitterFn, getTd tdRetrievalFn, insertHeaders headerChainInsertFn,
insertBlocks blockChainInsertFn, insertReceipts receiptChainInsertFn, rollback chainRollbackFn, dropPeer peerDropFn) *Downloader {
dl := &Downloader{
- mode: FullSync,
+ mode: mode,
mux: mux,
queue: newQueue(stateDb),
peers: newPeerSet(),
@@ -1179,10 +1179,23 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
for i, header := range rollback {
hashes[i] = header.Hash()
}
- lastHeader, lastFastBlock, lastBlock := d.headHeader().Number, d.headFastBlock().Number(), d.headBlock().Number()
+ lastHeader, lastFastBlock, lastBlock := d.headHeader().Number, common.Big0, common.Big0
+ if d.headFastBlock != nil {
+ lastFastBlock = d.headFastBlock().Number()
+ }
+ if d.headBlock != nil {
+ lastBlock = d.headBlock().Number()
+ }
d.rollback(hashes)
+ curFastBlock, curBlock := common.Big0, common.Big0
+ if d.headFastBlock != nil {
+ curFastBlock = d.headFastBlock().Number()
+ }
+ if d.headBlock != nil {
+ curBlock = d.headBlock().Number()
+ }
glog.V(logger.Warn).Infof("Rolled back %d headers (LH: %d->%d, FB: %d->%d, LB: %d->%d)",
- len(hashes), lastHeader, d.headHeader().Number, lastFastBlock, d.headFastBlock().Number(), lastBlock, d.headBlock().Number())
+ len(hashes), lastHeader, d.headHeader().Number, lastFastBlock, curFastBlock, lastBlock, curBlock)
// If we're already past the pivot point, this could be an attack, thread carefully
if rollback[len(rollback)-1].Number.Uint64() > pivot {
@@ -1229,8 +1242,10 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error {
// L: Sync begins, and finds common ancestor at 11
// L: Request new headers up from 11 (R's TD was higher, it must have something)
// R: Nothing to give
- if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
- return errStallingPeer
+ if d.mode != LightSync {
+ if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
+ return errStallingPeer
+ }
}
// If fast or light syncing, ensure promised headers are indeed delivered. This is
// needed to detect scenarios where an attacker feeds a bad pivot and then bails out
diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go
index 2849712ab..ff8cd1044 100644
--- a/eth/downloader/downloader_test.go
+++ b/eth/downloader/downloader_test.go
@@ -96,7 +96,7 @@ func newTester() *downloadTester {
tester.stateDb, _ = ethdb.NewMemDatabase()
tester.stateDb.Put(genesis.Root().Bytes(), []byte{0x00})
- tester.downloader = New(tester.stateDb, new(event.TypeMux), tester.hasHeader, tester.hasBlock, tester.getHeader,
+ tester.downloader = New(FullSync, tester.stateDb, new(event.TypeMux), tester.hasHeader, tester.hasBlock, tester.getHeader,
tester.getBlock, tester.headHeader, tester.headBlock, tester.headFastBlock, tester.commitHeadBlock, tester.getTd,
tester.insertHeaders, tester.insertBlocks, tester.insertReceipts, tester.rollback, tester.dropPeer)