Cross-app calls
Task: have one app tell, or ask, another app in the same organization.
The protocol side, delivery order, dead letters and the node’s checks, is in Cross-app calls.
1. Admit the caller
Section titled “1. Admit the caller”A method is reachable from another app only if it says so:
/// Records a ping from the app `from`; only `pinger` may call it.#[app::allow(app = "pinger")]pub fn pong(&mut self, from: String) -> app::Result<()> { let env::Caller::App(origin) = env::caller() else { return Err(AppError::forbidden()); }; self.pings.insert(&origin, &from)?; Ok(())}#[app::allow(app = "<app id>")] admits one app; allow(app = "*") admits any
app the node delivers from. The node refuses every other app. Inside the method,
env::caller() is Caller::App(<id>), set by the node, so it cannot be forged:
use it, not an argument, to know who called.
2. Send a message
Section titled “2. Send a message”pub fn ping(&mut self, target: String) -> app::Result<()> { let args = std::collections::BTreeMap::from([("from", env::app_id())]); env::xcall(&target, "pong", &args)?; Ok(())}args is any serde value, sent as the JSON arguments a client would send.
env::xcall(app, method, &args) queues the message in this run’s outbox. The
node delivers it after the entry commits, only if the run succeeds, at least
once and in order per sending app. The target must be in the same
organization: since 1.0.0-rc.20 every app is in one, and an app in an
organization of its own reaches only the other apps there. A run may queue at
most 100.
A message the target answers with an error or a trap is still delivered: that
answer is final. One the node cannot deliver for an hour (or at once, for a
deleted target or one outside the organization) goes to the sender’s dead
letters, and the sender’s onXcallFailed method runs if its ABI has one that
allows the system.
3. Call and read synchronously
Section titled “3. Call and read synchronously”// Inside an `async fn` app method.let args = std::collections::BTreeMap::from([("account", account)]);// A u64 result arrives as a decimal string, like every u64 on the wire.let balance: String = env::xcall_view("ledger", "balance", &args).await?;env::xcall_call::<T>(app, method, &args) and env::xcall_view::<T>(..) run
the target now and decode its JSON result as T. They are futures: a method that
awaits one is an async fn, and futures awaited together with app::join! or
app::join_all run at once (Synchronous calls and
views). A failure is AppError
XcallFailed, carrying the target’s error. Messages, calls and views all reach
only methods that allow this app.
Test it
Section titled “Test it”TestApp keeps the messages a run sent: take_xcalls() returns them and
deliver(..) runs one against another TestApp; mock_app(..) stands in for
the target of a synchronous call (Testing).
Call as the person you serve
Section titled “Call as the person you serve”Every call above reaches the target as your app. To call it as the person your run
serves, so the target applies its own rules for them, your release lists the methods
and the person allows it once: see Build on other apps.
To find which instance to call, name it in your release’s uses and read it with
env::linked.