2018-01-15 18:42:40 +00:00
|
|
|
import
|
|
|
|
../logging, ../constants, ../validation
|
|
|
|
|
|
|
|
type
|
|
|
|
Message* = ref object
|
|
|
|
# A message for VM computation
|
|
|
|
|
|
|
|
# depth = None
|
|
|
|
|
|
|
|
# code = None
|
|
|
|
# codeAddress = None
|
|
|
|
|
|
|
|
# createAddress = None
|
|
|
|
|
|
|
|
# shouldTransferValue = None
|
|
|
|
# isStatic = None
|
|
|
|
|
|
|
|
# logger = logging.getLogger("evm.vm.message.Message")
|
|
|
|
|
|
|
|
gas*: Int256
|
|
|
|
gasPrice*: Int256
|
2018-01-31 12:57:05 +00:00
|
|
|
to*: string
|
|
|
|
sender*: string
|
2018-01-15 18:42:40 +00:00
|
|
|
value*: Int256
|
2018-01-29 17:40:22 +00:00
|
|
|
data*: seq[byte]
|
2018-01-31 12:57:05 +00:00
|
|
|
code*: string
|
|
|
|
internalOrigin: string
|
|
|
|
internalCodeAddress: string
|
2018-01-30 18:12:05 +00:00
|
|
|
depth*: int
|
2018-01-31 12:57:05 +00:00
|
|
|
internalStorageAddress: string
|
2018-01-15 18:42:40 +00:00
|
|
|
shouldTransferValue*: bool
|
|
|
|
isStatic*: bool
|
2018-01-16 17:05:20 +00:00
|
|
|
isCreate*: bool
|
|
|
|
|
|
|
|
MessageOptions* = ref object
|
2018-01-31 12:57:05 +00:00
|
|
|
origin*: string
|
2018-01-30 18:12:05 +00:00
|
|
|
depth*: int
|
2018-01-31 12:57:05 +00:00
|
|
|
createAddress*: string
|
|
|
|
codeAddress*: string
|
2018-01-16 17:05:20 +00:00
|
|
|
shouldTransferValue*: bool
|
|
|
|
isStatic*: bool
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc `origin=`*(message: var Message, value: string) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalOrigin = value
|
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc `codeAddress=`*(message: var Message, value: string) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalCodeAddress = value
|
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc `storageAddress=`*(message: var Message, value: string) =
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalStorageAddress = value
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc newMessageOptions*(
|
2018-01-31 12:57:05 +00:00
|
|
|
origin: string = "",
|
2018-01-30 18:12:05 +00:00
|
|
|
depth: int = 0,
|
2018-01-31 12:57:05 +00:00
|
|
|
createAddress: string = "",
|
|
|
|
codeAddress: string = "",
|
2018-01-16 17:05:20 +00:00
|
|
|
shouldTransferValue: bool = true,
|
|
|
|
isStatic: bool = false): MessageOptions =
|
|
|
|
|
|
|
|
result = MessageOptions(
|
|
|
|
origin: origin,
|
|
|
|
depth: depth,
|
|
|
|
createAddress: createAddress,
|
|
|
|
codeAddress: codeAddress,
|
|
|
|
shouldTransferValue: shouldTransferValue,
|
|
|
|
isStatic: isStatic)
|
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
proc newMessage*(
|
|
|
|
gas: Int256,
|
|
|
|
gasPrice: Int256,
|
2018-01-31 12:57:05 +00:00
|
|
|
to: string,
|
|
|
|
sender: string,
|
2018-01-15 18:42:40 +00:00
|
|
|
value: Int256,
|
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-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
new(result)
|
|
|
|
result.gas = gas
|
|
|
|
result.gasPrice = gasPrice
|
|
|
|
|
|
|
|
if to != CREATE_CONTRACT_ADDRESS:
|
|
|
|
validateCanonicalAddress(to, title="Message.to")
|
|
|
|
result.to = to
|
|
|
|
|
|
|
|
validateCanonicalAddress(sender, title="Message.sender")
|
|
|
|
result.sender = sender
|
|
|
|
|
|
|
|
result.value = value
|
|
|
|
|
|
|
|
result.data = data
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
if not options.origin.isNil:
|
|
|
|
validateCanonicalAddress(options.origin, title="Message.origin")
|
|
|
|
result.internalOrigin = options.origin
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
validateGte(options.depth, minimum=0, title="Message.depth")
|
|
|
|
result.depth = options.depth
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
result.code = code
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
if not options.createAddress.isNil:
|
|
|
|
validateCanonicalAddress(options.createAddress, title="Message.storage_address")
|
|
|
|
result.storageAddress = options.createAddress
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
if not options.codeAddress.isNil:
|
|
|
|
validateCanonicalAddress(options.codeAddress, title="Message.code_address")
|
|
|
|
result.codeAddress = options.codeAddress
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
result.shouldTransferValue = options.shouldTransferValue
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
result.isStatic = options.isStatic
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc origin*(message: Message): string =
|
|
|
|
if not message.internalOrigin.len == 0:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalOrigin
|
|
|
|
else:
|
|
|
|
message.sender
|
|
|
|
|
|
|
|
proc isOrigin*(message: Message): bool =
|
|
|
|
message.sender == message.origin
|
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc codeAddress*(message: Message): string =
|
|
|
|
if not message.internalCodeAddress.len == 0:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalCodeAddress
|
|
|
|
else:
|
|
|
|
message.to
|
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc `storageAddress`*(message: Message): string =
|
|
|
|
if not message.internalStorageAddress.len == 0:
|
2018-01-15 18:42:40 +00:00
|
|
|
message.internalStorageAddress
|
|
|
|
else:
|
|
|
|
message.to
|
|
|
|
|
|
|
|
proc isCreate(message: Message): bool =
|
|
|
|
message.to == CREATE_CONTRACT_ADDRESS
|