Skip to content
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

rfq: actually set subject asset in QueryRateTick #1076

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-nit: could be fn.CopySlice(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)
guggero marked this conversation as resolved.
Show resolved Hide resolved

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