|
| 1 | +use ark_ff::{BigInteger as _, PrimeField as _}; |
| 2 | +use groth16::Fr; |
| 3 | +use num_bigint::BigUint; |
| 4 | +use serde::{Deserializer, Serializer}; |
| 5 | + |
| 6 | +use crate::block::References; |
| 7 | + |
| 8 | +#[derive(serde::Serialize, serde::Deserialize)] |
| 9 | +struct WireData { |
| 10 | + service_reward: Option<Vec<u8>>, |
| 11 | + mempool_transactions: Vec<Vec<u8>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl serde::Serialize for References { |
| 15 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 16 | + where |
| 17 | + S: Serializer, |
| 18 | + { |
| 19 | + // Convert References to WireData |
| 20 | + let service_reward = self.service_reward.as_ref().map(|fr| { |
| 21 | + let big_int = fr.into_bigint(); |
| 22 | + big_int.to_bytes_le() |
| 23 | + }); |
| 24 | + |
| 25 | + let mempool_transactions = self |
| 26 | + .mempool_transactions |
| 27 | + .iter() |
| 28 | + .map(|fr| { |
| 29 | + let big_int = fr.into_bigint(); |
| 30 | + big_int.to_bytes_le() |
| 31 | + }) |
| 32 | + .collect(); |
| 33 | + |
| 34 | + let wire = WireData { |
| 35 | + service_reward, |
| 36 | + mempool_transactions, |
| 37 | + }; |
| 38 | + |
| 39 | + wire.serialize(serializer) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<'de> serde::Deserialize<'de> for References { |
| 44 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 45 | + where |
| 46 | + D: Deserializer<'de>, |
| 47 | + { |
| 48 | + let wire: WireData = WireData::deserialize(deserializer)?; |
| 49 | + |
| 50 | + let service_reward = wire.service_reward.map(|bytes| { |
| 51 | + let big_int = BigUint::from_bytes_le(&bytes); |
| 52 | + Fr::from(big_int) |
| 53 | + }); |
| 54 | + |
| 55 | + let mempool_transactions = wire |
| 56 | + .mempool_transactions |
| 57 | + .into_iter() |
| 58 | + .map(|bytes| { |
| 59 | + let big_int = BigUint::from_bytes_le(&bytes); |
| 60 | + Fr::from(big_int) |
| 61 | + }) |
| 62 | + .collect(); |
| 63 | + |
| 64 | + Ok(Self { |
| 65 | + service_reward, |
| 66 | + mempool_transactions, |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::Fr; |
| 74 | + use crate::block::References; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_references_wire_serialization() { |
| 78 | + let service_reward = Some(Fr::from(42u64)); |
| 79 | + let mempool_transactions = vec![Fr::from(123u64), Fr::from(456u64), Fr::from(789u64)]; |
| 80 | + |
| 81 | + let references = References { |
| 82 | + service_reward, |
| 83 | + mempool_transactions, |
| 84 | + }; |
| 85 | + |
| 86 | + let wire_bytes = |
| 87 | + crate::wire::serialize(&references).expect("Failed to serialize with wire"); |
| 88 | + let deserialized: References = |
| 89 | + crate::wire::deserialize(&wire_bytes).expect("Failed to deserialize with wire"); |
| 90 | + |
| 91 | + assert_eq!(references.service_reward, deserialized.service_reward); |
| 92 | + assert_eq!( |
| 93 | + references.mempool_transactions, |
| 94 | + deserialized.mempool_transactions |
| 95 | + ); |
| 96 | + |
| 97 | + assert_eq!(deserialized.service_reward.unwrap(), Fr::from(42u64)); |
| 98 | + assert_eq!(deserialized.mempool_transactions.len(), 3); |
| 99 | + assert_eq!(deserialized.mempool_transactions[0], Fr::from(123u64)); |
| 100 | + assert_eq!(deserialized.mempool_transactions[1], Fr::from(456u64)); |
| 101 | + assert_eq!(deserialized.mempool_transactions[2], Fr::from(789u64)); |
| 102 | + } |
| 103 | +} |
0 commit comments