-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathobserver.go
353 lines (312 loc) · 9.93 KB
/
observer.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package sample
import (
"fmt"
"math/rand"
"testing"
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/ethereum/go-ethereum/crypto"
"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/cosmos"
zetacrypto "github.com/zeta-chain/node/pkg/crypto"
"github.com/zeta-chain/node/pkg/ptr"
"github.com/zeta-chain/node/x/observer/types"
)
func Ballot(t *testing.T, index string) *types.Ballot {
r := newRandFromStringSeed(t, index)
return &types.Ballot{
Index: index,
BallotIdentifier: StringRandom(r, 32),
VoterList: []string{AccAddress(), AccAddress()},
Votes: []types.VoteType{types.VoteType_FailureObservation, types.VoteType_SuccessObservation},
ObservationType: types.ObservationType_EmptyObserverType,
BallotThreshold: sdkmath.LegacyNewDec(1),
BallotStatus: types.BallotStatus_BallotInProgress,
BallotCreationHeight: r.Int63(),
}
}
func ObserverSet(n int) types.ObserverSet {
observerList := make([]string, n)
for i := 0; i < n; i++ {
observerList[i] = AccAddress()
}
return types.ObserverSet{
ObserverList: observerList,
}
}
func NodeAccount() *types.NodeAccount {
return &types.NodeAccount{
Operator: AccAddress(),
GranteeAddress: AccAddress(),
GranteePubkey: PubKeySet(),
NodeStatus: types.NodeStatus_Active,
}
}
func CrosschainFlags() *types.CrosschainFlags {
return &types.CrosschainFlags{
IsInboundEnabled: true,
IsOutboundEnabled: true,
}
}
func Keygen(t *testing.T) *types.Keygen {
pubKey := ed25519.GenPrivKey().PubKey().String()
r := newRandFromStringSeed(t, pubKey)
return &types.Keygen{
Status: types.KeygenStatus_KeyGenSuccess,
GranteePubkeys: []string{pubKey},
BlockNumber: r.Int63(),
}
}
func KeygenFromRand(r *rand.Rand) types.Keygen {
pubkey := PubKey(r)
return types.Keygen{
Status: types.KeygenStatus_KeyGenSuccess,
GranteePubkeys: []string{pubkey.String()},
BlockNumber: r.Int63(),
}
}
func LastObserverCount(lastChangeHeight int64) *types.LastObserverCount {
r := newRandFromSeed(lastChangeHeight)
return &types.LastObserverCount{
Count: r.Uint64(),
LastChangeHeight: lastChangeHeight,
}
}
func ChainParams(chainID int64) *types.ChainParams {
r := newRandFromSeed(chainID)
fiftyPercent, err := sdkmath.LegacyNewDecFromStr("0.5")
if err != nil {
return nil
}
return &types.ChainParams{
ChainId: chainID,
ConfirmationCount: r.Uint64(),
GasPriceTicker: Uint64InRange(1, 300),
InboundTicker: Uint64InRange(1, 300),
OutboundTicker: Uint64InRange(1, 300),
WatchUtxoTicker: Uint64InRange(1, 300),
ZetaTokenContractAddress: EthAddress().String(),
ConnectorContractAddress: EthAddress().String(),
Erc20CustodyContractAddress: EthAddress().String(),
OutboundScheduleInterval: Int64InRange(1, 100),
OutboundScheduleLookahead: Int64InRange(1, 500),
BallotThreshold: fiftyPercent,
MinObserverDelegation: sdkmath.LegacyNewDec(r.Int63()),
IsSupported: false,
GatewayAddress: EthAddress().String(),
ConfirmationParams: ConfirmationParams(r),
}
}
func ChainParamsFromRand(r *rand.Rand, chainID int64) *types.ChainParams {
fiftyPercent := sdkmath.LegacyMustNewDecFromStr("0.5")
return &types.ChainParams{
ChainId: chainID,
ConfirmationCount: r.Uint64(),
GasPriceTicker: Uint64InRangeFromRand(r, 1, 300),
InboundTicker: Uint64InRangeFromRand(r, 1, 300),
OutboundTicker: Uint64InRangeFromRand(r, 1, 300),
WatchUtxoTicker: Uint64InRangeFromRand(r, 1, 300),
ZetaTokenContractAddress: EthAddressFromRand(r).String(),
ConnectorContractAddress: EthAddressFromRand(r).String(),
Erc20CustodyContractAddress: EthAddressFromRand(r).String(),
OutboundScheduleInterval: Int64InRangeFromRand(r, 1, 100),
OutboundScheduleLookahead: Int64InRangeFromRand(r, 1, 500),
BallotThreshold: fiftyPercent,
MinObserverDelegation: sdkmath.LegacyNewDec(r.Int63()),
IsSupported: true,
}
}
func ChainParamsSupported(chainID int64) *types.ChainParams {
cp := ChainParams(chainID)
cp.IsSupported = true
return cp
}
func ChainParamsList() (cpl types.ChainParamsList) {
chainList := chains.ChainListByNetworkType(chains.NetworkType_privnet, []chains.Chain{})
for _, chain := range chainList {
cpl.ChainParams = append(cpl.ChainParams, ChainParams(chain.ChainId))
}
return
}
// TSSFromRand returns a random TSS,it uses the randomness provided as a parameter
func TSSFromRand(r *rand.Rand) (types.TSS, error) {
pubKey := PubKey(r)
spk, err := cosmos.Bech32ifyPubKey(cosmos.Bech32PubKeyTypeAccPub, pubKey)
if err != nil {
return types.TSS{}, err
}
pk, err := zetacrypto.NewPubKey(spk)
if err != nil {
return types.TSS{}, err
}
pubkeyString := pk.String()
return types.TSS{
TssPubkey: pubkeyString,
TssParticipantList: []string{},
OperatorAddressList: []string{},
FinalizedZetaHeight: r.Int63(),
KeyGenZetaHeight: r.Int63(),
}, nil
}
// TODO: rename to TSS
// https://github.com/zeta-chain/node/issues/3098
func Tss() types.TSS {
_, pubKey, _ := testdata.KeyTestPubAddr()
spk, err := cosmos.Bech32ifyPubKey(cosmos.Bech32PubKeyTypeAccPub, pubKey)
if err != nil {
panic(err)
}
pk, err := zetacrypto.NewPubKey(spk)
if err != nil {
panic(err)
}
pubkeyString := pk.String()
return types.TSS{
TssPubkey: pubkeyString,
FinalizedZetaHeight: 1000,
KeyGenZetaHeight: 1000,
}
}
func TssList(n int) (tssList []types.TSS) {
for i := 0; i < n; i++ {
tss := Tss()
tss.FinalizedZetaHeight = tss.FinalizedZetaHeight + int64(i)
tss.KeyGenZetaHeight = tss.KeyGenZetaHeight + int64(i)
tssList = append(tssList, tss)
}
return
}
func TssFundsMigrator(chainID int64) types.TssFundMigratorInfo {
return types.TssFundMigratorInfo{
ChainId: chainID,
MigrationCctxIndex: "sampleIndex",
}
}
func BlameRecord(t *testing.T, index string) types.Blame {
r := newRandFromStringSeed(t, index)
return types.Blame{
Index: fmt.Sprintf("%d-%s", r.Int63(), index),
FailureReason: "sample failure reason",
Nodes: nil,
}
}
func BlameRecordsList(t *testing.T, n int) []types.Blame {
blameList := make([]types.Blame, n)
for i := 0; i < n; i++ {
blameList[i] = BlameRecord(t, fmt.Sprintf("%d", i))
}
return blameList
}
func ChainNonces(chainID int64) types.ChainNonces {
r := newRandFromSeed(chainID)
return types.ChainNonces{
Creator: AccAddress(),
ChainId: chainID,
Nonce: r.Uint64(),
Signers: []string{AccAddress(), AccAddress()},
FinalizedHeight: r.Uint64(),
}
}
func ChainNoncesList(n int) []types.ChainNonces {
chainNoncesList := make([]types.ChainNonces, n)
for i := 0; i < n; i++ {
chainNoncesList[i] = ChainNonces(int64(i))
}
return chainNoncesList
}
func PendingNoncesList(t *testing.T, index string, count int) []types.PendingNonces {
r := newRandFromStringSeed(t, index)
nonceLow := r.Int63()
list := make([]types.PendingNonces, count)
for i := 0; i < count; i++ {
list[i] = types.PendingNonces{
ChainId: int64(i),
NonceLow: nonceLow,
NonceHigh: nonceLow + r.Int63(),
Tss: StringRandom(r, 32),
}
}
return list
}
func NonceToCctxList(t *testing.T, index string, count int) []types.NonceToCctx {
r := newRandFromStringSeed(t, index)
list := make([]types.NonceToCctx, count)
for i := 0; i < count; i++ {
list[i] = types.NonceToCctx{
ChainId: int64(i),
Nonce: r.Int63(),
CctxIndex: StringRandom(r, 32),
}
}
return list
}
func BallotList(n int, observerSet []string) []types.Ballot {
r := newRandFromSeed(int64(n))
ballotList := make([]types.Ballot, n)
for i := 0; i < n; i++ {
identifier := crypto.Keccak256Hash([]byte(fmt.Sprintf("%d-%d-%d", r.Int63(), r.Int63(), r.Int63())))
ballotList[i] = types.Ballot{
Index: identifier.Hex(),
BallotIdentifier: identifier.Hex(),
VoterList: observerSet,
Votes: VotesSuccessOnly(len(observerSet)),
ObservationType: types.ObservationType_InboundTx,
BallotThreshold: sdkmath.LegacyOneDec(),
BallotStatus: types.BallotStatus_BallotFinalized_SuccessObservation,
BallotCreationHeight: 0,
}
}
return ballotList
}
func VotesSuccessOnly(voteCount int) []types.VoteType {
votes := make([]types.VoteType, voteCount)
for i := 0; i < voteCount; i++ {
votes[i] = types.VoteType_SuccessObservation
}
return votes
}
func NonceToCCTX(t *testing.T, seed string) types.NonceToCctx {
r := newRandFromStringSeed(t, seed)
return types.NonceToCctx{
ChainId: r.Int63(),
Nonce: r.Int63(),
CctxIndex: StringRandom(r, 64),
Tss: Tss().TssPubkey,
}
}
func GasPriceIncreaseFlags() types.GasPriceIncreaseFlags {
return types.GasPriceIncreaseFlags{
EpochLength: 1,
RetryInterval: 1,
GasPriceIncreasePercent: 1,
MaxPendingCctxs: 100,
}
}
func GasPriceIncreaseFlagsFromRand(r *rand.Rand) types.GasPriceIncreaseFlags {
minValue := 1
maxValue := 100
return types.GasPriceIncreaseFlags{
EpochLength: int64(r.Intn(maxValue-minValue) + minValue),
RetryInterval: time.Duration(r.Intn(maxValue-minValue) + minValue),
GasPriceIncreasePercent: 1,
MaxPendingCctxs: 100,
}
}
func OperationalFlags() types.OperationalFlags {
return types.OperationalFlags{
RestartHeight: 1,
SignerBlockTimeOffset: ptr.Ptr(time.Second),
}
}
func ConfirmationParams(r *rand.Rand) types.ConfirmationParams {
randInboundCount := Uint64InRangeFromRand(r, 1, 200)
randOutboundCount := Uint64InRangeFromRand(r, 1, 200)
return types.ConfirmationParams{
SafeInboundCount: randInboundCount,
FastInboundCount: Uint64InRange(1, randInboundCount),
SafeOutboundCount: randOutboundCount,
FastOutboundCount: Uint64InRange(1, randOutboundCount),
}
}