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

runtime-sdk/modules/evm: Add support for simulating CREATE #1669

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

kostko
Copy link
Member

@kostko kostko commented Mar 26, 2024

Story: oasisprotocol/oasis-web3-gateway#543 (comment)

Problem:

Performing an eth_call to simulate contract creation is currently not possible.

This is used by hardhat-ignition (a deterministic deployment framework) to determine if creating the contract will be possible.

In oasis web3 gateway the EstimateGas function calls either evm.Create or evm.Call depending on if the to field is empty. I initially made a workaround in oasis-web3-gateway by setting the to field to the zero address, however this will erroneously succeed (as calls to unknown addresses succeed so transfers can be made to previously unknown EOAs). So while that silences the error in hardhat-ignition the behavior is incorrect.

For the correct behavior the evm.SimulateCall function would be modified to handle contract creation with EVM semantics (no to address = create contract & run init code). The evm.SimulateCall function would make the address parameter optional. This would then have to be invoked with a signed query to faithfully replicate EVM semantics.

The initial workaround (setting to=0 ) is broken and shouldn't be used.

Example RPC call performed by hardhat-ignition :

{
  jsonrpc: '2.0',
  method: 'eth_call',
  params: [
    {
      value: '0x0',
      data: '0x608060405234801561001057600080fd5b506101bc806100206000396000f3fe60806040526004361061001e5760003560e01c806323b872dd14610023575b600080fd5b6100366100313660046100e5565b610038565b005b604080516001600160a01b03858116602483015284166044820152606480820184905282518083039091018152608490910182526020810180516001600160e01b03166000356001600160e01b03191617905290517f998729e624ef59107d5781f8c021b04a5d7b5153dd0ef79e7a08a9f8ab40a5a3916100bc9133913491610121565b60405180910390a1505050565b80356001600160a01b03811681146100e057600080fd5b919050565b6000806000606084860312156100fa57600080fd5b610103846100c9565b9250610111602085016100c9565b9150604084013590509250925092565b60018060a01b038416815260006020848184015260606040840152835180606085015260005b8181101561016357858101830151858201608001528201610147565b506000608082860101526080601f19601f8301168501019250505094935050505056fea2646970667358221220fd6667e915f896f3ce18c5cb6d6753a873674e2380a080ec28379c887a0cc56564736f6c63430008120033',
      from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
      nonce: '0x0',
      gas: '0x24dd3',
      gasPrice: '0x174876e800'
    },
    'pending'
  ],
  id: 12
}

Suggested modification to SimulateCallQuery struct:

/// Transaction body for simulating an EVM call.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct SimulateCallQuery {
    pub gas_price: U256,
    pub gas_limit: u64,
    pub caller: H160,
    #[cbor(optional)]
    pub address: H160,
    pub value: U256,
    pub data: Vec<u8>,
}

Suggested modification in oasis-web3-gateway/rpc/eth/api.go :

    if args.Data != nil {
		input = *args.Data
	}
	if args.To == nil {
		if len(input) > 0 {
			// When simulating a contract deploy the To address may not be specified
			args.To = nil
		} else {
			return []byte{}, errors.New("to address not specified")
		}
	}

Suggested modification in runtime-sdk/modules/evm/src/lib.rs :

  • Add logic from evm.Create tofn simulate_call
    • Or introduce a separate evm.SimulateCreate which can be invoked from oasis-web3-gateway instead of evm.Call

This PR

... solves this.

@kostko kostko requested a review from CedarMist March 26, 2024 12:17
Copy link

netlify bot commented Mar 26, 2024

Deploy Preview for oasisprotocol-oasis-sdk canceled.

Name Link
🔨 Latest commit 44eaec5
🔍 Latest deploy log https://app.netlify.com/sites/oasisprotocol-oasis-sdk/deploys/6602c8ec643ec00008dbfecb

Copy link

codecov bot commented Mar 26, 2024

Codecov Report

Attention: Patch coverage is 81.03448% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 65.33%. Comparing base (da60d8d) to head (44eaec5).

Files Patch % Lines
runtime-sdk/modules/evm/src/lib.rs 76.59% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1669      +/-   ##
==========================================
- Coverage   65.34%   65.33%   -0.01%     
==========================================
  Files         114      114              
  Lines        8365     8380      +15     
==========================================
+ Hits         5466     5475       +9     
- Misses       2899     2905       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kostko kostko force-pushed the kostko/feature/evm-simulate-create branch from fb2417a to da8c76e Compare March 26, 2024 12:28
@kostko kostko force-pushed the kostko/feature/evm-simulate-create branch from da8c76e to c9b9a9d Compare March 26, 2024 12:46
@kostko kostko force-pushed the kostko/feature/evm-simulate-create branch from c9b9a9d to 44eaec5 Compare March 26, 2024 13:08
Copy link
Member

@CedarMist CedarMist left a comment

Choose a reason for hiding this comment

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

It seems errors & reversions aren't being handled when simulating a contract creation via eth_call.

Oh, my bad, need to update oasis-web3-gateway to specify nil as the to field.

@CedarMist CedarMist dismissed their stale review March 27, 2024 11:41

I need to test against updated oasis-web3-gateway

Copy link
Member

@CedarMist CedarMist left a comment

Choose a reason for hiding this comment

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

@kostko kostko merged commit a9c70cb into main Mar 27, 2024
29 checks passed
@kostko kostko deleted the kostko/feature/evm-simulate-create branch March 27, 2024 14:37
github-actions bot added a commit that referenced this pull request Mar 27, 2024
…ostko/feature/evm-simulate-create

runtime-sdk/modules/evm: Add support for simulating CREATE a9c70cb
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.

3 participants