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 api cache #2562

Merged
merged 3 commits into from
May 9, 2024
Merged
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
45 changes: 35 additions & 10 deletions services/rfq/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gin-gonic/gin"
"github.com/jellydator/ttlcache/v3"
"github.com/synapsecns/sanguine/core/metrics"
baseServer "github.com/synapsecns/sanguine/core/server"
omniClient "github.com/synapsecns/sanguine/services/omnirpc/client"
Expand All @@ -35,6 +36,7 @@
omnirpcClient omniClient.RPCClient
handler metrics.Handler
fastBridgeContracts map[uint32]*fastbridge.FastBridge
roleCache map[uint32]*ttlcache.Cache[string, bool]
}

// NewAPI holds the configuration, database connection, gin engine, RPC client, metrics handler, and fast bridge contracts.
Expand Down Expand Up @@ -62,6 +64,7 @@
docs.SwaggerInfo.Title = "RFQ Quoter API"

bridges := make(map[uint32]*fastbridge.FastBridge)
roles := make(map[uint32]*ttlcache.Cache[string, bool])
for chainID, bridge := range cfg.Bridges {
chainClient, err := omniRPCClient.GetChainClient(ctx, int(chainID))
if err != nil {
Expand All @@ -71,6 +74,18 @@
if err != nil {
return nil, fmt.Errorf("could not create bridge contract: %w", err)
}

// create the roles cache
roles[chainID] = ttlcache.New[string, bool](
ttlcache.WithTTL[string, bool](cacheInterval),
)
roleCache := roles[chainID]

roleCache.Start()
go func() {
<-ctx.Done()
roleCache.Stop()
}()
}

return &QuoterAPIServer{
Expand All @@ -79,12 +94,14 @@
omnirpcClient: omniRPCClient,
handler: handler,
fastBridgeContracts: bridges,
roleCache: roles,
}, nil
}

// QuoteRoute is the API endpoint for handling quote related requests.
const (
QuoteRoute = "/quotes"
QuoteRoute = "/quotes"
cacheInterval = time.Minute
)

var logger = log.Logger("rfq-api")
Expand Down Expand Up @@ -145,15 +162,23 @@
return
}

has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain"})
c.Abort()
return
} else if !has {
c.JSON(http.StatusBadRequest, gin.H{"msg": "q.Relayer not an on-chain relayer"})
c.Abort()
return
hasRole := r.roleCache[uint32(req.DestChainID)].Get(addressRecovered.Hex())

if hasRole == nil || hasRole.IsExpired() {
has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
if err == nil {
r.roleCache[uint32(req.DestChainID)].Set(addressRecovered.Hex(), has, cacheInterval)
}

if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain"})
c.Abort()
return

Check warning on line 176 in services/rfq/api/rest/server.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/api/rest/server.go#L174-L176

Added lines #L174 - L176 were not covered by tests
} else if !has {
c.JSON(http.StatusBadRequest, gin.H{"msg": "q.Relayer not an on-chain relayer"})
c.Abort()
return
}
Comment on lines +165 to +181
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor the role checking logic in AuthMiddleware to reduce complexity.

-		hasRole := r.roleCache[uint32(req.DestChainID)].Get(addressRecovered.Hex())
-		if hasRole == nil || hasRole.IsExpired() {
-			has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
-			if err == nil {
-				r.roleCache[uint32(req.DestChainID)].Set(addressRecovered.Hex(), has, cacheInterval)
-			}
-			if err != nil {
-				c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain"})
-				c.Abort()
-				return
-			} else if !has {
-				c.JSON(http.StatusBadRequest, gin.H{"msg": "q.Relayer not an on-chain relayer"})
-				c.Abort()
-				return
-			}
-		}
+		// Simplified role checking logic
+		addressHex := addressRecovered.Hex()
+		hasRole, found := r.roleCache[uint32(req.DestChainID)].Get(addressHex)
+		if !found || hasRole.IsExpired() {
+			has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
+			if err != nil {
+				c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain: " + err.Error()})
+				c.Abort()
+				return
+			}
+			r.roleCache[uint32(req.DestChainID)].Set(addressHex, has, cacheInterval)
+			if !has {
+				c.JSON(http.StatusBadRequest, gin.H{"msg": "Relayer not authorized on-chain"})
+				c.Abort()
+				return
+			}
+		}

This refactoring simplifies the logic by consolidating error handling and reducing the depth of conditional nesting.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
hasRole := r.roleCache[uint32(req.DestChainID)].Get(addressRecovered.Hex())
if hasRole == nil || hasRole.IsExpired() {
has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
if err == nil {
r.roleCache[uint32(req.DestChainID)].Set(addressRecovered.Hex(), has, cacheInterval)
}
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain"})
c.Abort()
return
} else if !has {
c.JSON(http.StatusBadRequest, gin.H{"msg": "q.Relayer not an on-chain relayer"})
c.Abort()
return
}
// Simplified role checking logic
addressHex := addressRecovered.Hex()
hasRole, found := r.roleCache[uint32(req.DestChainID)].Get(addressHex)
if !found || hasRole.IsExpired() {
has, err := bridge.HasRole(ops, relayerRole, addressRecovered)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "unable to check relayer role on-chain: " + err.Error()})
c.Abort()
return
}
r.roleCache[uint32(req.DestChainID)].Set(addressHex, has, cacheInterval)
if !has {
c.JSON(http.StatusBadRequest, gin.H{"msg": "Relayer not authorized on-chain"})
c.Abort()
return
}
}

}

// Log and pass to the next middleware if authentication succeeds
Expand Down
Loading