Skip to content

Add Comprehensive Tests for ContractOp::exec in vm/op_contract.rs#2

Open
astral-bitlight wants to merge 21 commits intozoedberg:0.11.1-3from
astral-bitlight:0.11.1-3
Open

Add Comprehensive Tests for ContractOp::exec in vm/op_contract.rs#2
astral-bitlight wants to merge 21 commits intozoedberg:0.11.1-3from
astral-bitlight:0.11.1-3

Conversation

@astral-bitlight
Copy link

@astral-bitlight astral-bitlight commented May 22, 2025

Description:

This pull request introduces a comprehensive test suite for the exec method of all ContractOp variants located in rgb-core/src/vm/op_contract.rs. The primary goal of this effort was to significantly improve the test coverage for this critical component of the RGB Core virtual machine, ensuring its robustness and verifying its deterministic behavior.

Key Contributions:

  1. New Test Module: A dedicated test module (vm/op_contract/tests.rs) has been created to house all tests related to ContractOp execution.
  2. Mocking Infrastructure:
    • MockContractState: Implements the ContractStateAccess trait, providing a controlled environment for simulating contract state interactions during tests.
    • MockGlobalStateIter: Implements the GlobalStateIter trait, crucial for testing operations that access historical global state (e.g., LdC).
    • TestEnv Helper: A builder struct to simplify the setup of test contexts (CoreRegs, VmContext) and mock operation data (Genesis, Transitions, assignments, global state, metadata).
  3. Exhaustive Test Cases for ContractOp::exec:
    • Each ContractOp variant's exec method has been tested for both successful execution paths and all anticipated failure scenarios (specifically, those triggering the fail!() macro).
    • Tests cover various conditions, including:
      • Correct register modifications on success.
      • ST0 flag being set to false and ExecStep::Stop on failure.
      • Handling of revealed vs. concealed states.
      • Boundary conditions for indices and counts.
      • Correctness of arithmetic and verification operations (e.g., Svs, Sas, Sps, Vts).
      • Interactions with VmContext data (previous state, current operation's state, contract-wide state).
  4. Bytecode and InstructionSet Trait Implementation Tests:
    • Added tests to verify the correctness of instr_range(), instr_byte(), encode_args(), decode(), isa_ids(), src_regs(), dst_regs(), and complexity() for ContractOp. This ensures the integrity of the instruction definitions themselves.

Impact on Coverage:

This work has resulted in a substantial increase in test coverage for the target file and has also positively impacted overall project coverage:

  • vm/op_contract/mod.rs (Target File):
    • Function Coverage: 0.00% -> 100.00%
    • Line Coverage: 0.00% -> 99.69%
    • Region Coverage: 0.00% -> 80.81%
  • Overall Project (Illustrative Increases):
    • Total Line Coverage: Increased from ~7.75% to ~50.12%.
    • Notable improvements were seen in related modules like operation/* and vm/contract.rs due to the comprehensive nature of the test setup. (Refer to the detailed test report for precise figures across all files).

Motivation:

The ContractOp::exec method is central to the RGB Core virtual machine's execution logic. Ensuring its reliability through thorough testing is crucial for the integrity and security of the RGB protocol's consensus layer. This PR aims to provide a high degree of confidence in this foundational component.

@astral-bitlight astral-bitlight marked this pull request as ready for review May 29, 2025 09:09
@astral-bitlight astral-bitlight changed the title AluVM Op tests tests: add comprehensive contract op tests May 29, 2025
@astral-bitlight astral-bitlight changed the title tests: add comprehensive contract op tests Add Comprehensive Tests for ContractOp::exec in vm/op_contract.rs May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant