Skip to content

Commit

Permalink
evm: Reject contract creation if the storage is non-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Apr 16, 2024
1 parent 7a941c8 commit af6d73e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 34 deletions.
30 changes: 15 additions & 15 deletions newBlockchainTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,8 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ RevertDepthCreate2OOGBerlin.json OK
+ RevertDepthCreateAddressCollision.json OK
+ RevertDepthCreateAddressCollisionBerlin.json OK
+ RevertInCreateInInitCreate2.json OK
+ RevertInCreateInInitCreate2Paris.json OK
RevertInCreateInInitCreate2.json Skip
RevertInCreateInInitCreate2Paris.json Skip
+ RevertOpcodeCreate.json OK
+ RevertOpcodeInCreateReturnsCreate2.json OK
+ call_outsize_then_create2_successful_then_returndatasize.json OK
Expand All @@ -1203,8 +1203,8 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ create2collisionSelfdestructed2.json OK
+ create2collisionSelfdestructedOOG.json OK
+ create2collisionSelfdestructedRevert.json OK
+ create2collisionStorage.json OK
+ create2collisionStorageParis.json OK
create2collisionStorage.json Skip
create2collisionStorageParis.json Skip
+ create2noCash.json OK
+ returndatacopy_0_0_following_successful_create.json OK
+ returndatacopy_afterFailing_create.json OK
Expand All @@ -1213,7 +1213,7 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ returndatacopy_following_successful_create.json OK
+ returndatasize_following_successful_create.json OK
```
OK: 53/54 Fail: 0/54 Skip: 1/54
OK: 49/54 Fail: 0/54 Skip: 5/54
## stCreateTest
```diff
+ CREATE2_CallData.json OK
Expand Down Expand Up @@ -1508,8 +1508,8 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ codeCopyZero.json OK
+ codeCopyZero_Paris.json OK
+ createEmptyThenExtcodehash.json OK
+ dynamicAccountOverwriteEmpty.json OK
+ dynamicAccountOverwriteEmpty_Paris.json OK
dynamicAccountOverwriteEmpty.json Skip
dynamicAccountOverwriteEmpty_Paris.json Skip
+ extCodeCopyBounds.json OK
+ extCodeHashAccountWithoutCode.json OK
+ extCodeHashCALL.json OK
Expand Down Expand Up @@ -1543,7 +1543,7 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ extcodehashEmpty.json OK
+ extcodehashEmpty_Paris.json OK
```
OK: 39/39 Fail: 0/39 Skip: 0/39
OK: 37/39 Fail: 0/39 Skip: 2/39
## stHomesteadSpecific
```diff
+ contractCreationOOGdontLeaveEmptyContract.json OK
Expand Down Expand Up @@ -2573,8 +2573,8 @@ OK: 41/41 Fail: 0/41 Skip: 0/41
+ RevertDepthCreateAddressCollision.json OK
+ RevertDepthCreateOOG.json OK
+ RevertInCallCode.json OK
+ RevertInCreateInInit.json OK
+ RevertInCreateInInit_Paris.json OK
RevertInCreateInInit.json Skip
RevertInCreateInInit_Paris.json Skip
+ RevertInDelegateCall.json OK
+ RevertInStaticCall.json OK
+ RevertOnEmptyStack.json OK
Expand Down Expand Up @@ -2620,17 +2620,17 @@ OK: 41/41 Fail: 0/41 Skip: 0/41
+ costRevert.json OK
+ stateRevert.json OK
```
OK: 52/57 Fail: 0/57 Skip: 5/57
OK: 50/57 Fail: 0/57 Skip: 7/57
## stSLoadTest
```diff
+ sloadGasCost.json OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## stSStoreTest
```diff
+ InitCollision.json OK
InitCollision.json Skip
+ InitCollisionNonZeroNonce.json OK
+ InitCollisionParis.json OK
InitCollisionParis.json Skip
+ SstoreCallToSelfSubRefundBelowZero.json OK
+ sstoreGas.json OK
+ sstore_0to0.json OK
Expand Down Expand Up @@ -2658,7 +2658,7 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ sstore_changeFromExternalCallInInitCode.json OK
+ sstore_gasLeft.json OK
```
OK: 29/29 Fail: 0/29 Skip: 0/29
OK: 27/29 Fail: 0/29 Skip: 2/29
## stSelfBalance
```diff
+ diffPlaces.json OK
Expand Down Expand Up @@ -3718,4 +3718,4 @@ OK: 11/11 Fail: 0/11 Skip: 0/11
OK: 1/1 Fail: 0/1 Skip: 0/1

---TOTAL---
OK: 3162/3268 Fail: 0/3268 Skip: 106/3268
OK: 3152/3268 Fail: 0/3268 Skip: 116/3268
30 changes: 15 additions & 15 deletions newGeneralStateTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ RevertDepthCreate2OOGBerlin.json OK
+ RevertDepthCreateAddressCollision.json OK
+ RevertDepthCreateAddressCollisionBerlin.json OK
+ RevertInCreateInInitCreate2.json OK
+ RevertInCreateInInitCreate2Paris.json OK
RevertInCreateInInitCreate2.json Skip
RevertInCreateInInitCreate2Paris.json Skip
+ RevertOpcodeCreate.json OK
+ RevertOpcodeInCreateReturnsCreate2.json OK
+ call_outsize_then_create2_successful_then_returndatasize.json OK
Expand All @@ -637,8 +637,8 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ create2collisionSelfdestructed2.json OK
+ create2collisionSelfdestructedOOG.json OK
+ create2collisionSelfdestructedRevert.json OK
+ create2collisionStorage.json OK
+ create2collisionStorageParis.json OK
create2collisionStorage.json Skip
create2collisionStorageParis.json Skip
+ create2noCash.json OK
+ returndatacopy_0_0_following_successful_create.json OK
+ returndatacopy_afterFailing_create.json OK
Expand All @@ -647,7 +647,7 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ returndatacopy_following_successful_create.json OK
+ returndatasize_following_successful_create.json OK
```
OK: 53/54 Fail: 0/54 Skip: 1/54
OK: 49/54 Fail: 0/54 Skip: 5/54
## stCreateTest
```diff
+ CREATE2_CallData.json OK
Expand Down Expand Up @@ -942,8 +942,8 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ codeCopyZero.json OK
+ codeCopyZero_Paris.json OK
+ createEmptyThenExtcodehash.json OK
+ dynamicAccountOverwriteEmpty.json OK
+ dynamicAccountOverwriteEmpty_Paris.json OK
dynamicAccountOverwriteEmpty.json Skip
dynamicAccountOverwriteEmpty_Paris.json Skip
+ extCodeCopyBounds.json OK
+ extCodeHashAccountWithoutCode.json OK
+ extCodeHashCALL.json OK
Expand Down Expand Up @@ -977,7 +977,7 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ extcodehashEmpty.json OK
+ extcodehashEmpty_Paris.json OK
```
OK: 39/39 Fail: 0/39 Skip: 0/39
OK: 37/39 Fail: 0/39 Skip: 2/39
## stHomesteadSpecific
```diff
+ contractCreationOOGdontLeaveEmptyContract.json OK
Expand Down Expand Up @@ -2007,8 +2007,8 @@ OK: 41/41 Fail: 0/41 Skip: 0/41
+ RevertDepthCreateAddressCollision.json OK
+ RevertDepthCreateOOG.json OK
+ RevertInCallCode.json OK
+ RevertInCreateInInit.json OK
+ RevertInCreateInInit_Paris.json OK
RevertInCreateInInit.json Skip
RevertInCreateInInit_Paris.json Skip
+ RevertInDelegateCall.json OK
+ RevertInStaticCall.json OK
+ RevertOnEmptyStack.json OK
Expand Down Expand Up @@ -2054,17 +2054,17 @@ OK: 41/41 Fail: 0/41 Skip: 0/41
+ costRevert.json OK
+ stateRevert.json OK
```
OK: 52/57 Fail: 0/57 Skip: 5/57
OK: 50/57 Fail: 0/57 Skip: 7/57
## stSLoadTest
```diff
+ sloadGasCost.json OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## stSStoreTest
```diff
+ InitCollision.json OK
InitCollision.json Skip
+ InitCollisionNonZeroNonce.json OK
+ InitCollisionParis.json OK
InitCollisionParis.json Skip
+ SstoreCallToSelfSubRefundBelowZero.json OK
+ sstoreGas.json OK
+ sstore_0to0.json OK
Expand Down Expand Up @@ -2092,7 +2092,7 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ sstore_changeFromExternalCallInInitCode.json OK
+ sstore_gasLeft.json OK
```
OK: 29/29 Fail: 0/29 Skip: 0/29
OK: 27/29 Fail: 0/29 Skip: 2/29
## stSelfBalance
```diff
+ diffPlaces.json OK
Expand Down Expand Up @@ -3152,4 +3152,4 @@ OK: 11/11 Fail: 0/11 Skip: 0/11
OK: 1/1 Fail: 0/1 Skip: 0/1

---TOTAL---
OK: 2719/2822 Fail: 0/2822 Skip: 103/2822
OK: 2709/2822 Fail: 0/2822 Skip: 113/2822
4 changes: 3 additions & 1 deletion nimbus/db/ledger/accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ proc contractCollision*(ac: AccountsCache, address: EthAddress): bool {.inline.}
let acc = ac.getAccount(address, false)
if acc.isNil:
return
acc.account.nonce != 0 or acc.account.codeHash != EMPTY_SHA3
acc.account.nonce != 0 or
acc.account.codeHash != EMPTY_SHA3 or
acc.account.storageRoot != EMPTY_ROOT_HASH

proc accountExists*(ac: AccountsCache, address: EthAddress): bool {.inline.} =
let acc = ac.getAccount(address, false)
Expand Down
4 changes: 3 additions & 1 deletion nimbus/db/ledger/accounts_ledger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ proc contractCollision*(ac: AccountsLedgerRef, address: EthAddress): bool =
let acc = ac.getAccount(address, false)
if acc.isNil:
return
acc.statement.nonce != 0 or acc.statement.codeHash != EMPTY_SHA3
acc.statement.nonce != 0 or
acc.statement.codeHash != EMPTY_SHA3 or
acc.statement.stoTrie.rootHash.isOk # not EMPTY_ROOT_HASH

proc accountExists*(ac: AccountsLedgerRef, address: EthAddress): bool =
let acc = ac.getAccount(address, false)
Expand Down
4 changes: 3 additions & 1 deletion nimbus/db/state_db/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ proc getCode*(db: AccountStateDB, address: EthAddress): seq[byte] =
triedb.get(contractHashKey(db.getCodeHash(address)).toOpenArray)

proc contractCollision*(db: AccountStateDB, address: EthAddress): bool {.inline.} =
db.getNonce(address) != 0 or db.getCodeHash(address) != EMPTY_SHA3
db.getNonce(address) != 0 or
db.getCodeHash(address) != EMPTY_SHA3 or
db.getStorageRoot(address) != EMPTY_ROOT_HASH

proc dumpAccount*(db: AccountStateDB, addressS: string): string =
let address = addressS.parseAddress
Expand Down
21 changes: 20 additions & 1 deletion tests/test_allowed_to_fail.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http:https://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http:https://opensource.org/licenses/MIT)
Expand Down Expand Up @@ -92,6 +92,20 @@ func slowGSTTests(folder: string, name: string): bool =

]

const
EIP7610_Problematic = [
"dynamicAccountOverwriteEmpty.json",
"dynamicAccountOverwriteEmpty_Paris.json",
"RevertInCreateInInit.json",
"RevertInCreateInInit_Paris.json",
"InitCollision.json",
"InitCollisionParis.json",
"create2collisionStorage.json",
"create2collisionStorageParis.json",
"RevertInCreateInInitCreate2.json",
"RevertInCreateInInitCreate2Paris.json",
]

func skipGSTTests*(folder: string, name: string): bool =
# we skip tests that are slow or expected to fail for now
if slowGSTTests(folder, name):
Expand All @@ -102,6 +116,8 @@ func skipNewGSTTests*(folder: string, name: string): bool =
if skipGSTTests(folder, name):
return true

name in EIP7610_Problematic

func skipVMTests*(folder: string, name: string): bool =
result = (folder == "vmPerformance" and "loop" in name)

Expand All @@ -124,6 +140,9 @@ func skipNewBCTests*(folder: string, name: string): bool =
if slowGSTTests(folder, name):
return true

if name in EIP7610_Problematic:
return true

name in @[
# BC huge memory consumption
"randomStatetest94.json",
Expand Down
22 changes: 22 additions & 0 deletions tests/test_state_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,27 @@ proc stateDBMain*() =
check ac.vts(0xcc, 7, 88) == false
check ac.vts(0xdd, 2, 66) == false

test "accounts cache contractCollision":
# use previous hash
var ac = init(AccountsCache, acDB, emptyRlpHash, true)
let addr2 = initAddr(2)
check ac.contractCollision(addr2) == false

ac.setStorage(addr2, 1.u256, 1.u256)
check ac.contractCollision(addr2) == false

ac.persist()
check ac.contractCollision(addr2) == true

let addr3 = initAddr(3)
check ac.contractCollision(addr3) == false
ac.setCode(addr3, @[0xaa.byte, 0xbb])
check ac.contractCollision(addr3) == true

let addr4 = initAddr(4)
check ac.contractCollision(addr4) == false
ac.setNonce(addr4, 1)
check ac.contractCollision(addr4) == true

when isMainModule:
stateDBMain()

0 comments on commit af6d73e

Please sign in to comment.