Exemplo n.º 1
0
def test_fuzzy_is_valid_opcode(opcodes, data):
    if opcodes:
        indices, bytecode_sections = zip(*_mk_bytecode(opcodes, data))
        bytecode = b''.join(bytecode_sections)
    else:
        indices = set()
        bytecode = b''

    valid_indices = set(indices)

    stream = CodeStream(bytecode)

    index_st = st.integers(min_value=0, max_value=len(bytecode) + 10)
    to_check = data.draw(st.lists(index_st, max_size=len(bytecode)))
    for index in to_check:
        is_valid = stream.is_valid_opcode(index)
        expected = index in valid_indices
        assert is_valid is expected
Exemplo n.º 2
0
    def __init__(self, state: BaseState, message: Message,
                 transaction_context: BaseTransactionContext) -> None:

        self.state = state
        self.msg = message
        self.transaction_context = transaction_context

        self._memory = Memory()
        self._stack = Stack()
        self._gas_meter = self.get_gas_meter()

        self.children = []
        self.accounts_to_delete = {}
        self._log_entries = []

        code = message.code
        self.code = CodeStream(code)
Exemplo n.º 3
0
def test_harder_is_valid_opcode():
    code_stream = CodeStream(b'\x02\x03\x72' + (b'\x04' * 32) + b'\x05')
    # valid: 0 - 2 :: 22 - 35
    # invalid: 3-21 (PUSH19) :: 36+ (too long)
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is True
    assert code_stream.is_valid_opcode(3) is False
    assert code_stream.is_valid_opcode(21) is False
    assert code_stream.is_valid_opcode(22) is True
    assert code_stream.is_valid_opcode(35) is True
    assert code_stream.is_valid_opcode(36) is False
Exemplo n.º 4
0
def test_even_harder_is_valid_opcode():
    test = b'\x02\x03\x7d' + (b'\x04' * 32) + b'\x05\x7e' + (
        b'\x04' * 35) + b'\x01\x61\x01\x01\x01'
    code_stream = CodeStream(test)
    # valid: 0 - 2 :: 33 - 36 :: 68 - 73 :: 76
    # invalid: 3 - 32 (PUSH30) :: 37 - 67 (PUSH31) :: 74, 75 (PUSH2) :: 77+ (too long)
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is True
    assert code_stream.is_valid_opcode(3) is False
    assert code_stream.is_valid_opcode(32) is False
    assert code_stream.is_valid_opcode(33) is True
    assert code_stream.is_valid_opcode(36) is True
    assert code_stream.is_valid_opcode(37) is False
    assert code_stream.is_valid_opcode(67) is False
    assert code_stream.is_valid_opcode(68) is True
    assert code_stream.is_valid_opcode(71) is True
    assert code_stream.is_valid_opcode(72) is True
    assert code_stream.is_valid_opcode(73) is True
    assert code_stream.is_valid_opcode(74) is False
    assert code_stream.is_valid_opcode(75) is False
    assert code_stream.is_valid_opcode(76) is True
    assert code_stream.is_valid_opcode(77) is False
Exemplo n.º 5
0
def test_get_item_returns_correct_opcode():
    code_stream = CodeStream(b'\x01\x02\x30')
    assert code_stream.__getitem__(0) == opcode_values.ADD
    assert code_stream.__getitem__(1) == opcode_values.MUL
    assert code_stream.__getitem__(2) == opcode_values.ADDRESS
Exemplo n.º 6
0
def test_STOP_opcode_is_returned_when_bytecode_end_is_reached():
    code_stream = CodeStream(b'\x01\x02')
    next(code_stream)
    next(code_stream)
    assert next(code_stream) == opcode_values.STOP
Exemplo n.º 7
0
def test_next_returns_the_correct_next_opcode():
    code_stream = CodeStream(b'\x01\x02\x30')
    assert next(code_stream) == opcode_values.ADD
    assert next(code_stream) == opcode_values.MUL
    assert next(code_stream) == opcode_values.ADDRESS
Exemplo n.º 8
0
def test_code_stream_rejects_invalid_code_byte_values(code_bytes):
    with pytest.raises(ValidationError):
        CodeStream(code_bytes)
Exemplo n.º 9
0
def test_code_stream_accepts_bytes():
    code_stream = CodeStream(b'\x01')
    assert len(code_stream.stream.getvalue()) == 1
Exemplo n.º 10
0
def test_get_item_returns_correct_opcode():
    code_stream = CodeStream(b'\x01\x02\x30')
    assert code_stream[0] == opcode_values.ADD
    assert code_stream[1] == opcode_values.MUL
    assert code_stream[2] == opcode_values.ADDRESS
Exemplo n.º 11
0
def test_code_stream_accepts_bytes():
    code_stream = CodeStream(b'\x02')
    assert len(code_stream) == 1
    assert code_stream[0] == 2