2021-07-02 15:54:52 +00:00
|
|
|
package beacon
|
2021-02-02 14:44:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2021-06-15 15:28:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/testutil"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-08-10 19:55:24 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
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-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2021-02-02 14:44:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetGenesis(t *testing.T) {
|
2021-02-11 21:08:36 +00:00
|
|
|
ctx := context.Background()
|
2021-02-02 14:44:28 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
|
|
|
config := params.BeaconConfig()
|
|
|
|
config.GenesisForkVersion = []byte("genesis")
|
|
|
|
params.OverrideBeaconConfig(config)
|
|
|
|
genesis := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
validatorsRoot := [32]byte{1, 2, 3, 4, 5, 6}
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
chainService := &chainMock.ChainService{
|
|
|
|
Genesis: genesis,
|
|
|
|
ValidatorsRoot: validatorsRoot,
|
|
|
|
}
|
|
|
|
s := Server{
|
|
|
|
GenesisTimeFetcher: chainService,
|
|
|
|
ChainInfoFetcher: chainService,
|
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
resp, err := s.GetGenesis(ctx, &emptypb.Empty{})
|
2021-02-02 14:44:28 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 10:05:22 +00:00
|
|
|
assert.Equal(t, genesis.Unix(), resp.Data.GenesisTime.Seconds)
|
|
|
|
assert.Equal(t, int32(0), resp.Data.GenesisTime.Nanos)
|
|
|
|
assert.DeepEqual(t, validatorsRoot[:], resp.Data.GenesisValidatorsRoot)
|
|
|
|
assert.DeepEqual(t, []byte("genesis"), resp.Data.GenesisForkVersion)
|
2021-02-02 14:44:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No genesis time", func(t *testing.T) {
|
|
|
|
chainService := &chainMock.ChainService{
|
|
|
|
Genesis: time.Time{},
|
|
|
|
ValidatorsRoot: validatorsRoot,
|
|
|
|
}
|
|
|
|
s := Server{
|
|
|
|
GenesisTimeFetcher: chainService,
|
|
|
|
ChainInfoFetcher: chainService,
|
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
_, err := s.GetGenesis(ctx, &emptypb.Empty{})
|
2021-02-02 14:44:28 +00:00
|
|
|
assert.ErrorContains(t, "Chain genesis info is not yet known", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No genesis validator root", func(t *testing.T) {
|
|
|
|
chainService := &chainMock.ChainService{
|
|
|
|
Genesis: genesis,
|
|
|
|
ValidatorsRoot: [32]byte{},
|
|
|
|
}
|
|
|
|
s := Server{
|
|
|
|
GenesisTimeFetcher: chainService,
|
|
|
|
ChainInfoFetcher: chainService,
|
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
_, err := s.GetGenesis(ctx, &emptypb.Empty{})
|
2021-02-02 14:44:28 +00:00
|
|
|
assert.ErrorContains(t, "Chain genesis info is not yet known", err)
|
|
|
|
})
|
|
|
|
}
|
2021-02-11 21:08:36 +00:00
|
|
|
|
|
|
|
func TestGetStateRoot(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState()
|
2021-06-15 15:28:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
stateRoot, err := fakeState.HashTreeRoot(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconStateRoot: stateRoot[:],
|
|
|
|
},
|
|
|
|
}
|
2021-02-11 21:08:36 +00:00
|
|
|
|
2021-08-10 19:55:24 +00:00
|
|
|
resp, err := server.GetStateRoot(context.Background(), ð.StateRequest{
|
2021-06-15 15:28:49 +00:00
|
|
|
StateId: make([]byte, 0),
|
2021-02-11 21:08:36 +00:00
|
|
|
})
|
2021-06-15 15:28:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
assert.DeepEqual(t, stateRoot[:], resp.Data.Root)
|
2021-02-11 21:08:36 +00:00
|
|
|
}
|
2021-02-17 15:26:39 +00:00
|
|
|
|
|
|
|
func TestGetStateFork(t *testing.T) {
|
2021-08-10 19:55:24 +00:00
|
|
|
fillFork := func(state *ethpb.BeaconState) error {
|
|
|
|
state.Fork = ðpb.Fork{
|
2021-02-17 15:26:39 +00:00
|
|
|
PreviousVersion: []byte("prev"),
|
|
|
|
CurrentVersion: []byte("curr"),
|
|
|
|
Epoch: 123,
|
|
|
|
}
|
2021-03-24 03:47:36 +00:00
|
|
|
return nil
|
2021-02-17 15:26:39 +00:00
|
|
|
}
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState(fillFork)
|
2021-02-17 15:26:39 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-15 15:28:49 +00:00
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
2021-02-17 15:26:39 +00:00
|
|
|
|
2021-08-10 19:55:24 +00:00
|
|
|
resp, err := server.GetStateFork(context.Background(), ð.StateRequest{
|
2021-06-15 15:28:49 +00:00
|
|
|
StateId: make([]byte, 0),
|
2021-02-17 15:26:39 +00:00
|
|
|
})
|
2021-06-15 15:28:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
expectedFork := fakeState.Fork()
|
|
|
|
assert.Equal(t, expectedFork.Epoch, resp.Data.Epoch)
|
|
|
|
assert.DeepEqual(t, expectedFork.CurrentVersion, resp.Data.CurrentVersion)
|
|
|
|
assert.DeepEqual(t, expectedFork.PreviousVersion, resp.Data.PreviousVersion)
|
2021-02-17 15:26:39 +00:00
|
|
|
}
|
2021-02-18 15:46:17 +00:00
|
|
|
|
|
|
|
func TestGetFinalityCheckpoints(t *testing.T) {
|
2021-08-10 19:55:24 +00:00
|
|
|
fillCheckpoints := func(state *ethpb.BeaconState) error {
|
|
|
|
state.PreviousJustifiedCheckpoint = ðpb.Checkpoint{
|
2021-02-18 15:46:17 +00:00
|
|
|
Root: bytesutil.PadTo([]byte("previous"), 32),
|
|
|
|
Epoch: 113,
|
|
|
|
}
|
2021-08-10 19:55:24 +00:00
|
|
|
state.CurrentJustifiedCheckpoint = ðpb.Checkpoint{
|
2021-02-18 15:46:17 +00:00
|
|
|
Root: bytesutil.PadTo([]byte("current"), 32),
|
|
|
|
Epoch: 123,
|
|
|
|
}
|
2021-08-10 19:55:24 +00:00
|
|
|
state.FinalizedCheckpoint = ðpb.Checkpoint{
|
2021-02-18 15:46:17 +00:00
|
|
|
Root: bytesutil.PadTo([]byte("finalized"), 32),
|
|
|
|
Epoch: 103,
|
|
|
|
}
|
2021-03-24 03:47:36 +00:00
|
|
|
return nil
|
2021-02-18 15:46:17 +00:00
|
|
|
}
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState(fillCheckpoints)
|
2021-02-18 15:46:17 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-15 15:28:49 +00:00
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
2021-02-18 15:46:17 +00:00
|
|
|
|
2021-08-10 19:55:24 +00:00
|
|
|
resp, err := server.GetFinalityCheckpoints(context.Background(), ð.StateRequest{
|
2021-06-15 15:28:49 +00:00
|
|
|
StateId: make([]byte, 0),
|
2021-02-18 15:46:17 +00:00
|
|
|
})
|
2021-06-15 15:28:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
assert.Equal(t, fakeState.FinalizedCheckpoint().Epoch, resp.Data.Finalized.Epoch)
|
|
|
|
assert.DeepEqual(t, fakeState.FinalizedCheckpoint().Root, resp.Data.Finalized.Root)
|
|
|
|
assert.Equal(t, fakeState.CurrentJustifiedCheckpoint().Epoch, resp.Data.CurrentJustified.Epoch)
|
|
|
|
assert.DeepEqual(t, fakeState.CurrentJustifiedCheckpoint().Root, resp.Data.CurrentJustified.Root)
|
|
|
|
assert.Equal(t, fakeState.PreviousJustifiedCheckpoint().Epoch, resp.Data.PreviousJustified.Epoch)
|
|
|
|
assert.DeepEqual(t, fakeState.PreviousJustifiedCheckpoint().Root, resp.Data.PreviousJustified.Root)
|
2021-02-18 15:46:17 +00:00
|
|
|
}
|