2021-07-27 15:47:03 +00:00
|
|
|
package v2
|
2021-07-19 16:47:42 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-27 15:47:03 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-07-19 16:47:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-07-27 15:47:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
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-27 15:47:03 +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"
|
2021-07-19 16:47:42 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 15:47:03 +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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-07-19 16:47:42 +00:00
|
|
|
func TestInitializeFromProto(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-29 21:45:17 +00:00
|
|
|
state *ethpb.BeaconStateAltair
|
2021-07-19 16:47:42 +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.BeaconStateAltair{
|
2021-07-19 16:47:42 +00:00
|
|
|
Slot: 4,
|
|
|
|
Validators: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty state",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconStateAltair{},
|
2021-07-19 16:47:42 +00:00
|
|
|
},
|
|
|
|
// TODO: Add full state. Blocked by testutil migration.
|
|
|
|
}
|
|
|
|
for _, tt := range initTests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-07-27 15:47:03 +00:00
|
|
|
_, err := InitializeFromProto(tt.state)
|
2021-07-19 16:47:42 +00:00
|
|
|
if tt.error != "" {
|
|
|
|
require.ErrorContains(t, tt.error, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 15:47:03 +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.BeaconStateAltair{
|
2021-07-27 15:47:03 +00:00
|
|
|
Validators: vals,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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-07-27 15:47:03 +00:00
|
|
|
}
|
|
|
|
f.Unlock()
|
2021-08-09 21:54:24 +00:00
|
|
|
f.FieldReference().AddRef()
|
2021-07-27 15:47:03 +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-07-19 16:47:42 +00:00
|
|
|
func TestInitializeFromProtoUnsafe(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
name string
|
2021-07-29 21:45:17 +00:00
|
|
|
state *ethpb.BeaconStateAltair
|
2021-07-19 16:47:42 +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.BeaconStateAltair{
|
2021-07-19 16:47:42 +00:00
|
|
|
Slot: 4,
|
|
|
|
Validators: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty state",
|
2021-07-29 21:45:17 +00:00
|
|
|
state: ðpb.BeaconStateAltair{},
|
2021-07-19 16:47:42 +00:00
|
|
|
},
|
|
|
|
// TODO: Add full state. Blocked by testutil migration.
|
|
|
|
}
|
2021-07-27 15:47:03 +00:00
|
|
|
_ = initTests
|
2021-07-19 16:47:42 +00:00
|
|
|
}
|