Skip to content

Commit 51b2f16

Browse files
holimankaralabe
authored andcommitted
downloader: different sync strategy
1 parent 68be45e commit 51b2f16

2 files changed

Lines changed: 155 additions & 39 deletions

File tree

‎eth/downloader/downloader.go‎

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
430430
}
431431
height := latest.Number.Uint64()
432432

433-
origin, err := d.findAncestor(p, height)
433+
origin, err := d.findAncestor(p, latest)
434434
if err != nil {
435435
return err
436436
}
@@ -587,41 +587,85 @@ func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) {
587587
}
588588
}
589589

590+
// calculateRequestSpan calculates what headers to request from a peer when trying to determine the
591+
// common ancestor.
592+
// It returns parameters to be used for peer.RequestHeadersByNumber:
593+
// from - starting block number
594+
// count - number of headers to request
595+
// skip - number of headers to skip
596+
// and also returns 'max', the last block which is expected to be returned by the remote peers,
597+
// given the (from,count,skip)
598+
func calculateRequestSpan(remoteHeight, localHeight uint64) (int64, int, int, uint64) {
599+
var (
600+
from int
601+
count int
602+
MaxCount = MaxHeaderFetch / 16
603+
)
604+
// requestHead is the highest block that we will ask for. If requestHead is not offset,
605+
// the highest block that we will get is 16 blocks back from head, which means we
606+
// will fetch 14 or 15 blocks unnecessarily in the case the height difference
607+
// between us and the peer is 1-2 blocks, which is most common
608+
requestHead := int(remoteHeight) - 1
609+
if requestHead < 0 {
610+
requestHead = 0
611+
}
612+
// requestBottom is the lowest block we want included in the query
613+
// Ideally, we want to include just below own head
614+
requestBottom := int(localHeight - 1)
615+
if requestBottom < 0 {
616+
requestBottom = 0
617+
}
618+
totalSpan := requestHead - requestBottom
619+
span := 1 + totalSpan/MaxCount
620+
if span < 2 {
621+
span = 2
622+
}
623+
if span > 16 {
624+
span = 16
625+
}
626+
627+
count = 1 + totalSpan/span
628+
if count > MaxCount {
629+
count = MaxCount
630+
}
631+
if count < 2 {
632+
count = 2
633+
}
634+
from = requestHead - (count-1)*span
635+
if from < 0 {
636+
from = 0
637+
}
638+
max := from + (count-1)*span
639+
return int64(from), count, span - 1, uint64(max)
640+
}
641+
590642
// findAncestor tries to locate the common ancestor link of the local chain and
591643
// a remote peers blockchain. In the general case when our node was in sync and
592644
// on the correct chain, checking the top N links should already get us a match.
593645
// In the rare scenario when we ended up on a long reorganisation (i.e. none of
594646
// the head links match), we do a binary search to find the common ancestor.
595-
func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, error) {
647+
func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header) (uint64, error) {
596648
// Figure out the valid ancestor range to prevent rewrite attacks
597-
floor, ceil := int64(-1), d.lightchain.CurrentHeader().Number.Uint64()
598-
599-
if d.mode == FullSync {
600-
ceil = d.blockchain.CurrentBlock().NumberU64()
601-
} else if d.mode == FastSync {
602-
ceil = d.blockchain.CurrentFastBlock().NumberU64()
649+
var (
650+
floor = int64(-1)
651+
localHeight uint64
652+
remoteHeight = remoteHeader.Number.Uint64()
653+
)
654+
switch d.mode {
655+
case FullSync:
656+
localHeight = d.blockchain.CurrentBlock().NumberU64()
657+
case FastSync:
658+
localHeight = d.blockchain.CurrentFastBlock().NumberU64()
659+
default:
660+
localHeight = d.lightchain.CurrentHeader().Number.Uint64()
603661
}
604-
if ceil >= MaxForkAncestry {
605-
floor = int64(ceil - MaxForkAncestry)
662+
p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight)
663+
if localHeight >= MaxForkAncestry {
664+
floor = int64(localHeight - MaxForkAncestry)
606665
}
607-
p.log.Debug("Looking for common ancestor", "local", ceil, "remote", height)
608666

609-
// Request the topmost blocks to short circuit binary ancestor lookup
610-
head := ceil
611-
if head > height {
612-
head = height
613-
}
614-
from := int64(head) - int64(MaxHeaderFetch)
615-
if from < 0 {
616-
from = 0
617-
}
618-
// Span out with 15 block gaps into the future to catch bad head reports
619-
limit := 2 * MaxHeaderFetch / 16
620-
count := 1 + int((int64(ceil)-from)/16)
621-
if count > limit {
622-
count = limit
623-
}
624-
go p.peer.RequestHeadersByNumber(uint64(from), count, 15, false)
667+
from, count, skip, max := calculateRequestSpan(remoteHeight, localHeight)
668+
go p.peer.RequestHeadersByNumber(uint64(from), count, skip, false)
625669

626670
// Wait for the remote response to the head fetch
627671
number, hash := uint64(0), common.Hash{}
@@ -647,30 +691,26 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
647691
return 0, errEmptyHeaderSet
648692
}
649693
// Make sure the peer's reply conforms to the request
650-
for i := 0; i < len(headers); i++ {
651-
if number := headers[i].Number.Int64(); number != from+int64(i)*16 {
652-
p.log.Warn("Head headers broke chain ordering", "index", i, "requested", from+int64(i)*16, "received", number)
694+
for i, header := range headers {
695+
expectNumber := from + int64(i)*int64((skip+1))
696+
if number := header.Number.Int64(); number != expectNumber {
697+
p.log.Warn("Head headers broke chain ordering", "index", i, "requested", expectNumber, "received", number)
653698
return 0, errInvalidChain
654699
}
655700
}
656701
// Check if a common ancestor was found
657702
finished = true
658703
for i := len(headers) - 1; i >= 0; i-- {
659704
// Skip any headers that underflow/overflow our requested set
660-
if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > ceil {
705+
if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > max {
661706
continue
662707
}
663708
// Otherwise check if we already know the header or not
664709
h := headers[i].Hash()
665710
n := headers[i].Number.Uint64()
666-
if (d.mode == FullSync && d.blockchain.HasBlock(h, n)) || (d.mode != FullSync && d.lightchain.HasHeader(h, n)) {
711+
if (d.mode == FullSync && d.blockchain.HasBlock(h, n)) ||
712+
(d.mode != FullSync && d.lightchain.HasHeader(h, n)) {
667713
number, hash = n, h
668-
669-
// If every header is known, even future ones, the peer straight out lied about its head
670-
if number > height && i == limit-1 {
671-
p.log.Warn("Lied about chain head", "reported", height, "found", number)
672-
return 0, errStallingPeer
673-
}
674714
break
675715
}
676716
}
@@ -694,7 +734,7 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
694734
return number, nil
695735
}
696736
// Ancestor not found, we need to binary search over our chain
697-
start, end := uint64(0), head
737+
start, end := uint64(0), remoteHeight
698738
if floor > 0 {
699739
start = uint64(floor)
700740
}

‎eth/downloader/downloader_test.go‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"math/big"
23+
"strings"
2324
"sync"
2425
"sync/atomic"
2526
"testing"
@@ -1479,3 +1480,78 @@ func (ftp *floodingTestPeer) RequestHeadersByNumber(from uint64, count, skip int
14791480
}
14801481
return nil
14811482
}
1483+
1484+
func TestRemoteHeaderRequestSpan(t *testing.T) {
1485+
testCases := []struct {
1486+
remoteHeight uint64
1487+
localHeight uint64
1488+
expected []int
1489+
}{
1490+
// Remote is way higher. We should ask for the remote head and go backwards
1491+
{1500, 1000,
1492+
[]int{1323, 1339, 1355, 1371, 1387, 1403, 1419, 1435, 1451, 1467, 1483, 1499},
1493+
},
1494+
{15000, 13006,
1495+
[]int{14823, 14839, 14855, 14871, 14887, 14903, 14919, 14935, 14951, 14967, 14983, 14999},
1496+
},
1497+
//Remote is pretty close to us. We don't have to fetch as many
1498+
{1200, 1150,
1499+
[]int{1149, 1154, 1159, 1164, 1169, 1174, 1179, 1184, 1189, 1194, 1199},
1500+
},
1501+
// Remote is equal to us (so on a fork with higher td)
1502+
// We should get the closest couple of ancestors
1503+
{1500, 1500,
1504+
[]int{1497, 1499},
1505+
},
1506+
// We're higher than the remote! Odd
1507+
{1000, 1500,
1508+
[]int{997, 999},
1509+
},
1510+
// Check some weird edgecases that it behaves somewhat rationally
1511+
{0, 1500,
1512+
[]int{0, 2},
1513+
},
1514+
{6000000, 0,
1515+
[]int{5999823, 5999839, 5999855, 5999871, 5999887, 5999903, 5999919, 5999935, 5999951, 5999967, 5999983, 5999999},
1516+
},
1517+
{0, 0,
1518+
[]int{0, 2},
1519+
},
1520+
}
1521+
reqs := func(from, count, span int) []int {
1522+
var r []int
1523+
num := from
1524+
for len(r) < count {
1525+
r = append(r, num)
1526+
num += span + 1
1527+
}
1528+
return r
1529+
}
1530+
for i, tt := range testCases {
1531+
from, count, span, max := calculateRequestSpan(tt.remoteHeight, tt.localHeight)
1532+
data := reqs(int(from), count, span)
1533+
1534+
if max != uint64(data[len(data)-1]) {
1535+
t.Errorf("test %d: wrong last value %d != %d", i, data[len(data)-1], max)
1536+
}
1537+
failed := false
1538+
if len(data) != len(tt.expected) {
1539+
failed = true
1540+
t.Errorf("test %d: length wrong, expected %d got %d", i, len(tt.expected), len(data))
1541+
} else {
1542+
for j, n := range data {
1543+
if n != tt.expected[j] {
1544+
failed = true
1545+
break
1546+
}
1547+
}
1548+
}
1549+
if failed {
1550+
res := strings.Replace(fmt.Sprint(data), " ", ",", -1)
1551+
exp := strings.Replace(fmt.Sprint(tt.expected), " ", ",", -1)
1552+
fmt.Printf("got: %v\n", res)
1553+
fmt.Printf("exp: %v\n", exp)
1554+
t.Errorf("test %d: wrong values", i)
1555+
}
1556+
}
1557+
}

0 commit comments

Comments
 (0)