Skip to content

Commit 8c7d465

Browse files
authored
Get current tip (#13)
* Format example scripts * Add get_current_tip query * Fix epoch number return and more descriptive variable names
1 parent 42f3b93 commit 8c7d465

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

.formatter.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "*.exs"]
44
]

lib/xander/messages.ex

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule Xander.Messages do
1414
@get_current_block_height [2]
1515
@get_current_era [0, [2, [1]]]
1616
@get_epoch_number [0, [0, [6, [1]]]]
17+
@get_current_tip [3]
1718

1819
# See the CDDL for details on mapping of messages to numbers.
1920
# https://github.com/IntersectMBO/ouroboros-network/blob/main/ouroboros-network-protocols/cddl/specs/local-state-query.cddl
@@ -111,6 +112,27 @@ defmodule Xander.Messages do
111112
header(@mini_protocols.local_state_query, bitstring_payload) <> bitstring_payload
112113
end
113114

115+
@doc """
116+
Builds a static query to get the current tip of the chain
117+
118+
Payload CBOR: [3]
119+
Payload Bitstring: <<129, 3>>
120+
121+
## Examples
122+
123+
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.get_current_tip()
124+
iex> msg
125+
<<0, 7, 0, 4, 130, 3, 129, 3>>
126+
127+
"""
128+
@spec get_current_tip() :: binary()
129+
def get_current_tip do
130+
payload = build_query(@get_current_tip)
131+
bitstring_payload = CBOR.encode(payload)
132+
133+
header(@mini_protocols.local_state_query, bitstring_payload) <> bitstring_payload
134+
end
135+
114136
defp build_query(query), do: [@message_query, query]
115137

116138
# middle 16 bits are: 1 bit == 0 for initiator and 15 bits for the mini protocol ID

lib/xander/query.ex

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ defmodule Xander.Query do
135135
:get_current_era -> Messages.get_current_era()
136136
:get_current_block_height -> Messages.get_current_block_height()
137137
:get_epoch_number -> Messages.get_epoch_number()
138+
:get_current_tip -> Messages.get_current_tip()
138139
end
139140

140141
:ok = client.send(socket, message)

lib/xander/query/response.ex

+15-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ defmodule Xander.Query.Response do
1313
end
1414
end
1515

16-
# For get_current_block_height
17-
defp parse_cbor([@message_response, [@slot_timeline, response]]) do
18-
{:ok, response}
16+
# This clause parses the response from the get_current_tip query
17+
defp parse_cbor([@message_response, [slot_number, %CBOR.Tag{tag: :bytes, value: response}]]) do
18+
block_hash = Base.encode16(response, case: :lower)
19+
{:ok, {slot_number, block_hash}}
20+
end
21+
22+
# This clause parses the response from the get_current_block_height query
23+
defp parse_cbor([@message_response, [@slot_timeline, block_height]]) do
24+
{:ok, block_height}
25+
end
26+
27+
# This clause parses the response from get_epoch_number
28+
defp parse_cbor([@message_response, [epoch_number]]) do
29+
{:ok, epoch_number}
1930
end
2031

32+
# This clause parses the response from all other queries
2133
defp parse_cbor([@message_response, response]) do
2234
{:ok, response}
2335
end

run.exs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ config = Config.default_config!(socket_path)
2020
queries = [
2121
:get_epoch_number,
2222
:get_current_era,
23-
:get_current_block_height
23+
:get_current_block_height,
24+
:get_current_tip
2425
]
2526

2627
case Query.start_link(config) do

run_with_demeter.exs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ config = Config.demeter_config!(demeter_url, network: network)
2323
queries = [
2424
:get_epoch_number,
2525
:get_current_era,
26-
:get_current_block_height
26+
:get_current_block_height,
27+
:get_current_tip
2728
]
2829

2930
case Query.start_link(config) do

0 commit comments

Comments
 (0)