Skip to content

Commit ec0478b

Browse files
committed
more documentation
1 parent de1d29b commit ec0478b

File tree

7 files changed

+208
-10
lines changed

7 files changed

+208
-10
lines changed

include/scl/protocol.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,108 @@
2626

2727
namespace scl {
2828

29+
/**
30+
* @brief Protocol environment.
31+
*
32+
* Env is the interface with which a Protocol can interact with the outside
33+
* world, as it were. It contains (1) a way to send and receive data to the
34+
* other parties, and (2) a way to see how long the protocol has been running
35+
* for.
36+
*/
2937
struct Env {
38+
/**
39+
* @brief The network.
40+
*/
3041
Network network;
42+
43+
/**
44+
* @brief A clock.
45+
*/
3146
std::unique_ptr<Clock> clock;
3247
};
3348

3449
struct Result;
3550

51+
/**
52+
* @brief Protocol interface.
53+
*
54+
* Protocol defines the "smallest possible" protocol. Namely a protocol which
55+
* can be run once, and which might produce (1) some output, and (2) another
56+
* protocol which can then be run next. This allows us to string multiple
57+
* Protocols together in order to get a much larger protocol. For example,
58+
* instead of implementing a full evaluation of a circuit, it might be easier to
59+
* implement something which only evaluates a single layer of the circuit. To
60+
* evaluate the full circuit, we just need stich enough of these single-layer
61+
* protocols together.
62+
*
63+
* The following simple example illustrates this idea.
64+
* @code
65+
* class CountdownProtocol final : public Protocol {
66+
* public:
67+
* CountdownProtocol(int current) : m_current(current) {}
68+
* Task<Result> run(Env& ignored) const override {
69+
* std::cout << "countdown: " << m_current << "\n";
70+
* if (m_current == 0) {
71+
* co_return Result::done();
72+
* } else {
73+
* co_return Result::next(
74+
* std::make_unique<CountdownProtocol>(m_current - 1));
75+
* }
76+
* }
77+
* private:
78+
* int m_current;
79+
* };
80+
* @endcode
81+
*/
3682
struct Protocol {
3783
virtual ~Protocol() {}
3884

85+
/**
86+
* @brief Default name of a protocol.
87+
*/
3988
constexpr static const char* DEFAULT_NAME = "N/A";
4089

90+
/**
91+
* @brief Runs the protocol.
92+
*/
4193
virtual Task<Result> run(Env& env) const = 0;
4294

95+
/**
96+
* @brief Returns the name of the protocol.
97+
*/
4398
virtual std::string name() const {
4499
return DEFAULT_NAME;
45100
}
46101
};
47102

103+
/**
104+
* @brief The result of calling Protocol::run.
105+
*/
48106
struct Result {
107+
/**
108+
* @brief Creates a Result with no next step, and no output.
109+
*/
49110
static Result done() {
50111
return Result{.next = nullptr, .output = {}};
51112
}
52113

114+
/**
115+
* @brief Creates a Result with no next step, and some output.
116+
*/
53117
static Result done(std::any output) {
54118
return Result{.next = nullptr, .output = output};
55119
}
56120

121+
/**
122+
* @brief Creates a Result with a next step, and no output.
123+
*/
57124
static Result nextStep(std::unique_ptr<Protocol> next) {
58125
return Result{.next = std::move(next), .output = {}};
59126
}
60127

128+
/**
129+
* @brief Creates a Result with a next step, and some output.
130+
*/
61131
static Result nextStep(std::unique_ptr<Protocol> next, std::any output) {
62132
return Result{.next = std::move(next), .output = output};
63133
}

include/scl/serialization.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,54 @@
2525

2626
namespace scl {
2727

28+
/**
29+
* @brief Serializer type.
30+
*
31+
* A type \p T is serializable if it <code>Serializer<T></code> contains three
32+
* static functions.
33+
*
34+
* @code
35+
* struct Thing {
36+
* static std::size_t sizeOf(Thing t) {
37+
* // return the size in bytes of the object t of type Thing
38+
* }
39+
*
40+
* static std::size_t write(Thing t, unsigned char* out) {
41+
* // write t to out.
42+
* // out will be guaranteed to point to at least sizeOf(t) available bytes
43+
* // return the amount of bytes written to out
44+
* }
45+
*
46+
* static std::size_t read(Thing& t, const unsigned char* in) {
47+
* // read a Thing from in, store the result in t
48+
* // return the amount of bytes read from in
49+
* }
50+
* }
51+
* @endcode
52+
*
53+
* Note that no guarantees are made about the buffer passed to
54+
* <code>read</code>.
55+
*
56+
* Serializer's mostly play a role in reading and writing to Packets for the
57+
* purpose of communicating with other parties.
58+
*/
2859
template <typename T>
2960
struct Serializer;
3061

62+
/**
63+
* @brief Serializable concept.
64+
*/
3165
template <typename T>
3266
concept Serializable =
33-
requires(T v, const unsigned char* in, unsigned char* out) {
67+
requires(T v, T& r, const unsigned char* in, unsigned char* out) {
3468
{ Serializer<T>::sizeOf(v) } -> std::same_as<std::size_t>;
3569
{ Serializer<T>::write(v, out) } -> std::same_as<std::size_t>;
36-
{ Serializer<T>::read(v, in) } -> std::same_as<std::size_t>;
70+
{ Serializer<T>::read(r, in) } -> std::same_as<std::size_t>;
3771
};
3872

73+
/**
74+
* @brief Serializer for trivially copyable types.
75+
*/
3976
template <typename T>
4077
requires(std::is_trivially_copyable_v<T>)
4178
struct Serializer<T> {
@@ -54,7 +91,10 @@ struct Serializer<T> {
5491
}
5592
};
5693

57-
template <typename T>
94+
/**
95+
* @brief Serializer for STL vectors of something serializable.
96+
*/
97+
template <Serializable T>
5898
struct Serializer<std::vector<T>> {
5999
using VecSizeType = std::uint32_t;
60100

include/scl/simulation/channel_id.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace scl {
2525

2626
/**
27-
* @brief x
27+
* @brief Identifier used for Channels.
2828
*/
2929
struct ChannelId final {
3030
std::size_t local;

include/scl/simulation/event.h

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ enum class EventType {
102102

103103
/**
104104
* @brief Base class for all events.
105+
*
106+
* All events in SCL's simulator inheret from Event. The most basic Event is
107+
* effectively just a timestamp telling us when the event was generated.
105108
*/
106109
class Event {
107110
public:
108111
/**
109112
* @brief Constructor.
110-
* @param timestamp the time that the event was issued.
111113
*/
112114
Event(Time::Duration timestamp) : m_timestamp(timestamp) {}
113115

@@ -136,6 +138,7 @@ class Event {
136138

137139
/**
138140
* @brief Event issued when a party starts executing a Protocol.
141+
* @see EndEvent
139142
*/
140143
class BeginEvent final : public Event {
141144
public:
@@ -148,6 +151,10 @@ class BeginEvent final : public Event {
148151
return EventType::BEGIN;
149152
}
150153

154+
/**
155+
* @brief The name of protocol.
156+
* @see Protocol::name
157+
*/
151158
std::string_view name() const {
152159
return m_name;
153160
}
@@ -158,6 +165,7 @@ class BeginEvent final : public Event {
158165

159166
/**
160167
* @brief Event issued when a party finishes executing a Protocol.
168+
* @see BeginEvent
161169
*/
162170
class EndEvent final : public Event {
163171
public:
@@ -170,6 +178,10 @@ class EndEvent final : public Event {
170178
return EventType::END;
171179
}
172180

181+
/**
182+
* @brief The name of protocol.
183+
* @see Protocol::name
184+
*/
173185
std::string_view name() const {
174186
return m_name;
175187
}
@@ -180,6 +192,7 @@ class EndEvent final : public Event {
180192

181193
/**
182194
* @brief Event issued when a party starts running.
195+
* @see StopEvent
183196
*/
184197
class StartEvent final : public Event {
185198
public:
@@ -192,6 +205,7 @@ class StartEvent final : public Event {
192205

193206
/**
194207
* @brief Event issued when a party stops running.
208+
* @see StartEvent
195209
*/
196210
class StopEvent final : public Event {
197211
public:
@@ -209,6 +223,10 @@ class KilledEvent final : public Event {
209223
public:
210224
KilledEvent(Time::Duration timestamp, const std::string& reason)
211225
: Event(timestamp), m_reason(reason) {}
226+
227+
/**
228+
* @brief The exception's what message.
229+
*/
212230
std::string reason() const {
213231
return m_reason;
214232
}
@@ -241,6 +259,9 @@ class ChannelEvent : public Event {
241259
ChannelEvent(Time::Duration timestamp, ChannelId id)
242260
: Event(timestamp), m_id(id) {}
243261

262+
/**
263+
* @brief The identifier of the channel this event pertains to.
264+
*/
244265
ChannelId id() const {
245266
return m_id;
246267
}
@@ -255,9 +276,7 @@ class ChannelEvent : public Event {
255276
class CloseEvent final : public ChannelEvent {
256277
public:
257278
using ChannelEvent::ChannelEvent;
258-
259279
void write(std::ostream& stream) override;
260-
261280
EventType type() const override {
262281
return EventType::CHANNEL_CLOSE;
263282
}
@@ -271,6 +290,9 @@ class ChannelDataEvent : public ChannelEvent {
271290
ChannelDataEvent(Time::Duration timestamp, ChannelId id, std::size_t amount)
272291
: ChannelEvent(timestamp, id), m_amount(amount) {}
273292

293+
/**
294+
* @brief The amount of data sent or received.
295+
*/
274296
std::size_t amount() const {
275297
return m_amount;
276298
}
@@ -303,6 +325,9 @@ class RecvEvent final : public ChannelDataEvent {
303325
}
304326
};
305327

328+
/**
329+
* @brief Event issued when a party times out during a recv call.
330+
*/
306331
class RecvTimeoutEvent final : public ChannelEvent {
307332
public:
308333
using ChannelEvent::ChannelEvent;
@@ -325,6 +350,9 @@ class PollEvent final : public ChannelEvent {
325350
return EventType::CHANNEL_POLL;
326351
}
327352

353+
/**
354+
* @brief The result of the Channel::poll call.
355+
*/
328356
bool result() const {
329357
return m_result;
330358
}
@@ -333,6 +361,9 @@ class PollEvent final : public ChannelEvent {
333361
bool m_result;
334362
};
335363

364+
/**
365+
* @brief Event issued when a party sleeps.
366+
*/
336367
class SleepEvent final : public Event {
337368
public:
338369
SleepEvent(Time::Duration timestamp, Time::Duration duration)
@@ -344,6 +375,9 @@ class SleepEvent final : public Event {
344375
return EventType::SLEEP;
345376
}
346377

378+
/**
379+
* @brief The amount of time a party slept.
380+
*/
347381
Time::Duration duration() const {
348382
return m_duration;
349383
}
@@ -352,9 +386,22 @@ class SleepEvent final : public Event {
352386
Time::Duration m_duration;
353387
};
354388

389+
namespace details {
390+
391+
/**
392+
* @brief Tracks events added by a party during simulation.
393+
*
394+
* EventList is mostly just a wrapper around an STL vector of Event
395+
* pointers. Minor book keeping is done to ensure that all events of type
396+
* EventType::TRANSIENT only appear of the head of the list.
397+
*/
355398
class EventList final {
356399
public:
357400
EventList();
401+
402+
/**
403+
* @brief Add a new event by in-place construction.
404+
*/
358405
template <typename EVENT, typename... ARGS>
359406
requires(std::is_base_of_v<Event, EVENT>)
360407
void add(ARGS... args) {
@@ -403,6 +450,7 @@ class EventList final {
403450
std::vector<std::unique_ptr<Event>> m_events;
404451
};
405452

453+
} // namespace details
406454
} // namespace scl
407455

408456
#endif // SCL_SIMULATION_EVENT_H

0 commit comments

Comments
 (0)