mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-13 05:44:40 +00:00
768307d91d
It is common for many accounts to share the same code - at the database level, code is stored by hash meaning only one copy exists per unique program but when loaded in memory, a copy is made for each account. Further, every time we execute the code, it must be scanned for invalid jump destinations which slows down EVM exeuction. Finally, the extcodesize call causes code to be loaded even if only the size is needed. This PR improves on all these points by introducing a shared CodeBytesRef type whose code section is immutable and that can be shared between accounts. Further, a dedicated `len` API call is added so that the EXTCODESIZE opcode can operate without polluting the GC and code cache, for cases where only the size is requested - rocksdb will in this case cache the code itself in the row cache meaning that lookup of the code itself remains fast when length is asked for first. With 16k code entries, there's a 90% hit rate which goes up to 99% during the 2.3M attack - the cache significantly lowers memory consumption and execution time not only during this event but across the board.
119 lines
4.5 KiB
Nim
119 lines
4.5 KiB
Nim
# Nimbus
|
|
# Copyright (c) 2018-2024 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 unittest2, sequtils,
|
|
../nimbus/evm/internals
|
|
|
|
proc codeStreamMain*() =
|
|
suite "parse bytecode":
|
|
test "accepts bytes":
|
|
let codeStream = CodeStream.init("\x01")
|
|
check(codeStream.len == 1)
|
|
|
|
|
|
# quicktest
|
|
# @pytest.mark.parametrize("code_bytes", (1010, '1010', True, bytearray(32)))
|
|
# def test_codeStream_rejects_invalid_code_byte_values(code_bytes):
|
|
# with pytest.raises(ValidationError):
|
|
# CodeStream(code_bytes)
|
|
|
|
test "next returns the correct opcode":
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream.next == Op.ADD)
|
|
check(codeStream.next == Op.MUL)
|
|
check(codeStream.next == Op.ADDRESS)
|
|
|
|
|
|
test "peek returns next opcode without changing location":
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream.pc == 0)
|
|
check(codeStream.peek == Op.ADD)
|
|
check(codeStream.pc == 0)
|
|
check(codeStream.next == Op.ADD)
|
|
check(codeStream.pc == 1)
|
|
check(codeStream.peek == Op.MUL)
|
|
check(codeStream.pc == 1)
|
|
|
|
|
|
test "stop opcode is returned when end reached":
|
|
var codeStream = CodeStream.init("\x01\x02")
|
|
discard codeStream.next
|
|
discard codeStream.next
|
|
check(codeStream.next == Op.STOP)
|
|
|
|
# Seek has been dommented out for future deletion
|
|
# test "seek reverts to original position on exit":
|
|
# var codeStream = CodeStream.init("\x01\x02\x30")
|
|
# check(codeStream.pc == 0)
|
|
# codeStream.seek(1):
|
|
# check(codeStream.pc == 1)
|
|
# check(codeStream.next == Op.MUL)
|
|
# check(codeStream.pc == 0)
|
|
# check(codeStream.peek == Op.ADD)
|
|
|
|
test "[] returns opcode":
|
|
let codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream[0] == Op.ADD)
|
|
check(codeStream[1] == Op.MUL)
|
|
check(codeStream[2] == Op.ADDRESS)
|
|
|
|
test "isValidOpcode invalidates after PUSHXX":
|
|
var codeStream = CodeStream.init("\x02\x60\x02\x04")
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(not codeStream.isValidOpcode(2))
|
|
check(codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(4))
|
|
|
|
|
|
test "isValidOpcode 0":
|
|
var codeStream = CodeStream.init(@[2.byte, 3.byte, 0x72.byte].concat(repeat(4.byte, 32)).concat(@[5.byte]))
|
|
# valid: 0 - 2 :: 22 - 35
|
|
# invalid: 3-21 (PUSH19) :: 36+ (too long)
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(21))
|
|
check(codeStream.isValidOpcode(22))
|
|
check(codeStream.isValidOpcode(35))
|
|
check(not codeStream.isValidOpcode(36))
|
|
|
|
|
|
test "isValidOpcode 1":
|
|
let test = @[2.byte, 3.byte, 0x7d.byte].concat(repeat(4.byte, 32)).concat(@[5.byte, 0x7e.byte]).concat(repeat(4.byte, 35)).concat(@[1.byte, 0x61.byte, 1.byte, 1.byte, 1.byte])
|
|
var codeStream = CodeStream.init(test)
|
|
# valid: 0 - 2 :: 33 - 36 :: 68 - 73 :: 76
|
|
# invalid: 3 - 32 (PUSH30) :: 37 - 67 (PUSH31) :: 74, 75 (PUSH2) :: 77+ (too long)
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(32))
|
|
check(codeStream.isValidOpcode(33))
|
|
check(codeStream.isValidOpcode(36))
|
|
check(not codeStream.isValidOpcode(37))
|
|
check(not codeStream.isValidOpcode(67))
|
|
check(codeStream.isValidOpcode(68))
|
|
check(codeStream.isValidOpcode(71))
|
|
check(codeStream.isValidOpcode(72))
|
|
check(codeStream.isValidOpcode(73))
|
|
check(not codeStream.isValidOpcode(74))
|
|
check(not codeStream.isValidOpcode(75))
|
|
check(codeStream.isValidOpcode(76))
|
|
check(not codeStream.isValidOpcode(77))
|
|
|
|
|
|
test "right number of bytes invalidates":
|
|
var codeStream = CodeStream.init("\x02\x03\x60\x02\x02")
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(codeStream.isValidOpcode(4))
|
|
check(not codeStream.isValidOpcode(5))
|