-
Notifications
You must be signed in to change notification settings - Fork 109
/
rand.go
117 lines (97 loc) · 4.29 KB
/
rand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package testutils
import (
"time"
"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/axelarnetwork/axelar-core/testutils/rand"
"github.com/axelarnetwork/axelar-core/utils"
utilstestutils "github.com/axelarnetwork/axelar-core/utils/testutils"
"github.com/axelarnetwork/axelar-core/x/multisig/exported"
multisigtestutils "github.com/axelarnetwork/axelar-core/x/multisig/exported/testutils"
"github.com/axelarnetwork/axelar-core/x/multisig/types"
snapshottypes "github.com/axelarnetwork/axelar-core/x/snapshot/exported"
snapshottestutils "github.com/axelarnetwork/axelar-core/x/snapshot/exported/testutils"
"github.com/axelarnetwork/utils/funcs"
"github.com/axelarnetwork/utils/slices"
)
// PublicKey returns a random public key
func PublicKey() exported.PublicKey {
privKey, _ := btcec.NewPrivateKey(btcec.S256())
return privKey.PubKey().SerializeCompressed()
}
// KeygenSession returns a random multisig keygen session
func KeygenSession() types.KeygenSession {
key := Key()
keygenThreshold := utils.NewThreshold(rand.I64Between(key.SigningThreshold.Numerator, key.SigningThreshold.Denominator+1), key.SigningThreshold.Denominator)
return types.KeygenSession{
Key: key,
State: exported.Pending,
ExpiresAt: rand.I64Between(1, 100),
CompletedAt: rand.I64Between(1, 100),
GracePeriod: rand.I64Between(1, 10),
KeygenThreshold: keygenThreshold,
}
}
// Key returns a random multisig key
func Key() types.Key {
threshold := utilstestutils.RandThreshold()
snapshot := snapshottestutils.Snapshot(uint64(rand.I64Between(10, 20)), threshold)
participants := snapshot.GetParticipantAddresses()
pubKeys := make(map[string]exported.PublicKey, len(participants))
for _, p := range participants {
pubKeys[p.String()] = PublicKey()
}
return types.Key{
ID: multisigtestutils.KeyID(),
State: exported.Inactive,
Snapshot: snapshot,
PubKeys: pubKeys,
SigningThreshold: threshold,
}
}
// KeyWithMissingParticipants returns a random multisig key with some missing participants
func KeyWithMissingParticipants() types.Key {
participantCount := uint64(rand.I64Between(10, 20))
missingCount := uint64(rand.I64Between(1, int64(participantCount)))
participants := slices.Expand(func(_ int) snapshottypes.Participant {
return snapshottypes.Participant{Address: rand.ValAddr(), Weight: sdk.NewUint(uint64(rand.I64Between(1, 100)))}
}, int(participantCount))
missingParticipants := slices.Expand(func(_ int) snapshottypes.Participant {
return snapshottypes.Participant{Address: rand.ValAddr(), Weight: sdk.NewUint(uint64(rand.I64Between(1, 100)))}
}, int(missingCount))
participants = append(participants, missingParticipants...)
weightAdder := func(total sdk.Uint, p snapshottypes.Participant) sdk.Uint { total = total.Add(p.Weight); return total }
participantWeight := slices.Reduce(participants, sdk.ZeroUint(), weightAdder)
missingParticipantWeight := slices.Reduce(missingParticipants, sdk.ZeroUint(), weightAdder)
bondedWeight := rand.UintBetween(participantWeight, participantWeight.MulUint64(2))
threshold := utils.NewThreshold(rand.I64Between(1, participantWeight.Sub(missingParticipantWeight).BigInt().Int64()), bondedWeight.BigInt().Int64())
snapshot := snapshottypes.NewSnapshot(time.Now(), rand.I64Between(1, 100), participants, bondedWeight)
pubKeys := make(map[string]exported.PublicKey, len(participants))
for _, p := range snapshot.GetParticipantAddresses() {
pubKeys[p.String()] = PublicKey()
}
for _, p := range missingParticipants {
delete(pubKeys, p.String())
}
return types.Key{
ID: multisigtestutils.KeyID(),
Snapshot: snapshot,
PubKeys: pubKeys,
SigningThreshold: threshold,
}
}
// MultiSig returns a random multi sig
func MultiSig() types.MultiSig {
payloadHash := rand.Bytes(exported.HashLength)
participants := slices.Expand(func(int) sdk.ValAddress { return rand.ValAddr() }, int(rand.I64Between(5, 10)))
sigs := make(map[string]types.Signature, len(participants))
for _, p := range participants {
sk := funcs.Must(btcec.NewPrivateKey(btcec.S256()))
sigs[p.String()] = funcs.Must(sk.Sign(payloadHash)).Serialize()
}
return types.MultiSig{
KeyID: multisigtestutils.KeyID(),
Sigs: sigs,
PayloadHash: payloadHash,
}
}