Repository navigation
Feat: stacks abi host interface - #7904
brice-stacks wants to merge 8 commits into
Conversation
bf1cf1c to
5c01ac0
Compare
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.
5c01ac0 to
72d6577
Compare
|
This is rebased on |
aaronb-stacks
left a comment
There was a problem hiding this comment.
Just a couple comments
| name: "tenure_miner_address", | ||
| group: BlockData, | ||
| params: &[p("height", U128), p("out", Buf)], | ||
| result: ResultKind::Len, |
There was a problem hiding this comment.
Why does tenure_miner_address just return len and not a status (same question for burn_block_pox_addr)?
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
Does key get counted as part of the written bytes? Or does this assume that the key is getting hashed in any event?
There was a problem hiding this comment.
Addressed in 0ec17e0. Comments now explain that it's a fixed size hash.
| /// `len`: the number of bytes written to a `buf`. | ||
| Len, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
ResultKind::LenOrNone now makes this more clear.
| name: "kv_get", | ||
| group: Storage, | ||
| params: &[p("key", Bytes), p("out", Buf)], | ||
| result: ResultKind::Len, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good call. Now "none" is represented by i32::MIN.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
5ac6002 to
b93c6b6
Compare
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-abicrate 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. Astatusresult carries the codes of the function's type instacks_abi::status.HostFunction::wasm_paramsandResultKind::wasm: the expanded Wasm type of an import, for deploy validation and linking.host::LEN_NONEandResultKind::LenOrNone: the "none" result of alenfunction that has one (ABI 6.1), the leasti32, 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
.watimport 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, andOutput(per function);Read,Write,ReadBytes, andWrittenBytes(storage, shared); andFrameEntries(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
stx_burn(6.1) andcall(5.3).Basefirst, each term names a parameter of the right kind, and every variable-length input and output has its per-byte term.Debugrendering, 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-unknownthrough the existingstacks-abiCI steps.