Skip to content

Commit c369d6a

Browse files
committed
rfq: actually set subject asset in QueryRateTick
Fixes the problem that because of the missing allocation, the subject asset ID was always set to an empty slice.
1 parent 39c1f27 commit c369d6a

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

rfq/oracle.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,14 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
221221
return nil, fmt.Errorf("asset ID is nil")
222222
}
223223

224-
// Construct query request.
225-
var subjectAssetId []byte
226-
copy(subjectAssetId, assetId[:])
224+
var (
225+
subjectAssetId = make([]byte, 32)
226+
paymentAssetId = make([]byte, 32)
227+
)
227228

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

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

306-
// Construct query request.
307-
var subjectAssetId []byte
308-
copy(subjectAssetId, assetId[:])
309+
var (
310+
subjectAssetId = make([]byte, 32)
311+
paymentAssetId = make([]byte, 32)
312+
)
309313

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

312318
req := &oraclerpc.QueryRateTickRequest{
313319
TransactionType: oraclerpc.TransactionType_PURCHASE,

rfq/oracle_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rfq
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"net"
@@ -43,6 +44,11 @@ func (p *mockRpcPriceOracleServer) QueryRateTick(_ context.Context,
4344
ExpiryTimestamp: uint64(expiry),
4445
}
4546

47+
err := validateRateTickRequest(req)
48+
if err != nil {
49+
return nil, err
50+
}
51+
4652
// If a rate tick hint is provided, return it as the rate tick.
4753
if req.RateTickHint != nil {
4854
rateTick.Rate = req.RateTickHint.Rate
@@ -58,6 +64,32 @@ func (p *mockRpcPriceOracleServer) QueryRateTick(_ context.Context,
5864
}, nil
5965
}
6066

67+
// validateRateTickRequest validates the given rate tick request.
68+
func validateRateTickRequest(req *priceoraclerpc.QueryRateTickRequest) error {
69+
var zeroAssetID [32]byte
70+
if req.SubjectAsset == nil {
71+
return fmt.Errorf("subject asset must be specified")
72+
}
73+
if len(req.SubjectAsset.GetAssetId()) != 32 {
74+
return fmt.Errorf("invalid subject asset ID length")
75+
}
76+
if bytes.Equal(req.SubjectAsset.GetAssetId(), zeroAssetID[:]) {
77+
return fmt.Errorf("subject asset ID must NOT be all zero")
78+
}
79+
80+
if req.PaymentAsset == nil {
81+
return fmt.Errorf("payment asset must be specified")
82+
}
83+
if len(req.PaymentAsset.GetAssetId()) != 32 {
84+
return fmt.Errorf("invalid payment asset ID length")
85+
}
86+
if !bytes.Equal(req.PaymentAsset.GetAssetId(), zeroAssetID[:]) {
87+
return fmt.Errorf("payment asset ID must be all zero")
88+
}
89+
90+
return nil
91+
}
92+
6193
// startBackendRPC starts the given RPC server and blocks until the server is
6294
// shut down.
6395
func startBackendRPC(grpcServer *grpc.Server) error {
@@ -95,7 +127,7 @@ func runQueryAskPriceTest(t *testing.T, tc *testCaseQueryAskPrice) {
95127
defer backendService.Stop()
96128

97129
// Wait for the server to start.
98-
time.Sleep(2 * time.Second)
130+
time.Sleep(200 * time.Millisecond)
99131

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

0 commit comments

Comments
 (0)