Skip to content

Commit

Permalink
rfqmsg: revise request wire message TLV type integers
Browse files Browse the repository at this point in the history
Update TLV type integers to align with the latest BLIP state. Some
integer values are intentionally skipped to reserve space for new
fields defined in the BLIP.
  • Loading branch information
ffranr committed Oct 17, 2024
1 parent 5766a0d commit adc6b6b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
6 changes: 3 additions & 3 deletions rfqmsg/buy_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func NewBuyRequestMsgFromWire(wireMsg WireMessage,

var assetID *asset.ID
msgData.InAssetID.WhenSome(
func(inAssetID tlv.RecordT[tlv.TlvType5, asset.ID]) {
func(inAssetID tlv.RecordT[tlv.TlvType9, asset.ID]) {
assetID = &inAssetID.Val
},
)

var assetGroupKey *btcec.PublicKey
msgData.InAssetGroupKey.WhenSome(
func(key tlv.RecordT[tlv.TlvType6, *btcec.PublicKey]) {
func(key tlv.RecordT[tlv.TlvType11, *btcec.PublicKey]) {
assetGroupKey = key.Val
},
)
Expand All @@ -107,7 +107,7 @@ func NewBuyRequestMsgFromWire(wireMsg WireMessage,
// Extract the suggested asset to BTC rate if provided.
var suggestedAssetRate fn.Option[rfqmath.BigIntFixedPoint]
msgData.SuggestedAssetRate.WhenSome(
func(rate tlv.RecordT[tlv.TlvType4, TlvFixedPoint]) {
func(rate tlv.RecordT[tlv.TlvType19, TlvFixedPoint]) {
fp := rate.Val.IntoBigIntFixedPoint()
suggestedAssetRate =
fn.Some[rfqmath.BigIntFixedPoint](fp)
Expand Down
85 changes: 43 additions & 42 deletions rfqmsg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ type (
// requestSuggestedAssetRate is a type alias for a record that
// represents the suggested asset to BTC rate for the quote request.
requestSuggestedAssetRate = tlv.OptionalRecordT[
tlv.TlvType4, TlvFixedPoint,
tlv.TlvType19, TlvFixedPoint,
]

// requestInAssetID is a type alias for a record that represents the
// asset ID of the inbound asset.
requestInAssetID = tlv.OptionalRecordT[tlv.TlvType5, asset.ID]
requestInAssetID = tlv.OptionalRecordT[tlv.TlvType9, asset.ID]

// requestInAssetGroupKey is a type alias for a record that represents
// the public group key of the inbound asset.
requestInAssetGroupKey = tlv.OptionalRecordT[
tlv.TlvType6, *btcec.PublicKey,
tlv.TlvType11, *btcec.PublicKey,
]

// requestOutAssetID is a type alias for a record that represents the
// asset ID of the outbound asset.
requestOutAssetID = tlv.OptionalRecordT[tlv.TlvType7, asset.ID]
requestOutAssetID = tlv.OptionalRecordT[tlv.TlvType13, asset.ID]

// requestOutAssetGroupKey is a type alias for a record that represents
// the public group key of the outbound asset.
requestOutAssetGroupKey = tlv.OptionalRecordT[
tlv.TlvType8, *btcec.PublicKey,
tlv.TlvType15, *btcec.PublicKey,
]
)

Expand All @@ -57,16 +57,16 @@ type requestWireMsgData struct {
Version tlv.RecordT[tlv.TlvType0, WireMsgDataVersion]

// ID is the unique identifier of the quote request.
ID tlv.RecordT[tlv.TlvType1, ID]
ID tlv.RecordT[tlv.TlvType2, 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]
Expiry tlv.RecordT[tlv.TlvType6, uint64]

// AssetMaxAmount represents the maximum asset amount that the target
// peer is expected to accept/divest.
AssetMaxAmount tlv.RecordT[tlv.TlvType3, uint64]
AssetMaxAmount tlv.RecordT[tlv.TlvType16, uint64]

// SuggestedAssetRate is the peer's proposed asset to BTC rate. This is
// not the final rate, but a suggested rate that the requesting peer
Expand Down Expand Up @@ -104,38 +104,38 @@ type requestWireMsgData struct {
// request.
func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
version := tlv.NewRecordT[tlv.TlvType0](q.Version)
id := tlv.NewRecordT[tlv.TlvType1](q.ID)
id := tlv.NewRecordT[tlv.TlvType2](q.ID)

// Calculate the expiration unix timestamp in seconds.
// TODO(ffranr): The expiry timestamp should be obtained from the
// request message.
expiry := tlv.NewPrimitiveRecord[tlv.TlvType2](
expiry := tlv.NewPrimitiveRecord[tlv.TlvType6](
uint64(time.Now().Add(defaultRequestExpiry).Unix()),
)

assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType3](q.AssetAmount)
assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType16](q.AssetAmount)

// Convert the suggested asset to BTC rate to a TLV record.
var suggestedAssetRate requestSuggestedAssetRate
q.SuggestedAssetRate.WhenSome(func(rate rfqmath.BigIntFixedPoint) {
// Convert the BigIntFixedPoint to a Uint64FixedPoint.
wireRate := NewTlvFixedPointFromBigInt(rate)
suggestedAssetRate = tlv.SomeRecordT[tlv.TlvType4](
tlv.NewRecordT[tlv.TlvType4](wireRate),
suggestedAssetRate = tlv.SomeRecordT[tlv.TlvType19](
tlv.NewRecordT[tlv.TlvType19](wireRate),
)
})

var inAssetID requestInAssetID
if q.AssetID != nil {
inAssetID = tlv.SomeRecordT[tlv.TlvType5](
tlv.NewPrimitiveRecord[tlv.TlvType5](*q.AssetID),
inAssetID = tlv.SomeRecordT[tlv.TlvType9](
tlv.NewPrimitiveRecord[tlv.TlvType9](*q.AssetID),
)
}

var inAssetGroupKey requestInAssetGroupKey
if q.AssetGroupKey != nil {
inAssetGroupKey = tlv.SomeRecordT[tlv.TlvType6](
tlv.NewPrimitiveRecord[tlv.TlvType6](
inAssetGroupKey = tlv.SomeRecordT[tlv.TlvType11](
tlv.NewPrimitiveRecord[tlv.TlvType11](
q.AssetGroupKey,
),
)
Expand All @@ -144,8 +144,8 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
// Use a zero asset ID for the outbound asset ID. This indicates that
// the outbound asset is BTC.
var zeroOutAssetID asset.ID
outAssetID := tlv.SomeRecordT[tlv.TlvType7](
tlv.NewPrimitiveRecord[tlv.TlvType7](zeroOutAssetID),
outAssetID := tlv.SomeRecordT[tlv.TlvType13](
tlv.NewPrimitiveRecord[tlv.TlvType13](zeroOutAssetID),
)

outAssetGroupKey := requestOutAssetGroupKey{}
Expand All @@ -168,22 +168,22 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
// request.
func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version)
id := tlv.NewRecordT[tlv.TlvType1](q.ID)
id := tlv.NewRecordT[tlv.TlvType2](q.ID)

// Calculate the expiration unix timestamp in seconds.
expiry := tlv.NewPrimitiveRecord[tlv.TlvType2](
expiry := tlv.NewPrimitiveRecord[tlv.TlvType6](
uint64(time.Now().Add(defaultRequestExpiry).Unix()),
)

assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType3](q.AssetAmount)
assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType16](q.AssetAmount)

// Convert the suggested rate to a TLV record.
var suggestedRateTick requestSuggestedAssetRate
q.SuggestedAssetRate.WhenSome(func(rate rfqmath.BigIntFixedPoint) {
// Convert the BigIntFixedPoint to a Uint64FixedPoint.
wireRate := NewTlvFixedPointFromBigInt(rate)
suggestedRateTick = tlv.SomeRecordT[tlv.TlvType4](
tlv.NewRecordT[tlv.TlvType4](
suggestedRateTick = tlv.SomeRecordT[tlv.TlvType19](
tlv.NewRecordT[tlv.TlvType19](
wireRate,
),
)
Expand All @@ -195,21 +195,21 @@ func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
// Use a zero asset ID for the inbound asset ID. This indicates that
// the inbound asset is BTC.
var zeroAssetID asset.ID
inAssetID := tlv.SomeRecordT[tlv.TlvType5](
tlv.NewPrimitiveRecord[tlv.TlvType5](zeroAssetID),
inAssetID := tlv.SomeRecordT[tlv.TlvType9](
tlv.NewPrimitiveRecord[tlv.TlvType9](zeroAssetID),
)

outAssetID := requestOutAssetID{}
if q.AssetID != nil {
outAssetID = tlv.SomeRecordT[tlv.TlvType7](
tlv.NewPrimitiveRecord[tlv.TlvType7](*q.AssetID),
outAssetID = tlv.SomeRecordT[tlv.TlvType13](
tlv.NewPrimitiveRecord[tlv.TlvType13](*q.AssetID),
)
}

outAssetGroupKey := requestOutAssetGroupKey{}
if q.AssetGroupKey != nil {
outAssetGroupKey = tlv.SomeRecordT[tlv.TlvType8](
tlv.NewPrimitiveRecord[tlv.TlvType8](
outAssetGroupKey = tlv.SomeRecordT[tlv.TlvType15](
tlv.NewPrimitiveRecord[tlv.TlvType15](
q.AssetGroupKey,
),
)
Expand Down Expand Up @@ -269,14 +269,14 @@ func (m *requestWireMsgData) Validate() error {

inAssetIsBTC := false
m.InAssetID.WhenSome(
func(inAssetID tlv.RecordT[tlv.TlvType5, asset.ID]) {
func(inAssetID tlv.RecordT[tlv.TlvType9, asset.ID]) {
inAssetIsBTC = inAssetID.Val == zeroAssetID
},
)

outAssetIsBTC := false
m.OutAssetID.WhenSome(
func(outAssetID tlv.RecordT[tlv.TlvType7, asset.ID]) {
func(outAssetID tlv.RecordT[tlv.TlvType13, asset.ID]) {
outAssetIsBTC = outAssetID.Val == zeroAssetID
},
)
Expand Down Expand Up @@ -305,31 +305,31 @@ func (m *requestWireMsgData) Encode(w io.Writer) error {
}

m.SuggestedAssetRate.WhenSome(
func(r tlv.RecordT[tlv.TlvType4, TlvFixedPoint]) {
func(r tlv.RecordT[tlv.TlvType19, TlvFixedPoint]) {
records = append(records, r.Record())
},
)

// Encode the inbound asset.
m.InAssetID.WhenSome(
func(r tlv.RecordT[tlv.TlvType5, asset.ID]) {
func(r tlv.RecordT[tlv.TlvType9, asset.ID]) {
records = append(records, r.Record())
},
)
m.InAssetGroupKey.WhenSome(
func(r tlv.RecordT[tlv.TlvType6, *btcec.PublicKey]) {
func(r tlv.RecordT[tlv.TlvType11, *btcec.PublicKey]) {
records = append(records, r.Record())
},
)

// Encode the outbound asset.
m.OutAssetID.WhenSome(
func(r tlv.RecordT[tlv.TlvType7, asset.ID]) {
func(r tlv.RecordT[tlv.TlvType13, asset.ID]) {
records = append(records, r.Record())
},
)
m.OutAssetGroupKey.WhenSome(
func(r tlv.RecordT[tlv.TlvType8, *btcec.PublicKey]) {
func(r tlv.RecordT[tlv.TlvType15, *btcec.PublicKey]) {
records = append(records, r.Record())
},
)
Expand Down Expand Up @@ -361,15 +361,16 @@ func (m *requestWireMsgData) Decode(r io.Reader) error {
m.Version.Record(),
m.ID.Record(),
m.Expiry.Record(),
m.AssetMaxAmount.Record(),

suggestedAssetRate.Record(),

inAssetID.Record(),
inAssetGroupKey.Record(),

outAssetID.Record(),
outAssetGroupKey.Record(),

m.AssetMaxAmount.Record(),

suggestedAssetRate.Record(),
)
if err != nil {
return err
Expand Down Expand Up @@ -446,7 +447,7 @@ func NewIncomingRequestFromWire(wireMsg WireMessage) (IncomingMsg, error) {

// Check the outgoing asset ID to determine if this is a buy request.
msgData.OutAssetID.WhenSome(
func(outAssetID tlv.RecordT[tlv.TlvType7, asset.ID]) {
func(outAssetID tlv.RecordT[tlv.TlvType13, asset.ID]) {
var zeroAssetID [32]byte

// If the outgoing asset ID is all zeros (signifying
Expand All @@ -463,7 +464,7 @@ func NewIncomingRequestFromWire(wireMsg WireMessage) (IncomingMsg, error) {
// In other words, only the inbound asset is specified, and the outbound
// asset is BTC.
msgData.OutAssetGroupKey.WhenSome(
func(gk tlv.RecordT[tlv.TlvType8, *btcec.PublicKey]) {
func(gk tlv.RecordT[tlv.TlvType15, *btcec.PublicKey]) {
// Here we carry through any ture value of isBuyRequest
// from the previous check.
isBuyRequest = isBuyRequest || (gk.Val != nil)
Expand Down
26 changes: 13 additions & 13 deletions rfqmsg/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,48 @@ type testCaseEncodeDecode struct {
// Request generates a requestWireMsgData instance from the test case.
func (tc testCaseEncodeDecode) Request() requestWireMsgData {
version := tlv.NewPrimitiveRecord[tlv.TlvType0](tc.version)
id := tlv.NewPrimitiveRecord[tlv.TlvType1](tc.id)
expiry := tlv.NewPrimitiveRecord[tlv.TlvType2](tc.expiry)
assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType3](
id := tlv.NewPrimitiveRecord[tlv.TlvType2](tc.id)
expiry := tlv.NewPrimitiveRecord[tlv.TlvType6](tc.expiry)
assetMaxAmount := tlv.NewPrimitiveRecord[tlv.TlvType16](
tc.assetMaxAmount,
)

var suggestedAssetRate requestSuggestedAssetRate
if tc.suggestedAssetRate != nil {
// We use a fixed-point scale of 2 here just for testing.
rate := NewTlvFixedPointFromUint64(*tc.suggestedAssetRate, 2)
suggestedAssetRate = tlv.SomeRecordT[tlv.TlvType4](
tlv.NewRecordT[tlv.TlvType4](rate),
suggestedAssetRate = tlv.SomeRecordT[tlv.TlvType19](
tlv.NewRecordT[tlv.TlvType19](rate),
)
}

var inAssetID requestInAssetID
if tc.inAssetId != nil {
inAssetID = tlv.SomeRecordT[tlv.TlvType5](
tlv.NewPrimitiveRecord[tlv.TlvType5](*tc.inAssetId),
inAssetID = tlv.SomeRecordT[tlv.TlvType9](
tlv.NewPrimitiveRecord[tlv.TlvType9](*tc.inAssetId),
)
}

var inAssetGroupKey requestInAssetGroupKey
if tc.inAssetGroupKey != nil {
inAssetGroupKey = tlv.SomeRecordT[tlv.TlvType6](
tlv.NewPrimitiveRecord[tlv.TlvType6](
inAssetGroupKey = tlv.SomeRecordT[tlv.TlvType11](
tlv.NewPrimitiveRecord[tlv.TlvType11](
tc.inAssetGroupKey,
),
)
}

var outAssetID requestOutAssetID
if tc.outAssetId != nil {
outAssetID = tlv.SomeRecordT[tlv.TlvType7](
tlv.NewPrimitiveRecord[tlv.TlvType7](*tc.outAssetId),
outAssetID = tlv.SomeRecordT[tlv.TlvType13](
tlv.NewPrimitiveRecord[tlv.TlvType13](*tc.outAssetId),
)
}

var outAssetGroupKey requestOutAssetGroupKey
if tc.outAssetGroupKey != nil {
outAssetGroupKey = tlv.SomeRecordT[tlv.TlvType8](
tlv.NewPrimitiveRecord[tlv.TlvType8](
outAssetGroupKey = tlv.SomeRecordT[tlv.TlvType15](
tlv.NewPrimitiveRecord[tlv.TlvType15](
tc.outAssetGroupKey,
),
)
Expand Down
6 changes: 3 additions & 3 deletions rfqmsg/sell_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ func NewSellRequestMsgFromWire(wireMsg WireMessage,
// Extract outbound asset ID/group key.
var assetID *asset.ID
msgData.OutAssetID.WhenSome(
func(inAssetID tlv.RecordT[tlv.TlvType7, asset.ID]) {
func(inAssetID tlv.RecordT[tlv.TlvType13, asset.ID]) {
assetID = &inAssetID.Val
},
)

var assetGroupKey *btcec.PublicKey
msgData.OutAssetGroupKey.WhenSome(
// nolint: lll
func(inAssetGroupKey tlv.RecordT[tlv.TlvType8, *btcec.PublicKey]) {
func(inAssetGroupKey tlv.RecordT[tlv.TlvType15, *btcec.PublicKey]) {
assetGroupKey = inAssetGroupKey.Val
},
)
Expand All @@ -110,7 +110,7 @@ func NewSellRequestMsgFromWire(wireMsg WireMessage,
// Extract the suggested asset to BTC rate if provided.
var suggestedAssetRate fn.Option[rfqmath.BigIntFixedPoint]
msgData.SuggestedAssetRate.WhenSome(
func(rate tlv.RecordT[tlv.TlvType4, TlvFixedPoint]) {
func(rate tlv.RecordT[tlv.TlvType19, TlvFixedPoint]) {
fp := rate.Val.IntoBigIntFixedPoint()
suggestedAssetRate =
fn.Some[rfqmath.BigIntFixedPoint](fp)
Expand Down

0 comments on commit adc6b6b

Please sign in to comment.