Skip to content

Use precise cache duration for "future beacon" error responses#22

Open
alienx5499 wants to merge 1 commit intodrand:masterfrom
alienx5499:fix/precise-future-beacon-cache
Open

Use precise cache duration for "future beacon" error responses#22
alienx5499 wants to merge 1 commit intodrand:masterfrom
alienx5499:fix/precise-future-beacon-cache

Conversation

@alienx5499
Copy link
Contributor

Problem

When a client requests a round that is too far in the future, the relay returns a "Requested future beacon" (425 Too Early) response. The response was cached for the full chain period (info.Period), even when the next valid round would become available sooner. That over-caches the error and can serve a stale "future beacon" response longer than necessary.

Location: routes_handler.gogetBeacon() when round >= nextRound+1

Previous behavior: Cache duration was always -int64(info.Period) (full period in seconds).

Impact:

  • Suboptimal caching: error responses cached for the whole period instead of until the next round
  • Clients may see the "future beacon" error longer than needed
  • A TODO in code noted this improvement

Solution

Compute the exact time until the next round and use that as the cache duration. Round N is at GenesisTime + (N-1)*Period; seconds from now until that time is used for the negative nextTime (cache duration) when returning the future-beacon error.

  • Added SecondsUntilRound(round uint64) int64 on JsonInfoV2 in grpc/grpc.go: returns seconds from now until the given round, or 0 if the round is in the past or round is 0.
  • In getBeacon(), when round >= nextRound+1, use info.SecondsUntilRound(nextRound) instead of info.Period for the cache duration.
  • Removed the TODO about using a more precise nextTime.

Changes

grpc/grpc.go

  • New method SecondsUntilRound(round uint64) int64 on JsonInfoV2: computes round time as GenesisTime + (round-1)*Period, returns max(0, roundTime - now).

routes_handler.go

  • In the future-beacon branch: set preciseCacheSeconds := info.SecondsUntilRound(nextRound) and return -preciseCacheSeconds instead of -int64(info.Period).
  • Removed the TODO comment.

grpc/grpc_test.go

  • New test TestSecondsUntilRound: next round in 5s, round 0 returns 0, round already passed returns 0, round in 30s.

Testing

  • go test ./grpc/... -v -run TestSecondsUntilRound
  • go test ./grpc/... -v -run TestNextBeaconTime (unchanged)
  • go build ./...

Notes

  • Cache duration is now at most the time until the next round; it can be less than the full period when the next round is soon.
  • No change to success paths or to when the "future beacon" error is returned; only the cache duration for that error is improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant