Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove application #4

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,35 @@ jobs:
- name: Formatted
run: mix format --check-formatted

# Don't cache PLTs based on mix.lock hash, as Dialyzer can incrementally update even old ones
# Cache key based on Elixir & Erlang version (also useful when running in matrix)
- name: Restore PLT cache
uses: actions/cache@v3
uses: actions/cache/restore@v3
id: plt_cache
with:
key: plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}-${{ hashFiles('**/*.ex') }}
key: |
${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-plt
restore-keys: |
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}-${{ hashFiles('**/*.ex') }}
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-${{ hashFiles('**/mix.lock') }}-
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-${{ steps.beam.outputs.elixir-version }}-
plt-${{ runner.os }}-${{ steps.beam.outputs.otp-version }}-
path: priv/plts
${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-plt
path: |
priv/plts

# Create PLTs if no cache was found
- name: Create PLTs
if: steps.plt_cache.outputs.cache-hit != 'true' || github.run_attempt != '1'
if: steps.plt_cache.outputs.cache-hit != 'true'
run: mix dialyzer --plt

- name: Run Dialyzer
run: mix dialyzer --format github
# By default, the GitHub Cache action will only save the cache if all steps in the job succeed,
# so we separate the cache restore and save steps in case running dialyzer fails.
- name: Save PLT cache
uses: actions/cache/save@v3
if: steps.plt_cache.outputs.cache-hit != 'true'
id: plt_cache_save
with:
key: |
${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}-plt
path: |
priv/plts

- name: Run dialyzer
run: mix dialyzer --format github
61 changes: 37 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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.

## Running as a library
## Installing

Add the dependency to `mix.exs`:

Expand All @@ -16,10 +16,15 @@ defp deps do
end
```

## Examples

From a new module, call `use Xogmios.ChainSync` and implement the `start_link/1` and `handle_block/2` functions as such:

```elixir
defmodule MyApp.XogmiosClient do
defmodule MyApp.ChainSyncClient do
@moduledoc """
This module syncs with the tip of the chain and reads blocks indefinitely
"""

use Xogmios.ChainSync

Expand All @@ -34,47 +39,55 @@ defmodule MyApp.XogmiosClient do
end
```

Add the new module to the app's supervision tree in `application.ex`:
Add the new module to your app's supervision tree in `application.ex`:

```elixir
def start(_type, _args) do
children =[
{MyApp.XogmiosClient, url: "ws://url-for-ogmios"},
{MyApp.ChainSyncClient, url: "ws://url-for-ogmios"},
]

opts = [strategy: :one_for_one, name: Xogmios.Supervisor]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
```

See more examples in [Xogmios.ClientExampleA](lib/xogmios/client_example_a.ex) and [Xogmios.ClientExampleB](lib/xogmios/client_example_b.ex)

## Running as a standalone client

Clone this repo, populate `OGMIOS_URL` and run the following to start following the chain and printing newly minted blocks as they become available:

```shell
OGMIOS_URL="ws://..." mix run --no-halt
Compiling 4 files (.ex)

21:29:25.609 [info] Finding intersection...
The example below syncs with the tip and prints the next 3 blocks:

21:29:25.619 [info] Intersection found.
```elixir
defmodule MyApp.ChainSyncClient do
@moduledoc """
This module syncs with the tip of the chain and reads the following 3 blocks
"""

21:29:25.619 [info] Waiting for next block...
use Xogmios.ChainSync

21:29:36.330 [info] Elixir.Xogmios.ClientExampleA handle_block 9751015
require Logger

21:29:53.522 [info] Elixir.Xogmios.ClientExampleA handle_block 9751016
def start_link(opts),
do: start_connection(opts)

21:30:08.763 [info] Elixir.Xogmios.ClientExampleA handle_block 9751017
@impl true
def init(_args) do
{:ok, %{counter: 3}}
end

21:30:12.240 [info] Elixir.Xogmios.ClientExampleA handle_block 9751018
@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}
end

...
@impl true
def handle_block(block, state) do
Logger.info("final handle_block #{block["height"]}")
{:ok, :close, state}
end
end
```

## Test

Run `mix test`
Run `mix test`. Tests do not rely on a running OGMIOS instance.

28 changes: 0 additions & 28 deletions lib/xogmios/application.ex

This file was deleted.

18 changes: 0 additions & 18 deletions lib/xogmios/client_example_a.ex

This file was deleted.

30 changes: 0 additions & 30 deletions lib/xogmios/client_example_b.ex

This file was deleted.

3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ defmodule Xogmios.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :observer, :wx, :runtime_tools],
mod: {Xogmios.Application, []}
extra_applications: [:logger]
]
end

Expand Down