RAILGUN Reloaded
    Preparing search index...

    Module @railgun-reloaded/snapshot

    Source: dev 73c4f88

    @railgun-reloaded/snapshot

    A package to create snapshot of railgun historical events

    • Canonical artifact: a single brotli-compressed blob containing a DAG-CBOR snapshot root object. Its CIDv1 is the root of a UnixFS file of the exact compressed .rsnap bytes: 1 MiB raw leaves, balanced layout with at most 174 links per node, multihash sha2-256.
    • Root schema [WIP](subject to change like FAFO-TXID)
    • The in-memory root object is encoded with deterministic DAG-CBOR.
    • The DAG-CBOR bytes are then brotli-compressed into the .rsnap artifact.
    • Consumers fetch the artifact bytes, recompute the UnixFS CID, decompress, DAG-CBOR decode, and validate the root schema.
    • The artifact is a UnixFS file because one raw block above 2 MiB cannot move between IPFS peers. The 1 MiB leaves can.
    • A gateway returns the artifact bytes for /ipfs/<artifact-cid>. Pass them to decodeArtifact or decodeSnapshot, which recompute the CID from the bytes.
    • Kubo gives the same CID: ipfs add --cid-version=1 --raw-leaves=true --chunker=size-1048576 --hash=sha2-256 --trickle=false --inline=false.
    • stable encoding: single DAG-CBOR implementation plus deterministic brotli settings
    • Structural validation: entryCount must match, endHeight ≥ startHeight, block numbers are unique and ascending, transaction indexes are unique and ascending within each block, commitment entries are contiguous in Merkle insertion order, and maps use string keys only. Action batches preserve source order.
    • Identity: the UnixFS root CIDv1 of the exact compressed .rsnap bytes; any mutation changes the CID.

    There is a single artifact format. The snapshot root carries version 1, and the artifact CID is the UnixFS root CID of the exact compressed .rsnap bytes. Consumers reject any other root version with UnsupportedSnapshotVersionError (unsupported version <n>).

    The root is a DAG-CBOR map with the keys version, chainID, startHeight, endHeight, entryCount and blocks. Each entry of blocks is the EVMBlock model of @railgun-reloaded/scanner, as the scanner formatter makes it from a rainder evmBlocksConnection node. A block read from an artifact deep-equals the block that CustomGQLProvider yields for the same node.

    • Uint8Array is a CBOR byte string.
    • bigint fields are number and timestamp of a block, preimage.value, amount and fee of Unshield, fee of ShieldCommitment, and unshieldValue. The codec writes a CBOR integer up to 2^64 - 1 and CBOR tag 2 above. DAG-CBOR decodes an integer below 2^53 as a number. The decoder converts it back to bigint.
    • Other numbers (index, treeNumber, treePosition, batchStartTreePosition, utxoTreeIn, utxoTreeOut, utxoBatchStartPositionOut, eventLogIndex, recordIndex, callIndex) are CBOR integers. Each is a safe non-negative integer.
    • An absent optional field is an absent key. The encoder writes an undefined field as an absent key.
    • The decoder rejects an unknown key, an unknown actionType, a missing required field and a value of the wrong type.
    • The decoder encodes the decoded snapshot again and compares the bytes. It rejects content that is not in canonical DAG-CBOR form, for example a float in an integer field or a map key out of order.
    Item Rule
    actionType A scanner ActionType value: ShieldCommitment, GeneratedCommitment, Unshield, TransactCommitment, EncryptedCommitment. The value selects the fields of the action.
    32 bytes Block hash, transaction hash, commitment hash, txID, nullifiers, boundParamsHash, npk, token id and tokenSubID, shieldKey, blindedSenderViewingKey, blindedReceiverViewingKey, ephemeralKeys, unshieldCommitment, verificationHash, merkleRoot.
    20 bytes Transaction from, to of Unshield, tokenAddress, unshieldToAddress.
    16 bytes iv and tag of a ciphertext.
    Any length annotationData, memo, ciphertext data words, encryptedBundle words, encryptedRandom words.
    Transact recordIndex and verificationHash are required. merkleRoot and callIndex are optional. unshieldCommitment, unshieldToAddress, unshieldToken and unshieldValue are present if and only if hasUnshield is true.
    ShieldCommitment.fee Optional.
    Block hash, transaction from Optional. An artifact built from the squid entities does not hold them.
    unshieldToAddress A 20-byte recipient address. The scanner uses the low 160 bits of the unshield npk. Regenerate artifacts that contain 32-byte recipients.

    The scanner formatter drops these values, so the artifact does not hold them: merkleRoot, a null Shield fee, the word boundaries of a V1 memo, the hex form and its length, and the rainder action type names.

    The test fixture test/fixtures/evm-blocks.json holds the nodes that the rainder read API served for Sepolia blocks 10,288,431 and 10,289,393. The tests format them with CustomGQLProvider. They cover an Unshield, two Transact records of one chain transaction (the second with an unshield) and a Shield. No V1 action is in the fixture.

    Three rules exist for the tree number and the position:

    1. The contract. In RailgunSmartWallet.sol of the contract repository, Transact (line 117) emits the values of getInsertionTreeNumberAndStartingIndex (Commitments.sol). These values are after the rollover. Shield (line 49) emits Commitments.treeNumber and Commitments.nextLeafIndex. These values are before the rollover.
    2. rainder. insertionBatchPosition applies the rollover rule to the event values of Shield and Transact. For Shield the rule is required. For Transact the rule does nothing: after a rollover the start is 0, and 0 plus the count does not exceed 65,536.
    3. The old codec. It rewrote treePosition of 65,536 or more with a modulo. This rule is removed. rainder writes positions below 65,536.

    Decision: the artifact stores the values that rainder writes and the scanner formatter keeps. The codec checks the order of the commitments and does not change a value. The first commitment in a new tree must be at position 0. The codec does not check how many positions the old tree leaves free, because the artifact has no event boundary: rainder puts each transaction of one Transact event in its own action array.

    Not verified: which deployed contract versions emitted the values above, the root check at the Ethereum rollover (tree 0 to 1, near block 21,332,114), and the hosted squid that shows tree 1 starting at position 1. Each needs a live RPC or squid call.

    npm install @railgun-reloaded/snapshot
    

    Producer-side encoder for callers that hold scanner EVMBlock values and want the compressed .rsnap artifact bytes.

    The platform-neutral encodeSnapshot resolves to compressed bytes after the browser-safe brotli runtime is ready. It canonicalizes block and transaction order, preserves action-batch order, validates the artifact before returning bytes, and can be imported directly — there is no setup step.

    import {
    encodeSnapshot,
    artifactCIDFromBytes
    } from "@railgun-reloaded/snapshot";
    import type { EncodeSnapshotMetadata } from "@railgun-reloaded/snapshot";

    async function main() {
    const metadata: EncodeSnapshotMetadata = {
    chainID: 1,
    startHeight: 14693013n,
    endHeight: 18500000n
    };

    const bytes = await encodeSnapshot(blocks, metadata); // Uint8Array (.rsnap bytes)
    const cid = await artifactCIDFromBytes(bytes);

    console.log("Artifact CID:", cid);
    }

    main().catch(console.error);
    import { restoreSnapshot } from "@railgun-reloaded/snapshot";

    async function main() {
    const restoredData = await restoreSnapshot("ethereum-snapshot.rsnap");

    if (restoredData) {
    console.log("Latest synced height:", restoredData.latestHeight);
    console.log("Number of events:", restoredData.events?.length || 0);
    console.log("Sample event:", restoredData.events?.[0]);
    }
    }

    main().catch(console.error);
    Latest synced height: 18500000
    Number of events: 15420
    Sample event: {
    blockNumber: 14737692n,
    blockHash: "0x1234...",
    transactionHash: "0x5678...",
    // ... other event data
    }

    MIT

    RailgunDB
    UnsupportedSnapshotVersionError
    NetworkName
    getNetworkConfigFromChainID
    rawCIDFromDigestBytes
    artifactCIDFromBytes
    decodeArtifact
    encodeSnapshot
    encodeSnapshotFromDB
    EncodeSnapshotMetadata
    Snapshot
    NETWORK_CONFIG