Files
grin-wallet/libwallet/src/contract/actions
..
2026-09-02 17:31:11 +02:00
2026-09-04 20:47:07 +02:00
2026-09-04 21:00:37 +02:00
2026-09-04 20:47:07 +02:00
2026-09-04 20:47:07 +02:00

Contract actions

The workflow, command behaviour and current limitations are documented in doc/contracts.md. This file tracks implementation details and unfinished work.

Rust implementation

Every contract action on a slate is divided in 3 parts:

  1. compute the new state
  2. save the new state
  3. return slate

Putting this into code, it looks like the following:

// Compute the new state (both of the Slate and the Context)
let (slate, context) = compute(slate, args);
// Atomically commit the new state
contract_utils::save_step(slate, context, ...);
// Return the newly produced slate
return slate;

save_step writes the context, tx log, outputs and input locks in one LMDB batch. The signed transaction is stored separately outside LMDB and cannot share that batch.

Input and output choices are made during setup. The confirmation limit is stored in the local context because it is also used for the fee estimate. add_outputs selects and locks inputs during setup; otherwise this happens during signing. The inputs and outputs are only added to the slate when signing, so the counterparty does not see them earlier.

Ideally we'd also separate side effects out of these functions e.g. computing the current_height or refreshing the outputs with updater::refresh_outputs(...). The current_height could be communicated through a &ChainState parameter which would collect these values before the call. Setup still reads an existing Context again; passing it through would avoid that DB read. Separating side effects until the 'save_step' part would make these functions much easier to test.

TODOs

  • Reserve funds across open late-locked contracts, or make the possible signing failure clearer to callers.
  • Keep contracts at two participants until the known multi-party attack is addressed.
  • For a payjoin, --use-inputs any picks an input for us; naming the commitment would be clearer, for the API as much as the CLI.
  • new has no --no-setup.
  • Add custom fee rates and contract TTLs, or document them as unsupported.
  • Raise the replacement fee when revoke races a transaction already in the mempool.
  • Merge invoice-proof retrieval with the existing payment-proof API.
  • Add a Grin node API for kernel lookup by MMR index, then use the witness index instead of searching by commitment.
  • Align the proof memo encoding with the early payment proofs proposal.
  • Implement sender-nonce proofs for the RSR flow.
  • Move payment-proof creation and verification onto Slate so it can also be used outside contracts and is versioned with the slate format.
  • Keep V5 when decoding Slatepacks through Owner RPC.
  • Preserve kernel features and their arguments across V4 and V5; see #793.
  • Choose the sender derivation path explicitly and keep encrypted outgoing files identifiable locally.
  • Decide whether target_slate_version should control Slatepack output or be removed.
  • Add contract history, lookup by id, transport support and configurable proof memos.
  • Decide whether the foreign JSON-RPC API should expose new and sign.
  • Add full API and RPC examples once the contract interface is stable.
  • Separate side effects out from the computation, as described above.
  • Store transactions in LMDB so they can be committed with the wallet state.

Side-effects

Setup

// Persisting setup::compute() requires verify_not_signed first
// Side-effects:
//  - contract_utils::verify_not_signed -> tx_log_iter
//  - height = w.w2n_client().get_chain_tip()?.0;
//  - maybe_context = w.get_private_context(keychain_mask, sl.id.as_bytes())
//  - create_contract_ctx -> updater::refresh_outputs(wallet, keychain_mask, parent_key_id, false)?;
//  - add_outputs -> let current_height = w.w2n_client().get_chain_tip()?.0;
//  - add_outputs -> add_outputs_to_ctx -> w.next_child_for(...)

Sign

// Side-effects:
//  - contract_utils::verify_not_signed -> tx_log_iter
//  - sign -> w.get_private_context(...)
//  - verify_incoming_own_commitments -> w.iter()
//  - verify_own_commitments -> w.get(...)
//  - everything from 'setup'