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

Circuit for opcode COINBASE #259

Merged
merged 27 commits into from
Jan 14, 2022

Conversation

scroll-dev
Copy link
Collaborator

The PR includes the following features:

  • Add block table & lookup
  • Coinbase opcode circuit
  • Add tests

The spec can be found at privacy-scaling-explorations/zkevm-specs#83

@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 Jan 3, 2022
@DreamWuGit
Copy link
Collaborator

@han0110 Could you please have a look ?

bus-mapping/src/eth_types.rs Outdated Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/witness.rs Outdated Show resolved Hide resolved
Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

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

Aside from @han0110's comment on the Lookup of previous blocks by hash and the panic question, the rest looks good to me!

Nice job!!! 😄

bus-mapping/src/eth_types.rs Outdated Show resolved Hide resolved
) {
self.add_lookup(Lookup::Block {
field_tag: tag,
number: number.unwrap_or_else(|| 0.expr()),
Copy link
Member

Choose a reason for hiding this comment

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

If the block comes without a number provided shouldn't we panic directly? As it means that some deep internal logic must be wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

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

if the block construct not initialize explicitly with a number, the default value will set to zero, so will not panic directly, does it make sense?

Copy link
Member

Choose a reason for hiding this comment

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

But 0 would probbaly not be the correct value no? So we will be passing a wrong block to the circuit condtructor IIUC.

Copy link
Collaborator

Choose a reason for hiding this comment

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

the block constants include block number will be pub input of circuit eventually as planned, so it will be easy to figure out if it is incorrect .

Copy link
Member

Choose a reason for hiding this comment

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

Yes, what I meant is that if you check the code, if there's any unexpected error/behavior and we receive a None/Err, putting a 0 will not be correct.

Therefore the question of paniking.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, that is it , at first I used number parameter type Expression directly without Option wrapper, but considering maybe zero in cases , change to Option as now. what is your ideas ?

Copy link
Member

@CPerezz CPerezz Jan 11, 2022

Choose a reason for hiding this comment

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

To me it looks like number should not be an Option or Result and instead a pure value directly.
In that case, it's impossible to construct a non-valid number and you don't need to unwrap_or_zero it.

Maybe @han0110 has some insight on that. I'm just wondering that this cannot be the source of errors in the future. If it cannot, we can simply resolve the conversation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO it's just semantic difference, since the block_table now is constructed as it is, the field number will always be 0 for all fields except history_hashes. So it would not be source of errors, but just might not look so straightforward.

Another approach is to split this into 2 function w/o number as input, to make it less ambiguous.

Copy link
Member

@CPerezz CPerezz Jan 11, 2022

Choose a reason for hiding this comment

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

Then you can proceed as you wish @DreamWuGit !!

state_ref.push_stack_op(
RW::WRITE,
StackAddress::from(1024 - 1),
block.ctants.coinbase.to_word(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I want to refactor to use BlockConstants's coinbase field here and remove ChainConstants coinbase field, not sure why exists in chain constant of bussmapping before, want to hear from you guys first @ChihChengLiang @han0110 @CPerezz

Copy link
Member

Choose a reason for hiding this comment

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

Maybe @ed255 can provide some insight on the bus-mapping related stuff.
From my perspective, the coinbase will always be the same. And therefore is only passed once via ChainConstants instead of passing it on each tx/block.

Copy link
Collaborator

Choose a reason for hiding this comment

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

coinbase is only block's field not related to tx, each block can have different coinbase address, chainid is global which make sense exists in ChainConstants

Copy link
Member

Choose a reason for hiding this comment

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

I originally put the coinbase in ChainConstants. I had the wrong idea of what the coinbase was, so it was a mistake. coinbase should be removed from ChainConstants.

The coinbase that you need here I think should be block.eth_block.author. See documentation here https://docs.rs/ethers/latest/ethers/core/types/struct.Block.html#structfield.author
Block miner, author, coinbase are all the same thing.

The struct BlockConstants defined in external_tracer has a name which is a bit misleading. That struct is really a configuration parameter to setup an environment for the external tracer.

Copy link
Member

Choose a reason for hiding this comment

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

Ohh my bad...

I understood coinbase on a different way...

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks @ed255 , I will try remove coinbase in ChainConstants at this PR!

Copy link
Collaborator

Choose a reason for hiding this comment

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

fixed in 21ad52f

// converting to block context
let context = BlockContext {
coinbase: b.constants.coinbase,
..Default::default()
Copy link
Member

Choose a reason for hiding this comment

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

Here there should be a TODO: bus_mapping::circuit_input_builder::Block should contain all the fields required in the BlockContext:

    pub coinbase: Address, // u160
    pub gas_limit: u64,
    pub block_number: F,
    pub time: u64,
    pub difficulty: Word,
    pub base_fee: Word,
    pub previous_block_hashes: Vec<Word>,

Copy link
Member

Choose a reason for hiding this comment

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

I can take care of that if you want! Just let me know and I'll open the issue and address it myself.

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed to use BlockConstant which holds all fields now.

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

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

LGTM!!

@ed255
Copy link
Member

ed255 commented Jan 13, 2022

@DreamWuGit it seems the branch has conflicts with main.

@DreamWuGit
Copy link
Collaborator

it seems the branch has conflicts with main.
yes, will rebase to main

@DreamWuGit
Copy link
Collaborator

it seems the branch has conflicts with main.
yes, will rebase to main

rebased in 723912d

@CPerezz
Copy link
Member

CPerezz commented Jan 14, 2022

Tests are failing @DreamWuGit

Maybe was introduced some inconistency in tbhe rebase?

@DreamWuGit
Copy link
Collaborator

Maybe was introduced some inconistency in tbhe rebase?

I am looking into this :)

@han0110
Copy link
Collaborator

han0110 commented Jan 14, 2022

Need 3 fixes of the doc in crate bus-mapping

https://github.com/appliedzkp/zkevm-circuits/blob/40cc46fd044de225a07c381d1df255bc5dad1190/bus-mapping/src/lib.rs#L55-L58

+ //! use bus_mapping::external_tracer::BlockConstants;

https://github.com/appliedzkp/zkevm-circuits/blob/40cc46fd044de225a07c381d1df255bc5dad1190/bus-mapping/src/lib.rs#L109-L112

- //!     coinbase: Address::zero(),

https://github.com/appliedzkp/zkevm-circuits/blob/40cc46fd044de225a07c381d1df255bc5dad1190/bus-mapping/src/lib.rs#L120-L121

- //! let mut builder =
- //!     CircuitInputBuilder::new(sdb, CodeDB::new(), &eth_block, ctants);
+ //! let mut builder = CircuitInputBuilder::new(
+ //!     sdb,
+ //!     CodeDB::new(),
+ //!     &eth_block,
+ //!     ctants.clone(),
+ //!     BlockConstants::from_eth_block(
+ //!         &eth_block,
+ //!         &Word::from(ctants.chain_id),
+ //!     ),
+ //! );

@DreamWuGit
Copy link
Collaborator

DreamWuGit commented Jan 14, 2022

thanks @han0110 and @CPerezz ! should be fixed by 776bc42, just be curious that why need commented code snippet in bus-mapping/src/lib.rs. can we remove them ?

@CPerezz
Copy link
Member

CPerezz commented Jan 14, 2022

thanks @han0110 and @CPerezz ! should be fixed by 776bc42, just be curious that why need commented code snippet in bus-mapping/src/lib.rs. can we remove them ?

Is an example of usage. We have it there so that is easier to see which are the purposes and the recommended way to use the bus-mapping.

Since it's an example we can also compiled on each test run and notice regressions in API or any other similar error introduced.

I'm not against removing it. But IMO it is useful.

@DreamWuGit
Copy link
Collaborator

understood, THX!

@CPerezz CPerezz merged commit 1c1a89b into privacy-scaling-explorations:main Jan 14, 2022
@CPerezz CPerezz mentioned this pull request Jan 18, 2022
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.

8 participants