From 0e0018f95f238b98c9a88b4f01af6d1be23e99d0 Mon Sep 17 00:00:00 2001
From: evgeniy-scherbina <evgeniy.shcherbina.es@gmail.com>
Date: Wed, 11 Oct 2023 09:56:58 -0400
Subject: [PATCH] CR's fixes

---
 service/middleware.go | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/service/middleware.go b/service/middleware.go
index b77f2f1..df88d70 100644
--- a/service/middleware.go
+++ b/service/middleware.go
@@ -16,6 +16,7 @@ import (
 	"github.com/kava-labs/kava-proxy-service/config"
 	"github.com/kava-labs/kava-proxy-service/decode"
 	"github.com/kava-labs/kava-proxy-service/logging"
+	"github.com/kava-labs/kava-proxy-service/service/cachemdw"
 )
 
 const (
@@ -228,8 +229,11 @@ func createProxyRequestMiddleware(next http.Handler, config config.Config, servi
 				serviceLogger.Trace().Msg("request body is empty, skipping before request interceptors")
 			}
 
-			// proxy request to backend origin servers
-			proxy.ServeHTTP(lrw, r)
+			// Only proxy the request if it's not cached
+			isCached := cachemdw.IsRequestCached(r.Context())
+			if !isCached {
+				proxy.ServeHTTP(lrw, r)
+			}
 
 			serviceLogger.Trace().Msg(fmt.Sprintf("response %+v \nheaders %+v \nstatus %+v for request %+v", lrw.Status(), lrw.Header(), lrw.body, r))
 
@@ -383,6 +387,7 @@ func createAfterProxyFinalizer(service *ProxyService, config config.Config) http
 		}
 
 		var blockNumber *int64
+		// TODO: Redundant ExtractBlockNumberFromEVMRPCRequest call here if request is cached
 		rawBlockNumber, err := decodedRequestBody.ExtractBlockNumberFromEVMRPCRequest(r.Context(), service.evmClient)
 
 		if err != nil {