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

fix: Compare local nonce w/ backend to detect invalidated cache #153

Merged
merged 2 commits into from
Aug 22, 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
5 changes: 5 additions & 0 deletions server/redisstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func (s *RedisState) GetSenderMaxNonce(txFrom string) (senderMaxNonce uint64, fo
return senderMaxNonce, true, nil
}

func (s *RedisState) DelSenderMaxNonce(txFrom string) error {
key := RedisKeySenderMaxNonce(txFrom)
return s.RedisClient.Del(context.Background(), key).Err()
}

// Block transactions, with a specific return value (eg. "nonce too low")
func (s *RedisState) SetBlockedTxHash(txHash string, returnValue string) error {
key := RedisKeyBlockedTxHash(txHash)
Expand Down
40 changes: 37 additions & 3 deletions server/request_intercepts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package server

import (
"encoding/json"
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/flashbots/rpc-endpoint/types"
)

Expand Down Expand Up @@ -179,7 +182,9 @@ func (r *RpcRequest) intercept_signed_eth_getTransactionCount() (requestFinished
return false
}

nonce, found, err := RState.GetSenderMaxNonce(addr)
// since it's possible that the user sent another tx via another provider, we need to check the nonce from
// both the backend and our cache, and return the greater of the two
cachedNonce, found, err := RState.GetSenderMaxNonce(addr)
if err != nil {
r.logger.Error("[eth_getTransactionCount] Redis:GetSenderMaxNonce error", "error", err)
return false
Expand All @@ -188,9 +193,38 @@ func (r *RpcRequest) intercept_signed_eth_getTransactionCount() (requestFinished
r.logger.Info("[eth_getTransactionCount] No nonce found")
return false
}
if !r.proxyRequestRead() {
r.logger.Info("[ProcessRequest] Proxy to node failed", "method", r.jsonReq.Method)
r.writeRpcError("internal server error", types.JsonRpcInternalError)
return true
}
r.logger.Info("[eth_getTransactionCount] intercept", "cachedNonce", cachedNonce, "addr", addr)

backendTxCount := uint64(0)
if r.jsonRes.Result != nil {
count := hexutil.Uint64(0)
if err := json.Unmarshal(r.jsonRes.Result, &count); err != nil {
r.logger.Info("[ProcessRequest] Unmarshal backend response failed", "method", r.jsonReq.Method)
r.writeRpcError("internal server error", types.JsonRpcInternalError)
return true
}
backendTxCount = uint64(count)
r.logger.Info("[eth_getTransactionCount] intercept", "backendTxCount", backendTxCount, "addr", addr)
}

// return either the cached nonce plus one, or the backend tx count
txCount := cachedNonce + 1
if backendTxCount > txCount {
txCount = backendTxCount
// since the cached value is invalid lets remove it from redis
r.logger.Info("[eth_getTransactionCount] intercept invalidated nonce", "addr", addr)
if err := RState.DelSenderMaxNonce(addr); err != nil {
// log the error but continue
r.logger.Error("[eth_getTransactionCount] Redis:DelSenderMaxNonce error", "error", err, "addr", addr)
}
}

r.logger.Info("[eth_getTransactionCount] intercept", "nonce", nonce)
resp := fmt.Sprintf("0x%x", nonce+1)
resp := fmt.Sprintf("0x%x", txCount)
r.writeRpcResult(resp)
return true
}
Loading