move origin and gasPrice from Message to vmState

This commit is contained in:
andri lim 2020-01-16 13:36:58 +07:00 committed by zah
parent d300bc352d
commit bab359cabf
9 changed files with 34 additions and 21 deletions

View File

@ -39,7 +39,7 @@ proc newComputation*(vmState: BaseVMState, message: Message, forkOverride=none(F
proc isOriginComputation*(c: Computation): bool = proc isOriginComputation*(c: Computation): bool =
# Is this computation the computation initiated by a transaction # Is this computation the computation initiated by a transaction
c.msg.isOrigin c.msg.sender == c.vmState.txOrigin
template isSuccess*(c: Computation): bool = template isSuccess*(c: Computation): bool =
c.error.isNil c.error.isNil

View File

@ -240,7 +240,7 @@ op balance, inline = true:
op origin, inline = true: op origin, inline = true:
## 0x32, Get execution origination address. ## 0x32, Get execution origination address.
push: c.msg.origin push: c.vmState.txOrigin
op caller, inline = true: op caller, inline = true:
## 0x33, Get caller address. ## 0x33, Get caller address.
@ -301,7 +301,7 @@ op codeCopy, inline = false, memStartPos, copyStartPos, size:
op gasprice, inline = true: op gasprice, inline = true:
## 0x3A, Get price of gas in current environment. ## 0x3A, Get price of gas in current environment.
push: c.msg.gasPrice push: c.vmState.txGasPrice
op extCodeSize, inline = true: op extCodeSize, inline = true:
## 0x3b, Get size of an account's code ## 0x3b, Get size of an account's code
@ -569,8 +569,6 @@ proc setupCreate(c: Computation, memPos, len: int, value: Uint256, opCode: stati
kind: callKind, kind: callKind,
depth: c.msg.depth + 1, depth: c.msg.depth + 1,
gas: createMsgGas, gas: createMsgGas,
gasPrice: c.msg.gasPrice,
origin: c.msg.origin,
sender: c.msg.contractAddress, sender: c.msg.contractAddress,
contractAddress: contractAddress, contractAddress: contractAddress,
codeAddress: CREATE_CONTRACT_ADDRESS, codeAddress: CREATE_CONTRACT_ADDRESS,
@ -749,8 +747,6 @@ template genCall(callName: untyped, opCode: Op): untyped =
kind: callKind, kind: callKind,
depth: c.msg.depth + 1, depth: c.msg.depth + 1,
gas: childGasLimit, gas: childGasLimit,
gasPrice: c.msg.gasPrice,
origin: c.msg.origin,
sender: sender, sender: sender,
contractAddress: contractAddress, contractAddress: contractAddress,
codeAddress: codeAddress, codeAddress: codeAddress,

View File

@ -9,8 +9,5 @@ import
eth/common, eth/common,
../constants, ../validation, ../vm_types, chronicles ../constants, ../validation, ../vm_types, chronicles
proc isOrigin*(message: Message): bool =
message.sender == message.origin
proc isCreate*(message: Message): bool = proc isCreate*(message: Message): bool =
message.kind in {evmcCreate, evmcCreate2} message.kind in {evmcCreate, evmcCreate2}

View File

@ -42,6 +42,12 @@ proc newBaseVMState*(prevStateRoot: Hash256, header: BlockHeader,
new result new result
result.init(prevStateRoot, header, chainDB, tracerFlags) result.init(prevStateRoot, header, chainDB, tracerFlags)
proc txContext*(vmState: BaseVMState, origin: EthAddress, gasPrice: GasInt) =
## this proc will be called each time a new transaction
## is going to be executed
vmState.txOrigin = origin
vmState.txGasPrice = gasPrice
method blockhash*(vmState: BaseVMState): Hash256 {.base, gcsafe.} = method blockhash*(vmState: BaseVMState): Hash256 {.base, gcsafe.} =
vmState.blockHeader.hash vmState.blockHeader.hash

View File

@ -44,12 +44,15 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender, recipient:
debug "not enough gas to perform calculation", gas=gas debug "not enough gas to perform calculation", gas=gas
return return
vmState.txContext(
origin = sender,
gasPrice = tx.gasPrice
)
let msg = Message( let msg = Message(
kind: if tx.isContractCreation: evmcCreate else: evmcCall, kind: if tx.isContractCreation: evmcCreate else: evmcCall,
depth: 0, depth: 0,
gas: gas, gas: gas,
gasPrice: tx.gasPrice,
origin: sender,
sender: sender, sender: sender,
contractAddress: recipient, contractAddress: recipient,
codeAddress: tx.to, codeAddress: tx.to,

View File

@ -28,6 +28,8 @@ type
touchedAccounts*: HashSet[EthAddress] touchedAccounts*: HashSet[EthAddress]
suicides* : HashSet[EthAddress] suicides* : HashSet[EthAddress]
status* : bool status* : bool
txOrigin* : EthAddress
txGasPrice* : GasInt
AccessLogs* = ref object AccessLogs* = ref object
reads*: Table[string, string] reads*: Table[string, string]
@ -100,8 +102,6 @@ type
kind*: CallKind kind*: CallKind
depth*: int depth*: int
gas*: GasInt gas*: GasInt
gasPrice*: GasInt
origin*: EthAddress
sender*: EthAddress sender*: EthAddress
contractAddress*: EthAddress contractAddress*: EthAddress
codeAddress*: EthAddress codeAddress*: EthAddress

View File

@ -205,13 +205,16 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress,
let gasUsed = 0 #tx.payload.intrinsicGas.GasInt + gasFees[fork][GasTXCreate] let gasUsed = 0 #tx.payload.intrinsicGas.GasInt + gasFees[fork][GasTXCreate]
vmState.txContext(
origin = sender,
gasPrice = tx.gasPrice
)
let contractAddress = generateAddress(sender, tx.accountNonce) let contractAddress = generateAddress(sender, tx.accountNonce)
let msg = Message( let msg = Message(
kind: evmcCall, kind: evmcCall,
depth: 0, depth: 0,
gas: tx.gasLimit - gasUsed, gas: tx.gasLimit - gasUsed,
gasPrice: tx.gasPrice,
origin: sender,
sender: sender, sender: sender,
contractAddress: contractAddress, contractAddress: contractAddress,
codeAddress: tx.to, codeAddress: tx.to,

View File

@ -29,11 +29,16 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped =
gasPrice = 1.GasInt gasPrice = 1.GasInt
sender: EthAddress sender: EthAddress
toAddress = initAddress(address) toAddress = initAddress(address)
vmState.txContext(
origin = sender,
gasPrice = gasPrice
)
var
message = Message( message = Message(
kind: evmcCall, kind: evmcCall,
gas: gas, gas: gas,
gasPrice: gasPrice,
origin: sender,
sender: sender, sender: sender,
contractAddress: toAddress, contractAddress: toAddress,
codeAddress: toAddress, codeAddress: toAddress,

View File

@ -47,14 +47,17 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
let address = fexec{"address"}.getStr.parseAddress let address = fexec{"address"}.getStr.parseAddress
code = db.getCode(address).toSeq code = db.getCode(address).toSeq
vmState.txContext(
origin = fexec{"origin"}.getStr.parseAddress,
gasPrice = fexec{"gasPrice"}.getHexadecimalInt
)
code = fexec{"code"}.getStr.hexToSeqByte code = fexec{"code"}.getStr.hexToSeqByte
let toAddress = fexec{"address"}.getStr.parseAddress let toAddress = fexec{"address"}.getStr.parseAddress
let message = Message( let message = Message(
kind: if toAddress == ZERO_ADDRESS: evmcCreate else: evmcCall, # assume ZERO_ADDRESS is a contract creation kind: if toAddress == ZERO_ADDRESS: evmcCreate else: evmcCall, # assume ZERO_ADDRESS is a contract creation
depth: 0, depth: 0,
gas: fexec{"gas"}.getHexadecimalInt, gas: fexec{"gas"}.getHexadecimalInt,
gasPrice: fexec{"gasPrice"}.getHexadecimalInt,
origin: fexec{"origin"}.getStr.parseAddress,
sender: fexec{"caller"}.getStr.parseAddress, sender: fexec{"caller"}.getStr.parseAddress,
contractAddress: toAddress, contractAddress: toAddress,
codeAddress: toAddress, codeAddress: toAddress,