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

Commit 9ac4219

Browse files
vubvub
authored andcommitted
Simplified transactions.py
1 parent f75ff85 commit 9ac4219

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

ethereum/state_transition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def rp(what, actual, target):
237237
if tx.startgas < intrinsic_gas:
238238
raise InsufficientStartGas(rp('startgas', tx.startgas, intrinsic_gas))
239239

240-
log_tx.debug('TX NEW', tx_dict=tx.log_dict(abbrev=True))
240+
log_tx.debug('TX NEW', tx_dict=tx.to_dict())
241241
# start transacting #################
242242
state.increment_nonce(tx.sender)
243243

ethereum/transactions.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ethereum import opcodes
1010
from ethereum import utils
1111
from ethereum.slogging import get_logger
12-
from ethereum.utils import TT256, mk_contract_address, zpad, int_to_32bytearray, big_endian_to_int, ecsign, ecrecover_to_pub
12+
from ethereum.utils import TT256, mk_contract_address, zpad, int_to_32bytearray, big_endian_to_int, ecsign, ecrecover_to_pub, normalize_key
1313

1414

1515
log = get_logger('eth.chain.tx')
@@ -57,18 +57,13 @@ def __init__(self, nonce, gasprice, startgas, to, value, data, v=0, r=0, s=0):
5757
self.data = None
5858

5959
to = utils.normalize_address(to, allow_blank=True)
60-
assert len(to) == 20 or len(to) == 0
60+
6161
super(Transaction, self).__init__(nonce, gasprice, startgas, to, value, data, v, r, s)
62-
self.logs = []
6362
self.network_id = None
6463

6564
if self.gasprice >= TT256 or self.startgas >= TT256 or \
6665
self.value >= TT256 or self.nonce >= TT256:
6766
raise InvalidTransaction("Values way too high!")
68-
if self.startgas < self.intrinsic_gas_used:
69-
raise InvalidTransaction("Startgas too low")
70-
71-
log.debug('deserialized tx', tx=encode_hex(self.hash)[:8])
7267

7368
@property
7469
def sender(self):
@@ -108,59 +103,42 @@ def sign(self, key, network_id=None):
108103
109104
A potentially already existing signature would be overridden.
110105
"""
111-
if key in (0, '', b'\x00' * 32, '0' * 64):
112-
raise InvalidTransaction("Zero privkey cannot sign")
113106
if network_id is None:
114107
rawhash = utils.sha3(rlp.encode(self, UnsignedTransaction))
115108
else:
116109
assert 1 <= network_id < 2**63 - 18
117-
rlpdata = rlp.encode(rlp.infer_sedes(self).serialize(self)[:-3] + [big_endian_to_int(network_id), '', ''])
110+
rlpdata = rlp.encode(rlp.infer_sedes(self).serialize(self)[:-3] + [big_endian_to_int(network_id), b'', b''])
118111
rawhash = utils.sha3(rlpdata)
119112

120-
if len(key) == 64:
121-
# we need a binary key
122-
key = encode_privkey(key, 'bin')
113+
key = normalize_key(key)
123114

124115
self.v, self.r, self.s = ecsign(rawhash, key)
125116
if network_id is not None:
126117
self.v += 8 + network_id * 2
127118

128-
self.sender = utils.privtoaddr(key)
119+
self._sender = utils.privtoaddr(key)
129120
return self
130121

131122
@property
132123
def hash(self):
133124
return utils.sha3(rlp.encode(self))
134125

135-
def log_bloom(self):
136-
"returns int"
137-
bloomables = [x.bloomables() for x in self.logs]
138-
return bloom.bloom_from_list(utils.flatten(bloomables))
139-
140-
def log_bloom_b64(self):
141-
return bloom.b64(self.log_bloom())
142-
143126
def to_dict(self):
144-
# TODO: previous version used printers
145127
d = {}
146128
for name, _ in self.__class__.fields:
147129
d[name] = getattr(self, name)
148-
d['sender'] = self.sender
149-
d['hash'] = encode_hex(self.hash)
150-
return d
151-
152-
def log_dict(self, abbrev=False):
153-
d = self.to_dict()
154-
d['sender'] = encode_hex(d['sender'] or '')
155-
d['to'] = encode_hex(d['to'])
156-
d['data'] = encode_hex(d['data']) if len(d['data']) < 2500 or not abbrev else "data<%d>" % len(d['data'])
130+
if name in ('to', 'data'):
131+
d[name] = '0x' + encode_hex(d[name])
132+
d['sender'] = '0x' + encode_hex(self.sender)
133+
d['hash'] = '0x' + encode_hex(self.hash)
157134
return d
158135

159136
@property
160137
def intrinsic_gas_used(self):
161138
num_zero_bytes = str_to_bytes(self.data).count(ascii_chr(0))
162139
num_non_zero_bytes = len(self.data) - num_zero_bytes
163140
return (opcodes.GTXCOST
141+
+ (0 if self.to else opcodes.CREATE[3])
164142
+ opcodes.GTXDATAZERO * num_zero_bytes
165143
+ opcodes.GTXDATANONZERO * num_non_zero_bytes)
166144

@@ -198,9 +176,3 @@ def check_low_s_homestead(self):
198176

199177

200178
UnsignedTransaction = Transaction.exclude(['v', 'r', 's'])
201-
202-
203-
def contract(nonce, gasprice, startgas, endowment, code, v=0, r=0, s=0):
204-
"""A contract is a special transaction without the `to` argument."""
205-
tx = Transaction(nonce, gasprice, startgas, '', endowment, code, v, r, s)
206-
return tx

ethereum/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ def normalize_address(x, allow_blank=False):
207207
raise Exception("Invalid address format: %r" % x)
208208
return x
209209

210+
def normalize_key(key):
211+
if len(key) == 32:
212+
o = key
213+
elif len(key) == 64:
214+
o = decode_hex(key)
215+
elif len(key) == 66 and key[:2] == '0x':
216+
o = decode_hex(key[2:])
217+
elif is_numeric(key):
218+
o = encode_int32(key)
219+
else:
220+
raise Exception("Invalid key format: %r" % key)
221+
if o == b'\x00' * 32:
222+
raise Exception("Zero privkey invalid")
223+
return o
210224

211225
def zpad(x, l):
212226
return b'\x00' * max(0, l - len(x)) + x

0 commit comments

Comments
 (0)