2017-03-05 15:52:03 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-08-15 20:25:46 +00:00
|
|
|
"context"
|
2021-03-19 12:32:57 +00:00
|
|
|
"errors"
|
2017-08-03 16:25:06 +00:00
|
|
|
"fmt"
|
2017-03-05 15:52:03 +00:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-03-05 15:52:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// Runs multiple tests with randomized parameters.
|
2017-03-05 15:52:03 +00:00
|
|
|
func TestChainIndexerSingle(t *testing.T) {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
testChainIndexer(t, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// Runs multiple tests with randomized parameters and different number of
|
|
|
|
// chain backends.
|
2017-03-05 15:52:03 +00:00
|
|
|
func TestChainIndexerWithChildren(t *testing.T) {
|
|
|
|
for i := 2; i < 8; i++ {
|
|
|
|
testChainIndexer(t, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// testChainIndexer runs a test with either a single chain indexer or a chain of
|
|
|
|
// multiple backends. The section size and required confirmation count parameters
|
|
|
|
// are randomized.
|
|
|
|
func testChainIndexer(t *testing.T, count int) {
|
2018-09-24 12:57:49 +00:00
|
|
|
db := rawdb.NewMemoryDatabase()
|
2017-08-03 16:25:06 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create a chain of indexers and ensure they all report empty
|
|
|
|
backends := make([]*testChainIndexBackend, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
var (
|
|
|
|
sectionSize = uint64(rand.Intn(100) + 1)
|
|
|
|
confirmsReq = uint64(rand.Intn(10))
|
|
|
|
)
|
|
|
|
backends[i] = &testChainIndexBackend{t: t, processCh: make(chan uint64)}
|
2018-09-24 12:57:49 +00:00
|
|
|
backends[i].indexer = NewChainIndexer(db, rawdb.NewTable(db, string([]byte{byte(i)})), backends[i], sectionSize, confirmsReq, 0, fmt.Sprintf("indexer-%d", i))
|
2017-08-03 16:25:06 +00:00
|
|
|
|
|
|
|
if sections, _, _ := backends[i].indexer.Sections(); sections != 0 {
|
|
|
|
t.Fatalf("Canonical section count mismatch: have %v, want %v", sections, 0)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
if i > 0 {
|
|
|
|
backends[i-1].indexer.AddChildIndexer(backends[i].indexer)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 19:52:20 +00:00
|
|
|
defer backends[0].indexer.Close() // parent indexer shuts down children
|
2017-08-03 16:25:06 +00:00
|
|
|
// notify pings the root indexer about a new head or reorg, then expect
|
|
|
|
// processed blocks if a section is processable
|
|
|
|
notify := func(headNum, failNum uint64, reorg bool) {
|
|
|
|
backends[0].indexer.newHead(headNum, reorg)
|
|
|
|
if reorg {
|
|
|
|
for _, backend := range backends {
|
|
|
|
headNum = backend.reorg(headNum)
|
|
|
|
backend.assertSections()
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var cascade bool
|
|
|
|
for _, backend := range backends {
|
|
|
|
headNum, cascade = backend.assertBlocks(headNum, failNum)
|
|
|
|
if !cascade {
|
|
|
|
break
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
backend.assertSections()
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
// inject inserts a new random canonical header into the database directly
|
|
|
|
inject := func(number uint64) {
|
|
|
|
header := &types.Header{Number: big.NewInt(int64(number)), Extra: big.NewInt(rand.Int63()).Bytes()}
|
|
|
|
if number > 0 {
|
2018-05-07 11:35:06 +00:00
|
|
|
header.ParentHash = rawdb.ReadCanonicalHash(db, number-1)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2018-05-07 11:35:06 +00:00
|
|
|
rawdb.WriteHeader(db, header)
|
|
|
|
rawdb.WriteCanonicalHash(db, header.Hash(), number)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
// Start indexer with an already existing chain
|
2017-03-05 15:52:03 +00:00
|
|
|
for i := uint64(0); i <= 100; i++ {
|
2017-08-03 16:25:06 +00:00
|
|
|
inject(i)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
notify(100, 100, false)
|
2017-08-03 16:25:06 +00:00
|
|
|
|
|
|
|
// Add new blocks one by one
|
2017-03-05 15:52:03 +00:00
|
|
|
for i := uint64(101); i <= 1000; i++ {
|
2017-08-03 16:25:06 +00:00
|
|
|
inject(i)
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(i, i, false)
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
// Do a reorg
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(500, 500, true)
|
2017-08-03 16:25:06 +00:00
|
|
|
|
|
|
|
// Create new fork
|
2017-03-05 15:52:03 +00:00
|
|
|
for i := uint64(501); i <= 1000; i++ {
|
2017-08-03 16:25:06 +00:00
|
|
|
inject(i)
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(i, i, false)
|
|
|
|
}
|
|
|
|
for i := uint64(1001); i <= 1500; i++ {
|
2017-08-03 16:25:06 +00:00
|
|
|
inject(i)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
// Failed processing scenario where less blocks are available than notified
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(2000, 1500, false)
|
2017-08-03 16:25:06 +00:00
|
|
|
|
|
|
|
// Notify about a reorg (which could have caused the missing blocks if happened during processing)
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(1500, 1500, true)
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// Create new fork
|
2017-03-05 15:52:03 +00:00
|
|
|
for i := uint64(1501); i <= 2000; i++ {
|
2017-08-03 16:25:06 +00:00
|
|
|
inject(i)
|
2017-03-05 15:52:03 +00:00
|
|
|
notify(i, i, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// testChainIndexBackend implements ChainIndexerBackend
|
|
|
|
type testChainIndexBackend struct {
|
2017-03-05 15:52:03 +00:00
|
|
|
t *testing.T
|
|
|
|
indexer *ChainIndexer
|
2017-08-03 16:25:06 +00:00
|
|
|
section, headerCnt, stored uint64
|
2017-03-05 15:52:03 +00:00
|
|
|
processCh chan uint64
|
|
|
|
}
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
// assertSections verifies if a chain indexer has the correct number of section.
|
|
|
|
func (b *testChainIndexBackend) assertSections() {
|
|
|
|
// Keep trying for 3 seconds if it does not match
|
|
|
|
var sections uint64
|
|
|
|
for i := 0; i < 300; i++ {
|
|
|
|
sections, _, _ = b.indexer.Sections()
|
|
|
|
if sections == b.stored {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
b.t.Fatalf("Canonical section count mismatch: have %v, want %v", sections, b.stored)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertBlocks expects processing calls after new blocks have arrived. If the
|
|
|
|
// failNum < headNum then we are simulating a scenario where a reorg has happened
|
|
|
|
// after the processing has started and the processing of a section fails.
|
|
|
|
func (b *testChainIndexBackend) assertBlocks(headNum, failNum uint64) (uint64, bool) {
|
|
|
|
var sections uint64
|
|
|
|
if headNum >= b.indexer.confirmsReq {
|
|
|
|
sections = (headNum + 1 - b.indexer.confirmsReq) / b.indexer.sectionSize
|
|
|
|
if sections > b.stored {
|
2017-03-05 15:52:03 +00:00
|
|
|
// expect processed blocks
|
2017-08-03 16:25:06 +00:00
|
|
|
for expectd := b.stored * b.indexer.sectionSize; expectd < sections*b.indexer.sectionSize; expectd++ {
|
|
|
|
if expectd > failNum {
|
2017-03-05 15:52:03 +00:00
|
|
|
// rolled back after processing started, no more process calls expected
|
|
|
|
// wait until updating is done to make sure that processing actually fails
|
2017-08-03 16:25:06 +00:00
|
|
|
var updating bool
|
|
|
|
for i := 0; i < 300; i++ {
|
|
|
|
b.indexer.lock.Lock()
|
|
|
|
updating = b.indexer.knownSections > b.indexer.storedSections
|
|
|
|
b.indexer.lock.Unlock()
|
|
|
|
if !updating {
|
2017-03-05 15:52:03 +00:00
|
|
|
break
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
if updating {
|
|
|
|
b.t.Fatalf("update did not finish")
|
|
|
|
}
|
|
|
|
sections = expectd / b.indexer.sectionSize
|
2017-03-05 15:52:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
2017-08-03 16:25:06 +00:00
|
|
|
b.t.Fatalf("Expected processed block #%d, got nothing", expectd)
|
|
|
|
case processed := <-b.processCh:
|
|
|
|
if processed != expectd {
|
|
|
|
b.t.Errorf("Expected processed block #%d, got #%d", expectd, processed)
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
b.stored = sections
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
if b.stored == 0 {
|
2017-03-05 15:52:03 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
return b.stored*b.indexer.sectionSize - 1, true
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:25:06 +00:00
|
|
|
func (b *testChainIndexBackend) reorg(headNum uint64) uint64 {
|
2020-01-20 08:38:08 +00:00
|
|
|
firstChanged := (headNum + 1) / b.indexer.sectionSize
|
2017-08-03 16:25:06 +00:00
|
|
|
if firstChanged < b.stored {
|
|
|
|
b.stored = firstChanged
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2017-08-03 16:25:06 +00:00
|
|
|
return b.stored * b.indexer.sectionSize
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:25:46 +00:00
|
|
|
func (b *testChainIndexBackend) Reset(ctx context.Context, section uint64, prevHead common.Hash) error {
|
2017-08-03 16:25:06 +00:00
|
|
|
b.section = section
|
|
|
|
b.headerCnt = 0
|
2017-10-24 13:19:09 +00:00
|
|
|
return nil
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:25:46 +00:00
|
|
|
func (b *testChainIndexBackend) Process(ctx context.Context, header *types.Header) error {
|
2017-08-03 16:25:06 +00:00
|
|
|
b.headerCnt++
|
|
|
|
if b.headerCnt > b.indexer.sectionSize {
|
|
|
|
b.t.Error("Processing too many headers")
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
//t.processCh <- header.Number.Uint64()
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
2021-03-19 12:32:57 +00:00
|
|
|
b.t.Error("Unexpected call to Process")
|
|
|
|
// Can't use Fatal since this is not the test's goroutine.
|
|
|
|
// Returning error stops the chainIndexer's updateLoop
|
|
|
|
return errors.New("Unexpected call to Process")
|
2017-08-03 16:25:06 +00:00
|
|
|
case b.processCh <- header.Number.Uint64():
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
2018-08-15 20:25:46 +00:00
|
|
|
return nil
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:52:20 +00:00
|
|
|
func (b *testChainIndexBackend) Commit() error {
|
2017-08-03 16:25:06 +00:00
|
|
|
if b.headerCnt != b.indexer.sectionSize {
|
|
|
|
b.t.Error("Not enough headers processed")
|
2017-03-05 15:52:03 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-13 09:02:54 +00:00
|
|
|
|
|
|
|
func (b *testChainIndexBackend) Prune(threshold uint64) error {
|
|
|
|
return nil
|
|
|
|
}
|