-
Notifications
You must be signed in to change notification settings - Fork 643
/
send_obs.go
441 lines (369 loc) · 14.1 KB
/
send_obs.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This tool can be used to send various observations to the accounting smart contract.
// It is meant for testing purposes only.
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"time"
"github.com/certusone/wormhole/node/pkg/accountant"
"github.com/certusone/wormhole/node/pkg/common"
"github.com/certusone/wormhole/node/pkg/wormconn"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"go.uber.org/zap"
)
func main() {
ctx := context.Background()
logger, _ := zap.NewDevelopment()
wormchainURL := string("localhost:9090")
wormchainKeyPath := string("./dev.wormchain.key")
contract := "wormhole14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9srrg465"
guardianKeyPath := string("./dev.guardian.key")
wormchainKey, err := wormconn.LoadWormchainPrivKey(wormchainKeyPath, "test0000")
if err != nil {
logger.Fatal("failed to load devnet wormchain private key", zap.Error(err))
}
wormchainConn, err := wormconn.NewConn(ctx, wormchainURL, wormchainKey, "wormchain")
if err != nil {
logger.Fatal("failed to connect to wormchain", zap.Error(err))
}
logger.Info("Connected to wormchain",
zap.String("wormchainURL", wormchainURL),
zap.String("wormchainKeyPath", wormchainKeyPath),
zap.String("senderAddress", wormchainConn.SenderAddress()),
)
logger.Info("Loading guardian key", zap.String("guardianKeyPath", guardianKeyPath))
gk, err := common.LoadGuardianKey(guardianKeyPath, true)
if err != nil {
logger.Fatal("failed to load guardian key", zap.Error(err))
}
sequence := uint64(time.Now().Unix())
timestamp := time.Now()
if !testSubmit(ctx, logger, gk, wormchainConn, contract, "0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16", timestamp, sequence, true, "Submit should succeed") {
return
}
if !testSubmit(ctx, logger, gk, wormchainConn, contract, "0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16", timestamp, sequence, true, "Already commited should succeed") {
return
}
sequence += 10
if !testSubmit(ctx, logger, gk, wormchainConn, contract, "0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c17", timestamp, sequence, false, "Bad emitter address should fail") {
return
}
sequence += 10
if !testBatch(ctx, logger, gk, wormchainConn, contract, timestamp, sequence) {
return
}
sequence += 10
if !testBatchWithcommitted(ctx, logger, gk, wormchainConn, contract, timestamp, sequence) {
return
}
sequence += 10
if !testBatchWithDigestError(ctx, logger, gk, wormchainConn, contract, timestamp, sequence) {
return
}
logger.Info("Success! All tests passed!")
}
func testSubmit(
ctx context.Context,
logger *zap.Logger,
gk *ecdsa.PrivateKey,
wormchainConn *wormconn.ClientConn,
contract string,
emitterAddressStr string,
timestamp time.Time,
sequence uint64,
expectedResult bool,
tag string,
) bool {
EmitterAddress, _ := vaa.StringToAddress(emitterAddressStr)
TxHash, _ := vaa.StringToHash("82ea2536c5d1671830cb49120f94479e34b54596a8dd369fbc2666667a765f4b")
Payload, _ := hex.DecodeString("010000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000002d8be6bf0baa74e0a907016679cae9190e80dd0a0002000000000000000000000000c10820983f33456ce7beb3a046f5a83fa34f027d0c200000000000000000000000000000000000000000000000000000000000000000")
msg := common.MessagePublication{
TxHash: TxHash,
Timestamp: timestamp,
Nonce: uint32(0),
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs := []*common.MessagePublication{&msg}
txResp, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to broadcast Observation request", zap.String("test", tag), zap.Error(err))
return false
}
responses, err := accountant.GetObservationResponses(txResp)
if err != nil {
logger.Error("failed to get responses", zap.Error(err))
return false
}
if len(responses) != len(msgs) {
logger.Error("number of responses does not match number of messages", zap.Int("numMsgs", len(msgs)), zap.Int("numResp", len(responses)), zap.Error(err))
return false
}
msgId := msgs[0].MessageIDString()
status, exists := responses[msgId]
if !exists {
logger.Info("test failed: did not receive an observation response for message", zap.String("test", tag), zap.String("msgId", msgId))
return false
}
committed := status.Type == "committed"
if committed != expectedResult {
logger.Info("test failed", zap.String("test", tag), zap.Uint64("seqNo", sequence), zap.Bool("committed", committed),
zap.String("response", wormchainConn.BroadcastTxResponseToString(txResp)))
return false
}
logger.Info("test succeeded", zap.String("test", tag))
return true
}
func testBatch(
ctx context.Context,
logger *zap.Logger,
gk *ecdsa.PrivateKey,
wormchainConn *wormconn.ClientConn,
contract string,
timestamp time.Time,
sequence uint64,
) bool {
EmitterAddress, _ := vaa.StringToAddress("0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16")
TxHash, _ := vaa.StringToHash("82ea2536c5d1671830cb49120f94479e34b54596a8dd369fbc2666667a765f4b")
Payload, _ := hex.DecodeString("010000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000002d8be6bf0baa74e0a907016679cae9190e80dd0a0002000000000000000000000000c10820983f33456ce7beb3a046f5a83fa34f027d0c200000000000000000000000000000000000000000000000000000000000000000")
nonce := uint32(123456)
msgs := []*common.MessagePublication{}
msg1 := common.MessagePublication{
TxHash: TxHash,
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg1)
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg2)
txResp, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to broadcast Observation request", zap.Error(err))
return false
}
responses, err := accountant.GetObservationResponses(txResp)
if err != nil {
logger.Error("failed to get responses", zap.Error(err))
return false
}
if len(responses) != len(msgs) {
logger.Error("number of responses does not match number of messages", zap.Int("numMsgs", len(msgs)), zap.Int("numResp", len(responses)), zap.Error(err))
return false
}
for idx, msg := range msgs {
msgId := msg.MessageIDString()
status, exists := responses[msgId]
if !exists {
logger.Error("did not receive an observation response for message", zap.Int("idx", idx), zap.String("msgId", msgId))
return false
}
if status.Type != "committed" {
logger.Error("unexpected response on observation", zap.Int("idx", idx), zap.String("msgId", msgId), zap.String("status", status.Type), zap.String("text", status.Data))
return false
}
}
logger.Info("test of batch passed.")
return true
}
func testBatchWithcommitted(
ctx context.Context,
logger *zap.Logger,
gk *ecdsa.PrivateKey,
wormchainConn *wormconn.ClientConn,
contract string,
timestamp time.Time,
sequence uint64,
) bool {
EmitterAddress, _ := vaa.StringToAddress("0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16")
TxHash, _ := vaa.StringToHash("82ea2536c5d1671830cb49120f94479e34b54596a8dd369fbc2666667a765f4b")
Payload, _ := hex.DecodeString("010000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000002d8be6bf0baa74e0a907016679cae9190e80dd0a0002000000000000000000000000c10820983f33456ce7beb3a046f5a83fa34f027d0c200000000000000000000000000000000000000000000000000000000000000000")
nonce := uint32(123456)
msgs := []*common.MessagePublication{}
logger.Info("submitting a single transfer that should work")
msg1 := common.MessagePublication{
TxHash: TxHash,
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg1)
_, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to submit initial observation that should work", zap.Error(err))
return false
}
logger.Info("submitting a second batch where the second one has already been committed")
msgs = msgs[:0]
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg2)
// Same one we just committed.
msg3 := msg1
msgs = append(msgs, &msg3)
txResp, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to broadcast Observation request", zap.Error(err))
return false
}
responses, err := accountant.GetObservationResponses(txResp)
if err != nil {
logger.Error("failed to get responses", zap.Error(err))
return false
}
if len(responses) != len(msgs) {
logger.Error("number of responses does not match number of messages", zap.Int("numMsgs", len(msgs)), zap.Int("numResp", len(responses)), zap.Error(err))
return false
}
for idx, msg := range msgs {
msgId := msg.MessageIDString()
status, exists := responses[msgId]
if !exists {
logger.Error("did not receive an observation response for message", zap.Int("idx", idx), zap.String("msgId", msgId))
return false
}
if status.Type != "committed" {
logger.Error("unexpected response on observation", zap.Int("idx", idx), zap.String("msgId", msgId), zap.String("status", status.Type), zap.String("text", status.Data))
return false
}
}
logger.Info("test of batch with already committed passed.")
return true
}
func testBatchWithDigestError(
ctx context.Context,
logger *zap.Logger,
gk *ecdsa.PrivateKey,
wormchainConn *wormconn.ClientConn,
contract string,
timestamp time.Time,
sequence uint64,
) bool {
EmitterAddress, _ := vaa.StringToAddress("0000000000000000000000000290fb167208af455bb137780163b7b7a9a10c16")
TxHash, _ := vaa.StringToHash("82ea2536c5d1671830cb49120f94479e34b54596a8dd369fbc2666667a765f4b")
Payload, _ := hex.DecodeString("010000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000002d8be6bf0baa74e0a907016679cae9190e80dd0a0002000000000000000000000000c10820983f33456ce7beb3a046f5a83fa34f027d0c200000000000000000000000000000000000000000000000000000000000000000")
nonce := uint32(123456)
msgs := []*common.MessagePublication{}
logger.Info("submitting a single transfer that should work")
msg1 := common.MessagePublication{
TxHash: TxHash,
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg1)
_, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to submit initial observation that should work", zap.Error(err))
return false
}
logger.Info("submitting a second batch where the second one has a digest error")
msgs = msgs[:0]
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
EmitterChain: vaa.ChainIDEthereum,
EmitterAddress: EmitterAddress,
ConsistencyLevel: uint8(15),
Payload: Payload,
}
msgs = append(msgs, &msg2)
// Same key as the message we committed but change the digest.
msg3 := msg1
msg3.Nonce = msg3.Nonce + 1
msgs = append(msgs, &msg3)
txResp, err := submit(ctx, logger, gk, wormchainConn, contract, msgs)
if err != nil {
logger.Error("failed to submit second observation that should work", zap.Error(err))
return false
}
responses, err := accountant.GetObservationResponses(txResp)
if err != nil {
logger.Error("failed to get responses", zap.Error(err))
return false
}
if len(responses) != len(msgs) {
logger.Error("number of responses does not match number of messages", zap.Int("numMsgs", len(msgs)), zap.Int("numResp", len(responses)), zap.Error(err))
return false
}
msgId := msgs[0].MessageIDString()
status, exists := responses[msgId]
if !exists {
logger.Error("did not receive an observation response for message 0", zap.String("msgId", msgId))
return false
}
if status.Type != "committed" {
logger.Error("unexpected response on observation for message 0", zap.String("msgId", msgId), zap.String("status", status.Type), zap.String("text", status.Data))
return false
}
msgId = msgs[1].MessageIDString()
status, exists = responses[msgId]
if !exists {
logger.Error("did not receive an observation response for message 1", zap.String("msgId", msgId))
return false
}
if status.Type != "error" {
logger.Error("unexpected response on observation for message 1", zap.String("status", status.Type), zap.String("text", status.Data))
return false
}
if status.Data != "digest mismatch for processed message" {
logger.Error("unexpected error text on observation for message 1", zap.String("text", status.Data))
return false
}
logger.Info("test of batch with digest error passed.")
return true
}
func submit(
ctx context.Context,
logger *zap.Logger,
gk *ecdsa.PrivateKey,
wormchainConn *wormconn.ClientConn,
contract string,
msgs []*common.MessagePublication,
) (*sdktx.BroadcastTxResponse, error) {
gsIndex := uint32(0)
guardianIndex := uint32(0)
return accountant.SubmitObservationsToContract(ctx, logger, gk, gsIndex, guardianIndex, wormchainConn, contract, accountant.SubmitObservationPrefix, msgs)
}