Skip to content

Commit 0ff7314

Browse files
committed
Add lifi tests
1 parent 43d765d commit 0ff7314

File tree

7 files changed

+640
-34
lines changed

7 files changed

+640
-34
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ genmocks:
3737
mockgen -destination=./comm/p2p/mock/stream/stream.go github.com/libp2p/go-libp2p/core/network Stream,Conn
3838
mockgen -source=./chains/evm/message/across.go -destination=./chains/evm/message/mock/across.go
3939
mockgen -source=./chains/evm/message/rhinestone.go -destination=./chains/evm/message/mock/rhinestone.go
40+
mockgen -source=./chains/evm/message/lifi.go -destination=./chains/evm/message/mock/lifi.go
4041
mockgen -source=./chains/evm/message/mayan.go -destination=./chains/evm/message/mock/mayan.go
4142
mockgen -source=./chains/evm/message/confirmations.go -destination=./chains/evm/message/mock/confirmations.go
4243
mockgen -source=./api/handlers/signing.go -destination=./api/handlers/mock/signing.go

chains/evm/message/lifi.go

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package message
22

33
import (
44
"context"
5+
"encoding/hex"
56
"encoding/json"
67
"fmt"
78
"math/big"
@@ -43,10 +44,9 @@ type Compact interface {
4344
type LifiCompactMessageHandler struct {
4445
chainID uint64
4546

46-
mpcAddress common.Address
47-
lifiAddresses map[uint64]common.Address
48-
liquidityPools map[uint64]common.Address
49-
tokenStore config.TokenStore
47+
mpcAddress common.Address
48+
lifiAddresses map[uint64]common.Address
49+
tokenStore config.TokenStore
5050

5151
orderFetcher OrderFetcher
5252
compact Compact
@@ -58,25 +58,31 @@ type LifiCompactMessageHandler struct {
5858
sigChn chan any
5959
}
6060

61-
func NewLifiMessageHandler(
61+
func NewLifiCompactMessageHandler(
6262
chainID uint64,
63-
liquidityPools map[uint64]common.Address,
63+
mpcAddress common.Address,
64+
lifiAddresses map[uint64]common.Address,
65+
tokenStore config.TokenStore,
66+
orderFetcher OrderFetcher,
67+
compact Compact,
6468
coordinator Coordinator,
6569
host host.Host,
6670
comm comm.Communication,
6771
fetcher signing.SaveDataFetcher,
68-
tokenStore config.TokenStore,
6972
sigChn chan any,
7073
) *LifiCompactMessageHandler {
7174
return &LifiCompactMessageHandler{
72-
chainID: chainID,
73-
liquidityPools: liquidityPools,
74-
coordinator: coordinator,
75-
host: host,
76-
comm: comm,
77-
fetcher: fetcher,
78-
sigChn: sigChn,
79-
tokenStore: tokenStore,
75+
chainID: chainID,
76+
mpcAddress: mpcAddress,
77+
lifiAddresses: lifiAddresses,
78+
orderFetcher: orderFetcher,
79+
compact: compact,
80+
coordinator: coordinator,
81+
host: host,
82+
comm: comm,
83+
fetcher: fetcher,
84+
sigChn: sigChn,
85+
tokenStore: tokenStore,
8086
}
8187
}
8288

@@ -143,32 +149,58 @@ func (h *LifiCompactMessageHandler) HandleMessage(m *message.Message) (*proposal
143149
}
144150

145151
func (h *LifiCompactMessageHandler) calldata(order *lifi.LifiOrder) ([]byte, error) {
152+
type output struct {
153+
Oracle common.Hash
154+
Settler common.Hash
155+
Recipient common.Hash
156+
ChainId *big.Int
157+
Token common.Hash
158+
Amount *big.Int
159+
Call []byte
160+
Context []byte
161+
}
162+
outputs := make([]output, len(order.Order.Outputs))
163+
for i, o := range order.Order.Outputs {
164+
chainID, _ := new(big.Int).SetString(o.ChainID, 10)
165+
call, _ := hex.DecodeString(o.Call)
166+
context, _ := hex.DecodeString(o.Context)
167+
outputs[i] = output{
168+
Oracle: common.HexToHash(o.Oracle),
169+
Settler: common.HexToHash(o.Settler),
170+
ChainId: chainID,
171+
Amount: o.Amount.Int,
172+
Recipient: common.HexToHash(o.Recipient),
173+
Call: call,
174+
Context: context,
175+
}
176+
}
177+
146178
return consts.LifiABI.Pack(
147179
"fillOrderOutputs",
148-
order.Order.FillDeadline,
149-
order.Meta.OnChainOrderID,
150-
order.Order.Outputs,
180+
uint32(order.Order.FillDeadline),
181+
common.HexToHash(order.Meta.OnChainOrderID),
182+
outputs,
151183
common.HexToHash(h.mpcAddress.Hex()))
152184
}
153185

154186
// verifyOrder verifies order based on these instructions https://docs.catalyst.exchange/solver/orderflow/#order-validation
155187
func (h *LifiCompactMessageHandler) verifyOrder(order *lifi.LifiOrder, data *LifiData) error {
156-
err := h.verifySignatures(order)
188+
err := h.verifyDeadline(order)
157189
if err != nil {
158190
return err
159191
}
160192

161-
err = h.verifyDeadline(order)
193+
err = h.verifyInputs(order)
162194
if err != nil {
163195
return err
164196
}
165197

166-
err = h.verifyInputs(order)
198+
err = h.verifyOutput(order, data.Caller, data.BorrowAmount)
167199
if err != nil {
168200
return err
169201
}
170202

171-
err = h.verifyOutput(order, data.Caller, data.BorrowAmount)
203+
err = h.verifySignatures(order)
172204
if err != nil {
173205
return err
174206
}
@@ -221,7 +253,8 @@ func (h *LifiCompactMessageHandler) verifyOutput(
221253
return fmt.Errorf("order has different output tokens")
222254
}
223255

224-
_, _, err = h.tokenStore.ConfigByAddress(chainID, common.HexToAddress(output.Token))
256+
address := common.BytesToAddress(common.Hex2Bytes(output.Token[2:])[12:])
257+
_, _, err = h.tokenStore.ConfigByAddress(chainID, address)
225258
if err != nil {
226259
return fmt.Errorf("token %s not configured", token)
227260
}
@@ -230,7 +263,8 @@ func (h *LifiCompactMessageHandler) verifyOutput(
230263
return fmt.Errorf("output call exceeds max length")
231264
}
232265

233-
if common.HexToAddress(output.Settler).Hex() != caller.Hex() {
266+
settlerAddress := common.BytesToAddress(common.Hex2Bytes(output.Settler[2:])[12:])
267+
if settlerAddress.Hex() != caller.Hex() {
234268
return fmt.Errorf("output settler %s is not caller %s", output.Settler, caller)
235269
}
236270

@@ -291,28 +325,36 @@ func (h *LifiCompactMessageHandler) verifySignatures(order *lifi.LifiOrder) erro
291325
if err != nil {
292326
return err
293327
}
294-
295328
expiryDeadline := time.Unix(order.Order.Expires, 0)
329+
296330
if time.Until(expiryDeadline) > resetPeriod {
297331
return fmt.Errorf("expiry less than reset period")
298332
}
299333
}
300334

335+
sSig, err := hex.DecodeString(order.SponsorSignature[2:])
336+
if err != nil {
337+
return err
338+
}
301339
valid, err := lifi.VerifyCompactSignature(
302340
digest,
303-
[]byte(order.SponsorSignature),
341+
sSig,
304342
common.HexToAddress(order.Order.User))
305343
if !valid || err != nil {
306344
return fmt.Errorf("sponsor signature invalid: %s", err)
307345
}
308346

347+
aSig, err := hex.DecodeString(order.AllocatorSignature[2:])
348+
if err != nil {
349+
return err
350+
}
309351
allocator, err := h.compact.Allocator(allocatorID)
310352
if err != nil {
311353
return err
312354
}
313355
valid, err = lifi.VerifyCompactSignature(
314356
digest,
315-
[]byte(order.AllocatorSignature),
357+
aSig,
316358
allocator,
317359
)
318360
if !valid || err != nil {
@@ -332,7 +374,7 @@ func (h *LifiCompactMessageHandler) verifySignatures(order *lifi.LifiOrder) erro
332374

333375
func (h *LifiCompactMessageHandler) Listen(ctx context.Context) {
334376
msgChn := make(chan *comm.WrappedMessage)
335-
subID := h.comm.Subscribe(fmt.Sprintf("%d-%s", h.chainID, comm.LifiSessionID), comm.MayanMsg, msgChn)
377+
subID := h.comm.Subscribe(fmt.Sprintf("%d-%s", h.chainID, comm.LifiSessionID), comm.LifiMsg, msgChn)
336378

337379
for {
338380
select {
@@ -372,5 +414,5 @@ func (h *LifiCompactMessageHandler) notify(data *LifiData) error {
372414
return err
373415
}
374416

375-
return h.comm.Broadcast(h.host.Peerstore().Peers(), msgBytes, comm.MayanMsg, fmt.Sprintf("%d-%s", h.chainID, comm.LifiSessionID))
417+
return h.comm.Broadcast(h.host.Peerstore().Peers(), msgBytes, comm.LifiMsg, fmt.Sprintf("%d-%s", h.chainID, comm.LifiSessionID))
376418
}

0 commit comments

Comments
 (0)