Skip to content

Commit ad5538a

Browse files
authored
feat: use nonce from api (#25)
* feat: use nonce from api request * Update codeowners * Improve logs * Fix missing bracket * Lint
1 parent f7424d4 commit ad5538a

File tree

10 files changed

+36
-34
lines changed

10 files changed

+36
-34
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# CODEOWNERS: https://help.github.com/articles/about-codeowners/
22

33
# Primary repo maintainers
4-
* @mpetrun5 @tcar121293 @MakMuftic @nmlinaric @freddyli7
5-
tss/* @timoth-y
4+
* @mpetrun5 @tcar121293 @MakMuftic @nmlinaric @mpetrunic

api/handlers/signing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
type SigningBody struct {
2424
ChainId uint64
2525
DepositId *BigInt `json:"depositId"`
26+
Nonce *BigInt `json:"nonce"`
2627
Protocol ProtocolType `json:"protocol"`
2728
LiquidityPool string `json:"liquidityPool"`
2829
Caller string `json:"caller"`
@@ -65,6 +66,7 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
6566
{
6667
m = across.NewAcrossMessage(0, b.ChainId, across.AcrossData{
6768
DepositId: b.DepositId.Int,
69+
Nonce: b.Nonce.Int,
6870
LiquidityPool: common.HexToAddress(b.LiquidityPool),
6971
Caller: common.HexToAddress(b.Caller),
7072
ErrChn: errChn,
@@ -104,6 +106,10 @@ func (h *SigningHandler) validate(b *SigningBody, vars map[string]string) error
104106
return fmt.Errorf("missing field 'caller'")
105107
}
106108

109+
if b.Nonce == nil {
110+
return fmt.Errorf("missing field 'nonce'")
111+
}
112+
107113
if b.ChainId == 0 {
108114
return fmt.Errorf("missing field 'chainId'")
109115
}

api/handlers/signing_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingDepositID() {
4545
Protocol: "across",
4646
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
4747
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
48+
Nonce: &handlers.BigInt{big.NewInt(1001)},
4849
}
4950
body, _ := json.Marshal(input)
5051

@@ -75,6 +76,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingCaller() {
7576
Protocol: "across",
7677
DepositId: &handlers.BigInt{big.NewInt(1000)},
7778
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
79+
Nonce: &handlers.BigInt{big.NewInt(1001)},
7880
}
7981
body, _ := json.Marshal(input)
8082

@@ -105,6 +107,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingLiquidityPool() {
105107
Protocol: "across",
106108
DepositId: &handlers.BigInt{big.NewInt(1000)},
107109
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
110+
Nonce: &handlers.BigInt{big.NewInt(1001)},
108111
}
109112
body, _ := json.Marshal(input)
110113

@@ -136,6 +139,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidChainID() {
136139
Protocol: "across",
137140
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
138141
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
142+
Nonce: &handlers.BigInt{big.NewInt(1001)},
139143
}
140144
body, _ := json.Marshal(input)
141145

@@ -167,6 +171,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ChainNotSupported() {
167171
Protocol: "across",
168172
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
169173
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
174+
Nonce: &handlers.BigInt{big.NewInt(1001)},
170175
}
171176
body, _ := json.Marshal(input)
172177

@@ -198,6 +203,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidProtocol() {
198203
Protocol: "invalid",
199204
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
200205
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
206+
Nonce: &handlers.BigInt{big.NewInt(1001)},
201207
}
202208
body, _ := json.Marshal(input)
203209

@@ -229,6 +235,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ErrorHandlingMessage() {
229235
Protocol: "across",
230236
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
231237
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
238+
Nonce: &handlers.BigInt{big.NewInt(1001)},
232239
}
233240
body, _ := json.Marshal(input)
234241

@@ -260,6 +267,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_Success() {
260267
Protocol: "across",
261268
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
262269
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
270+
Nonce: &handlers.BigInt{big.NewInt(1001)},
263271
}
264272
body, _ := json.Marshal(input)
265273

chains/evm/message/across.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type EventFilterer interface {
4646

4747
type AcrossData struct {
4848
DepositId *big.Int
49+
Nonce *big.Int
4950
LiquidityPool common.Address
5051
Caller common.Address
5152
Coordinator peer.ID
@@ -134,6 +135,7 @@ func (h *AcrossMessageHandler) Listen(ctx context.Context) {
134135

135136
msg := NewAcrossMessage(acrossMsg.Source, acrossMsg.Destination, AcrossData{
136137
DepositId: acrossMsg.DepositId,
138+
Nonce: acrossMsg.Nonce,
137139
Coordinator: wMsg.From,
138140
LiquidityPool: common.HexToAddress(acrossMsg.LiqudityPool),
139141
Caller: common.HexToAddress(acrossMsg.Caller),
@@ -159,6 +161,8 @@ func (h *AcrossMessageHandler) Listen(ctx context.Context) {
159161
func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Proposal, error) {
160162
data := m.Data.(AcrossData)
161163

164+
log.Info().Str("depositId", data.DepositId.String()).Msgf("Handling across message %+v", data)
165+
162166
sourceChainID := h.chainID
163167
if data.Coordinator == peer.ID("") {
164168
data.Coordinator = h.host.ID()
@@ -214,6 +218,7 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
214218
func (h *AcrossMessageHandler) notify(m *message.Message, data AcrossData) error {
215219
msgBytes, err := tssMessage.MarshalAcrossMessage(
216220
data.DepositId,
221+
data.Nonce,
217222
data.LiquidityPool.Hex(),
218223
data.Caller.Hex(),
219224
m.Source,
@@ -287,7 +292,9 @@ func (h *AcrossMessageHandler) waitForConfirmations(
287292
}
288293

289294
// nolint:gosec
290-
time.Sleep(time.Duration(uint64(h.blocktime) * (requiredConfirmations - confirmations.Uint64())))
295+
duration := time.Duration(uint64(h.blocktime) * (requiredConfirmations - confirmations.Uint64()))
296+
log.Debug().Msgf("Waiting for tx %s for %s", txHash, duration)
297+
time.Sleep(duration)
291298
}
292299
}
293300
}
@@ -378,7 +385,7 @@ func (h *AcrossMessageHandler) unlockHash(
378385
"amount": deposit.OutputAmount,
379386
"target": common.BytesToAddress(deposit.Recipient[12:]).Hex(),
380387
"targetCallData": hexutil.Encode(calldata),
381-
"nonce": h.nonce(deposit, sourceChainId),
388+
"nonce": data.Nonce,
382389
"deadline": new(big.Int).SetUint64(uint64(deposit.FillDeadline)),
383390
}
384391

@@ -423,29 +430,3 @@ func (h *AcrossMessageHandler) unlockHash(
423430
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(messageHash)))
424431
return crypto.Keccak256(rawData), nil
425432
}
426-
427-
// nonce creates a unique ID from the across deposit id, origin chain id and protocol id.
428-
// Resulting id has this format: [originChainID (8 bits)][protocolID (8 bits)][nonce (240 bits)].
429-
func (h *AcrossMessageHandler) nonce(deposit *events.AcrossDeposit, sourceChainId uint64) *big.Int {
430-
// Create a new big.Int
431-
nonce := new(big.Int)
432-
433-
// Set originChainID (8 bits)
434-
originChainID := new(big.Int).SetUint64(sourceChainId)
435-
originChainID.And(originChainID, new(big.Int).SetUint64(0xFF)) // Ensure only 8 bits are used
436-
nonce.Lsh(originChainID, 248) // Shift left by 248 bits
437-
438-
// Add protocolID in the middle (8 bits, shifted left by 240 bits)
439-
protocolInt := big.NewInt(PROTOCOL_ID)
440-
protocolInt.And(protocolInt, new(big.Int).SetUint64(0xFF)) // Ensure only 8 bits are used
441-
protocolInt.Lsh(protocolInt, 240)
442-
nonce.Or(nonce, protocolInt)
443-
444-
// Add depositId at the end (240 bits)
445-
depositIdMask := new(big.Int).Lsh(big.NewInt(1), 240)
446-
depositIdMask.Sub(depositIdMask, big.NewInt(1)) // Create a mask of 240 1's
447-
depositId := new(big.Int).And(deposit.DepositId, depositIdMask) // Ensure only 240 bits are used
448-
nonce.Or(nonce, depositId)
449-
450-
return nonce
451-
}

chains/evm/message/across_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_FailedLogQuery() {
108108
ad := message.AcrossData{
109109
ErrChn: errChn,
110110
DepositId: big.NewInt(100),
111+
Nonce: big.NewInt(101),
111112
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
112113
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
113114
}
@@ -142,6 +143,7 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_LogMissing() {
142143
ad := message.AcrossData{
143144
ErrChn: errChn,
144145
DepositId: big.NewInt(100),
146+
Nonce: big.NewInt(101),
145147
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
146148
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
147149
}
@@ -181,6 +183,7 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_IgnoreRemovedLogs() {
181183
ad := message.AcrossData{
182184
ErrChn: errChn,
183185
DepositId: big.NewInt(100),
186+
Nonce: big.NewInt(101),
184187
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
185188
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
186189
}
@@ -237,6 +240,7 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_ValidLog() {
237240
ad := message.AcrossData{
238241
ErrChn: errChn,
239242
DepositId: big.NewInt(100),
243+
Nonce: big.NewInt(101),
240244
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
241245
Caller: common.HexToAddress("0x5ECF7351930e4A251193aA022Ef06249C6cBfa27"),
242246
}

example/cfg/config_evm-evm_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"confirmationsByValue": {
33-
"1000": "4000",
33+
"1000": "2",
3434
"10000": "10"
3535
}
3636
},

example/cfg/config_evm-evm_2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"confirmationsByValue": {
33-
"1000": "4000",
33+
"1000": "2",
3434
"10000": "5"
3535
}
3636
},

example/cfg/config_evm-evm_3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"confirmationsByValue": {
33-
"1000": "4000",
33+
"1000": "2",
3434
"10000": "10"
3535
}
3636
},

tss/message/message.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,17 @@ func UnmarshalSignatureMessage(msgBytes []byte) (*SignatureMessage, error) {
9595

9696
type AcrossMessage struct {
9797
DepositId *big.Int `json:"depositId"`
98+
Nonce *big.Int `json:"nonce"`
9899
LiqudityPool string `json:"liqudityPool"`
99100
Caller string `json:"caller"`
100101
Source uint64 `json:"source"`
101102
Destination uint64 `json:"destination"`
102103
}
103104

104-
func MarshalAcrossMessage(depositId *big.Int, pool, caller string, source, destination uint64) ([]byte, error) {
105+
func MarshalAcrossMessage(depositId, nonce *big.Int, pool, caller string, source, destination uint64) ([]byte, error) {
105106
signatureMessage := &AcrossMessage{
106107
LiqudityPool: pool,
108+
Nonce: nonce,
107109
Caller: caller,
108110
DepositId: depositId,
109111
Source: source,

tss/message/message_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ func TestRunAcrossMessageTestSuite(t *testing.T) {
8787
func (s *AcrossMessageTestSuite) Test_UnmarshaledMessageShouldBeEqual() {
8888
originalMsg := &message.AcrossMessage{
8989
DepositId: big.NewInt(100),
90+
Nonce: big.NewInt(101),
9091
Caller: "caller",
9192
LiqudityPool: "pool",
9293
Source: 1,
9394
Destination: 2,
9495
}
9596
msgBytes, err := message.MarshalAcrossMessage(
9697
originalMsg.DepositId,
98+
originalMsg.Nonce,
9799
originalMsg.LiqudityPool,
98100
originalMsg.Caller,
99101
originalMsg.Source,

0 commit comments

Comments
 (0)