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

implement insufficient balance state #313

Conversation

DreamWuGit
Copy link
Collaborator

@DreamWuGit DreamWuGit commented Nov 16, 2022

circuit PR https://github.com/privacy-scaling-explorations/zkevm-circuits/pull/919, this error execution is different from others(OOG, stack error etc.) we have done, when this error happens, the result was pushed into stack and execution will consume instead of restore to caller's context.

@DreamWuGit DreamWuGit marked this pull request as ready for review November 17, 2022 07:27
Copy link
Contributor

@silathdiir silathdiir 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 ed255 linked an issue Nov 18, 2022 that may be closed by this pull request
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.

Thanks for the work and the PR!

I have a couple of concerns regarding the incompleteness of the opcodes to support (which might cause significant changes later in the spec and possibly even bigger in the implementation) and also in the tests.

We should find a common ground where we have enough things speced and at the same time doesn't imply a titanic work now and we can leaverage some stuff to the future.

What I guess I want to say is that is "OK" to not include CREATE and CREATE2 maybe (although I'll do it). But at least the design of the specs should already consider all of them. As for example, we will need a selection method for the OPCODE such a lagrange polynomial that only allows 1 of the OPCODES to be selected.


### circuit behavior
1. pop 7 stack elements, even though the other six elements not closely relevant to this error
state constraints, but in order to be accordance with evm trace, also need to handle them here for stack pointer
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
state constraints, but in order to be accordance with evm trace, also need to handle them here for stack pointer
state constraints, but in order to be in accordance with evm trace, we also need to handle them here for stack pointer

### circuit behavior
1. pop 7 stack elements, even though the other six elements not closely relevant to this error
state constraints, but in order to be accordance with evm trace, also need to handle them here for stack pointer
transition, which impact next step's stack status.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
transition, which impact next step's stack status.
transition, which impacts the next step's stack status.

Comment on lines +9 to +12
# TODO: add Create / Create2 in the future
instruction.constrain_in(opcode, [FQ(Opcode.CALL), FQ(Opcode.CALLCODE)])
# TODO: for create/create2 have different stack from Call, will handle it in the future
# Lookup values from stack
Copy link
Member

Choose a reason for hiding this comment

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

IMO, we should already build the lagrange polynomial now that we will use as a selector for the different possible opcodes.
Also, I think we should also add the switch between the opcode executions and leave with todo!() the ones that don't matter for now.

This would be the bear minimum to me. Because otherwise when we implement stuff into zkevm-circuits the code will be significantly worse as there are a lot of things we could abstract and simplify for the 4 cases that we're probably not be able to do with the 2 cases now and the 2 later in the future.

# if is_success_rlc value is zero then decode RLC should also be zero
instruction.constrain_zero(is_success_rlc)

value = instruction.rlc_to_fq(value_rlc, 31)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of 31, please use

value = instruction.rlc_to_fq(value_rlc, 31)
current_address = instruction.call_context_lookup(CallContextFieldTag.CalleeAddress)
caller_balance_rlc = instruction.account_read(current_address, AccountFieldTag.Balance)
caller_balance = instruction.rlc_to_fq(caller_balance_rlc, 31)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

caller_balance_rlc = instruction.account_read(current_address, AccountFieldTag.Balance)
caller_balance = instruction.rlc_to_fq(caller_balance_rlc, 31)
# compare value and balance
insufficient_balance, _ = instruction.compare(caller_balance, value, 31)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

rw_counter=Transition.delta(10),
program_counter=Transition.delta(1),
stack_pointer=Transition.delta(6),
# TODO: handle gas_left
Copy link
Member

Choose a reason for hiding this comment

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

I think we should add the gas_left at least for the Opcodes we're implementing in this PR. Otherwise, this is a slightly incomplete PR, considering we don't support the 4 cases neither.



@pytest.mark.parametrize("balance, transfer_value", TESTING_DATA)
def test_insufficient_balance(balance: int, transfer_value: int):
Copy link
Member

Choose a reason for hiding this comment

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

We should have test cases for both CALL and CALLCODE.

rw_counter=Transition.delta(10),
program_counter=Transition.delta(1),
stack_pointer=Transition.delta(6),
# TODO: handle gas_left
Copy link
Member

Choose a reason for hiding this comment

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

We should at least handle CALL and CALLCODE use cases for the gas_left. And consider the others for what the design refers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@CPerezz thanks for reviewing, yes, I am considering refactoring this merging into CallOp as it shares more codes with it , will update spec after circuit refactoring done.

@aguzmant103
Copy link
Member

Hey @DreamWuGit just looking at older PRs. Are you still working on this one?

@DreamWuGit
Copy link
Collaborator Author

close this , as it is merged into call op code from circuit side, will open new spec PR to match it.

@DreamWuGit DreamWuGit closed this Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

handle ErrorInsufficientBalance state
4 participants