Skip to content

Commit

Permalink
Merge branch 'main' into joe/update_crates
Browse files Browse the repository at this point in the history
  • Loading branch information
joe authored Aug 31, 2023
2 parents d370f41 + 0e90fc1 commit ce34a34
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
9 changes: 9 additions & 0 deletions masp_primitives/src/asset_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ impl AssetType {
pub fn get_nonce(&self) -> Option<u8> {
self.nonce
}

/// Deserialize an AssetType object
pub fn read<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let mut atype = [0; crate::constants::ASSET_IDENTIFIER_LENGTH];
reader.read_exact(&mut atype)?;
AssetType::from_identifier(&atype).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type")
})
}
}

impl PartialEq for AssetType {
Expand Down
18 changes: 3 additions & 15 deletions masp_primitives/src/transaction/components/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,9 @@ impl ValueSum<AssetType, i32> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
let vec = Vector::read(reader, |reader| {
let mut atype = [0; 32];
let atype = AssetType::read(reader)?;
let mut value = [0; 4];
reader.read_exact(&mut atype)?;
reader.read_exact(&mut value)?;
let atype = AssetType::from_identifier(&atype).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type")
})?;
Ok((atype, i32::from_le_bytes(value)))
})?;
let mut ret = Self::zero();
Expand Down Expand Up @@ -195,13 +191,9 @@ impl ValueSum<AssetType, i64> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
let vec = Vector::read(reader, |reader| {
let mut atype = [0; 32];
let atype = AssetType::read(reader)?;
let mut value = [0; 8];
reader.read_exact(&mut atype)?;
reader.read_exact(&mut value)?;
let atype = AssetType::from_identifier(&atype).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type")
})?;
Ok((atype, i64::from_le_bytes(value)))
})?;
let mut ret = Self::zero();
Expand Down Expand Up @@ -230,13 +222,9 @@ impl ValueSum<AssetType, i128> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
let vec = Vector::read(reader, |reader| {
let mut atype = [0; 32];
let atype = AssetType::read(reader)?;
let mut value = [0; 16];
reader.read_exact(&mut atype)?;
reader.read_exact(&mut value)?;
let atype = AssetType::from_identifier(&atype).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type")
})?;
Ok((atype, i128::from_le_bytes(value)))
})?;
let mut ret = Self::zero();
Expand Down
52 changes: 40 additions & 12 deletions masp_primitives/src/transaction/components/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ pub struct TxIn<A: Authorization> {

impl TxIn<Authorized> {
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
let asset_type = {
let mut tmp = [0u8; 32];
reader.read_exact(&mut tmp)?;
AssetType::from_identifier(&tmp)
}
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid asset identifier"))?;
let asset_type = AssetType::read(reader)?;
let value = {
let mut tmp = [0u8; 8];
reader.read_exact(&mut tmp)?;
Expand Down Expand Up @@ -138,12 +133,7 @@ pub struct TxOut {

impl TxOut {
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
let asset_type = {
let mut tmp = [0u8; 32];
reader.read_exact(&mut tmp)?;
AssetType::from_identifier(&tmp)
}
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid asset identifier"))?;
let asset_type = AssetType::read(reader)?;
let value = {
let mut tmp = [0u8; 8];
reader.read_exact(&mut tmp)?;
Expand Down Expand Up @@ -237,3 +227,41 @@ pub mod testing {
}
}
}

#[cfg(test)]
mod test_serialization {
use super::*;

/// Simple test that a serialization round trip is the identity
#[test]
fn test_roundtrip_txin() {
let asset_type = AssetType::new_with_nonce(&[1, 2, 3, 4], 1).expect("Test failed");
let txin = TxIn::<Authorized> {
asset_type,
value: MAX_MONEY - 1,
address: TransparentAddress([12u8; 20]),
transparent_sig: (),
};

let mut buf = vec![];
txin.write(&mut buf).expect("Test failed");
let deserialized = TxIn::read::<&[u8]>(&mut buf.as_ref()).expect("Test failed");
assert_eq!(deserialized, txin);
}

/// Simple test that a serialization round trip is the identity
#[test]
fn test_roundtrip_txout() {
let asset_type = AssetType::new_with_nonce(&[1, 2, 3, 4], 1).expect("Test failed");
let txout = TxOut {
asset_type,
value: MAX_MONEY - 1,
address: TransparentAddress([12u8; 20]),
};

let mut buf = vec![];
txout.write(&mut buf).expect("Test failed");
let deserialized = TxOut::read::<&[u8]>(&mut buf.as_ref()).expect("Test failed");
assert_eq!(deserialized, txout);
}
}
2 changes: 1 addition & 1 deletion masp_proofs/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl TxProver for LocalTxProver {
fn binding_sig(
&self,
ctx: &mut Self::SaplingProvingContext,
assets_and_values: &I128Sum, //&[(AssetType, i64)],
assets_and_values: &I128Sum,
sighash: &[u8; 32],
) -> Result<Signature, ()> {
ctx.binding_sig(assets_and_values, sighash)
Expand Down

0 comments on commit ce34a34

Please sign in to comment.