[skip ci] change storageAddress to contractAddres

This commit is contained in:
andri lim 2020-01-07 23:11:06 +07:00 committed by zah
parent 40ddbca1ee
commit 836e087984
9 changed files with 70 additions and 57 deletions

View File

@ -113,7 +113,7 @@ proc writeContract*(computation: BaseComputation, fork: Fork): bool {.gcsafe.} =
debug "Contract code size exceeds EIP170", limit=EIP170_CODE_SIZE_LIMIT, actual=contractCode.len
return false
let storageAddr = computation.msg.storageAddress
let storageAddr = computation.msg.contractAddress
if computation.isSuicided(storageAddr): return
let gasParams = GasParams(kind: Create, cr_memLength: contractCode.len)
@ -138,7 +138,7 @@ proc transferBalance(computation: BaseComputation, opCode: static[Op]) =
when opCode in {Call, Create}:
computation.vmState.mutateStateDb:
db.subBalance(computation.msg.sender, computation.msg.value)
db.addBalance(computation.msg.storageAddress, computation.msg.value)
db.addBalance(computation.msg.contractAddress, computation.msg.value)
template continuation*(comp: BaseComputation, body: untyped) =
# this is a helper template to implement continuation
@ -177,10 +177,10 @@ proc applyMessage*(computation: BaseComputation, opCode: static[Op]) =
when opCode in {Create, Create2}:
if computation.getFork >= FkSpurious:
computation.vmState.mutateStateDb:
db.incNonce(computation.msg.storageAddress)
db.incNonce(computation.msg.contractAddress)
if computation.getFork >= FkByzantium:
# RevertInCreateInInit.json
db.setStorageRoot(computation.msg.storageAddress, emptyRlpHash)
db.setStorageRoot(computation.msg.contractAddress, emptyRlpHash)
when opCode in {CallCode, Call, Create}:
computation.transferBalance(opCode)
@ -219,7 +219,7 @@ proc addChildComputation*(computation: BaseComputation, child: BaseComputation)
computation.children.add(child)
proc registerAccountForDeletion*(c: BaseComputation, beneficiary: EthAddress) =
if c.msg.storageAddress in c.accountsToDelete:
if c.msg.contractAddress in c.accountsToDelete:
raise newException(ValueError,
"invariant: should be impossible for an account to be " &
"registered for deletion multiple times")
@ -227,8 +227,8 @@ proc registerAccountForDeletion*(c: BaseComputation, beneficiary: EthAddress) =
# we should remove this accountsToDelete map delegate it to state db
# we will only keep suicides list here in order to make
# evmc integration easier
c.accountsToDelete[c.msg.storageAddress] = beneficiary
c.suicides.incl(c.msg.storageAddress)
c.accountsToDelete[c.msg.contractAddress] = beneficiary
c.suicides.incl(c.msg.contractAddress)
proc addLogEntry*(c: BaseComputation, log: Log) {.inline.} =
c.logEntries.add(log)
@ -284,10 +284,10 @@ proc collectTouchedAccounts*(c: BaseComputation, output: var HashSet[EthAddress]
if condition:
# Special case to account for geth+parity bug
# https://github.com/ethereum/EIPs/issues/716
if cmpThree(c.msg.storageAddress):
output.incl c.msg.storageAddress
if cmpThree(c.msg.contractAddress):
output.incl c.msg.contractAddress
else:
output.incl c.msg.storageAddress
output.incl c.msg.contractAddress
if c.isSuccess or isIstanbul:
# recurse into nested computations (even errored ones, since looking for RIPEMD160)

View File

@ -231,7 +231,7 @@ proc writePaddedResult(mem: var Memory,
op address, inline = true:
## 0x30, Get address of currently executing account.
push: computation.msg.storageAddress
push: computation.msg.contractAddress
op balance, inline = true:
## 0x31, Get balance of the given account.
@ -380,7 +380,7 @@ op chainId, inline = true:
op selfBalance, inline = true:
## 0x47, Get current contract's balance.
let stateDb = computation.vmState.readOnlyStateDb
push: stateDb.getBalance(computation.msg.storageAddress)
push: stateDb.getBalance(computation.msg.contractAddress)
# ##########################################
# 50s: Stack, Memory, Storage and Flow Operations
@ -428,26 +428,26 @@ op mstore8, inline = true, memStartPos, value:
op sload, inline = true, slot:
## 0x54, Load word from storage.
let (value, _) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.storageAddress, slot)
let (value, _) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.contractAddress, slot)
push(value)
op sstore, inline = false, slot, value:
## 0x55, Save word to storage.
checkInStaticContext(computation)
let (currentValue, existing) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.storageAddress, slot)
let (currentValue, existing) = computation.vmState.readOnlyStateDB.getStorage(computation.msg.contractAddress, slot)
let
gasParam = GasParams(kind: Op.Sstore, s_isStorageEmpty: currentValue.isZero)
(gasCost, gasRefund) = computation.gasCosts[Sstore].c_handler(value, gasParam)
computation.gasMeter.consumeGas(gasCost, &"SSTORE: {computation.msg.storageAddress}[{slot}] -> {value} ({currentValue})")
computation.gasMeter.consumeGas(gasCost, &"SSTORE: {computation.msg.contractAddress}[{slot}] -> {value} ({currentValue})")
if gasRefund > 0:
computation.gasMeter.refundGas(gasRefund)
computation.vmState.mutateStateDB:
db.setStorage(computation.msg.storageAddress, slot, value)
db.setStorage(computation.msg.contractAddress, slot, value)
proc jumpImpl(computation: BaseComputation, jumpTarget: UInt256) =
if jumpTarget >= computation.code.len.u256:
@ -522,7 +522,7 @@ proc canTransfer(computation: BaseComputation, memPos, memLen: int, value: Uint2
# to avoid confusion
let senderBalance =
computation.vmState.readOnlyStateDb().
getBalance(computation.msg.storageAddress)
getBalance(computation.msg.contractAddress)
if senderBalance < value:
debug "Computation Failure", reason = "Insufficient funds available to transfer", required = computation.msg.value, balance = senderBalance
@ -559,16 +559,16 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
computation.vmState.mutateStateDB:
# Regarding collisions, see: https://github.com/status-im/nimbus/issues/133
# See: https://github.com/ethereum/EIPs/issues/684
let creationNonce = db.getNonce(computation.msg.storageAddress)
db.setNonce(computation.msg.storageAddress, creationNonce + 1)
let creationNonce = db.getNonce(computation.msg.contractAddress)
db.setNonce(computation.msg.contractAddress, creationNonce + 1)
contractAddress = generateAddress(computation.msg.storageAddress, creationNonce)
contractAddress = generateAddress(computation.msg.contractAddress, creationNonce)
isCollision = db.hasCodeOrNonce(contractAddress)
else:
computation.vmState.mutateStateDB:
db.incNonce(computation.msg.storageAddress)
db.incNonce(computation.msg.contractAddress)
let salt = computation.stack.popInt()
contractAddress = generateSafeAddress(computation.msg.storageAddress, salt, callData)
contractAddress = generateSafeAddress(computation.msg.contractAddress, salt, callData)
isCollision = db.hasCodeOrNonce(contractAddress)
if isCollision:
@ -581,8 +581,8 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
gas: createMsgGas,
gasPrice: computation.msg.gasPrice,
origin: computation.msg.origin,
sender: computation.msg.storageAddress,
storageAddress: contractAddress,
sender: computation.msg.contractAddress,
contractAddress: contractAddress,
codeAddress: CREATE_CONTRACT_ADDRESS,
value: value,
data: @[],
@ -613,7 +613,7 @@ template genCreate(callName: untyped, opCode: Op): untyped =
if childComp.isError:
push: 0
else:
push: childComp.msg.storageAddress
push: childComp.msg.contractAddress
checkInStaticContext(computation)
childComp.applyMessage(Create)
@ -632,7 +632,7 @@ proc callParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, Et
result = (gas,
value,
codeAddress, # contractAddress
computation.msg.storageAddress, # sender
computation.msg.contractAddress, # sender
codeAddress,
memoryInputStartPosition,
memoryInputSize,
@ -650,8 +650,8 @@ proc callCodeParams(computation: BaseComputation): (UInt256, UInt256, EthAddress
result = (gas,
value,
computation.msg.storageAddress, # contractAddress
computation.msg.storageAddress, # sender
computation.msg.contractAddress, # contractAddress
computation.msg.contractAddress, # sender
codeAddress,
memoryInputStartPosition,
memoryInputSize,
@ -668,7 +668,7 @@ proc delegateCallParams(computation: BaseComputation): (UInt256, UInt256, EthAdd
result = (gas,
computation.msg.value, # value
computation.msg.storageAddress, # contractAddress
computation.msg.contractAddress, # contractAddress
computation.msg.sender, # sender
codeAddress,
memoryInputStartPosition,
@ -687,7 +687,7 @@ proc staticCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddre
result = (gas,
0.u256, # value
codeAddress, # contractAddress
computation.msg.storageAddress, # sender
computation.msg.contractAddress, # sender
codeAddress,
memoryInputStartPosition,
memoryInputSize,
@ -745,7 +745,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
gasPrice: computation.msg.gasPrice,
origin: computation.msg.origin,
sender: sender,
storageAddress: contractAddress,
contractAddress: contractAddress,
codeAddress: codeAddress,
value: value,
data: callData,
@ -827,7 +827,7 @@ proc selfDestructImpl(computation: BaseComputation, beneficiary: EthAddress) =
# In particular, EIP150 and EIP161 have extra requirements.
computation.vmState.mutateStateDB:
let
localBalance = db.getBalance(computation.msg.storageAddress)
localBalance = db.getBalance(computation.msg.contractAddress)
beneficiaryBalance = db.getBalance(beneficiary)
# Transfer to beneficiary
@ -836,13 +836,13 @@ proc selfDestructImpl(computation: BaseComputation, beneficiary: EthAddress) =
# Zero the balance of the address being deleted.
# This must come after sending to beneficiary in case the
# contract named itself as the beneficiary.
db.setBalance(computation.msg.storageAddress, 0.u256)
db.setBalance(computation.msg.contractAddress, 0.u256)
# Register the account to be deleted
computation.registerAccountForDeletion(beneficiary)
trace "SELFDESTRUCT",
storageAddress = computation.msg.storageAddress.toHex,
contractAddress = computation.msg.contractAddress.toHex,
localBalance = localBalance.toString,
beneficiary = beneficiary.toHex
@ -868,7 +868,7 @@ op selfDestructEip161, inline = false:
beneficiary = computation.stack.popAddress()
stateDb = computation.vmState.readOnlyStateDb
isDead = stateDb.isDeadAccount(beneficiary)
balance = stateDb.getBalance(computation.msg.storageAddress)
balance = stateDb.getBalance(computation.msg.contractAddress)
let gasParams = GasParams(kind: SelfDestruct,
sd_condition: isDead and not balance.isZero
@ -929,20 +929,20 @@ op sstoreEIP2200, inline = false, slot, value:
raise newException(OutOfGas, "Gas not enough to perform EIP2200 SSTORE")
let stateDB = computation.vmState.readOnlyStateDB
let (currentValue, existing) = stateDB.getStorage(computation.msg.storageAddress, slot)
let (currentValue, existing) = stateDB.getStorage(computation.msg.contractAddress, slot)
let
gasParam = GasParams(kind: Op.Sstore,
s_isStorageEmpty: currentValue.isZero,
s_currentValue: currentValue,
s_originalValue: stateDB.getCommittedStorage(computation.msg.storageAddress, slot)
s_originalValue: stateDB.getCommittedStorage(computation.msg.contractAddress, slot)
)
(gasCost, gasRefund) = computation.gasCosts[Sstore].c_handler(value, gasParam)
computation.gasMeter.consumeGas(gasCost, &"SSTORE EIP2200: {computation.msg.storageAddress}[{slot}] -> {value} ({currentValue})")
computation.gasMeter.consumeGas(gasCost, &"SSTORE EIP2200: {computation.msg.contractAddress}[{slot}] -> {value} ({currentValue})")
if gasRefund != 0:
computation.gasMeter.refundGas(gasRefund)
computation.vmState.mutateStateDB:
db.setStorage(computation.msg.storageAddress, slot, value)
db.setStorage(computation.msg.contractAddress, slot, value)

View File

@ -125,7 +125,7 @@ proc logImpl(c: BaseComputation, opcode: Op, topicCount: int) =
c.memory.extend(memPos, len)
log.data = c.memory.read(memPos, len)
log.address = c.msg.storageAddress
log.address = c.msg.contractAddress
c.addLogEntry(log)
template genLog*() =

View File

@ -100,7 +100,7 @@ proc traceOpCodeEnded*(tracer: var TransactionTracer, c: BaseComputation, op: Op
if c.msg.depth < tracer.storageKeys.len:
var stateDB = c.vmState.accountDb
for key in tracer.storage(c.msg.depth):
let (value, _) = stateDB.getStorage(c.msg.storageAddress, key)
let (value, _) = stateDB.getStorage(c.msg.contractAddress, key)
storage[key.dumpHex] = %(value.dumpHex)
j["storage"] = storage

View File

@ -50,7 +50,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient:
gasPrice: tx.gasPrice,
origin: sender,
sender: sender,
storageAddress: recipient,
contractAddress: recipient,
codeAddress: tx.to,
value: tx.value,
data: data,

View File

@ -103,7 +103,7 @@ type
gasPrice*: GasInt
origin*: EthAddress
sender*: EthAddress
storageAddress*: EthAddress
contractAddress*: EthAddress
codeAddress*: EthAddress
value*: UInt256
data*: seq[byte]

View File

@ -287,7 +287,7 @@ proc runVM*(blockNumber: Uint256, chainDB: BaseChainDB, boa: Assembler): bool =
var
stateDB = computation.vmState.accountDb
account = stateDB.getAccount(computation.msg.storageAddress)
account = stateDB.getAccount(computation.msg.contractAddress)
trie = initSecureHexaryTrie(chainDB.db, account.storageRoot)
for kv in boa.storage:

View File

@ -28,8 +28,19 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped =
gas = 1_000_000.GasInt
gasPrice = 1.GasInt
sender: EthAddress
to = initAddress(address)
message = newMessage(gas, gasPrice, to, sender, 0.u256, data, @[], contractCreation = false)
toAddress = initAddress(address)
message = Message(
gas: gas,
gasPrice: gasPrice,
origin: sender,
sender: sender,
contractAddress: toAddress,
codeAddress: toAddress,
value: 0.u256,
data: data,
code: @[],
contractCreation: false
)
computation = newBaseComputation(vmState, header.blockNumber, message)
echo "Running ", action.astToStr, " - ", test["name"]
`action`(computation)

View File

@ -45,17 +45,19 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
code = fexec{"code"}.getStr.hexToSeqByte
let toAddress = fexec{"address"}.getStr.parseAddress
let message = newMessage(
to = toAddress,
sender = fexec{"caller"}.getStr.parseAddress,
value = cast[uint64](fexec{"value"}.getHexadecimalInt).u256, # Cast workaround for negative value
data = fexec{"data"}.getStr.hexToSeqByte,
code = code,
contractCreation = toAddress == ZERO_ADDRESS, # assume ZERO_ADDRESS is a contract creation
gas = fexec{"gas"}.getHexadecimalInt,
gasPrice = fexec{"gasPrice"}.getHexadecimalInt,
options = newMessageOptions(origin=fexec{"origin"}.getStr.parseAddress,
createAddress = toAddress))
let message = Message(
depth: 0,
gas: fexec{"gas"}.getHexadecimalInt,
gasPrice: fexec{"gasPrice"}.getHexadecimalInt,
origin: fexec{"origin"}.getStr.parseAddress,
sender: fexec{"caller"}.getStr.parseAddress,
contractAddress: toAddress,
codeAddress: toAddress,
value: cast[uint64](fexec{"value"}.getHexadecimalInt).u256, # Cast workaround for negative value
data: fexec{"data"}.getStr.hexToSeqByte,
code: code,
contractCreation: toAddress == ZERO_ADDRESS # assume ZERO_ADDRESS is a contract creation
)
var computation = newBaseComputation(vmState, header.blockNumber, message)
computation.executeOpcodes()