From 2feb8451ba36e6b92710969307ab4dc8d5945a50 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 6 Nov 2024 11:17:55 -0800 Subject: [PATCH] fmt --- sia/src/common.rs | 8 +- sia/src/specifier.rs | 8 +- sia/src/transactions.rs | 337 ++++++++++++++++++----------------- sia/src/unlock_conditions.rs | 8 +- 4 files changed, 179 insertions(+), 182 deletions(-) diff --git a/sia/src/common.rs b/sia/src/common.rs index 1ee9d4f..8859e87 100644 --- a/sia/src/common.rs +++ b/sia/src/common.rs @@ -1,11 +1,5 @@ use crate::encoding::{ - SiaDecodable, - SiaDecode, - SiaEncodable, - SiaEncode, - V1SiaDecodable, - V1SiaDecode, - V1SiaEncodable, + SiaDecodable, SiaDecode, SiaEncodable, SiaEncode, V1SiaDecodable, V1SiaDecode, V1SiaEncodable, V1SiaEncode, }; use blake2b_simd::Params; diff --git a/sia/src/specifier.rs b/sia/src/specifier.rs index 56ca3d1..c468337 100644 --- a/sia/src/specifier.rs +++ b/sia/src/specifier.rs @@ -1,11 +1,5 @@ use crate::encoding::{ - SiaDecodable, - SiaDecode, - SiaEncodable, - SiaEncode, - V1SiaDecodable, - V1SiaDecode, - V1SiaEncodable, + SiaDecodable, SiaDecode, SiaEncodable, SiaEncode, V1SiaDecodable, V1SiaDecode, V1SiaEncodable, V1SiaEncode, }; use core::{fmt, str}; diff --git a/sia/src/transactions.rs b/sia/src/transactions.rs index 9b8df5e..d96990c 100644 --- a/sia/src/transactions.rs +++ b/sia/src/transactions.rs @@ -1,19 +1,12 @@ use crate::encoding::{ - self, - SiaDecodable, - SiaDecode, - SiaEncodable, - SiaEncode, - V1SiaDecodable, - V1SiaDecode, - V1SiaEncodable, - V1SiaEncode, + self, SiaDecodable, SiaDecode, SiaEncodable, SiaEncode, V1SiaDecodable, V1SiaDecode, + V1SiaEncodable, V1SiaEncode, }; use crate::signing::{PrivateKey, PublicKey, Signature, SigningState}; use crate::specifier::{specifier, Specifier}; use crate::spendpolicy::SatisfiedPolicy; use crate::unlock_conditions::UnlockConditions; -use crate::{BlockID, Address, ChainIndex, Currency, Hash256, Leaf}; +use crate::{Address, BlockID, ChainIndex, Currency, Hash256, Leaf}; use blake2b_simd::Params; use serde::de::{Error, MapAccess, Visitor}; use serde::ser::SerializeStruct; @@ -379,47 +372,47 @@ impl Transaction { #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2FileContract { - pub capacity: u64, - pub filesize: u64, - pub file_merkle_root: Hash256, - pub proof_height: u64, - pub expiration_height: u64, - pub renter_output: SiacoinOutput, - pub host_output: SiacoinOutput, - pub missed_host_value: Currency, - pub total_collateral: Currency, - pub renter_public_key: PublicKey, - pub host_public_key: PublicKey, - pub revision_number: u64, - - pub renter_signature: Signature, - pub host_signature: Signature, + pub capacity: u64, + pub filesize: u64, + pub file_merkle_root: Hash256, + pub proof_height: u64, + pub expiration_height: u64, + pub renter_output: SiacoinOutput, + pub host_output: SiacoinOutput, + pub missed_host_value: Currency, + pub total_collateral: Currency, + pub renter_public_key: PublicKey, + pub host_public_key: PublicKey, + pub revision_number: u64, + + pub renter_signature: Signature, + pub host_signature: Signature, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2FileContractRevision { - pub parent: V2FileContractElement, - pub revision: V2FileContract + pub parent: V2FileContractElement, + pub revision: V2FileContract, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2FileContractRenewal { - pub final_revision: V2FileContract, - pub new_contract: V2FileContract, + pub final_revision: V2FileContract, + pub new_contract: V2FileContract, pub renter_rollover: Currency, - pub host_rollover: Currency, + pub host_rollover: Currency, // signatures cover above fields pub renter_signature: Signature, - pub host_signature: Signature, + pub host_signature: Signature, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2StorageProof { - // Selecting the leaf requires a source of unpredictable entropy; we use the + // Selecting the leaf requires a source of unpredictable entropy; we use the // ID of the block at the contract's ProofHeight. The storage proof thus // includes a proof that this ID is the correct ancestor. // @@ -436,134 +429,156 @@ pub struct V2StorageProof { #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum V2ContractResolution { - Finalization(Signature), - Renewal(V2FileContractRenewal), - StorageProof(V2StorageProof), - Expiration(), + Finalization(Signature), + Renewal(V2FileContractRenewal), + StorageProof(V2StorageProof), + Expiration(), } #[derive(Debug, PartialEq)] pub struct V2FileContractResolution { - pub parent: V2FileContractElement, - pub resolution: V2ContractResolution, + pub parent: V2FileContractElement, + pub resolution: V2ContractResolution, } impl SiaEncodable for V2FileContractResolution { - fn encode(&self, w: &mut W) -> encoding::Result<()> { - self.parent.encode(w)?; - match &self.resolution { - V2ContractResolution::Finalization(sig) => { - 0u8.encode(w)?; - sig.encode(w) - }, - V2ContractResolution::Renewal(renewal) => { - 1u8.encode(w)?; - renewal.encode(w) - }, - V2ContractResolution::StorageProof(proof) => { - 2u8.encode(w)?; - proof.encode(w) - }, - V2ContractResolution::Expiration() => { - 3u8.encode(w) - }, - } - } + fn encode(&self, w: &mut W) -> encoding::Result<()> { + self.parent.encode(w)?; + match &self.resolution { + V2ContractResolution::Finalization(sig) => { + 0u8.encode(w)?; + sig.encode(w) + } + V2ContractResolution::Renewal(renewal) => { + 1u8.encode(w)?; + renewal.encode(w) + } + V2ContractResolution::StorageProof(proof) => { + 2u8.encode(w)?; + proof.encode(w) + } + V2ContractResolution::Expiration() => 3u8.encode(w), + } + } } impl SiaDecodable for V2FileContractResolution { - fn decode(r: &mut R) -> encoding::Result { - let parent = V2FileContractElement::decode(r)?; - let resolution = match u8::decode(r)? { - 0 => V2ContractResolution::Finalization(Signature::decode(r)?), - 1 => V2ContractResolution::Renewal(V2FileContractRenewal::decode(r)?), - 2 => V2ContractResolution::StorageProof(V2StorageProof::decode(r)?), - 3 => V2ContractResolution::Expiration(), - _ => return Err(encoding::Error::Custom("invalid contract resolution type".to_string())), - }; - Ok(V2FileContractResolution { parent, resolution }) - } + fn decode(r: &mut R) -> encoding::Result { + let parent = V2FileContractElement::decode(r)?; + let resolution = match u8::decode(r)? { + 0 => V2ContractResolution::Finalization(Signature::decode(r)?), + 1 => V2ContractResolution::Renewal(V2FileContractRenewal::decode(r)?), + 2 => V2ContractResolution::StorageProof(V2StorageProof::decode(r)?), + 3 => V2ContractResolution::Expiration(), + _ => { + return Err(encoding::Error::Custom( + "invalid contract resolution type".to_string(), + )) + } + }; + Ok(V2FileContractResolution { parent, resolution }) + } } impl Serialize for V2FileContractResolution { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer { - let mut state = serializer.serialize_struct("V2FileContractResolution", 2)?; - state.serialize_field("parent", &self.parent)?; - state.serialize_field("resolution", &json!({ - "parent": self.parent, - "type": match &self.resolution { - V2ContractResolution::Finalization(_) => "finalization", - V2ContractResolution::Renewal(_) => "renewal", - V2ContractResolution::StorageProof(_) => "storagePoof", - V2ContractResolution::Expiration() => "expiration", - }, - "resolution": self.resolution, - }))?; - state.end() - } + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut state = serializer.serialize_struct("V2FileContractResolution", 2)?; + state.serialize_field("parent", &self.parent)?; + state.serialize_field( + "resolution", + &json!({ + "parent": self.parent, + "type": match &self.resolution { + V2ContractResolution::Finalization(_) => "finalization", + V2ContractResolution::Renewal(_) => "renewal", + V2ContractResolution::StorageProof(_) => "storagePoof", + V2ContractResolution::Expiration() => "expiration", + }, + "resolution": self.resolution, + }), + )?; + state.end() + } } impl<'de> Deserialize<'de> for V2FileContractResolution { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de> { - struct V2FileContractResolutionVisitor; - - impl<'de> Visitor<'de> for V2FileContractResolutionVisitor { - type Value = V2FileContractResolution; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("struct V2FileContractResolution") - } - - fn visit_map(self, mut map: V) -> Result - where - V: MapAccess<'de>, - { - let mut parent: Option = None; - let mut resolution_type: Option = None; - let mut resolution_value: Option = None; - - while let Some(key) = map.next_key::()? { - match key.as_str() { - "parent" => parent = Some(map.next_value()?), - "type" => resolution_type = Some(map.next_value()?), - "resolution" => resolution_value = Some(map.next_value()?), - _ => { - return Err(serde::de::Error::unknown_field(key.as_str(), &["parent", "type", "resolution"])); - } - } - } - - let parent = parent.ok_or_else(|| serde::de::Error::missing_field("parent"))?; - let resolution_type = resolution_type.ok_or_else(|| serde::de::Error::missing_field("type"))?; - let resolution_value = resolution_value.ok_or_else(|| serde::de::Error::missing_field("resolution"))?; - - let resolution = match resolution_type.as_str() { - "finalization" => V2ContractResolution::Finalization(serde_json::from_value(resolution_value).map_err(Error::custom)?), - "renewal" => V2ContractResolution::Renewal(serde_json::from_value(resolution_value).map_err(Error::custom)?), - "storageProof" => V2ContractResolution::StorageProof(serde_json::from_value(resolution_value).map_err(Error::custom)?), - "expiration" => V2ContractResolution::Expiration(), - _ => return Err(serde::de::Error::custom("invalid contract resolution type")), - }; - - Ok(V2FileContractResolution { parent, resolution }) - } - } - deserializer.deserialize_struct("V2FileContractResolution", &["parent", "type", "resolution"], V2FileContractResolutionVisitor) - } + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + struct V2FileContractResolutionVisitor; + + impl<'de> Visitor<'de> for V2FileContractResolutionVisitor { + type Value = V2FileContractResolution; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("struct V2FileContractResolution") + } + + fn visit_map(self, mut map: V) -> Result + where + V: MapAccess<'de>, + { + let mut parent: Option = None; + let mut resolution_type: Option = None; + let mut resolution_value: Option = None; + + while let Some(key) = map.next_key::()? { + match key.as_str() { + "parent" => parent = Some(map.next_value()?), + "type" => resolution_type = Some(map.next_value()?), + "resolution" => resolution_value = Some(map.next_value()?), + _ => { + return Err(serde::de::Error::unknown_field( + key.as_str(), + &["parent", "type", "resolution"], + )); + } + } + } + + let parent = parent.ok_or_else(|| serde::de::Error::missing_field("parent"))?; + let resolution_type = + resolution_type.ok_or_else(|| serde::de::Error::missing_field("type"))?; + let resolution_value = resolution_value + .ok_or_else(|| serde::de::Error::missing_field("resolution"))?; + + let resolution = match resolution_type.as_str() { + "finalization" => V2ContractResolution::Finalization( + serde_json::from_value(resolution_value).map_err(Error::custom)?, + ), + "renewal" => V2ContractResolution::Renewal( + serde_json::from_value(resolution_value).map_err(Error::custom)?, + ), + "storageProof" => V2ContractResolution::StorageProof( + serde_json::from_value(resolution_value).map_err(Error::custom)?, + ), + "expiration" => V2ContractResolution::Expiration(), + _ => return Err(serde::de::Error::custom("invalid contract resolution type")), + }; + + Ok(V2FileContractResolution { parent, resolution }) + } + } + deserializer.deserialize_struct( + "V2FileContractResolution", + &["parent", "type", "resolution"], + V2FileContractResolutionVisitor, + ) + } } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct Attestation { - pub public_key: PublicKey, - pub key: String, - pub value: Vec, + pub public_key: PublicKey, + pub key: String, + pub value: Vec, - pub signature: Signature, + pub signature: Signature, } type AttestationID = Hash256; @@ -595,9 +610,9 @@ pub struct SiafundElement { #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2FileContractElement { - pub id: FileContractID, - pub state_element: StateElement, - pub file_contract: V2FileContract, + pub id: FileContractID, + pub state_element: StateElement, + pub file_contract: V2FileContract, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] @@ -609,39 +624,39 @@ pub struct ChainIndexElement { } pub struct AttestationElement { - pub id: AttestationID, - pub state_element: StateElement, - pub attestation: Attestation, + pub id: AttestationID, + pub state_element: StateElement, + pub attestation: Attestation, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2SiacoinInput { - pub parent: SiacoinElement, - pub satisfied_policy: SatisfiedPolicy, + pub parent: SiacoinElement, + pub satisfied_policy: SatisfiedPolicy, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2SiafundInput { - pub parent: SiafundElement, - pub claim_address: Address, - pub satisfied_policy: SatisfiedPolicy, + pub parent: SiafundElement, + pub claim_address: Address, + pub satisfied_policy: SatisfiedPolicy, } #[derive(Debug, PartialEq, Serialize, Deserialize, SiaEncode, SiaDecode)] #[serde(rename_all = "camelCase")] pub struct V2Transaction { - pub siacoin_inputs: Vec, - pub siacoin_outputs: Vec, - pub siafund_inputs: Vec, - pub siafund_outputs: Vec, - pub file_contracts: Vec, - pub file_contract_revisions: Vec, - pub file_contract_resolutions: Vec, - pub arbitrary_data: Vec>, - pub new_foundation_address: Option
, - pub miner_fee: Currency, + pub siacoin_inputs: Vec, + pub siacoin_outputs: Vec, + pub siafund_inputs: Vec, + pub siafund_outputs: Vec, + pub file_contracts: Vec, + pub file_contract_revisions: Vec, + pub file_contract_resolutions: Vec, + pub arbitrary_data: Vec>, + pub new_foundation_address: Option
, + pub miner_fee: Currency, } // Create a helper module for base64 serialization @@ -717,8 +732,8 @@ mod tests { 28, 225, 182, 1, 0, 0, 0, 0, 0, 0, 0 ] ); - let siacoin_input_deserialized: SiacoinInput = - SiacoinInput::decode_v1(&mut &siacoin_input_serialized[..]).unwrap(); + let siacoin_input_deserialized: SiacoinInput = + SiacoinInput::decode_v1(&mut &siacoin_input_serialized[..]).unwrap(); assert_eq!(siacoin_input_deserialized, siacoin_input); // json diff --git a/sia/src/unlock_conditions.rs b/sia/src/unlock_conditions.rs index 33d9bbc..861259e 100644 --- a/sia/src/unlock_conditions.rs +++ b/sia/src/unlock_conditions.rs @@ -1,11 +1,5 @@ use crate::encoding::{ - SiaDecodable, - SiaDecode, - SiaEncodable, - SiaEncode, - V1SiaDecodable, - V1SiaDecode, - V1SiaEncodable, + SiaDecodable, SiaDecode, SiaEncodable, SiaEncode, V1SiaDecodable, V1SiaDecode, V1SiaEncodable, V1SiaEncode, }; use crate::merkle::{Accumulator, LEAF_HASH_PREFIX};