Skip to main content
The Engine is the core component that runs the Tashi Vertex consensus algorithm. It manages the network communication, transaction processing, and event ordering.

Methods

start

Starts the consensus engine.
pub fn start(
    context: &Context,
    socket: Socket,
    options: Options,
    secret: &KeySecret,
    peers: Peers,
) -> crate::Result<Self>

Parameters

context
&Context
required
Reference to a Tashi Vertex context
socket
Socket
required
A bound socket for network communication. Ownership is transferred to the engine.
options
Options
required
Configuration options for the engine. Ownership is transferred to the engine.
secret
&KeySecret
required
The secret key used for signing transactions and events
peers
Peers
required
The set of peer nodes to connect to. Ownership is transferred to the engine.

Returns

return
Result<Engine>
Returns an Engine instance on success, or an Error if the engine fails to start.
The socket, options, and peers parameters transfer ownership to the engine. They cannot be used after calling start().

Example

use tashi_vertex::{Context, Socket, Options, KeySecret, Peers, Engine};

let context = Context::new()?;
let socket = Socket::bind(&context, "0.0.0.0:8080").await?;
let options = Options::new();
let secret = KeySecret::generate();
let peers = Peers::new()?;

let engine = Engine::start(&context, socket, options, &secret, peers)?;

recv_message

Listens for the next incoming message on the given engine.
pub async fn recv_message(&self) -> crate::Result<Option<Message>>

Returns

return
Result<Option<Message>>
Returns Some(Message) when a message is received, None when the engine is shutting down, or an Error if an error occurs.
The Message enum can be one of:
  • Message::Event(Event) - A consensus event that has been ordered
  • Message::SyncPoint(SyncPoint) - A synchronization point in the consensus

Example

use tashi_vertex::{Engine, Message};

while let Some(message) = engine.recv_message().await? {
    match message {
        Message::Event(event) => {
            // Process consensus event
            println!("Received event");
        }
        Message::SyncPoint(sync_point) => {
            // Handle synchronization point
            println!("Reached sync point");
        }
    }
}
This method is asynchronous and will await the next message. It’s typically called in a loop to continuously process messages.

send_transaction

Sends a transaction to the network.
pub fn send_transaction(&self, transaction: Transaction) -> crate::Result<()>

Parameters

transaction
Transaction
required
The transaction to send. Ownership is transferred to the engine.

Returns

return
Result<()>
Returns Ok(()) on success, or an Error if the transaction fails to send.

Example

use tashi_vertex::{Engine, Transaction};

// Allocate a transaction buffer
let mut transaction = Transaction::allocate(1024);

// Write data to the transaction
transaction[0..5].copy_from_slice(b"hello");

// Send the transaction
engine.send_transaction(transaction)?;
The transaction ownership is transferred to the engine. Do not use the transaction after calling send_transaction().

Complete usage example

use tashi_vertex::{
    Context, Socket, Options, KeySecret, Peers, Engine, Message
};

// Initialize context
let context = Context::new()?;

// Bind socket
let socket = Socket::bind(&context, "0.0.0.0:8080").await?;

// Configure options
let mut options = Options::new();
options.set_heartbeat_us(500_000);

// Generate keys
let secret = KeySecret::generate();

// Create peer set
let mut peers = Peers::new()?;
// Add peers as needed

// Start engine
let engine = Engine::start(&context, socket, options, &secret, peers)?;

// Process messages
while let Some(message) = engine.recv_message().await? {
    match message {
        Message::Event(event) => { /* handle event */ }
        Message::SyncPoint(sync_point) => { /* handle sync */ }
    }
}
  • Context - Required to start the engine
  • Socket - Network socket transferred to the engine
  • Options - Configuration options transferred to the engine
  • KeySecret - Secret key for signing operations
  • Peers - Peer set transferred to the engine
  • Transaction - Transactions to send to the network
  • Message - Messages received from the engine
  • Event - Consensus events
  • SyncPoint - Synchronization points

Build docs developers (and LLMs) love