mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-17 15:28:45 +00:00
c45fe5cc1c
* implementation # Conflicts: # beacon-chain/state/v2/BUILD.bazel * Revert "Auxiliary commit to revert individual files from 2cbe98c88777cac071876fe97f85029fad964e51" This reverts commit edc4ff52e7796aefd1782e31eaf40231a3134693. * tests * fix function call * make state package visible to migration * fix span names Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
155 lines
4.0 KiB
Go
155 lines
4.0 KiB
Go
package debug
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
blockchainmock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/testutil"
|
|
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
sharedtestutil "github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
)
|
|
|
|
func TestGetBeaconState(t *testing.T) {
|
|
fakeState, err := sharedtestutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
server := &Server{
|
|
StateFetcher: &testutil.MockFetcher{
|
|
BeaconState: fakeState,
|
|
},
|
|
}
|
|
resp, err := server.GetBeaconState(context.Background(), ðpbv1.StateRequest{
|
|
StateId: make([]byte, 0),
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, resp)
|
|
}
|
|
|
|
func TestGetBeaconStateV2(t *testing.T) {
|
|
t.Run("Phase 0", func(t *testing.T) {
|
|
fakeState, err := sharedtestutil.NewBeaconState()
|
|
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)
|
|
})
|
|
t.Run("Altair", func(t *testing.T) {
|
|
fakeState, _ := sharedtestutil.DeterministicGenesisStateAltair(t, 1)
|
|
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)
|
|
})
|
|
}
|
|
|
|
func TestGetBeaconStateSSZ(t *testing.T) {
|
|
fakeState, err := sharedtestutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
sszState, err := fakeState.MarshalSSZ()
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{
|
|
StateFetcher: &testutil.MockFetcher{
|
|
BeaconState: fakeState,
|
|
},
|
|
}
|
|
resp, err := server.GetBeaconStateSSZ(context.Background(), ðpbv1.StateRequest{
|
|
StateId: make([]byte, 0),
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, resp)
|
|
|
|
assert.DeepEqual(t, sszState, resp.Data)
|
|
}
|
|
|
|
func TestGetBeaconStateSSZV2(t *testing.T) {
|
|
t.Run("Phase 0", func(t *testing.T) {
|
|
fakeState, err := sharedtestutil.NewBeaconState()
|
|
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) {
|
|
fakeState, _ := sharedtestutil.DeterministicGenesisStateAltair(t, 1)
|
|
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)
|
|
})
|
|
}
|
|
|
|
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{},
|
|
}
|
|
resp, err := server.ListForkChoiceHeads(ctx, &emptypb.Empty{})
|
|
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")
|
|
}
|
|
}
|