chore(txbuilder): fix lint warnings (#343)

This commit is contained in:
Santiago Carmuega 2023-12-03 10:29:27 -03:00 committed by GitHub
parent 1582883dc6
commit f9ea2c4790
5 changed files with 56 additions and 50 deletions

View file

@ -27,7 +27,8 @@ use crate::{
pub trait BuildBabbage { pub trait BuildBabbage {
fn build_babbage_raw(self) -> Result<BuiltTransaction, TxBuilderError>; fn build_babbage_raw(self) -> Result<BuiltTransaction, TxBuilderError>;
// fn build_babbage(staging_tx: StagingTransaction, resolver: (), params: ()) -> Result<BuiltTransaction, TxBuilderError>; // fn build_babbage(staging_tx: StagingTransaction, resolver: (), params: ()) ->
// Result<BuiltTransaction, TxBuilderError>;
} }
impl BuildBabbage for StagingTransaction { impl BuildBabbage for StagingTransaction {
@ -51,28 +52,23 @@ impl BuildBabbage for StagingTransaction {
.map(babbage_output) .map(babbage_output)
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
let mint: Option<KeyValuePairs<Hash<28>, KeyValuePairs<_, _>>> = let mint: Option<KeyValuePairs<Hash<28>, KeyValuePairs<_, _>>> = self.mint.map(|massets| {
if let Some(massets) = self.mint { massets
Some( .deref()
massets .iter()
.deref() .map(|(pid, assets)| {
.iter() (
.map(|(pid, assets)| { pid.0.into(),
( assets
pid.0.into(), .iter()
assets .map(|(n, x)| (n.clone().into(), *x))
.into_iter() .collect::<Vec<_>>()
.map(|(n, x)| (n.clone().into(), *x)) .into(),
.collect::<Vec<_>>() )
.into(), })
) .collect::<Vec<_>>()
}) .into()
.collect::<Vec<_>>() });
.into(),
)
} else {
None
};
let collateral = self let collateral = self
.collateral_inputs .collateral_inputs
@ -260,8 +256,8 @@ impl BuildBabbage for StagingTransaction {
}) })
} }
// fn build_babbage(staging_tx: StagingTransaction) -> Result<BuiltTransaction, TxBuilderError> { // fn build_babbage(staging_tx: StagingTransaction) -> Result<BuiltTransaction,
// todo!() // TxBuilderError> { todo!()
// } // }
} }
@ -276,7 +272,7 @@ fn babbage_output(
( (
pid.0.into(), pid.0.into(),
assets assets
.into_iter() .iter()
.map(|(n, x)| (n.clone().into(), *x)) .map(|(n, x)| (n.clone().into(), *x))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into(), .into(),

View file

@ -15,7 +15,8 @@ pub enum TxBuilderError {
/// Provided datum hash was not 32 bytes in length /// Provided datum hash was not 32 bytes in length
#[error("Invalid bytes length for datum hash")] #[error("Invalid bytes length for datum hash")]
MalformedDatumHash, MalformedDatumHash,
/// Input, policy, etc pointed to by a redeemer was not found in the transaction /// Input, policy, etc pointed to by a redeemer was not found in the
/// transaction
#[error("Input/policy pointed to by redeemer not found in tx")] #[error("Input/policy pointed to by redeemer not found in tx")]
RedeemerTargetMissing, RedeemerTargetMissing,
/// Provided network ID is invalid (must be 0 or 1) /// Provided network ID is invalid (must be 0 or 1)

View file

@ -26,9 +26,9 @@ pub struct Hash28(pub [u8; 28]);
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Bytes(pub Vec<u8>); pub struct Bytes(pub Vec<u8>);
impl Into<pallas_codec::utils::Bytes> for Bytes { impl From<Bytes> for pallas_codec::utils::Bytes {
fn into(self) -> pallas_codec::utils::Bytes { fn from(value: Bytes) -> Self {
self.0.into() value.0.into()
} }
} }

View file

@ -469,7 +469,7 @@ impl Output {
} }
} }
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Debug, Clone, Default)]
pub struct OutputAssets(HashMap<PolicyId, HashMap<AssetName, u64>>); pub struct OutputAssets(HashMap<PolicyId, HashMap<AssetName, u64>>);
impl Deref for OutputAssets { impl Deref for OutputAssets {
@ -481,10 +481,6 @@ impl Deref for OutputAssets {
} }
impl OutputAssets { impl OutputAssets {
pub fn new() -> Self {
Self(HashMap::new())
}
pub fn from_map(map: HashMap<PolicyId, HashMap<Bytes, u64>>) -> Self { pub fn from_map(map: HashMap<PolicyId, HashMap<Bytes, u64>>) -> Self {
Self(map) Self(map)
} }
@ -561,7 +557,7 @@ pub struct ExUnits {
pub steps: u64, pub steps: u64,
} }
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
pub struct Redeemers(HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>); pub struct Redeemers(HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>);
impl Deref for Redeemers { impl Deref for Redeemers {
@ -573,10 +569,6 @@ impl Deref for Redeemers {
} }
impl Redeemers { impl Redeemers {
pub fn new() -> Self {
Redeemers(HashMap::new())
}
pub fn from_map(map: HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>) -> Self { pub fn from_map(map: HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>) -> Self {
Self(map) Self(map)
} }
@ -653,12 +645,24 @@ impl BuiltTransaction {
Ok(self) Ok(self)
} }
pub fn add_signature(mut self, pub_key: ed25519::PublicKey, signature: [u8; 64]) -> Result<Self, TxBuilderError> { pub fn add_signature(
mut self,
pub_key: ed25519::PublicKey,
signature: [u8; 64],
) -> Result<Self, TxBuilderError> {
match self.era { match self.era {
BuilderEra::Babbage => { BuilderEra::Babbage => {
let mut new_sigs = self.signatures.unwrap_or_default(); let mut new_sigs = self.signatures.unwrap_or_default();
new_sigs.insert(Bytes32(pub_key.as_ref().try_into().map_err(|_| TxBuilderError::MalformedKey)?), Bytes64(signature)); new_sigs.insert(
Bytes32(
pub_key
.as_ref()
.try_into()
.map_err(|_| TxBuilderError::MalformedKey)?,
),
Bytes64(signature),
);
self.signatures = Some(new_sigs); self.signatures = Some(new_sigs);
@ -687,7 +691,12 @@ impl BuiltTransaction {
BuilderEra::Babbage => { BuilderEra::Babbage => {
let mut new_sigs = self.signatures.unwrap_or_default(); let mut new_sigs = self.signatures.unwrap_or_default();
let pk = Bytes32(pub_key.as_ref().try_into().map_err(|_| TxBuilderError::MalformedKey)?); let pk = Bytes32(
pub_key
.as_ref()
.try_into()
.map_err(|_| TxBuilderError::MalformedKey)?,
);
new_sigs.remove(&pk); new_sigs.remove(&pk);

View file

@ -18,7 +18,7 @@ impl Serialize for Bytes32 {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&hex::encode(&self.0)) serializer.serialize_str(&hex::encode(self.0))
} }
} }
@ -58,7 +58,7 @@ impl Serialize for Hash28 {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&hex::encode(&self.0)) serializer.serialize_str(&hex::encode(self.0))
} }
} }
@ -245,9 +245,9 @@ impl Serialize for RedeemerPurpose {
{ {
let str = match self { let str = match self {
RedeemerPurpose::Spend(Input { tx_hash, txo_index }) => { RedeemerPurpose::Spend(Input { tx_hash, txo_index }) => {
format!("spend:{}#{}", hex::encode(&tx_hash.0), txo_index) format!("spend:{}#{}", hex::encode(tx_hash.0), txo_index)
} }
RedeemerPurpose::Mint(hash) => format!("mint:{}", hex::encode(&hash.0)), RedeemerPurpose::Mint(hash) => format!("mint:{}", hex::encode(hash.0)),
}; };
serializer.serialize_str(&str) serializer.serialize_str(&str)
@ -277,13 +277,13 @@ impl<'de> Visitor<'de> for RedeemerPurposeVisitor {
E: de::Error, E: de::Error,
{ {
let (tag, item) = v let (tag, item) = v
.split_once(":") .split_once(':')
.ok_or(E::custom("invalid redeemer purpose"))?; .ok_or(E::custom("invalid redeemer purpose"))?;
match tag { match tag {
"spend" => { "spend" => {
let (hash, index) = item let (hash, index) = item
.split_once("#") .split_once('#')
.ok_or(E::custom("invalid spend redeemer item"))?; .ok_or(E::custom("invalid spend redeemer item"))?;
let tx_hash = Bytes32( let tx_hash = Bytes32(
@ -355,7 +355,7 @@ impl Serialize for Bytes64 {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&hex::encode(&self.0)) serializer.serialize_str(&hex::encode(self.0))
} }
} }