Compare commits

...

3 Commits

Author SHA1 Message Date
Nishant Das
bc9151d647 Fix Multivalue Slice Deadlock (#13087)
* fix deadlock

* gofmt

* lint

(cherry picked from commit f91efafe24)
2023-10-23 06:49:21 -05:00
Preston Van Loon
747d7b5190 rpc/apimiddleware: Test all paths can be created (#13073)
(cherry picked from commit f592bf7f07)
2023-10-18 19:39:10 -05:00
james-prysm
05874df18a removing bad path
(cherry picked from commit a7e56058c1)
2023-10-18 19:38:51 -05:00
5 changed files with 56 additions and 8 deletions

View File

@@ -33,6 +33,7 @@ go_test(
srcs = [
"custom_handlers_test.go",
"custom_hooks_test.go",
"endpoint_factory_test.go",
"structs_marshalling_test.go",
],
embed = [":go_default_library"],

View File

@@ -19,7 +19,6 @@ func (_ *BeaconEndpointFactory) Paths() []string {
"/eth/v1/beacon/states/{state_id}/root",
"/eth/v1/beacon/states/{state_id}/sync_committees",
"/eth/v1/beacon/states/{state_id}/randao",
"/eth/v1/beacon/blinded_blocks",
"/eth/v1/beacon/blocks/{block_id}",
"/eth/v2/beacon/blocks/{block_id}",
"/eth/v1/beacon/blocks/{block_id}/attestations",

View File

@@ -0,0 +1,17 @@
package apimiddleware_test
import (
"testing"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/apimiddleware"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func TestBeaconEndpointFactory_AllPathsRegistered(t *testing.T) {
f := &apimiddleware.BeaconEndpointFactory{}
for _, p := range f.Paths() {
_, err := f.Create(p)
require.NoError(t, err, "failed to register %s", p)
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/types"
"github.com/prysmaticlabs/prysm/v4/config/features"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/testing/assert"
@@ -1015,6 +1016,43 @@ func TestValidatorReferences_RemainsConsistent_Bellatrix(t *testing.T) {
}))
}
func TestValidatorReferences_ApplyValidator_BalancesRead(t *testing.T) {
resetCfg := features.InitWithReset(&features.Flags{
EnableExperimentalState: true,
})
defer resetCfg()
s, err := InitializeFromProtoUnsafeAltair(&ethpb.BeaconStateAltair{
Validators: []*ethpb.Validator{
{PublicKey: []byte{'A'}},
{PublicKey: []byte{'B'}},
{PublicKey: []byte{'C'}},
{PublicKey: []byte{'D'}},
{PublicKey: []byte{'E'}},
},
Balances: []uint64{0, 0, 0, 0, 0},
})
require.NoError(t, err)
a, ok := s.(*BeaconState)
require.Equal(t, true, ok)
// Create a second state.
copied := a.Copy()
b, ok := copied.(*BeaconState)
require.Equal(t, true, ok)
// Modify all validators from copied state, it should not deadlock.
assert.NoError(t, b.ApplyToEveryValidator(func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error) {
b, err := b.BalanceAtIndex(0)
if err != nil {
return false, nil, err
}
newVal := ethpb.CopyValidator(val)
newVal.EffectiveBalance += b
val.EffectiveBalance += b
return true, val, nil
}))
}
// assertRefCount checks whether reference count for a given state
// at a given index is equal to expected amount.
func assertRefCount(t *testing.T, b *BeaconState, idx types.FieldIndex, want uint) {

View File

@@ -40,30 +40,23 @@ func (b *BeaconState) SetValidators(val []*ethpb.Validator) error {
func (b *BeaconState) ApplyToEveryValidator(f func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error)) error {
var changedVals []uint64
if features.Get().EnableExperimentalState {
b.lock.Lock()
l := b.validatorsMultiValue.Len(b)
for i := 0; i < l; i++ {
v, err := b.validatorsMultiValue.At(b, uint64(i))
if err != nil {
b.lock.Unlock()
return err
}
changed, newVal, err := f(i, v)
if err != nil {
b.lock.Unlock()
return err
}
if changed {
changedVals = append(changedVals, uint64(i))
if err = b.validatorsMultiValue.UpdateAt(b, uint64(i), newVal); err != nil {
b.lock.Unlock()
return errors.Wrapf(err, "could not update validator at index %d", i)
}
}
}
b.lock.Unlock()
} else {
b.lock.Lock()