|
|
@ -78,26 +78,36 @@ func makeChain(n int, seed byte, parent *types.Block) ([]common.Hash, map[common |
|
|
|
type fetcherTester struct { |
|
|
|
type fetcherTester struct { |
|
|
|
fetcher *BlockFetcher |
|
|
|
fetcher *BlockFetcher |
|
|
|
|
|
|
|
|
|
|
|
hashes []common.Hash // Hash chain belonging to the tester
|
|
|
|
hashes []common.Hash // Hash chain belonging to the tester
|
|
|
|
blocks map[common.Hash]*types.Block // Blocks belonging to the tester
|
|
|
|
headers map[common.Hash]*types.Header // Headers belonging to the tester
|
|
|
|
drops map[string]bool // Map of peers dropped by the fetcher
|
|
|
|
blocks map[common.Hash]*types.Block // Blocks belonging to the tester
|
|
|
|
|
|
|
|
drops map[string]bool // Map of peers dropped by the fetcher
|
|
|
|
|
|
|
|
|
|
|
|
lock sync.RWMutex |
|
|
|
lock sync.RWMutex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// newTester creates a new fetcher test mocker.
|
|
|
|
// newTester creates a new fetcher test mocker.
|
|
|
|
func newTester() *fetcherTester { |
|
|
|
func newTester(light bool) *fetcherTester { |
|
|
|
tester := &fetcherTester{ |
|
|
|
tester := &fetcherTester{ |
|
|
|
hashes: []common.Hash{genesis.Hash()}, |
|
|
|
hashes: []common.Hash{genesis.Hash()}, |
|
|
|
blocks: map[common.Hash]*types.Block{genesis.Hash(): genesis}, |
|
|
|
headers: map[common.Hash]*types.Header{genesis.Hash(): genesis.Header()}, |
|
|
|
drops: make(map[string]bool), |
|
|
|
blocks: map[common.Hash]*types.Block{genesis.Hash(): genesis}, |
|
|
|
|
|
|
|
drops: make(map[string]bool), |
|
|
|
} |
|
|
|
} |
|
|
|
tester.fetcher = NewBlockFetcher(tester.getBlock, tester.verifyHeader, tester.broadcastBlock, tester.chainHeight, tester.insertChain, tester.dropPeer) |
|
|
|
tester.fetcher = NewBlockFetcher(light, tester.getHeader, tester.getBlock, tester.verifyHeader, tester.broadcastBlock, tester.chainHeight, tester.insertHeaders, tester.insertChain, tester.dropPeer) |
|
|
|
tester.fetcher.Start() |
|
|
|
tester.fetcher.Start() |
|
|
|
|
|
|
|
|
|
|
|
return tester |
|
|
|
return tester |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// getHeader retrieves a header from the tester's block chain.
|
|
|
|
|
|
|
|
func (f *fetcherTester) getHeader(hash common.Hash) *types.Header { |
|
|
|
|
|
|
|
f.lock.RLock() |
|
|
|
|
|
|
|
defer f.lock.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return f.headers[hash] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// getBlock retrieves a block from the tester's block chain.
|
|
|
|
// getBlock retrieves a block from the tester's block chain.
|
|
|
|
func (f *fetcherTester) getBlock(hash common.Hash) *types.Block { |
|
|
|
func (f *fetcherTester) getBlock(hash common.Hash) *types.Block { |
|
|
|
f.lock.RLock() |
|
|
|
f.lock.RLock() |
|
|
@ -120,9 +130,33 @@ func (f *fetcherTester) chainHeight() uint64 { |
|
|
|
f.lock.RLock() |
|
|
|
f.lock.RLock() |
|
|
|
defer f.lock.RUnlock() |
|
|
|
defer f.lock.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if f.fetcher.light { |
|
|
|
|
|
|
|
return f.headers[f.hashes[len(f.hashes)-1]].Number.Uint64() |
|
|
|
|
|
|
|
} |
|
|
|
return f.blocks[f.hashes[len(f.hashes)-1]].NumberU64() |
|
|
|
return f.blocks[f.hashes[len(f.hashes)-1]].NumberU64() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// insertChain injects a new headers into the simulated chain.
|
|
|
|
|
|
|
|
func (f *fetcherTester) insertHeaders(headers []*types.Header) (int, error) { |
|
|
|
|
|
|
|
f.lock.Lock() |
|
|
|
|
|
|
|
defer f.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i, header := range headers { |
|
|
|
|
|
|
|
// Make sure the parent in known
|
|
|
|
|
|
|
|
if _, ok := f.headers[header.ParentHash]; !ok { |
|
|
|
|
|
|
|
return i, errors.New("unknown parent") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Discard any new blocks if the same height already exists
|
|
|
|
|
|
|
|
if header.Number.Uint64() <= f.headers[f.hashes[len(f.hashes)-1]].Number.Uint64() { |
|
|
|
|
|
|
|
return i, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Otherwise build our current chain
|
|
|
|
|
|
|
|
f.hashes = append(f.hashes, header.Hash()) |
|
|
|
|
|
|
|
f.headers[header.Hash()] = header |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return 0, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// insertChain injects a new blocks into the simulated chain.
|
|
|
|
// insertChain injects a new blocks into the simulated chain.
|
|
|
|
func (f *fetcherTester) insertChain(blocks types.Blocks) (int, error) { |
|
|
|
func (f *fetcherTester) insertChain(blocks types.Blocks) (int, error) { |
|
|
|
f.lock.Lock() |
|
|
|
f.lock.Lock() |
|
|
@ -233,7 +267,7 @@ func verifyCompletingEvent(t *testing.T, completing chan []common.Hash, arrive b |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// verifyImportEvent verifies that one single event arrive on an import channel.
|
|
|
|
// verifyImportEvent verifies that one single event arrive on an import channel.
|
|
|
|
func verifyImportEvent(t *testing.T, imported chan *types.Block, arrive bool) { |
|
|
|
func verifyImportEvent(t *testing.T, imported chan interface{}, arrive bool) { |
|
|
|
if arrive { |
|
|
|
if arrive { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-imported: |
|
|
|
case <-imported: |
|
|
@ -251,7 +285,7 @@ func verifyImportEvent(t *testing.T, imported chan *types.Block, arrive bool) { |
|
|
|
|
|
|
|
|
|
|
|
// verifyImportCount verifies that exactly count number of events arrive on an
|
|
|
|
// verifyImportCount verifies that exactly count number of events arrive on an
|
|
|
|
// import hook channel.
|
|
|
|
// import hook channel.
|
|
|
|
func verifyImportCount(t *testing.T, imported chan *types.Block, count int) { |
|
|
|
func verifyImportCount(t *testing.T, imported chan interface{}, count int) { |
|
|
|
for i := 0; i < count; i++ { |
|
|
|
for i := 0; i < count; i++ { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-imported: |
|
|
|
case <-imported: |
|
|
@ -263,7 +297,7 @@ func verifyImportCount(t *testing.T, imported chan *types.Block, count int) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// verifyImportDone verifies that no more events are arriving on an import channel.
|
|
|
|
// verifyImportDone verifies that no more events are arriving on an import channel.
|
|
|
|
func verifyImportDone(t *testing.T, imported chan *types.Block) { |
|
|
|
func verifyImportDone(t *testing.T, imported chan interface{}) { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-imported: |
|
|
|
case <-imported: |
|
|
|
t.Fatalf("extra block imported") |
|
|
|
t.Fatalf("extra block imported") |
|
|
@ -271,45 +305,62 @@ func verifyImportDone(t *testing.T, imported chan *types.Block) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that a fetcher accepts block announcements and initiates retrievals for
|
|
|
|
// verifyChainHeight verifies the chain height is as expected.
|
|
|
|
// them, successfully importing into the local chain.
|
|
|
|
func verifyChainHeight(t *testing.T, fetcher *fetcherTester, height uint64) { |
|
|
|
func TestSequentialAnnouncements62(t *testing.T) { testSequentialAnnouncements(t, 62) } |
|
|
|
if fetcher.chainHeight() != height { |
|
|
|
func TestSequentialAnnouncements63(t *testing.T) { testSequentialAnnouncements(t, 63) } |
|
|
|
t.Fatalf("chain height mismatch, got %d, want %d", fetcher.chainHeight(), height) |
|
|
|
func TestSequentialAnnouncements64(t *testing.T) { testSequentialAnnouncements(t, 64) } |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func testSequentialAnnouncements(t *testing.T, protocol int) { |
|
|
|
// Tests that a fetcher accepts block/header announcements and initiates retrievals
|
|
|
|
|
|
|
|
// for them, successfully importing into the local chain.
|
|
|
|
|
|
|
|
func TestFullSequentialAnnouncements(t *testing.T) { testSequentialAnnouncements(t, false) } |
|
|
|
|
|
|
|
func TestLightSequentialAnnouncements(t *testing.T) { testSequentialAnnouncements(t, true) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testSequentialAnnouncements(t *testing.T, light bool) { |
|
|
|
// Create a chain of blocks to import
|
|
|
|
// Create a chain of blocks to import
|
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
imported := make(chan *types.Block) |
|
|
|
imported := make(chan interface{}) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
if header == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty header") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- header |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
verifyImportEvent(t, imported, true) |
|
|
|
verifyImportEvent(t, imported, true) |
|
|
|
} |
|
|
|
} |
|
|
|
verifyImportDone(t, imported) |
|
|
|
verifyImportDone(t, imported) |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, uint64(len(hashes)-1)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that if blocks are announced by multiple peers (or even the same buggy
|
|
|
|
// Tests that if blocks are announced by multiple peers (or even the same buggy
|
|
|
|
// peer), they will only get downloaded at most once.
|
|
|
|
// peer), they will only get downloaded at most once.
|
|
|
|
func TestConcurrentAnnouncements62(t *testing.T) { testConcurrentAnnouncements(t, 62) } |
|
|
|
func TestFullConcurrentAnnouncements(t *testing.T) { testConcurrentAnnouncements(t, false) } |
|
|
|
func TestConcurrentAnnouncements63(t *testing.T) { testConcurrentAnnouncements(t, 63) } |
|
|
|
func TestLightConcurrentAnnouncements(t *testing.T) { testConcurrentAnnouncements(t, true) } |
|
|
|
func TestConcurrentAnnouncements64(t *testing.T) { testConcurrentAnnouncements(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testConcurrentAnnouncements(t *testing.T, protocol int) { |
|
|
|
func testConcurrentAnnouncements(t *testing.T, light bool) { |
|
|
|
// Create a chain of blocks to import
|
|
|
|
// Create a chain of blocks to import
|
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
// Assemble a tester with a built in counter for the requests
|
|
|
|
// Assemble a tester with a built in counter for the requests
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
firstHeaderFetcher := tester.makeHeaderFetcher("first", blocks, -gatherSlack) |
|
|
|
firstHeaderFetcher := tester.makeHeaderFetcher("first", blocks, -gatherSlack) |
|
|
|
firstBodyFetcher := tester.makeBodyFetcher("first", blocks, 0) |
|
|
|
firstBodyFetcher := tester.makeBodyFetcher("first", blocks, 0) |
|
|
|
secondHeaderFetcher := tester.makeHeaderFetcher("second", blocks, -gatherSlack) |
|
|
|
secondHeaderFetcher := tester.makeHeaderFetcher("second", blocks, -gatherSlack) |
|
|
@ -325,9 +376,20 @@ func testConcurrentAnnouncements(t *testing.T, protocol int) { |
|
|
|
return secondHeaderFetcher(hash) |
|
|
|
return secondHeaderFetcher(hash) |
|
|
|
} |
|
|
|
} |
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
imported := make(chan *types.Block) |
|
|
|
imported := make(chan interface{}) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
if header == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty header") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- header |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
tester.fetcher.Notify("first", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), firstHeaderWrapper, firstBodyFetcher) |
|
|
|
tester.fetcher.Notify("first", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), firstHeaderWrapper, firstBodyFetcher) |
|
|
|
tester.fetcher.Notify("second", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout+time.Millisecond), secondHeaderWrapper, secondBodyFetcher) |
|
|
|
tester.fetcher.Notify("second", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout+time.Millisecond), secondHeaderWrapper, secondBodyFetcher) |
|
|
@ -340,30 +402,42 @@ func testConcurrentAnnouncements(t *testing.T, protocol int) { |
|
|
|
if int(counter) != targetBlocks { |
|
|
|
if int(counter) != targetBlocks { |
|
|
|
t.Fatalf("retrieval count mismatch: have %v, want %v", counter, targetBlocks) |
|
|
|
t.Fatalf("retrieval count mismatch: have %v, want %v", counter, targetBlocks) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, uint64(len(hashes)-1)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that announcements arriving while a previous is being fetched still
|
|
|
|
// Tests that announcements arriving while a previous is being fetched still
|
|
|
|
// results in a valid import.
|
|
|
|
// results in a valid import.
|
|
|
|
func TestOverlappingAnnouncements62(t *testing.T) { testOverlappingAnnouncements(t, 62) } |
|
|
|
func TestFullOverlappingAnnouncements(t *testing.T) { testOverlappingAnnouncements(t, false) } |
|
|
|
func TestOverlappingAnnouncements63(t *testing.T) { testOverlappingAnnouncements(t, 63) } |
|
|
|
func TestLightOverlappingAnnouncements(t *testing.T) { testOverlappingAnnouncements(t, true) } |
|
|
|
func TestOverlappingAnnouncements64(t *testing.T) { testOverlappingAnnouncements(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testOverlappingAnnouncements(t *testing.T, protocol int) { |
|
|
|
func testOverlappingAnnouncements(t *testing.T, light bool) { |
|
|
|
// Create a chain of blocks to import
|
|
|
|
// Create a chain of blocks to import
|
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
targetBlocks := 4 * hashLimit |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
|
// Iteratively announce blocks, but overlap them continuously
|
|
|
|
// Iteratively announce blocks, but overlap them continuously
|
|
|
|
overlap := 16 |
|
|
|
overlap := 16 |
|
|
|
imported := make(chan *types.Block, len(hashes)-1) |
|
|
|
imported := make(chan interface{}, len(hashes)-1) |
|
|
|
for i := 0; i < overlap; i++ { |
|
|
|
for i := 0; i < overlap; i++ { |
|
|
|
imported <- nil |
|
|
|
imported <- nil |
|
|
|
} |
|
|
|
} |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
if header == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty header") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- header |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
@ -375,19 +449,19 @@ func testOverlappingAnnouncements(t *testing.T, protocol int) { |
|
|
|
} |
|
|
|
} |
|
|
|
// Wait for all the imports to complete and check count
|
|
|
|
// Wait for all the imports to complete and check count
|
|
|
|
verifyImportCount(t, imported, overlap) |
|
|
|
verifyImportCount(t, imported, overlap) |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, uint64(len(hashes)-1)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that announces already being retrieved will not be duplicated.
|
|
|
|
// Tests that announces already being retrieved will not be duplicated.
|
|
|
|
func TestPendingDeduplication62(t *testing.T) { testPendingDeduplication(t, 62) } |
|
|
|
func TestFullPendingDeduplication(t *testing.T) { testPendingDeduplication(t, false) } |
|
|
|
func TestPendingDeduplication63(t *testing.T) { testPendingDeduplication(t, 63) } |
|
|
|
func TestLightPendingDeduplication(t *testing.T) { testPendingDeduplication(t, true) } |
|
|
|
func TestPendingDeduplication64(t *testing.T) { testPendingDeduplication(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testPendingDeduplication(t *testing.T, protocol int) { |
|
|
|
func testPendingDeduplication(t *testing.T, light bool) { |
|
|
|
// Create a hash and corresponding block
|
|
|
|
// Create a hash and corresponding block
|
|
|
|
hashes, blocks := makeChain(1, 0, genesis) |
|
|
|
hashes, blocks := makeChain(1, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
// Assemble a tester with a built in counter and delayed fetcher
|
|
|
|
// Assemble a tester with a built in counter and delayed fetcher
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("repeater", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("repeater", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("repeater", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("repeater", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
@ -403,42 +477,58 @@ func testPendingDeduplication(t *testing.T, protocol int) { |
|
|
|
}() |
|
|
|
}() |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
checkNonExist := func() bool { |
|
|
|
|
|
|
|
return tester.getBlock(hashes[0]) == nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
checkNonExist = func() bool { |
|
|
|
|
|
|
|
return tester.getHeader(hashes[0]) == nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// Announce the same block many times until it's fetched (wait for any pending ops)
|
|
|
|
// Announce the same block many times until it's fetched (wait for any pending ops)
|
|
|
|
for tester.getBlock(hashes[0]) == nil { |
|
|
|
for checkNonExist() { |
|
|
|
tester.fetcher.Notify("repeater", hashes[0], 1, time.Now().Add(-arriveTimeout), headerWrapper, bodyFetcher) |
|
|
|
tester.fetcher.Notify("repeater", hashes[0], 1, time.Now().Add(-arriveTimeout), headerWrapper, bodyFetcher) |
|
|
|
time.Sleep(time.Millisecond) |
|
|
|
time.Sleep(time.Millisecond) |
|
|
|
} |
|
|
|
} |
|
|
|
time.Sleep(delay) |
|
|
|
time.Sleep(delay) |
|
|
|
|
|
|
|
|
|
|
|
// Check that all blocks were imported and none fetched twice
|
|
|
|
// Check that all blocks were imported and none fetched twice
|
|
|
|
if imported := len(tester.blocks); imported != 2 { |
|
|
|
|
|
|
|
t.Fatalf("synchronised block mismatch: have %v, want %v", imported, 2) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if int(counter) != 1 { |
|
|
|
if int(counter) != 1 { |
|
|
|
t.Fatalf("retrieval count mismatch: have %v, want %v", counter, 1) |
|
|
|
t.Fatalf("retrieval count mismatch: have %v, want %v", counter, 1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, 1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that announcements retrieved in a random order are cached and eventually
|
|
|
|
// Tests that announcements retrieved in a random order are cached and eventually
|
|
|
|
// imported when all the gaps are filled in.
|
|
|
|
// imported when all the gaps are filled in.
|
|
|
|
func TestRandomArrivalImport62(t *testing.T) { testRandomArrivalImport(t, 62) } |
|
|
|
func TestFullRandomArrivalImport(t *testing.T) { testRandomArrivalImport(t, false) } |
|
|
|
func TestRandomArrivalImport63(t *testing.T) { testRandomArrivalImport(t, 63) } |
|
|
|
func TestLightRandomArrivalImport(t *testing.T) { testRandomArrivalImport(t, true) } |
|
|
|
func TestRandomArrivalImport64(t *testing.T) { testRandomArrivalImport(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testRandomArrivalImport(t *testing.T, protocol int) { |
|
|
|
func testRandomArrivalImport(t *testing.T, light bool) { |
|
|
|
// Create a chain of blocks to import, and choose one to delay
|
|
|
|
// Create a chain of blocks to import, and choose one to delay
|
|
|
|
targetBlocks := maxQueueDist |
|
|
|
targetBlocks := maxQueueDist |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
skip := targetBlocks / 2 |
|
|
|
skip := targetBlocks / 2 |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
|
// Iteratively announce blocks, skipping one entry
|
|
|
|
// Iteratively announce blocks, skipping one entry
|
|
|
|
imported := make(chan *types.Block, len(hashes)-1) |
|
|
|
imported := make(chan interface{}, len(hashes)-1) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
if header == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty header") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- header |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
for i := len(hashes) - 1; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 1; i >= 0; i-- { |
|
|
|
if i != skip { |
|
|
|
if i != skip { |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
@ -448,27 +538,24 @@ func testRandomArrivalImport(t *testing.T, protocol int) { |
|
|
|
// Finally announce the skipped entry and check full import
|
|
|
|
// Finally announce the skipped entry and check full import
|
|
|
|
tester.fetcher.Notify("valid", hashes[skip], uint64(len(hashes)-skip-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[skip], uint64(len(hashes)-skip-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
verifyImportCount(t, imported, len(hashes)-1) |
|
|
|
verifyImportCount(t, imported, len(hashes)-1) |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, uint64(len(hashes)-1)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that direct block enqueues (due to block propagation vs. hash announce)
|
|
|
|
// Tests that direct block enqueues (due to block propagation vs. hash announce)
|
|
|
|
// are correctly schedule, filling and import queue gaps.
|
|
|
|
// are correctly schedule, filling and import queue gaps.
|
|
|
|
func TestQueueGapFill62(t *testing.T) { testQueueGapFill(t, 62) } |
|
|
|
func TestQueueGapFill(t *testing.T) { |
|
|
|
func TestQueueGapFill63(t *testing.T) { testQueueGapFill(t, 63) } |
|
|
|
|
|
|
|
func TestQueueGapFill64(t *testing.T) { testQueueGapFill(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testQueueGapFill(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
// Create a chain of blocks to import, and choose one to not announce at all
|
|
|
|
// Create a chain of blocks to import, and choose one to not announce at all
|
|
|
|
targetBlocks := maxQueueDist |
|
|
|
targetBlocks := maxQueueDist |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
hashes, blocks := makeChain(targetBlocks, 0, genesis) |
|
|
|
skip := targetBlocks / 2 |
|
|
|
skip := targetBlocks / 2 |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
|
// Iteratively announce blocks, skipping one entry
|
|
|
|
// Iteratively announce blocks, skipping one entry
|
|
|
|
imported := make(chan *types.Block, len(hashes)-1) |
|
|
|
imported := make(chan interface{}, len(hashes)-1) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { imported <- block } |
|
|
|
|
|
|
|
|
|
|
|
for i := len(hashes) - 1; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 1; i >= 0; i-- { |
|
|
|
if i != skip { |
|
|
|
if i != skip { |
|
|
@ -479,20 +566,17 @@ func testQueueGapFill(t *testing.T, protocol int) { |
|
|
|
// Fill the missing block directly as if propagated
|
|
|
|
// Fill the missing block directly as if propagated
|
|
|
|
tester.fetcher.Enqueue("valid", blocks[hashes[skip]]) |
|
|
|
tester.fetcher.Enqueue("valid", blocks[hashes[skip]]) |
|
|
|
verifyImportCount(t, imported, len(hashes)-1) |
|
|
|
verifyImportCount(t, imported, len(hashes)-1) |
|
|
|
|
|
|
|
verifyChainHeight(t, tester, uint64(len(hashes)-1)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Tests that blocks arriving from various sources (multiple propagations, hash
|
|
|
|
// Tests that blocks arriving from various sources (multiple propagations, hash
|
|
|
|
// announces, etc) do not get scheduled for import multiple times.
|
|
|
|
// announces, etc) do not get scheduled for import multiple times.
|
|
|
|
func TestImportDeduplication62(t *testing.T) { testImportDeduplication(t, 62) } |
|
|
|
func TestImportDeduplication(t *testing.T) { |
|
|
|
func TestImportDeduplication63(t *testing.T) { testImportDeduplication(t, 63) } |
|
|
|
|
|
|
|
func TestImportDeduplication64(t *testing.T) { testImportDeduplication(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testImportDeduplication(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
// Create two blocks to import (one for duplication, the other for stalling)
|
|
|
|
// Create two blocks to import (one for duplication, the other for stalling)
|
|
|
|
hashes, blocks := makeChain(2, 0, genesis) |
|
|
|
hashes, blocks := makeChain(2, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
// Create the tester and wrap the importer with a counter
|
|
|
|
// Create the tester and wrap the importer with a counter
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
@ -503,9 +587,9 @@ func testImportDeduplication(t *testing.T, protocol int) { |
|
|
|
} |
|
|
|
} |
|
|
|
// Instrument the fetching and imported events
|
|
|
|
// Instrument the fetching and imported events
|
|
|
|
fetching := make(chan []common.Hash) |
|
|
|
fetching := make(chan []common.Hash) |
|
|
|
imported := make(chan *types.Block, len(hashes)-1) |
|
|
|
imported := make(chan interface{}, len(hashes)-1) |
|
|
|
tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes } |
|
|
|
tester.fetcher.fetchingHook = func(hashes []common.Hash) { fetching <- hashes } |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { imported <- block } |
|
|
|
|
|
|
|
|
|
|
|
// Announce the duplicating block, wait for retrieval, and also propagate directly
|
|
|
|
// Announce the duplicating block, wait for retrieval, and also propagate directly
|
|
|
|
tester.fetcher.Notify("valid", hashes[0], 1, time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[0], 1, time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
@ -534,7 +618,7 @@ func TestDistantPropagationDiscarding(t *testing.T) { |
|
|
|
low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1 |
|
|
|
low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1 |
|
|
|
|
|
|
|
|
|
|
|
// Create a tester and simulate a head block being the middle of the above chain
|
|
|
|
// Create a tester and simulate a head block being the middle of the above chain
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
|
|
|
|
|
|
|
|
tester.lock.Lock() |
|
|
|
tester.lock.Lock() |
|
|
|
tester.hashes = []common.Hash{head} |
|
|
|
tester.hashes = []common.Hash{head} |
|
|
@ -558,11 +642,10 @@ func TestDistantPropagationDiscarding(t *testing.T) { |
|
|
|
// Tests that announcements with numbers much lower or higher than out current
|
|
|
|
// Tests that announcements with numbers much lower or higher than out current
|
|
|
|
// head get discarded to prevent wasting resources on useless blocks from faulty
|
|
|
|
// head get discarded to prevent wasting resources on useless blocks from faulty
|
|
|
|
// peers.
|
|
|
|
// peers.
|
|
|
|
func TestDistantAnnouncementDiscarding62(t *testing.T) { testDistantAnnouncementDiscarding(t, 62) } |
|
|
|
func TestFullDistantAnnouncementDiscarding(t *testing.T) { testDistantAnnouncementDiscarding(t, false) } |
|
|
|
func TestDistantAnnouncementDiscarding63(t *testing.T) { testDistantAnnouncementDiscarding(t, 63) } |
|
|
|
func TestLightDistantAnnouncementDiscarding(t *testing.T) { testDistantAnnouncementDiscarding(t, true) } |
|
|
|
func TestDistantAnnouncementDiscarding64(t *testing.T) { testDistantAnnouncementDiscarding(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testDistantAnnouncementDiscarding(t *testing.T, protocol int) { |
|
|
|
func testDistantAnnouncementDiscarding(t *testing.T, light bool) { |
|
|
|
// Create a long chain to import and define the discard boundaries
|
|
|
|
// Create a long chain to import and define the discard boundaries
|
|
|
|
hashes, blocks := makeChain(3*maxQueueDist, 0, genesis) |
|
|
|
hashes, blocks := makeChain(3*maxQueueDist, 0, genesis) |
|
|
|
head := hashes[len(hashes)/2] |
|
|
|
head := hashes[len(hashes)/2] |
|
|
@ -570,10 +653,11 @@ func testDistantAnnouncementDiscarding(t *testing.T, protocol int) { |
|
|
|
low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1 |
|
|
|
low, high := len(hashes)/2+maxUncleDist+1, len(hashes)/2-maxQueueDist-1 |
|
|
|
|
|
|
|
|
|
|
|
// Create a tester and simulate a head block being the middle of the above chain
|
|
|
|
// Create a tester and simulate a head block being the middle of the above chain
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
|
|
|
|
|
|
|
|
tester.lock.Lock() |
|
|
|
tester.lock.Lock() |
|
|
|
tester.hashes = []common.Hash{head} |
|
|
|
tester.hashes = []common.Hash{head} |
|
|
|
|
|
|
|
tester.headers = map[common.Hash]*types.Header{head: blocks[head].Header()} |
|
|
|
tester.blocks = map[common.Hash]*types.Block{head: blocks[head]} |
|
|
|
tester.blocks = map[common.Hash]*types.Block{head: blocks[head]} |
|
|
|
tester.lock.Unlock() |
|
|
|
tester.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
@ -601,21 +685,31 @@ func testDistantAnnouncementDiscarding(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
|
|
|
|
// Tests that peers announcing blocks with invalid numbers (i.e. not matching
|
|
|
|
// Tests that peers announcing blocks with invalid numbers (i.e. not matching
|
|
|
|
// the headers provided afterwards) get dropped as malicious.
|
|
|
|
// the headers provided afterwards) get dropped as malicious.
|
|
|
|
func TestInvalidNumberAnnouncement62(t *testing.T) { testInvalidNumberAnnouncement(t, 62) } |
|
|
|
func TestFullInvalidNumberAnnouncement(t *testing.T) { testInvalidNumberAnnouncement(t, false) } |
|
|
|
func TestInvalidNumberAnnouncement63(t *testing.T) { testInvalidNumberAnnouncement(t, 63) } |
|
|
|
func TestLightInvalidNumberAnnouncement(t *testing.T) { testInvalidNumberAnnouncement(t, true) } |
|
|
|
func TestInvalidNumberAnnouncement64(t *testing.T) { testInvalidNumberAnnouncement(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testInvalidNumberAnnouncement(t *testing.T, protocol int) { |
|
|
|
func testInvalidNumberAnnouncement(t *testing.T, light bool) { |
|
|
|
// Create a single block to import and check numbers against
|
|
|
|
// Create a single block to import and check numbers against
|
|
|
|
hashes, blocks := makeChain(1, 0, genesis) |
|
|
|
hashes, blocks := makeChain(1, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(light) |
|
|
|
badHeaderFetcher := tester.makeHeaderFetcher("bad", blocks, -gatherSlack) |
|
|
|
badHeaderFetcher := tester.makeHeaderFetcher("bad", blocks, -gatherSlack) |
|
|
|
badBodyFetcher := tester.makeBodyFetcher("bad", blocks, 0) |
|
|
|
badBodyFetcher := tester.makeBodyFetcher("bad", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
|
imported := make(chan *types.Block) |
|
|
|
imported := make(chan interface{}) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if light { |
|
|
|
|
|
|
|
if header == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty header") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- header |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// Announce a block with a bad number, check for immediate drop
|
|
|
|
// Announce a block with a bad number, check for immediate drop
|
|
|
|
tester.fetcher.Notify("bad", hashes[0], 2, time.Now().Add(-arriveTimeout), badHeaderFetcher, badBodyFetcher) |
|
|
|
tester.fetcher.Notify("bad", hashes[0], 2, time.Now().Add(-arriveTimeout), badHeaderFetcher, badBodyFetcher) |
|
|
|
verifyImportEvent(t, imported, false) |
|
|
|
verifyImportEvent(t, imported, false) |
|
|
@ -646,15 +740,11 @@ func testInvalidNumberAnnouncement(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
|
|
|
|
// Tests that if a block is empty (i.e. header only), no body request should be
|
|
|
|
// Tests that if a block is empty (i.e. header only), no body request should be
|
|
|
|
// made, and instead the header should be assembled into a whole block in itself.
|
|
|
|
// made, and instead the header should be assembled into a whole block in itself.
|
|
|
|
func TestEmptyBlockShortCircuit62(t *testing.T) { testEmptyBlockShortCircuit(t, 62) } |
|
|
|
func TestEmptyBlockShortCircuit(t *testing.T) { |
|
|
|
func TestEmptyBlockShortCircuit63(t *testing.T) { testEmptyBlockShortCircuit(t, 63) } |
|
|
|
|
|
|
|
func TestEmptyBlockShortCircuit64(t *testing.T) { testEmptyBlockShortCircuit(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testEmptyBlockShortCircuit(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
// Create a chain of blocks to import
|
|
|
|
// Create a chain of blocks to import
|
|
|
|
hashes, blocks := makeChain(32, 0, genesis) |
|
|
|
hashes, blocks := makeChain(32, 0, genesis) |
|
|
|
|
|
|
|
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
headerFetcher := tester.makeHeaderFetcher("valid", blocks, -gatherSlack) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
bodyFetcher := tester.makeBodyFetcher("valid", blocks, 0) |
|
|
|
|
|
|
|
|
|
|
@ -665,9 +755,13 @@ func testEmptyBlockShortCircuit(t *testing.T, protocol int) { |
|
|
|
completing := make(chan []common.Hash) |
|
|
|
completing := make(chan []common.Hash) |
|
|
|
tester.fetcher.completingHook = func(hashes []common.Hash) { completing <- hashes } |
|
|
|
tester.fetcher.completingHook = func(hashes []common.Hash) { completing <- hashes } |
|
|
|
|
|
|
|
|
|
|
|
imported := make(chan *types.Block) |
|
|
|
imported := make(chan interface{}) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { |
|
|
|
|
|
|
|
if block == nil { |
|
|
|
|
|
|
|
t.Fatalf("Fetcher try to import empty block") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
imported <- block |
|
|
|
|
|
|
|
} |
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
// Iteratively announce blocks until all are imported
|
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
for i := len(hashes) - 2; i >= 0; i-- { |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
|
tester.fetcher.Notify("valid", hashes[i], uint64(len(hashes)-i-1), time.Now().Add(-arriveTimeout), headerFetcher, bodyFetcher) |
|
|
@ -687,16 +781,12 @@ func testEmptyBlockShortCircuit(t *testing.T, protocol int) { |
|
|
|
// Tests that a peer is unable to use unbounded memory with sending infinite
|
|
|
|
// Tests that a peer is unable to use unbounded memory with sending infinite
|
|
|
|
// block announcements to a node, but that even in the face of such an attack,
|
|
|
|
// block announcements to a node, but that even in the face of such an attack,
|
|
|
|
// the fetcher remains operational.
|
|
|
|
// the fetcher remains operational.
|
|
|
|
func TestHashMemoryExhaustionAttack62(t *testing.T) { testHashMemoryExhaustionAttack(t, 62) } |
|
|
|
func TestHashMemoryExhaustionAttack(t *testing.T) { |
|
|
|
func TestHashMemoryExhaustionAttack63(t *testing.T) { testHashMemoryExhaustionAttack(t, 63) } |
|
|
|
|
|
|
|
func TestHashMemoryExhaustionAttack64(t *testing.T) { testHashMemoryExhaustionAttack(t, 64) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func testHashMemoryExhaustionAttack(t *testing.T, protocol int) { |
|
|
|
|
|
|
|
// Create a tester with instrumented import hooks
|
|
|
|
// Create a tester with instrumented import hooks
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
|
|
|
|
|
|
|
|
imported, announces := make(chan *types.Block), int32(0) |
|
|
|
imported, announces := make(chan interface{}), int32(0) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.announceChangeHook = func(hash common.Hash, added bool) { |
|
|
|
tester.fetcher.announceChangeHook = func(hash common.Hash, added bool) { |
|
|
|
if added { |
|
|
|
if added { |
|
|
|
atomic.AddInt32(&announces, 1) |
|
|
|
atomic.AddInt32(&announces, 1) |
|
|
@ -740,10 +830,10 @@ func testHashMemoryExhaustionAttack(t *testing.T, protocol int) { |
|
|
|
// system memory.
|
|
|
|
// system memory.
|
|
|
|
func TestBlockMemoryExhaustionAttack(t *testing.T) { |
|
|
|
func TestBlockMemoryExhaustionAttack(t *testing.T) { |
|
|
|
// Create a tester with instrumented import hooks
|
|
|
|
// Create a tester with instrumented import hooks
|
|
|
|
tester := newTester() |
|
|
|
tester := newTester(false) |
|
|
|
|
|
|
|
|
|
|
|
imported, enqueued := make(chan *types.Block), int32(0) |
|
|
|
imported, enqueued := make(chan interface{}), int32(0) |
|
|
|
tester.fetcher.importedHook = func(block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.importedHook = func(header *types.Header, block *types.Block) { imported <- block } |
|
|
|
tester.fetcher.queueChangeHook = func(hash common.Hash, added bool) { |
|
|
|
tester.fetcher.queueChangeHook = func(hash common.Hash, added bool) { |
|
|
|
if added { |
|
|
|
if added { |
|
|
|
atomic.AddInt32(&enqueued, 1) |
|
|
|
atomic.AddInt32(&enqueued, 1) |
|
|
|