2020-04-14 20:27:03 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-05-28 05:39:40 +00:00
|
|
|
"context"
|
2020-10-10 08:50:28 +00:00
|
|
|
"sync"
|
2020-04-14 20:27:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-19 05:15:58 +00:00
|
|
|
gcache "github.com/patrickmn/go-cache"
|
2020-04-14 20:27:03 +00:00
|
|
|
mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2020-05-28 05:39:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
2020-04-14 20:27:03 +00:00
|
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2021-06-30 15:06:19 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2020-04-14 20:27:03 +00:00
|
|
|
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-11-03 19:12:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/abool"
|
2020-05-28 05:39:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-15 04:41:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-04-14 20:27:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_StatusZeroEpoch(t *testing.T) {
|
2021-06-30 15:06:19 +00:00
|
|
|
bState, err := v1.InitializeFromProto(&pb.BeaconState{Slot: 0})
|
2020-07-15 04:41:11 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
r := &Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
cfg: &Config{
|
|
|
|
P2P: p2ptest.NewTestP2P(t),
|
|
|
|
InitialSync: new(mockSync.Sync),
|
|
|
|
Chain: &mockChain.ChainService{
|
|
|
|
Genesis: time.Now(),
|
|
|
|
State: bState,
|
|
|
|
},
|
2020-04-14 20:27:03 +00:00
|
|
|
},
|
2020-11-03 19:12:24 +00:00
|
|
|
chainStarted: abool.New(),
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2020-11-03 19:12:24 +00:00
|
|
|
r.chainStarted.Set()
|
2020-04-14 20:27:03 +00:00
|
|
|
|
2020-07-15 04:41:11 +00:00
|
|
|
assert.NoError(t, r.Status(), "Wanted non failing status")
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2020-05-28 05:39:40 +00:00
|
|
|
|
|
|
|
func TestSyncHandlers_WaitToSync(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
chainService := &mockChain.ChainService{
|
|
|
|
Genesis: time.Now(),
|
|
|
|
ValidatorsRoot: [32]byte{'A'},
|
|
|
|
}
|
|
|
|
r := Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
cfg: &Config{
|
|
|
|
P2P: p2p,
|
|
|
|
Chain: chainService,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
|
|
},
|
|
|
|
chainStarted: abool.New(),
|
2020-05-28 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
topic := "/eth2/%x/beacon_block"
|
|
|
|
go r.registerHandlers()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-03-21 19:07:42 +00:00
|
|
|
i := r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-05-28 05:39:40 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to subscribers")
|
|
|
|
}
|
|
|
|
b := []byte("sk")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
|
|
|
sk, err := bls.SecretKeyFromBytes(b32[:])
|
2020-07-15 04:41:11 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-28 05:39:40 +00:00
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
msg := testutil.NewBeaconBlock()
|
|
|
|
msg.Block.ParentRoot = testutil.Random32Bytes(t)
|
|
|
|
msg.Signature = sk.Sign([]byte("data")).Marshal()
|
2020-05-28 05:39:40 +00:00
|
|
|
p2p.ReceivePubSub(topic, msg)
|
|
|
|
// wait for chainstart to be sent
|
|
|
|
time.Sleep(400 * time.Millisecond)
|
2020-11-03 19:12:24 +00:00
|
|
|
require.Equal(t, true, r.chainStarted.IsSet(), "Did not receive chain start event.")
|
2020-05-28 05:39:40 +00:00
|
|
|
}
|
2020-10-10 08:50:28 +00:00
|
|
|
|
2020-10-15 14:03:48 +00:00
|
|
|
func TestSyncHandlers_WaitForChainStart(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
chainService := &mockChain.ChainService{
|
|
|
|
Genesis: time.Now(),
|
|
|
|
ValidatorsRoot: [32]byte{'A'},
|
|
|
|
}
|
|
|
|
r := Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
cfg: &Config{
|
|
|
|
P2P: p2p,
|
|
|
|
Chain: chainService,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
|
|
},
|
2020-11-19 05:15:58 +00:00
|
|
|
chainStarted: abool.New(),
|
|
|
|
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
|
2020-10-15 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go r.registerHandlers()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-03-21 19:07:42 +00:00
|
|
|
i := r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-10-15 14:03:48 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now().Add(2 * time.Second),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to subscribers")
|
|
|
|
}
|
2020-11-03 19:12:24 +00:00
|
|
|
require.Equal(t, false, r.chainStarted.IsSet(), "Chainstart was marked prematurely")
|
2020-10-15 14:03:48 +00:00
|
|
|
|
|
|
|
// wait for chainstart to be sent
|
|
|
|
time.Sleep(3 * time.Second)
|
2020-11-03 19:12:24 +00:00
|
|
|
require.Equal(t, true, r.chainStarted.IsSet(), "Did not receive chain start event.")
|
2020-10-15 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 08:50:28 +00:00
|
|
|
func TestSyncHandlers_WaitTillSynced(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
chainService := &mockChain.ChainService{
|
|
|
|
Genesis: time.Now(),
|
|
|
|
ValidatorsRoot: [32]byte{'A'},
|
|
|
|
}
|
|
|
|
r := Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
cfg: &Config{
|
|
|
|
P2P: p2p,
|
|
|
|
Chain: chainService,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
|
|
},
|
|
|
|
chainStarted: abool.New(),
|
2020-10-10 08:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
topic := "/eth2/%x/beacon_block"
|
|
|
|
go r.registerHandlers()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-03-21 19:07:42 +00:00
|
|
|
i := r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-10-10 08:50:28 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to subscribers")
|
|
|
|
}
|
|
|
|
b := []byte("sk")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
|
|
|
sk, err := bls.SecretKeyFromBytes(b32[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msg := testutil.NewBeaconBlock()
|
|
|
|
msg.Block.ParentRoot = testutil.Random32Bytes(t)
|
|
|
|
msg.Signature = sk.Sign([]byte("data")).Marshal()
|
|
|
|
p2p.Digest, err = r.forkDigest()
|
2021-03-21 19:07:42 +00:00
|
|
|
r.cfg.BlockNotifier = chainService.BlockNotifier()
|
2020-10-10 08:50:28 +00:00
|
|
|
blockChan := make(chan feed.Event, 1)
|
2021-03-21 19:07:42 +00:00
|
|
|
sub := r.cfg.BlockNotifier.BlockFeed().Subscribe(blockChan)
|
2020-10-10 08:50:28 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
p2p.ReceivePubSub(topic, msg)
|
|
|
|
|
|
|
|
// wait for chainstart to be sent
|
|
|
|
time.Sleep(2 * time.Second)
|
2020-11-03 19:12:24 +00:00
|
|
|
require.Equal(t, true, r.chainStarted.IsSet(), "Did not receive chain start event.")
|
2020-10-10 08:50:28 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, len(blockChan), "block was received by sync service despite not being fully synced")
|
|
|
|
|
2021-03-21 19:07:42 +00:00
|
|
|
i = r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-10-10 08:50:28 +00:00
|
|
|
Type: statefeed.Synced,
|
|
|
|
Data: &statefeed.SyncedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to sync event subscribers")
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// Wait for block to be received by service.
|
|
|
|
<-blockChan
|
|
|
|
wg.Done()
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}()
|
|
|
|
|
|
|
|
p2p.ReceivePubSub(topic, msg)
|
|
|
|
// wait for message to be sent
|
|
|
|
testutil.WaitTimeout(wg, 2*time.Second)
|
|
|
|
}
|
2020-11-04 07:24:29 +00:00
|
|
|
|
|
|
|
func TestSyncService_StopCleanly(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
|
|
|
chainService := &mockChain.ChainService{
|
|
|
|
Genesis: time.Now(),
|
|
|
|
ValidatorsRoot: [32]byte{'A'},
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
r := Service{
|
2021-03-21 19:07:42 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
cfg: &Config{
|
|
|
|
P2P: p2p,
|
|
|
|
Chain: chainService,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
InitialSync: &mockSync.Sync{IsSyncing: false},
|
|
|
|
},
|
|
|
|
chainStarted: abool.New(),
|
2020-11-04 07:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go r.registerHandlers()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-03-21 19:07:42 +00:00
|
|
|
i := r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-11-04 07:24:29 +00:00
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to subscribers")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p2p.Digest, err = r.forkDigest()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// wait for chainstart to be sent
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
require.Equal(t, true, r.chainStarted.IsSet(), "Did not receive chain start event.")
|
|
|
|
|
2021-03-21 19:07:42 +00:00
|
|
|
i = r.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-11-04 07:24:29 +00:00
|
|
|
Type: statefeed.Synced,
|
|
|
|
Data: &statefeed.SyncedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if i == 0 {
|
|
|
|
t.Fatal("didn't send genesis time to sync event subscribers")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2021-03-21 19:07:42 +00:00
|
|
|
require.NotEqual(t, 0, len(r.cfg.P2P.PubSub().GetTopics()))
|
|
|
|
require.NotEqual(t, 0, len(r.cfg.P2P.Host().Mux().Protocols()))
|
2020-11-04 07:24:29 +00:00
|
|
|
|
|
|
|
// Both pubsub and rpc topcis should be unsubscribed.
|
|
|
|
require.NoError(t, r.Stop())
|
|
|
|
|
|
|
|
// Sleep to allow pubsub topics to be deregistered.
|
|
|
|
time.Sleep(1 * time.Second)
|
2021-03-21 19:07:42 +00:00
|
|
|
require.Equal(t, 0, len(r.cfg.P2P.PubSub().GetTopics()))
|
|
|
|
require.Equal(t, 0, len(r.cfg.P2P.Host().Mux().Protocols()))
|
2020-11-04 07:24:29 +00:00
|
|
|
}
|