-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process_registry_updates without a full copy of the validator set (#1…
…4130) * Avoid copying validator set in ProcessRegistryUpdates * Fix bug with sortIndices. Thanks to @terencechain for the expert debugging
- Loading branch information
1 parent
e5dd73f
commit 787e4a3
Showing
7 changed files
with
163 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package epoch | ||
|
||
import ( | ||
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
) | ||
|
||
// sortableIndices implements the Sort interface to sort newly activated validator indices | ||
// by activation epoch and by index number. | ||
type sortableIndices struct { | ||
indices []primitives.ValidatorIndex | ||
state state.ReadOnlyValidators | ||
} | ||
|
||
// Len is the number of elements in the collection. | ||
func (s sortableIndices) Len() int { return len(s.indices) } | ||
|
||
// Swap swaps the elements with indexes i and j. | ||
func (s sortableIndices) Swap(i, j int) { s.indices[i], s.indices[j] = s.indices[j], s.indices[i] } | ||
|
||
// Less reports whether the element with index i must sort before the element with index j. | ||
func (s sortableIndices) Less(i, j int) bool { | ||
vi, erri := s.state.ValidatorAtIndexReadOnly(s.indices[i]) | ||
vj, errj := s.state.ValidatorAtIndexReadOnly(s.indices[j]) | ||
|
||
if erri != nil || errj != nil { | ||
return false | ||
} | ||
|
||
a, b := vi.ActivationEligibilityEpoch(), vj.ActivationEligibilityEpoch() | ||
if a == b { | ||
return s.indices[i] < s.indices[j] | ||
} | ||
return a < b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package epoch | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native" | ||
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" | ||
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
) | ||
|
||
func TestSortableIndices(t *testing.T) { | ||
st, err := state_native.InitializeFromProtoPhase0(ð.BeaconState{ | ||
Validators: []*eth.Validator{ | ||
{ActivationEligibilityEpoch: 0}, | ||
{ActivationEligibilityEpoch: 5}, | ||
{ActivationEligibilityEpoch: 4}, | ||
{ActivationEligibilityEpoch: 4}, | ||
{ActivationEligibilityEpoch: 2}, | ||
{ActivationEligibilityEpoch: 1}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
s := sortableIndices{ | ||
indices: []primitives.ValidatorIndex{ | ||
4, | ||
2, | ||
5, | ||
3, | ||
1, | ||
0, | ||
}, | ||
state: st, | ||
} | ||
|
||
sort.Sort(s) | ||
|
||
want := []primitives.ValidatorIndex{ | ||
0, | ||
5, | ||
4, | ||
2, // Validators with the same ActivationEligibilityEpoch are sorted by index, ascending. | ||
3, | ||
1, | ||
} | ||
|
||
if !cmp.Equal(s.indices, want) { | ||
t.Errorf("Failed to sort indices correctly, wanted %v, got %v", want, s.indices) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters