Skip to content

Feat: stacks abi host interface - #7904

Open
brice-stacks wants to merge 8 commits into
stacks-network:mainfrom
brice-stacks:feat/stacks-abi-host-interface
Open

brice-stacks wants to merge 8 commits into
stacks-network:mainfrom
brice-stacks:feat/stacks-abi-host-interface

Conversation

@brice-stacks

@brice-stacks brice-stacks commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds the host interface of the chain ABI to stacks-abi: one table of the 66 host functions (chain ABI 6.1, 6.2, R8).

Closes #7877. Extends the stacks-abi crate added in #7887.

What is in it

  • host::HOST_FUNCTIONS (src/host/table.rs): the definition. Each entry has the function's name, group, parameters in the kinds of ABI 6.1, result, and charge formula. A status result carries the codes of the function's type in stacks_abi::status.
  • HostFunction::wasm_params and ResultKind::wasm: the expanded Wasm type of an import, for deploy validation and linking.
  • host::LEN_NONE and ResultKind::LenOrNone: the "none" result of a len function that has one (ABI 6.1), the least i32, so it is never a negated length.
  • host::host_function: lookup by import name.
  • host::IMPORT_MODULE: the module name of ABI 3.2.

The table is written in Rust and is the source of truth; there is no separate definition file to parse or keep in step. Other forms, such as a .wat import block, can be generated from it.

Charge formulas

ABI 9.2 says each host function has a base charge plus a per-byte charge on its inputs and outputs, and that storage is charged per access and per byte, at separate prices for reads and writes. The table states this per function, as terms over eight prices: Base, Input, and Output (per function); Read, Write, ReadBytes, and WrittenBytes (storage, shared); and FrameEntries (shared), charged by the two functions that close a frame for the entries they merge or undo. A storage key or an NFT identifier is charged only as input, since its length does not change what is stored (ABI 8.1). The values of the prices belong to the fuel schedule, which is a later change.

Tests

  • 66 functions, group by group, each with its own valid name.
  • The Wasm expansion of every parameter kind, and the two worked examples of the ABI: stx_burn (6.1) and call (5.3).
  • Every signature is well formed: parameter names, sizes, and status codes that increase from 1.
  • Every charge formula keeps the rules: Base first, each term names a parameter of the right kind, and every variable-length input and output has its per-byte term.
  • Storage reads and writes are charged per byte at separate prices, and no storage key or NFT identifier has a per-byte storage term.
  • The whole table is pinned by a digest of its Debug rendering, so any change to an entry fails a test; once the ABI version is frozen, such a change is a consensus change.

The tests run natively and on wasm32-unknown-unknown through the existing stacks-abi CI steps.

Define the host interface of the chain ABI as one table: for each of the
66 host functions its group, parameters in the kinds of ABI 6.1, result,
status codes and charge formula. Deploy validation, linking, the fuel
schedule and the tools derive from it.

Tests check the Wasm types against ABI 6.1 and the rules every signature
and charge formula must keep.
@brice-stacks
brice-stacks force-pushed the feat/stacks-abi-host-interface branch from 5c01ac0 to 72d6577 Compare November 30, 2026 22:23
@brice-stacks

Copy link
Copy Markdown
Contributor Author

This is rebased on main now that #7887 is merged. It is now ready for review.

@aaronb-stacks aaronb-stacks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple comments

Comment thread crates/stacks-abi/src/host/table.rs Outdated
name: "tenure_miner_address",
group: BlockData,
params: &[p("height", U128), p("out", Buf)],
result: ResultKind::Len,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does tenure_miner_address just return len and not a status (same question for burn_block_pox_addr)?

@brice-stacks brice-stacks Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added ResultKind::LenOrNone and LEN_NONE for these so that it's clear that "none" is possible and distinguishable.

Input("key"),
Input("value"),
Write(1),
WrittenBytes("value"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does key get counted as part of the written bytes? Or does this assume that the key is getting hashed in any event?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 0ec17e0. Comments now explain that it's a fixed size hash.

Comment thread crates/stacks-abi/src/host/mod.rs Outdated
Comment on lines +102 to +103
/// `len`: the number of bytes written to a `buf`.
Len,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This maybe should describe more of the Len result possibilities:

the number of bytes written to the buf, with the short-buffer rule of 5.3; -1 means "none" where the function says so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResultKind::LenOrNone now makes this more clear.

Comment thread crates/stacks-abi/src/host/table.rs Outdated
name: "kv_get",
group: Storage,
params: &[p("key", Bytes), p("out", Buf)],
result: ResultKind::Len,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kv_get can return an ambiguous value here: -1 could mean "not found" or that the supplied buffer was too small, and the stored value was length 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Now "none" is represented by i32::MIN.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment on this testset in general: at the moment, it seems kind of vacuous to pin the whole host table with tight assertion tests, but it seems like something that would be wise to do, because sometime down the line, maybe its less obvious that a change in the host table is a consensus breaking change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I added table_is_pinned test in 66889f8.

The chain ABI now returns "none" from a `len` function as `NONE`,
`-2^31`, instead of `-1` (ABI 6.1). A `len` function returns the negated
length of the value when the `buf` is too short, so `-1` was also the
answer for a 1-byte value read with `cap` 0, and `kv_get` could not tell
a missing entry from one.

- Add `LEN_NONE`, the least `i32`, which no negated length can equal.
- Add `ResultKind::LenOrNone` for the functions that have a "none"
  result, and use it for `sponsor`, `tenure_miner_address`,
  `burn_block_pox_addr`, `kv_get` and `nft_owner`. The other `len`
  functions keep `ResultKind::Len`.
- Test that `LEN_NONE` has no negation, that a function has a `len`
  result exactly when it takes a `buf`, and that those five are the only
  functions with a "none" result.
Closing a frame merges its buffered writes, asset-map entries and events
into the parent, or undoes them, so its work grows with what the frame
holds. Frames a module opens nest without limit, and an entry is merged
again at each enclosing frame that closes, so no price paid when an
entry is recorded can cover its merges.

- Add `Charge::FrameEntries`: the price per frame entry, times the
  entries of the frame the function closes. The schedule has its price
  once, like the storage prices.
- Charge it to `frame_close`, for its own frame, and to `call`, for the
  callee's frame.
- Test that exactly those two functions carry the term.
@CLAassistant

CLAassistant commented Dec 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@brice-stacks
brice-stacks force-pushed the feat/stacks-abi-host-interface branch from 5ac6002 to b93c6b6 Compare December 3, 2026 18:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clarity 8] ABI crate: host-interface table

3 participants