From 01756705f606b686c3dca06d3a8f8b1c30e8fe8d Mon Sep 17 00:00:00 2001 From: Alexander Peters Date: Tue, 7 May 2024 11:17:47 +0200 Subject: [PATCH] fix(sims): Prevent duplicates in sims accounts (#20265) --- types/simulation/account.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/types/simulation/account.go b/types/simulation/account.go index 48a0ed34e32e..0c3f786b5f36 100644 --- a/types/simulation/account.go +++ b/types/simulation/account.go @@ -31,22 +31,32 @@ func RandomAcc(r *rand.Rand, accs []Account) (Account, int) { return accs[idx], idx } -// RandomAccounts generates n random accounts +// RandomAccounts deterministic generates n random accounts without duplicates. func RandomAccounts(r *rand.Rand, n int) []Account { accs := make([]Account, n) - - for i := 0; i < n; i++ { + idx := make(map[string]struct{}, n) + var i int + for i < n { // don't need that much entropy for simulation privkeySeed := make([]byte, 15) - r.Read(privkeySeed) - - accs[i].PrivKey = secp256k1.GenPrivKeyFromSecret(privkeySeed) - accs[i].PubKey = accs[i].PrivKey.PubKey() - accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) - - accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(privkeySeed) + if _, err := r.Read(privkeySeed); err != nil { + panic(err) + } + privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed) + pubKey := privKey.PubKey() + addr := sdk.AccAddress(pubKey.Address()) + if _, exists := idx[string(addr.Bytes())]; exists { + continue + } + idx[string(addr.Bytes())] = struct{}{} + accs[i] = Account{ + Address: addr, + PrivKey: privKey, + PubKey: pubKey, + ConsKey: ed25519.GenPrivKeyFromSecret(privkeySeed), + } + i++ } - return accs }