-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat(rfq-api): filter old quotes #2958
Changes from all commits
399fd60
0cc05e3
a7fd85e
327825d
6ed3c5d
bf8b67b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
Bridges map[uint32]string `yaml:"bridges"` | ||
Port string `yaml:"port"` | ||
RelayAckTimeout time.Duration `yaml:"relay_ack_timeout"` | ||
MaxQuoteAge time.Duration `yaml:"max_quote_age"` | ||
} | ||
|
||
const defaultRelayAckTimeout = 30 * time.Second | ||
|
@@ -37,6 +38,16 @@ | |
return c.RelayAckTimeout | ||
} | ||
|
||
const defaultMaxQuoteAge = 1 * time.Hour | ||
|
||
// GetMaxQuoteAge returns the max quote age. | ||
func (c Config) GetMaxQuoteAge() time.Duration { | ||
if c.MaxQuoteAge == 0 { | ||
return defaultMaxQuoteAge | ||
} | ||
return c.MaxQuoteAge | ||
} | ||
Comment on lines
+43
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check: Consider adding validation to ensure |
||
|
||
// LoadConfig loads the config from the given path. | ||
func LoadConfig(path string) (config Config, err error) { | ||
input, err := os.ReadFile(filepath.Clean(path)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/synapsecns/sanguine/services/rfq/api/config" | ||
"github.com/synapsecns/sanguine/services/rfq/api/db" | ||
) | ||
|
||
// FilterQuoteAge exports filterQuoteAge for testing. | ||
func FilterQuoteAge(cfg config.Config, dbQuotes []*db.Quote) []*db.Quote { | ||
return filterQuoteAge(cfg, dbQuotes) | ||
} | ||
Comment on lines
+8
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider adding a comment to clarify that this function is intended for testing purposes only.
Comment on lines
+1
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for This file is exporting the Do you want me to generate the unit testing code or open a GitHub issue to track this task? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ package rest | |
|
||
import ( | ||
"fmt" | ||
"github.com/synapsecns/sanguine/services/rfq/api/config" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Consider the performance impact of importing the |
||
|
||
"github.com/synapsecns/sanguine/services/rfq/api/config" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/shopspring/decimal" | ||
|
@@ -210,6 +212,9 @@ func (h *Handler) GetQuotes(c *gin.Context) { | |
} | ||
} | ||
|
||
// Filter quotes | ||
dbQuotes = filterQuoteAge(h.cfg, dbQuotes) | ||
|
||
Comment on lines
+215
to
+217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Ensure |
||
// Convert quotes from db model to api model | ||
quotes := make([]*model.GetQuoteResponse, len(dbQuotes)) | ||
for i, dbQuote := range dbQuotes { | ||
|
@@ -236,3 +241,17 @@ func (h *Handler) GetContracts(c *gin.Context) { | |
} | ||
c.JSON(http.StatusOK, model.GetContractsResponse{Contracts: contracts}) | ||
} | ||
|
||
func filterQuoteAge(cfg config.Config, dbQuotes []*db.Quote) []*db.Quote { | ||
maxAge := cfg.GetMaxQuoteAge() | ||
|
||
thresh := time.Now().Add(-maxAge) | ||
var filteredQuotes []*db.Quote | ||
for _, quote := range dbQuotes { | ||
if quote.UpdatedAt.After(thresh) { | ||
filteredQuotes = append(filteredQuotes, quote) | ||
} | ||
} | ||
|
||
return filteredQuotes | ||
} | ||
Comment on lines
+245
to
+257
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider edge cases where |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,15 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
apiClient "github.com/synapsecns/sanguine/services/rfq/api/client" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
apiClient "github.com/synapsecns/sanguine/services/rfq/api/client" | ||
"github.com/synapsecns/sanguine/services/rfq/api/db" | ||
"github.com/synapsecns/sanguine/services/rfq/api/rest" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/synapsecns/sanguine/ethergo/signer/wallet" | ||
|
@@ -205,6 +208,22 @@ func (c *ServerSuite) TestPutAndGetQuoteByRelayer() { | |
c.Assert().True(found, "Newly added quote not found") | ||
} | ||
|
||
func (c *ServerSuite) TestFilterQuoteAge() { | ||
now := time.Now() | ||
|
||
// insert quote outside age range | ||
quotes := []*db.Quote{ | ||
{OriginChainID: 1, UpdatedAt: now.Add(-time.Hour)}, | ||
{OriginChainID: 2, UpdatedAt: now.Add(-time.Minute)}, | ||
Comment on lines
+216
to
+217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Ensure the |
||
} | ||
|
||
filteredQuotes := rest.FilterQuoteAge(c.cfg, quotes) | ||
|
||
// verify old quote is filtered out | ||
c.Equal(1, len(filteredQuotes)) | ||
c.Equal(quotes[1], filteredQuotes[0]) | ||
} | ||
|
||
func (c *ServerSuite) TestPutAck() { | ||
c.startQuoterAPIServer() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"math/big" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Flaque/filet" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
|
@@ -77,7 +78,8 @@ func (c *ServerSuite) SetupTest() { | |
1: ethFastBridgeAddress.Hex(), | ||
42161: arbFastBridgeAddress.Hex(), | ||
}, | ||
Port: fmt.Sprintf("%d", port), | ||
Port: fmt.Sprintf("%d", port), | ||
MaxQuoteAge: 15 * time.Minute, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Ensure that the MaxQuoteAge parameter is consistently used across all relevant parts of the codebase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Verify that the filtering mechanism for old quotes does not introduce performance bottlenecks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Ensure that the MaxQuoteAge parameter is consistently used across all relevant parts of the codebase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Verify that the filtering mechanism for old quotes does not introduce performance bottlenecks. |
||
} | ||
c.cfg = testConfig | ||
|
||
|
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.
style: Ensure
MaxQuoteAge
is correctly set in all deployment environments to avoid unexpected behavior.