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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Book configuration. Every capacity and the whole tick grid are fixed
//! at construction (ARCHITECTURE.md section 3.2); nothing resizes later,
//! which is what makes the zero steady-state allocation contract
//! possible.
use crate::error::BookError;
use crate::types::StpPolicy;
/// Static configuration for one [`crate::Book`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BookConfig {
/// Arena capacity: the maximum number of simultaneously live orders.
/// At least 1 and at most 2^31 - 2, so slot indices can reserve
/// `u32::MAX` as NIL.
pub max_orders: u32,
/// Number of price ticks on the grid. A multiple of 64, at least 64,
/// at most 2^22.
pub grid_len: u32,
/// Quote units per tick, at least 1.
pub tick_size: u64,
/// Tick value of grid index 0. A price whose tick is `t` maps to grid
/// index `t - grid_start_tick`.
pub grid_start_tick: u64,
/// Self-trade prevention policy applied when a command does not name
/// one (executes from Milestone 2).
pub default_stp: StpPolicy,
}
impl BookConfig {
/// The tick value one past the last tick on the grid.
///
/// # Errors
///
/// Returns [`BookError::InvalidConfig`] if the grid end overflows
/// `u64` (the m9a construction bound, ARCHITECTURE.md section 3.2).
pub fn grid_end_tick(&self) -> Result<u64, BookError> {
self.grid_start_tick
.checked_add(u64::from(self.grid_len))
.ok_or(BookError::InvalidConfig {
field: "grid_start_tick + grid_len",
rule: "the grid end must fit in u64 (checked_add at construction)",
})
}
/// Smallest valid raw price on the grid, in quote units.
///
/// Price zero is never valid on a [`crate::Book`] (ARCHITECTURE.md
/// section 3.2), so when `grid_start_tick` is `0` the lowest
/// addressable price tick is `1` (`tick_size`); otherwise it is
/// `grid_start_tick * tick_size`.
///
/// # Examples
///
/// ```
/// use chronicle_core::{BookConfig, StpPolicy};
///
/// let cfg = BookConfig {
/// max_orders: 64,
/// grid_len: 640,
/// tick_size: 5,
/// grid_start_tick: 100,
/// default_stp: StpPolicy::CancelIncoming,
/// };
/// assert_eq!(cfg.min_price(), 500);
/// assert_eq!(cfg.max_price().unwrap(), 3695);
/// ```
#[must_use]
pub const fn min_price(&self) -> u64 {
let first_tick = if self.grid_start_tick == 0 {
1
} else {
self.grid_start_tick
};
first_tick.saturating_mul(self.tick_size)
}
/// Largest valid raw price on the grid, in quote units:
/// `(grid_start_tick + grid_len - 1) * tick_size`.
///
/// # Errors
///
/// Returns [`BookError::InvalidConfig`] when `grid_len` or
/// `tick_size` is zero or when the grid end or highest raw price
/// overflows `u64`.
pub fn max_price(&self) -> Result<u64, BookError> {
if self.grid_len == 0 {
return Err(BookError::InvalidConfig {
field: "grid_len",
rule: "must be a multiple of 64, at least 64, at most 2^22",
});
}
if self.tick_size == 0 {
return Err(BookError::InvalidConfig {
field: "tick_size",
rule: "must be at least 1",
});
}
let end = self.grid_end_tick()?;
let highest_tick = end - 1;
highest_tick
.checked_mul(self.tick_size)
.ok_or(BookError::InvalidConfig {
field: "grid end * tick_size",
rule: "the largest raw price on the grid must fit in u64",
})
}
/// Validate every construction rule.
///
/// # Errors
///
/// Returns [`BookError::InvalidConfig`] naming the first field and
/// rule that fail.
pub fn validate(&self) -> Result<(), BookError> {
if self.max_orders == 0 || self.max_orders > (1 << 31) - 2 {
return Err(BookError::InvalidConfig {
field: "max_orders",
rule: "must be at least 1 and at most 2^31 - 2",
});
}
if self.grid_len < 64 || !self.grid_len.is_multiple_of(64) || self.grid_len > 1 << 22 {
return Err(BookError::InvalidConfig {
field: "grid_len",
rule: "must be a multiple of 64, at least 64, at most 2^22",
});
}
if self.tick_size == 0 {
return Err(BookError::InvalidConfig {
field: "tick_size",
rule: "must be at least 1",
});
}
let _ = self.max_price()?;
Ok(())
}
/// Raw price (quote units) of an absolute tick value. Callers pass
/// only ticks inside the validated grid, so the multiplication
/// cannot overflow (see [`BookConfig::validate`]).
#[must_use]
pub(crate) const fn price_of_tick(&self, tick: u64) -> u64 {
tick * self.tick_size
}
}
#[cfg(test)]
mod tests {
use super::*;
fn base() -> BookConfig {
BookConfig {
max_orders: 1024,
grid_len: 4096,
tick_size: 1,
grid_start_tick: 1,
default_stp: StpPolicy::CancelIncoming,
}
}
#[test]
fn valid_config_passes() {
assert!(base().validate().is_ok());
}
#[test]
fn grid_end_overflow_is_a_config_error() {
let mut cfg = base();
cfg.grid_start_tick = u64::MAX - 10;
cfg.grid_len = 64;
assert!(matches!(
cfg.validate(),
Err(BookError::InvalidConfig { .. })
));
}
#[test]
fn grid_len_must_be_a_multiple_of_64() {
let mut cfg = base();
cfg.grid_len = 100;
assert!(matches!(
cfg.validate(),
Err(BookError::InvalidConfig { .. })
));
}
}