Skip to content

Commit

Permalink
Merge pull request #860 from lightninglabs/rfq-cleanup
Browse files Browse the repository at this point in the history
RFQ cleanup
  • Loading branch information
ffranr authored Apr 2, 2024
2 parents 1f37bdf + 6155862 commit 2affd4e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 58 deletions.
24 changes: 9 additions & 15 deletions itest/rfq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,13 @@ func testRfqAssetBuyHtlcIntercept(t *harnessTest) {

// Wait until Carol receives an incoming quote accept message (sent from
// Bob) RFQ event notification.
waitErr := wait.NoError(func() error {
BeforeTimeout(t.t, func() {
event, err := carolEventNtfns.Recv()
require.NoError(t.t, err)

_, ok := event.Event.(*rfqrpc.RfqEvent_PeerAcceptedBuyQuote)
require.True(t.t, ok, "unexpected event: %v", event)

return nil
}, defaultWaitTimeout)
require.NoError(t.t, waitErr)

// Carol should have received an accepted quote from Bob. This accepted
// quote can be used by Carol to make a payment to Bob.
Expand Down Expand Up @@ -188,25 +185,22 @@ func testRfqAssetBuyHtlcIntercept(t *harnessTest) {
// At this point Bob should have received a HTLC with the asset transfer
// specific scid. We'll wait for Bob to publish an accept HTLC event and
// then validate it against the accepted quote.
waitErr = wait.NoError(func() error {
BeforeTimeout(t.t, func() {
t.Log("Waiting for Bob to receive HTLC")

event, err := bobEventNtfns.Recv()
require.NoError(t.t, err)

acceptHtlc, ok := event.Event.(*rfqrpc.RfqEvent_AcceptHtlc)
if ok {
require.Equal(
t.t, acceptedQuote.Scid,
acceptHtlc.AcceptHtlc.Scid,
)
t.Log("Bob has accepted the HTLC")
return nil
}
require.True(t.t, ok, "unexpected event type: %v", event)

return fmt.Errorf("unexpected event: %v", event)
// Ensure that the scid of the HTLC matches the scid of the
// accepted quote.
require.Equal(
t.t, acceptedQuote.Scid, acceptHtlc.AcceptHtlc.Scid,
)
t.Log("Bob has accepted the HTLC")
}, defaultWaitTimeout)
require.NoError(t.t, waitErr)

// Close event streams.
err = carolEventNtfns.CloseSend()
Expand Down
40 changes: 10 additions & 30 deletions rfqmsg/buy_accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,48 +85,28 @@ type buyAcceptMsgData struct {
sig [64]byte
}

// encodeRecords determines the non-nil records to include when encoding at
// runtime.
func (q *buyAcceptMsgData) encodeRecords() []tlv.Record {
var records []tlv.Record

// Add id record.
records = append(records, TypeRecordBuyAcceptID(&q.ID))

// Add ask price record.
records = append(records, TypeRecordBuyAcceptAskPrice(&q.AskPrice))

// Add expiry record.
records = append(records, TypeRecordBuyAcceptExpiry(&q.Expiry))

// Add signature record.
records = append(records, TypeRecordBuyAcceptSig(&q.sig))

return records
// records provides all TLV records for encoding/decoding.
func (q *buyAcceptMsgData) records() []tlv.Record {
return []tlv.Record{
TypeRecordBuyAcceptID(&q.ID),
TypeRecordBuyAcceptAskPrice(&q.AskPrice),
TypeRecordBuyAcceptExpiry(&q.Expiry),
TypeRecordBuyAcceptSig(&q.sig),
}
}

// Encode encodes the structure into a TLV stream.
func (q *buyAcceptMsgData) Encode(writer io.Writer) error {
stream, err := tlv.NewStream(q.encodeRecords()...)
stream, err := tlv.NewStream(q.records()...)
if err != nil {
return err
}
return stream.Encode(writer)
}

// DecodeRecords provides all TLV records for decoding.
func (q *buyAcceptMsgData) decodeRecords() []tlv.Record {
return []tlv.Record{
TypeRecordBuyAcceptID(&q.ID),
TypeRecordBuyAcceptAskPrice(&q.AskPrice),
TypeRecordBuyAcceptExpiry(&q.Expiry),
TypeRecordBuyAcceptSig(&q.sig),
}
}

// Decode decodes the structure from a TLV stream.
func (q *buyAcceptMsgData) Decode(r io.Reader) error {
stream, err := tlv.NewStream(q.decodeRecords()...)
stream, err := tlv.NewStream(q.records()...)
if err != nil {
return err
}
Expand Down
64 changes: 64 additions & 0 deletions rfqmsg/buy_accept_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rfqmsg

import (
"bytes"
"encoding/binary"
"math/rand"
"testing"
Expand Down Expand Up @@ -40,3 +41,66 @@ func TestAcceptShortChannelId(t *testing.T) {
// short channel ID.
require.Equal(t, scidInt, uint64(actualScidInt))
}

// TestBuyAcceptMsgDataEncodeDecode tests the encoding and decoding of a buy
// accept message.
func TestBuyAcceptMsgDataEncodeDecode(t *testing.T) {
t.Parallel()

// Create a random ID.
randomIdBytes := test.RandBytes(32)
id := ID(randomIdBytes)

// Create a random signature.
randomSigBytes := test.RandBytes(64)
var signature [64]byte
copy(signature[:], randomSigBytes[:])

testCases := []struct {
testName string

id ID
askPrice lnwire.MilliSatoshi
expiry uint64
sig [64]byte
}{
{
testName: "all fields populated with basic values",
id: id,
askPrice: 1000,
expiry: 42000,
sig: signature,
},
{
testName: "empty fields",
id: [32]byte{},
askPrice: 0,
expiry: 0,
sig: [64]byte{},
},
}

for _, tc := range testCases {
t.Run(tc.testName, func(tt *testing.T) {
msg := buyAcceptMsgData{
ID: tc.id,
AskPrice: tc.askPrice,
Expiry: tc.expiry,
sig: tc.sig,
}

// Encode the message.
reqBytes, err := msg.Bytes()
require.NoError(tt, err, "unable to encode message")

// Decode the message.
decodedMsg := buyAcceptMsgData{}
err = decodedMsg.Decode(bytes.NewReader(reqBytes))
require.NoError(tt, err, "unable to decode message")

// Assert that the decoded message is equal to the
// original message.
require.Equal(tt, msg, decodedMsg)
})
}
}
8 changes: 4 additions & 4 deletions taprpc/rfqrpc/rfq.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions taprpc/rfqrpc/rfq.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ message AddAssetBuyOrderRequest {
// The maximum amount BTC to spend (units: millisats).
uint64 max_bid = 3;

// The unix timestamp after which the order is no longer valid.
// The unix timestamp in seconds after which the order is no longer valid.
uint64 expiry = 4;

// peer_pub_key is an optional field for specifying the public key of the
Expand Down Expand Up @@ -130,7 +130,7 @@ message PeerAcceptedBuyQuote {
// ask_price is the price in millisats for the entire asset amount.
uint64 ask_price = 5;

// The unix timestamp after which the quote is no longer valid.
// The unix timestamp in seconds after which the quote is no longer valid.
uint64 expiry = 6;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ message SubscribeRfqEventNtfnsRequest {
}

message PeerAcceptedBuyQuoteEvent {
// Unix timestamp.
// Unix timestamp in microseconds.
uint64 timestamp = 1;

// The asset buy quote that was accepted by out peer.
Expand All @@ -185,7 +185,7 @@ message PeerAcceptedSellQuoteEvent {
}

message AcceptHtlcEvent {
// Unix timestamp.
// Unix timestamp in microseconds.
uint64 timestamp = 1;

// scid is the short channel ID of the channel over which the payment for
Expand Down
10 changes: 5 additions & 5 deletions taprpc/rfqrpc/rfq.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"expiry": {
"type": "string",
"format": "uint64",
"description": "The unix timestamp after which the order is no longer valid."
"description": "The unix timestamp in seconds after which the order is no longer valid."
},
"peer_pub_key": {
"type": "string",
Expand Down Expand Up @@ -166,7 +166,7 @@
"expiry": {
"type": "string",
"format": "uint64",
"description": "The unix timestamp after which the order is no longer valid."
"description": "The unix timestamp in seconds after which the order is no longer valid."
},
"peer_pub_key": {
"type": "string",
Expand Down Expand Up @@ -566,7 +566,7 @@
"timestamp": {
"type": "string",
"format": "uint64",
"description": "Unix timestamp."
"description": "Unix timestamp in microseconds."
},
"scid": {
"type": "string",
Expand Down Expand Up @@ -637,7 +637,7 @@
"expiry": {
"type": "string",
"format": "uint64",
"description": "The unix timestamp after which the quote is no longer valid."
"description": "The unix timestamp in seconds after which the quote is no longer valid."
}
}
},
Expand All @@ -647,7 +647,7 @@
"timestamp": {
"type": "string",
"format": "uint64",
"description": "Unix timestamp."
"description": "Unix timestamp in microseconds."
},
"peer_accepted_buy_quote": {
"$ref": "#/definitions/rfqrpcPeerAcceptedBuyQuote",
Expand Down

0 comments on commit 2affd4e

Please sign in to comment.