Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: account impersonating improvements and eth_sendTransaction #137

Closed
wants to merge 4 commits into from

Conversation

AntonD3
Copy link
Collaborator

@AntonD3 AntonD3 commented Sep 27, 2023

What 💻

  • Using playground bootloader with eth_call flag for impersonating
  • The same logic implemented for the eth_estimateGas
  • eth_sendTransaction method implemented

Why ✋

  • It allows to avoid any account checks, and use any address for the impersonating
  • It will return the correct amount of gas, when impersonating is used
  • For now, it works only with impersonated accounts, it allows to reuse existent js tools, for example, hardhat signer

Evidence 📷

Screenshot 2023-09-27 at 14 59 27 Screenshot 2023-09-27 at 14 58 49

@AntonD3 AntonD3 changed the title Account impersonating improvements and eth_sendTransaction feat: account impersonating improvements and eth_sendTransaction Sep 27, 2023
@AntonD3 AntonD3 marked this pull request as ready for review September 27, 2023 13:00
@AntonD3 AntonD3 requested a review from a team as a code owner September 27, 2023 13:00
///
#[rpc]
pub trait EthTestNodeNamespaceT {
#[rpc(name = "eth_sendTransaction")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that this is implemented, can you enable the following e2e test? xdescribe -> describe

describe("hardhat_impersonateAccount & hardhat_stopImpersonatingAccount" ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Where I can find this test?

Copy link
Collaborator

Choose a reason for hiding this comment

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

At the bottom of the following file e2e-tests/test/hardhat-api.tests.ts

You can also search for the describe(...) I posted above in the repo and it'll show up

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see it, looks like my branch is outdated

/// # Returns
///
/// A future that resolves to the hash of the transaction if successful, or an error if the transaction is invalid or execution fails.
fn send_transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add unit tests for send_transaction

@MexicanAce
Copy link
Collaborator

Just as a general heads up, there's an open PR updating the meaning of current_miniblock, current_batch, current_timestamp #135

I'm guessing they'll be done first, but this will have slight conflicts with your changes (not much)

};

l2_tx.set_input(bytes, hash);
if hash != l2_tx.hash() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add a comment on when does this happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think never, this code was stolen from the send_raw_transaction method. Maybe I will remove it

if !reader
.impersonated_accounts
.contains(&l2_tx.common_data.initiator_address)
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we also print a clear error message, that this method is ONLY for impersonated accounts.
(and mention the initiator_address, and how many entries we have in impersonated_accounts list).

(as I'm worried that people will try using this method without knowing this)

);
// Using the eth call here to avoid all the account checks
// TODO: think about fictive blocks in case of impersonating via eth call
TxExecutionMode::EthCall
Copy link
Collaborator

Choose a reason for hiding this comment

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

the alternative, is to use the other DefaultAccount implementation in such case.

(as EthCall is in theory not supposed to be used for blocks that we commit)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but then the bootloader will do a check that the sender is an account and it's not in the kernel space

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah right, you actually want to have even more permissions...

The cool thing, is that we have a local copy of bootloader, so we can add some additional modes to do exactly that - and we can be explicit about it (instead of depending on a lucky side effect of EthCall..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, I was planning to do that with preprocessing, but for now that's just a quick solution


// If it was not eth call, we increase the batch by 1, but miniblock (and timestamp) by 2.
// You can look at insert_fictive_l2_block function in VM to see how this fake block is inserted.
if let TxExecutionMode::VerifyExecute = execution_mode {
Copy link
Collaborator

Choose a reason for hiding this comment

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

uh, let's avoid this... I'm worried that we'll get bootloader in a bad spot.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What exactly? Do you mean a missing fictive block?

};
// v = 27 corresponds to 0
let bytes = tx_req.get_signed_bytes(
&PackedEthSignature::from_rsv(&H256::default(), &H256::default(), 0),
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we use PackedEthSignature::default?

@AntonD3
Copy link
Collaborator Author

AntonD3 commented Oct 19, 2023

@MexicanAce these changes were implemented in the boojum-integration branch in a better way. Do you think we should duplicate it to the main right now, or it's fine if we will merge it later from the boojum branch?

@MexicanAce
Copy link
Collaborator

@AntonD3 I think we're okay to wait for the boojum-integration implementation. Please still implement the feedback in this PR (especially unit tests)

@MexicanAce MexicanAce closed this Oct 19, 2023
@dutterbutter dutterbutter deleted the ad-contract-impersonation branch February 25, 2024 23:28
IAvecilla added a commit to lambdaclass/era-test-node that referenced this pull request Feb 29, 2024
* Initial new implementation

* Fix compilation error

* Implementation of add operation for big integers (matter-labs#136)

* Implement Big UInt Left Shift (matter-labs#139)

* Add `bigUIntShl` implementation

* Add constants

* Fix compilation

* Implement Big UInt Right Shift (matter-labs#137)

* Add `bigUIntShr` implementation

* Add constants

* Implement Big UInt Bitwise Or for modexp (matter-labs#135)

* Implement bigUIntBitOr

* Fix bigUIntBitOr

* Fix bigUIntBitOr

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Fix missing closing brackets

---------

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Implement big uint conditional select for modexp (matter-labs#148)

* Implement bigUIntCondSelect

* Fix missing curly braces

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Implement Big UInt Right Shift (matter-labs#137)

* Add `bigUIntShr` implementation

* Add constants

* Implement Big UInt Bitwise Or for modexp (matter-labs#135)

* Implement bigUIntBitOr

* Fix bigUIntBitOr

* Fix bigUIntBitOr

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Fix missing closing brackets

---------

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

---------

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Implement mul operation for big UInts (matter-labs#151)

* First implementation of mul operation for bigints

* Fix multiplication for big integers

* Fix some merge issues

* Improve comments and function docs

* Delete whitespaces

* Substraction with borrow (matter-labs#149)

* First substraction draft

* Fix compile problems

* Working implementation

* Updated code

* Updated code

* Update subtract implementation

* Remove console_log

* Add docs for function

* Update function docs

* Remove tests from from ModExp.yul

* Fix typo

* Restore horrible whitespaces to avoid an ugly merge conflict

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Revert "Update precompiles/Modexp.yul"

This reverts commit 582bc41a0bb7fb02a1a68717fdf83c6fe432d422.

---------

Co-authored-by: Joaquín P. Centeno <jpcenteno@users.noreply.github.com>
Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Refactor `modexp` reimplementation (matter-labs#156)

* Make Big UInt API functions naming consistent

* Refactor `bigUIntAdd` variable names

* Refactor `bigUIntMul` variable names

* Refactor `subLimbsWithBorrow`

* Refactor `bigUintSubtractionWithBorrow`

* Refactor `bigUIntAdd`

* Fix `bigUIntSubWithBorrow`

* Format `storeLimbValueAtOffset`

* Refactor `bigUIntBitOr`

Made it consistent with the rest of the code convention and naming

* Refactor `bigUIntCondSelect`

Made it consistent with the rest of the code convention and naming

* Reorder `overflowingSubWithBorrow`

* Move comment to modexp API Docs section

* Biguint division (matter-labs#159)

* Division draft

* Non working draft

* Fix compile errors

* Use proper pointers for quotient and remainder

* Add fix note, some more changes

* Add comment

* Implement `big_uint_bit_size`

* Increase pointer to prevent it from steping over console_log

* WIP divrem

* Fix loop and zero initializer

* Push test cases

* Add other test case

* Add docs and tests for `big_uint_inplace_or_1`

* Fix bug related to bit shifting

* Fix borrow return in big uint sub function

* Delete playground file used for debugging

* Fix sub with borrow function

* Add playground again to check more big integer division tests

* Remove playground used for testing

* Write documentation for new shift functions

* Improve naming and documentation for new helper functions

* Rename bigUIntOrWith1 to bigUintInPlaceOrWith1

* Add tmp buffer parameters to bigUIntDivRem. Improve docs.

* Simplify subLimbsWithBorrow

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Remove `mul` call from `bigUIntInPlaceOrWith1`

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Remove multiplications from copyBigUint

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Optimize bigUIntBitSize loop

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Simplify zeroWithLimbSizeAt

---------

Co-authored-by: Francisco Krause Arnim <fkrausear@gmail.com>
Co-authored-by: IAvecilla <iavecilla@fi.uba.ar>
Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Implement mul mod operation for big UInts (matter-labs#161)

* Division draft

* Non working draft

* Fix compile errors

* Use proper pointers for quotient and remainder

* Add fix note, some more changes

* Add comment

* Implement `big_uint_bit_size`

* Increase pointer to prevent it from steping over console_log

* WIP divrem

* Fix loop and zero initializer

* Push test cases

* Add other test case

* Add docs and tests for `big_uint_inplace_or_1`

* Fix bug related to bit shifting

* Fix borrow return in big uint sub function

* Delete playground file used for debugging

* Fix sub with borrow function

* Add playground again to check more big integer division tests

* Remove playground used for testing

* Write documentation for new shift functions

* Improve naming and documentation for new helper functions

* Rename bigUIntOrWith1 to bigUintInPlaceOrWith1

* Add tmp buffer parameters to bigUIntDivRem. Improve docs.

* Add big uint mul mod skeleton

* Remove wrong comment

* Update algorithm comment

* Add limb size doubling and divide by two for mul mod operation

* Functions to duplicate and halve limb size work in place

* Use camelCase

* Remove console_log

* Add docs

* Update doc

---------

Co-authored-by: Francisco Krause Arnim <fkrausear@gmail.com>
Co-authored-by: Joaquín P. Centeno <jpcenteno@users.noreply.github.com>

* Add parseCallData function

* Add function to left-pad big uints

* Remove console log function

* Change left padding functions for big uints to not work in place

* Add `parseCalldata` function (matter-labs#168)

* Remove redundant parse call data declaration

* Free memory pointer (matter-labs#169)

* Add free memory pointer function

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Update precompiles/Modexp.yul

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

---------

Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>

* Start parsing the input calldata

* Correctly parse call data

* Add left pad steps for modexp inputs

* Add pad if needed function

* Modexp for big UInts skeleton (matter-labs#164)

* WIP: modexp skeleton

* Use of mul mod function for big integers

* imlement aux function to check if big uint is larger than 1

* minor fix

* Restore modexp from target branch

* Fix mul mod

* Finish modexp implementation

* Remove playground used for debugging

* Update modexp with final state of modular exponentiation function

* Fix merge issue

* Change all names to camel case

* fix typo

Co-authored-by: Francisco Krause Arnim <56402156+fkrause98@users.noreply.github.com>

---------

Co-authored-by: IAvecilla <iavecilla@fi.uba.ar>
Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>
Co-authored-by: Francisco Krause Arnim <56402156+fkrause98@users.noreply.github.com>

* Add simple integration

* Fix calldata buffer in zero check

* Uncomment checks for base cases

* Fix result length to match with mod length

* Fix condition in parse call data

* Update test assertions with new test node updates

* Add comment for tests with a temp patch

* Fix modexp result length

* Fix limb amount for modexp operands

* Clean sratch buffers in each iteration

* Clean sratch buffers for every operation

* Remove unused functions

* Delete free memory pointer usage and calculate pointers manually

* Replace all mul operations for shifts to improve gas usage

* Include basic optimizations

* Add optimizations for reminder calculations

* Add small improvement for main loop in modular exp

* Add temporary fix for modexp test

* Add modex reference script

* Remove unnecesary memory stores

* Reduce iterations in rem function

* Compilation fix

* Print gas used on tests

* Add build script to create gas reports

* Save gas used for each test of the precompiles

* Add aux functions to write lines in each report

* Merge main

* Fix tests lint

* Fix lint in test utils

* Change L1 url

---------

Co-authored-by: Nacho Avecilla <nachoavecilla@gmail.com>
Co-authored-by: Joaquín Centeno <jpcenteno@users.noreply.github.com>
Co-authored-by: Francisco Krause Arnim <56402156+fkrause98@users.noreply.github.com>
Co-authored-by: Francisco Krause Arnim <fkrausear@gmail.com>
Co-authored-by: IAvecilla <iavecilla@fi.uba.ar>
Co-authored-by: Javier Chatruc <jrchatruc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants