From 8886fa8ca9671fdd8125e0acdcfb9952d4640c62 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Fri, 3 Dec 2021 16:33:34 +0300 Subject: [PATCH] neotest: simplify committee account construction Simple `sort.Slice` is more readable than multi-level for-loop. Signed-off-by: Evgeniy Stratonikov --- pkg/neotest/chain/chain.go | 44 +++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/pkg/neotest/chain/chain.go b/pkg/neotest/chain/chain.go index 732bfc2fb..53ca25f71 100644 --- a/pkg/neotest/chain/chain.go +++ b/pkg/neotest/chain/chain.go @@ -75,37 +75,33 @@ func init() { multiValidatorAcc = make([]*wallet.Account, mv) sort.Sort(pubs[:4]) -vloop: - for i := 0; i < mv; i++ { - for j := range accs { - if accs[j].PrivateKey().PublicKey().Equal(pubs[i]) { - multiValidatorAcc[i] = wallet.NewAccountFromPrivateKey(accs[j].PrivateKey()) - err := multiValidatorAcc[i].ConvertMultisig(mv, pubs[:4]) - if err != nil { - panic(err) - } - continue vloop - } + sort.Slice(accs[:4], func(i, j int) bool { + p1 := accs[i].PrivateKey().PublicKey() + p2 := accs[j].PrivateKey().PublicKey() + return p1.Cmp(p2) == -1 + }) + for i := range multiValidatorAcc { + multiValidatorAcc[i] = wallet.NewAccountFromPrivateKey(accs[i].PrivateKey()) + err := multiValidatorAcc[i].ConvertMultisig(mv, pubs[:4]) + if err != nil { + panic(err) } - panic("invalid committee WIFs") } multiCommitteeAcc = make([]*wallet.Account, mc) sort.Sort(pubs) -cloop: - for i := 0; i < mc; i++ { - for j := range accs { - if accs[j].PrivateKey().PublicKey().Equal(pubs[i]) { - multiCommitteeAcc[i] = wallet.NewAccountFromPrivateKey(accs[j].PrivateKey()) - err := multiCommitteeAcc[i].ConvertMultisig(mc, pubs) - if err != nil { - panic(err) - } - continue cloop - } + sort.Slice(accs, func(i, j int) bool { + p1 := accs[i].PrivateKey().PublicKey() + p2 := accs[j].PrivateKey().PublicKey() + return p1.Cmp(p2) == -1 + }) + for i := range multiCommitteeAcc { + multiCommitteeAcc[i] = wallet.NewAccountFromPrivateKey(accs[i].PrivateKey()) + err := multiCommitteeAcc[i].ConvertMultisig(mc, pubs) + if err != nil { + panic(err) } - panic("invalid committee WIFs") } }