Example: Standalone

One process hosts the actors and asks them itself. No network, no other process. This is how tests, tools and single-process services run, and it is the demo’s standalone role.

The actors

A greeter and a bank account per id, both hosted by route (src/Demo/Greeter.ai, src/Demo/Account.ai in MLambda.Actor.Demo):

data Account = Account { owner :: String, balance :: Int }

data Command = Deposit Int | Withdraw Int | Balance

@route("account/{id}")
instance Actor Account where
  type Message Account = Hosted
  receive ctx acc msg = case msg of
    HostedMsg t payload reply -> applied reply acc (commandOf (S.splitOnChar 32 (bytesText payload)))

instance Initial Account where
  initial params = Account (param "id" params) 0

applied :: Maybe (Reply (Either Fault Bytes)) -> Account -> Command -> Step Account
applied reply acc command = case command of
  Deposit n  -> answered reply (balanceText acc.owner (acc.balance + n)) acc { balance = acc.balance + n }
  Withdraw n -> case lt acc.balance n of
    True  -> answered reply (concatStrs ["refused: ", acc.owner, " has ", showInt acc.balance]) acc
    False -> answered reply (balanceText acc.owner (acc.balance - n)) acc { balance = acc.balance - n }
  Balance    -> answered reply (balanceText acc.owner acc.balance) acc

answered :: Maybe (Reply (Either Fault Bytes)) -> String -> Account -> Step Account
answered reply text acc = Sat.answer reply (Right (textBytes text)) >> continue acc

accountOf :: ActorSystem -> Parameters -> Address Account
accountOf system params = actorOf system params

accountHosting :: Hosting
accountHosting = hostingOf accountOf

Its messages are text (“deposit 100”, “withdraw 30”, “balance”) carried as a payload string, so the same actor can be asked from another process.

The host

localClient :: ActorSystem -> DemoSettings -> Either Fault ActorClient
localClient system s = case startHost system (hostConfig Standalone (settingsSelf s) [] (hostingsOf s)) of
  Right host -> Right (hostClient host)
  Left f     -> Left f

hostingsOf :: DemoSettings -> [Hosting]
hostingsOf s = [greeterHosting (settingsId s), accountHosting]

The scenario

The client asks routes and checks the answers (src/Demo/Scenario.ai):

exchanges :: [Exchange]
exchanges =
  [ Exchange "greeter"       "Ada"          (StartsWith "Hello, Ada!")
  , Exchange "account/alice" "deposit 100"  (Exactly "alice balance 100")
  , Exchange "account/alice" "withdraw 30"  (Exactly "alice balance 70")
  , Exchange "account/alice" "withdraw 500" (Exactly "refused: alice has 70")
  , Exchange "account/bob"   "deposit 5"    (Exactly "bob balance 5")
  , Exchange "account/alice" "balance"      (Exactly "alice balance 70")
  ]

The first ask of account/alice makes Alice’s actor; every later one reaches it. Bob gets an actor of his own.

Run it

cd MLambda.Actor.Demo
DEMO_ROLE=standalone aleph run

# or in Docker
docker compose -f compose.standalone.yml up --build --abort-on-container-exit --exit-code-from standalone
  greeter <- Ada  =>  Hello, Ada! (greeting 1 from standalone)
  account/alice <- deposit 100  =>  alice balance 100
  ...
demo: OK (7/7 answers as expected)

The process exits 0 when every answer was the expected one.

Next

The same actors in a cluster: Cluster Client.