2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-04-12 10:38:25 +00:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2015-06-11 15:13:13 +00:00
|
|
|
"fmt"
|
2018-11-12 13:18:56 +00:00
|
|
|
"strings"
|
2015-09-09 16:02:54 +00:00
|
|
|
"sync"
|
2015-04-12 10:38:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-09-11 15:36:59 +00:00
|
|
|
const OverwriteBlockCacheItems = 1024
|
|
|
|
const OverwriteMaxForkAncestry = 3000
|
2020-07-21 01:58:00 +00:00
|
|
|
|
2016-05-13 10:12:13 +00:00
|
|
|
// Reduce some of the parameters to make the tester faster.
|
2016-02-23 10:32:09 +00:00
|
|
|
func init() {
|
2020-09-11 15:36:59 +00:00
|
|
|
blockCacheMaxItems = OverwriteBlockCacheItems
|
2020-07-21 01:58:00 +00:00
|
|
|
fsHeaderSafetyNet = 256
|
|
|
|
fsHeaderContCheck = 50 * time.Millisecond
|
2020-09-11 15:36:59 +00:00
|
|
|
fullMaxForkAncestry = OverwriteMaxForkAncestry
|
2020-08-07 12:25:40 +00:00
|
|
|
fsHeaderContCheck = 500 * time.Millisecond
|
2016-10-31 11:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 13:18:56 +00:00
|
|
|
func TestRemoteHeaderRequestSpan(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
remoteHeight uint64
|
|
|
|
localHeight uint64
|
|
|
|
expected []int
|
|
|
|
}{
|
|
|
|
// Remote is way higher. We should ask for the remote head and go backwards
|
|
|
|
{1500, 1000,
|
|
|
|
[]int{1323, 1339, 1355, 1371, 1387, 1403, 1419, 1435, 1451, 1467, 1483, 1499},
|
|
|
|
},
|
|
|
|
{15000, 13006,
|
|
|
|
[]int{14823, 14839, 14855, 14871, 14887, 14903, 14919, 14935, 14951, 14967, 14983, 14999},
|
|
|
|
},
|
2020-10-13 08:58:41 +00:00
|
|
|
// Remote is pretty close to us. We don't have to fetch as many
|
2018-11-12 13:18:56 +00:00
|
|
|
{1200, 1150,
|
|
|
|
[]int{1149, 1154, 1159, 1164, 1169, 1174, 1179, 1184, 1189, 1194, 1199},
|
|
|
|
},
|
|
|
|
// Remote is equal to us (so on a fork with higher td)
|
|
|
|
// We should get the closest couple of ancestors
|
|
|
|
{1500, 1500,
|
|
|
|
[]int{1497, 1499},
|
|
|
|
},
|
|
|
|
// We're higher than the remote! Odd
|
|
|
|
{1000, 1500,
|
|
|
|
[]int{997, 999},
|
|
|
|
},
|
|
|
|
// Check some weird edgecases that it behaves somewhat rationally
|
|
|
|
{0, 1500,
|
|
|
|
[]int{0, 2},
|
|
|
|
},
|
|
|
|
{6000000, 0,
|
|
|
|
[]int{5999823, 5999839, 5999855, 5999871, 5999887, 5999903, 5999919, 5999935, 5999951, 5999967, 5999983, 5999999},
|
|
|
|
},
|
|
|
|
{0, 0,
|
|
|
|
[]int{0, 2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reqs := func(from, count, span int) []int {
|
|
|
|
var r []int
|
|
|
|
num := from
|
|
|
|
for len(r) < count {
|
|
|
|
r = append(r, num)
|
|
|
|
num += span + 1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
for i, tt := range testCases {
|
|
|
|
from, count, span, max := calculateRequestSpan(tt.remoteHeight, tt.localHeight)
|
|
|
|
data := reqs(int(from), count, span)
|
|
|
|
|
|
|
|
if max != uint64(data[len(data)-1]) {
|
|
|
|
t.Errorf("test %d: wrong last value %d != %d", i, data[len(data)-1], max)
|
|
|
|
}
|
|
|
|
failed := false
|
|
|
|
if len(data) != len(tt.expected) {
|
|
|
|
failed = true
|
|
|
|
t.Errorf("test %d: length wrong, expected %d got %d", i, len(tt.expected), len(data))
|
|
|
|
} else {
|
|
|
|
for j, n := range data {
|
|
|
|
if n != tt.expected[j] {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
res := strings.Replace(fmt.Sprint(data), " ", ",", -1)
|
|
|
|
exp := strings.Replace(fmt.Sprint(tt.expected), " ", ",", -1)
|
2019-07-17 11:20:24 +00:00
|
|
|
t.Logf("got: %v\n", res)
|
|
|
|
t.Logf("exp: %v\n", exp)
|
2018-11-12 13:18:56 +00:00
|
|
|
t.Errorf("test %d: wrong values", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 10:20:38 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func TestDataRace(t *testing.T) {
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
const N = 10
|
|
|
|
wg.Add(N)
|
2020-10-02 13:08:28 +00:00
|
|
|
ln := getTestChainBase().len()
|
2019-05-27 13:51:49 +00:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go makeFork(wg, ln, i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeFork(wg *sync.WaitGroup, ln, i int) {
|
2020-10-02 13:08:28 +00:00
|
|
|
getTestChainBase().makeFork(ln, false, uint8(i))
|
2019-05-27 13:51:49 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|