State Machines

State machines are simple units that can be used to model the state of a system. They are composed of states, events, and transitions. A state machine can be in one state at a time, and can transition to another state when an event is triggered. The state machine can also have actions that are executed when a transition occurs.

Stackr-JS provides a simple state machine implementation that takes in the Rollup state and STF

All of this should go outside the state.ts file

example

import { StateMachine } from "@stackr/stackr-js/execution";
import * as genesisState from "../genesis-state.json";

const counterFsm = new StateMachine({
  state: new CounterRollup(genesisState.state),
  stf: counterSTF,
});

Genesis State

The genesis state is the initial state of the state machine. It is the state that is used to initialize the state machine. It is also the state that is used to initialize the state of the rollup.

You need to set the genesis state inside genesis-state.json file.

The format is

{ "state" : WHATEVER_YOU_WANT_AS_JSON }

example

Input type schema

The user-input type schema is the schema that is used to validate the user input. It is used to validate the user input before it is applied to the state machine.

This is what the user/ another entity send to the app or rollup. This is NOT what is applied to the State transition function.

You can define to get a custom input from the user using this schema -> optionally process it or add more items -> create an object for ActionInput for the state machine

To define a user input type schema, there are some special things to take care of since Stackr creates EIP-712 typed data field for the same

Example, for a chess game you can define something like

const actionSchemaType = {
  move: "String",
};

Or for a counter you can define something like

const actionSchemaType = {
  type: "String",
};

or for a AMM you can define something like

const actionSchemaType = {
  type: "String",
  owner: "String",
  quantityA: "Number",
  quantityB: "Number",
};

Notice the types

These are not TypeScript interfaces, these are actual Objects hence the way the type is passed on is inside quotes.

Supported user input types

These are same as solidity types

internal stackr-js
export type PrimitiveType = "String" | "Uint" | "Bool" | "Address" | "Bytes";
export type SchemaType =
  | PrimitiveType
  | { [key: string]: SchemaType }
  | SchemaType[];

So you can define both simple and complex input schema

Stackr Converts these to EIP-712 typed data fields and expects user signatures on it

You can now generate an ActionSchema from this

import { ActionSchema } from "@stackr/stackr-js";

const userInputSchema = new ActionSchema("update-move", actionSchemaType);

This userInputSchema can be used to create instances of user Actions