accumulate-sdk 2.1.0

Accumulate Rust SDK (V2/V3 unified) with DevNet-first flows
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
//! Type matrix for comprehensive roundtrip testing
//!
//! This module contains all protocol types that need to be tested for
//! encode → decode → re-encode roundtrip consistency.

use serde::{Deserialize, Serialize};
use crate::codec::transaction_codec::{TransactionHeader, TransactionSignature};

/// All protocol type names that must pass roundtrip tests
pub const TYPE_NAMES: &[&str] = &[
    // Core transaction types
    "TransactionEnvelope",
    "TransactionHeader",
    "TransactionSignature",
    "TransactionKeyPage",

    // Transaction body builders and components
    "TokenRecipient",
    "KeySpec",

    // API response types
    "StatusResponse",
    "NodeInfo",
    "QueryResponse",
    "TransactionResponse",
    "TransactionResult",
    "Event",
    "Attribute",
    "SignedTransaction",
    "Signature",
    "Account",
    "FaucetResponse",

    // V3 specific types
    "V3SubmitRequest",
    "V3SubmitResponse",
    "SubmitResult",
    "V3Signature",

    // Protocol types
    "ProtocolTransactionEnvelope",
    "ProtocolTransactionSignature",
    "ProtocolTransactionHeader",

    // Codec support types
    "BinaryReader",
    "BinaryWriter",
    "EncodingError",
    "DecodingError",
    "FieldReader",

    // Crypto types
    "Ed25519Signer",

    // Client configuration
    "AccOptions",
];

/// Trait for types that can generate safe sample instances for testing
pub trait SampleGenerator {
    /// Generate a safe sample instance suitable for roundtrip testing
    fn generate_sample() -> Self;

    /// Generate multiple sample instances with different characteristics
    fn generate_samples() -> Vec<Self> where Self: Sized {
        vec![Self::generate_sample()]
    }
}

/// Trait for types that support roundtrip encoding/decoding
pub trait RoundtripTestable: Serialize + for<'de> Deserialize<'de> + Clone + PartialEq {
    /// Test JSON roundtrip: serialize → deserialize → re-serialize
    fn test_json_roundtrip(&self) -> Result<(), String> {
        // Serialize to JSON
        let json = serde_json::to_string(self)
            .map_err(|e| format!("Failed to serialize to JSON: {}", e))?;

        // Deserialize from JSON
        let deserialized: Self = serde_json::from_str(&json)
            .map_err(|e| format!("Failed to deserialize from JSON: {}", e))?;

        // Re-serialize to JSON
        let json2 = serde_json::to_string(&deserialized)
            .map_err(|e| format!("Failed to re-serialize to JSON: {}", e))?;

        // Compare original and deserialized objects
        if self != &deserialized {
            return Err("Deserialized object differs from original".to_string());
        }

        // Compare JSON strings
        if json != json2 {
            return Err(format!(
                "Re-serialized JSON differs from original\nOriginal: {}\nRe-serialized: {}",
                json, json2
            ));
        }

        Ok(())
    }

    /// Test binary roundtrip if the type supports binary encoding
    fn test_binary_roundtrip(&self) -> Result<(), String> {
        // Default implementation - override for types with binary encoding
        Ok(())
    }
}

// Implement SampleGenerator for core transaction types

impl SampleGenerator for crate::codec::TransactionEnvelope {
    fn generate_sample() -> Self {
        use crate::codec::{TransactionHeader, TransactionSignature};

        Self {
            header: TransactionHeader {
                principal: "acc://alice.acme/tokens".to_string(),
                initiator: Some("acc://alice.acme".to_string()),
                timestamp: 1234567890123,
                nonce: Some(42),
                memo: Some("Test transaction".to_string()),
                metadata: Some(serde_json::json!({"test": "metadata"})),
            },
            body: serde_json::json!({
                "type": "send-tokens",
                "to": [{
                    "url": "acc://bob.acme/tokens",
                    "amount": "1000"
                }]
            }),
            signatures: vec![TransactionSignature::generate_sample()],
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            // Minimal envelope
            Self {
                header: TransactionHeader {
                    principal: "acc://test.acme".to_string(),
                    initiator: None,
                    timestamp: 1000000000000,
                    nonce: None,
                    memo: None,
                    metadata: None,
                },
                body: serde_json::json!({"type": "create-identity"}),
                signatures: vec![],
            },
            // Complex envelope with multiple signatures
            Self {
                header: TransactionHeader {
                    principal: "acc://complex.acme/tokens".to_string(),
                    initiator: Some("acc://initiator.acme".to_string()),
                    timestamp: 9999999999999,
                    nonce: Some(999999),
                    memo: Some("Complex test with unicode: test nono".to_string()),
                    metadata: Some(serde_json::json!({
                        "version": "1.0",
                        "flags": ["test", "complex"],
                        "nested": {"deep": {"value": 42}}
                    })),
                },
                body: serde_json::json!({
                    "type": "send-tokens",
                    "to": [
                        {"url": "acc://recipient1.acme/tokens", "amount": "100"},
                        {"url": "acc://recipient2.acme/tokens", "amount": "200"},
                        {"url": "acc://recipient3.acme/tokens", "amount": "300"}
                    ]
                }),
                signatures: vec![
                    TransactionSignature::generate_sample(),
                    TransactionSignature {
                        signature: vec![0x99; 64],
                        signer: "acc://signer2.acme/book/1".to_string(),
                        timestamp: 1234567890124,
                        vote: Some("approve".to_string()),
                        public_key: Some(vec![0xAA; 32]),
                        key_page: None,
                    }
                ],
            }
        ]
    }
}

impl SampleGenerator for crate::codec::TransactionHeader {
    fn generate_sample() -> Self {
        Self {
            principal: "acc://sample.acme/tokens".to_string(),
            initiator: Some("acc://sample.acme".to_string()),
            timestamp: 1234567890123,
            nonce: Some(1),
            memo: Some("Sample header".to_string()),
            metadata: Some(serde_json::json!({"sample": true})),
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            // Minimal header
            Self {
                principal: "acc://minimal.acme".to_string(),
                initiator: None,
                timestamp: 0,
                nonce: None,
                memo: None,
                metadata: None,
            },
            // Unicode and special characters
            Self {
                principal: "acc://üñíçødé.acme/tøkeñs".to_string(),
                initiator: Some("acc://spëçîál.acme".to_string()),
                timestamp: u64::MAX,
                nonce: Some(u64::MAX),
                memo: Some("Unicode test: star nono cafe resume".to_string()),
                metadata: Some(serde_json::json!({
                    "unicode": "test",
                    "special": "special chars: !@#$%^&*()_+-=[]{}|;':\",./<>?",
                    "nested": {"array": [1, 2, 3], "object": {"key": "value"}}
                })),
            }
        ]
    }
}

impl SampleGenerator for crate::codec::TransactionSignature {
    fn generate_sample() -> Self {
        Self {
            signature: vec![0x42; 64], // 64-byte signature
            signer: "acc://signer.acme/book/0".to_string(),
            timestamp: 1234567890000,
            vote: Some("approve".to_string()),
            public_key: Some(vec![0x33; 32]), // 32-byte public key
            key_page: Some(crate::codec::TransactionKeyPage {
                height: 1000,
                index: 0,
            }),
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            // Minimal signature
            Self {
                signature: vec![],
                signer: "acc://min.acme/book/0".to_string(),
                timestamp: 0,
                vote: None,
                public_key: None,
                key_page: None,
            },
            // Maximum values
            Self {
                signature: vec![0xFF; 128], // Large signature
                signer: "acc://very-long-signer-name-with-many-characters.acme/book/999".to_string(),
                timestamp: u64::MAX,
                vote: Some("reject".to_string()),
                public_key: Some(vec![0x00; 64]), // Large public key
                key_page: Some(crate::codec::TransactionKeyPage {
                    height: u64::MAX,
                    index: u32::MAX,
                }),
            }
        ]
    }
}

impl SampleGenerator for crate::codec::TransactionKeyPage {
    fn generate_sample() -> Self {
        Self {
            height: 12345,
            index: 42,
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            Self { height: 0, index: 0 },
            Self { height: u64::MAX, index: u32::MAX },
        ]
    }
}

impl SampleGenerator for crate::codec::TokenRecipient {
    fn generate_sample() -> Self {
        Self {
            url: "acc://recipient.acme/tokens".to_string(),
            amount: "1000".to_string(),
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            Self {
                url: "acc://zero.acme/tokens".to_string(),
                amount: "0".to_string(),
            },
            Self {
                url: "acc://big.acme/tokens".to_string(),
                amount: "999999999999999999999999".to_string(),
            },
            Self {
                url: "acc://üñíçødé.acme/tøkeñs".to_string(),
                amount: "42.123456789".to_string(),
            }
        ]
    }
}

impl SampleGenerator for crate::codec::transaction_codec::KeySpec {
    fn generate_sample() -> Self {
        Self {
            public_key_hash: "abcdef1234567890abcdef1234567890abcdef12".to_string(),
            delegate: None,
        }
    }

    fn generate_samples() -> Vec<Self> {
        vec![
            Self::generate_sample(),
            Self {
                public_key_hash: "0000000000000000000000000000000000000000".to_string(),
                delegate: None,
            },
            Self {
                public_key_hash: "ffffffffffffffffffffffffffffffffffffffff".to_string(),
                delegate: Some("acc://example.acme/delegate".to_string()),
            }
        ]
    }
}

// Implement RoundtripTestable for all the main types
impl RoundtripTestable for crate::codec::TransactionEnvelope {}
impl RoundtripTestable for crate::codec::TransactionHeader {}
impl RoundtripTestable for crate::codec::TransactionSignature {}
impl RoundtripTestable for crate::codec::TransactionKeyPage {}
impl RoundtripTestable for crate::codec::TokenRecipient {}
impl RoundtripTestable for crate::codec::KeySpec {}

// Also implement for types from the types.rs file
impl RoundtripTestable for crate::types::StatusResponse {}
impl RoundtripTestable for crate::types::NodeInfo {}
impl RoundtripTestable for crate::types::TransactionResponse {}
impl RoundtripTestable for crate::types::TransactionResult {}
impl RoundtripTestable for crate::types::Event {}
impl RoundtripTestable for crate::types::Attribute {}
impl RoundtripTestable for crate::types::SignedTransaction {}
impl RoundtripTestable for crate::types::Signature {}
impl RoundtripTestable for crate::types::Account {}
impl RoundtripTestable for crate::types::FaucetResponse {}
impl RoundtripTestable for crate::types::V3SubmitRequest {}
impl RoundtripTestable for crate::types::V3SubmitResponse {}
impl RoundtripTestable for crate::types::SubmitResult {}
impl RoundtripTestable for crate::types::TransactionEnvelope {}
impl RoundtripTestable for crate::types::V3Signature {}

/// Get type name for a given type (for debugging and reporting)
pub fn get_type_name<T>() -> &'static str {
    std::any::type_name::<T>()
}

/// Check if all types in TYPE_NAMES are covered by tests
pub fn verify_type_coverage() -> Result<(), Vec<String>> {
    let mut missing_types = Vec::new();

    // Check that each type in TYPE_NAMES has some form of test coverage
    // This is a basic implementation - could be enhanced with more sophisticated checks
    for type_name in TYPE_NAMES {
        match *type_name {
            // Core types that have SampleGenerator implementations
            "TransactionEnvelope" | "TransactionHeader" | "TransactionSignature"
            | "TransactionKeyPage" | "TokenRecipient" | "KeySpec" => {
                // These are covered by SampleGenerator
            }

            // Types that have manual test implementations
            "StatusResponse" | "NodeInfo" | "TransactionResponse" | "TransactionResult"
            | "Event" | "Attribute" | "Account" | "FaucetResponse" | "V3Signature" => {
                // These are covered by manual tests
            }

            // Types that might need implementation
            "QueryResponse" | "SignedTransaction" | "Signature" | "V3SubmitRequest"
            | "V3SubmitResponse" | "SubmitResult" | "ProtocolTransactionEnvelope"
            | "ProtocolTransactionSignature" | "ProtocolTransactionHeader"
            | "BinaryReader" | "BinaryWriter" | "EncodingError" | "DecodingError"
            | "FieldReader" | "Ed25519Signer" | "AccOptions" => {
                // These types might need test implementations
                missing_types.push(type_name.to_string());
            }

            _ => {
                // Unknown type
                missing_types.push(format!("Unknown type: {}", type_name));
            }
        }
    }

    if missing_types.is_empty() {
        Ok(())
    } else {
        Err(missing_types)
    }
}

/// Generate a comprehensive test report for all types
pub fn generate_type_test_report() -> String {
    let mut report = String::new();

    report.push_str("# Type Matrix Test Coverage Report\n\n");
    report.push_str(&format!("Total types in matrix: {}\n\n", TYPE_NAMES.len()));

    report.push_str("## Core Transaction Types\n");
    let core_types = [
        "TransactionEnvelope", "TransactionHeader", "TransactionSignature",
        "TransactionKeyPage", "TokenRecipient", "KeySpec"
    ];

    for type_name in core_types {
        if TYPE_NAMES.contains(&type_name) {
            report.push_str(&format!("- [OK] {}\n", type_name));
        } else {
            report.push_str(&format!("- [MISSING] {} (missing from TYPE_NAMES)\n", type_name));
        }
    }

    report.push_str("\n## API Response Types\n");
    let api_types = [
        "StatusResponse", "NodeInfo", "QueryResponse", "TransactionResponse",
        "TransactionResult", "Event", "Attribute", "Account", "FaucetResponse"
    ];

    for type_name in api_types {
        if TYPE_NAMES.contains(&type_name) {
            report.push_str(&format!("- [OK] {}\n", type_name));
        } else {
            report.push_str(&format!("- [MISSING] {} (missing from TYPE_NAMES)\n", type_name));
        }
    }

    report.push_str("\n## V3 Protocol Types\n");
    let v3_types = [
        "V3SubmitRequest", "V3SubmitResponse", "SubmitResult", "V3Signature"
    ];

    for type_name in v3_types {
        if TYPE_NAMES.contains(&type_name) {
            report.push_str(&format!("- [OK] {}\n", type_name));
        } else {
            report.push_str(&format!("- [MISSING] {} (missing from TYPE_NAMES)\n", type_name));
        }
    }

    report.push_str("\n## Coverage Status\n");
    match verify_type_coverage() {
        Ok(()) => {
            report.push_str("[OK] All types have test coverage\n");
        }
        Err(missing) => {
            report.push_str(&format!("[MISSING] {} types need test implementations:\n", missing.len()));
            for missing_type in missing {
                report.push_str(&format!("  - {}\n", missing_type));
            }
        }
    }

    report
}

/// Utility to count the number of samples generated for a type
pub fn count_samples<T: SampleGenerator>() -> usize {
    T::generate_samples().len()
}