View Source Xogmios.ChainSync behaviour (xogmios v0.8.0)

This module interfaces with the Chain Synchronization protocol.

Summary

Callbacks

Invoked when a new block is emitted. This callback is required.

Invoked upon connecting to the server. This callback is optional.

Invoked upon disconnecting from the server. This callback is optional.

Invoked when a message is sent to the process. This callback is optional.

Invoked when a rollback event is emitted. This callback is optional.

Functions

Sends a message to the ChainSync process. This function is useful to send messages to the ChainSync process from outside of the ChainSync module. The message should be handled by a matching handle_info/2 callback.

Starts a new Chain Sync process linked to the current process.

Callbacks

Link to this callback

handle_block(block, state)

View Source
@callback handle_block(block :: map(), state) ::
  {:ok, :next_block, new_state} | {:ok, new_state} | {:close, new_state}
when state: term(), new_state: term()

Invoked when a new block is emitted. This callback is required.

Receives block information as argument and the current state of the handler.

Returning {:ok, :next_block, new_state} will request the next block once it's made available.

Returning {:ok, new_state} will not request anymore blocks.

Returning {:close, new_state} will close the connection to the server.

@callback handle_connect(state) :: {:ok, new_state} when state: term(), new_state: term()

Invoked upon connecting to the server. This callback is optional.

Link to this callback

handle_disconnect(reason, state)

View Source
@callback handle_disconnect(reason, state) ::
  {:reconnect, interval_in_ms :: non_neg_integer(), new_state}
  | {:close, reason, new_state}
  | {:ok, new_state}
when reason: String.t(), state: term(), new_state: term()

Invoked upon disconnecting from the server. This callback is optional.

Returning {:reconnect, interval_in_ms, new_state} will attempt a reconnection after interval_in_ms.

Returning {:close, reason, new_state} will allow the connection to close and shut the process down cleanly.

Returning {:ok, new_state} will allow the connection to close but keeps process alive.

Link to this callback

handle_info(message, state)

View Source (optional)
@callback handle_info(message :: term(), state) ::
  {:ok, :next_block, new_state} | {:ok, new_state} | {:close, new_state}
when state: term(), new_state: term()

Invoked when a message is sent to the process. This callback is optional.

Return type is the same as handle_block/2 and handle_rollback/2.

Link to this callback

handle_rollback(point, state)

View Source
@callback handle_rollback(point :: map(), state) ::
  {:ok, :next_block, new_state} | {:ok, new_state} | {:close, new_state}
when state: term(), new_state: term()

Invoked when a rollback event is emitted. This callback is optional.

Receives as argument a point and the state of the handler. The point is a map with keys for id (block id) and a slot. This information can then be used by the handler module to perform the necessary corrections. For example, resetting all current known state past this point and then rewriting it from future invokations of handle_block/2

Returning {:ok, :next_block, new_state} will request the next block once it's made available.

Returning {:ok, new_state} will not request anymore blocks.

Returning {:close, new_state} will close the connection to the server.

Functions

@spec call(pid() | atom(), term()) :: {:ok, term()} | {:error, term()}

Sends a message to the ChainSync process. This function is useful to send messages to the ChainSync process from outside of the ChainSync module. The message should be handled by a matching handle_info/2 callback.

Link to this function

start_link(client, opts)

View Source
@spec start_link(module(), start_options :: Keyword.t()) ::
  {:ok, pid()} | {:error, term()}

Starts a new Chain Sync process linked to the current process.

This function should not be called directly, but rather via Xogmios.start_chain_sync_link/2