Inter-covenant communication with dynamic dispatch

In the KCC-20 discussion, I wrote about open ICC in Argent. One covenant can observe another covenant’s state transition without knowing its concrete implementation at compile time. Both sides agree on a public state shape, and the observer can read fields or constrain how they change.

I’ve been thinking about another kind of interaction. Sometimes what you want from another covenant is an answer to a question about this transaction. What fee applies to this trade? Is this withdrawal authorized? The caller knows the question and the type of answer it expects. The callee can use its own state, witnesses, and rules to check the answer. It can even recreate its UTXO unchanged.

The interface in this case describes a service. That gives us dynamic dispatch over behavior. Someone can write a caller today, and an independently written covenant can provide the service later. The caller can use it through the same interface once it has a way to identify and trust that implementation.

A verification-oriented function call

In a covenant, an operation describes what the script will verify in a proposed transaction. Take a counter increment. The transaction composer supplies a successor output. The counter script reads the old count from the input and checks that the proposed output continues the counter with the expected incremented count. Consensus adds that output to the UTXO set when it accepts the transaction.

The pattern is propose the result, then verify it. We can apply it to a function call between two covenants in the same transaction. The composer supplies the arguments and proposed return value. Both sides check the same record, each according to its role:

  • The caller checks the service, method, and arguments it intended, then uses the result in its own checks.
  • The callee checks that the stated result is correct for those arguments under its policy.

At the language level, the caller passes arguments and receives a result. At the transaction level, both inputs independently verify a fixed transaction. There’s no runtime call stack between them.

The proposed mechanism

Give each service input a section of the transaction payload containing the statements it agrees to verify. Each statement contains an interface method, its arguments, and a proposed result. A small header identifies the statements in that input’s section. The callee verifies the header and every statement it describes.

The caller first resolves the service input. It can usually do that through an existing convention, such as the leader for a known covenant ID, or through an input handle it already resolved. A witness argument then selects a statement from that input’s header. The caller checks that the selected statement matches the method and arguments it needs.

The statement belongs to the service input. Several callers can rely on the same statement when it applies to all of them. If a result is specific to one action, such as an approval for a particular withdrawal, that restriction belongs in the statement itself.

In Argent, I’d express the callee’s check with an unconditional serve assertion. A participating entry with two serve statements expects exactly two records and checks both. An entry with no serve requires an empty section. That way an admin entry, for example, can still run on its own while rejecting a spend that presents it as having served a call.

A DEX example

The Argent DEX example already gives us a way to write the pair logic once and instantiate it for different assets by using the observes pattern to check each asset’s state transition through a statically known state shape. A natural extension is to let each pair also choose a fee policy when it is created.

Two pairs could use the same swap code and trade the same assets. One charges a flat fee. The other charges more when a proposed swap would push its reserves further out of balance. How much more it charges might depend on a rolling volatility estimate that the fee policy updates as new prices come in. The pair asks its policy for the fee for that particular swap, then applies it while enforcing the reserve invariant and the trader’s minimum output. Uniswap v4 has a similar use of dynamic dispatch. A pool can choose a hook that returns a fee override for the current swap. Uniswap v4 hook implementation

One possible Argent syntax would look like this:

interface SwapFee {
    fn quote(Reserves reserves, bool a_to_b, int amount) -> int;
}

// Inside the pair's swap entry, fees_id identifies its SwapFee provider.
int fee_bps = self.fees_id.quote(reserves, a_to_b, amount);

On the other side, an actor declares that it implements the interface. Its actual spending entry can have a different signature:

actor ImbalanceFees owns FeeState implements SwapFee {
    entry settle(QuoteWitness witness) {
        // Calculate the fee from the proposed swap and the policy's own state.
        serve SwapFee.quote as |reserves, a_to_b, amount| =>
            compute_fee(reserves, a_to_b, amount, volatility);
        // Check the policy's own continuation.
    }
}

settle is the entry being spent. quote is the service it fulfills. The compiler binds the lambda’s parameters to the arguments in the payload statement. The lambda can also use values from the enclosing entry, such as volatility. The generated script checks that the value the lambda computes matches the proposed result in the statement. The pair checks that the arguments match the swap it is validating.

The same mechanism can share verification work. An oracle input could verify a signed price report once and serve the checked quote to several vault inputs in the same transaction. Similarly, one input could verify an opening of a KIP-21 sequencing commitment and serve the authenticated timestamp for that block to other inputs. KIP-21 block context commitment

Why this needs a payload convention

The transaction payload is a natural place for these statements. Both inputs can read the same bytes with OpTxPayloadSubstr and check bounds with OpTxPayloadLen. For independently written contracts to compose, they need to agree on exactly which bytes form the statement they are checking.

I think this is where a KCC would help. A starting point would be an opt-in, versioned payload header with a section directory. A call section could have a fixed-width directory indexed by transaction input. Each entry would locate that service input’s header and statement bytes. The per-input header would give the statement count and the boundaries of each record.

That lets the caller locate one record directly and the callee verify its complete list with a known bound. Kaspa Script has no general loops, so the layout and the compiler need to make those checks bounded. KIP-20 scripting patterns

The envelope prefix should be distinguishable from existing payload formats, including those used by encrypted chat and based apps. Other uses, such as event signals for an indexer or RPC subscriber, could have separate sections.

There are further choices around whether a service is promised across a whole covenant ID or only by a specific actor template. Those affect how the caller authenticates the service. The common model is two inputs checking the same statement in one transaction, with a payload convention that lets independent tools put it together.

I’d be interested in other use cases for this and in how people would approach the shared payload format.

4 Likes