Skip to content

Commit

Permalink
Fix ChainSync initial state.
Browse files Browse the repository at this point in the history
Update README.
  • Loading branch information
caike committed Jan 16, 2024
1 parent 65f14a6 commit 3fa95d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

![CI Status](https://github.com/wowica/xogmios/actions/workflows/ci.yml/badge.svg)

An Elixir client for [Ogmios](https://github.com/CardanoSolutions/ogmios). This project is highly experimental. It currently only partially supports the chain sync Ouroboros mini-protocol.
An Elixir client for Cardano's [Ogmios](https://github.com/CardanoSolutions/ogmios).

Currently supports the **Chain Synchronization** and **State Query** Ouroboros mini-protocol.

## Installing

Expand Down Expand Up @@ -64,19 +66,20 @@ defmodule MyApp.ChainSyncClient do

require Logger

def start_link(opts),
do: start_connection(opts)
def start_link(opts) do
# Initial state currently has to be defined here
# and passed as argument to start_connection
initial_state = [counter: 3]

@impl true
def init(_args) do
{:ok, %{counter: 3}}
opts
|> Keyword.merge(initial_state)
|> start_connection()
end

@impl true
def handle_block(block, %{counter: counter} = state) when counter > 1 do
Logger.info("handle_block #{block["height"]}")
new_state = Map.merge(state, %{counter: counter - 1})
{:ok, :next_block, new_state}
{:ok, :next_block, %{state | counter: counter - 1}}
end

@impl true
Expand All @@ -87,6 +90,8 @@ defmodule MyApp.ChainSyncClient do
end
```

See more examples in the [examples](./examples/) folder.

## Test

Run `mix test`. Tests do not rely on a running OGMIOS instance.
Expand Down
14 changes: 8 additions & 6 deletions lib/xogmios/chain_sync/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ defmodule Xogmios.ChainSync.Connection do
def do_start_link(opts) do
url = Keyword.fetch!(opts, :url)

initial_state =
opts
|> Enum.into(%{})
|> Map.merge(%{handler: __MODULE__})
state = Keyword.merge(opts, handler: __MODULE__)

:websocket_client.start_link(url, __MODULE__, initial_state)
:websocket_client.start_link(url, __MODULE__, state)
end

@impl true
def init(initial_state) do
def init(state) do
initial_state =
state
|> Enum.into(%{})
|> Map.merge(%{handler: __MODULE__})

{:once, initial_state}
end

Expand Down

0 comments on commit 3fa95d9

Please sign in to comment.