Skip to content

Commit 42f3b93

Browse files
authored
Merge pull request #12 from wowica/response-cleanup
Response cleanup
2 parents c8e7d49 + 5144ec6 commit 42f3b93

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

lib/xander/messages.ex

+27-10
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,38 @@ defmodule Xander.Messages do
1818
# See the CDDL for details on mapping of messages to numbers.
1919
# https://github.com/IntersectMBO/ouroboros-network/blob/main/ouroboros-network-protocols/cddl/specs/local-state-query.cddl
2020
@message_query 3
21-
@message_acquire 8
22-
@message_release 5
21+
@message_acquire [8]
22+
@message_release [5]
2323

24-
def msg_acquire do
25-
header = [<<0, 0, 44, 137, 0, 7, 0, 2>>]
26-
payload = [<<129, @message_acquire>>]
24+
@doc """
25+
Acquires a snapshot of the mempool, allowing the protocol to make queries.
26+
27+
## Examples
2728
28-
[header | payload]
29+
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.msg_acquire()
30+
iex> msg
31+
<<0, 7, 0, 2, 129, 8>>
32+
33+
"""
34+
def msg_acquire do
35+
payload = CBOR.encode(@message_acquire)
36+
header(@mini_protocols.local_state_query, payload) <> payload
2937
end
3038

31-
def msg_release do
32-
header = [<<0, 0, 167, 211, 0, 7, 0, 2>>]
33-
payload = [<<129, @message_release>>]
39+
@doc """
40+
Releases the current snapshot of the mempool, allowing the protocol to return
41+
to the idle state.
42+
43+
## Examples
3444
35-
[header | payload]
45+
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.msg_release()
46+
iex> msg
47+
<<0, 7, 0, 2, 129, 5>>
48+
49+
"""
50+
def msg_release do
51+
payload = CBOR.encode(@message_release)
52+
header(@mini_protocols.local_state_query, payload) <> payload
3653
end
3754

3855
@doc """

lib/xander/query.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ defmodule Xander.Query do
147147
{_tcp_or_ssl, socket, bytes},
148148
%__MODULE__{socket: socket} = data
149149
) do
150-
{:ok, current_era} = Response.parse_response(bytes)
150+
{:ok, query_response} = Response.parse_response(bytes)
151151
{{:value, caller}, data} = get_and_update_in(data.queue, &:queue.out/1)
152-
# This action issues the response back to the clinet
153-
actions = [{:reply, caller, {:ok, current_era}}]
152+
# This action issues the response back to the client
153+
actions = [{:reply, caller, {:ok, query_response}}]
154154
{:keep_state, data, actions}
155155
end
156156

lib/xander/query/response.ex

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ defmodule Xander.Query.Response do
22
# See the CDDL for details on mapping of messages to numbers.
33
# https://github.com/IntersectMBO/ouroboros-network/blob/main/ouroboros-network-protocols/cddl/specs/local-state-query.cddl
44
@message_response 4
5+
@slot_timeline 1
56

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

910
case CBOR.decode(response_payload) do
1011
{:ok, decoded, ""} -> parse_cbor(decoded)
1112
{:error, _reason} -> {:error, :error_decoding_cbor}
1213
end
1314
end
1415

16+
# For get_current_block_height
17+
defp parse_cbor([@message_response, [@slot_timeline, response]]) do
18+
{:ok, response}
19+
end
20+
1521
defp parse_cbor([@message_response, response]) do
1622
{:ok, response}
1723
end

lib/xander/util.ex

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
defmodule Xander.Util do
22
@doc """
3-
Unwrap the multiplexer header from the CDDL message.
3+
Unwrap the multiplexer header from a CDDL message. This can be used to
4+
extract the payload from node responses.
5+
6+
## Examples
7+
8+
iex> Xander.Util.plex(<<0, 0, 0, 0, 1, 2, 0, 3, 97, 98, 99>>)
9+
%{payload: <<97, 98, 99>>, protocol_id: <<1, 2>>, size: <<0, 3>>}
10+
411
"""
512
@spec plex(binary) :: map()
613
def plex(msg) do
7-
<<_ts::binary-size(4), protocol_id::binary-size(2), payload_size::binary-size(2),
14+
<<_timestamp::binary-size(4), protocol_id::binary-size(2), payload_size::binary-size(2),
815
payload::binary>> = msg
916

1017
%{payload: payload, protocol_id: protocol_id, size: payload_size}

test/xander/util_test.exs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Xander.UtilTest do
2+
use ExUnit.Case
3+
4+
doctest Xander.Util
5+
end

0 commit comments

Comments
 (0)