forked from noble-assets/noble
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cctp_replace_deposit_for_burn_test.go
284 lines (222 loc) · 9.3 KB
/
cctp_replace_deposit_for_burn_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
package interchaintest_test
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"fmt"
"sort"
"testing"
"time"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/crypto"
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/ethereum/go-ethereum/common"
"github.com/noble-assets/noble/v7/cmd"
"github.com/strangelove-ventures/interchaintest/v4"
"github.com/strangelove-ventures/interchaintest/v4/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v4/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
// run `make local-image`to rebuild updated binary before running test
func TestCCTP_ReplaceDepositForBurn(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
ic := interchaintest.NewInterchain().
AddChain(noble)
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()
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: denomMetadataUsdc.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)
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"+denomMetadataUsdc.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)
burnRecipientPadded := append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, receiverBz...)
messageSender := make([]byte, 32)
copy(messageSender[12:], sdk.MustAccAddressFromBech32(gw.extraWallets.User.FormattedAddress()))
// someone burned USDC on Ethereum -> Mint on Noble
depositForBurn := cctptypes.BurnMessage{
BurnToken: burnToken,
MintRecipient: burnRecipientPadded,
Amount: cosmossdk_io_math.NewInt(1000000),
MessageSender: messageSender,
}
depositForBurnBz, err := depositForBurn.Bytes()
require.NoError(t, err)
emptyDestinationCaller := make([]byte, 32)
wrappedDepositForBurn := cctptypes.Message{
Version: 0,
SourceDomain: 4, // noble is 4
DestinationDomain: 0,
Nonce: 0, // dif per message
Sender: cctptypes.PaddedModuleAddress,
Recipient: cctptypes.PaddedModuleAddress,
DestinationCaller: emptyDestinationCaller,
MessageBody: depositForBurnBz,
}
wrappedDepositForBurnBz, err := wrappedDepositForBurn.Bytes()
require.NoError(t, err)
digestBurn := crypto.Keccak256(wrappedDepositForBurnBz) // hashed message is the key to the attestation
attestationBurn := make([]byte, 0, len(attesters)*65) //65 byte
// 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...)
}
t.Logf("Attested to messages: %s", tx.TxHash)
newDestCaller := []byte("12345678901234567890123456789012")
newMintRecipient := []byte("12345678901234567890123456789012")
bCtx, bCancel = context.WithTimeout(ctx, 20*time.Second)
defer bCancel()
tx, err = cosmos.BroadcastTx(
bCtx,
broadcaster,
gw.extraWallets.User,
&cctptypes.MsgReplaceDepositForBurn{
From: gw.extraWallets.User.FormattedAddress(),
OriginalMessage: wrappedDepositForBurnBz,
OriginalAttestation: attestationBurn,
NewDestinationCaller: newDestCaller,
NewMintRecipient: newMintRecipient,
},
)
require.NoError(t, err, "error submitting cctp replace deposit for burn tx")
require.Zerof(t, tx.Code, "cctp replace deposit for burn transaction failed: %s - %s - %s", tx.Codespace, tx.RawLog, tx.Data)
t.Logf("CCTP replace message successfully received: %s", tx.TxHash)
for _, rawEvent := range tx.Events {
switch rawEvent.Type {
case "circle.cctp.v1.DepositForBurn":
parsedEvent, err := sdk.ParseTypedEvent(rawEvent)
require.NoError(t, err)
actualDepositForBurn, ok := parsedEvent.(*cctptypes.DepositForBurn)
require.True(t, ok)
expectedBurnToken := hex.EncodeToString(crypto.Keccak256(depositForBurn.BurnToken))
require.Equal(t, wrappedDepositForBurn.Nonce, actualDepositForBurn.Nonce)
require.Equal(t, expectedBurnToken, actualDepositForBurn.BurnToken)
require.Equal(t, depositForBurn.Amount, actualDepositForBurn.Amount)
require.Equal(t, gw.extraWallets.User.FormattedAddress(), actualDepositForBurn.Depositor)
require.Equal(t, newMintRecipient, actualDepositForBurn.MintRecipient) // new
require.Equal(t, wrappedDepositForBurn.DestinationDomain, actualDepositForBurn.DestinationDomain)
require.Equal(t, wrappedDepositForBurn.Recipient, actualDepositForBurn.DestinationTokenMessenger)
require.Equal(t, newDestCaller, actualDepositForBurn.DestinationCaller) // new
case "circle.cctp.v1.MessageSent":
parsedEvent, err := sdk.ParseTypedEvent(rawEvent)
require.NoError(t, err)
event, ok := parsedEvent.(*cctptypes.MessageSent)
require.True(t, ok)
message, err := new(cctptypes.Message).Parse(event.Message)
require.NoError(t, err)
expectedBurnToken := hex.EncodeToString(crypto.Keccak256(depositForBurn.BurnToken))
fmt.Println(expectedBurnToken)
moduleAddress := make([]byte, 32)
copy(moduleAddress[12:], sdk.MustAccAddressFromBech32(gw.extraWallets.User.FormattedAddress()))
require.Equal(t, wrappedDepositForBurn.Version, message.Version)
require.Equal(t, wrappedDepositForBurn.SourceDomain, message.SourceDomain)
require.Equal(t, wrappedDepositForBurn.DestinationDomain, message.DestinationDomain)
require.Equal(t, wrappedDepositForBurn.Nonce, message.Nonce)
require.Equal(t, cctptypes.PaddedModuleAddress, message.Sender)
require.Equal(t, cctptypes.PaddedModuleAddress, message.Recipient)
require.Equal(t, newDestCaller, message.DestinationCaller)
body, err := new(cctptypes.BurnMessage).Parse(message.MessageBody)
require.NoError(t, err)
require.Equal(t, depositForBurn.Version, body.Version)
require.Equal(t, newMintRecipient, body.MintRecipient)
require.Equal(t, depositForBurn.Amount, body.Amount)
require.True(t, bytes.Equal(depositForBurn.BurnToken, body.BurnToken))
require.Equal(t, depositForBurn.MessageSender, body.MessageSender)
}
}
}