thruput-json-rpc-server 0.42.0

Reusable JSON-RPC server layer for Thruput
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Validate native history at the shared service boundary without reading cursors.

use super::{NativeReadService, input, invalid, out};
use crate::{RpcServerError, native::read::NativeHistoryPlane};
use jsonrpsee::core::RpcResult;
use std::cmp::Ordering;
use thruput_json_rpc_types::{BlockHeight, events, hex::HexBytes};

fn parameter(message: &str) -> RpcServerError {
    RpcServerError::InvalidParam(message.into())
}

fn prepare<S>(
    start: Option<&S>,
    cursor: Option<&HexBytes>,
    limit: &mut Option<u32>,
    cap: u32,
    max_cursor_bytes: usize,
) -> Result<(), RpcServerError> {
    if start.is_some() == cursor.is_some() {
        return Err(parameter("require exactly one of start or cursor"));
    }
    if cursor.is_some_and(|c| c.0.is_empty() || c.0.len() > max_cursor_bytes) {
        return Err(parameter("cursor length outside configured bounds"));
    }
    let effective = limit.unwrap_or(50.min(cap));
    if effective == 0 || effective > cap {
        return Err(parameter("limit outside configured bounds"));
    }
    *limit = Some(effective);
    Ok(())
}

fn direction(start: &input::HistoryStart) -> Option<events::IterationOrder> {
    match start {
        input::HistoryStart::Ascending { .. } => Some(events::IterationOrder::Ascending),
        input::HistoryStart::Descending { .. } => Some(events::IterationOrder::Descending),
        _ => None,
    }
}

fn anchor(start: &input::HistoryStart) -> Result<Option<&BlockHeight>, RpcServerError> {
    let value = match start {
        input::HistoryStart::Ascending { anchor } => match anchor {
            input::AscendingAnchor::AtBeginning {} => None,
            input::AscendingAnchor::AtHeight { block_height } => Some(block_height),
            _ => return Err(parameter("unsupported history anchor")),
        },
        input::HistoryStart::Descending { anchor } => match anchor {
            input::DescendingAnchor::AtEnd {} => None,
            input::DescendingAnchor::AtHeight { block_height } => Some(block_height),
            _ => return Err(parameter("unsupported history anchor")),
        },
        _ => return Err(parameter("unsupported history direction")),
    };
    if value.is_some_and(|height| **height < 0) {
        return Err(parameter("blockHeight must be nonnegative"));
    }
    Ok(value)
}

fn validate_filters(filters: &[input::EventFilter]) -> Result<(), RpcServerError> {
    if filters.is_empty() || filters.len() > events::MAX_EVENT_FILTERS_PER_QUERY as usize {
        return Err(parameter("filters outside event-query bounds"));
    }
    for filter in filters {
        if filter.addresses.is_empty()
            || filter.addresses.len() > events::MAX_CONTRACT_ADDRESSES_PER_EVENT_FILTER as usize
        {
            return Err(parameter("addresses outside event-filter bounds"));
        }
        if let Some(topics) = &filter.topics {
            for position in [&topics.topic0, &topics.topic1, &topics.topic2]
                .into_iter()
                .flatten()
            {
                if position.len() > events::MAX_TOPIC_VALUES_PER_POSITION as usize {
                    return Err(parameter("topics outside event-filter bounds"));
                }
            }
        }
    }
    Ok(())
}

fn page(
    count: usize,
    next: Option<&HexBytes>,
    oldest: &BlockHeight,
    latest: &BlockHeight,
    cursor: Option<&HexBytes>,
    limit: Option<u32>,
    max_cursor_bytes: usize,
) -> Result<(), RpcServerError> {
    if **oldest < 0 || latest < oldest || count > limit.unwrap_or(0) as usize {
        return Err(invalid("history page has invalid bounds or cardinality"));
    }
    if next.is_some_and(|next| {
        next.0.is_empty() || next.0.len() > max_cursor_bytes || Some(next) == cursor
    }) {
        return Err(invalid("history page has an invalid or repeated cursor"));
    }
    Ok(())
}

fn ordered(
    comparisons: impl Iterator<Item = Ordering>,
    order: Option<events::IterationOrder>,
) -> bool {
    let mut ascending = true;
    let mut descending = true;
    for comparison in comparisons {
        ascending &= comparison.is_lt();
        descending &= comparison.is_gt();
    }
    match order {
        Some(events::IterationOrder::Ascending) => ascending,
        Some(events::IterationOrder::Descending) => descending,
        None => ascending || descending,
    }
}

fn in_range(
    height: &BlockHeight,
    oldest: &BlockHeight,
    latest: &BlockHeight,
    start: Option<&input::HistoryStart>,
) -> bool {
    if height < oldest || height > latest {
        return false;
    }
    match start {
        Some(input::HistoryStart::Ascending {
            anchor: input::AscendingAnchor::AtHeight { block_height },
        }) => height >= block_height,
        Some(input::HistoryStart::Descending {
            anchor: input::DescendingAnchor::AtHeight { block_height },
        }) => height <= block_height,
        _ => true,
    }
}

fn retained_anchor(
    start: Option<&input::HistoryStart>,
    oldest: &BlockHeight,
) -> Result<(), RpcServerError> {
    if let Some(start) = start
        && anchor(start)?.is_some_and(|height| height < oldest)
    {
        return Err(invalid(
            "successful history page excludes the requested anchor",
        ));
    }
    Ok(())
}

fn matches_filter(event: &out::EmittedEvent, filter: &input::EventFilter) -> bool {
    if !filter.addresses.contains(&event.address) {
        return false;
    }
    let Some(topics) = &filter.topics else {
        return true;
    };
    [&topics.topic0, &topics.topic1, &topics.topic2]
        .iter()
        .enumerate()
        .all(|(index, values)| {
            values.as_ref().is_none_or(|values| {
                values.is_empty()
                    || event
                        .topics
                        .get(index)
                        .is_some_and(|topic| values.contains(topic))
            })
        })
}

/// Validate an event page against its prepared request before recording backend success.
/// The request limit must already contain the effective page limit.
pub fn validate_event_page(
    request: &input::EventQuery,
    result: &out::EventPage,
    max_cursor_bytes: usize,
) -> Result<(), RpcServerError> {
    page(
        result.events.len(),
        result.next_cursor.as_ref(),
        &result.oldest_indexed_block_height,
        &result.latest_indexed_block_height,
        request.cursor.as_ref(),
        request.limit,
        max_cursor_bytes,
    )?;
    retained_anchor(request.start.as_ref(), &result.oldest_indexed_block_height)?;
    let mut identities = std::collections::BTreeSet::new();
    for event in &result.events {
        let hash: &[u8] = event.transaction_hash.0.0.as_ref();
        if !identities.insert((hash, event.sequence_number)) {
            return Err(invalid("history page repeats an event identity"));
        }
        if !in_range(
            &event.block_height,
            &result.oldest_indexed_block_height,
            &result.latest_indexed_block_height,
            request.start.as_ref(),
        ) || event.event.topics.len() > 3
            || !request
                .filters
                .iter()
                .any(|filter| matches_filter(&event.event, filter))
        {
            return Err(invalid("event does not satisfy its history query"));
        }
    }
    let comparisons = result.events.windows(2).map(|pair| {
        let left: &[u8] = pair[0].transaction_hash.0.0.as_ref();
        let right: &[u8] = pair[1].transaction_hash.0.0.as_ref();
        (&pair[0].block_height, left, pair[0].sequence_number).cmp(&(
            &pair[1].block_height,
            right,
            pair[1].sequence_number,
        ))
    });
    if !ordered(comparisons, request.start.as_ref().and_then(direction)) {
        return Err(invalid("history events are not strictly ordered"));
    }
    Ok(())
}

fn validate_refs(
    result: &out::TransactionRefPage,
    order: Option<events::IterationOrder>,
) -> Result<(), RpcServerError> {
    let mut hashes = std::collections::BTreeSet::new();
    for item in &result.transactions {
        let hash: &[u8] = item.transaction_hash.0.0.as_ref();
        if !hashes.insert(hash) {
            return Err(invalid("history page repeats a transaction"));
        }
    }
    let comparisons = result.transactions.windows(2).map(|pair| {
        let left: &[u8] = pair[0].transaction_hash.0.0.as_ref();
        let right: &[u8] = pair[1].transaction_hash.0.0.as_ref();
        (&pair[0].block_height, left).cmp(&(&pair[1].block_height, right))
    });
    if !ordered(comparisons, order) {
        return Err(invalid("history references are not strictly ordered"));
    }
    Ok(())
}

/// Validate a root-address page before recording backend success.
/// The request limit must already contain the effective page limit.
pub fn validate_root_transaction_page(
    request: &input::RootAddressQuery,
    result: &out::TransactionRefPage,
    max_cursor_bytes: usize,
) -> Result<(), RpcServerError> {
    page(
        result.transactions.len(),
        result.next_cursor.as_ref(),
        &result.oldest_indexed_block_height,
        &result.latest_indexed_block_height,
        request.cursor.as_ref(),
        request.limit,
        max_cursor_bytes,
    )?;
    retained_anchor(request.start.as_ref(), &result.oldest_indexed_block_height)?;
    if result.transactions.iter().any(|item| {
        !in_range(
            &item.block_height,
            &result.oldest_indexed_block_height,
            &result.latest_indexed_block_height,
            request.start.as_ref(),
        )
    }) {
        return Err(invalid("reference does not satisfy its history range"));
    }
    validate_refs(result, request.start.as_ref().and_then(direction))?;
    Ok(())
}

/// Validate an exact-block page before recording backend success.
/// The request limit must already contain the effective page limit.
pub fn validate_block_transaction_page(
    request: &input::BlockPageRequest,
    result: &out::BlockTransactionPage,
    max_cursor_bytes: usize,
) -> Result<(), RpcServerError> {
    page(
        result.transactions.len(),
        result.next_cursor.as_ref(),
        &result.oldest_indexed_block_height,
        &result.latest_indexed_block_height,
        request.cursor.as_ref(),
        request.limit,
        max_cursor_bytes,
    )?;
    if request.cursor.is_some()
        && (request.block_height < result.oldest_indexed_block_height
            || request.block_height > result.latest_indexed_block_height)
    {
        return Err(invalid(
            "successful block continuation is outside indexed coverage",
        ));
    }
    if *result.oldest_indexed_block_height < 1
        || (result.transactions.is_empty() && result.next_cursor.is_some())
        || result.transactions.iter().any(|item| {
            item.block_height != request.block_height
                || !in_range(
                    &item.block_height,
                    &result.oldest_indexed_block_height,
                    &result.latest_indexed_block_height,
                    None,
                )
        })
    {
        return Err(invalid("invalid exact-block reference page"));
    }
    validate_refs(result, request.start.as_ref().map(|start| start.order))?;
    Ok(())
}

impl<C, R: NativeHistoryPlane, V> NativeReadService<C, R, V> {
    /// Scan an indexed event range; an empty page can still have a continuation.
    #[tracing::instrument(skip_all)]
    pub async fn get_events(&self, mut request: input::EventQuery) -> RpcResult<out::EventPage> {
        let context = self.context();
        self.within(context, async {
            prepare(
                request.start.as_ref(),
                request.cursor.as_ref(),
                &mut request.limit,
                self.config.event_page_cap(),
                self.config.max_cursor_bytes,
            )?;
            if let Some(start) = &request.start {
                anchor(start)?;
            }
            validate_filters(&request.filters)?;
            let result = self.state.native_events(context, request.clone()).await?;
            validate_event_page(&request, &result, self.config.max_cursor_bytes)?;
            Ok(result)
        })
        .await
    }

    /// Read root-address references without claiming internal-call or transfer history.
    #[tracing::instrument(skip_all)]
    pub async fn get_transactions_by_root_address(
        &self,
        mut request: input::RootAddressQuery,
    ) -> RpcResult<out::TransactionRefPage> {
        let context = self.context();
        self.within(context, async {
            prepare(
                request.start.as_ref(),
                request.cursor.as_ref(),
                &mut request.limit,
                self.config.transaction_page_cap(),
                self.config.max_cursor_bytes,
            )?;
            if let Some(start) = &request.start {
                anchor(start)?;
            }
            let result = self
                .state
                .native_root_transactions(context, request.clone())
                .await?;
            validate_root_transaction_page(&request, &result, self.config.max_cursor_bytes)?;
            Ok(result)
        })
        .await
    }

    /// Read exactly one block, with cursor-owned continuation order.
    #[tracing::instrument(skip_all)]
    pub async fn get_transactions_by_block(
        &self,
        mut request: input::BlockPageRequest,
    ) -> RpcResult<out::BlockTransactionPage> {
        let context = self.context();
        self.within(context, async {
            prepare(
                request.start.as_ref(),
                request.cursor.as_ref(),
                &mut request.limit,
                self.config.transaction_page_cap(),
                self.config.max_cursor_bytes,
            )?;
            if *request.block_height < 0 {
                return Err(parameter("blockHeight must be nonnegative"));
            }
            let result = self
                .state
                .native_block_transactions(context, request.clone())
                .await?;
            validate_block_transaction_page(&request, &result, self.config.max_cursor_bytes)?;
            Ok(result)
        })
        .await
    }
}

#[cfg(test)]
mod tests;