forked from noble-assets/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cctp_receive_message_with_caller_test.go
359 lines (280 loc) · 10.7 KB
/
cctp_receive_message_with_caller_test.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
package interchaintest_test
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"sort"
"testing"
"time"
cosmossdk_io_math "cosmossdk.io/math"
cctptypes "github.com/circlefin/noble-cctp/x/cctp/types"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/strangelove-ventures/interchaintest/v3"
"github.com/strangelove-ventures/interchaintest/v3/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v3/ibc"
"github.com/strangelove-ventures/interchaintest/v3/testreporter"
"github.com/strangelove-ventures/interchaintest/v3/testutil"
routertypes "github.com/strangelove-ventures/noble-router/x/router/types"
"github.com/strangelove-ventures/noble/cmd"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
// run `make local-image`to rebuild updated binary before running test
func TestCCTP_ReceiveMessageWithCaller(t *testing.T) {
if testing.Short() {
t.Skip()
}
t.Parallel()
ctx := context.Background()
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)
client, network := interchaintest.DockerSetup(t)
var gw genesisWrapper
nv := 1
nf := 0
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
nobleChainSpec(ctx, &gw, "noble-1", nv, nf, true, false, true, false),
{
Name: "gaia",
Version: "v10.0.2",
NumValidators: &nv,
NumFullNodes: &nf,
},
})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
gw.chain = chains[0].(*cosmos.CosmosChain)
noble := gw.chain
gaia := chains[1].(*cosmos.CosmosChain)
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayerImage,
).Build(t, client, network)
pathName := "noble-gaia"
ic := interchaintest.NewInterchain().
AddChain(noble).
AddChain(gaia).
AddRelayer(r, "r").
AddLink(interchaintest.InterchainLink{
Chain1: noble,
Chain2: gaia,
Relayer: r,
Path: pathName,
})
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
})
nobleChainCfg := noble.Config()
gaiaChainCfg := gaia.Config()
cmd.SetPrefixes(nobleChainCfg.Bech32Prefix)
attesters := make([]*ecdsa.PrivateKey, 2)
msgs := make([]sdk.Msg, 2)
// attester - ECDSA public key (Circle will own these keys for mainnet)
for i := range attesters {
p, err := crypto.GenerateKey() // private key
require.NoError(t, err)
attesters[i] = p
pubKey := elliptic.Marshal(p.PublicKey, p.PublicKey.X, p.PublicKey.Y) //public key
attesterPub := hex.EncodeToString(pubKey)
// Adding an attester to protocal
msgs[i] = &cctptypes.MsgEnableAttester{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
Attester: attesterPub,
}
}
broadcaster := cosmos.NewBroadcaster(t, noble)
broadcaster.ConfigureClientContextOptions(func(clientContext sdkclient.Context) sdkclient.Context {
return clientContext.WithBroadcastMode(flags.BroadcastBlock)
})
t.Log("preparing to submit add public keys tx")
burnToken := make([]byte, 32)
copy(burnToken[12:], common.FromHex("0x07865c6E87B9F70255377e024ace6630C1Eaa37F"))
// maps remote token on remote domain to a local token -- used for minting
msgs = append(msgs, &cctptypes.MsgLinkTokenPair{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
RemoteDomain: 0,
RemoteToken: burnToken,
LocalToken: denomMetadataDrachma.Base,
})
bCtx, bCancel := context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err := cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.fiatTfRoles.Owner,
msgs...,
)
require.NoError(t, err, "error submitting add public keys tx")
require.Zero(t, tx.Code, "cctp add pub keys transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("Submitted add public keys tx: %s", tx.TxHash)
var senderForward = []byte("12345678901234567890123456789015")
bCtx, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err = cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.paramAuthority,
&routertypes.MsgAddAllowedSourceDomainSender{
From: gw.paramAuthority.FormattedAddress(),
DomainId: 0,
Address: senderForward,
},
)
require.NoError(t, err, "error submitting add allowed source domain sender tx")
require.Zero(t, tx.Code, "cctp add allowed source domain sender transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
nobleValidator := noble.Validators[0]
cctpModuleAccount := authtypes.NewModuleAddress(cctptypes.ModuleName).String()
_, err = nobleValidator.ExecTx(ctx, gw.fiatTfRoles.MasterMinter.KeyName(),
"fiat-tokenfactory", "configure-minter-controller", gw.fiatTfRoles.MinterController.FormattedAddress(), cctpModuleAccount, "-b", "block",
)
require.NoError(t, err, "failed to execute configure minter controller tx")
_, err = nobleValidator.ExecTx(ctx, gw.fiatTfRoles.MinterController.KeyName(),
"fiat-tokenfactory", "configure-minter", cctpModuleAccount, "1000000"+denomMetadataDrachma.Base, "-b", "block",
)
require.NoError(t, err, "failed to execute configure minter tx")
const receiver = "9B6CA0C13EB603EF207C4657E1E619EF531A4D27" //account
receiverBz, err := hex.DecodeString(receiver)
require.NoError(t, err)
nobleReceiver, err := bech32.ConvertAndEncode(nobleChainCfg.Bech32Prefix, receiverBz)
require.NoError(t, err)
gaiaReceiver, err := bech32.ConvertAndEncode(gaiaChainCfg.Bech32Prefix, receiverBz)
require.NoError(t, err)
burnRecipientPadded := append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, receiverBz...)
// someone burned USDC on Ethereum -> Mint on Noble
depositForBurn := cctptypes.BurnMessage{
BurnToken: burnToken,
MintRecipient: burnRecipientPadded,
Amount: cosmossdk_io_math.NewInt(1000000),
MessageSender: burnRecipientPadded,
}
depositForBurnBz, err := depositForBurn.Bytes()
require.NoError(t, err)
var senderBurn = []byte("12345678901234567890123456789012")
destinationCaller := make([]byte, 32)
copy(destinationCaller[12:], gw.fiatTfRoles.Owner.Address())
wrappedDepositForBurn := cctptypes.Message{
Version: 0,
SourceDomain: 0,
DestinationDomain: 4, // Noble is 4
Nonce: 0, // dif per message
Sender: senderBurn,
Recipient: cctptypes.PaddedModuleAddress,
DestinationCaller: destinationCaller,
MessageBody: depositForBurnBz,
}
wrappedDepositForBurnBz, err := wrappedDepositForBurn.Bytes()
require.NoError(t, err)
// in mainnet this would forward to dydx chain
forward := routertypes.IBCForwardMetadata{
Port: "transfer",
Channel: "channel-0",
DestinationReceiver: gaiaReceiver,
}
forwardBz, err := forward.Bytes(gaiaChainCfg.Bech32Prefix)
require.NoError(t, err)
wrappedForward := cctptypes.Message{
Version: 0,
SourceDomain: 0, // same source domain !
DestinationDomain: 4,
Nonce: 1, // cant be same nonce as above
Sender: senderForward, // different sender !
Recipient: burnRecipientPadded,
DestinationCaller: destinationCaller,
MessageBody: forwardBz,
}
wrappedForwardBz, err := wrappedForward.Bytes()
require.NoError(t, err)
digestBurn := crypto.Keccak256(wrappedDepositForBurnBz) // hashed message is the key to the attestation
digestForward := crypto.Keccak256(wrappedForwardBz)
attestationBurn := make([]byte, 0, len(attesters)*65) //65 byte
attestationForward := make([]byte, 0, len(attesters)*65)
// CCTP requires attestations to have signatures sorted by address
sort.Slice(attesters, func(i, j int) bool {
return bytes.Compare(
crypto.PubkeyToAddress(attesters[i].PublicKey).Bytes(),
crypto.PubkeyToAddress(attesters[j].PublicKey).Bytes(),
) < 0
})
for i := range attesters {
sig, err := crypto.Sign(digestBurn, attesters[i])
require.NoError(t, err)
attestationBurn = append(attestationBurn, sig...)
sig, err = crypto.Sign(digestForward, attesters[i])
require.NoError(t, err)
attestationForward = append(attestationForward, sig...)
}
t.Logf("Attested to messages: %s", tx.TxHash)
bCtx, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err = cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.fiatTfRoles.Owner,
&cctptypes.MsgReceiveMessage{ //note: all messages that go to noble go through MsgReceiveMessage
From: gw.fiatTfRoles.Owner.FormattedAddress(),
Message: wrappedDepositForBurnBz,
Attestation: attestationBurn,
},
)
require.NoError(t, err, "error submitting cctp burn recv tx")
require.Zerof(t, tx.Code, "cctp burn recv transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("CCTP burn message successfully received: %s", tx.TxHash)
balance, err := noble.GetBalance(ctx, nobleReceiver, denomMetadataDrachma.Base)
require.NoError(t, err)
require.Equal(t, int64(1000000), balance)
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)
t.Cleanup(func() {
_ = r.StopRelayer(ctx, eRep)
})
bCtx, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err = cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.fiatTfRoles.Owner,
&cctptypes.MsgReceiveMessage{
From: gw.fiatTfRoles.Owner.FormattedAddress(),
Message: wrappedForwardBz,
Attestation: attestationForward,
},
)
require.NoError(t, err, "error submitting cctp forward recv tx")
require.Zerof(t, tx.Code, "cctp forward recv transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("CCTP IBC forward message successfully received: %s", tx.TxHash)
err = testutil.WaitForBlocks(ctx, 10, noble, gaia)
require.NoError(t, err)
srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", "channel-0", denomMetadataDrachma.Base))
dstIbcDenom := srcDenomTrace.IBCDenom()
gaiaBal, err := gaia.GetBalance(ctx, gaiaReceiver, dstIbcDenom)
require.NoError(t, err)
require.Equal(t, int64(999900), gaiaBal)
// now test deposit for burn noble -> eth
// gaia.Validators[0].CreateKey(ctx, "my_key")
// send ibc tx back to noble
// gaia.Validators[0].SendIBCTransfer(ctx, "channel-0", )
// depositForBurnNoble := &cctptypes.MsgDepositForBurn{
// BurnToken: "is this the sam as coming from e TRY UUSDC",
// Amount: cosmossdk_io_math.NewInt(1000000),
// }
// broad cast... take tx..
// tx.event
// make sure balance went down
}