Skip to content

Commit f644421

Browse files
committed
Improve NEAR trigger stub with documented behaviour
1 parent a77318f commit f644421

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

chain/near/src/trigger.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,49 @@ impl MappingTriggerTrait for NearTrigger {
144144
}
145145
}
146146

147+
/// Sentinel header byte returned by [`NearTrigger::to_rust_bytes`] to
148+
/// signal "unsupported chain" to any SDK that attempts to decode a NEAR
149+
/// trigger payload. Chosen as `0xFF` because it is outside the current
150+
/// TLV value-tag range (`0x00`..=`0x09`, see
151+
/// `runtime/wasm/src/rust_abi/types.rs::tags`) and therefore guarantees a
152+
/// deterministic decode failure rather than silent misinterpretation.
153+
const UNSUPPORTED_CHAIN_SENTINEL: u8 = 0xFF;
154+
147155
impl ToRustBytes for NearTrigger {
156+
/// Produce a Rust ABI payload for a NEAR trigger.
157+
///
158+
/// NEAR triggers are **not** yet wired up to the Rust ABI. A real
159+
/// implementation would need to serialise, at minimum:
160+
///
161+
/// - For [`NearTrigger::Block`]: the block header fields (hash,
162+
/// prev_hash, height, timestamp, author, gas_price, total_supply,
163+
/// etc.) plus any chunk metadata the SDK exposes to block handlers.
164+
/// - For [`NearTrigger::Receipt`]: the receipt id, predecessor /
165+
/// receiver account ids, the enclosing block's hash and height, the
166+
/// full action list (`CreateAccount`, `DeployContract`,
167+
/// `FunctionCall`, `Transfer`, `Stake`, `AddKey`, `DeleteKey`,
168+
/// `DeleteAccount`), and the execution outcome (status, gas burnt,
169+
/// tokens burnt, logs, emitted receipt ids).
170+
///
171+
/// None of the above is currently serialised. Instead we emit a
172+
/// single-byte sentinel payload (`[UNSUPPORTED_CHAIN_SENTINEL]`, i.e.
173+
/// `[0xFF]`) that is guaranteed to fail decoding on the SDK side
174+
/// because `0xFF` is not a valid TLV value tag (the tag space is
175+
/// `0x00`..=`0x09`; see `runtime/wasm/src/rust_abi/types.rs::tags`).
176+
///
177+
/// This is intentionally louder than the previous `Vec::new()` stub:
178+
/// an empty payload could be silently round-tripped as "no data", and
179+
/// a Rust subgraph targeting NEAR would index garbage without ever
180+
/// realising something was wrong. The sentinel byte forces a
181+
/// deterministic handler failure at the first byte of the payload.
182+
///
183+
/// There is deliberately no logging call here: `to_rust_bytes` has
184+
/// no `Logger` in scope and threading one through would pollute the
185+
/// trait for every chain. Operators will see the SDK-side decode
186+
/// error surfaced as a mapping failure, which is the loud signal we
187+
/// want.
148188
fn to_rust_bytes(&self) -> Vec<u8> {
149-
// NEAR triggers are not yet supported by the Rust ABI.
150-
// Return empty bytes rather than panicking; the handler will receive no data
151-
// and can decide how to proceed. A Rust subgraph targeting NEAR would need
152-
// to implement NEAR-specific serialization in a future PR.
153-
Vec::new()
189+
vec![UNSUPPORTED_CHAIN_SENTINEL]
154190
}
155191
}
156192

docs/rust-abi-spec.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,13 @@ Total: 156 bytes (fixed-width).
398398

399399
#### 4.7.4 Other Chains
400400

401-
Only Ethereum implements `ToRustBytes` at present. NEAR has a stub
402-
returning `unimplemented!()`. Adding a new chain requires only an impl of
401+
Only Ethereum implements `ToRustBytes` at present. NEAR has a documented
402+
stub in `chain/near/src/trigger.rs` that returns a single-byte
403+
"unsupported chain" sentinel (`0xFF`). The sentinel is deliberately
404+
outside the TLV value-tag range (`0x00`..=`0x09`) so that any SDK that
405+
attempts to decode it will fail deterministically at the first byte
406+
rather than silently treating an empty payload as "no data". Adding a
407+
new chain requires only an impl of
403408
`ToRustBytes for <Chain>::MappingTrigger`; no changes to `rust_abi/` are
404409
required.
405410

@@ -851,8 +856,9 @@ may address:
851856
limitation, not a design decision.
852857

853858
4. **Non-Ethereum chains.** Only Ethereum implements `ToRustBytes`. NEAR
854-
has a stub. Other chains will require a per-chain serialization impl
855-
when they want to opt into Rust mappings.
859+
has a documented sentinel stub (section 4.7.4) that fails decode
860+
deterministically. Other chains will require a per-chain
861+
serialization impl when they want to opt into Rust mappings.
856862

857863
5. **`apiVersion` initial value.** Whether to start at `0.0.1` (own
858864
namespace) or `0.0.8` (after the latest AS version) is a policy

0 commit comments

Comments
 (0)