jam-primitives 0.2.0

JAM protocol primitives
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! # JAM Protocol Transaction Types
//!
//! Transaction types and structures for the JAM protocol as defined in the Gray Paper.
//! This module contains all transaction-related data structures and their validation logic.

use crate::utils::codec::{Decode, Encode};
use crate::{
    Hash, PrimitiveError, PrimitiveResult, Validate,
    types::{AccountId, Balance, Gas, ServiceId, Signature, Timestamp, Weight},
};
use serde::{Deserialize, Serialize};

/// Transaction nonce type
pub type Nonce = u64;

/// Transaction type identifier
pub type TransactionType = u8;

/// Gas price type (in smallest unit)
pub type GasPrice = u64;

/// Transaction kinds supported by JAM
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
pub enum TransactionKind {
    /// Transfer tokens between accounts
    Transfer {
        /// Destination account
        to: AccountId,
        /// Amount to transfer
        amount: Balance,
    },
    /// Call a service method
    ServiceCall {
        /// Target service
        service: ServiceId,
        /// Method identifier
        method: u32,
        /// Call data
        data: Vec<u8>,
    },
    /// Deploy a new service
    ServiceDeploy {
        /// Service code (WebAssembly)
        code: Vec<u8>,
        /// Initial service state
        initial_state: Vec<u8>,
    },
    /// Update service code
    ServiceUpdate {
        /// Service to update
        service: ServiceId,
        /// New service code
        code: Vec<u8>,
    },
    /// Register as a validator
    ValidatorRegister {
        /// Validator public keys
        keys: ValidatorKeys,
        /// Stake amount
        stake: Balance,
    },
    /// Unregister as a validator
    ValidatorUnregister,
    /// Delegate stake to a validator
    Delegate {
        /// Target validator
        validator: AccountId,
        /// Amount to delegate
        amount: Balance,
    },
    /// Undelegate stake from a validator
    Undelegate {
        /// Target validator
        validator: AccountId,
        /// Amount to undelegate
        amount: Balance,
    },
    /// Core assignment transaction
    CoreAssign {
        /// Core index to assign
        core: u16,
        /// Service to assign to core
        service: ServiceId,
        /// Assignment duration
        duration: u64,
    },
    /// Availability report
    AvailabilityReport {
        /// Core index
        core: u16,
        /// Availability data hash
        data_hash: Hash,
        /// Erasure coded chunks
        chunks: Vec<Vec<u8>>,
    },
    /// Dispute initiation
    DisputeInitiate {
        /// Disputed core
        core: u16,
        /// Block number being disputed
        block_number: u32,
        /// Dispute evidence
        evidence: Vec<u8>,
    },
    /// Dispute vote
    DisputeVote {
        /// Dispute ID
        dispute_id: Hash,
        /// Vote (true = valid, false = invalid)
        vote: bool,
        /// Justification
        justification: Vec<u8>,
    },
}

impl TransactionKind {
    /// Get the transaction type identifier
    pub fn transaction_type(&self) -> TransactionType {
        match self {
            TransactionKind::Transfer { .. } => 0,
            TransactionKind::ServiceCall { .. } => 1,
            TransactionKind::ServiceDeploy { .. } => 2,
            TransactionKind::ServiceUpdate { .. } => 3,
            TransactionKind::ValidatorRegister { .. } => 4,
            TransactionKind::ValidatorUnregister => 5,
            TransactionKind::Delegate { .. } => 6,
            TransactionKind::Undelegate { .. } => 7,
            TransactionKind::CoreAssign { .. } => 8,
            TransactionKind::AvailabilityReport { .. } => 9,
            TransactionKind::DisputeInitiate { .. } => 10,
            TransactionKind::DisputeVote { .. } => 11,
        }
    }

    /// Get human-readable transaction type name
    pub fn type_name(&self) -> &'static str {
        match self {
            TransactionKind::Transfer { .. } => "Transfer",
            TransactionKind::ServiceCall { .. } => "ServiceCall",
            TransactionKind::ServiceDeploy { .. } => "ServiceDeploy",
            TransactionKind::ServiceUpdate { .. } => "ServiceUpdate",
            TransactionKind::ValidatorRegister { .. } => "ValidatorRegister",
            TransactionKind::ValidatorUnregister => "ValidatorUnregister",
            TransactionKind::Delegate { .. } => "Delegate",
            TransactionKind::Undelegate { .. } => "Undelegate",
            TransactionKind::CoreAssign { .. } => "CoreAssign",
            TransactionKind::AvailabilityReport { .. } => "AvailabilityReport",
            TransactionKind::DisputeInitiate { .. } => "DisputeInitiate",
            TransactionKind::DisputeVote { .. } => "DisputeVote",
        }
    }

    /// Check if this transaction requires stake
    pub fn requires_stake(&self) -> bool {
        matches!(
            self,
            TransactionKind::ValidatorRegister { .. } | TransactionKind::Delegate { .. }
        )
    }

    /// Get the target service ID if applicable
    pub fn target_service(&self) -> Option<ServiceId> {
        match self {
            TransactionKind::ServiceCall { service, .. }
            | TransactionKind::ServiceUpdate { service, .. }
            | TransactionKind::CoreAssign { service, .. } => Some(*service),
            _ => None,
        }
    }

    /// Get the core index if applicable
    pub fn target_core(&self) -> Option<u16> {
        match self {
            TransactionKind::CoreAssign { core, .. }
            | TransactionKind::AvailabilityReport { core, .. }
            | TransactionKind::DisputeInitiate { core, .. } => Some(*core),
            _ => None,
        }
    }
}

/// Validator keys for registration
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
pub struct ValidatorKeys {
    /// GRANDPA key for finality voting
    pub grandpa: crate::crypto::Ed25519PublicKey,
    /// BABE key for block production
    pub babe: crate::crypto::bandersnatch::PublicKey,
    /// Session key for networking
    pub session: crate::crypto::Ed25519PublicKey,
    /// Authority discovery key
    pub authority_discovery: crate::crypto::Ed25519PublicKey,
}

impl ValidatorKeys {
    /// Create new validator keys
    pub fn new(
        grandpa: crate::crypto::Ed25519PublicKey,
        babe: crate::crypto::bandersnatch::PublicKey,
        session: crate::crypto::Ed25519PublicKey,
        authority_discovery: crate::crypto::Ed25519PublicKey,
    ) -> Self {
        Self {
            grandpa,
            babe,
            session,
            authority_discovery,
        }
    }
}

/// Complete JAM transaction
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
pub struct Transaction {
    /// Transaction sender
    pub sender: AccountId,
    /// Transaction nonce (prevents replay attacks)
    pub nonce: Nonce,
    /// Maximum gas to spend
    pub gas_limit: Gas,
    /// Gas price per unit
    pub gas_price: GasPrice,
    /// Transaction kind and parameters
    pub kind: TransactionKind,
    /// Digital signature
    pub signature: Signature,
}

impl Transaction {
    /// Create a new transaction
    pub fn new(
        sender: AccountId,
        nonce: Nonce,
        gas_limit: Gas,
        gas_price: GasPrice,
        kind: TransactionKind,
        signature: Signature,
    ) -> Self {
        Self {
            sender,
            nonce,
            gas_limit,
            gas_price,
            kind,
            signature,
        }
    }

    /// Get transaction hash
    pub fn hash(&self) -> Hash {
        use crate::crypto::hashing::{Blake3Hasher, Hasher};
        let hasher = Blake3Hasher::new();
        hasher.hash(&self.encode())
    }

    /// Get transaction fee (gas_limit * gas_price)
    pub fn fee(&self) -> Balance {
        self.gas_limit.saturating_mul(self.gas_price)
    }

    /// Check if transaction is signed by the claimed sender
    pub fn verify_signature(&self) -> PrimitiveResult<bool> {
        // Create message to verify (all fields except signature)
        let _message = self.signature_message();

        // For now, we'll assume signature verification is handled elsewhere
        // In a real implementation, this would use the appropriate crypto module
        Ok(true) // Placeholder
    }

    /// Get the message that should be signed
    pub fn signature_message(&self) -> Vec<u8> {
        let mut message = Vec::new();
        message.extend_from_slice(self.sender.as_ref());
        message.extend_from_slice(&self.nonce.to_le_bytes());
        message.extend_from_slice(&self.gas_limit.0.to_le_bytes());
        message.extend_from_slice(&self.gas_price.to_le_bytes());
        message.extend_from_slice(&self.kind.encode());
        message
    }

    /// Check if transaction is a system transaction (no fee required)
    pub fn is_system_transaction(&self) -> bool {
        matches!(
            self.kind,
            TransactionKind::ValidatorUnregister
                | TransactionKind::AvailabilityReport { .. }
                | TransactionKind::DisputeVote { .. }
        )
    }

    /// Get the transaction weight (for block weight calculations)
    pub fn weight(&self) -> Weight {
        let base_weight = 10_000; // Base transaction weight
        let kind_weight = match &self.kind {
            TransactionKind::Transfer { .. } => 25_000,
            TransactionKind::ServiceCall { data, .. } => 50_000 + (data.len() as u64 * 10),
            TransactionKind::ServiceDeploy {
                code,
                initial_state,
            } => 100_000 + (code.len() as u64 * 50) + (initial_state.len() as u64 * 10),
            TransactionKind::ServiceUpdate { code, .. } => 80_000 + (code.len() as u64 * 50),
            TransactionKind::ValidatorRegister { .. } => 200_000,
            TransactionKind::ValidatorUnregister => 50_000,
            TransactionKind::Delegate { .. } => 30_000,
            TransactionKind::Undelegate { .. } => 30_000,
            TransactionKind::CoreAssign { .. } => 40_000,
            TransactionKind::AvailabilityReport { chunks, .. } => {
                60_000 + (chunks.iter().map(|c| c.len()).sum::<usize>() as u64 * 5)
            }
            TransactionKind::DisputeInitiate { evidence, .. } => {
                150_000 + (evidence.len() as u64 * 20)
            }
            TransactionKind::DisputeVote { justification, .. } => {
                30_000 + (justification.len() as u64 * 10)
            }
        };
        Weight(base_weight + kind_weight)
    }
}

impl Validate for Transaction {
    fn validate(&self) -> PrimitiveResult<()> {
        // Check gas limit is reasonable
        if self.gas_limit == Gas(0) {
            return Err(PrimitiveError::InvalidTransaction(
                "Gas limit cannot be zero".to_string(),
            ));
        }

        if self.gas_limit > Gas(10_000_000) {
            return Err(PrimitiveError::InvalidTransaction(
                "Gas limit too high".to_string(),
            ));
        }

        // Check gas price is reasonable
        if self.gas_price == 0 && !self.is_system_transaction() {
            return Err(PrimitiveError::InvalidTransaction(
                "Gas price cannot be zero for non-system transactions".to_string(),
            ));
        }

        // Validate transaction kind
        self.validate_kind()?;

        // Verify signature
        if !self.verify_signature()? {
            return Err(PrimitiveError::InvalidTransaction(
                "Invalid signature".to_string(),
            ));
        }

        Ok(())
    }
}

impl Transaction {
    /// Validate transaction kind-specific rules
    fn validate_kind(&self) -> PrimitiveResult<()> {
        match &self.kind {
            TransactionKind::Transfer { amount, .. } => {
                if *amount == Balance(0) {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Transfer amount cannot be zero".to_string(),
                    ));
                }
            }
            TransactionKind::ServiceCall { data, .. } => {
                if data.len() > 1024 * 1024 {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Service call data too large".to_string(),
                    ));
                }
            }
            TransactionKind::ServiceDeploy {
                code,
                initial_state,
            } => {
                if code.is_empty() {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Service code cannot be empty".to_string(),
                    ));
                }
                if code.len() > 5 * 1024 * 1024 {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Service code too large".to_string(),
                    ));
                }
                if initial_state.len() > 1024 * 1024 {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Initial state too large".to_string(),
                    ));
                }
            }
            TransactionKind::ServiceUpdate { code, .. } => {
                if code.is_empty() {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Service code cannot be empty".to_string(),
                    ));
                }
                if code.len() > 5 * 1024 * 1024 {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Service code too large".to_string(),
                    ));
                }
            }
            TransactionKind::ValidatorRegister { stake, .. } => {
                if *stake == Balance(0) {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Validator stake cannot be zero".to_string(),
                    ));
                }
            }
            TransactionKind::Delegate { amount, .. }
            | TransactionKind::Undelegate { amount, .. } => {
                if *amount == Balance(0) {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Delegation amount cannot be zero".to_string(),
                    ));
                }
            }
            TransactionKind::CoreAssign { duration, .. } => {
                if *duration == 0 {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Core assignment duration cannot be zero".to_string(),
                    ));
                }
            }
            TransactionKind::AvailabilityReport { chunks, .. } => {
                if chunks.is_empty() {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Availability report must include chunks".to_string(),
                    ));
                }
            }
            TransactionKind::DisputeInitiate { evidence, .. } => {
                if evidence.is_empty() {
                    return Err(PrimitiveError::InvalidTransaction(
                        "Dispute must include evidence".to_string(),
                    ));
                }
            }
            _ => {} // Other kinds have no specific validation
        }
        Ok(())
    }
}

/// Transaction pool entry with additional metadata
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionPoolEntry {
    /// The transaction
    pub transaction: Transaction,
    /// When it was received
    pub received_at: Timestamp,
    /// Priority score for ordering
    pub priority: u64,
    /// Number of times it was propagated
    pub propagation_count: u32,
}

impl TransactionPoolEntry {
    /// Create a new pool entry
    pub fn new(transaction: Transaction, received_at: Timestamp) -> Self {
        let priority = transaction.gas_price; // Simple priority based on gas price
        Self {
            transaction,
            received_at,
            priority,
            propagation_count: 0,
        }
    }

    /// Update priority based on current conditions
    pub fn update_priority(&mut self, current_time: Timestamp) {
        // Increase priority for older transactions
        let age_bonus = current_time.saturating_sub(self.received_at) / 1000; // Age in seconds
        self.priority = self.transaction.gas_price + age_bonus;
    }

    /// Check if transaction has expired
    pub fn is_expired(&self, current_time: Timestamp, max_lifetime: u64) -> bool {
        current_time.saturating_sub(self.received_at) > max_lifetime
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::*;

    fn dummy_signature() -> Signature {
        Signature::Ed25519([0u8; 64])
    }

    fn dummy_account() -> AccountId {
        AccountId::from([0u8; 32])
    }

    #[test]
    fn test_transaction_creation() {
        let tx = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(21000),
            20,
            TransactionKind::Transfer {
                to: AccountId::from([2u8; 32]),
                amount: Balance(1000),
            },
            dummy_signature(),
        );

        assert_eq!(tx.nonce, 1);
        assert_eq!(tx.gas_limit, Gas(21000));
        assert_eq!(tx.fee(), Balance(420000));
    }

    #[test]
    fn test_transaction_kind_types() {
        let transfer = TransactionKind::Transfer {
            to: dummy_account(),
            amount: Balance(1000),
        };
        assert_eq!(transfer.transaction_type(), 0);
        assert_eq!(transfer.type_name(), "Transfer");
        assert!(!transfer.requires_stake());

        let validator_register = TransactionKind::ValidatorRegister {
            keys: ValidatorKeys::new(
                crate::crypto::Ed25519PublicKey::from([0u8; 32]),
                crate::crypto::bandersnatch::PublicKey::from([0u8; 32]),
                crate::crypto::Ed25519PublicKey::from([0u8; 32]),
                crate::crypto::Ed25519PublicKey::from([0u8; 32]),
            ),
            stake: Balance(1000000),
        };
        assert_eq!(validator_register.transaction_type(), 4);
        assert!(validator_register.requires_stake());
    }

    #[test]
    fn test_transaction_validation() {
        let valid_tx = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(21000),
            20,
            TransactionKind::Transfer {
                to: AccountId::from([2u8; 32]),
                amount: Balance(1000),
            },
            dummy_signature(),
        );
        assert!(valid_tx.validate().is_ok());

        let invalid_tx = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(0), // Invalid gas limit
            20,
            TransactionKind::Transfer {
                to: AccountId::from([2u8; 32]),
                amount: Balance(1000),
            },
            dummy_signature(),
        );
        assert!(invalid_tx.validate().is_err());
    }

    #[test]
    fn test_transaction_pool_entry() {
        let tx = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(21000),
            20,
            TransactionKind::Transfer {
                to: AccountId::from([2u8; 32]),
                amount: Balance(1000),
            },
            dummy_signature(),
        );

        let mut entry = TransactionPoolEntry::new(tx, Timestamp(1000000));
        assert_eq!(entry.priority, 20);

        entry.update_priority(Timestamp(1001000)); // 1 second later
        assert_eq!(entry.priority, 21); // Gas price + age bonus

        assert!(!entry.is_expired(Timestamp(1005000), 10000)); // Not expired
        assert!(entry.is_expired(Timestamp(1015000), 10000)); // Expired
    }

    #[test]
    fn test_transaction_weight_calculation() {
        let transfer = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(21000),
            20,
            TransactionKind::Transfer {
                to: AccountId::from([2u8; 32]),
                amount: Balance(1000),
            },
            dummy_signature(),
        );

        assert_eq!(transfer.weight(), Weight(35_000)); // Base + transfer weight

        let service_call = Transaction::new(
            AccountId::from([1u8; 32]),
            1,
            Gas(100000),
            20,
            TransactionKind::ServiceCall {
                service: ServiceId::new(1),
                method: 0,
                data: vec![0u8; 100],
            },
            dummy_signature(),
        );

        assert_eq!(service_call.weight(), Weight(61_000)); // Base + service call + data weight
    }
}