You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds a new debug HTTP endpoint, POST /debug/reset-scores, in both:
protocol/rpcsmartrouter/rpcsmartrouter.go
protocol/rpcconsumer/rpcconsumer.go
The endpoint clears optimizer score state immediately by calling ProviderOptimizer.ResetState() for every chain optimizer, without changing clock offset.
This is an additive capability. Existing /debug/time-warp and /debug/time behavior remains unchanged.
Validator / Provider impact
Validators: no impact (no consensus/state-machine changes).
Providers: no impact.
Consumers / Smart router: no impact unless debug server is explicitly enabled via --debug-address.
Add /debug/reset-scores endpoint for optimizer state reset
✨ Enhancement
Walkthroughs
Description
• Add POST /debug/reset-scores endpoint to clear optimizer scores
• Implement handler in both RPCConsumer and RPCSmartRouter packages
• Returns JSON with reset status and chain count
• Add comprehensive unit tests for new endpoint
Diagram
flowchart LR
A["POST /debug/reset-scores"] --> B["Method Guard"]
B --> C["Iterate Optimizers"]
C --> D["Call ResetState"]
D --> E["Return JSON Response"]
E --> F["chains_reset count"]
• Add POST /debug/reset-scores handler to buildDebugMux function
• Validate POST method only, reject other HTTP methods
• Iterate through all optimizers and call ResetState on each
• Return JSON response with reset status and chain count
• Update function documentation to mention new endpoint
• Add postResetScores helper function for test requests
• Add TestDebugResetScores_ReturnsJSON to verify successful response
• Add TestDebugResetScores_MethodNotAllowed to verify POST-only enforcement
• Add TestDebugResetScores_DoesNotChangeOffset to verify offset preservation
• Add POST /debug/reset-scores handler to buildDebugMux function
• Validate POST method only, reject other HTTP methods
• Iterate through all optimizers and call ResetState on each
• Return JSON response with reset status and chain count
• Update function documentation to mention new endpoint
View more (1) 4. protocol/rpcsmartrouter/debug_server_test.go
🧪 Tests +47/-0
Add unit tests for SmartRouter reset-scores endpoint
• Add postResetScoresRouter helper function for test requests
• Add TestDebugResetScores_SmartRouter_ReturnsJSON to verify response
• Add TestDebugResetScores_SmartRouter_MethodNotAllowed to verify POST-only
• Add TestDebugResetScores_SmartRouter_DoesNotChangeOffset to verify offset
The new POST /debug/reset-scores handler mutates optimizer state but accepts an empty POST with no
Content-Type/body/token validation, so it can be triggered by cross-origin browser requests (e.g., a
simple HTML form) whenever the debug server is enabled. This is a meaningful security footgun
compared to /debug/time-warp, which effectively requires JSON parsing and therefore is harder to
invoke without preflight-triggering headers.
+ // POST /debug/reset-scores — clears optimizer score state without changing+ // current time offset or NowFunc.+ mux.HandleFunc("/debug/reset-scores", func(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodPost {+ http.Error(w, "POST only", http.StatusMethodNotAllowed)+ return+ }+ count := 0+ optimizers.Range(func(chainID string, opt *provideroptimizer.ProviderOptimizer) bool {+ opt.ResetState()+ count+++ return true+ })+ w.Header().Set("Content-Type", "application/json")+ fmt.Fprintf(w, `{"reset":true,"chains_reset":%d}`, count)+ })
Evidence
In both buildDebugMux copies, /debug/reset-scores only checks the HTTP method and then immediately
calls ResetState() across optimizers; there is no check for a required header/token or JSON body,
which makes it trivially invokable by "simple" cross-origin POSTs. The new tests explicitly
demonstrate that a nil-body POST with no headers succeeds (200), confirming the endpoint is designed
to be callable this way.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
### Issue description
`POST /debug/reset-scores` is state-changing but currently accepts an empty POST with no header/body validation. This makes it easy to trigger from untrusted browser contexts (CSRF-style) whenever the debug server is enabled.
### Issue Context
Unlike `/debug/time-warp`, this endpoint does not require JSON decoding, so it can be invoked by a "simple" cross-origin POST (e.g., HTML form) without any special headers.
### Fix Focus Areas
- Add a required header token (e.g., `X-Debug-Token`) configured via flag/env, and return 401 if missing/invalid; **and/or**
- Require `Content-Type: application/json` and decode a trivial body (e.g., `{}`) so "simple" form POSTs fail.
- protocol/rpcconsumer/rpcconsumer.go[402-417]
- protocol/rpcsmartrouter/rpcsmartrouter.go[417-432]
- protocol/rpcconsumer/debug_server_test.go[31-37]
- protocol/rpcsmartrouter/debug_server_test.go[27-32]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
ⓘ The new review experience is currently in Beta. Learn more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #1565
PR: Add
POST /debug/reset-scoresDebug EndpointWhat this does
Adds a new debug HTTP endpoint,
POST /debug/reset-scores, in both:protocol/rpcsmartrouter/rpcsmartrouter.goprotocol/rpcconsumer/rpcconsumer.goThe endpoint clears optimizer score state immediately by calling
ProviderOptimizer.ResetState()for every chain optimizer, without changing clock offset.This is an additive capability. Existing
/debug/time-warpand/debug/timebehavior remains unchanged.Validator / Provider impact
--debug-address.Author Checklist
feat!is not needed (no breaking changes)mainChanges
protocol/rpcconsumer/rpcconsumer.gobuildDebugMux(...)with:POST /debug/reset-scoresoptimizers.Range(...)opt.ResetState()on each optimizer{"reset":true,"chains_reset":<N>}currentOffsetNanoNowFuncprotocol/rpcsmartrouter/rpcsmartrouter.goPOST /debug/reset-scoreshandler added inbuildDebugMux(...).rpcconsumer.protocol/rpcconsumer/debug_server_test.goTestDebugResetScores_ReturnsJSONTestDebugResetScores_MethodNotAllowedTestDebugResetScores_DoesNotChangeOffsetprotocol/rpcsmartrouter/debug_server_test.goTestDebugResetScores_SmartRouter_ReturnsJSONTestDebugResetScores_SmartRouter_MethodNotAllowedTestDebugResetScores_SmartRouter_DoesNotChangeOffsetWhy this is needed
Current test flow to clear optimizer scores often uses a full clock dance:
That flow works, but is multi-step and also changes effective time.
POST /debug/reset-scoresprovides a direct score reset operation for integration tests when time-shift semantics are not needed.This endpoint is not a replacement for clock injection; it is an additional debug tool.
API
Existing (unchanged)
Shift clock:
Read effective/real time:
New
Reset optimizer score state immediately:
Response example:
{"reset":true,"chains_reset":2}Method guard example:
GET /debug/reset-scores->405 POST onlyTests
Executed locally:
go test ./protocol/rpcconsumer/... ./protocol/rpcsmartrouter/...Result:
ok github.com/lavanet/lava/v5/protocol/rpcconsumerok github.com/lavanet/lava/v5/protocol/rpcsmartrouterProduction safety
--debug-addressis set.Example usage