0xSpaceShard / starknet-devnet-rs

A local testnet for Starknet... in Rust
https://0xspaceshard.github.io/starknet-devnet-rs/
MIT License
104 stars 61 forks source link

`flush` will load messages from previous tests #602

Open pscott opened 1 week ago

pscott commented 1 week ago

I've explained the situation in the README.md of this example repo: https://github.com/pscott/flush_example .

But basically, flush will load messages from L1 even if we have reset L2 in between. This leads to harder testing, as one must either re-deploy contracts, or load L1 from a dump file (currently not possible using Anvil, as it's not a dump on request but a dump on exit).

FabijanC commented 1 week ago

Unable to replicate the issue: https://github.com/pscott/flush_example/issues/1

FabijanC commented 2 days ago

I was not able to run your JS test, but I wrote something very similar in Rust, based on Devnet's existing messaging tests and it seems I am observing the same problem. Will continue debugging tomorrow.

Just for when you are back, I am confused by this comment in your StarknetCommit.ts file:

    // Checking that the L1 -> L2 message has been propagated. Should be `1` message but due to how `flush` will look at ALL
    // the logs of the previous messages, it will return `2` messages.
    // Due to the fact that the "authenticator" from the previous test has not been deployed on the current network, the "flush"
    // will error with "0x..." is not deployed.

How can it at the same time fail with the mentioned error, and have a length of 2 instead of 1? I.e. if the flush fails, doesn't it exit and prevent the array length assertion from happening?

FabijanC commented 1 day ago

Bug found

The problem is that Devnet's L1-L2 messaging mechanism is not well integrated with the feature of restarting. Flushing works by fetching messages from L1 blocks in the range : from_block - to_block. But from_block is kept as a property in Devnet's memory and to_block is the latest chain block. So what happens is that on a restart request, from_block is restarted together with the rest of the state, leading to the re-sending of the older messages.

Code

https://github.com/0xSpaceShard/starknet-devnet-rs/blob/4d321a3982ce483a9787a34045944e56ce67c6e4/crates/starknet-devnet-core/src/messaging/ethereum.rs#L160-L167

FabijanC commented 1 day ago

Look what I just found: https://github.com/0xSpaceShard/starknet-devnet-rs/blob/4d321a3982ce483a9787a34045944e56ce67c6e4/crates/starknet-devnet-core/src/messaging/ethereum.rs#L81-L83