2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2021-01-25 03:01:55 +00:00
|
|
|
|
|
|
|
import (
|
2021-11-19 12:01:15 +00:00
|
|
|
"context"
|
2021-01-25 03:01:55 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-19 12:01:15 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
2021-03-11 02:57:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2021-12-14 18:42:05 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
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-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
2021-01-25 03:01:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 := [48]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,
|
|
|
|
})
|
|
|
|
}
|
2021-03-11 02:57:46 +00:00
|
|
|
handler := stateutil.NewValMapHandler(vals)
|
|
|
|
newHandler := handler.Copy()
|
2021-01-25 03:01:55 +00:00
|
|
|
wantedPubkey := strconv.Itoa(22)
|
2021-03-11 02:57:46 +00:00
|
|
|
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")
|
2021-01-25 03:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeaconState_NoDeadlock(t *testing.T) {
|
|
|
|
count := uint64(100)
|
|
|
|
vals := make([]*ethpb.Validator, 0, count)
|
|
|
|
for i := uint64(1); i < count; i++ {
|
|
|
|
someRoot := [32]byte{}
|
|
|
|
someKey := [48]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,
|
|
|
|
})
|
|
|
|
}
|
2021-07-29 21:45:17 +00:00
|
|
|
st, err := InitializeFromProtoUnsafe(ðpb.BeaconState{
|
2021-01-25 03:01:55 +00:00
|
|
|
Validators: vals,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-03-08 22:37:33 +00:00
|
|
|
|
2021-01-25 03:01:55 +00:00
|
|
|
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()
|
2021-08-09 21:54:24 +00:00
|
|
|
if f.Empty() {
|
|
|
|
f.InsertFieldLayer(make([][]*[32]byte, 10))
|
2021-01-25 03:01:55 +00:00
|
|
|
}
|
|
|
|
f.Unlock()
|
2021-08-09 21:54:24 +00:00
|
|
|
f.FieldReference().AddRef()
|
2021-01-25 03:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
2021-05-24 04:55:42 +00:00
|
|
|
|
|
|
|
func TestStateTrie_IsNil(t *testing.T) {
|
|
|
|
var emptyState *BeaconState
|
|
|
|
assert.Equal(t, true, emptyState.IsNil())
|
|
|
|
|
|
|
|
emptyProto := &BeaconState{state: nil}
|
|
|
|
assert.Equal(t, true, emptyProto.IsNil())
|
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
nonNilState := &BeaconState{state: ðpb.BeaconState{}}
|
2021-05-24 04:55:42 +00:00
|
|
|
assert.Equal(t, false, nonNilState.IsNil())
|
|
|
|
}
|
2021-11-19 12:01:15 +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{}
|
|
|
|
someKey := [48]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[:]
|
|
|
|
}
|
|
|
|
st, err := InitializeFromProto(ðpb.BeaconState{
|
|
|
|
Slot: 1,
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: make([]byte, 4),
|
|
|
|
CurrentVersion: make([]byte, 4),
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
LatestBlockHeader: ðpb.BeaconBlockHeader{
|
2021-12-14 18:42:05 +00:00
|
|
|
ParentRoot: make([]byte, fieldparams.RootLength),
|
|
|
|
StateRoot: make([]byte, fieldparams.RootLength),
|
|
|
|
BodyRoot: make([]byte, fieldparams.RootLength),
|
2021-11-19 12:01:15 +00:00
|
|
|
},
|
|
|
|
Validators: vals,
|
|
|
|
Balances: bals,
|
|
|
|
Eth1Data: ðpb.Eth1Data{
|
|
|
|
DepositRoot: make([]byte, 32),
|
|
|
|
BlockHash: make([]byte, 32),
|
|
|
|
},
|
|
|
|
BlockRoots: mockblockRoots,
|
|
|
|
StateRoots: mockstateRoots,
|
|
|
|
RandaoMixes: mockrandaoMixes,
|
|
|
|
JustificationBits: bitfield.NewBitvector4(),
|
2021-12-14 18:42:05 +00:00
|
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
|
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
|
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
|
2021-11-19 12:01:15 +00:00
|
|
|
Slashings: make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector),
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
newRt := bytesutil.ToBytes32(st.merkleLayers[0][balances])
|
|
|
|
wantedRt, err := stateutil.Uint64ListRootWithRegistryLimit(st.state.Balances)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, wantedRt, newRt, "state roots are unequal")
|
|
|
|
}
|