Skip to content

Implement Automated E2E Test Generator for Game Contracts #45

@elazarg

Description

@elazarg

The current testing strategy relies on "Golden Master" comparisons, which only verify that the compiler output (Solidity code) hasn't changed. It does not verify that the generated game logic works correctly on an actual blockchain. We need to verify that players can join, commit, reveal, and withdraw without reverting, and that security mechanisms (like preventing replay attacks) actually function.

Proposed Solution

Implement a Test Artifact Generator that runs alongside the Solidity compiler. For every .vg game compiled, the system should generate a corresponding Hardhat test script (e.g., VickreyAuction.test.js).

Recommended Toolchain

Since our compiler output is Solidity, we should use the standard JavaScript/TypeScript Ethereum development stack for testing.

  • Runtime Environment: Node.js

  • Why: The industry standard for EVM tooling; allows us to script complex player interactions easily.

  • Test Runner & Development Framework: Hardhat

  • Why: It spins up a local, in-memory Ethereum network ("Hardhat Network") for every test run. It is extremely fast, resets state automatically between tests, and provides detailed stack traces when contracts revert.

  • Client Library: Ethers.js (v5 or v6)

  • Why: The standard library for interacting with smart contracts. We will use it to generate random wallets for players (ethers.Wallet.createRandom()), create cryptographic commitments, and send transactions.

  • Assertion Library: Chai + Hardhat-Chai-Matchers

  • Why: Extends standard assertions with blockchain-specific checks. Crucial for verifying failure states (e.g., expect(tx).to.be.revertedWith("bad reveal")) and event emissions.

  • Solidity Compiler: solc (managed via Hardhat)

  • Why: Hardhat automatically downloads and uses the correct solc version specified in the contract's pragma, ensuring our tests match production behavior.

Implementation Plan

  1. Update Build Config: Ensure our project can run npx hardhat test. Add hardhat, ethers, and chai to package.json dev dependencies.
  2. Create Test Generator (HardhatTest.kt):
  • Input: GameIR (Internal Representation of the game).
  • Output: A string containing valid JavaScript code.
  1. Generator Logic:
  • Setup: Generate a beforeEach block that deploys the contract using ethers.getContractFactory.
  • Scenario Generation: Walk the ActionDag (the game's dependency graph) to generate a sequence of valid moves ("Happy Path").
  • Cryptographic Helpers: Inject a JS helper function to generate the commitment hashes off-chain (matching the solidity keccak256 logic) so the tests can successfully submit "hidden" moves.
  1. Execution: The CI pipeline will run:
  2. ./vegas compile (Generates .sol and .test.js)
  3. npx hardhat test (Executes the generated tests against the local Hardhat Network)

Definition of Done

  • The compiler produces a [GameName].test.js alongside [GameName].sol.
  • Running npx hardhat test successfully compiles the generated Solidity and executes the generated test script.
  • The tests correctly pass for valid game flows and fail if we introduce a known bug (e.g., modifying the contract to reject valid reveals).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions