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-05-07 12:41:54 +00:00
|
|
|
../logging, ../constants, ../validation, stint
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Message* = ref object
|
|
|
|
# A message for VM computation
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
# depth = None
|
|
|
|
|
|
|
|
# code = None
|
|
|
|
# codeAddress = None
|
|
|
|
|
|
|
|
# createAddress = None
|
|
|
|
|
|
|
|
# shouldTransferValue = None
|
|
|
|
# isStatic = None
|
|
|
|
|
|
|
|
# logger = logging.getLogger("evm.vm.message.Message")
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
gas*: UInt256
|
|
|
|
gasPrice*: UInt256
|
2018-01-31 12:57:05 +00:00
|
|
|
to*: string
|
2018-04-06 14:52:10 +00:00
|
|
|
sender*: string
|
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
|
|
|
|
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*(
|
2018-02-20 17:27:43 +00:00
|
|
|
gas: UInt256,
|
|
|
|
gasPrice: UInt256,
|
2018-01-31 12:57:05 +00:00
|
|
|
to: string,
|
|
|
|
sender: string,
|
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-01-15 18:42:40 +00:00
|
|
|
new(result)
|
|
|
|
result.gas = gas
|
|
|
|
result.gasPrice = gasPrice
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
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
|