2021-11-25 20:41:05 +00:00
|
|
|
package v3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
|
|
stateTypes "github.com/prysmaticlabs/prysm/beacon-chain/state/types"
|
2022-01-06 17:33:08 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-11-25 20:41:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppendBeyondIndicesLimit(t *testing.T) {
|
|
|
|
zeroHash := params.BeaconConfig().ZeroHash
|
|
|
|
mockblockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockblockRoots); i++ {
|
|
|
|
mockblockRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
mockstateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockstateRoots); i++ {
|
|
|
|
mockstateRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
mockrandaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(mockrandaoMixes); i++ {
|
|
|
|
mockrandaoMixes[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
payload := ðpb.ExecutionPayloadHeader{
|
|
|
|
ParentHash: make([]byte, 32),
|
|
|
|
FeeRecipient: make([]byte, 20),
|
|
|
|
StateRoot: make([]byte, 32),
|
|
|
|
ReceiptRoot: make([]byte, 32),
|
|
|
|
LogsBloom: make([]byte, 256),
|
2022-02-26 03:47:16 +00:00
|
|
|
PrevRandao: make([]byte, 32),
|
2021-11-25 20:41:05 +00:00
|
|
|
BaseFeePerGas: make([]byte, 32),
|
|
|
|
BlockHash: make([]byte, 32),
|
|
|
|
TransactionsRoot: make([]byte, 32),
|
|
|
|
}
|
2022-01-10 16:47:30 +00:00
|
|
|
st, err := InitializeFromProto(ðpb.BeaconStateBellatrix{
|
2021-11-25 20:41:05 +00:00
|
|
|
Slot: 1,
|
|
|
|
CurrentEpochParticipation: []byte{},
|
|
|
|
PreviousEpochParticipation: []byte{},
|
2021-12-07 17:52:39 +00:00
|
|
|
Validators: []*ethpb.Validator{},
|
|
|
|
Eth1Data: ðpb.Eth1Data{},
|
2021-11-25 20:41:05 +00:00
|
|
|
BlockRoots: mockblockRoots,
|
|
|
|
StateRoots: mockstateRoots,
|
|
|
|
RandaoMixes: mockrandaoMixes,
|
|
|
|
LatestExecutionPayloadHeader: payload,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = st.HashTreeRoot(context.Background())
|
2022-02-08 09:30:06 +00:00
|
|
|
s, ok := st.(*BeaconState)
|
|
|
|
require.Equal(t, true, ok)
|
2021-11-25 20:41:05 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-10 16:47:30 +00:00
|
|
|
for i := stateTypes.FieldIndex(0); i < stateTypes.FieldIndex(params.BeaconConfig().BeaconStateBellatrixFieldCount); i++ {
|
2022-02-08 09:30:06 +00:00
|
|
|
s.dirtyFields[i] = true
|
2021-11-25 20:41:05 +00:00
|
|
|
}
|
|
|
|
_, err = st.HashTreeRoot(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i := 0; i < 10; i++ {
|
2021-12-07 17:52:39 +00:00
|
|
|
assert.NoError(t, st.AppendValidator(ðpb.Validator{}))
|
2021-11-25 20:41:05 +00:00
|
|
|
}
|
2022-02-08 09:30:06 +00:00
|
|
|
assert.Equal(t, false, s.rebuildTrie[validators])
|
|
|
|
assert.NotEqual(t, len(s.dirtyIndices[validators]), 0)
|
2021-11-25 20:41:05 +00:00
|
|
|
|
|
|
|
for i := 0; i < indicesLimit; i++ {
|
2021-12-07 17:52:39 +00:00
|
|
|
assert.NoError(t, st.AppendValidator(ðpb.Validator{}))
|
2021-11-25 20:41:05 +00:00
|
|
|
}
|
2022-02-08 09:30:06 +00:00
|
|
|
assert.Equal(t, true, s.rebuildTrie[validators])
|
|
|
|
assert.Equal(t, len(s.dirtyIndices[validators]), 0)
|
2021-11-25 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_AppendBalanceWithTrie(t *testing.T) {
|
|
|
|
count := uint64(100)
|
|
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
|
|
bals := make([]uint64, 0, count)
|
|
|
|
for i := uint64(1); i < count; i++ {
|
|
|
|
someRoot := [32]byte{}
|
2022-01-06 17:33:08 +00:00
|
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
2021-11-25 20:41:05 +00:00
|
|
|
copy(someRoot[:], strconv.Itoa(int(i)))
|
|
|
|
copy(someKey[:], strconv.Itoa(int(i)))
|
|
|
|
vals = append(vals, ðpb.Validator{
|
|
|
|
PublicKey: someKey[:],
|
|
|
|
WithdrawalCredentials: someRoot[:],
|
|
|
|
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
Slashed: false,
|
|
|
|
ActivationEligibilityEpoch: 1,
|
|
|
|
ActivationEpoch: 1,
|
|
|
|
ExitEpoch: 1,
|
|
|
|
WithdrawableEpoch: 1,
|
|
|
|
})
|
|
|
|
bals = append(bals, params.BeaconConfig().MaxEffectiveBalance)
|
|
|
|
}
|
|
|
|
zeroHash := params.BeaconConfig().ZeroHash
|
|
|
|
mockblockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockblockRoots); i++ {
|
|
|
|
mockblockRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
mockstateRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(mockstateRoots); i++ {
|
|
|
|
mockstateRoots[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
mockrandaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(mockrandaoMixes); i++ {
|
|
|
|
mockrandaoMixes[i] = zeroHash[:]
|
|
|
|
}
|
|
|
|
var pubKeys [][]byte
|
|
|
|
for i := uint64(0); i < params.BeaconConfig().SyncCommitteeSize; i++ {
|
|
|
|
pubKeys = append(pubKeys, bytesutil.PadTo([]byte{}, params.BeaconConfig().BLSPubkeyLength))
|
|
|
|
}
|
|
|
|
payload := ðpb.ExecutionPayloadHeader{
|
|
|
|
ParentHash: make([]byte, 32),
|
|
|
|
FeeRecipient: make([]byte, 20),
|
|
|
|
StateRoot: make([]byte, 32),
|
|
|
|
ReceiptRoot: make([]byte, 32),
|
|
|
|
LogsBloom: make([]byte, 256),
|
2022-02-26 03:47:16 +00:00
|
|
|
PrevRandao: make([]byte, 32),
|
2021-11-25 20:41:05 +00:00
|
|
|
BaseFeePerGas: make([]byte, 32),
|
|
|
|
BlockHash: make([]byte, 32),
|
|
|
|
TransactionsRoot: make([]byte, 32),
|
|
|
|
}
|
2022-01-10 16:47:30 +00:00
|
|
|
st, err := InitializeFromProto(ðpb.BeaconStateBellatrix{
|
2021-11-25 20:41:05 +00:00
|
|
|
Slot: 1,
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: make([]byte, 4),
|
|
|
|
CurrentVersion: make([]byte, 4),
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
LatestBlockHeader: ðpb.BeaconBlockHeader{
|
|
|
|
ParentRoot: make([]byte, 32),
|
|
|
|
StateRoot: make([]byte, 32),
|
|
|
|
BodyRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
CurrentEpochParticipation: []byte{},
|
|
|
|
PreviousEpochParticipation: []byte{},
|
|
|
|
Validators: vals,
|
|
|
|
Balances: bals,
|
2021-12-07 17:52:39 +00:00
|
|
|
Eth1Data: ðpb.Eth1Data{
|
2021-11-25 20:41:05 +00:00
|
|
|
DepositRoot: make([]byte, 32),
|
|
|
|
BlockHash: make([]byte, 32),
|
|
|
|
},
|
|
|
|
BlockRoots: mockblockRoots,
|
|
|
|
StateRoots: mockstateRoots,
|
|
|
|
RandaoMixes: mockrandaoMixes,
|
|
|
|
JustificationBits: bitfield.NewBitvector4(),
|
|
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
|
|
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
|
|
|
|
CurrentSyncCommittee: ðpb.SyncCommittee{
|
|
|
|
Pubkeys: pubKeys,
|
|
|
|
AggregatePubkey: make([]byte, 48),
|
|
|
|
},
|
|
|
|
NextSyncCommittee: ðpb.SyncCommittee{
|
|
|
|
Pubkeys: pubKeys,
|
|
|
|
AggregatePubkey: make([]byte, 48),
|
|
|
|
},
|
|
|
|
LatestExecutionPayloadHeader: payload,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = st.HashTreeRoot(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
if i%2 == 0 {
|
|
|
|
assert.NoError(t, st.UpdateBalancesAtIndex(types.ValidatorIndex(i), 1000))
|
|
|
|
}
|
|
|
|
if i%3 == 0 {
|
|
|
|
assert.NoError(t, st.AppendBalance(1000))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = st.HashTreeRoot(context.Background())
|
|
|
|
assert.NoError(t, err)
|
2022-02-08 09:30:06 +00:00
|
|
|
s, ok := st.(*BeaconState)
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
newRt := bytesutil.ToBytes32(s.merkleLayers[0][balances])
|
|
|
|
wantedRt, err := stateutil.Uint64ListRootWithRegistryLimit(s.state.Balances)
|
2021-11-25 20:41:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, wantedRt, newRt, "state roots are unequal")
|
|
|
|
}
|