Skip to content

Commit

Permalink
Merge pull request #12 from wowica/response-cleanup
Browse files Browse the repository at this point in the history
Response cleanup
  • Loading branch information
daveminer authored Feb 10, 2025
2 parents c8e7d49 + 5144ec6 commit 42f3b93
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
37 changes: 27 additions & 10 deletions lib/xander/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,38 @@ defmodule Xander.Messages do
# See the CDDL for details on mapping of messages to numbers.
# https://github.com/IntersectMBO/ouroboros-network/blob/main/ouroboros-network-protocols/cddl/specs/local-state-query.cddl
@message_query 3
@message_acquire 8
@message_release 5
@message_acquire [8]
@message_release [5]

def msg_acquire do
header = [<<0, 0, 44, 137, 0, 7, 0, 2>>]
payload = [<<129, @message_acquire>>]
@doc """
Acquires a snapshot of the mempool, allowing the protocol to make queries.
## Examples
[header | payload]
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.msg_acquire()
iex> msg
<<0, 7, 0, 2, 129, 8>>
"""
def msg_acquire do
payload = CBOR.encode(@message_acquire)
header(@mini_protocols.local_state_query, payload) <> payload
end

def msg_release do
header = [<<0, 0, 167, 211, 0, 7, 0, 2>>]
payload = [<<129, @message_release>>]
@doc """
Releases the current snapshot of the mempool, allowing the protocol to return
to the idle state.
## Examples
[header | payload]
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.msg_release()
iex> msg
<<0, 7, 0, 2, 129, 5>>
"""
def msg_release do
payload = CBOR.encode(@message_release)
header(@mini_protocols.local_state_query, payload) <> payload
end

@doc """
Expand Down
6 changes: 3 additions & 3 deletions lib/xander/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ defmodule Xander.Query do
{_tcp_or_ssl, socket, bytes},
%__MODULE__{socket: socket} = data
) do
{:ok, current_era} = Response.parse_response(bytes)
{:ok, query_response} = Response.parse_response(bytes)
{{:value, caller}, data} = get_and_update_in(data.queue, &:queue.out/1)
# This action issues the response back to the clinet
actions = [{:reply, caller, {:ok, current_era}}]
# This action issues the response back to the client
actions = [{:reply, caller, {:ok, query_response}}]
{:keep_state, data, actions}
end

Expand Down
8 changes: 7 additions & 1 deletion lib/xander/query/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ defmodule Xander.Query.Response do
# See the CDDL for details on mapping of messages to numbers.
# https://github.com/IntersectMBO/ouroboros-network/blob/main/ouroboros-network-protocols/cddl/specs/local-state-query.cddl
@message_response 4
@slot_timeline 1

def parse_response(full_response) do
<<_header_todo_investigate::binary-size(8), response_payload::binary>> = full_response
%{payload: response_payload} = Xander.Util.plex(full_response)

case CBOR.decode(response_payload) do
{:ok, decoded, ""} -> parse_cbor(decoded)
{:error, _reason} -> {:error, :error_decoding_cbor}
end
end

# For get_current_block_height
defp parse_cbor([@message_response, [@slot_timeline, response]]) do
{:ok, response}
end

defp parse_cbor([@message_response, response]) do
{:ok, response}
end
Expand Down
11 changes: 9 additions & 2 deletions lib/xander/util.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
defmodule Xander.Util do
@doc """
Unwrap the multiplexer header from the CDDL message.
Unwrap the multiplexer header from a CDDL message. This can be used to
extract the payload from node responses.
## Examples
iex> Xander.Util.plex(<<0, 0, 0, 0, 1, 2, 0, 3, 97, 98, 99>>)
%{payload: <<97, 98, 99>>, protocol_id: <<1, 2>>, size: <<0, 3>>}
"""
@spec plex(binary) :: map()
def plex(msg) do
<<_ts::binary-size(4), protocol_id::binary-size(2), payload_size::binary-size(2),
<<_timestamp::binary-size(4), protocol_id::binary-size(2), payload_size::binary-size(2),
payload::binary>> = msg

%{payload: payload, protocol_id: protocol_id, size: payload_size}
Expand Down
5 changes: 5 additions & 0 deletions test/xander/util_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Xander.UtilTest do
use ExUnit.Case

doctest Xander.Util
end

0 comments on commit 42f3b93

Please sign in to comment.