foundry-rs / foundry-rs/foundry

anvil --init genesis.json incompatible with --load-state state

Open
#6,679 5 comments 0 reactions 0 assignees View on GitHub
C-anvil T-feature T-to-investigate
Dominant language
Rust
Stars
10.6k
Forks
2.6k
Avg merge
18h 20m
Merged PRs (30d)
510

Description

### Component

Anvil

### Describe the feature you would like

I need to setup deploy some contracts and setup some state, save it in a state json file, and then load it later. However, importantly, some state depends on the block-number at which the interaction happened, so when I restart the anvil node with `--load-state` option, I need the chain to restart at the same (or greater) block-number, but the `anvil --load-state ...` restarts the chain at block 0.

One way to restart the chain at a specific block number is to setup a genesis.json file and start anvil with `anvil --init genesis.json`. However, when trying to use both with `anvil --init ... --load-state ...` I get
```
error: the argument '--init ' cannot be used with '--load-state '
```

Is there a reason why the state file being dumped is not just a genesis.json file with default parameters, instead of some subset of it which uses a separate load flag? Would be nice if they were merged, since it would give us a lot more control on the initial configuration.

### Additional context

I tried using jq commands to copy the contents of the state file into the `alloc` section, however the state file looks like
```
"0x0165878a594ca255338adfa4d48449f69242eb8f": {
"nonce": 1,
"balance": "0x0",
"code": "0x...",
"storage": {
"0x0": "0x1",
"0x33": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"0x65": "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512",
"0x66": "0x1e",
"0x97": "0x0",
"0x9a": "0x0",
"0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc": "0xdcd1bf9a1b36ce34237eeafef220932846bcd82",
"0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103": "0x5fbdb2315678afecb367f032d93f642f64180aa3"
}
}
```
and when I put it in the genesis.json I get
```
error: invalid value 'genesis.json' for '--init ': failed to parse json file: "genesis.json": invalid length 1, expected a (both 0x-prefixed or not) hex string or byte array containing 32 bytes at line 18 column 21
```

Seems like I'd need to parse all of the "storage" elements and fill them to 32 byte long (both key and value)... this feels like a bug in anvil.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.