foundry-rs / foundry-fork-db

Apache License 2.0
28 stars 9 forks source link

Use eth_getAccount if provided by the endpoint #9

Open mattsse opened 1 month ago

mattsse commented 1 month ago

eth_getaccount is very useful because it sends us the nonce+balance+codehash in one response:

https://www.quicknode.com/docs/ethereum/eth_getAccount

but this is not supported by all endpoints so we need to check if this is available and cache that info.

if support we can optimize this via a single eth_getAccount call, and a followup eth_getCode if the code_hash is not KECCAK_EMPTY

https://github.com/foundry-rs/alloy-fork-db/blob/cbd6df29d515dfbab9ce48c1bdb37a3e592c62d0/src/backend.rs#L215-L221

see also alloy provider fn https://github.com/alloy-rs/alloy/blob/067cc464bd1357e745653709db051707b949cc6b/crates/provider/src/provider/trait.rs#L242-L249

nkysg commented 1 month ago

How check if endpoints supported eth_getAccount?

mattsse commented 1 month ago

we need to add a new variable that tracks this and when we fetch the first account we can try fetching it with eth_getAccount if that succeeds we know the endpoint supports it and can keep using it otherwise we use what we currently use (3 rpc calls).

something like

enum GetAccountMode {
  Unknown,
  EthGetAccount,
  AccountCodeNonce
}