From f485e3119c9c821332192fee64e00eb01ba65c96 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 5 May 2021 18:20:04 +0700 Subject: [PATCH] x/staking: lazily get consensus key address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The consensus key address is only used for error reporting, but benchmarking shown that it takes a big number of running time. By getting the consensus lazily, the validation process both run faster and use less memory. ValidateGenesis10Validators-8 2.06µs ± 0% 1.13µs ± 1% -45.15% (p=0.008 n=5+5) ValidateGenesis100Validators-8 19.1µs ± 0% 10.1µs ± 0% -47.47% (p=0.008 n=5+5) ValidateGenesis400Validators-8 75.7µs ± 2% 38.3µs ± 0% -49.43% (p=0.008 n=5+5) name old alloc/op new alloc/op delta ValidateGenesis10Validators-8 987B ± 0% 667B ± 0% -32.42% (p=0.008 n=5+5) ValidateGenesis100Validators-8 9.42kB ± 0% 6.22kB ± 0% -33.97% (p=0.008 n=5+5) ValidateGenesis400Validators-8 37.2kB ± 0% 24.4kB ± 0% -34.37% (p=0.008 n=5+5) name old allocs/op new allocs/op delta ValidateGenesis10Validators-8 23.0 ± 0% 13.0 ± 0% -43.48% (p=0.008 n=5+5) ValidateGenesis100Validators-8 205 ± 0% 105 ± 0% -48.78% (p=0.008 n=5+5) ValidateGenesis400Validators-8 808 ± 0% 408 ± 0% -49.50% (p=0.008 n=5+5) Fixes #9263 --- x/staking/genesis.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/x/staking/genesis.go b/x/staking/genesis.go index d433d0f01b73..34a5be852a02 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -237,17 +237,22 @@ func validateGenesisStateValidators(validators []types.Validator) error { if err != nil { return err } - consAddr, err := val.GetConsAddr() - if err != nil { - return err - } + strKey := string(consPk.Bytes()) if _, ok := addrMap[strKey]; ok { + consAddr, err := val.GetConsAddr() + if err != nil { + return err + } return fmt.Errorf("duplicate validator in genesis state: moniker %v, address %v", val.Description.Moniker, consAddr) } if val.Jailed && val.IsBonded() { + consAddr, err := val.GetConsAddr() + if err != nil { + return err + } return fmt.Errorf("validator is bonded and jailed in genesis state: moniker %v, address %v", val.Description.Moniker, consAddr) }