Key Concepts
Welcome to the key concepts section of mintBlue's Machine functionality. Here, you'll get a high-level understanding of the fundamental components of Machines. This overview lays the groundwork for further exploration of mintBlue's Machine functionality.
Let's dive in to explore each concept and build a Machine.
Core Concepts
Machine
The core engine for running applications that interface with blockchain-driven architectures. Provides mechanisms for reading from and writing to the blockchain and invoking app-specific methods. In short, a machine requires:
- Application Logic
- Reader
- Writer
- Recorder (optional)
Application Logic (App)
The App represents a class that contains the logic for your application interacting with a blockchain. Each App maintains a state and includes one or multiple functions that modify the state.
Example: Here's a simple SupplyChain
class that keeps track of harvested coffee bean batches.
class SupplyChain implements App {
state = {
batches: [],
};
addBatch(_ctx: Context, batchId: string) {
this.state.batches.push({ batchId });
}
}
Writer
A Writer
is responsible for writing actions to storage. Each function you call from your App
will be sent to mintBlue using a transaction generated by the mintBlue SDK. The mintBlue platform then interacts with the blockchain to store your data, abstracting the complexity of blockchain technology.
The Writer
uses the Data ouput type to store your function invocation and arguments. It allows the machine to iterate over all its transactions to build its latest state and enable time travel debugging.
Here's a snippet from the Writer
implementation.
await this.mintblue.createTransaction({
project_id: this.project_id,
outputs: [
{
type: "data",
value: { f: action.f, args: action.args, metadata: Date.now() },
encrypt: true,
sign: true,
},
],
});
// Inteface
interface Writer {
write(action: ActionWithoutId): Promise<void>;
handleError(error: any): void;
fetchWriteStatus?(dataId: string): Promise<string>;
}
Reader
The Reader
facilitates listening to transaction events on the blockchain using mintBlue and dispatching them accordingly as actions. It accepts your SDK Access Token and a project ID to initialize the Reader
.
await MintblueReader.create(sdkToken, project_id),
// Interface
interface Reader {
initialize(actionDispatcher: ActionDispatcher): void;
start(lastActionId?: string): void;
stop(): void;
}
Recorder
After dispatching an action, you can add an optional Recorder
that automatically creates in-memory snapshots. It allows you to time-travel the application state. Beyond that, you can manually create snapshots. It's up to you if and where to store these snapshots.
Here's the Recorder
interface.
const recorder = new Recorder();
// Interface
interface Recorder {
afterDispatch(snapshot: Snapshot): Promise<void>;
getSnapshots(): Promise<Snapshot[]>;
createSnapshot(snapshot: Snapshot): Promise<void>;
loadSnapshot(id?: string | undefined): Promise<Snapshot | null>;
}
Summary
The mintBlue SDK is designed to handle the communication between your application and the blockchain, abstracting away the complexities of blockchain interaction.