AgentSkillsCN

compact-core:testing-debugging

当您测试Compact合约、调试编译错误、理解诸如“潜在的见证值泄露”或“电路约束未通过”之类的错误信息,搭建TypeScript测试框架,或为单元测试模拟见证函数时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-core:testing-debugging
description: Use when testing Compact contracts, debugging compile errors, understanding error messages like "potential witness-value disclosure" or "circuit constraint failed", setting up TypeScript test harnesses, or mocking witness functions for unit tests.

Testing & Debugging

Essential guidance for testing Compact contracts, debugging common errors, and understanding compiler/runtime error messages.

Common Errors Quick Reference

Error MessageLikely CauseQuick Fix
potential witness-value disclosureWitness flows to public outputAdd disclose() or use commitment
type mismatchIncompatible types in operationCheck type signatures, use as for conversion
unbounded loopLoop bounds not compile-time constantUse literal or #N parameter for bounds
circuit constraint failedRuntime assertion failedCheck assert conditions and inputs
proof generation failedInvalid witness valuesVerify witness function returns valid data
overflowArithmetic exceeds type boundsUse larger Uint<N> or check bounds

Debugging Decision Tree

code
Error during compilation?
├── "potential witness-value disclosure"
│   └── See: references/error-messages.md#disclosure-errors
├── "type mismatch" or "cannot convert"
│   └── See: references/error-messages.md#type-errors
├── "unbounded loop" or "bounds must be constant"
│   └── See: references/error-messages.md#loop-errors
└── Other compilation error
    └── See: references/error-messages.md

Error during proof generation?
├── "assert failed" or "constraint failed"
│   └── Check assertion conditions and witness values
├── "proof generation failed"
│   └── Verify witness functions return expected types
└── "overflow" or "division by zero"
    └── Check arithmetic bounds

Testing Approaches

Unit Testing Circuits

typescript
import { TestContext } from '@midnight-ntwrk/compact-testing';

describe('MyContract', () => {
    let ctx: TestContext;

    beforeEach(async () => {
        ctx = await TestContext.create('my_contract.compact');
    });

    it('should process valid input', async () => {
        const result = await ctx.call('process', [42n]);
        expect(result.success).toBe(true);
    });
});

Witness Mocking

typescript
const mockWitness = {
    get_secret: () => BigInt('12345'),
    get_balance: () => BigInt(1000)
};

const result = await ctx.call('transfer', [recipient], mockWitness);

State Verification

typescript
// Read ledger state after circuit execution
const balance = await ctx.ledger.get('balances', userKey);
expect(balance).toBe(expectedBalance);

Common Pitfalls

PitfallSymptomSolution
Forgetting disclose()Compile errorAdd explicit disclosure
Wrong ADT choiceInefficient proofsUse Counter for counting, MerkleTree for membership
Attempting unbounded loopsCompile errorUse bounded for i in 0..N
Uint overflowProof failureUse larger bit width or check bounds
Division by zeroProof failureAdd zero check before division

References

Examples