Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(txbuilder): fix lint warnings #343

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions pallas-txbuilder/src/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use crate::{
pub trait BuildBabbage {
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 {
Expand All @@ -51,28 +52,23 @@ impl BuildBabbage for StagingTransaction {
.map(babbage_output)
.collect::<Result<Vec<_>, _>>()?;

let mint: Option<KeyValuePairs<Hash<28>, KeyValuePairs<_, _>>> =
if let Some(massets) = self.mint {
Some(
massets
.deref()
.iter()
.map(|(pid, assets)| {
(
pid.0.into(),
assets
.into_iter()
.map(|(n, x)| (n.clone().into(), *x))
.collect::<Vec<_>>()
.into(),
)
})
.collect::<Vec<_>>()
.into(),
)
} else {
None
};
let mint: Option<KeyValuePairs<Hash<28>, KeyValuePairs<_, _>>> = self.mint.map(|massets| {
massets
.deref()
.iter()
.map(|(pid, assets)| {
(
pid.0.into(),
assets
.iter()
.map(|(n, x)| (n.clone().into(), *x))
.collect::<Vec<_>>()
.into(),
)
})
.collect::<Vec<_>>()
.into()
});

let collateral = self
.collateral_inputs
Expand Down Expand Up @@ -260,8 +256,8 @@ impl BuildBabbage for StagingTransaction {
})
}

// fn build_babbage(staging_tx: StagingTransaction) -> Result<BuiltTransaction, TxBuilderError> {
// todo!()
// fn build_babbage(staging_tx: StagingTransaction) -> Result<BuiltTransaction,
// TxBuilderError> { todo!()
// }
}

Expand All @@ -276,7 +272,7 @@ fn babbage_output(
(
pid.0.into(),
assets
.into_iter()
.iter()
.map(|(n, x)| (n.clone().into(), *x))
.collect::<Vec<_>>()
.into(),
Expand Down
3 changes: 2 additions & 1 deletion pallas-txbuilder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub enum TxBuilderError {
/// Provided datum hash was not 32 bytes in length
#[error("Invalid bytes length for datum hash")]
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")]
RedeemerTargetMissing,
/// Provided network ID is invalid (must be 0 or 1)
Expand Down
6 changes: 3 additions & 3 deletions pallas-txbuilder/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub struct Hash28(pub [u8; 28]);
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Bytes(pub Vec<u8>);

impl Into<pallas_codec::utils::Bytes> for Bytes {
fn into(self) -> pallas_codec::utils::Bytes {
self.0.into()
impl From<Bytes> for pallas_codec::utils::Bytes {
fn from(value: Bytes) -> Self {
value.0.into()
}
}

Expand Down
35 changes: 22 additions & 13 deletions pallas-txbuilder/src/transaction/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>);

impl Deref for OutputAssets {
Expand All @@ -481,10 +481,6 @@ impl Deref for OutputAssets {
}

impl OutputAssets {
pub fn new() -> Self {
Self(HashMap::new())
}

pub fn from_map(map: HashMap<PolicyId, HashMap<Bytes, u64>>) -> Self {
Self(map)
}
Expand Down Expand Up @@ -561,7 +557,7 @@ pub struct ExUnits {
pub steps: u64,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
pub struct Redeemers(HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>);

impl Deref for Redeemers {
Expand All @@ -573,10 +569,6 @@ impl Deref for Redeemers {
}

impl Redeemers {
pub fn new() -> Self {
Redeemers(HashMap::new())
}

pub fn from_map(map: HashMap<RedeemerPurpose, (Bytes, Option<ExUnits>)>) -> Self {
Self(map)
}
Expand Down Expand Up @@ -653,12 +645,24 @@ impl BuiltTransaction {
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 {
BuilderEra::Babbage => {
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);

Expand Down Expand Up @@ -687,7 +691,12 @@ impl BuiltTransaction {
BuilderEra::Babbage => {
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);

Expand Down
14 changes: 7 additions & 7 deletions pallas-txbuilder/src/transaction/serialise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Serialize for Bytes32 {
where
S: Serializer,
{
serializer.serialize_str(&hex::encode(&self.0))
serializer.serialize_str(&hex::encode(self.0))
}
}

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Serialize for Hash28 {
where
S: Serializer,
{
serializer.serialize_str(&hex::encode(&self.0))
serializer.serialize_str(&hex::encode(self.0))
}
}

Expand Down Expand Up @@ -245,9 +245,9 @@ impl Serialize for RedeemerPurpose {
{
let str = match self {
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)
Expand Down Expand Up @@ -277,13 +277,13 @@ impl<'de> Visitor<'de> for RedeemerPurposeVisitor {
E: de::Error,
{
let (tag, item) = v
.split_once(":")
.split_once(':')
.ok_or(E::custom("invalid redeemer purpose"))?;

match tag {
"spend" => {
let (hash, index) = item
.split_once("#")
.split_once('#')
.ok_or(E::custom("invalid spend redeemer item"))?;

let tx_hash = Bytes32(
Expand Down Expand Up @@ -355,7 +355,7 @@ impl Serialize for Bytes64 {
where
S: Serializer,
{
serializer.serialize_str(&hex::encode(&self.0))
serializer.serialize_str(&hex::encode(self.0))
}
}

Expand Down
Loading