-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add general RFQ accept wire msg type #937
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d319b69
rfqmsg: add quote accept message type
ffranr a75c747
rfqmsg: add accept message type encode/decode unit test
ffranr af33bf4
rfqmsg: remove redundant type sellAcceptMsgData
ffranr 0d4010c
rfqmsg: remove redundant type buyAcceptMsgData
ffranr 8107ab3
multi: remove redundant field `AssetAmount` from `rfqmsg.BuyAccept`
ffranr 8bcf46e
multi: remove redundant field `AssetAmount` from `rfqmsg.SellAccept`
ffranr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
package rfqmsg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"github.com/lightningnetwork/lnd/tlv" | ||
) | ||
|
||
const ( | ||
// latestAcceptWireMsgDataVersion is the latest supported quote accept | ||
// wire message data field version. | ||
latestAcceptWireMsgDataVersion = V0 | ||
) | ||
|
||
type ( | ||
// acceptInOutRateTick is a type alias for a record that represents the | ||
// in-out rate tick of a quote accept message. | ||
acceptInOutRateTick = tlv.OptionalRecordT[tlv.TlvType4, uint64] | ||
|
||
// acceptOutInRateTick is a type alias for a record that represents the | ||
// out-in rate tick of a quote accept message. | ||
acceptOutInRateTick = tlv.OptionalRecordT[tlv.TlvType5, uint64] | ||
) | ||
|
||
// acceptWireMsgData is a struct that represents the message data field for | ||
// a quote accept wire message. | ||
type acceptWireMsgData struct { | ||
// Version is the version of the message data. | ||
Version tlv.RecordT[tlv.TlvType0, WireMsgDataVersion] | ||
|
||
// ID is the unique identifier of the quote request. | ||
ID tlv.RecordT[tlv.TlvType1, ID] | ||
|
||
// Expiry is the expiry Unix timestamp (in seconds) of the quote | ||
// request. This timestamp defines the lifetime of both the suggested | ||
// rate tick and the quote request. | ||
Expiry tlv.RecordT[tlv.TlvType2, uint64] | ||
|
||
// Sig is a signature over the serialized contents of the message. | ||
Sig tlv.RecordT[tlv.TlvType3, [64]byte] | ||
|
||
InOutRateTick acceptInOutRateTick | ||
|
||
OutInRateTick acceptOutInRateTick | ||
} | ||
|
||
// newAcceptWireMsgDataFromBuy creates a new acceptWireMsgData from a buy | ||
// accept message. | ||
func newAcceptWireMsgDataFromBuy(q BuyAccept) acceptWireMsgData { | ||
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version) | ||
id := tlv.NewRecordT[tlv.TlvType1](q.ID) | ||
expiry := tlv.NewPrimitiveRecord[tlv.TlvType2](q.Expiry) | ||
sig := tlv.NewPrimitiveRecord[tlv.TlvType3](q.sig) | ||
|
||
// When processing a buy request/accept, the incoming asset must be | ||
// specified. Currently, we assume the outgoing asset is BTC, | ||
// considering the perspective of the quote request initiator. | ||
// To indicate that this quote accept wire message is for a buy request, | ||
// we set the in-out rate tick instead of the out-in rate tick. | ||
inOutRateTick := tlv.SomeRecordT[tlv.TlvType4]( | ||
tlv.NewPrimitiveRecord[tlv.TlvType4]( | ||
uint64(q.AskPrice), | ||
), | ||
) | ||
|
||
// Encode message data component as TLV bytes. | ||
return acceptWireMsgData{ | ||
Version: version, | ||
ID: id, | ||
Expiry: expiry, | ||
Sig: sig, | ||
InOutRateTick: inOutRateTick, | ||
} | ||
} | ||
|
||
// newAcceptWireMsgDataFromSell creates a new acceptWireMsgData from a sell | ||
// accept message. | ||
func newAcceptWireMsgDataFromSell(q SellAccept) acceptWireMsgData { | ||
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version) | ||
id := tlv.NewRecordT[tlv.TlvType1](q.ID) | ||
expiry := tlv.NewPrimitiveRecord[tlv.TlvType2](q.Expiry) | ||
sig := tlv.NewPrimitiveRecord[tlv.TlvType3](q.sig) | ||
|
||
// When processing a sell request/accept, the outgoing asset must be | ||
// specified. Currently, we assume the incoming asset is BTC, | ||
// considering the perspective of the quote request initiator. | ||
// To indicate that this quote accept wire message is for a sell | ||
// request, we set the out-in rate tick instead of the in-out rate tick. | ||
outInRateTick := tlv.SomeRecordT[tlv.TlvType5]( | ||
tlv.NewPrimitiveRecord[tlv.TlvType5]( | ||
uint64(q.BidPrice), | ||
), | ||
) | ||
|
||
// Encode message data component as TLV bytes. | ||
return acceptWireMsgData{ | ||
Version: version, | ||
ID: id, | ||
Expiry: expiry, | ||
Sig: sig, | ||
OutInRateTick: outInRateTick, | ||
} | ||
} | ||
|
||
// Validate ensures that the quote accept message is valid. | ||
func (m *acceptWireMsgData) Validate() error { | ||
// Ensure the version specified in the version field is supported. | ||
if m.Version.Val > latestAcceptWireMsgDataVersion { | ||
return fmt.Errorf("unsupported quote accept message data "+ | ||
"version: %d", m.Version.Val) | ||
} | ||
|
||
// Ensure that the expiry is set to a future time. | ||
if m.Expiry.Val <= uint64(time.Now().Unix()) { | ||
return fmt.Errorf("expiry must be set to a future time") | ||
} | ||
|
||
// Ensure that at least one of the rate ticks is set. | ||
if m.InOutRateTick.IsNone() && m.OutInRateTick.IsNone() { | ||
return fmt.Errorf("at least one of the rate ticks must be set") | ||
} | ||
|
||
// Ensure that both rate ticks are not set. | ||
if m.InOutRateTick.IsSome() && m.OutInRateTick.IsSome() { | ||
return fmt.Errorf("both rate ticks cannot be set") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Encode serializes the acceptWireMsgData to the given io.Writer. | ||
func (m *acceptWireMsgData) Encode(w io.Writer) error { | ||
// Validate the message before encoding. | ||
err := m.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
records := []tlv.Record{ | ||
m.Version.Record(), | ||
m.ID.Record(), | ||
m.Expiry.Record(), | ||
m.Sig.Record(), | ||
} | ||
|
||
m.InOutRateTick.WhenSome( | ||
func(r tlv.RecordT[tlv.TlvType4, uint64]) { | ||
records = append(records, r.Record()) | ||
}, | ||
) | ||
|
||
m.OutInRateTick.WhenSome( | ||
func(r tlv.RecordT[tlv.TlvType5, uint64]) { | ||
records = append(records, r.Record()) | ||
}, | ||
) | ||
|
||
tlv.SortRecords(records) | ||
|
||
// Create the tlv stream. | ||
tlvStream, err := tlv.NewStream(records...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tlvStream.Encode(w) | ||
} | ||
|
||
// Decode deserializes the acceptWireMsgData from the given io.Reader. | ||
func (m *acceptWireMsgData) Decode(r io.Reader) error { | ||
// Define zero values for optional fields. | ||
inOutRateTick := m.InOutRateTick.Zero() | ||
outInRateTick := m.OutInRateTick.Zero() | ||
|
||
// Create a tlv stream with all the fields. | ||
tlvStream, err := tlv.NewStream( | ||
m.Version.Record(), | ||
m.ID.Record(), | ||
m.Expiry.Record(), | ||
m.Sig.Record(), | ||
|
||
inOutRateTick.Record(), | ||
outInRateTick.Record(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Decode the reader's contents into the tlv stream. | ||
tlvMap, err := tlvStream.DecodeWithParsedTypes(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set optional fields if they are present. | ||
if _, ok := tlvMap[inOutRateTick.TlvType()]; ok { | ||
m.InOutRateTick = tlv.SomeRecordT(inOutRateTick) | ||
} | ||
|
||
if _, ok := tlvMap[outInRateTick.TlvType()]; ok { | ||
m.OutInRateTick = tlv.SomeRecordT(outInRateTick) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Bytes encodes the structure into a TLV stream and returns the bytes. | ||
func (m *acceptWireMsgData) Bytes() ([]byte, error) { | ||
var b bytes.Buffer | ||
err := m.Encode(&b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return b.Bytes(), nil | ||
} | ||
|
||
// NewIncomingAcceptFromWire creates a new quote accept message from an incoming | ||
// wire message. | ||
// | ||
// This is an incoming accept message. An incoming buy accept message indicates | ||
// that our peer accepts our buy request, meaning they are willing to sell the | ||
// asset to us. Conversely, an incoming sell accept message indicates that our | ||
// peer accepts our sell request, meaning they are willing to buy the asset from | ||
// us. | ||
func NewIncomingAcceptFromWire(wireMsg WireMessage) (IncomingMsg, error) { | ||
// Ensure that the message type is a quote accept message. | ||
if wireMsg.MsgType != MsgTypeAccept { | ||
return nil, ErrUnknownMessageType | ||
} | ||
|
||
var msgData acceptWireMsgData | ||
err := msgData.Decode(bytes.NewBuffer(wireMsg.Data)) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to decode incoming quote "+ | ||
"accept message data: %w", err) | ||
} | ||
|
||
if err := msgData.Validate(); err != nil { | ||
return nil, fmt.Errorf("unable to validate incoming "+ | ||
"quote accept message: %w", err) | ||
} | ||
|
||
// We will now determine whether this is a buy or sell accept. We can | ||
// distinguish between buy/sell accept messages by inspecting which tick | ||
// rate field is populated. | ||
isBuyAccept := msgData.InOutRateTick.IsSome() | ||
|
||
// If this is a buy request, then we will create a new buy request | ||
// message. | ||
if isBuyAccept { | ||
return newBuyAcceptFromWireMsg(wireMsg, msgData) | ||
} | ||
|
||
// Otherwise, this is a sell request. | ||
return newSellAcceptFromWireMsg(wireMsg, msgData) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing Godoc comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will address in my PR.