Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

253 Commits

Folders and files

Repository files navigation

Anoma Kind Trees

Crates

The kind trees the Anoma protocol adapters are committed to — one per chain per environment — and the supported-token list they are built from. The anoma-risc0-kind-trees crate computes the roots the way the compliance circuit does, so one source answers what every protocol adapter must store.

The documentation states what the repository contains and why the protocol needs it.

How it fits together

A kind tree maps kinds, written as (logic_ref, label_ref), to kind bases. Its root — the Merkle root that arm-risc0 builds over the entries — is what a protocol adapter stores via setKindTreeRoot and what every compliance proof that takes a kind from the tree reproduces. A chain's tree is derived: the generic call entry from the recorded forwarder, and the ERC20 entries of each supported token from the recorded ERC20 forwarder. The padding kind has no entry: nothing converts padding resources and their speed does not matter, so the compliance circuit computes their kind base, and the trees do not depend on the arm-risc0 release (ADR-0008). The ERC20 entries of one token form a fungibility domain: every listed circuit version under the forwarder's label and, for a token marked for soft migration on a chain that ran a legacy protocol adapter, the legacy ERC20 forwarder's logic ref under its own label, all assigned the kind of the active version under the label of the environment's forwarder, so their resources are fungible. The aliases are always entries. The active version keeps its own kind, which the circuit computes without the tree, so it has an entry only if the token sets precompute_kind_base: the circuit then checks a Merkle path to its kind base instead of computing it by hash to curve. Every entry is machine-checked — an alias against its fungibility domain, every other entry against its own kind — and no kind base is authored.

A Solana cluster's tree is built the same way from different records. Each supported SPL token mint is one fungibility domain under the label sha256(forwarder program id ‖ mint), with every listed SPL token circuit version as a member. An environment gets a Solana tree for each cluster on which the deployment record of anoma-pa-solana-client holds its protocol adapter and tokens.json lists a mint, and the forwarder program comes from the deployment record of anomapay-spl-token-forwarder-client. Both clients come from crates.io. circuit-versions.json lists the logic ref of each anomapay-solana-resource release. Solana has no generic call forwarder and no legacy protocol adapter, so its trees have neither entry (ADR-0010).

Layout

crates/kind-trees/             the library: the types, the root, the token list and the embedded trees
├── data/
│   ├── tokens.json            authored: the supported tokens, per chain
│   ├── circuit-versions.json  authored: the ERC20 and SPL token circuit versions, once for every chain
│   ├── migrated-chains.json   authored: the migrated chains of each environment
│   └── generated/
│       ├── staging/           <namespace>_<reference>.json trees + roots.json
│       └── production/
└── src/
crates/generator/              the generator, not published: writes data/generated/ from the inputs and the deployment records
crates/integration-test/       on-chain token, forwarder, migrated chain and Solana cluster checks, promotion freshness, version and soft-migration gates, arm-risc0 cross-check
docs/adr/                      the decisions behind this layout

Every chain-keyed file — tokens.json, roots.json — is keyed by the chain's CAIP-2 chain ID, such as eip155:11155111 for Sepolia, so that a chain of another family than EVM, such as Solana, has a key too. A Solana cluster's reference is the first 32 characters of its base58 genesis hash: devnet is solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1. Each section carries the chain name in a _comment the loaders ignore. A generated tree is named for the same ID with the colon replaced by _, such as eip155_11155111.json, because cargo package refuses a colon in a file name (ADR-0009). It is otherwise anoma-rm-risc0's kind tree schema, so init_kind_tree_from_file reads one unchanged. The lookups tree, root and tokens::on take a Caip2ChainId, an alloy_chains::NamedChain or a SolanaCluster.

The generator and the tests read the deployments as they read the EVM bindings: the program ids from the clients' addresses (protocol_adapter_deployments_map, spl_token_forwarder_address), and the deployed programs through their contract instances (protocol_adapter, spl_token_forwarder). A consumer that only reads trees needs neither client.

The arm feature converts an Entry into anoma-rm-risc0's KindTreeEntry, so a prover loads a recorded tree with init_kind_tree_from_entries, without a file.

Entries

An entry carries its kind, the kind base it is assigned, and a _metadata object naming what the kind belongs to. The root covers logic_ref, label_ref and kind_base only, so _metadata is free to carry whatever a reviewer needs and never moves the root. version is the version of the circuit crate that owns logic_ref, read from the resolved dependency graph, so bumping a pin cannot leave a stale version behind. type names the resource the kind belongs to: ERC20Resource, SPLTokenResource, or GenericCallResource. An SPLTokenResource entry carries mint and forwarder as base58 addresses where an ERC20 entry carries token and forwarder.

{
  "_metadata": {
    "type": "ERC20Resource",
    "version": "2.0.0",
    "name": "WETH",
    "token": "0x4200000000000000000000000000000000000006",
    "forwarder": "0xE54182d915dE447deFc4A17Ec1D4E0dc627551F7"
  },
  "logic_ref": "bc12323668c37c3d381ca798f11116f35fb1639d12239b29da7810df3985e7ad",
  "label_ref": "55008fad9bfccef776960bfca715e365cfba843cfb947190fafc69e4d9fac674",
  "kind_base": "047494d66fd5abf01f4eb01d204f9c288db95873e6696cc0c5e143855d2a36000fa2c0ba7484f59110ffdfdd546e064c595d0ab979f09c48c26285f3aba5dc2471"
}

That entry is the active member of the WETH fungibility domain on Base Sepolia. A fungibility domain is one forwarder holding one token, and every listed circuit version has a member under that forwarder's label, except a version that only the legacy forwarders accept, all carrying the same kind_base. The kind base is the kind of the active version under that label, so every deprecated version's entry is an alias of it; a version outside the fungibility domain is fungible with nothing. The active version's entry assigns its own kind, which the circuit computes anyway, so it is listed only because WETH sets precompute_kind_base. Generic call has no fungibility domain, and its entry assigns its own kind for the same reason: the circuit reads its kind base instead of computing it.

An alias also says what it is a second name for. Its alias_of names the kind it takes its kind base from, so a reviewer reads it without recomputing anything, and its status is deprecated: a member is active if and only if it has no alias_of. When version 3.0.0 becomes active, 2.0.0 becomes deprecated, every kind base moves to 3.0.0's kind, and the WETH entry for 2.0.0 becomes this:

{
  "_metadata": {
    "type": "ERC20Resource",
    "version": "2.0.0",
    "name": "WETH",
    "token": "0x4200000000000000000000000000000000000006",
    "forwarder": "0xE54182d915dE447deFc4A17Ec1D4E0dc627551F7",
    "status": "deprecated",
    "alias_of": {
      "version": "3.0.0",
      "logic_ref": "<the transfer logic ref of version 3.0.0>",
      "label_ref": "55008fad9bfccef776960bfca715e365cfba843cfb947190fafc69e4d9fac674"
    }
  },
  "logic_ref": "bc12323668c37c3d381ca798f11116f35fb1639d12239b29da7810df3985e7ad",
  "label_ref": "55008fad9bfccef776960bfca715e365cfba843cfb947190fafc69e4d9fac674",
  "kind_base": "<3.0.0's kind under this label, the same as the new 3.0.0 entry's>"
}

Members are never authored. A circuit release is one appended line in crates/kind-trees/data/circuit-versions.json:

{
  "ERC20Resource": [
    { "version": "2.0.0", "logic_ref": "bc12323668c37c3d381ca798f11116f35fb1639d12239b29da7810df3985e7ad", "status": "deprecated" },
    { "version": "3.0.0", "logic_ref": "<the transfer logic ref of version 3.0.0>", "status": "active" }
  ]
}

The generator then writes one more member per token on every chain that has the token, so neither a token nor a chain can be left out, and checks every version whose crate is pinned against the logic ref that crate compiles to. A deprecated version leaves the list only when no resource of it exists under the label of a recorded forwarder. Until then it stays, even when the protocol adapter denies its logic ref (ADR-0008). A version marked legacy_forwarders_only has members only under the labels of the legacy forwarders, for the tokens that soft-migrate.

Workflows

Adding a token, listing a circuit version and recording a legacy ERC20 forwarder all change a chain's entries, and therefore change its root. A transaction built against the new tree verifies only after that chain's protocol adapter stores the new root, so all of them end in Update the deployed roots.

Add a token

  1. Add the token to crates/kind-trees/data/tokens.json, under the CAIP-2 chain ID of the chain it lives on. An ERC20 token records address and soft_migrate_from_legacy_forwarder; an SPL token records mint (base58) and has no such flag. Every token records precompute_kind_base: true gives its active kind an entry, so proofs of its resources check a Merkle path instead of computing the kind base by hash to curve. Each time the entry count passes a power of two, the tree gains a level, and every Merkle path a proof checks gets one more hash. Set it for the tokens that most transactions carry.
  2. Run just generate.
  3. Commit the edited file together with the regenerated trees.

The token validation test runs on every pull request and push. It calls an ERC20 contract and checks that it reports the recorded symbol, name and decimals. A mint reports only its decimals on chain, so for an SPL token it checks that the mint account exists with the recorded decimals; symbol and name are review context.

Add a circuit version

Listing a version makes its resources fungible with every other listed version's, behind the same forwarder, one for one. Nothing passes from one version to another: both kinds are assigned one kind base. Exactly one version is active — under the label of the environment's forwarder it keeps its own kind, and the backend creates its resources — and it must be the highest listed; every other version is deprecated, an alias of the active one, which the backend only consumes. Making a version active moves every ERC20 kind base on every chain to its kind. Review the change as carefully as a permission to create new tokens.

The generator pins the crate of the active version and the crate of the legacy circuit, and checks that the logic ref each compiles to is listed. Any other deprecated version stays listed and is checked against nothing.

  1. Pin the release in [workspace.dependencies] in Cargo.toml in place of the previous one:
    anomapay_erc20_library = "=2.0.0-rc.1"
  2. If the release has a new crate name, use it in crates/generator/Cargo.toml, and in versions() and pinned_erc20_circuits in crates/generator/src/main.rs.
  3. In crates/kind-trees/data/circuit-versions.json, append the version with "status": "active" and set the previous active version to "deprecated".
  4. Run just generate. Read the diff: one new entry per token on every chain, and every older version's entries now alias the new one.
  5. Commit the edited files together with the regenerated trees.

Old resources leave through the new version once the forwarder accepts it, which is an upgrade in the forwarder repository. The promotion gate reads each chain's forwarder and requires that the logic ref it accepts is listed.

An SPL token circuit release appends its version to SPLTokenResource in circuit-versions.json. The generator pins no SPL token circuit crate, so it checks this list by the list rules only, and no check compares the active version's logic ref with the circuit. On Solana the forwarder accepts the new logic ref after its upgrade, whose reinitialize installs it; the promotion gate reads the forwarder's config account for it.

Alias the legacy ERC20 forwarder

On a chain that ran a legacy protocol adapter, the ERC20 forwarder changes once: the legacy ERC20 forwarder is immutable, and the environment's forwarder is a proxy at a new address. Both accept the same logic ref, so a legacy-forwarder resource differs from a resource of the environment's forwarder only in the forwarder inside its label. On each migrated chain of an environment, the generator writes one member under the legacy forwarder's label for each token the list marks for soft migration, an alias of the active version under the label of the environment's forwarder. A migrated chain is one on which the environment's protocol adapter copied the legacy state. A chain whose protocol adapter starts fresh holds no legacy-forwarder resource, so its tree has no such member.

  1. Record the legacy ERC20 forwarder, with the logic ref it accepts, in the legacy array of the forwarder repository's deployments.json, and release the forwarder bindings.
  2. In crates/kind-trees/data/migrated-chains.json, list the chain under each environment whose protocol adapter copies the legacy state. The generator rejects a listed chain that records no legacy ERC20 forwarder. An integration test checks each recorded protocol adapter on a chain with a legacy ERC20 forwarder: it denies the legacy logic ref for created resources if and only if the list names its chain.
  3. In crates/kind-trees/data/tokens.json, set soft_migrate_from_legacy_forwarder to true for every token whose legacy-forwarder resources must be fungible with those of the environment's forwarder, and to false for the rest. A token set to false, or missing from the list, gets no member under the legacy forwarder's label, so its legacy-forwarder resources stay in the legacy forwarder. Nothing checks this.
  4. Pin the new bindings and run just generate. Read the diff: one member under the legacy forwarder's label per marked token on each migrated chain whose environment records its ERC20 forwarder.
  5. Commit the edited files together with the regenerated trees.

A member under the legacy forwarder's label lets a legacy-forwarder resource unwrap from the environment's forwarder. Install the tree while the protocol adapter is paused, and unpause it only after the legacy forwarder's tokens moved to the environment's forwarder (ADR-0008).

Update the deployed roots

  1. Merge into next.
  2. Read each chain's root from crates/kind-trees/data/generated/<environment>/roots.json.
  3. Install it with the protocol adapter repo's contracts-*-kind-tree-root-* recipes, which call setKindTreeRoot. On Solana, the adapter's owner calls set_kind_tree_root, through the Solana protocol adapter repo's dev.sh set-kind-tree-root --cluster <cluster> with PA_KIND_TREE_ROOT set.
  4. Open the promotion pull request into staging or main. It passes only when every protocol adapter of that environment already stores the root this source generates. The soft-migration gate also proves, on a fork of Sepolia, that a legacy-forwarder resource converts and leaves through the environment's forwarder, so the environment's forwarder must already hold the tokens moved from the legacy forwarder.

Verifying

just crates-fmt-check && just crates-build && just crates-lint && just generate-check && just crates-test

The tests read ALCHEMY_API_KEY from the environment (or .env, see .env-example). The Solana checks use the public cluster RPC unless SOLANA_RPC_URL_DEVNET or SOLANA_RPC_URL_MAINNET_BETA names another.

The e2e tests fork E2E_CHAIN_ID (Sepolia by default) with Foundry's anvil and prove through the queue at QUEUE_BASE_URL with QUEUE_AUTH_TOKEN. CI runs them only on a promotion pull request:

just crates-test-e2e

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages