-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackets.hpp
More file actions
64 lines (51 loc) · 2.25 KB
/
Copy pathpackets.hpp
File metadata and controls
64 lines (51 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// The three LOTI datagram types, each behind a header carrying the message type
// and the sender's NodeId.
#pragma once
#include <cstdint>
#include <optional>
#include <variant>
#include <vector>
#include "domain/types.hpp"
namespace loti::wire {
enum class Type : std::uint8_t {
clock_notification = 0,
chain_request = 1,
chain_response = 2,
};
struct ClockNotification {
std::uint32_t chain = 0; // which of the sender's chains this tip belongs to
domain::EventHash last_clock_event_hash;
domain::EventHash neighbor_last_clock_event_hash;
};
struct ChainRequest {
domain::NodeId originator = 0;
domain::EventReference event;
domain::TimeRange range; // the querying party's estimated window for `event`
std::uint32_t hop_limit = 0; // max forward hops (0 = unlimited) — the flood-depth cap
// Reverse-path breadcrumb: the request's forward path of senders, [originator, …, prev].
// It doubles as the per-copy visited set (a node forwards only to neighbors not already in
// it — loop avoidance). The creator reflects the response back along it (Decision 6).
std::vector<domain::NodeId> path;
};
struct ChainResponse {
domain::NodeId originator = 0;
domain::EventChain chain;
// Remaining reverse path back to the originator: the next hop is path.back(), popped each
// hop; empty once the response reaches the originator.
std::vector<domain::NodeId> path;
};
using Payload = std::variant<ClockNotification, ChainRequest, ChainResponse>;
struct Datagram {
domain::NodeId sender = 0;
Payload payload;
};
[[nodiscard]] domain::Bytes encode(domain::NodeId sender, const ClockNotification&);
[[nodiscard]] domain::Bytes encode(domain::NodeId sender, const ChainRequest&);
[[nodiscard]] domain::Bytes encode(domain::NodeId sender, const ChainResponse&);
[[nodiscard]] Datagram decode(const domain::Bytes& datagram);
// Cheaply read just the sender NodeId from a datagram header (type byte + sender u64),
// without decoding the payload. Returns nullopt if the datagram is too short to hold a
// header. Used by the transport adapter to check a datagram's source against its claimed
// sender before the full decode.
[[nodiscard]] std::optional<domain::NodeId> sender_of(const domain::Bytes& datagram);
} // namespace loti::wire