Repository navigation
Replies: 1 comment 1 reply
|
What about instead using an approach where we:
|
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Motivation
User-facing JSON-RPC activity can make the validator fetch account state from the
external/base-layer RPC. In a public or high-traffic deployment, a single user or
a small set of users can therefore amplify inbound RPC traffic into outbound
pressure on the configured external RPC provider.
The validator needs a per-user protection mechanism that limits this outbound
fetch pressure without throttling unrelated validator work, local-only RPCs, or
background maintenance flows.
The important unit is not "JSON-RPC requests" and not "requested pubkeys". The
unit we need to bound is direct outbound external RPC request count caused by
user-triggered fetch paths.
Requirements
Scope
Limit user-triggered account fetch paths when they cause the validator to issue
direct outbound requests to the external/base-layer RPC.
Covered paths include:
getAccountInfo,getMultipleAccounts,getBalance,getDelegationStatus, andgetTokenAccountBalance;sendTransaction;snapshots for
simulateTransaction;Principal
The limiter must rate-limit by a user principal. Supported principal sources can
include:
If no reliable principal is available, requests fall into a stricter shared
unknown/anonymous bucket.If IP headers such as
X-Forwarded-FororForwardedare used, they must onlybe trusted behind a configured trusted proxy boundary. They must not be trusted
when supplied directly by arbitrary clients.
Key and quota unit
The first implementation uses a generic per-principal key:
Method-specific or operation-class-specific quotas are not required initially.
Method or operation labels may still be carried for bounded metrics/debugging,
but they are not part of the initial limiter key.
Each direct outbound external RPC request consumes one limiter event:
getMultipleAccountsrequest counts as one event, even if itcontains many pubkeys;
once;
outbound external RPC request, it is not double-charged.
Enforcement point
Enforcement must happen where the actual outbound request count is known: near
the Chainlink /
RemoteAccountProviderdirect outbound fetch boundary.Aperture can parse JSON-RPC methods and extract identity,
but it cannot know the final outbound request count because
that depends on local bank checks, pending-operation
deduplication, batching, chunking, subscription races, and
direct fetch retries.
Therefore an Aperture-only limiter is insufficient for this MIMD's exact quota
unit.
Error behavior
When the limiter denies a user-triggered direct external fetch request, the
validator should return a clear rate-limit error rather than silently falling
back to stale or local-only behavior.
For
sendTransactionandsimulateTransaction, rate-limit denial during accountavailability checks must fail before the transaction or simulation enters the
scheduler.
Non-goals
The first implementation does not need:
Companion/internal follow-up fetches include delegation-record fetches,
program-data fetches, ATA/eATA projection dependencies, and post-delegation
action dependencies. These are deliberately outside the initial user quota.
Considered Options
Option 1: External service in front of the validator
Put a gateway or reverse proxy in front of Aperture. The gateway identifies the
user and applies limits before forwarding JSON-RPC requests to the validator.
Aperture could later report actual direct-fetch counts back in response headers
if Chainlink records them.
Pros
Cons
count.
request from causing remote RPC work.
bypass the gateway.
Verdict: useful future outer protection, but not sufficient as the core
solution for exact direct outbound request-count limiting.
Option 2: External service between validator and external RPCs
Put an outbound RPC proxy between Chainlink and the configured base-layer RPC.
Chainlink passes principal/context headers for direct user-triggered fetches; the
proxy counts and limits actual outbound requests.
Pros
Cons
Verdict: attractive if operators already want a shared outbound RPC
governance layer, but too much infrastructure for the initial per-node
requirement.
Option 3: Internal validator limiter at the direct fetch boundary
Implement a local in-process limiter owned by the validator. Aperture extracts
the principal and passes bounded context into the user-triggered account
ensure/fetch paths. Chainlink /
RemoteAccountProviderchecks the limiter onceper direct outbound external RPC request or chunk, immediately before issuing
that request.
Pros
fetches to the user quota.
Cons
infrastructure.
Verdict: chosen.
Chosen Solution
Use Option 3: an internal per-node limiter enforced at the Chainlink /
RemoteAccountProviderdirect outbound fetch boundary.This is the only option that satisfies all first-pass requirements without
adding external infrastructure. It counts what we actually care about, rejects
before outbound work is performed, and fits the local per-validator-node state
assumption.
The implementation should keep separation of concerns by introducing a small
FetchRateLimiterservice with a narrow API:Aperture remains responsible for extracting the user principal. Chainlink /
RemoteAccountProviderremains responsible for deciding when an actual directoutbound external RPC request is about to be issued and checking the limiter at
that point.
Approach
flowchart TD A[User JSON-RPC request] --> B[Aperture extracts IP/API key/user principal] B --> C[User-triggered account read / transaction / simulation path] C --> D[Carry bounded RateLimitContext into ensure/fetch flow] D --> E[Local bank checks, undelegating checks, pending-fetch dedup] E --> F{Will this path issue a direct outbound external RPC request?} F -- No: bank hit / joined in-flight / no remote request --> G[Continue without charging limiter] F -- Yes: direct external request or chunk --> H[Check local keyed limiter for principal] H -- Allowed --> I[Send one external RPC request] H -- Denied --> J[Return clear rate-limit error before external RPC] I --> K[Continue normal fetch/ensure result handling] K --> L[RPC response or transaction/simulation scheduling after availability is resolved] M[Companion, background, subscription, local-only work] --> N[Do not charge user limiter]High-level steps:
mode, sustained rate, burst capacity, trusted proxy behavior, and stricter
unknown-principal behavior.
RateLimitContextthrough user-triggered accountensure/fetch paths.
direct outbound request will be submitted, check the limiter for cost
1.made.
local-only work.
Library Choice:
governorUse
governoras the default local rate-limit library.governoris the best fit because:it is a mature, widely used Rust rate-limiting crate;
it supports keyed in-memory limiters directly;
keys can be compact typed values rather than formatted strings;
its GCRA algorithm gives smooth average-rate enforcement with bounded bursts;
non-blocking checks such as
check_keyfit a reject-before-fetch design;check_key_nremains available if a future policy needs weighted costs;keyed-state cleanup via
retain_recentaddresses unbounded principalcardinality;
ecosystem wrappers exist for Tower/Axum/Actix, so choosing the core crate does
not block future HTTP-layer defenses.
Crates.io: https://crates.io/crates/governor
Docs: https://docs.rs/governor
Current version found:
0.10.4Algorithm: GCRA, functionally equivalent to leaky bucket.
Storage: direct/unkeyed and keyed in-memory state; keyed store can use
DashMap; supports cleanup viaretain_recent.Alternatives considered:
tokio-rate-limit: promising high-performance keyed/weighted limiter, butnewer and less battle-tested than
governor.throttlecrab: flexible low-level GCRA with TTL/store options, but moremanual and less established.
mailrs-rate-limit: simple string-keyed token bucket, but less obviouslysuited to a hot internal fetch path.
the sole solution because they limit inbound HTTP requests, not actual outbound
remote fetches.
add network failure modes to a latency-sensitive path.
Observability
The limiter should emit bounded metrics and logs for:
ip,api_key), not the principal value;Metrics and logs must not use pubkeys, signatures, raw IPs, raw API keys,
endpoint URLs, raw errors, or arbitrary user-controlled values as labels.
Validation
Behavioral tests should cover:
sendTransactionandsimulateTransactionfailing before scheduling whenaccount availability is blocked by the limiter;
Future Extensions
The design should leave room for, but not implement initially:
All reactions