thruput-json-rpc-server 0.43.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
pub(super) mod history;

use super::{NativeSimulationPlane, NativeStatePlane, StateKey, invalid, projection, rpc_error};
use crate::{BuildVersion, ChainPlane, RpcRequestContext, RpcServerConfig, RpcServerError};
use jsonrpsee::core::RpcResult;
use std::{future::Future, time::Duration};
use thruput_bundle_identity::BundleIdentity;
use thruput_json_rpc_api::WIRE_FORMAT_VERSION;
use thruput_json_rpc_types::{BlockHeight, NetworkHash, requests as input, responses as out};

/// Configured state and simulation sources, independent of their health.
/// `NativeService` owns indexer and submission registration separately.
#[derive(Debug, Clone, Copy)]
pub struct NativeReadCapabilities {
    db: bool,
    simulation: bool,
}
impl NativeReadCapabilities {
    /// Select configured `DbGateway` and `ViewGateway` sources, not their health.
    pub const fn new(db: bool, simulation: bool) -> Self {
        Self { db, simulation }
    }
    /// Sorted implemented method names for these configured sources.
    pub fn method_names(self) -> Vec<&'static str> {
        let mut names = vec!["getVersionInfo", "getNetworkInfo", "getServerLimits"];
        if self.db {
            names.extend([
                "getChainTip",
                "getChainConfig",
                "getStorage",
                "getBalance",
                "getRegisteredAuth",
            ]);
        }
        if self.simulation {
            names.extend(["simulateInvocation", "simulateInvocations"]);
        }
        names.sort_unstable();
        names
    }
}

/// Read-only producers over caller-owned backend connections.
/// Tip/config calls reuse the backend coalescer and the shared submit cache.
#[derive(Debug)]
pub struct NativeReadService<C, R, V> {
    chain: C,
    state: R,
    simulation: V,
    network_hash: NetworkHash,
    config: RpcServerConfig,
    version: out::VersionInfo,
    capabilities: NativeReadCapabilities,
}

impl<C, R, V> NativeReadService<C, R, V> {
    /// Construct producers without registering public methods.
    pub fn new(
        chain: C,
        state: R,
        simulation: V,
        network_hash: NetworkHash,
        config: RpcServerConfig,
        build: BuildVersion,
        capabilities: NativeReadCapabilities,
    ) -> Self {
        let mut version = out::VersionInfo::new(WIRE_FORMAT_VERSION, vec![], build.crate_version());
        version.commit_hash = build.source_commit().map(str::to_owned);
        version.platform_version = build.platform_version().map(str::to_owned);
        Self {
            chain,
            state,
            simulation,
            network_hash,
            config,
            version,
            capabilities,
        }
    }
    /// Preserve installed-bundle provenance; release fields are not supplied by requests.
    #[must_use]
    pub fn with_bundle_identity(mut self, identity: &BundleIdentity) -> Self {
        identity
            .build_version()
            .crate_version()
            .clone_into(&mut self.version.implementation);
        self.version.commit_hash = identity.build_version().source_commit().map(str::to_owned);
        self.version.platform_version = identity
            .build_version()
            .platform_version()
            .map(str::to_owned);
        self.version.release_version = identity.release_version().map(str::to_owned);
        self.version.bundle_manifest_digest = identity.bundle_manifest_digest().map(str::to_owned);
        self
    }
    /// Implemented methods enabled by this service configuration.
    pub fn method_names(&self) -> Vec<&'static str> {
        self.capabilities.method_names()
    }
    fn context(&self) -> RpcRequestContext {
        RpcRequestContext::new(self.config.request_timeout_ms.map(Duration::from_millis))
    }
    fn require(configured: bool, method: &str) -> Result<(), RpcServerError> {
        if configured {
            Ok(())
        } else {
            Err(RpcServerError::NotImplemented(method.into()))
        }
    }
    async fn within<T>(
        &self,
        context: RpcRequestContext,
        future: impl Future<Output = Result<T, RpcServerError>>,
    ) -> RpcResult<T> {
        let result = if let Some(deadline) = context.deadline() {
            tokio::time::timeout_at(deadline.into(), future)
                .await
                .map_err(|_| RpcServerError::BackendTimeout)
                .and_then(std::convert::identity)
        } else {
            future.await
        };
        result.map_err(|error| rpc_error(&error))
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_version_info(&self, _: input::EmptyRequest) -> RpcResult<out::VersionInfo> {
        let mut result = self.version.clone();
        result.supported_methods = self.method_names().into_iter().map(str::to_owned).collect();
        Ok(result)
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_network_info(&self, _: input::EmptyRequest) -> RpcResult<out::NetworkInfo> {
        Ok(out::NetworkInfo {
            network_hash: self.network_hash,
        })
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_server_limits(&self, _: input::EmptyRequest) -> RpcResult<out::ServerLimits> {
        let config = &self.config;
        let mut result = out::ServerLimits::new(
            config.view_gas_limit,
            config.max_view_calldata_bytes as u64,
            config.event_page_cap(),
            config.transaction_page_cap(),
            config.max_cursor_bytes as u64,
        );
        if self.capabilities.simulation {
            result.simulation_batch = Some(out::SimulationBatchLimits {
                max_invocations: config.view_batch.max_calls,
                max_calldata_bytes: config.view_batch.max_calldata_bytes,
                max_gas: config.view_batch.max_gas,
                max_backend_response_bytes: config.view_batch.max_response_bytes,
                timeout_ms: config
                    .view_timeout_ms
                    .min(config.request_timeout_ms.unwrap_or(u64::MAX)),
            });
        }
        Ok(result)
    }
}
impl<C, R: super::NativeBlockPlane, V> NativeReadService<C, R, V> {
    /// Read committed metadata from the configured indexer. `NativeService` owns
    /// the indexer capability gate; this producer does not register a method.
    #[tracing::instrument(skip_all)]
    pub async fn get_block_by_height(
        &self,
        request: input::BlockQuery,
    ) -> RpcResult<out::BlockResult> {
        let context = self.context();
        self.within(context, async {
            if *request.block_height < 0 {
                return Err(RpcServerError::InvalidParam(
                    "blockHeight must be nonnegative".into(),
                ));
            }
            let result = self
                .state
                .native_block(context, request.block_height.clone())
                .await?;
            if let Some(block) = &result {
                if *block.block_height <= 0 || block.block_height != request.block_height {
                    return Err(invalid("block metadata has an unexpected height"));
                }
                if block.network_hash != self.network_hash {
                    return Err(invalid("block metadata has a different network hash"));
                }
            }
            Ok(result)
        })
        .await
    }
}
impl<C: ChainPlane, R: NativeStatePlane, V: NativeSimulationPlane> NativeReadService<C, R, V> {
    #[tracing::instrument(skip_all)]
    pub async fn get_chain_tip(&self, _: input::EmptyRequest) -> RpcResult<out::ChainTip> {
        let context = self.context();
        self.within(context, async {
            Self::require(self.capabilities.db, "getChainTip")?;
            projection::tip(&self.chain.chain_tip(context).await?.tip)
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_chain_config(&self, _: input::EmptyRequest) -> RpcResult<out::ChainConfig> {
        let context = self.context();
        self.within(context, async {
            Self::require(self.capabilities.db, "getChainConfig")?;
            projection::config(self.chain.chain_tip(context).await?)
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_storage(&self, request: input::StorageQuery) -> RpcResult<out::StorageResult> {
        let context = self.context();
        self.within(context, async {
            Self::require(self.capabilities.db, "getStorage")?;
            let key = request.key.0.as_slice().try_into().map_err(|_| {
                RpcServerError::InvalidParam("storage key must be exactly 32 bytes".into())
            })?;
            let result = self
                .state
                .native_storage(context, request.address, StateKey::ContractData(key))
                .await?;
            snapshot(&result.state_block_height)?;
            Ok(result)
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_balance(&self, request: input::AddressQuery) -> RpcResult<out::BalanceResult> {
        let context = self.context();
        self.within(context, async {
            Self::require(self.capabilities.db, "getBalance")?;
            let result = self
                .state
                .native_storage(context, request.address, StateKey::FreeNativeTokenBalance)
                .await?;
            snapshot(&result.state_block_height)?;
            let raw_balance = match result.item {
                None => 0,
                Some(out::StorageItem::NonnegativeInt { value }) if value >= 0 => value,
                _ => return Err(invalid("balance row is not a nonnegative integer")),
            };
            Ok(out::BalanceResult {
                address: request.address,
                state_block_height: result.state_block_height,
                raw_balance,
            })
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn get_registered_auth(
        &self,
        request: input::AddressQuery,
    ) -> RpcResult<out::RegisteredAuthResult> {
        let context = self.context();
        self.within(context, async {
            Self::require(self.capabilities.db, "getRegisteredAuth")?;
            let result = self
                .state
                .native_registered_auth(context, request.address)
                .await?;
            snapshot(&result.state_block_height)?;
            if result.address != request.address {
                return Err(invalid("authorization response has a different address"));
            }
            Ok(result)
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn simulate_invocation(
        &self,
        request: input::SimulateInvocationRequest,
    ) -> RpcResult<out::SimulationResult> {
        let context = self
            .context()
            .capped_from_start(Duration::from_millis(self.config.view_timeout_ms));
        self.within(context, async {
            Self::require(self.capabilities.simulation, "simulateInvocation")?;
            floor(request.min_state_block_height.as_ref())?;
            let gas = validate_call(
                &request.entrypoint.0,
                request.calldata.0.len(),
                request.gas_limit,
                &self.config,
            )?;
            let min = request.min_state_block_height.clone();
            let result = self.simulation.simulate(context, request, gas).await?;
            snapshot(&result.state_block_height)?;
            if let Some(requested) = min.filter(|min| result.state_block_height < *min) {
                return Err(RpcServerError::ViewHeightUnavailable(
                    thruput_json_rpc_types::view::ViewHeightUnavailable {
                        requested,
                        observed: result.state_block_height,
                    },
                ));
            }
            Ok(result)
        })
        .await
    }
    #[tracing::instrument(skip_all)]
    pub async fn simulate_invocations(
        &self,
        request: input::SimulateInvocationsRequest,
    ) -> RpcResult<out::SimulationResults> {
        let context = self
            .context()
            .capped_from_start(Duration::from_millis(self.config.view_timeout_ms));
        self.within(context, async {
            Self::require(self.capabilities.simulation, "simulateInvocations")?;
            floor(request.min_state_block_height.as_ref())?;
            let count = request.invocations.len();
            if count == 0 || count > self.config.view_batch.max_calls as usize {
                return Err(RpcServerError::InvalidParam(
                    "invocation count exceeds endpoint limits or is empty".into(),
                ));
            }
            let mut gas_left = self.config.view_batch.max_gas;
            let mut bytes_left = self.config.view_batch.max_calldata_bytes as usize;
            for call in &request.invocations {
                let gas = validate_call(
                    &call.entrypoint.0,
                    call.calldata.0.len(),
                    call.gas_limit,
                    &self.config,
                )?;
                gas_left = gas_left.checked_sub(gas).ok_or_else(|| {
                    RpcServerError::InvalidParam(
                        "aggregate simulation gas exceeds endpoint limit".into(),
                    )
                })?;
                bytes_left = bytes_left
                    .checked_sub(call.calldata.0.len())
                    .ok_or_else(|| {
                        RpcServerError::InvalidParam(
                            "aggregate simulation calldata exceeds endpoint limit".into(),
                        )
                    })?;
            }
            let min = request.min_state_block_height.clone();
            let result = self
                .simulation
                .simulate_batch(
                    context,
                    request,
                    self.config.view_gas_limit,
                    self.config.view_batch,
                )
                .await?;
            snapshot(&result.state_block_height)?;
            if result.results.len() != count {
                return Err(RpcServerError::invalid_data(
                    super::BackendDataInvalidReason::InvalidCardinality,
                    "simulation result count mismatch",
                ));
            }
            if min.is_some_and(|min| result.state_block_height < min) {
                return Err(invalid(
                    "batch success is below the requested snapshot floor",
                ));
            }
            Ok(result)
        })
        .await
    }
}
pub(super) fn snapshot(height: &BlockHeight) -> Result<(), RpcServerError> {
    if **height == -1 {
        return Err(RpcServerError::BackendUnavailable(
            "no committed snapshot".into(),
        ));
    }
    if **height < 0 {
        Err(invalid("negative committed snapshot height"))
    } else {
        Ok(())
    }
}
fn floor(height: Option<&BlockHeight>) -> Result<(), RpcServerError> {
    if height.is_some_and(|height| **height < 0) {
        Err(RpcServerError::InvalidParam(
            "minStateBlockHeight must be nonnegative".into(),
        ))
    } else {
        Ok(())
    }
}
fn validate_call(
    entrypoint: &[u8],
    bytes: usize,
    gas: Option<u64>,
    config: &RpcServerConfig,
) -> Result<u64, RpcServerError> {
    if entrypoint.len() != 8 {
        return Err(RpcServerError::InvalidParam(
            "entrypoint must be exactly 8 bytes".into(),
        ));
    }
    if bytes > config.max_view_calldata_bytes {
        return Err(RpcServerError::InvalidParam(
            "simulation calldata exceeds endpoint limit".into(),
        ));
    }
    let gas = gas.unwrap_or(config.view_gas_limit);
    if gas == 0 || gas > config.view_gas_limit {
        return Err(RpcServerError::InvalidParam(
            "gasLimit must be positive and within the endpoint limit".into(),
        ));
    }
    Ok(gas)
}

impl<C: ChainPlane, R, V> NativeReadService<C, R, V> {
    /// Read this local runtime's pending pool count, without claiming network-wide state.
    #[tracing::instrument(skip_all)]
    pub async fn get_pending_transaction_count(
        &self,
        _: input::EmptyRequest,
    ) -> RpcResult<out::PendingTransactionCount> {
        let context = self.context();
        self.within(context, async {
            Ok(out::PendingTransactionCount {
                count: self.chain.pending_transaction_count(context).await?,
            })
        })
        .await
    }
}