2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-06-17 07:56:39 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2018-04-06 14:52:10 +00:00
|
|
|
# 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.
|
|
|
|
|
2019-11-13 14:49:39 +00:00
|
|
|
import unittest2, sequtils,
|
2024-06-17 07:56:39 +00:00
|
|
|
../nimbus/evm/internals
|
2018-01-31 12:57:05 +00:00
|
|
|
|
2019-09-21 05:45:23 +00:00
|
|
|
proc codeStreamMain*() =
|
|
|
|
suite "parse bytecode":
|
|
|
|
test "accepts bytes":
|
2024-06-21 07:44:10 +00:00
|
|
|
let codeStream = CodeStream.init("\x01")
|
2019-09-21 05:45:23 +00:00
|
|
|
check(codeStream.len == 1)
|
|
|
|
|
|
|
|
|
2019-05-30 16:42:55 +00:00
|
|
|
# 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)
|
2019-09-21 05:45:23 +00:00
|
|
|
|
|
|
|
test "next returns the correct opcode":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
2019-09-21 05:45:23 +00:00
|
|
|
check(codeStream.next == Op.ADD)
|
|
|
|
check(codeStream.next == Op.MUL)
|
|
|
|
check(codeStream.next == Op.ADDRESS)
|
|
|
|
|
|
|
|
|
|
|
|
test "peek returns next opcode without changing location":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
2019-09-21 05:45:23 +00:00
|
|
|
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":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init("\x01\x02")
|
2019-09-21 05:45:23 +00:00
|
|
|
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":
|
2024-06-21 07:44:10 +00:00
|
|
|
# var codeStream = CodeStream.init("\x01\x02\x30")
|
2019-09-21 05:45:23 +00:00
|
|
|
# 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":
|
2024-06-21 07:44:10 +00:00
|
|
|
let codeStream = CodeStream.init("\x01\x02\x30")
|
2019-09-21 05:45:23 +00:00
|
|
|
check(codeStream[0] == Op.ADD)
|
|
|
|
check(codeStream[1] == Op.MUL)
|
|
|
|
check(codeStream[2] == Op.ADDRESS)
|
|
|
|
|
|
|
|
test "isValidOpcode invalidates after PUSHXX":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init("\x02\x60\x02\x04")
|
2019-09-21 05:45:23 +00:00
|
|
|
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":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init(@[2.byte, 3.byte, 0x72.byte].concat(repeat(4.byte, 32)).concat(@[5.byte]))
|
2019-09-21 05:45:23 +00:00
|
|
|
# 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])
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init(test)
|
2019-09-21 05:45:23 +00:00
|
|
|
# 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":
|
2024-06-21 07:44:10 +00:00
|
|
|
var codeStream = CodeStream.init("\x02\x03\x60\x02\x02")
|
2019-09-21 05:45:23 +00:00
|
|
|
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))
|