[skip ci] add evmc CallKind to EVM Message

This commit is contained in:
andri lim 2020-01-08 10:12:06 +07:00 committed by zah
parent dfa2f0099f
commit 8f7597e23b
7 changed files with 24 additions and 18 deletions

View File

@ -556,6 +556,7 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
isCollision: bool
when opCode == Create:
const callKind = evmcCreate
computation.vmState.mutateStateDB:
# Regarding collisions, see: https://github.com/status-im/nimbus/issues/133
# See: https://github.com/ethereum/EIPs/issues/684
@ -565,6 +566,7 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
contractAddress = generateAddress(computation.msg.contractAddress, creationNonce)
isCollision = db.hasCodeOrNonce(contractAddress)
else:
const callKind = evmcCreate2
computation.vmState.mutateStateDB:
db.incNonce(computation.msg.contractAddress)
let salt = computation.stack.popInt()
@ -577,6 +579,7 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
return
let childMsg = Message(
kind: callKind,
depth: computation.msg.depth + 1,
gas: createMsgGas,
gasPrice: computation.msg.gasPrice,
@ -586,8 +589,7 @@ proc setupCreate(computation: BaseComputation, memPos, len: int, value: Uint256,
codeAddress: CREATE_CONTRACT_ADDRESS,
value: value,
data: @[],
code: callData,
contractCreation: true
code: callData
)
result = newBaseComputation(
@ -621,7 +623,7 @@ template genCreate(callName: untyped, opCode: Op): untyped =
genCreate(create, Create)
genCreate(create2, Create2)
proc callParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
proc callParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, CallKind, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
let gas = computation.stack.popInt()
let codeAddress = computation.stack.popAddress()
@ -634,13 +636,14 @@ proc callParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, Et
codeAddress, # contractAddress
computation.msg.contractAddress, # sender
codeAddress,
evmcCall,
memoryInputStartPosition,
memoryInputSize,
memoryOutputStartPosition,
memoryOutputSize,
computation.msg.flags)
proc callCodeParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
proc callCodeParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, CallKind, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
let gas = computation.stack.popInt()
let codeAddress = computation.stack.popAddress()
@ -653,13 +656,14 @@ proc callCodeParams(computation: BaseComputation): (UInt256, UInt256, EthAddress
computation.msg.contractAddress, # contractAddress
computation.msg.contractAddress, # sender
codeAddress,
evmcCallCode,
memoryInputStartPosition,
memoryInputSize,
memoryOutputStartPosition,
memoryOutputSize,
computation.msg.flags)
proc delegateCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
proc delegateCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, CallKind, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
let gas = computation.stack.popInt()
let codeAddress = computation.stack.popAddress()
@ -671,13 +675,14 @@ proc delegateCallParams(computation: BaseComputation): (UInt256, UInt256, EthAdd
computation.msg.contractAddress, # contractAddress
computation.msg.sender, # sender
codeAddress,
evmcDelegateCall,
memoryInputStartPosition,
memoryInputSize,
memoryOutputStartPosition,
memoryOutputSize,
computation.msg.flags)
proc staticCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
proc staticCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddress, EthAddress, EthAddress, CallKind, UInt256, UInt256, UInt256, UInt256, MsgFlags) =
let gas = computation.stack.popInt()
let codeAddress = computation.stack.popAddress()
@ -689,6 +694,7 @@ proc staticCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddre
codeAddress, # contractAddress
computation.msg.contractAddress, # sender
codeAddress,
evmcCall,
memoryInputStartPosition,
memoryInputSize,
memoryOutputStartPosition,
@ -698,7 +704,7 @@ proc staticCallParams(computation: BaseComputation): (UInt256, UInt256, EthAddre
template genCall(callName: untyped, opCode: Op): untyped =
proc `callName Setup`(computation: BaseComputation, callNameStr: string): BaseComputation =
let (gas, value, contractAddress, sender,
codeAddress,
codeAddress, callKind,
memoryInputStartPosition, memoryInputSize,
memoryOutputStartPosition, memoryOutputSize,
flags) = `callName Params`(computation)
@ -740,6 +746,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
code = computation.vmState.readOnlyStateDb.getCode(codeAddress)
var childMsg = Message(
kind: callKind,
depth: computation.msg.depth + 1,
gas: childGasLimit,
gasPrice: computation.msg.gasPrice,
@ -750,7 +757,6 @@ template genCall(callName: untyped, opCode: Op): untyped =
value: value,
data: callData,
code: code.toSeq,
contractCreation: false,
flags: flags)
var childComp = newBaseComputation(

View File

@ -13,4 +13,4 @@ proc isOrigin*(message: Message): bool =
message.sender == message.origin
proc isCreate*(message: Message): bool =
message.contractCreation
message.kind in {evmcCreate, evmcCreate2}

View File

@ -45,6 +45,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient:
return
let msg = Message(
kind: if tx.isContractCreation: evmcCreate else: evmcCall,
depth: 0,
gas: gas,
gasPrice: tx.gasPrice,
@ -54,8 +55,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient:
codeAddress: tx.to,
value: tx.value,
data: data,
code: code,
contractCreation: tx.isContractCreation
code: code
)
result = newBaseComputation(vmState, vmState.blockNumber, msg, some(fork))

View File

@ -98,6 +98,7 @@ type
emvcStatic = 1
Message* = ref object
kind*: CallKind
depth*: int
gas*: GasInt
gasPrice*: GasInt
@ -108,5 +109,4 @@ type
value*: UInt256
data*: seq[byte]
code*: seq[byte]
contractCreation*: bool
flags*: MsgFlags

View File

@ -207,6 +207,7 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress,
let contractAddress = generateAddress(sender, tx.accountNonce)
let msg = Message(
kind: evmcCall,
depth: 0,
gas: tx.gasLimit - gasUsed,
gasPrice: tx.gasPrice,
@ -216,8 +217,7 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress,
codeAddress: tx.to,
value: tx.value,
data: data,
code: tx.payload,
contractCreation: false
code: tx.payload
)
newBaseComputation(vmState, vmState.blockNumber, msg, some(fork))

View File

@ -30,6 +30,7 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped =
sender: EthAddress
toAddress = initAddress(address)
message = Message(
kind: evmcCall,
gas: gas,
gasPrice: gasPrice,
origin: sender,
@ -38,8 +39,7 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped =
codeAddress: toAddress,
value: 0.u256,
data: data,
code: @[],
contractCreation: false
code: @[]
)
computation = newBaseComputation(vmState, header.blockNumber, message)
echo "Running ", action.astToStr, " - ", test["name"]

View File

@ -46,6 +46,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
code = fexec{"code"}.getStr.hexToSeqByte
let toAddress = fexec{"address"}.getStr.parseAddress
let message = Message(
kind: if toAddress == ZERO_ADDRESS: evmcCreate else: evmcCall, # assume ZERO_ADDRESS is a contract creation
depth: 0,
gas: fexec{"gas"}.getHexadecimalInt,
gasPrice: fexec{"gasPrice"}.getHexadecimalInt,
@ -55,8 +56,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
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
code: code
)
var computation = newBaseComputation(vmState, header.blockNumber, message)