> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pfbridge.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# How ProofBridge is tested

> The contract suites on both chains, the cross-chain parity checks, the chaos pack, and the end-to-end runs that drive real trades through the relayer

In plain terms: ProofBridge has two copies of every contract, one in
Solidity for Ethereum and one in Rust for Stellar, and a trade only works
if both copies agree. So the testing has three layers:

1. each chain's contracts are tested on their own
2. shared scenario files make both chains run the same cases and reach the
   same result
3. end-to-end runs start both chains locally, deploy everything, and push
   real trades through the relayer from start to finish

## Contract tests on each chain

The contracts live in the public
[proofbridge-contracts](https://github.com/Explore-Beyond-Innovations/proofbridge-contracts)
repository.

| Chain | Tooling | What runs |
| - | - | - |
| Ethereum | Foundry | the full `forge test` suite, plus separate invariant campaigns |
| Stellar | Soroban, `cargo test` | unit tests for each contract, plus an integration suite that runs the contracts together |

Both suites build the real zero-knowledge circuit and prove with it, so the
`Verifier` is tested against real proofs. On Ethereum, CI also checks that
the `Verifier` contract matches the circuit it was generated from.

The **invariant campaigns** on Ethereum make random sequences of calls
(file, rule, settle, cancel, claim, pause) and check after every step that
certain things always hold:

* **funds are conserved**: what each escrow holds equals exactly what it
  owes. A surplus fails the check as surely as a shortfall.
* **the dispute module can always pay its bonds**
* **no finished order keeps an open dispute**

A campaign also fails if it never reached every outcome, so a run can't
pass by never getting to the interesting states.

## The same scenarios on both chains

Two hand-written implementations can drift apart without either one
failing its own tests. To catch that, both chains read the same scenario
files and must produce the same result.

* **Order hashing.** Both chains must compute the same hash for the same
  order, from shared test vectors.
* **Agent policy.** Both chains must allow and refuse the same agent
  actions, with the same cap math.
* **Disputes.** A shared scenario file lists the steps of each dispute.
  Each chain runs them and must reach the same final status, record the
  same event, and send the bond to the same place.
* **Joint outcomes.** Both legs of one trade run side by side: the ad
  chain decides how the trade ends, and the order chain follows by proof.
  Each of 13 scenarios must end in the recorded outcome on both legs, and
  every scenario ends with a funds-conservation check. The scenarios
  include a normal settlement, a cancel, a cancel racing an unlock, a
  settled trade that is then disputed, each dispute ruling, a dispute with
  no ruling, and a replayed leaf.

The joint-outcome sweep uses real co-signatures and real proofs, not test
doubles.

The scenario files are generated by code, not written by hand. On every
relevant change, CI regenerates them and fails if the result differs from
the committed files, so a generator and its output can't drift apart.

## The chaos pack

The chaos pack deliberately breaks things and checks that nobody's money
goes anywhere it shouldn't. Every scenario runs on both chains, against the
real escrow, agent account, policy and dispute contracts. Each new test was
also checked the other way round: the guard it relies on was removed from
the contract, and the test had to fail before it counted.

| | Scenario | What must hold |
| - | - | - |
| C1 | an agent's key is stolen while a trade it locked is open | the thief can only lock within the policy; it can't withdraw, close the ad, change the policy or redirect the payout |
| C2 | an agent tries to lock more than its cap | the lock is refused, and nothing counts against the cap |
| C3 | an agent is revoked mid-trade | new locks are refused at once and forever; the open trade still settles or refunds |
| C4 | a settlement key is rotated mid-trade | the open order stays on the key it was locked with, or moves only with the signer's consent; no ad closes |
| C5 | a dispute is filed against an agent's lock | it runs exactly as for a lock made by hand; the agent can't answer, rule or profit |
| C6 | two disputes on one trade | the second is refused; one bond, paid out once |
| C7 | a cancel races an unlock at the deadline | exactly one wins; funds are never paid twice |
| C8 | a settled trade is then disputed | it can't be disputed or ruled on; a settlement proof overrides any ruling |

The full list, the test names on each chain, and the pack's known limits
are in
[CHAOS.md](https://github.com/Explore-Beyond-Innovations/proofbridge-contracts/blob/main/CHAOS.md).
The chaos tests stub the signature and proof checks so they can focus on
the escrow, policy and dispute logic. The real cryptography is covered by
the joint-outcome sweep and the end-to-end runs below.

## End-to-end runs

These runs start a local Stellar network and a local Ethereum node, deploy
every contract fresh, and drive real trades. Every run covers **both bridge
directions**: an ad on Stellar with the order on Ethereum, and an ad on
Ethereum with the order on Stellar.

### Contracts across chains

The cross-chain run uses the contracts directly, with no relayer. Both
parties register their settlement keys on both chains, a trade is created
and locked, both parties co-sign, the proofs are built, and each side
unlocks on its own chain. It reads token balances on both chains to check
the payouts.

### The relayer

The relayer run starts the relayer and its database in containers, seeds
them, and drives each flow through the relayer's API, the way the app
does. Several flows read balances and contract state on-chain, not only
the relayer's records.

| Flow | What it covers |
| - | - |
| Ad lifecycle | create, fund, withdraw and close an ad, on each chain |
| Trade lifecycle | an order taken from creation to locked |
| Settlement | co-sign, combine the signatures, prove and pay out on both chains |
| Key lifecycle | register a key on both chains with one signature, retire it, revoke it |
| Key rotation | add a second key, retire the first with a grace window; an order signed under the old key settles inside the window, is refused after it, and one under the new key settles |
| Agent runtime | the real agent runtime trades as the maker through an agent account on each chain, including the owner's key registration and emergency stop |
| Dispute lifecycle | a dispute filed on a locked trade that still settles, in both directions; evidence beating a ruling; and three ruled disputes that finalize with no evidence, checking where each bond goes |
| Cancels | a deposit the maker never locks is cancelled and refunded by the relayer alone; a lock refused because the fee changed |
| Soak-profile boot | the relayer refuses to start with an unsafe configuration, then starts every worker and stays healthy |
| Incomplete scan | an ad update the relayer can't fully verify is refused, then the ad is re-synced to the chain |

More flows can be run on demand: a relayer killed mid-trade (the trade
still settles), a long Stellar outage during key registration (the
registration lands afterwards with no re-sign), a cancel when nobody
co-signs, and the order-chain side of each dispute outcome.

## How CI runs them

* **Contracts.** Both contract workflows run on every pull request and every
  push to `main` in the public repository: formatting, build, contract size
  budget, the full test suites, the parity checks, the chaos pack as its own
  named step, and gas and compute-cost baselines for unlocks. The runs are
  public on the
  [contracts Actions page](https://github.com/Explore-Beyond-Innovations/proofbridge-contracts/actions).
* **Everything else.** In the main repository, a pull request's checks start
  when a maintainer labels it. Each stage runs only if the pull request
  touches what it tests. The fast stages run in parallel: the contracts, the
  circuits, the scenario-file check, and the relayer, agent runtime,
  frontend and pod service tests. The two end-to-end runs wait for those and
  run only if none failed.
* **On `main`.** Merges to `main` run the end-to-end workflows again when
  the contracts, circuits or relayer change.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.