2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
2018-07-05 12:41:01 +00:00
|
|
|
eth_common,
|
|
|
|
../logging, ../constants, ../validation, ../vm_types
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc `origin=`*(message: var Message, value: EthAddress) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalOrigin = value
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc `codeAddress=`*(message: var Message, value: EthAddress) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalCodeAddress = value
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc `storageAddress=`*(message: var Message, value: EthAddress) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalStorageAddress = value
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc newMessageOptions*(
|
2018-05-30 16:11:15 +00:00
|
|
|
origin = ZERO_ADDRESS,
|
2018-01-30 18:12:05 +00:00
|
|
|
depth: int = 0,
|
2018-05-30 16:11:15 +00:00
|
|
|
createAddress = ZERO_ADDRESS,
|
|
|
|
codeAddress = ZERO_ADDRESS,
|
2018-08-13 23:47:42 +00:00
|
|
|
flags: MsgFlags = static(emvcNoFlags)): MessageOptions =
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
result = MessageOptions(
|
|
|
|
origin: origin,
|
|
|
|
depth: depth,
|
|
|
|
createAddress: createAddress,
|
|
|
|
codeAddress: codeAddress,
|
2018-08-13 23:47:42 +00:00
|
|
|
flags: flags)
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
proc newMessage*(
|
2018-05-25 10:25:19 +00:00
|
|
|
gas: GasInt,
|
|
|
|
gasPrice: GasInt,
|
2018-05-30 16:11:15 +00:00
|
|
|
to: EthAddress,
|
|
|
|
sender: EthAddress,
|
2018-02-20 17:27:43 +00:00
|
|
|
value: UInt256,
|
2018-01-29 17:40:22 +00:00
|
|
|
data: seq[byte],
|
2018-01-31 12:57:05 +00:00
|
|
|
code: string,
|
2018-01-16 17:05:20 +00:00
|
|
|
options: MessageOptions = newMessageOptions()): Message =
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-07-06 15:08:31 +00:00
|
|
|
validateGte(options.depth, minimum=0, title="Message.depth")
|
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
new(result)
|
|
|
|
result.gas = gas
|
|
|
|
result.gasPrice = gasPrice
|
2018-08-13 23:47:42 +00:00
|
|
|
result.destination = to
|
2018-01-15 18:42:40 +00:00
|
|
|
result.sender = sender
|
|
|
|
result.value = value
|
|
|
|
result.data = data
|
2018-01-16 17:05:20 +00:00
|
|
|
result.depth = options.depth
|
|
|
|
result.storageAddress = options.createAddress
|
|
|
|
result.codeAddress = options.codeAddress
|
2018-08-13 23:47:42 +00:00
|
|
|
result.flags = options.flags
|
2018-07-06 15:08:31 +00:00
|
|
|
result.code = code
|
|
|
|
|
|
|
|
if options.origin != ZERO_ADDRESS:
|
|
|
|
result.internalOrigin = options.origin
|
|
|
|
else:
|
|
|
|
result.internalOrigin = sender
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc origin*(message: Message): EthAddress =
|
|
|
|
if message.internalOrigin != ZERO_ADDRESS:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalOrigin
|
|
|
|
else:
|
|
|
|
message.sender
|
|
|
|
|
|
|
|
proc isOrigin*(message: Message): bool =
|
|
|
|
message.sender == message.origin
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc codeAddress*(message: Message): EthAddress =
|
|
|
|
if message.internalCodeAddress != ZERO_ADDRESS:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalCodeAddress
|
|
|
|
else:
|
2018-08-13 23:47:42 +00:00
|
|
|
message.destination
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc `storageAddress`*(message: Message): EthAddress =
|
|
|
|
if message.internalStorageAddress != ZERO_ADDRESS:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalStorageAddress
|
|
|
|
else:
|
2018-08-13 23:47:42 +00:00
|
|
|
message.destination
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
proc isCreate(message: Message): bool =
|
2018-08-13 23:47:42 +00:00
|
|
|
message.destination == CREATE_CONTRACT_ADDRESS
|