chainrpc-core 0.2.2

Transport trait, policy engine and types for ChainRPC
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Unified `ChainClient` trait — a high-level typed abstraction over any
//! blockchain's RPC interface.
//!
//! While [`RpcTransport`] operates at the raw JSON-RPC level (any method, any
//! params), `ChainClient` provides a minimal, chain-agnostic API for common
//! operations like fetching the current block height or retrieving a block by
//! number.
//!
//! Each chain family (EVM, Solana, Cosmos, Substrate, Bitcoin, Aptos, Sui)
//! provides its own implementation wrapping the appropriate transport.

use async_trait::async_trait;
use serde::de::Error as _;
use serde::{Deserialize, Serialize};

use crate::error::TransportError;

// ─── ChainBlock ──────────────────────────────────────────────────────────────

/// A chain-agnostic block summary.
///
/// Contains only the fields that every blockchain provides. Individual chain
/// clients may expose richer block types through chain-specific methods.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainBlock {
    /// Block height / slot / checkpoint number.
    pub height: u64,
    /// Block hash (hex string, chain-specific format).
    pub hash: String,
    /// Parent block hash.
    pub parent_hash: String,
    /// Block timestamp (Unix seconds).
    pub timestamp: i64,
    /// Number of transactions in the block.
    pub tx_count: u32,
}

// ─── ChainClient trait ──────────────────────────────────────────────────────

/// A high-level, chain-agnostic blockchain client.
///
/// Provides a minimal set of operations that any blockchain supports.
/// Use this trait when writing chain-agnostic infrastructure (indexers,
/// monitors, dashboards) that needs to work across multiple blockchain
/// families.
///
/// For chain-specific operations (e.g. EVM `eth_getLogs`, Solana
/// `getSignaturesForAddress`), use the concrete client types directly.
#[async_trait]
pub trait ChainClient: Send + Sync {
    /// Get the current head height (block number / slot / checkpoint).
    async fn get_head_height(&self) -> Result<u64, TransportError>;

    /// Get a block by its height.
    ///
    /// Returns `None` if the block does not exist (e.g. future block number).
    async fn get_block_by_height(
        &self,
        height: u64,
    ) -> Result<Option<ChainBlock>, TransportError>;

    /// Return the chain identifier (e.g. `"1"` for Ethereum mainnet,
    /// `"mainnet-beta"` for Solana).
    fn chain_id(&self) -> &str;

    /// Return the chain family name (e.g. `"evm"`, `"solana"`, `"cosmos"`).
    fn chain_family(&self) -> &str;

    /// Perform a health check against the underlying transport.
    async fn health_check(&self) -> Result<bool, TransportError>;
}

// ─── EvmChainClient ─────────────────────────────────────────────────────────

use std::sync::Arc;
use crate::transport::RpcTransport;

/// EVM implementation of [`ChainClient`].
///
/// Wraps any `Arc<dyn RpcTransport>` and translates `ChainClient` methods
/// into the appropriate `eth_*` JSON-RPC calls.
pub struct EvmChainClient {
    transport: Arc<dyn RpcTransport>,
    chain_id: String,
}

impl EvmChainClient {
    /// Create an EVM chain client with the given transport and chain ID.
    pub fn new(transport: Arc<dyn RpcTransport>, chain_id: impl Into<String>) -> Self {
        Self {
            transport,
            chain_id: chain_id.into(),
        }
    }
}

#[async_trait]
impl ChainClient for EvmChainClient {
    async fn get_head_height(&self) -> Result<u64, TransportError> {
        let req = crate::request::JsonRpcRequest::new(
            1,
            "eth_blockNumber",
            vec![],
        );
        let resp = self.transport.send(req).await?;
        let result = resp.into_result().map_err(TransportError::Rpc)?;
        let hex_str = result
            .as_str()
            .ok_or_else(|| TransportError::Deserialization(
                serde_json::Error::custom("expected hex string for block number"),
            ))?;
        let stripped = hex_str.strip_prefix("0x").unwrap_or(hex_str);
        u64::from_str_radix(stripped, 16).map_err(|e| {
            TransportError::Deserialization(serde_json::Error::custom(format!(
                "invalid block number hex: {e}"
            )))
        })
    }

    async fn get_block_by_height(
        &self,
        height: u64,
    ) -> Result<Option<ChainBlock>, TransportError> {
        let hex_height = format!("0x{height:x}");
        let req = crate::request::JsonRpcRequest::new(
            1,
            "eth_getBlockByNumber",
            vec![
                serde_json::Value::String(hex_height),
                serde_json::Value::Bool(false), // don't include full txs
            ],
        );
        let resp = self.transport.send(req).await?;
        let result = resp.into_result().map_err(TransportError::Rpc)?;

        if result.is_null() {
            return Ok(None);
        }

        let hash = result["hash"]
            .as_str()
            .unwrap_or_default()
            .to_string();
        let parent_hash = result["parentHash"]
            .as_str()
            .unwrap_or_default()
            .to_string();
        let timestamp = parse_hex_u64(result["timestamp"].as_str().unwrap_or("0x0"))
            as i64;
        let tx_count = result["transactions"]
            .as_array()
            .map(|a| a.len() as u32)
            .unwrap_or(0);

        Ok(Some(ChainBlock {
            height,
            hash,
            parent_hash,
            timestamp,
            tx_count,
        }))
    }

    fn chain_id(&self) -> &str {
        &self.chain_id
    }

    fn chain_family(&self) -> &str {
        "evm"
    }

    async fn health_check(&self) -> Result<bool, TransportError> {
        // Simple: if eth_blockNumber succeeds, we're healthy
        self.get_head_height().await.map(|_| true)
    }
}

// ─── SolanaChainClient ──────────────────────────────────────────────────────

/// Solana implementation of [`ChainClient`].
///
/// Wraps any `Arc<dyn RpcTransport>` and translates `ChainClient` methods
/// into the appropriate Solana JSON-RPC calls.
pub struct SolanaChainClient {
    transport: Arc<dyn RpcTransport>,
    chain_id: String,
}

impl SolanaChainClient {
    /// Create a Solana chain client.
    pub fn new(transport: Arc<dyn RpcTransport>, chain_id: impl Into<String>) -> Self {
        Self {
            transport,
            chain_id: chain_id.into(),
        }
    }
}

#[async_trait]
impl ChainClient for SolanaChainClient {
    async fn get_head_height(&self) -> Result<u64, TransportError> {
        let req = crate::request::JsonRpcRequest::new(
            1,
            "getSlot",
            vec![],
        );
        let resp = self.transport.send(req).await?;
        let result = resp.into_result().map_err(TransportError::Rpc)?;
        // Solana returns slot as a JSON number, not hex
        result.as_u64().ok_or_else(|| {
            TransportError::Deserialization(serde_json::Error::custom(
                "expected u64 for slot number",
            ))
        })
    }

    async fn get_block_by_height(
        &self,
        height: u64,
    ) -> Result<Option<ChainBlock>, TransportError> {
        let req = crate::request::JsonRpcRequest::new(
            1,
            "getBlock",
            vec![
                serde_json::Value::Number(serde_json::Number::from(height)),
                serde_json::json!({
                    "encoding": "json",
                    "transactionDetails": "none",
                    "rewards": false,
                }),
            ],
        );
        let resp = self.transport.send(req).await?;
        let result = resp.into_result().map_err(TransportError::Rpc)?;

        if result.is_null() {
            return Ok(None);
        }

        let hash = result["blockhash"]
            .as_str()
            .unwrap_or_default()
            .to_string();
        let parent_hash = result["previousBlockhash"]
            .as_str()
            .unwrap_or_default()
            .to_string();
        let timestamp = result["blockTime"].as_i64().unwrap_or(0);
        let tx_count = result["transactions"]
            .as_array()
            .map(|a| a.len() as u32)
            .unwrap_or(0);

        Ok(Some(ChainBlock {
            height,
            hash,
            parent_hash,
            timestamp,
            tx_count,
        }))
    }

    fn chain_id(&self) -> &str {
        &self.chain_id
    }

    fn chain_family(&self) -> &str {
        "solana"
    }

    async fn health_check(&self) -> Result<bool, TransportError> {
        let req = crate::request::JsonRpcRequest::new(
            1,
            "getHealth",
            vec![],
        );
        let resp = self.transport.send(req).await?;
        let result = resp.into_result().map_err(TransportError::Rpc)?;
        Ok(result.as_str() == Some("ok"))
    }
}

// ─── Helpers ────────────────────────────────────────────────────────────────

fn parse_hex_u64(hex_str: &str) -> u64 {
    let stripped = hex_str.strip_prefix("0x").unwrap_or(hex_str);
    u64::from_str_radix(stripped, 16).unwrap_or(0)
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::request::{JsonRpcRequest, JsonRpcResponse, RpcId};
    use std::sync::Mutex;

    /// A mock transport that records requests and returns pre-configured responses.
    struct MockTransport {
        url: String,
        responses: Mutex<Vec<JsonRpcResponse>>,
        recorded_requests: Mutex<Vec<(String, Vec<serde_json::Value>)>>,
    }

    impl MockTransport {
        fn new(responses: Vec<JsonRpcResponse>) -> Self {
            Self {
                url: "mock://test".to_string(),
                responses: Mutex::new(responses),
                recorded_requests: Mutex::new(Vec::new()),
            }
        }

        fn recorded(&self) -> Vec<(String, Vec<serde_json::Value>)> {
            self.recorded_requests.lock().unwrap().clone()
        }
    }

    #[async_trait]
    impl RpcTransport for MockTransport {
        async fn send(&self, req: JsonRpcRequest) -> Result<JsonRpcResponse, TransportError> {
            self.recorded_requests.lock().unwrap().push((
                req.method.clone(),
                req.params.clone(),
            ));
            let mut responses = self.responses.lock().unwrap();
            if responses.is_empty() {
                Err(TransportError::Other("no more mock responses".into()))
            } else {
                Ok(responses.remove(0))
            }
        }

        fn url(&self) -> &str {
            &self.url
        }
    }

    fn ok_response(result: serde_json::Value) -> JsonRpcResponse {
        JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id: RpcId::Number(1),
            result: Some(result),
            error: None,
        }
    }

    // ── EVM tests ───────────────────────────────────────────────────────

    #[tokio::test]
    async fn evm_get_head_height() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::String("0x10".to_string())),
        ]));
        let client = EvmChainClient::new(transport.clone(), "1");

        let height = client.get_head_height().await.unwrap();
        assert_eq!(height, 16);

        let reqs = transport.recorded();
        assert_eq!(reqs[0].0, "eth_blockNumber");
    }

    #[tokio::test]
    async fn evm_get_block_by_height() {
        let block_json = serde_json::json!({
            "hash": "0xabc123",
            "parentHash": "0xdef456",
            "timestamp": "0x60000000",
            "transactions": ["0xtx1", "0xtx2", "0xtx3"]
        });
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(block_json),
        ]));
        let client = EvmChainClient::new(transport.clone(), "1");

        let block = client.get_block_by_height(100).await.unwrap().unwrap();
        assert_eq!(block.height, 100);
        assert_eq!(block.hash, "0xabc123");
        assert_eq!(block.parent_hash, "0xdef456");
        assert_eq!(block.tx_count, 3);

        let reqs = transport.recorded();
        assert_eq!(reqs[0].0, "eth_getBlockByNumber");
        assert_eq!(reqs[0].1[0], serde_json::Value::String("0x64".to_string()));
    }

    #[tokio::test]
    async fn evm_get_block_null() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::Null),
        ]));
        let client = EvmChainClient::new(transport, "1");

        let block = client.get_block_by_height(99999999).await.unwrap();
        assert!(block.is_none());
    }

    #[tokio::test]
    async fn evm_chain_metadata() {
        let transport = Arc::new(MockTransport::new(vec![]));
        let client = EvmChainClient::new(transport, "137");
        assert_eq!(client.chain_id(), "137");
        assert_eq!(client.chain_family(), "evm");
    }

    #[tokio::test]
    async fn evm_health_check() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::String("0x1".to_string())),
        ]));
        let client = EvmChainClient::new(transport, "1");
        assert!(client.health_check().await.unwrap());
    }

    // ── Solana tests ────────────────────────────────────────────────────

    #[tokio::test]
    async fn solana_get_head_height() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::Number(200_000_000u64.into())),
        ]));
        let client = SolanaChainClient::new(transport.clone(), "mainnet-beta");

        let slot = client.get_head_height().await.unwrap();
        assert_eq!(slot, 200_000_000);

        let reqs = transport.recorded();
        assert_eq!(reqs[0].0, "getSlot");
    }

    #[tokio::test]
    async fn solana_get_block_by_height() {
        let block_json = serde_json::json!({
            "blockhash": "5abc123def",
            "previousBlockhash": "4abc123def",
            "blockTime": 1700000000i64,
            "transactions": [{"tx": 1}, {"tx": 2}]
        });
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(block_json),
        ]));
        let client = SolanaChainClient::new(transport.clone(), "mainnet-beta");

        let block = client.get_block_by_height(100).await.unwrap().unwrap();
        assert_eq!(block.height, 100);
        assert_eq!(block.hash, "5abc123def");
        assert_eq!(block.parent_hash, "4abc123def");
        assert_eq!(block.timestamp, 1700000000);
        assert_eq!(block.tx_count, 2);

        let reqs = transport.recorded();
        assert_eq!(reqs[0].0, "getBlock");
    }

    #[tokio::test]
    async fn solana_health_check_ok() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::String("ok".to_string())),
        ]));
        let client = SolanaChainClient::new(transport, "mainnet-beta");
        assert!(client.health_check().await.unwrap());
    }

    #[tokio::test]
    async fn solana_health_check_behind() {
        let transport = Arc::new(MockTransport::new(vec![
            ok_response(serde_json::Value::String("behind".to_string())),
        ]));
        let client = SolanaChainClient::new(transport, "mainnet-beta");
        assert!(!client.health_check().await.unwrap());
    }

    #[tokio::test]
    async fn solana_chain_metadata() {
        let transport = Arc::new(MockTransport::new(vec![]));
        let client = SolanaChainClient::new(transport, "devnet");
        assert_eq!(client.chain_id(), "devnet");
        assert_eq!(client.chain_family(), "solana");
    }

    // ── ChainBlock tests ────────────────────────────────────────────────

    #[test]
    fn chain_block_serde_roundtrip() {
        let block = ChainBlock {
            height: 100,
            hash: "0xabc".to_string(),
            parent_hash: "0xdef".to_string(),
            timestamp: 1700000000,
            tx_count: 42,
        };
        let json = serde_json::to_string(&block).unwrap();
        let back: ChainBlock = serde_json::from_str(&json).unwrap();
        assert_eq!(back.height, 100);
        assert_eq!(back.tx_count, 42);
    }

    #[test]
    fn parse_hex_u64_works() {
        assert_eq!(parse_hex_u64("0x10"), 16);
        assert_eq!(parse_hex_u64("0xff"), 255);
        assert_eq!(parse_hex_u64("10"), 16);
        assert_eq!(parse_hex_u64("0x0"), 0);
        assert_eq!(parse_hex_u64("invalid"), 0);
    }
}