Skip to content

feat: add @meshsdk/tx-emulator for simulating transactions on a local ledger - #858

Open
lisicky wants to merge 10 commits into
feat/governance-fetcher-interfacesfrom
feat/tx-emulator
Open

lisicky wants to merge 10 commits into
feat/governance-fetcher-interfacesfrom
feat/tx-emulator

Conversation

@lisicky

@lisicky lisicky commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Merge #864 first: this PR builds on its IGovernanceFetcher and IStakePoolFetcher interfaces.

What

Adds @meshsdk/tx-emulator, a local Cardano ledger for trying transactions out, alone or in chains.

  • simulate(tx) reports what a transaction would do without changing anything. submit(tx) applies it, and every later transaction is checked against the result.
  • UTxOs are kept by the Scalus ledger through @meshsdk/scalus-emulator. Transactions are validated by the validator the caller picks: "scalus" (built in) or "cquisitor" (@cardananium/cquisitor-lib, an optional peer dependency).
  • The emulator is itself a Mesh IFetcher, IGovernanceFetcher, IStakePoolFetcher, ISubmitter and IEvaluator, so transactions are built and submitted against it with the usual Mesh tools.
  • TxEmulator.fromChain(tx, …) builds a ledger holding the chain state a transaction reads, from providers, to check the transaction against the chain as it is now.

Contents

packages/mesh-tx-emulator (new):

  • TxEmulator.create(options) sets the starting state:
    • the validator (required) and the network (mainnet by default);
    • protocol parameters replacing the network's, all or some;
    • UTxOs and the starting slot;
    • stake addresses (rewards, deposit, pool and DRep delegation), pools and DReps;
    • governance state: actions, the last enacted action of each purpose, the committee, the treasury and the constitution;
    • known datums.
  • simulate(tx, options?) / submit(tx, options?) take CBOR hex or a TransactionPrototype and return:
    • accepted: txHash, fee, isValid, redeemers (execution units each script used), consumed and produced UTxOs (the collateral and its return for a transaction marked phase-2 invalid), script logs and warnings;
    • rejected: errors, each with its ledger rule and message; with cquisitor every problem, with a hint, locations and the phase.
    • With cquisitor, { requireSignatures: false } checks a transaction before anyone signs it: missing key witnesses are not reported, native scripts pass when they would once signed, and the fee and the size limit are checked as they will be once it is signed.
    • submitTx submits like any Mesh submitter and throws a TxRejectedError carrying the result.
  • State transactions change: UTxOs; stake address registrations, deposits, withdrawn rewards, and pool and DRep delegations; pool registrations and retirements; DRep registrations; governance actions with their deposits; committee hot credentials and resignations; donations. It is read back through the provider interfaces (fetchAccountInfo, fetchPoolState, fetchDRepState, fetchGovernanceProposal, fetchCommitteeState, …) and utxos(filter).
  • Direct state changes, for what the emulator does not compute (rewards, governance outcomes) or to fund a wallet or change a parameter midway: addUtxos / removeUtxos, setAccount / removeAccount, setPool / removePool, setDRep / removeDRep, setGovernanceAction / removeGovernanceAction, setLastEnacted, setCommitteeMember / removeCommitteeMember, setTreasury, setConstitution, setProtocolParameters. A set merges into an existing item (null clears a field) and pays or refunds no deposit.
  • fork(): an independent copy of the whole ledger, so one setup can branch into several scenarios.
  • Time: advanceTo({ slot } | { time } | { epoch }), forward only. Entering an epoch retires the pools due to retire (their deposits go back to their reward accounts), adds the previous epoch's donations to the treasury, and removes the governance actions past their lifetime, returning their deposits. No rewards are paid, and no governance actions are ratified or enacted.
  • TxEmulator.fromChain(tx, { validator, fetcher, governance?, pools?, network? }) reads the outputs the transaction spends and references, the protocol parameters and cost models, and the stake addresses, pools, DReps, governance actions and committee it touches, plus the treasury and the constitution. State no provider reports, or reports in a form that cannot be read, is taken to be as each transaction needs it and listed as a NotChecked warning, never as an error.
  • README covering usage, the two validators and the rules each leaves unchecked, chains, the starting state, direct changes, fromChain, time and limits.

packages/mesh-scalus-emulator: exports the existing toMeshUtxo / fromMeshUtxo converters, so the new package seeds the ledger with them instead of duplicating them. No behaviour change.

Repo wiring:

  • build.yml runs the new package's tests next to the scalus-emulator ones.
  • publish.yml gets a publish job for it.
  • bump-version.sh now covers the package, and the @meshsdk/scalus-emulator dependency pin, which it did not rewrite before.
  • The lockfile adds the workspace package and its dev dependency @cardananium/cquisitor-lib; npm hoists scalus to the root instead of keeping two nested copies.

Validation

npm run build:mesh
npm test --workspace @meshsdk/tx-emulator
npm test --workspace @meshsdk/scalus-emulator
  • @meshsdk/tx-emulator: 122 tests.
    • The emulator suite and the fromChain suite run on both validators: chains of transactions, dry runs, forks, time and epoch boundaries, direct state changes, the provider interfaces, and checks against a chain read through plain and governance-capable fetchers.
    • A cquisitor suite covers the ledger rules only it checks: governance proposals, votes, deposits and guardrails, committee certificates, deposit refunds and unsigned transactions.

@Temasar1 Temasar1 mentioned this pull request Oct 27, 2026
28 tasks
@lisicky
lisicky changed the base branch from main to feat/governance-fetcher-interfaces October 29, 2026 01:06
@nau nau mentioned this pull request Nov 4, 2026
… as the ledger keeps them

- The scalus validator's ledger is rebuilt only when a transaction needs it: stake address
  registrations and delegations are applied in step, and a stake address withdrawn from is
  rebuilt before a transaction that reads it.
- At an epoch boundary, committee members proposed but not sitting lose their hot credentials
  and resignations, as the ledger keeps those only for the sitting committee.
- A governance action read from a provider that reports none of its epochs is taken as open to
  votes and reported as not checked.
EmulatorOptions.poolRegistrations is a union of { params } and { poolId } in Scalus 1.5.0, and the explicit PoolRegistration return type hid which of the two is given. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
A MeshTxBuilder with a fetcher evaluates locally by default since #866, so a test that builds a transaction with a script failing on purpose has to pass evaluator: null. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
…Scalus 1.5.0

Scalus 1.5.0 takes a withdrawal before a deregistration in the same transaction, and takes a pool re-registration as an update with no other deposit, as the ledger does. The two tests no longer differ by validator and the README drops both limits. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
… validators

Scalus 1.5.0 takes the registration of a registered pool as an update with no other deposit, so the fromChain test no longer expects the scalus validator to refuse it. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
The emulator builds only with the EmulatorOptions of Scalus 1.5.0, and the other packages already require ^1.5.0, so its own range of ^1.3.0 let an install resolve a version it does not work with. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
…elegated to

Scalus 1.5.0 refuses a delegation to a pool or a DRep that is not registered, as the ledger does. The test of it now runs under both validators, and the README no longer lists it as a limit of the scalus validator. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.
…udge

Scalus 1.5.0 checks the governance rules, and the DRep delegation of a key-hash stake address withdrawn to, against a governance state of its own. That state cannot be given the governance actions, the last enacted ones, the committee members only proposed or the vote delegations the emulator holds, so with the scalus validator Scalus refused transactions that depend on state set without a transaction or read from a chain. The emulator now lists every rule a refused transaction breaks (ledgerFailures) and does not take a refusal by those rules alone: it checks the scripts itself, which Scalus does not run once a rule is broken, holding each to the units its redeemer declares, and applies the transaction's inputs and outputs to Scalus's ledger. A transaction that breaks another rule too is refused by that rule, also when Scalus reports only one not taken. What Scalus checks of governance without that state, such as a proposal's deposit and return account, stays checked. This belongs in the tx-emulator commit of #858; it is kept apart here until that commit is updated.

This branch has not been deployed

No deployments
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