remove code from EVM Message and load code in 'Computation'

This commit is contained in:
andri lim 2020-01-20 21:02:06 +07:00 committed by zah
parent 577c7ca5aa
commit 933b2dad78
8 changed files with 18 additions and 37 deletions

View File

@ -26,7 +26,7 @@ proc `$`*(b: byte): string =
proc newCodeStream*(codeBytes: seq[byte]): CodeStream = proc newCodeStream*(codeBytes: seq[byte]): CodeStream =
new(result) new(result)
result.bytes = codeBytes shallowCopy(result.bytes, codeBytes)
result.pc = 0 result.pc = 0
result.invalidPositions = initHashSet[int]() result.invalidPositions = initHashSet[int]()
result.depthProcessed = 0 result.depthProcessed = 0

View File

@ -131,7 +131,7 @@ proc generateContractAddress(c: Computation, salt: Option[Uint256]): EthAddress
let creationNonce = c.vmState.readOnlyStateDb().getNonce(c.msg.sender) let creationNonce = c.vmState.readOnlyStateDb().getNonce(c.msg.sender)
result = generateAddress(c.msg.sender, creationNonce) result = generateAddress(c.msg.sender, creationNonce)
else: else:
result = generateSafeAddress(c.msg.sender, salt.get(), c.msg.code) result = generateSafeAddress(c.msg.sender, salt.get(), c.msg.data)
proc newComputation*(vmState: BaseVMState, message: Message, salt=none(Uint256)): Computation = proc newComputation*(vmState: BaseVMState, message: Message, salt=none(Uint256)): Computation =
new result new result
@ -142,10 +142,13 @@ proc newComputation*(vmState: BaseVMState, message: Message, salt=none(Uint256))
result.gasMeter.init(message.gas) result.gasMeter.init(message.gas)
result.touchedAccounts = initHashSet[EthAddress]() result.touchedAccounts = initHashSet[EthAddress]()
result.suicides = initHashSet[EthAddress]() result.suicides = initHashSet[EthAddress]()
result.code = newCodeStream(message.code)
if result.msg.isCreate(): if result.msg.isCreate():
result.msg.contractAddress = result.generateContractAddress(salt) result.msg.contractAddress = result.generateContractAddress(salt)
result.code = newCodeStream(message.data)
message.data = @[]
else:
result.code = newCodeStream(vmState.readOnlyStateDb.getCode(message.codeAddress).toSeq)
when evmc_enabled: when evmc_enabled:
result.host.init( result.host.init(

View File

@ -595,8 +595,7 @@ proc setupCreate(c: Computation, memPos, len: int, value: Uint256, opCode: stati
gas: createMsgGas, gas: createMsgGas,
sender: c.msg.contractAddress, sender: c.msg.contractAddress,
value: value, value: value,
data: @[], data: c.memory.read(memPos, len)
code: c.memory.read(memPos, len)
) )
when opCode == Create: when opCode == Create:
@ -759,10 +758,6 @@ template genCall(callName: untyped, opCode: Op): untyped =
c.gasMeter.returnGas(childGasLimit) c.gasMeter.returnGas(childGasLimit)
return return
let
callData = c.memory.read(memInPos, memInLen)
code = c.getCode(codeAddress)
var childMsg = Message( var childMsg = Message(
kind: callKind, kind: callKind,
depth: c.msg.depth + 1, depth: c.msg.depth + 1,
@ -771,8 +766,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
contractAddress: contractAddress, contractAddress: contractAddress,
codeAddress: codeAddress, codeAddress: codeAddress,
value: value, value: value,
data: callData, data: c.memory.read(memInPos, memInLen),
code: code.toSeq,
flags: flags) flags: flags)
var child = newComputation(c.vmState, childMsg) var child = newComputation(c.vmState, childMsg)

View File

@ -29,17 +29,6 @@ proc validateTransaction*(vmState: BaseVMState, tx: Transaction, sender: EthAddr
proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, fork: Fork) : Computation = proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress, fork: Fork) : Computation =
var gas = tx.gasLimit - tx.intrinsicGas(fork) var gas = tx.gasLimit - tx.intrinsicGas(fork)
# TODO: refactor message to use byterange
# instead of seq[byte]
var data, code: seq[byte]
if tx.isContractCreation:
data = @[]
code = tx.payload
else:
data = tx.payload
code = vmState.readOnlyStateDB.getCode(tx.to).toSeq
if gas < 0: if gas < 0:
debug "not enough gas to perform calculation", gas=gas debug "not enough gas to perform calculation", gas=gas
return return
@ -58,8 +47,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress
contractAddress: tx.getRecipient(), contractAddress: tx.getRecipient(),
codeAddress: tx.to, codeAddress: tx.to,
value: tx.value, value: tx.value,
data: data, data: tx.payload
code: code
) )
result = newComputation(vmState, msg) result = newComputation(vmState, msg)

View File

@ -112,5 +112,4 @@ type
codeAddress*: EthAddress codeAddress*: EthAddress
value*: UInt256 value*: UInt256
data*: seq[byte] data*: seq[byte]
code*: seq[byte]
flags*: MsgFlags flags*: MsgFlags

View File

@ -218,12 +218,14 @@ proc initComputation(vmState: BaseVMState, tx: Transaction, sender: EthAddress,
gas: tx.gasLimit - gasUsed, gas: tx.gasLimit - gasUsed,
sender: sender, sender: sender,
contractAddress: contractAddress, contractAddress: contractAddress,
codeAddress: tx.to, codeAddress: contractAddress,
value: tx.value, value: tx.value,
data: data, data: data
code: tx.payload
) )
vmState.mutateStateDb:
db.setCode(contractAddress, tx.payload.toRange)
newComputation(vmState, msg) newComputation(vmState, msg)
proc initDatabase*(): (Uint256, BaseChainDB) = proc initDatabase*(): (Uint256, BaseChainDB) =
@ -240,7 +242,7 @@ proc initDatabase*(): (Uint256, BaseChainDB) =
result = (blockNumber, newBaseChainDB(memoryDB, false)) result = (blockNumber, newBaseChainDB(memoryDB, false))
proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data: seq[byte], fork: Fork): Computation = proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, code, data: seq[byte], fork: Fork): Computation =
let let
parentNumber = blockNumber - 1 parentNumber = blockNumber - 1
parent = chainDB.getBlockHeader(parentNumber) parent = chainDB.getBlockHeader(parentNumber)
@ -253,7 +255,7 @@ proc initComputation(blockNumber: Uint256, chainDB: BaseChainDB, payload, data:
tx = body.transactions[0] tx = body.transactions[0]
sender = transaction.getSender(tx) sender = transaction.getSender(tx)
tx.payload = payload tx.payload = code
tx.gasLimit = 500000000 tx.gasLimit = 500000000
initComputation(vmState, tx, sender, data, some(fork)) initComputation(vmState, tx, sender, data, some(fork))

View File

@ -43,8 +43,7 @@ template doTest(fixture: JsonNode, address: byte, action: untyped): untyped =
contractAddress: toAddress, contractAddress: toAddress,
codeAddress: toAddress, codeAddress: toAddress,
value: 0.u256, value: 0.u256,
data: data, data: data
code: @[]
) )
computation = newComputation(vmState, message) computation = newComputation(vmState, message)
echo "Running ", action.astToStr, " - ", test["name"] echo "Running ", action.astToStr, " - ", test["name"]

View File

@ -41,18 +41,15 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
var vmState = newBaseVMState(emptyRlpHash, header, newBaseChainDB(newMemoryDB())) var vmState = newBaseVMState(emptyRlpHash, header, newBaseChainDB(newMemoryDB()))
let fexec = fixture["exec"] let fexec = fixture["exec"]
var code: seq[byte]
vmState.mutateStateDB: vmState.mutateStateDB:
setupStateDB(fixture{"pre"}, db) setupStateDB(fixture{"pre"}, db)
let address = fexec{"address"}.getStr.parseAddress let address = fexec{"address"}.getStr.parseAddress
code = db.getCode(address).toSeq
vmState.txContext( vmState.txContext(
origin = fexec{"origin"}.getStr.parseAddress, origin = fexec{"origin"}.getStr.parseAddress,
gasPrice = fexec{"gasPrice"}.getHexadecimalInt gasPrice = fexec{"gasPrice"}.getHexadecimalInt
) )
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
@ -62,8 +59,7 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
contractAddress: toAddress, contractAddress: toAddress,
codeAddress: toAddress, codeAddress: toAddress,
value: cast[uint64](fexec{"value"}.getHexadecimalInt).u256, # Cast workaround for negative value value: cast[uint64](fexec{"value"}.getHexadecimalInt).u256, # Cast workaround for negative value
data: fexec{"data"}.getStr.hexToSeqByte, data: fexec{"data"}.getStr.hexToSeqByte
code: code
) )
var computation = newComputation(vmState, message) var computation = newComputation(vmState, message)