mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
* v0 * getters/setters * init and copy * hasher * all the nice stuff * make bazel happy * remove tests for smaller PR * remove old states * move files * import fixes * custom MarshalSSZ * fixed deadlock * copy version when copying state * correct issues in state_trie * fix Copy() * better e2e comment * add code to minimal state * spectest test * Revert "Auxiliary commit to revert individual files from 84154423464e8372f7e0a03367403656ac5cd78e" This reverts commit 9602599d183081291dfa0ba4f1036430f63a7822. * native state assert * always error * always log * more native state usage * cleanup * remove empty line * Revert "spectests" This reverts commit 1c49bed5d1cf6224afaf21e18562bf72fae5d2b6. # Conflicts: # beacon-chain/powchain/service.go # beacon-chain/state/v1/state_trie.go # beacon-chain/state/v2/state_trie.go # beacon-chain/state/v3/state_trie.go # testing/spectest/shared/phase0/finality/BUILD.bazel # testing/spectest/shared/phase0/finality/runner.go * dedup field trie * fix test issues * cleanup * use correct field num in FinalizedRootProof * use existing version constant * halfway there * "working" version * some fixes * fix field nums in tests * rename v0types to nativetypes * Revert "Auxiliary commit to revert individual files from dc549b1cf8e724bd08cee1ecc760ff3771d5592d" This reverts commit 7254d3070d8693b283fc686a2e01a822ecbac1b3. * uncomment code * remove map size * Revert "Revert "spectests"" This reverts commit39c271ae6b. * use reverse map * Revert "Revert "Revert "spectests""" This reverts commit19ba8cf95c. * finally found the bug (cherry picked from commit a5414c4be1bdb61a50b391ea5301895e772cc5e9) * simplify populateFieldIndexes * fix copy (cherry picked from commit 7da4fb8cf51557ef931bb781872ea52fc6731af5) * remove native state from e2e * remove index map * unsupported functions * Use ProtobufBeaconState() from native state * tests * typo * reduce complexity of `SaveStatesEfficient` * remove unused receiver name * update doc.go * fix test assertion * fix test assertion 2 * Phase0 justification bits * bring back state tests * rename fieldIndexRev * versioning of ToProto * remove version check from unexported function * hasher tests * don't return error from JustificationBits * extract fieldConvertersNative * helper error function * use fieldConvertersNative * Introduce RealPosition method on FieldIndex * use RealPosition in hasher * remove unused fields * remove TestAppendBeyondIndicesLimit (cherry picked from commit 3017e700282969c30006b64c95c21ffe6b166f8b) * simplify RealPosition * rename field interface * use helper in proofs.go * Update beacon-chain/core/altair/upgrade.go Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
317 lines
9.9 KiB
Go
317 lines
9.9 KiB
Go
package state_native
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
nativetypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/types"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
|
"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 TestValidatorMap_DistinctCopy(t *testing.T) {
|
|
count := uint64(100)
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
for i := uint64(1); i < count; i++ {
|
|
someRoot := [32]byte{}
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
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,
|
|
})
|
|
}
|
|
handler := stateutil.NewValMapHandler(vals)
|
|
newHandler := handler.Copy()
|
|
wantedPubkey := strconv.Itoa(22)
|
|
handler.Set(bytesutil.ToBytes48([]byte(wantedPubkey)), 27)
|
|
val1, _ := handler.Get(bytesutil.ToBytes48([]byte(wantedPubkey)))
|
|
val2, _ := newHandler.Get(bytesutil.ToBytes48([]byte(wantedPubkey)))
|
|
assert.NotEqual(t, val1, val2, "Values are supposed to be unequal due to copy")
|
|
}
|
|
|
|
func TestBeaconState_NoDeadlock_Phase0(t *testing.T) {
|
|
count := uint64(100)
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
for i := uint64(1); i < count; i++ {
|
|
someRoot := [32]byte{}
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
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,
|
|
})
|
|
}
|
|
newState, err := InitializeFromProtoUnsafePhase0(ðpb.BeaconState{
|
|
Validators: vals,
|
|
})
|
|
assert.NoError(t, err)
|
|
st, ok := newState.(*BeaconState)
|
|
require.Equal(t, true, ok)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
// Continuously lock and unlock the state
|
|
// by acquiring the lock.
|
|
for i := 0; i < 1000; i++ {
|
|
for _, f := range st.stateFieldLeaves {
|
|
f.Lock()
|
|
if f.Empty() {
|
|
f.InsertFieldLayer(make([][]*[32]byte, 10))
|
|
}
|
|
f.Unlock()
|
|
f.FieldReference().AddRef()
|
|
}
|
|
}
|
|
wg.Done()
|
|
}()
|
|
// Constantly read from the offending portion
|
|
// of the code to ensure there is no possible
|
|
// recursive read locking.
|
|
for i := 0; i < 1000; i++ {
|
|
go func() {
|
|
_ = st.FieldReferencesCount()
|
|
}()
|
|
}
|
|
// Test will not terminate in the event of a deadlock.
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestBeaconState_NoDeadlock_Altair(t *testing.T) {
|
|
count := uint64(100)
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
for i := uint64(1); i < count; i++ {
|
|
someRoot := [32]byte{}
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
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,
|
|
})
|
|
}
|
|
st, err := InitializeFromProtoUnsafeAltair(ðpb.BeaconStateAltair{
|
|
Validators: vals,
|
|
})
|
|
assert.NoError(t, err)
|
|
s, ok := st.(*BeaconState)
|
|
require.Equal(t, true, ok)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
// Continuously lock and unlock the state
|
|
// by acquiring the lock.
|
|
for i := 0; i < 1000; i++ {
|
|
for _, f := range s.stateFieldLeaves {
|
|
f.Lock()
|
|
if f.Empty() {
|
|
f.InsertFieldLayer(make([][]*[32]byte, 10))
|
|
}
|
|
f.Unlock()
|
|
f.FieldReference().AddRef()
|
|
}
|
|
}
|
|
wg.Done()
|
|
}()
|
|
// Constantly read from the offending portion
|
|
// of the code to ensure there is no possible
|
|
// recursive read locking.
|
|
for i := 0; i < 1000; i++ {
|
|
go func() {
|
|
_ = st.FieldReferencesCount()
|
|
}()
|
|
}
|
|
// Test will not terminate in the event of a deadlock.
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestBeaconState_NoDeadlock_Bellatrix(t *testing.T) {
|
|
count := uint64(100)
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
for i := uint64(1); i < count; i++ {
|
|
someRoot := [32]byte{}
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
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,
|
|
})
|
|
}
|
|
st, err := InitializeFromProtoUnsafeBellatrix(ðpb.BeaconStateBellatrix{
|
|
Validators: vals,
|
|
})
|
|
assert.NoError(t, err)
|
|
s, ok := st.(*BeaconState)
|
|
require.Equal(t, true, ok)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
// Continuously lock and unlock the state
|
|
// by acquiring the lock.
|
|
for i := 0; i < 1000; i++ {
|
|
for _, f := range s.stateFieldLeaves {
|
|
f.Lock()
|
|
if f.Empty() {
|
|
f.InsertFieldLayer(make([][]*[32]byte, 10))
|
|
}
|
|
f.Unlock()
|
|
f.FieldReference().AddRef()
|
|
}
|
|
}
|
|
wg.Done()
|
|
}()
|
|
// Constantly read from the offending portion
|
|
// of the code to ensure there is no possible
|
|
// recursive read locking.
|
|
for i := 0; i < 1000; i++ {
|
|
go func() {
|
|
_ = st.FieldReferencesCount()
|
|
}()
|
|
}
|
|
// Test will not terminate in the event of a deadlock.
|
|
wg.Wait()
|
|
}
|
|
|
|
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{}
|
|
someKey := [fieldparams.BLSPubkeyLength]byte{}
|
|
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[:]
|
|
}
|
|
newState, err := InitializeFromProtoPhase0(ðpb.BeaconState{
|
|
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, fieldparams.RootLength),
|
|
StateRoot: make([]byte, fieldparams.RootLength),
|
|
BodyRoot: make([]byte, fieldparams.RootLength),
|
|
},
|
|
Validators: vals,
|
|
Balances: bals,
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositRoot: make([]byte, 32),
|
|
BlockHash: make([]byte, 32),
|
|
},
|
|
BlockRoots: mockblockRoots,
|
|
StateRoots: mockstateRoots,
|
|
RandaoMixes: mockrandaoMixes,
|
|
JustificationBits: bitfield.NewBitvector4(),
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
|
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
|
|
})
|
|
assert.NoError(t, err)
|
|
st, ok := newState.(*BeaconState)
|
|
require.Equal(t, true, ok)
|
|
_, 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)
|
|
newRt := bytesutil.ToBytes32(st.merkleLayers[0][nativetypes.Balances])
|
|
wantedRt, err := stateutil.Uint64ListRootWithRegistryLimit(st.Balances())
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, wantedRt, newRt, "state roots are unequal")
|
|
}
|
|
|
|
func TestBeaconState_ModifyPreviousParticipationBits(t *testing.T) {
|
|
st, err := InitializeFromProtoUnsafePhase0(ðpb.BeaconState{})
|
|
assert.NoError(t, err)
|
|
assert.ErrorContains(t, "ModifyPreviousParticipationBits is not supported", st.ModifyPreviousParticipationBits(func(val []byte) ([]byte, error) {
|
|
return nil, nil
|
|
}))
|
|
}
|
|
|
|
func TestBeaconState_ModifyCurrentParticipationBits(t *testing.T) {
|
|
st, err := InitializeFromProtoUnsafePhase0(ðpb.BeaconState{})
|
|
assert.NoError(t, err)
|
|
assert.ErrorContains(t, "ModifyCurrentParticipationBits is not supported", st.ModifyCurrentParticipationBits(func(val []byte) ([]byte, error) {
|
|
return nil, nil
|
|
}))
|
|
}
|