From c45cb7e188dd9ee206a8cb24adabe8f5ffe1be0e Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Sat, 1 Jul 2023 10:23:25 +0800 Subject: [PATCH] Optimize Validator Roots Computation (#12585) * add changes * change it --- beacon-chain/state/fieldtrie/field_trie_helpers.go | 11 +---------- beacon-chain/state/stateutil/field_root_validator.go | 6 ++++-- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/beacon-chain/state/fieldtrie/field_trie_helpers.go b/beacon-chain/state/fieldtrie/field_trie_helpers.go index 99840f4179bd..39b4d4c244af 100644 --- a/beacon-chain/state/fieldtrie/field_trie_helpers.go +++ b/beacon-chain/state/fieldtrie/field_trie_helpers.go @@ -207,7 +207,7 @@ func handle32ByteArrays(val [][32]byte, indices []uint64, convertAll bool) ([][3 func handleValidatorSlice(val []*ethpb.Validator, indices []uint64, convertAll bool) ([][32]byte, error) { length := len(indices) if convertAll { - length = len(val) + return stateutil.OptimizedValidatorRoots(val) } roots := make([][32]byte, 0, length) rootCreator := func(input *ethpb.Validator) error { @@ -218,15 +218,6 @@ func handleValidatorSlice(val []*ethpb.Validator, indices []uint64, convertAll b roots = append(roots, newRoot) return nil } - if convertAll { - for i := range val { - err := rootCreator(val[i]) - if err != nil { - return nil, err - } - } - return roots, nil - } if len(val) > 0 { for _, idx := range indices { if idx > uint64(len(val))-1 { diff --git a/beacon-chain/state/stateutil/field_root_validator.go b/beacon-chain/state/stateutil/field_root_validator.go index a3c5d3e44af9..4f88c2464278 100644 --- a/beacon-chain/state/stateutil/field_root_validator.go +++ b/beacon-chain/state/stateutil/field_root_validator.go @@ -30,7 +30,7 @@ func ValidatorRegistryRoot(vals []*ethpb.Validator) ([32]byte, error) { } func validatorRegistryRoot(validators []*ethpb.Validator) ([32]byte, error) { - roots, err := optimizedValidatorRoots(validators) + roots, err := OptimizedValidatorRoots(validators) if err != nil { return [32]byte{}, err } @@ -51,7 +51,9 @@ func validatorRegistryRoot(validators []*ethpb.Validator) ([32]byte, error) { return res, nil } -func optimizedValidatorRoots(validators []*ethpb.Validator) ([][32]byte, error) { +// OptimizedValidatorRoots uses an optimized routine with gohashtree in order to +// derive a list of validator roots from a list of validator objects. +func OptimizedValidatorRoots(validators []*ethpb.Validator) ([][32]byte, error) { // Exit early if no validators are provided. if len(validators) == 0 { return [][32]byte{}, nil