2020-09-21 12:20:26 +00:00
|
|
|
package initialsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-12-04 11:57:46 +00:00
|
|
|
"github.com/paulbellamy/ratecounter"
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-18 17:26:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/async/abool"
|
|
|
|
"github.com/prysmaticlabs/prysm/async/event"
|
2020-09-21 12:20:26 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
|
|
|
dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
p2pt "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2021-03-02 19:36:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-21 21:34:07 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2021-10-01 20:17:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/time/slots"
|
2020-09-21 12:20:26 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_Constants(t *testing.T) {
|
2022-02-01 08:51:17 +00:00
|
|
|
if params.BeaconConfig().MaxPeersToSync*flags.Get().BlockBatchLimit > 1000 {
|
2020-09-21 12:20:26 +00:00
|
|
|
t.Fatal("rpc rejects requests over 1000 range slots")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_InitStartStop(t *testing.T) {
|
2020-09-23 07:24:33 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
assert func()
|
2020-10-10 08:50:28 +00:00
|
|
|
methodRuns func(fd *event.Feed)
|
2020-09-23 07:24:33 +00:00
|
|
|
chainService func() *mock.ChainService
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "head is not ready",
|
|
|
|
assert: func() {
|
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "future genesis",
|
|
|
|
chainService: func() *mock.ChainService {
|
|
|
|
// Set to future time (genesis time hasn't arrived yet).
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-10 08:50:28 +00:00
|
|
|
|
2020-09-23 07:24:33 +00:00
|
|
|
return &mock.ChainService{
|
|
|
|
State: st,
|
|
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
2021-09-03 16:00:56 +00:00
|
|
|
Genesis: time.Unix(4113849600, 0),
|
|
|
|
ValidatorsRoot: [32]byte{},
|
2020-09-23 07:24:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-10 08:50:28 +00:00
|
|
|
methodRuns: func(fd *event.Feed) {
|
|
|
|
// Send valid event.
|
|
|
|
fd.Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Unix(4113849600, 0),
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2020-09-23 07:24:33 +00:00
|
|
|
assert: func() {
|
|
|
|
assert.LogsContain(t, hook, "Genesis time has not arrived - not syncing")
|
2020-10-10 08:50:28 +00:00
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zeroth epoch",
|
|
|
|
chainService: func() *mock.ChainService {
|
|
|
|
// Set to nearby slot.
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-23 07:24:33 +00:00
|
|
|
return &mock.ChainService{
|
|
|
|
State: st,
|
|
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
2021-09-03 16:00:56 +00:00
|
|
|
Genesis: time.Now().Add(-5 * time.Minute),
|
|
|
|
ValidatorsRoot: [32]byte{},
|
2020-09-23 07:24:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-10 08:50:28 +00:00
|
|
|
methodRuns: func(fd *event.Feed) {
|
|
|
|
// Send valid event.
|
|
|
|
fd.Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now().Add(-5 * time.Minute),
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2020-09-23 07:24:33 +00:00
|
|
|
assert: func() {
|
|
|
|
assert.LogsContain(t, hook, "Chain started within the last epoch - not syncing")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Genesis time has not arrived - not syncing")
|
2020-10-10 08:50:28 +00:00
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "already synced",
|
|
|
|
chainService: func() *mock.ChainService {
|
|
|
|
// Set to some future slot, and then make sure that current head matches it.
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-16 07:45:34 +00:00
|
|
|
futureSlot := types.Slot(27354)
|
2020-09-23 07:24:33 +00:00
|
|
|
require.NoError(t, st.SetSlot(futureSlot))
|
|
|
|
return &mock.ChainService{
|
|
|
|
State: st,
|
|
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
2021-10-01 20:17:57 +00:00
|
|
|
Epoch: slots.ToEpoch(futureSlot),
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
2021-09-03 16:00:56 +00:00
|
|
|
Genesis: makeGenesisTime(futureSlot),
|
|
|
|
ValidatorsRoot: [32]byte{},
|
2020-09-23 07:24:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-10 08:50:28 +00:00
|
|
|
methodRuns: func(fd *event.Feed) {
|
2021-02-16 07:45:34 +00:00
|
|
|
futureSlot := types.Slot(27354)
|
2020-10-10 08:50:28 +00:00
|
|
|
// Send valid event.
|
|
|
|
fd.Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: makeGenesisTime(futureSlot),
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2020-09-23 07:24:33 +00:00
|
|
|
assert: func() {
|
|
|
|
assert.LogsContain(t, hook, "Starting initial chain sync...")
|
|
|
|
assert.LogsContain(t, hook, "Already synced to the current chain head")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Chain started within the last epoch - not syncing")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Genesis time has not arrived - not syncing")
|
2020-10-10 08:50:28 +00:00
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-21 12:20:26 +00:00
|
|
|
|
2020-09-23 07:24:33 +00:00
|
|
|
p := p2pt.NewTestP2P(t)
|
|
|
|
connectPeers(t, p, []*peerData{}, p.Peers())
|
2020-10-10 08:50:28 +00:00
|
|
|
for i, tt := range tests {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-23 07:24:33 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer hook.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-09-03 16:00:56 +00:00
|
|
|
mc := &mock.ChainService{Genesis: time.Now(), ValidatorsRoot: [32]byte{}}
|
2020-09-23 07:24:33 +00:00
|
|
|
// Allow overriding with customized chain service.
|
|
|
|
if tt.chainService != nil {
|
|
|
|
mc = tt.chainService()
|
|
|
|
}
|
2020-10-10 08:50:28 +00:00
|
|
|
// Initialize feed
|
|
|
|
notifier := &mock.MockStateNotifier{}
|
2021-02-12 17:45:22 +00:00
|
|
|
s := NewService(ctx, &Config{
|
2020-09-23 07:24:33 +00:00
|
|
|
P2P: p,
|
|
|
|
Chain: mc,
|
2020-10-10 08:50:28 +00:00
|
|
|
StateNotifier: notifier,
|
2020-09-23 07:24:33 +00:00
|
|
|
})
|
2020-10-10 08:50:28 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2020-09-23 07:24:33 +00:00
|
|
|
assert.NotNil(t, s)
|
2020-10-10 08:50:28 +00:00
|
|
|
if tt.methodRuns != nil {
|
|
|
|
tt.methodRuns(notifier.StateFeed())
|
|
|
|
}
|
2020-09-23 07:24:33 +00:00
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
s.Start()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-10-10 08:50:28 +00:00
|
|
|
|
2020-09-23 07:24:33 +00:00
|
|
|
go func() {
|
|
|
|
// Allow to exit from test (on no head loop waiting for head is started).
|
|
|
|
// In most tests, this is redundant, as Start() already exited.
|
2020-10-10 08:50:28 +00:00
|
|
|
time.AfterFunc(3*time.Second, func() {
|
2020-09-23 07:24:33 +00:00
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
}()
|
2021-09-23 18:53:46 +00:00
|
|
|
if util.WaitTimeout(wg, time.Second*4) {
|
2020-09-23 07:24:33 +00:00
|
|
|
t.Fatalf("Test should have exited by now, timed out")
|
|
|
|
}
|
|
|
|
tt.assert()
|
|
|
|
})
|
|
|
|
}
|
2020-09-21 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_waitForStateInitialization(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
newService := func(ctx context.Context, mc *mock.ChainService) *Service {
|
2020-12-04 11:57:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
s := &Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
cfg: &Config{Chain: mc, StateNotifier: mc.StateNotifier()},
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
synced: abool.New(),
|
|
|
|
chainStarted: abool.New(),
|
|
|
|
counter: ratecounter.NewRateCounter(counterSeconds * time.Second),
|
|
|
|
genesisChan: make(chan time.Time),
|
2020-12-04 11:57:46 +00:00
|
|
|
}
|
2020-09-21 12:20:26 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no state and context close", func(t *testing.T) {
|
|
|
|
defer hook.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-09-03 16:00:56 +00:00
|
|
|
s := newService(ctx, &mock.ChainService{Genesis: time.Now(), ValidatorsRoot: [32]byte{}})
|
2020-09-21 12:20:26 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-10-10 08:50:28 +00:00
|
|
|
go s.waitForStateInitialization()
|
|
|
|
currTime := <-s.genesisChan
|
|
|
|
assert.Equal(t, true, currTime.IsZero())
|
2020-09-21 12:20:26 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.AfterFunc(500*time.Millisecond, func() {
|
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
if util.WaitTimeout(wg, time.Second*2) {
|
2020-09-21 12:20:26 +00:00
|
|
|
t.Fatalf("Test should have exited by now, timed out")
|
|
|
|
}
|
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
|
|
|
assert.LogsContain(t, hook, "Context closed, exiting goroutine")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Subscription to state notifier failed")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no state and state init event received", func(t *testing.T) {
|
|
|
|
defer hook.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-09-03 16:00:56 +00:00
|
|
|
s := newService(ctx, &mock.ChainService{Genesis: time.Now(), ValidatorsRoot: [32]byte{}})
|
2020-09-21 12:20:26 +00:00
|
|
|
|
|
|
|
expectedGenesisTime := time.Unix(358544700, 0)
|
|
|
|
var receivedGenesisTime time.Time
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2020-10-10 08:50:28 +00:00
|
|
|
go s.waitForStateInitialization()
|
|
|
|
receivedGenesisTime = <-s.genesisChan
|
|
|
|
assert.Equal(t, false, receivedGenesisTime.IsZero())
|
2020-09-21 12:20:26 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.AfterFunc(500*time.Millisecond, func() {
|
|
|
|
// Send invalid event at first.
|
2021-03-21 19:07:42 +00:00
|
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-09-21 12:20:26 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.BlockProcessedData{},
|
|
|
|
})
|
|
|
|
// Send valid event.
|
2021-03-21 19:07:42 +00:00
|
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-09-21 12:20:26 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: expectedGenesisTime,
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
if util.WaitTimeout(wg, time.Second*2) {
|
2020-09-21 12:20:26 +00:00
|
|
|
t.Fatalf("Test should have exited by now, timed out")
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedGenesisTime, receivedGenesisTime)
|
|
|
|
assert.LogsContain(t, hook, "Event feed data is not type *statefeed.InitializedData")
|
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
|
|
|
assert.LogsContain(t, hook, "Received state initialized event")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Context closed, exiting goroutine")
|
|
|
|
})
|
2020-10-10 08:50:28 +00:00
|
|
|
|
|
|
|
t.Run("no state and state init event received and service start", func(t *testing.T) {
|
|
|
|
defer hook.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-09-03 16:00:56 +00:00
|
|
|
s := newService(ctx, &mock.ChainService{Genesis: time.Now(), ValidatorsRoot: [32]byte{}})
|
2020-10-10 08:50:28 +00:00
|
|
|
// Initialize mock feed
|
2021-03-21 19:07:42 +00:00
|
|
|
_ = s.cfg.StateNotifier.StateFeed()
|
2020-10-10 08:50:28 +00:00
|
|
|
|
|
|
|
expectedGenesisTime := time.Now().Add(60 * time.Second)
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
s.waitForStateInitialization()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
time.AfterFunc(500*time.Millisecond, func() {
|
|
|
|
// Send valid event.
|
2021-03-21 19:07:42 +00:00
|
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-10-10 08:50:28 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: expectedGenesisTime,
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.Start()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
if util.WaitTimeout(wg, time.Second*5) {
|
2020-10-10 08:50:28 +00:00
|
|
|
t.Fatalf("Test should have exited by now, timed out")
|
|
|
|
}
|
|
|
|
assert.LogsContain(t, hook, "Waiting for state to be initialized")
|
|
|
|
assert.LogsContain(t, hook, "Received state initialized event")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Context closed, exiting goroutine")
|
|
|
|
})
|
2020-09-21 12:20:26 +00:00
|
|
|
}
|
2020-09-23 07:24:33 +00:00
|
|
|
|
|
|
|
func TestService_markSynced(t *testing.T) {
|
2021-09-03 16:00:56 +00:00
|
|
|
mc := &mock.ChainService{Genesis: time.Now(), ValidatorsRoot: [32]byte{}}
|
2020-09-23 07:24:33 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-02-12 17:45:22 +00:00
|
|
|
s := NewService(ctx, &Config{
|
2020-09-23 07:24:33 +00:00
|
|
|
Chain: mc,
|
|
|
|
StateNotifier: mc.StateNotifier(),
|
|
|
|
})
|
|
|
|
require.NotNil(t, s)
|
2020-11-14 02:28:14 +00:00
|
|
|
assert.Equal(t, false, s.chainStarted.IsSet())
|
|
|
|
assert.Equal(t, false, s.synced.IsSet())
|
2020-09-23 07:24:33 +00:00
|
|
|
assert.Equal(t, true, s.Syncing())
|
2021-01-06 11:19:53 +00:00
|
|
|
assert.NoError(t, s.Status())
|
2020-11-11 02:57:07 +00:00
|
|
|
s.chainStarted.Set()
|
2020-09-23 07:24:33 +00:00
|
|
|
assert.ErrorContains(t, "syncing", s.Status())
|
|
|
|
|
|
|
|
expectedGenesisTime := time.Unix(358544700, 0)
|
|
|
|
var receivedGenesisTime time.Time
|
|
|
|
|
|
|
|
stateChannel := make(chan *feed.Event, 1)
|
2021-03-21 19:07:42 +00:00
|
|
|
stateSub := s.cfg.StateNotifier.StateFeed().Subscribe(stateChannel)
|
2020-09-23 07:24:33 +00:00
|
|
|
defer stateSub.Unsubscribe()
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
select {
|
2021-01-07 16:42:03 +00:00
|
|
|
case stateEvent := <-stateChannel:
|
|
|
|
if stateEvent.Type == statefeed.Synced {
|
|
|
|
data, ok := stateEvent.Data.(*statefeed.SyncedData)
|
2020-09-23 07:24:33 +00:00
|
|
|
require.Equal(t, true, ok, "Event feed data is not type *statefeed.SyncedData")
|
|
|
|
receivedGenesisTime = data.StartTime
|
|
|
|
}
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
s.markSynced(expectedGenesisTime)
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
if util.WaitTimeout(wg, time.Second*2) {
|
2020-09-23 07:24:33 +00:00
|
|
|
t.Fatalf("Test should have exited by now, timed out")
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedGenesisTime, receivedGenesisTime)
|
|
|
|
assert.Equal(t, false, s.Syncing())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_Resync(t *testing.T) {
|
|
|
|
p := p2pt.NewTestP2P(t)
|
|
|
|
connectPeers(t, p, []*peerData{
|
|
|
|
{blocks: makeSequence(1, 160), finalizedEpoch: 5, headSlot: 160},
|
|
|
|
}, p.Peers())
|
|
|
|
cache.initializeRootCache(makeSequence(1, 160), t)
|
2020-12-16 16:56:21 +00:00
|
|
|
beaconDB := dbtest.SetupDB(t)
|
2021-09-23 18:53:46 +00:00
|
|
|
err := beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(util.NewBeaconBlock()))
|
2020-09-23 07:24:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
cache.RLock()
|
|
|
|
genesisRoot := cache.rootCache[0]
|
|
|
|
cache.RUnlock()
|
|
|
|
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
assert func(s *Service)
|
|
|
|
chainService func() *mock.ChainService
|
|
|
|
wantedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no head state",
|
|
|
|
wantedErr: "could not retrieve head state",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "resync ok",
|
|
|
|
chainService: func() *mock.ChainService {
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-16 07:45:34 +00:00
|
|
|
futureSlot := types.Slot(160)
|
2020-09-23 07:24:33 +00:00
|
|
|
require.NoError(t, st.SetGenesisTime(uint64(makeGenesisTime(futureSlot).Unix())))
|
|
|
|
return &mock.ChainService{
|
|
|
|
State: st,
|
|
|
|
Root: genesisRoot[:],
|
|
|
|
DB: beaconDB,
|
|
|
|
FinalizedCheckPoint: ð.Checkpoint{
|
2021-10-01 20:17:57 +00:00
|
|
|
Epoch: slots.ToEpoch(futureSlot),
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
2021-09-03 16:00:56 +00:00
|
|
|
Genesis: time.Now(),
|
|
|
|
ValidatorsRoot: [32]byte{},
|
2020-09-23 07:24:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
assert: func(s *Service) {
|
|
|
|
assert.LogsContain(t, hook, "Resync attempt complete")
|
2021-03-21 19:07:42 +00:00
|
|
|
assert.Equal(t, types.Slot(160), s.cfg.Chain.HeadSlot())
|
2020-09-23 07:24:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer hook.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
mc := &mock.ChainService{}
|
|
|
|
// Allow overriding with customized chain service.
|
|
|
|
if tt.chainService != nil {
|
|
|
|
mc = tt.chainService()
|
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
s := NewService(ctx, &Config{
|
2020-09-23 07:24:33 +00:00
|
|
|
DB: beaconDB,
|
|
|
|
P2P: p,
|
|
|
|
Chain: mc,
|
|
|
|
StateNotifier: mc.StateNotifier(),
|
|
|
|
})
|
|
|
|
assert.NotNil(t, s)
|
2021-03-21 19:07:42 +00:00
|
|
|
assert.Equal(t, types.Slot(0), s.cfg.Chain.HeadSlot())
|
2020-09-23 07:24:33 +00:00
|
|
|
err := s.Resync()
|
|
|
|
if tt.wantedErr != "" {
|
|
|
|
assert.ErrorContains(t, tt.wantedErr, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.assert != nil {
|
|
|
|
tt.assert(s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-06 10:45:22 +00:00
|
|
|
|
|
|
|
func TestService_Initialized(t *testing.T) {
|
2021-03-11 21:16:40 +00:00
|
|
|
s := NewService(context.Background(), &Config{
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
})
|
2021-01-06 10:45:22 +00:00
|
|
|
s.chainStarted.Set()
|
|
|
|
assert.Equal(t, true, s.Initialized())
|
|
|
|
s.chainStarted.UnSet()
|
|
|
|
assert.Equal(t, false, s.Initialized())
|
|
|
|
}
|
2021-06-15 15:28:49 +00:00
|
|
|
|
|
|
|
func TestService_Synced(t *testing.T) {
|
|
|
|
s := NewService(context.Background(), &Config{
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
})
|
|
|
|
s.synced.UnSet()
|
|
|
|
assert.Equal(t, false, s.Synced())
|
|
|
|
s.synced.Set()
|
|
|
|
assert.Equal(t, true, s.Synced())
|
|
|
|
}
|