Skip to content

Commit

Permalink
Add get_current_tip query
Browse files Browse the repository at this point in the history
  • Loading branch information
caike committed Feb 10, 2025
1 parent da711e5 commit 8b25606
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
22 changes: 22 additions & 0 deletions lib/xander/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Xander.Messages do
@get_current_block_height [2]
@get_current_era [0, [2, [1]]]
@get_epoch_number [0, [0, [6, [1]]]]
@get_current_tip [3]

# 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
Expand Down Expand Up @@ -111,6 +112,27 @@ defmodule Xander.Messages do
header(@mini_protocols.local_state_query, bitstring_payload) <> bitstring_payload
end

@doc """
Builds a static query to get the current tip of the chain
Payload CBOR: [3]
Payload Bitstring: <<129, 3>>
## Examples
iex> <<_timestamp::32, msg::binary>> = Xander.Messages.get_current_tip()
iex> msg
<<0, 7, 0, 4, 130, 3, 129, 3>>
"""
@spec get_current_tip() :: binary()
def get_current_tip do
payload = build_query(@get_current_tip)
bitstring_payload = CBOR.encode(payload)

header(@mini_protocols.local_state_query, bitstring_payload) <> bitstring_payload
end

defp build_query(query), do: [@message_query, query]

# middle 16 bits are: 1 bit == 0 for initiator and 15 bits for the mini protocol ID
Expand Down
1 change: 1 addition & 0 deletions lib/xander/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ defmodule Xander.Query do
:get_current_era -> Messages.get_current_era()
:get_current_block_height -> Messages.get_current_block_height()
:get_epoch_number -> Messages.get_epoch_number()
:get_current_tip -> Messages.get_current_tip()
end

:ok = client.send(socket, message)
Expand Down
6 changes: 6 additions & 0 deletions lib/xander/query/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ defmodule Xander.Query.Response do
end
end

# This clause parses the response from the get_current_tip query
defp parse_cbor([@message_response, [slot_number, %CBOR.Tag{tag: :bytes, value: response}]]) do
block_hash = Base.encode16(response, case: :lower)
{:ok, {slot_number, block_hash}}
end

# For get_current_block_height
defp parse_cbor([@message_response, [@slot_timeline, response]]) do
{:ok, response}
Expand Down
3 changes: 2 additions & 1 deletion run.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ config = Config.default_config!(socket_path)
queries = [
:get_epoch_number,
:get_current_era,
:get_current_block_height
:get_current_block_height,
:get_current_tip
]

case Query.start_link(config) do
Expand Down
3 changes: 2 additions & 1 deletion run_with_demeter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ config = Config.demeter_config!(demeter_url, network: network)
queries = [
:get_epoch_number,
:get_current_era,
:get_current_block_height
:get_current_block_height,
:get_current_tip
]

case Query.start_link(config) do
Expand Down

0 comments on commit 8b25606

Please sign in to comment.