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 =
new(result)
result.bytes = codeBytes
shallowCopy(result.bytes, codeBytes)
result.pc = 0
result.invalidPositions = initHashSet[int]()
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)
result = generateAddress(c.msg.sender, creationNonce)
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 =
new result
@ -142,10 +142,13 @@ proc newComputation*(vmState: BaseVMState, message: Message, salt=none(Uint256))
result.gasMeter.init(message.gas)
result.touchedAccounts = initHashSet[EthAddress]()
result.suicides = initHashSet[EthAddress]()
result.code = newCodeStream(message.code)
if result.msg.isCreate():
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:
result.host.init(

View File

@ -595,8 +595,7 @@ proc setupCreate(c: Computation, memPos, len: int, value: Uint256, opCode: stati
gas: createMsgGas,
sender: c.msg.contractAddress,
value: value,
data: @[],
code: c.memory.read(memPos, len)
data: c.memory.read(memPos, len)
)
when opCode == Create:
@ -759,10 +758,6 @@ template genCall(callName: untyped, opCode: Op): untyped =
c.gasMeter.returnGas(childGasLimit)
return
let
callData = c.memory.read(memInPos, memInLen)
code = c.getCode(codeAddress)
var childMsg = Message(
kind: callKind,
depth: c.msg.depth + 1,
@ -771,8 +766,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
contractAddress: contractAddress,
codeAddress: codeAddress,
value: value,
data: callData,
code: code.toSeq,
data: c.memory.read(memInPos, memInLen),
flags: flags)
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 =
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:
debug "not enough gas to perform calculation", gas=gas
return
@ -58,8 +47,7 @@ proc setupComputation*(vmState: BaseVMState, tx: Transaction, sender: EthAddress
contractAddress: tx.getRecipient(),
codeAddress: tx.to,
value: tx.value,
data: data,
code: code
data: tx.payload
)
result = newComputation(vmState, msg)

View File

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

View File

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

View File

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