Skip to content

Commit

Permalink
Merge pull request #1076 from lightninglabs/rfq-fix-asset-id
Browse files Browse the repository at this point in the history
rfq: actually set subject asset in QueryRateTick
  • Loading branch information
guggero authored Aug 13, 2024
2 parents dde75b8 + c369d6a commit 4ec0dd4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
22 changes: 14 additions & 8 deletions rfq/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
return nil, fmt.Errorf("asset ID is nil")
}

// Construct query request.
var subjectAssetId []byte
copy(subjectAssetId, assetId[:])
var (
subjectAssetId = make([]byte, 32)
paymentAssetId = make([]byte, 32)
)

paymentAssetId := make([]byte, 32)
// The payment asset ID is BTC, so we leave it at all zeroes. We only
// set the subject asset ID.
copy(subjectAssetId, assetId[:])

// Construct the RPC rate tick hint.
var rateTickHint *oraclerpc.RateTick
Expand Down Expand Up @@ -303,11 +306,14 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context, assetId *asset.ID,
return nil, fmt.Errorf("asset ID is nil")
}

// Construct query request.
var subjectAssetId []byte
copy(subjectAssetId, assetId[:])
var (
subjectAssetId = make([]byte, 32)
paymentAssetId = make([]byte, 32)
)

paymentAssetId := make([]byte, 32)
// The payment asset ID is BTC, so we leave it at all zeroes. We only
// set the subject asset ID.
copy(subjectAssetId, assetId[:])

req := &oraclerpc.QueryRateTickRequest{
TransactionType: oraclerpc.TransactionType_PURCHASE,
Expand Down
34 changes: 33 additions & 1 deletion rfq/oracle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rfq

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -43,6 +44,11 @@ func (p *mockRpcPriceOracleServer) QueryRateTick(_ context.Context,
ExpiryTimestamp: uint64(expiry),
}

err := validateRateTickRequest(req)
if err != nil {
return nil, err
}

// If a rate tick hint is provided, return it as the rate tick.
if req.RateTickHint != nil {
rateTick.Rate = req.RateTickHint.Rate
Expand All @@ -58,6 +64,32 @@ func (p *mockRpcPriceOracleServer) QueryRateTick(_ context.Context,
}, nil
}

// validateRateTickRequest validates the given rate tick request.
func validateRateTickRequest(req *priceoraclerpc.QueryRateTickRequest) error {
var zeroAssetID [32]byte
if req.SubjectAsset == nil {
return fmt.Errorf("subject asset must be specified")
}
if len(req.SubjectAsset.GetAssetId()) != 32 {
return fmt.Errorf("invalid subject asset ID length")
}
if bytes.Equal(req.SubjectAsset.GetAssetId(), zeroAssetID[:]) {
return fmt.Errorf("subject asset ID must NOT be all zero")
}

if req.PaymentAsset == nil {
return fmt.Errorf("payment asset must be specified")
}
if len(req.PaymentAsset.GetAssetId()) != 32 {
return fmt.Errorf("invalid payment asset ID length")
}
if !bytes.Equal(req.PaymentAsset.GetAssetId(), zeroAssetID[:]) {
return fmt.Errorf("payment asset ID must be all zero")
}

return nil
}

// startBackendRPC starts the given RPC server and blocks until the server is
// shut down.
func startBackendRPC(grpcServer *grpc.Server) error {
Expand Down Expand Up @@ -95,7 +127,7 @@ func runQueryAskPriceTest(t *testing.T, tc *testCaseQueryAskPrice) {
defer backendService.Stop()

// Wait for the server to start.
time.Sleep(2 * time.Second)
time.Sleep(200 * time.Millisecond)

// Create a new RPC price oracle client and connect to the mock service.
serviceAddr := fmt.Sprintf("rfqrpc://%s", testServiceAddress)
Expand Down

0 comments on commit 4ec0dd4

Please sign in to comment.