prysm-pulse/beacon-chain/state/state-native/mvslice_fuzz_test.go
terence 5a66807989
Update to V5 (#13622)
* First take at updating everything to v5

* Patch gRPC gateway to use prysm v5

Fix patch

* Update go ssz

---------

Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
2024-02-15 05:46:47 +00:00

72 lines
2.2 KiB
Go

package state_native
import (
"testing"
"github.com/prysmaticlabs/prysm/v5/config/features"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
)
func FuzzMultiValueBalances(f *testing.F) {
resetFn := features.InitWithReset(&features.Flags{
EnableExperimentalState: true,
})
defer resetFn()
bals := make([]uint64, 65536)
firstState, err := InitializeFromProtoPhase0(&ethpb.BeaconState{Balances: bals})
require.NoError(f, err)
f.Fuzz(func(t *testing.T, index uint16, value uint64) {
secondState := firstState
// there's a 25% chance we will copy the state
copyState := index%4 == 0
if copyState {
secondState = firstState.Copy()
}
if index%2 == 0 {
// update existing balance
oldValue, err := firstState.BalanceAtIndex(primitives.ValidatorIndex(index))
require.NoError(t, err)
require.NoError(t, secondState.UpdateBalancesAtIndex(primitives.ValidatorIndex(index), value))
firstValue, err := firstState.BalanceAtIndex(primitives.ValidatorIndex(index))
require.NoError(t, err)
secondValue, err := secondState.BalanceAtIndex(primitives.ValidatorIndex(index))
require.NoError(t, err)
if copyState {
require.Equal(t, oldValue, firstValue)
require.Equal(t, value, secondValue)
} else {
require.Equal(t, value, firstValue)
require.Equal(t, value, secondValue)
}
} else {
// append new balance
firstLength := firstState.BalancesLength()
require.NoError(t, secondState.AppendBalance(value))
if copyState {
require.Equal(t, firstLength, secondState.BalancesLength())
v, err := firstState.BalanceAtIndex(primitives.ValidatorIndex(firstLength - 1))
require.NoError(t, err)
require.Equal(t, value, v)
v, err = secondState.BalanceAtIndex(primitives.ValidatorIndex(secondState.BalancesLength() - 1))
require.NoError(t, err)
require.Equal(t, value, v)
} else {
require.Equal(t, firstLength+1, secondState.BalancesLength())
v, err := secondState.BalanceAtIndex(primitives.ValidatorIndex(secondState.BalancesLength() - 1))
require.NoError(t, err)
require.Equal(t, value, v)
}
}
})
}