Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIGHASH opcode #259

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions evm/logic/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,7 @@ def returndatacopy(computation):
value = computation.return_data[returndata_start_position: returndata_start_position + size]

computation.memory.write(mem_start_position, size, value)


def sighash(computation):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an EIP for this? If so, could we link it in the docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't, unfortunately.

computation.stack.push(computation.msg.sig_hash)
1 change: 1 addition & 0 deletions evm/mnemonics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
EXTCODECOPY = 'EXTCODECOPY'
RETURNDATASIZE = 'RETURNDATASIZE'
RETURNDATACOPY = 'RETURNDATACOPY'
SIGHASH = 'SIGHASH'
#
# Block Information
#
Expand Down
1 change: 1 addition & 0 deletions evm/opcode_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
EXTCODECOPY = 0x3c
RETURNDATASIZE = 0x3d
RETURNDATACOPY = 0x3e
SIGHASH = 0x3f


#
Expand Down
5 changes: 5 additions & 0 deletions evm/rlp/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ class BaseShardingTransaction(rlp.Serializable):
def hash(self):
return keccak(rlp.encode(self))

@property
def sig_hash(self):
sedes = self.__class__.exclude('data')
return keccak(rlp.encode(self, sedes))

#
# Validation
#
Expand Down
5 changes: 5 additions & 0 deletions evm/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ def validate_transaction_access_list(access_list, title="Access List"):
def validate_access_list(access_list):
for entry in access_list:
validate_is_bytes(entry, "Access prefix")


def validate_sig_hash(sig_hash, title="Sig Hash"):
validate_is_bytes(sig_hash, title)
validate_length(sig_hash, 32, title)
4 changes: 4 additions & 0 deletions evm/vm/forks/sharding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .computation import ShardingComputation
from .validation import validate_sharding_transaction
from .blocks import ShardingBlock
from .opcodes import SHARDING_OPCODES


def _execute_sharding_transaction(vm, transaction):
Expand Down Expand Up @@ -80,6 +81,7 @@ def _execute_sharding_transaction(vm, transaction):
gas=message_gas,
gas_price=transaction.gas_price,
to=transaction.to,
sig_hash=transaction.sig_hash,
sender=ENTRY_POINT,
value=0,
data=data,
Expand Down Expand Up @@ -170,4 +172,6 @@ def _execute_sharding_transaction(vm, transaction):
# Method overrides
validate_transaction=validate_sharding_transaction,
execute_transaction=_execute_sharding_transaction,
# opcodes
opcodes=SHARDING_OPCODES,
)
27 changes: 27 additions & 0 deletions evm/vm/forks/sharding/opcodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import copy
from cytoolz import merge

from evm import opcode_values
from evm import mnemonics

from evm.opcode import as_opcode
from evm.logic import (
context,
)

from evm.vm.forks.byzantium import BYZANTIUM_OPCODES


NEW_OPCODES = {
opcode_values.SIGHASH: as_opcode(
logic_fn=context.sighash,
mnemonic=mnemonics.SIGHASH,
gas_cost=0,
),
}


SHARDING_OPCODES = merge(
copy.deepcopy(BYZANTIUM_OPCODES),
NEW_OPCODES
)
28 changes: 28 additions & 0 deletions evm/vm/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
validate_uint256,
validate_is_boolean,
validate_access_list,
validate_sig_hash,
)


Expand Down Expand Up @@ -148,6 +149,7 @@ def __init__(self,
gas,
gas_price,
to,
sig_hash,
sender,
value,
data,
Expand Down Expand Up @@ -178,6 +180,32 @@ def __init__(self,
validate_is_boolean(is_create, title="Message.is_create")
self.is_create = is_create

validate_sig_hash(sig_hash, title="Message.sig_hash")
self.sig_hash = sig_hash

if access_list is not None:
validate_access_list(access_list)
self.access_list = access_list

def prepare_child_message(self,
gas,
to,
value,
data,
code,
**kwargs):
kwargs.setdefault('sender', self.msg.storage_address)

child_message = ShardingMessage(
gas=gas,
gas_price=self.msg.gas_price,
origin=self.msg.origin,
sig_hash=self.msg.sig_hash,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a "bad smell".

The sighash falls into the same category as the block data which indicates there should be a single location this value is stored. Yet it is copied for each level of computation.

After some investigation, the same is true for msg.gas_price and msg.origin. This isn't something we need to fix now, but I think it's worth investigating how we can remove this duplication.

Issue opened here: #262

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I must admit that I simply copied the msg.origin behavior.

to=to,
value=value,
data=data,
code=code,
depth=self.msg.depth + 1,
**kwargs
)
return child_message