2021-07-02 15:54:52 +00:00
|
|
|
package debug
|
2021-03-29 21:04:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-14 17:01:24 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
blockchainmock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2021-03-29 21:04:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/testutil"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-02 19:28:55 +00:00
|
|
|
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
|
|
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
|
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-03-29 21:04:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetBeaconState(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState()
|
2021-03-29 21:04:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
2021-09-02 19:28:55 +00:00
|
|
|
resp, err := server.GetBeaconState(context.Background(), ðpbv1.StateRequest{
|
2021-03-29 21:04:35 +00:00
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
}
|
2021-04-14 17:01:24 +00:00
|
|
|
|
2021-09-02 19:28:55 +00:00
|
|
|
func TestGetBeaconStateV2(t *testing.T) {
|
|
|
|
t.Run("Phase 0", func(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState()
|
2021-09-02 19:28:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := server.GetBeaconStateV2(context.Background(), ðpbv2.StateRequestV2{
|
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
2021-09-09 13:29:09 +00:00
|
|
|
assert.Equal(t, ethpbv2.Version_PHASE0, resp.Version)
|
2021-09-02 19:28:55 +00:00
|
|
|
})
|
|
|
|
t.Run("Altair", func(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, _ := util.DeterministicGenesisStateAltair(t, 1)
|
2021-09-02 19:28:55 +00:00
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := server.GetBeaconStateV2(context.Background(), ðpbv2.StateRequestV2{
|
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
2021-09-09 13:29:09 +00:00
|
|
|
assert.Equal(t, ethpbv2.Version_ALTAIR, resp.Version)
|
2021-09-02 19:28:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
func TestGetBeaconStateSSZ(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState()
|
2021-06-15 15:28:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
sszState, err := fakeState.MarshalSSZ()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
2021-09-02 19:28:55 +00:00
|
|
|
resp, err := server.GetBeaconStateSSZ(context.Background(), ðpbv1.StateRequest{
|
2021-06-15 15:28:49 +00:00
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
|
|
|
|
assert.DeepEqual(t, sszState, resp.Data)
|
|
|
|
}
|
|
|
|
|
2021-09-02 19:28:55 +00:00
|
|
|
func TestGetBeaconStateSSZV2(t *testing.T) {
|
|
|
|
t.Run("Phase 0", func(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, err := util.NewBeaconState()
|
2021-09-02 19:28:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
sszState, err := fakeState.MarshalSSZ()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := server.GetBeaconStateSSZV2(context.Background(), ðpbv2.StateRequestV2{
|
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
|
|
|
|
assert.DeepEqual(t, sszState, resp.Data)
|
|
|
|
})
|
|
|
|
t.Run("Altair", func(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
fakeState, _ := util.DeterministicGenesisStateAltair(t, 1)
|
2021-09-02 19:28:55 +00:00
|
|
|
sszState, err := fakeState.MarshalSSZ()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
StateFetcher: &testutil.MockFetcher{
|
|
|
|
BeaconState: fakeState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := server.GetBeaconStateSSZV2(context.Background(), ðpbv2.StateRequestV2{
|
|
|
|
StateId: make([]byte, 0),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp)
|
|
|
|
|
|
|
|
assert.DeepEqual(t, sszState, resp.Data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:01:24 +00:00
|
|
|
func TestListForkChoiceHeads(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
expectedSlotsAndRoots := []struct {
|
|
|
|
Slot types.Slot
|
|
|
|
Root [32]byte
|
|
|
|
}{{
|
|
|
|
Slot: 0,
|
|
|
|
Root: bytesutil.ToBytes32(bytesutil.PadTo([]byte("foo"), 32)),
|
|
|
|
}, {
|
|
|
|
Slot: 1,
|
|
|
|
Root: bytesutil.ToBytes32(bytesutil.PadTo([]byte("bar"), 32)),
|
|
|
|
}}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
HeadFetcher: &blockchainmock.ChainService{},
|
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
resp, err := server.ListForkChoiceHeads(ctx, &emptypb.Empty{})
|
2021-04-14 17:01:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, len(resp.Data))
|
|
|
|
for _, sr := range expectedSlotsAndRoots {
|
|
|
|
found := false
|
|
|
|
for _, h := range resp.Data {
|
|
|
|
if h.Slot == sr.Slot {
|
|
|
|
found = true
|
|
|
|
assert.DeepEqual(t, sr.Root[:], h.Root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, found, "Expected head not found")
|
|
|
|
}
|
|
|
|
}
|