diff options
author | Sonic <sonic@dexon.org> | 2018-12-04 16:13:41 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:54 +0800 |
commit | dbdeb63000ae5626806432137799d560e19d33d0 (patch) | |
tree | 8c6d7614b4fd018cd9d9804786c729e0df85a8f8 /dex/downloader | |
parent | 6de8d29263b2c184ddd0506d05a18d33554d9788 (diff) | |
download | dexon-dbdeb63000ae5626806432137799d560e19d33d0.tar.gz dexon-dbdeb63000ae5626806432137799d560e19d33d0.tar.zst dexon-dbdeb63000ae5626806432137799d560e19d33d0.zip |
dex: replace total difficulty with block number (#73)
In dexon, we don't "mine" and the blockchain won't and shouldn't fork, so
there is no difficulty concept, just replace it with block number.
Note: this commit only replace total difficulty related logic and code
in dex, dex/downloader package.
Diffstat (limited to 'dex/downloader')
-rw-r--r-- | dex/downloader/downloader.go | 32 | ||||
-rw-r--r-- | dex/downloader/downloader_test.go | 105 | ||||
-rw-r--r-- | dex/downloader/peer.go | 5 | ||||
-rw-r--r-- | dex/downloader/testchain_test.go | 12 |
4 files changed, 67 insertions, 87 deletions
diff --git a/dex/downloader/downloader.go b/dex/downloader/downloader.go index 07c72262c..809fe7e4a 100644 --- a/dex/downloader/downloader.go +++ b/dex/downloader/downloader.go @@ -19,7 +19,6 @@ package downloader import ( "errors" "fmt" - "math/big" "sync" "sync/atomic" "time" @@ -177,9 +176,6 @@ type LightChain interface { GetGovStateByNumber(number uint64) (*types.GovState, error) - // GetTd returns the total difficulty of a local block. - GetTd(common.Hash, uint64) *big.Int - // InsertDexonHeaderChain inserts a batch of headers into the local chain. InsertDexonHeaderChain([]*types.HeaderWithGovState, *dexCore.TSigVerifierCache) (int, error) @@ -330,8 +326,8 @@ func (d *Downloader) UnregisterPeer(id string) error { // Synchronise tries to sync up our local block chain with a remote peer, both // adding various sanity checks as well as wrapping it with various log entries. -func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode SyncMode) error { - err := d.synchronise(id, head, td, mode) +func (d *Downloader) Synchronise(id string, head common.Hash, number uint64, mode SyncMode) error { + err := d.synchronise(id, head, number, mode) switch err { case nil: case errBusy: @@ -354,9 +350,9 @@ func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode } // synchronise will select the peer and use it for synchronising. If an empty string is given -// it will use the best peer possible and synchronize if its TD is higher than our own. If any of the +// it will use the best peer possible and synchronize if its number is higher than our own. If any of the // checks fail an error will be returned. This method is synchronous -func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode SyncMode) error { +func (d *Downloader) synchronise(id string, hash common.Hash, number uint64, mode SyncMode) error { // Mock out the synchronisation if testing if d.synchroniseMock != nil { return d.synchroniseMock(id, hash) @@ -413,12 +409,12 @@ func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int, mode if p == nil { return errUnknownPeer } - return d.syncWithPeer(p, hash, td) + return d.syncWithPeer(p, hash, number) } // syncWithPeer starts a block synchronization based on the hash chain from the // specified peer and head hash. -func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.Int) (err error) { +func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, number uint64) (err error) { d.mux.Post(StartEvent{}) defer func() { // reset on error @@ -432,7 +428,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I return errTooOld } - log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "td", td, "mode", d.mode) + log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "number", number, "mode", d.mode) defer func(start time.Time) { log.Debug("Synchronisation terminated", "elapsed", time.Since(start)) }(time.Now()) @@ -510,7 +506,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I func() error { return d.fetchHeaders(p, origin+1, pivot) }, // Headers are always retrieved func() error { return d.fetchBodies(origin + 1) }, // Bodies are retrieved during normal and fast sync func() error { return d.fetchReceipts(origin + 1) }, // Receipts are retrieved during fast sync - func() error { return d.processHeaders(origin+1, pivot, td) }, + func() error { return d.processHeaders(origin+1, pivot, number) }, } if d.mode == FastSync { fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) }) @@ -1305,7 +1301,7 @@ func (d *Downloader) fetchParts(errCancel error, deliveryCh chan dataPack, deliv // processHeaders takes batches of retrieved headers from an input channel and // keeps processing and scheduling them into the header chain and downloader's // queue until the stream ends or a failure occurs. -func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) error { +func (d *Downloader) processHeaders(origin uint64, pivot uint64, number uint64) error { // Keep a count of uncertain headers to roll back rollback := []*types.Header{} defer func() { @@ -1351,21 +1347,21 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er case <-d.cancelCh: } } - // If no headers were retrieved at all, the peer violated its TD promise that it had a + // If no headers were retrieved at all, the peer violated its number promise that it had a // better chain compared to ours. The only exception is if its promised blocks were // already imported by other means (e.g. fecher): // // R <remote peer>, L <local node>: Both at block 10 // R: Mine block 11, and propagate it to L // L: Queue block 11 for import - // L: Notice that R's head and TD increased compared to ours, start sync + // L: Notice that R's head and number increased compared to ours, start sync // L: Import of block 11 finishes // 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) + // L: Request new headers up from 11 (R's number was higher, it must have something) // R: Nothing to give if d.mode != LightSync { head := d.blockchain.CurrentBlock() - if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 { + if !gotHeaders && number > head.NumberU64() { return errStallingPeer } } @@ -1378,7 +1374,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) er // peer gave us something useful, we're already happy/progressed (above check). if d.mode == FastSync || d.mode == LightSync { head := d.lightchain.CurrentHeader() - if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 { + if number > head.Number.Uint64() { return errStallingPeer } } diff --git a/dex/downloader/downloader_test.go b/dex/downloader/downloader_test.go index c5e1451f5..0d92ad97f 100644 --- a/dex/downloader/downloader_test.go +++ b/dex/downloader/downloader_test.go @@ -19,7 +19,6 @@ package downloader import ( "errors" "fmt" - "math/big" "sync" "sync/atomic" "testing" @@ -56,7 +55,6 @@ type downloadTester struct { ownHeaders map[common.Hash]*types.Header // Headers belonging to the tester ownBlocks map[common.Hash]*types.Block // Blocks belonging to the tester ownReceipts map[common.Hash]types.Receipts // Receipts belonging to the tester - ownChainTd map[common.Hash]*big.Int // Total difficulties of the blocks in the local chain lock sync.RWMutex } @@ -71,7 +69,6 @@ func newTester() *downloadTester { ownHeaders: map[common.Hash]*types.Header{testGenesis.Hash(): testGenesis.Header()}, ownBlocks: map[common.Hash]*types.Block{testGenesis.Hash(): testGenesis}, ownReceipts: map[common.Hash]types.Receipts{testGenesis.Hash(): nil}, - ownChainTd: map[common.Hash]*big.Int{testGenesis.Hash(): testGenesis.Difficulty()}, } tester.stateDb = ethdb.NewMemDatabase() tester.stateDb.Put(testGenesis.Root().Bytes(), []byte{0x00}) @@ -86,17 +83,17 @@ func (dl *downloadTester) terminate() { } // sync starts synchronizing with a remote peer, blocking until it completes. -func (dl *downloadTester) sync(id string, td *big.Int, mode SyncMode) error { +func (dl *downloadTester) sync(id string, number uint64, mode SyncMode) error { dl.lock.RLock() hash := dl.peers[id].chain.headBlock().Hash() - // If no particular TD was requested, load from the peer's blockchain - if td == nil { - td = dl.peers[id].chain.td(hash) + // If no particular number was requested, load from the peer's blockchain + if number == 0 { + number, _ = dl.peers[id].chain.hashToNumber(hash) } dl.lock.RUnlock() // Synchronise with the chosen peer and ensure proper cleanup afterwards - err := dl.downloader.synchronise(id, hash, td, mode) + err := dl.downloader.synchronise(id, hash, number, mode) select { case <-dl.downloader.cancelCh: // Ok, downloader fully cancelled after sync cycle @@ -202,14 +199,6 @@ func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error { return fmt.Errorf("non existent block: %x", hash[:4]) } -// GetTd retrieves the block's total difficulty from the canonical chain. -func (dl *downloadTester) GetTd(hash common.Hash, number uint64) *big.Int { - dl.lock.RLock() - defer dl.lock.RUnlock() - - return dl.ownChainTd[hash] -} - // InsertDexonHeaderChain injects a new batch of headers into the simulated chain. func (dl *downloadTester) InsertDexonHeaderChain(headers []*types.HeaderWithGovState, verifierCache *dexCore.TSigVerifierCache) (i int, err error) { dl.lock.Lock() @@ -234,7 +223,6 @@ func (dl *downloadTester) InsertDexonHeaderChain(headers []*types.HeaderWithGovS } dl.ownHashes = append(dl.ownHashes, header.Hash()) dl.ownHeaders[header.Hash()] = header.Header - dl.ownChainTd[header.Hash()] = new(big.Int).Add(dl.ownChainTd[header.ParentHash], header.Difficulty) } return len(headers), nil } @@ -256,7 +244,6 @@ func (dl *downloadTester) InsertDexonChain(blocks types.Blocks) (i int, err erro } dl.ownBlocks[block.Hash()] = block dl.stateDb.Put(block.Root().Bytes(), []byte{0x00}) - dl.ownChainTd[block.Hash()] = new(big.Int).Add(dl.ownChainTd[block.ParentHash()], block.Difficulty()) } return len(blocks), nil } @@ -288,7 +275,6 @@ func (dl *downloadTester) Rollback(hashes []common.Hash) { if dl.ownHashes[len(dl.ownHashes)-1] == hashes[i] { dl.ownHashes = dl.ownHashes[:len(dl.ownHashes)-1] } - delete(dl.ownChainTd, hashes[i]) delete(dl.ownHeaders, hashes[i]) delete(dl.ownReceipts, hashes[i]) delete(dl.ownBlocks, hashes[i]) @@ -324,9 +310,10 @@ type downloadTesterPeer struct { // Head constructs a function to retrieve a peer's current head hash // and total difficulty. -func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int) { +func (dlp *downloadTesterPeer) Head() (common.Hash, uint64) { b := dlp.chain.headBlock() - return b.Hash(), dlp.chain.td(b.Hash()) + number, _ := dlp.chain.hashToNumber(b.Hash()) + return b.Hash(), number } // RequestHeadersByHash constructs a GetBlockHeaders function based on a hashed @@ -457,7 +444,7 @@ func testCanonicalSynchronisation(t *testing.T, protocol int, mode SyncMode) { tester.newPeer("peer", protocol, chain) // Synchronise with the peer and make sure all relevant data was retrieved - if err := tester.sync("peer", nil, mode); err != nil { + if err := tester.sync("peer", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -489,7 +476,7 @@ func testThrottling(t *testing.T, protocol int, mode SyncMode) { // Start a synchronisation concurrently errc := make(chan error) go func() { - errc <- tester.sync("peer", nil, mode) + errc <- tester.sync("peer", 0, mode) }() // Iteratively take some blocks, always checking the retrieval count for { @@ -604,7 +591,7 @@ func testCancel(t *testing.T, protocol int, mode SyncMode) { t.Errorf("download queue not idle") } // Synchronise with the peer, but cancel afterwards - if err := tester.sync("peer", nil, mode); err != nil { + if err := tester.sync("peer", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } tester.downloader.Cancel() @@ -635,7 +622,7 @@ func testMultiSynchronisation(t *testing.T, protocol int, mode SyncMode) { id := fmt.Sprintf("peer #%d", i) tester.newPeer(id, protocol, chain.shorten(chain.len()/(i+1))) } - if err := tester.sync("peer #0", nil, mode); err != nil { + if err := tester.sync("peer #0", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -665,7 +652,7 @@ func testMultiProtoSync(t *testing.T, protocol int, mode SyncMode) { tester.newPeer("peer 64", 64, chain) // Synchronise with the requested peer and make sure all blocks were retrieved - if err := tester.sync(fmt.Sprintf("peer %d", protocol), nil, mode); err != nil { + if err := tester.sync(fmt.Sprintf("peer %d", protocol), 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -707,7 +694,7 @@ func testEmptyShortCircuit(t *testing.T, protocol int, mode SyncMode) { atomic.AddInt32(&receiptsHave, int32(len(headers))) } // Synchronise with the peer and make sure all blocks were retrieved - if err := tester.sync("peer", nil, mode); err != nil { + if err := tester.sync("peer", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -752,12 +739,12 @@ func testMissingHeaderAttack(t *testing.T, protocol int, mode SyncMode) { delete(brokenChain.headerm, brokenChain.chain[brokenChain.len()/2]) tester.newPeer("attack", protocol, brokenChain) - if err := tester.sync("attack", nil, mode); err == nil { + if err := tester.sync("attack", 0, mode); err == nil { t.Fatalf("succeeded attacker synchronisation") } // Synchronise with the valid peer and make sure sync succeeds tester.newPeer("valid", protocol, chain) - if err := tester.sync("valid", nil, mode); err != nil { + if err := tester.sync("valid", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -786,13 +773,13 @@ func testShiftedHeaderAttack(t *testing.T, protocol int, mode SyncMode) { delete(brokenChain.blockm, brokenChain.chain[1]) delete(brokenChain.receiptm, brokenChain.chain[1]) tester.newPeer("attack", protocol, brokenChain) - if err := tester.sync("attack", nil, mode); err == nil { + if err := tester.sync("attack", 0, mode); err == nil { t.Fatalf("succeeded attacker synchronisation") } // Synchronise with the valid peer and make sure sync succeeds tester.newPeer("valid", protocol, chain) - if err := tester.sync("valid", nil, mode); err != nil { + if err := tester.sync("valid", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } assertOwnChain(t, tester, chain.len()) @@ -822,7 +809,7 @@ func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { delete(fastAttackChain.headerm, fastAttackChain.chain[missing]) tester.newPeer("fast-attack", protocol, fastAttackChain) - if err := tester.sync("fast-attack", nil, mode); err == nil { + if err := tester.sync("fast-attack", 0, mode); err == nil { t.Fatalf("succeeded fast attacker synchronisation") } if head := tester.CurrentHeader().Number.Int64(); int(head) > MaxHeaderFetch { @@ -838,7 +825,7 @@ func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { delete(blockAttackChain.headerm, blockAttackChain.chain[missing]) tester.newPeer("block-attack", protocol, blockAttackChain) - if err := tester.sync("block-attack", nil, mode); err == nil { + if err := tester.sync("block-attack", 0, mode); err == nil { t.Fatalf("succeeded block attacker synchronisation") } if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch { @@ -861,7 +848,7 @@ func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { } tester.downloader.syncInitHook = nil } - if err := tester.sync("withhold-attack", nil, mode); err == nil { + if err := tester.sync("withhold-attack", 0, mode); err == nil { t.Fatalf("succeeded withholding attacker synchronisation") } if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch { @@ -878,7 +865,7 @@ func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { // sync. Note, we can't assert anything about the receipts since we won't purge the // database of them, hence we can't use assertOwnChain. tester.newPeer("valid", protocol, chain) - if err := tester.sync("valid", nil, mode); err != nil { + if err := tester.sync("valid", 0, mode); err != nil { t.Fatalf("failed to synchronise blocks: %v", err) } if hs := len(tester.ownHeaders); hs != chain.len() { @@ -891,16 +878,26 @@ func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { } } -// Tests that a peer advertising an high TD doesn't get to stall the downloader +// Tests that a peer advertising an high number doesn't get to stall the downloader // afterwards by not sending any useful hashes. -func TestHighTDStarvationAttack62(t *testing.T) { testHighTDStarvationAttack(t, 62, FullSync) } -func TestHighTDStarvationAttack63Full(t *testing.T) { testHighTDStarvationAttack(t, 63, FullSync) } -func TestHighTDStarvationAttack63Fast(t *testing.T) { testHighTDStarvationAttack(t, 63, FastSync) } -func TestHighTDStarvationAttack64Full(t *testing.T) { testHighTDStarvationAttack(t, 64, FullSync) } -func TestHighTDStarvationAttack64Fast(t *testing.T) { testHighTDStarvationAttack(t, 64, FastSync) } -func TestHighTDStarvationAttack64Light(t *testing.T) { testHighTDStarvationAttack(t, 64, LightSync) } - -func testHighTDStarvationAttack(t *testing.T, protocol int, mode SyncMode) { +func TestHighNumberStarvationAttack62(t *testing.T) { testHighNumberStarvationAttack(t, 62, FullSync) } +func TestHighNumberStarvationAttack63Full(t *testing.T) { + testHighNumberStarvationAttack(t, 63, FullSync) +} +func TestHighNumberStarvationAttack63Fast(t *testing.T) { + testHighNumberStarvationAttack(t, 63, FastSync) +} +func TestHighNumberStarvationAttack64Full(t *testing.T) { + testHighNumberStarvationAttack(t, 64, FullSync) +} +func TestHighNumberStarvationAttack64Fast(t *testing.T) { + testHighNumberStarvationAttack(t, 64, FastSync) +} +func TestHighNumberStarvationAttack64Light(t *testing.T) { + testHighNumberStarvationAttack(t, 64, LightSync) +} + +func testHighNumberStarvationAttack(t *testing.T, protocol int, mode SyncMode) { t.Parallel() tester := newTester() @@ -908,7 +905,7 @@ func testHighTDStarvationAttack(t *testing.T, protocol int, mode SyncMode) { chain := testChainBase.shorten(1) tester.newPeer("attack", protocol, chain) - if err := tester.sync("attack", big.NewInt(1000000), mode); err != errStallingPeer { + if err := tester.sync("attack", 1000000, mode); err != errStallingPeer { t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer) } } @@ -964,7 +961,7 @@ func testBlockHeaderAttackerDropping(t *testing.T, protocol int) { // Simulate a synchronisation and check the required result tester.downloader.synchroniseMock = func(string, common.Hash) error { return tt.result } - tester.downloader.Synchronise(id, tester.genesis.Hash(), big.NewInt(1000), FullSync) + tester.downloader.Synchronise(id, tester.genesis.Hash(), 1000, FullSync) if _, ok := tester.peers[id]; !ok != tt.drop { t.Errorf("test %d: peer drop mismatch for %v: have %v, want %v", i, tt.result, !ok, tt.drop) } @@ -1004,7 +1001,7 @@ func testSyncProgress(t *testing.T, protocol int, mode SyncMode) { go func() { defer pending.Done() - if err := tester.sync("peer-half", nil, mode); err != nil { + if err := tester.sync("peer-half", 0, mode); err != nil { panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) } }() @@ -1020,7 +1017,7 @@ func testSyncProgress(t *testing.T, protocol int, mode SyncMode) { pending.Add(1) go func() { defer pending.Done() - if err := tester.sync("peer-full", nil, mode); err != nil { + if err := tester.sync("peer-full", 0, mode); err != nil { panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) } }() @@ -1090,7 +1087,7 @@ func testFailedSyncProgress(t *testing.T, protocol int, mode SyncMode) { pending.Add(1) go func() { defer pending.Done() - if err := tester.sync("faulty", nil, mode); err == nil { + if err := tester.sync("faulty", 0, mode); err == nil { panic("succeeded faulty synchronisation") } }() @@ -1108,7 +1105,7 @@ func testFailedSyncProgress(t *testing.T, protocol int, mode SyncMode) { pending.Add(1) go func() { defer pending.Done() - if err := tester.sync("valid", nil, mode); err != nil { + if err := tester.sync("valid", 0, mode); err != nil { panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) } }() @@ -1161,7 +1158,7 @@ func testFakedSyncProgress(t *testing.T, protocol int, mode SyncMode) { pending.Add(1) go func() { defer pending.Done() - if err := tester.sync("attack", nil, mode); err == nil { + if err := tester.sync("attack", 0, mode); err == nil { panic("succeeded attacker synchronisation") } }() @@ -1181,7 +1178,7 @@ func testFakedSyncProgress(t *testing.T, protocol int, mode SyncMode) { go func() { defer pending.Done() - if err := tester.sync("valid", nil, mode); err != nil { + if err := tester.sync("valid", 0, mode); err != nil { panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) } }() @@ -1240,7 +1237,7 @@ func testDeliverHeadersHang(t *testing.T, protocol int, mode SyncMode) { peer: tester.downloader.peers.peers["peer"].peer, tester: tester, } - if err := tester.sync("peer", nil, mode); err != nil { + if err := tester.sync("peer", 0, mode); err != nil { t.Errorf("test %d: sync failed: %v", i, err) } tester.terminate() @@ -1252,7 +1249,7 @@ type floodingTestPeer struct { tester *downloadTester } -func (ftp *floodingTestPeer) Head() (common.Hash, *big.Int) { return ftp.peer.Head() } +func (ftp *floodingTestPeer) Head() (common.Hash, uint64) { return ftp.peer.Head() } func (ftp *floodingTestPeer) RequestHeadersByHash(hash common.Hash, count int, skip int, reverse, withGov bool) error { return ftp.peer.RequestHeadersByHash(hash, count, skip, reverse, withGov) } diff --git a/dex/downloader/peer.go b/dex/downloader/peer.go index b6f2936b7..25c355df1 100644 --- a/dex/downloader/peer.go +++ b/dex/downloader/peer.go @@ -23,7 +23,6 @@ import ( "errors" "fmt" "math" - "math/big" "sort" "sync" "sync/atomic" @@ -77,7 +76,7 @@ type peerConnection struct { // LightPeer encapsulates the methods required to synchronise with a remote light peer. type LightPeer interface { - Head() (common.Hash, *big.Int) + Head() (common.Hash, uint64) RequestHeadersByHash(common.Hash, int, int, bool, bool) error RequestHeadersByNumber(uint64, int, int, bool, bool) error RequestGovStateByHash(common.Hash) error @@ -96,7 +95,7 @@ type lightPeerWrapper struct { peer LightPeer } -func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() } +func (w *lightPeerWrapper) Head() (common.Hash, uint64) { return w.peer.Head() } func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse, withGov bool) error { return w.peer.RequestHeadersByHash(h, amount, skip, reverse, withGov) } diff --git a/dex/downloader/testchain_test.go b/dex/downloader/testchain_test.go index 5dcc1f403..810fb41b3 100644 --- a/dex/downloader/testchain_test.go +++ b/dex/downloader/testchain_test.go @@ -96,7 +96,6 @@ type testChain struct { headerm map[common.Hash]*types.Header blockm map[common.Hash]*types.Block receiptm map[common.Hash][]*types.Receipt - tdm map[common.Hash]*big.Int } // newTestChain creates a blockchain of the given length. @@ -105,7 +104,6 @@ func newTestChain(length int, genesis *types.Block) *testChain { tc.genesis = genesis tc.chain = append(tc.chain, genesis.Hash()) tc.headerm[tc.genesis.Hash()] = tc.genesis.Header() - tc.tdm[tc.genesis.Hash()] = tc.genesis.Difficulty() tc.blockm[tc.genesis.Hash()] = tc.genesis tc.generate(length-1, 0, genesis, false) return tc @@ -133,12 +131,10 @@ func (tc *testChain) copy(newlen int) *testChain { headerm: make(map[common.Hash]*types.Header, newlen), blockm: make(map[common.Hash]*types.Block, newlen), receiptm: make(map[common.Hash][]*types.Receipt, newlen), - tdm: make(map[common.Hash]*big.Int, newlen), } for i := 0; i < len(tc.chain) && i < newlen; i++ { hash := tc.chain[i] cpy.chain = append(cpy.chain, tc.chain[i]) - cpy.tdm[hash] = tc.tdm[hash] cpy.blockm[hash] = tc.blockm[hash] cpy.headerm[hash] = tc.headerm[hash] cpy.receiptm[hash] = tc.receiptm[hash] @@ -179,15 +175,12 @@ func (tc *testChain) generate(n int, seed byte, parent *types.Block, heavy bool) }) // Convert the block-chain into a hash-chain and header/block maps - td := new(big.Int).Set(tc.td(parent.Hash())) for i, b := range blocks { - td := td.Add(td, b.Difficulty()) hash := b.Hash() tc.chain = append(tc.chain, hash) tc.blockm[hash] = b tc.headerm[hash] = b.Header() tc.receiptm[hash] = receipts[i] - tc.tdm[hash] = new(big.Int).Set(td) } } @@ -201,11 +194,6 @@ func (tc *testChain) headBlock() *types.Block { return tc.blockm[tc.chain[len(tc.chain)-1]] } -// td returns the total difficulty of the given block. -func (tc *testChain) td(hash common.Hash) *big.Int { - return tc.tdm[hash] -} - // headersByHash returns headers in ascending order from the given hash. func (tc *testChain) headersByHash(origin common.Hash, amount int, skip int) []*types.HeaderWithGovState { num, _ := tc.hashToNumber(origin) |