2021-06-30 15:06:19 +00:00
|
|
|
package v1_test
|
2020-05-12 00:23:05 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-14 17:38:34 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-05-12 00:23:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-07-19 18:26:39 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-11-19 12:01:15 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/features"
|
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-07-29 21:45:17 +00:00
|
|
|
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"
|
2020-05-12 00:23:05 +00:00
|
|
|
)
|
|
|
|
|
2021-11-19 12:01:15 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
resetCfg := features.InitWithReset(&features.Flags{EnableBalanceTrieComputation: true})
|
|
|
|
defer resetCfg()
|
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:23:05 +00:00
|
|
|
func TestInitializeFromProto(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
testState, _ := util.DeterministicGenesisState(t, 64)
|
2021-06-30 15:06:19 +00:00
|
|
|
pbState, err := v1.ProtobufBeaconState(testState.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-12 00:23:05 +00:00
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-29 21:45:17 +00:00
|
|
|
state *ethpb.BeaconState
|
2020-05-12 00:23:05 +00:00
|
|
|
error string
|
|
|
|
}
|
|
|
|
initTests := []test{
|
|
|
|
{
|
|
|
|
name: "nil state",
|
|
|
|
state: nil,
|
|
|
|
error: "received nil state",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil validators",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconState{
|
2020-05-12 00:23:05 +00:00
|
|
|
Slot: 4,
|
|
|
|
Validators: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty state",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconState{},
|
2020-05-12 00:23:05 +00:00
|
|
|
},
|
2020-05-14 17:38:34 +00:00
|
|
|
{
|
|
|
|
name: "full state",
|
2021-03-17 03:26:17 +00:00
|
|
|
state: pbState,
|
2020-05-14 17:38:34 +00:00
|
|
|
},
|
2020-05-12 00:23:05 +00:00
|
|
|
}
|
|
|
|
for _, tt := range initTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-30 15:06:19 +00:00
|
|
|
_, err := v1.InitializeFromProto(tt.state)
|
2020-08-25 15:23:06 +00:00
|
|
|
if tt.error != "" {
|
|
|
|
assert.ErrorContains(t, tt.error, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
2020-05-12 00:23:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitializeFromProtoUnsafe(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
testState, _ := util.DeterministicGenesisState(t, 64)
|
2021-06-30 15:06:19 +00:00
|
|
|
pbState, err := v1.ProtobufBeaconState(testState.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-12 00:23:05 +00:00
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-29 21:45:17 +00:00
|
|
|
state *ethpb.BeaconState
|
2020-05-12 00:23:05 +00:00
|
|
|
error string
|
|
|
|
}
|
|
|
|
initTests := []test{
|
|
|
|
{
|
|
|
|
name: "nil state",
|
|
|
|
state: nil,
|
|
|
|
error: "received nil state",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil validators",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconState{
|
2020-05-12 00:23:05 +00:00
|
|
|
Slot: 4,
|
|
|
|
Validators: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty state",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconState{},
|
2020-05-12 00:23:05 +00:00
|
|
|
},
|
2020-05-14 17:38:34 +00:00
|
|
|
{
|
|
|
|
name: "full state",
|
2021-03-17 03:26:17 +00:00
|
|
|
state: pbState,
|
2020-05-14 17:38:34 +00:00
|
|
|
},
|
2020-05-12 00:23:05 +00:00
|
|
|
}
|
|
|
|
for _, tt := range initTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-06-30 15:06:19 +00:00
|
|
|
_, err := v1.InitializeFromProtoUnsafe(tt.state)
|
2020-08-25 15:23:06 +00:00
|
|
|
if tt.error != "" {
|
|
|
|
assert.ErrorContains(t, tt.error, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
2020-05-12 00:23:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 17:38:34 +00:00
|
|
|
|
|
|
|
func TestBeaconState_HashTreeRoot(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
testState, _ := util.DeterministicGenesisState(t, 64)
|
2020-05-14 17:38:34 +00:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify func(beaconState state.BeaconState) (state.BeaconState, error)
|
2020-05-14 17:38:34 +00:00
|
|
|
error string
|
|
|
|
}
|
|
|
|
initTests := []test{
|
|
|
|
{
|
|
|
|
name: "unchanged state",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different slot",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
if err := beaconState.SetSlot(5); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different validator balance",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
val, err := beaconState.ValidatorAtIndex(5)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
val.EffectiveBalance = params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement
|
|
|
|
if err := beaconState.UpdateValidatorAtIndex(5, val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var oldHTR []byte
|
|
|
|
for _, tt := range initTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
testState, err = tt.stateModify(testState)
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-14 17:38:34 +00:00
|
|
|
root, err := testState.HashTreeRoot(context.Background())
|
|
|
|
if err == nil && tt.error != "" {
|
|
|
|
t.Errorf("Expected error, expected %v, recevied %v", tt.error, err)
|
|
|
|
}
|
2021-06-30 15:06:19 +00:00
|
|
|
pbState, err := v1.ProtobufBeaconState(testState.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
genericHTR, err := pbState.HashTreeRoot()
|
2020-05-14 17:38:34 +00:00
|
|
|
if err == nil && tt.error != "" {
|
|
|
|
t.Errorf("Expected error, expected %v, recevied %v", tt.error, err)
|
|
|
|
}
|
2021-01-22 15:15:40 +00:00
|
|
|
assert.DeepNotEqual(t, []byte{}, root[:], "Received empty hash tree root")
|
|
|
|
assert.DeepEqual(t, genericHTR[:], root[:], "Expected hash tree root to match generic")
|
2020-05-14 17:38:34 +00:00
|
|
|
if len(oldHTR) != 0 && bytes.Equal(root[:], oldHTR) {
|
|
|
|
t.Errorf("Expected HTR to change, received %#x == old %#x", root, oldHTR)
|
|
|
|
}
|
|
|
|
oldHTR = root[:]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:25:55 +00:00
|
|
|
func TestBeaconState_HashTreeRoot_FieldTrie(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
testState, _ := util.DeterministicGenesisState(t, 64)
|
2020-05-14 17:38:34 +00:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify func(state.BeaconState) (state.BeaconState, error)
|
2020-05-14 17:38:34 +00:00
|
|
|
error string
|
|
|
|
}
|
|
|
|
initTests := []test{
|
|
|
|
{
|
|
|
|
name: "unchanged state",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different slot",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
if err := beaconState.SetSlot(5); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different validator balance",
|
2021-07-23 16:11:21 +00:00
|
|
|
stateModify: func(beaconState state.BeaconState) (state.BeaconState, error) {
|
2020-05-14 17:38:34 +00:00
|
|
|
val, err := beaconState.ValidatorAtIndex(5)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
val.EffectiveBalance = params.BeaconConfig().MaxEffectiveBalance - params.BeaconConfig().EffectiveBalanceIncrement
|
|
|
|
if err := beaconState.UpdateValidatorAtIndex(5, val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
},
|
|
|
|
error: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var oldHTR []byte
|
|
|
|
for _, tt := range initTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
testState, err = tt.stateModify(testState)
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-14 17:38:34 +00:00
|
|
|
root, err := testState.HashTreeRoot(context.Background())
|
|
|
|
if err == nil && tt.error != "" {
|
|
|
|
t.Errorf("Expected error, expected %v, recevied %v", tt.error, err)
|
|
|
|
}
|
2021-06-30 15:06:19 +00:00
|
|
|
pbState, err := v1.ProtobufBeaconState(testState.InnerStateUnsafe())
|
2021-03-17 03:26:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
genericHTR, err := pbState.HashTreeRoot()
|
2020-05-14 17:38:34 +00:00
|
|
|
if err == nil && tt.error != "" {
|
|
|
|
t.Errorf("Expected error, expected %v, recevied %v", tt.error, err)
|
|
|
|
}
|
2021-01-22 15:15:40 +00:00
|
|
|
assert.DeepNotEqual(t, []byte{}, root[:], "Received empty hash tree root")
|
|
|
|
assert.DeepEqual(t, genericHTR[:], root[:], "Expected hash tree root to match generic")
|
2020-05-14 17:38:34 +00:00
|
|
|
if len(oldHTR) != 0 && bytes.Equal(root[:], oldHTR) {
|
|
|
|
t.Errorf("Expected HTR to change, received %#x == old %#x", root, oldHTR)
|
|
|
|
}
|
|
|
|
oldHTR = root[:]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 05:43:15 +00:00
|
|
|
|
|
|
|
func TestBeaconState_AppendValidator_DoesntMutateCopy(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
st0, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-24 05:43:15 +00:00
|
|
|
st1 := st0.Copy()
|
|
|
|
originalCount := st1.NumValidators()
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
val := ðpb.Validator{Slashed: true}
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.NoError(t, st0.AppendValidator(val))
|
|
|
|
assert.Equal(t, originalCount, st1.NumValidators(), "st1 NumValidators mutated")
|
|
|
|
_, ok := st1.ValidatorIndexByPubkey(bytesutil.ToBytes48(val.PublicKey))
|
|
|
|
assert.Equal(t, false, ok, "Expected no validator index to be present in st1 for the newly inserted pubkey")
|
2020-07-24 05:43:15 +00:00
|
|
|
}
|