Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 8bec792

Browse files
committed
seperate tx validation
1 parent 034a0a8 commit 8bec792

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

ethereum/processblock.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
CREATE_CONTRACT_ADDRESS = b''
2828

2929

30+
def mk_contract_address(sender, nonce):
31+
return utils.sha3(rlp.encode([sender, nonce]))[12:]
32+
33+
3034
def verify(block, parent):
3135
from ethereum import blocks
3236
try:
@@ -71,41 +75,49 @@ def __repr__(self):
7175
(encode_hex(self.address), self.topics, self.data)
7276

7377

74-
def apply_transaction(block, tx):
78+
def intrinsic_gas_used(tx):
79+
num_zero_bytes = str_to_bytes(tx.data).count(ascii_chr(0))
80+
num_non_zero_bytes = len(tx.data) - num_zero_bytes
81+
return (opcodes.GTXCOST
82+
+ opcodes.GTXDATAZERO * num_zero_bytes
83+
+ opcodes.GTXDATANONZERO * num_non_zero_bytes)
84+
85+
86+
def validate_transaction(block, tx):
7587

76-
def rp(actual, target):
77-
return '%r, actual:%r target:%r' % (tx, actual, target)
88+
def rp(what, actual, target):
89+
return '%r: %r actual:%r target:%r' % (tx, what, actual, target)
7890

7991
# (1) The transaction signature is valid;
80-
if not tx.sender:
92+
if not tx.sender: # sender is set and validated on Transaction initialization
8193
raise UnsignedTransaction(tx)
8294

8395
# (2) the transaction nonce is valid (equivalent to the
8496
# sender account's current nonce);
8597
acctnonce = block.get_nonce(tx.sender)
8698
if acctnonce != tx.nonce:
87-
raise InvalidNonce(rp(tx.nonce, acctnonce))
99+
raise InvalidNonce(rp('nonce', tx.nonce, acctnonce))
88100

89101
# (3) the gas limit is no smaller than the intrinsic gas,
90102
# g0, used by the transaction;
91-
num_zero_bytes = str_to_bytes(tx.data).count(ascii_chr(0))
92-
num_non_zero_bytes = len(tx.data) - num_zero_bytes
93-
intrinsic_gas_used = (opcodes.GTXCOST
94-
+ opcodes.GTXDATAZERO * num_zero_bytes
95-
+ opcodes.GTXDATANONZERO * num_non_zero_bytes)
96-
if tx.startgas < intrinsic_gas_used:
97-
raise InsufficientStartGas(rp(tx.startgas, intrinsic_gas_used))
103+
if tx.startgas < intrinsic_gas_used(tx):
104+
raise InsufficientStartGas(rp('startgas', tx.startgas, intrinsic_gas_used))
98105

99106
# (4) the sender account balance contains at least the
100107
# cost, v0, required in up-front payment.
101108
total_cost = tx.value + tx.gasprice * tx.startgas
102109
if block.get_balance(tx.sender) < total_cost:
103-
raise InsufficientBalance(
104-
rp(block.get_balance(tx.sender), total_cost))
110+
raise InsufficientBalance(rp('balance', block.get_balance(tx.sender), total_cost))
105111

106112
# check block gas limit
107113
if block.gas_used + tx.startgas > block.gas_limit:
108-
raise BlockGasLimitReached(rp(block.gas_used + tx.startgas, block.gas_limit))
114+
raise BlockGasLimitReached(rp('gaslimit', block.gas_used + tx.startgas, block.gas_limit))
115+
116+
return True
117+
118+
119+
def apply_transaction(block, tx):
120+
validate_transaction(block, tx)
109121

110122
log_tx.debug('TX NEW', tx=encode_hex(tx.hash), tx_dict=tx.to_dict())
111123
# start transacting #################
@@ -115,11 +127,9 @@ def rp(actual, target):
115127
# buy startgas
116128
assert block.get_balance(tx.sender) >= tx.startgas * tx.gasprice
117129
block.delta_balance(tx.sender, -tx.startgas * tx.gasprice)
118-
119-
message_gas = tx.startgas - intrinsic_gas_used
130+
message_gas = tx.startgas - intrinsic_gas_used(tx)
120131
message_data = vm.CallData([safe_ord(x) for x in tx.data], 0, len(tx.data))
121-
message = vm.Message(tx.sender, tx.to, tx.value, message_gas, message_data,
122-
code_address=tx.to)
132+
message = vm.Message(tx.sender, tx.to, tx.value, message_gas, message_data, code_address=tx.to)
123133

124134
# MESSAGE
125135
ext = VMExt(block, tx)
@@ -250,7 +260,7 @@ def create_contract(ext, msg):
250260
if ext.tx_origin != msg.sender:
251261
ext._block.increment_nonce(msg.sender)
252262
nonce = utils.encode_int(ext._block.get_nonce(msg.sender) - 1)
253-
msg.to = utils.sha3(rlp.encode([sender, nonce]))[12:]
263+
msg.to = mk_contract_address(sender, nonce)
254264
b = ext.get_balance(msg.to)
255265
if b > 0:
256266
ext.set_balance(msg.to, b)

0 commit comments

Comments
 (0)