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

# How to Test the Credible Block Guard

> Test marker ordering, paid-call rejection, and the fail-open boundary on a local Anvil chain.

Test that a guarded operation succeeds after a marker in the same block, rejects an unmarked block, and becomes available after the configured block-gap threshold.

## Prerequisites

* Access to [`credible-registry`](https://github.com/phylaxsystems/credible-registry), with its submodules checked out
* `anvil`, `cast`, `forge`, and `jq` on your `PATH`
* A free local port `8545`, or another port selected with `RPC_PORT`

The guard and test harness live in `credible-registry`, not `credible-std`. These commands use revision `8804c9e`:

```shell theme={null}
git clone --recurse-submodules git@github.com:phylaxsystems/credible-registry.git
cd credible-registry
git checkout 8804c9ece0b9ed2580fee85eaaf8b17dafc1de71
git submodule update --init --recursive
```

## Run the Fixture

From the Registry repository root, run:

```shell theme={null}
./examples/protocol-guard/script/test-protocol-guard.sh
```

The script starts a fresh Anvil chain, deploys the Registry, marks an initial block, and deploys `GuardedCounter`. The initial marker is required by the guard constructor. This tests the fixture, not your protocol's upgrade.

To use another port:

```shell theme={null}
RPC_PORT=9545 ./examples/protocol-guard/script/test-protocol-guard.sh
```

The fixture uses a fixed threshold of 10 blocks. `FAIL_OPEN_THRESHOLD` and `--expected-threshold` change the runner's expectation, not the deployed contract. Use a custom target to test a different threshold.

## Test Your Contract

The runner cannot reuse a live-network address or another Anvil instance. Supply deployment commands that run after its local chain starts:

1. Deploy a Registry with `MARKER_ADDRESS` authorized as a builder. Print the Registry address as the command's final output line.
2. Deploy or upgrade your target with `REGISTRY_ADDRESS`. Set up balances, roles, and approvals, then print the target or proxy address as the final line.
3. Specify the guarded call, a state-reading call, and the expected state before and after success.

This runnable example uses the repository's `GuardedName` contract:

```shell theme={null}
./examples/protocol-guard/script/test-protocol-guard.sh \
  --registry-deploy-command 'cd "$REPO_ROOT" && forge create src/CredibleRegistry.sol:CredibleRegistry --rpc-url "$RPC_URL" --private-key "$ADMIN_KEY" --broadcast --json --constructor-args "$ADMIN_ADDRESS" "$ADMIN_ADDRESS" "[$MARKER_ADDRESS]" | jq -r ".deployedTo"' \
  --target-deploy-command 'cd "$REPO_ROOT" && forge create examples/protocol-guard/src/GuardedName.sol:GuardedName --rpc-url "$RPC_URL" --private-key "$ADMIN_KEY" --broadcast --json --constructor-args "$REGISTRY_ADDRESS" | jq -r ".deployedTo"' \
  --guarded-call 'setName(string)' \
  --guarded-call-arg 'hello [world]' \
  --state-read-call 'name()(string)' \
  --state-before '""' \
  --state-after '"hello [world]"'
```

Replace the target deployment and call details with your protocol's equivalents. Pass each function argument with a separate `--guarded-call-arg`; use `--state-read-call-arg` for getter arguments. String state values must include the quotes returned by `cast call`, as above.

Commands receive `RPC_URL`, `REPO_ROOT`, `REGISTRY_ADDRESS`, and `EXPECTED_THRESHOLD`, plus local test keys and addresses. The runner marks the Registry before invoking the target command. Use these keys only on the local test chain.

The runner checks `credibleRegistry()(address)` and `failOpenBlockThreshold()(uint256)` by default. For different getters, use `--registry-read-call` and `--threshold-read-call`. For targets without getters, supply `--config-assert-command`; it must exit successfully only when both settings match.

## Verify the Results

| Case | Required result |
| - | - |
| Marker queued before a guarded call, then mined in the same block | Both succeed; protocol state changes as expected. |
| Paid guarded call in an unmarked block while the marker is recent | `NonCredibleBlock()`; protected state is unchanged. |
| Block gap equals the threshold | The call still reverts. |
| Block gap exceeds the threshold | The call succeeds through fallback. |

The harness disables automining for the ordering test. Sending both transactions with automining enabled would put them in different blocks.

The fixture and example above report the following summary. Require zero failures and a zero exit status; a custom configuration check changes the check count.

```text theme={null}
Summary: 14 passed, 0 failed
```

## Test Simulation Separately

The Registry accepts zero-gas-price calls in nonzero blocks without a marker. The harness uses `--gas-price 1gwei` for its rejection probe so it tests paid-call behavior. In Foundry tests, set a nonzero `vm.txGasPrice` when testing guard rejection.

A successful wallet simulation is not proof of production protection. Monitor `lastCredibleBlock()` or marker events for actual marker activity. See [zero-gas-price calls](https://docs.phylax.systems/credible/credible-layer-contracts.md#zero-gas-price-calls).

The harness does not test the Credible RPC, real builder enforcement, assertion activation, Safe modules, or storage-layout compatibility. Complete those checks in your [integration test plan](https://docs.phylax.systems/credible/testing-strategy.md).


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