Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Modularize mock crate for easier and more customizable testing setup generation #349

Merged
merged 34 commits into from
Mar 30, 2022

Conversation

CPerezz
Copy link
Contributor

@CPerezz CPerezz commented Feb 24, 2022

Adds a Builder pattern for constructing almost any test case and context that is required.

This is an example on how to use it:

let code_a = bytecode! {
    PUSH1(0x0) // retLength
    PUSH1(0x0) // retOffset
    PUSH1(0x0) // argsLength
    PUSH1(0x0) // argsOffset
    PUSH32(Word::from(0x1000)) // value
    PUSH32(*WORD_ADDR_B) // addr
    PUSH32(0x1_0000) // gas
    CALL

    PUSH2(0xaa)
};
let code_b = bytecode! {
    PUSH1(0x01) // value
    PUSH1(0x02) // key
    SSTORE

    PUSH3(0xbb)
};

// Get the execution steps from the external tracer
let block: GethData = TestContext::<3, 2>::new(
    None,
    |accs| {
        accs[0]
            .address(address!("0x0000000000000000000000000000000000000000"))
            .code(code_a);
        accs[1]
            .address(address!("0x000000000000000000000000000000000cafe001"))
            .code(code_b);
        accs[2]
            .address(address!("0x000000000000000000000000000000000cafe002"))
            .balance(Word::from(1u64 << 30));
        accs
    },
    |mut txs, accs| {
        txs[0].to(accs[0].address).from(accs[2].address);
        txs[1]
            .to(accs[1].address)
            .from(accs[2].address)
            .nonce(Word::one());
    },
    |block, _tx| block.number(0xcafeu64),
)
.unwrap()
.into();

Here we can see that works with const-generics and we need to specify: NACC (Number of accounts) and NTX (Number of transactions).

TODO:

Resolves: #348

@github-actions github-actions bot added crate-bus-mapping Issues related to the bus-mapping workspace member T-opcode Type: opcode-related and focused PR/Issue crate-zkevm-circuits Issues related to the zkevm-circuits workspace member labels Feb 25, 2022
@CPerezz
Copy link
Contributor Author

CPerezz commented Feb 28, 2022

This will remain untouched until #312 gets merged so that no duplicated work/conflicts increase.

@CPerezz CPerezz marked this pull request as ready for review March 25, 2022 01:28
CPerezz added 2 commits March 28, 2022 16:05
Instead of requiering `Bytecode` as the type for the code passed to
MockAccount constructor, now it accepts anything that implements
`Into<Bytes>` which is much more ergonomic and generic.
@CPerezz CPerezz requested review from ed255 and han0110 March 28, 2022 15:09
@CPerezz
Copy link
Contributor Author

CPerezz commented Mar 28, 2022

@ed255 it's ready for the final review! :)

Copy link
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The builder pattern makes everything easier to understand! Only got some nitpicks.

Copy link
Member

@ed255 ed255 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've completed my review! I've left a few notes and comments, please take a look :)

@CPerezz CPerezz requested review from han0110 and ed255 March 30, 2022 10:29
Copy link
Member

@ed255 ed255 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Awesome work!

Copy link
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@CPerezz CPerezz merged commit 9cf0e56 into main Mar 30, 2022
@CPerezz CPerezz deleted the modularize_mock branch March 30, 2022 11:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
crate-bus-mapping Issues related to the bus-mapping workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member T-opcode Type: opcode-related and focused PR/Issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modularize mock structures generation to improve trace customisation
3 participants