Skip to content

Commit 601b824

Browse files
committed
Small cleanup, read bytes when you know you'll need them
1 parent 7e1f288 commit 601b824

File tree

6 files changed

+26
-17
lines changed

6 files changed

+26
-17
lines changed

eth/vm/computation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,16 @@ def memory_write(self, start_position: int, size: int, value: bytes) -> None:
246246

247247
def memory_read(self, start_position: int, size: int) -> memoryview:
248248
"""
249-
Read and return ``size`` bytes from memory starting at ``start_position``.
249+
Read and return a view of ``size`` bytes from memory starting at ``start_position``.
250250
"""
251251
return self._memory.read(start_position, size)
252252

253+
def memory_read_bytes(self, start_position: int, size: int) -> bytes:
254+
"""
255+
Read and return ``size`` bytes from memory starting at ``start_position``.
256+
"""
257+
return self._memory.read_bytes(start_position, size)
258+
253259
#
254260
# Gas Consumption
255261
#
@@ -447,12 +453,11 @@ def add_log_entry(
447453
self,
448454
account: Address,
449455
topics: List[int],
450-
data: Union[bytes, memoryview]) -> None:
456+
data: bytes) -> None:
451457
validate_canonical_address(account, title="Log entry address")
452458
for topic in topics:
453459
validate_uint256(topic, title="Log entry topic")
454-
validate_is_bytes_or_view(data, title="Log entry data")
455-
data = bytes(data)
460+
validate_is_bytes(data, title="Log entry data")
456461
self._log_entries.append(
457462
(self.transaction_context.get_next_log_counter(), account, topics, data))
458463

eth/vm/logic/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def log_XX(computation: BaseComputation, topic_count: int) -> None:
3030
)
3131

3232
computation.extend_memory(mem_start_position, size)
33-
log_data = computation.memory_read(mem_start_position, size)
33+
log_data = computation.memory_read_bytes(mem_start_position, size)
3434

3535
computation.add_log_entry(
3636
account=computation.msg.storage_address,

eth/vm/logic/memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def mload(computation: BaseComputation) -> None:
3232

3333
computation.extend_memory(start_position, 32)
3434

35-
value = computation.memory_read(start_position, 32)
36-
computation.stack_push(bytes(value))
35+
value = computation.memory_read_bytes(start_position, 32)
36+
computation.stack_push(value)
3737

3838

3939
def msize(computation: BaseComputation) -> None:

eth/vm/logic/sha3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ def sha3(computation: BaseComputation) -> None:
1212

1313
computation.extend_memory(start_position, size)
1414

15-
sha3_bytes = computation.memory_read(start_position, size)
15+
sha3_bytes = computation.memory_read_bytes(start_position, size)
1616
word_count = ceil32(len(sha3_bytes)) // 32
1717

1818
gas_cost = constants.GAS_SHA3WORD * word_count
1919
computation.consume_gas(gas_cost, reason="SHA3: word gas cost")
2020

21-
result = keccak(bytes(sha3_bytes))
21+
result = keccak(sha3_bytes)
2222

2323
computation.stack_push(result)

eth/vm/logic/system.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def return_op(computation: BaseComputation) -> None:
3232

3333
computation.extend_memory(start_position, size)
3434

35-
output = computation.memory_read(start_position, size)
36-
computation.output = bytes(output)
35+
computation.output = computation.memory_read_bytes(start_position, size)
3736
raise Halt('RETURN')
3837

3938

@@ -42,8 +41,7 @@ def revert(computation: BaseComputation) -> None:
4241

4342
computation.extend_memory(start_position, size)
4443

45-
output = computation.memory_read(start_position, size)
46-
computation.output = bytes(output)
44+
computation.output = computation.memory_read_bytes(start_position, size)
4745
raise Revert(computation.output)
4846

4947

@@ -163,8 +161,8 @@ def __call__(self, computation: BaseComputation) -> None:
163161
computation.stack_push(0)
164162
return
165163

166-
call_data = bytes(
167-
computation.memory_read(stack_data.memory_start, stack_data.memory_length)
164+
call_data = computation.memory_read_bytes(
165+
stack_data.memory_start, stack_data.memory_length
168166
)
169167

170168
create_msg_gas = self.max_child_gas_modifier(

eth/vm/memory.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ def write(self, start_position: int, size: int, value: bytes) -> None:
6161
for idx, v in enumerate(value):
6262
self._bytes[start_position + idx] = v
6363

64-
def read(self, start_position: int, size: int) -> Union[bytes, memoryview]:
64+
def read(self, start_position: int, size: int) -> memoryview:
6565
"""
66-
Read a value from memory.
66+
Return a view into the memory
6767
"""
6868
return memoryview(self._bytes)[start_position:start_position + size]
69+
70+
def read_bytes(self, start_position: int, size: int) -> bytes:
71+
"""
72+
Read a value from memory and return a fresh bytes instance
73+
"""
74+
return bytes(self._bytes[start_position:start_position + size])

0 commit comments

Comments
 (0)