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

fix: Parity state diff when creating SC with balance #172

Merged

Conversation

ZzPoLariszZ
Copy link
Contributor

Problem

The inconsistent problem in analysing the Parity state difference of a transaction
which includes creating a smart contract that already has a balance.

Reproducible Example

With the latest version reth v1.0.3, using trace_replayTransaction() and stateDiff to trace
the transaction 0x40926f14808e7217827e8ff9492d15d68d84edae76f71db5ab8c70a5664f4720

curl http://localhost:8545 \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{"method":"trace_replayTransaction","params":["0x40926f14808e7217827e8ff9492d15d68d84edae76f71db5ab8c70a5664f4720",["stateDiff"]],"id":1,"jsonrpc":"2.0"}'  

The state difference of the newly created smart contract 0xc2d517963c616ee780e833a5318413242a790f08

From Reth

"0xc2d517963c616ee780e833a5318413242a790f08": {
        "balance": {
          "+": "0x0"
        },
        "code": {
          "+": "0x363d3d373d3d3d363d73059ffafdc6ef594230de44f824e2bd0a51ca5ded5af43d82803e903d91602b57fd5bf3"
        },
        "nonce": {
          "+": "0x1"
        },
        "storage": {
          "0x0000000000000000000000000000000000000000000000000000000000000000": {
            "+": "0x000000000000000000000000a733309058d5eefaf9162e10d50efd44472105f3"
          }
        }
      },

From Etherscan and QuickNode

"0xc2d517963c616ee780e833a5318413242a790f08": {
        "balance": {
          "*": {
            "from": "0xc7831f1bdb4400",
            "to": "0x0"
          }
        },
        "code": {
          "*": {
            "from": "0x",
            "to": "0x363d3d373d3d3d363d73059ffafdc6ef594230de44f824e2bd0a51ca5ded5af43d82803e903d91602b57fd5bf3"
          }
        },
        "nonce": {
          "*": {
            "from": "0x0",
            "to": "0x1"
          }
        },
        "storage": {
          "0x0000000000000000000000000000000000000000000000000000000000000000": {
            "*": {
              "from": "0x0000000000000000000000000000000000000000000000000000000000000000",
              "to": "0x000000000000000000000000a733309058d5eefaf9162e10d50efd44472105f3"
            }
          }
        }
      },

Reason

The smart contract 0xc2d517963c616ee780e833a5318413242a790f08 is created in Tx#1 in Block 15,305,803;
however, it received around 0.056 ETH in Tx#0 in Block 15,305,797. Therefore, it has a balance when it is created.

Solution

Additionally check whether the account has a balance or not when it is created during the transaction.

Copy link
Contributor

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

ty!

only have doc nits

@@ -501,7 +502,13 @@ where
}
} else {
// account may exist or not, we need to fetch the account from the db
Copy link
Contributor

Choose a reason for hiding this comment

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

this doc is no longer relevant if we always reload it in L487, can be removed or moved to L487 and rephrased

Comment on lines 486 to 487
// we check if this account was created during the transaction
if changed_acc.is_created() {
let db_acc = db.basic_ref(addr)?.unwrap_or_default();
Copy link
Contributor

Choose a reason for hiding this comment

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

swap lines

@ZzPoLariszZ ZzPoLariszZ requested a review from mattsse July 29, 2024 09:51
@mattsse mattsse merged commit 2ef6dc1 into paradigmxyz:main Jul 29, 2024
11 checks passed
@ZzPoLariszZ ZzPoLariszZ deleted the fix-state-diff-create-sc-with-balance branch August 1, 2024 14:29
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.

2 participants