Skip to content

Commit 50ffbaf

Browse files
committed
RandomAccounts
1 parent e5d5c69 commit 50ffbaf

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

testutil/app.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,32 @@ func RandomGenesisAccounts(simState *module.SimulationState) authtypes.GenesisAc
147147
// TODO: replace secp256k1.GenPrivKeyFromSecret() with similar function in go-ethereum
148148
func RandomAccounts(r *rand.Rand, n int) []simtypes.Account {
149149
accs := make([]simtypes.Account, n)
150-
151-
for i := 0; i < n; i++ {
150+
idx := make(map[string]struct{}, n)
151+
var i int
152+
for i < n {
152153
// don't need that much entropy for simulation
153154
privkeySeed := make([]byte, 15)
154-
_, _ = r.Read(privkeySeed)
155-
155+
if _, err := r.Read(privkeySeed); err != nil {
156+
panic(err)
157+
}
156158
prv := secp256k1.GenPrivKeyFromSecret(privkeySeed)
157159
ethPrv := &ethsecp256k1.PrivKey{}
158160
_ = ethPrv.UnmarshalAmino(prv.Bytes()) // UnmarshalAmino simply copies the bytes and assigns them to ethPrv.Key
159-
accs[i].PrivKey = ethPrv
160-
accs[i].PubKey = accs[i].PrivKey.PubKey()
161-
accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address())
162-
163-
accs[i].ConsKey = ed25519.GenPrivKeyFromSecret(privkeySeed)
161+
pubKey := ethPrv.PubKey()
162+
addr := sdk.AccAddress(pubKey.Address())
163+
if _, exists := idx[string(addr.Bytes())]; exists {
164+
continue
165+
}
166+
idx[string(addr.Bytes())] = struct{}{}
167+
accs[i] = simtypes.Account{
168+
Address: addr,
169+
PrivKey: ethPrv,
170+
PubKey: pubKey,
171+
ConsKey: ed25519.GenPrivKeyFromSecret(privkeySeed),
172+
AddressBech32: addr.String(),
173+
}
174+
i++
164175
}
165-
166176
return accs
167177
}
168178

0 commit comments

Comments
 (0)