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

refuse to send if other rare ordinals would also be sent to recipient or fee #683

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Changes from 1 commit
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
116 changes: 93 additions & 23 deletions src/subcommand/wallet/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use {
pub(crate) enum Error {
NotInWallet(Ordinal),
NotEnoughCardinalUtxos,
AccidentalRareOrdinalSend(Ordinal),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AccidentalRareOrdinalSend(Ordinal),
CollateralDamage(Ordinal),

}

impl fmt::Display for Error {
Expand All @@ -45,6 +46,10 @@ impl fmt::Display for Error {
f,
"Wallet does not contain enough cardinal UTXOs. Please add additional funds to wallet."
),
Error::AccidentalRareOrdinalSend(ordinal) => write!(
f,
"This transaction would also send along Ordinal {ordinal}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"This transaction would also send along Ordinal {ordinal}"
"Cannot perform transaction without sending rare ordinal {ordinal} to recipient or losing it to the fee"

),
}
}
}
Expand Down Expand Up @@ -76,16 +81,14 @@ impl TransactionBuilder {
recipient: Address,
change: Vec<Address>,
) -> Result<Transaction> {
Ok(
Self::new(ranges, ordinal, recipient, change)
.select_ordinal()?
.align_ordinal()
.pad_alignment_output()?
.add_postage()?
.strip_excess_postage()
.deduct_fee()
.build(),
)
Self::new(ranges, ordinal, recipient, change)
.select_ordinal()?
.align_ordinal()
.pad_alignment_output()?
.add_postage()?
.strip_excess_postage()
.deduct_fee()
.build()
}

fn new(
Expand Down Expand Up @@ -262,7 +265,7 @@ impl TransactionBuilder {
Self::TARGET_FEE_RATE * dummy_transaction.vsize().try_into().unwrap()
}

fn build(self) -> Transaction {
fn build(self) -> Result<Transaction> {
let ordinal = self.ordinal.n();
let recipient = self.recipient.script_pubkey();
let transaction = Transaction {
Expand Down Expand Up @@ -393,7 +396,38 @@ impl TransactionBuilder {
);
}

transaction
let mut offset = 0;
let mut rare_ordinals = Vec::<(Ordinal, u64)>::new();
for input in &transaction.input {
for (start, end) in &self.ranges[&input.previous_output] {
if Ordinal(*start).rarity() > Rarity::Common {
rare_ordinals.push((Ordinal(*start), offset));
}
offset += end - start;
}
}
let total_input_amount = offset;

let mut offset = 0;
let mut recipient_range = (0, 0);
for output in &transaction.output {
if output.script_pubkey == self.recipient.script_pubkey() {
recipient_range = (offset, offset + output.value);
break;
}
offset += output.value;
}

for (rare_ordinal, offset) in &rare_ordinals {
if rare_ordinal != &self.ordinal
&& (offset >= &recipient_range.0 && offset < &recipient_range.1
|| offset >= &(total_input_amount - fee.to_sat()))
{
return Err(Error::AccidentalRareOrdinalSend(*rare_ordinal));
}
}

Ok(transaction)
}

fn calculate_ordinal_offset(&self) -> u64 {
Expand Down Expand Up @@ -542,7 +576,7 @@ mod tests {

pretty_assert_eq!(
tx_builder.build(),
Transaction {
Ok(Transaction {
version: 1,
lock_time: PackedLockTime::ZERO,
input: vec![tx_in(outpoint(1)), tx_in(outpoint(2)), tx_in(outpoint(3))],
Expand All @@ -551,7 +585,7 @@ mod tests {
tx_out(5_000, change(0)),
tx_out(1_774, change(1))
],
}
})
)
}

Expand Down Expand Up @@ -685,7 +719,8 @@ mod tests {
recipient(),
vec![change(0), change(1)],
)
.build();
.build()
.unwrap();
}

#[test]
Expand All @@ -697,7 +732,8 @@ mod tests {
recipient(),
vec![change(0), change(1)],
)
.build();
.build()
.unwrap();
}

#[test]
Expand All @@ -716,7 +752,7 @@ mod tests {
.parse()
.unwrap();

builder.build();
builder.build().unwrap();
}

#[test]
Expand All @@ -733,7 +769,7 @@ mod tests {

builder.outputs[0].1 = Amount::from_sat(0);

builder.build();
builder.build().unwrap();
}

#[test]
Expand Down Expand Up @@ -775,7 +811,8 @@ mod tests {
)
.select_ordinal()
.unwrap()
.build();
.build()
.unwrap();
}

#[test]
Expand Down Expand Up @@ -845,7 +882,7 @@ mod tests {

builder.change_addresses = BTreeSet::new();

builder.build();
builder.build().unwrap();
}

#[test]
Expand All @@ -866,7 +903,8 @@ mod tests {
.unwrap()
.strip_excess_postage()
.deduct_fee()
.build();
.build()
.unwrap();
}

#[test]
Expand All @@ -884,7 +922,8 @@ mod tests {
.unwrap()
.strip_excess_postage()
.deduct_fee()
.build();
.build()
.unwrap();
}

#[test]
Expand All @@ -901,7 +940,8 @@ mod tests {
.select_ordinal()
.unwrap()
.strip_excess_postage()
.build();
.build()
.unwrap();
}

#[test]
Expand All @@ -927,4 +967,34 @@ mod tests {
})
)
}

#[test]
fn rare_ordinals_are_not_sent_to_recipient() {
let utxos = vec![(outpoint(1), vec![(15_000, 25_000), (0, 10_000)])];

pretty_assert_eq!(
TransactionBuilder::build_transaction(
utxos.into_iter().collect(),
Ordinal(24_000),
recipient(),
vec![change(0), change(1),],
),
Err(Error::AccidentalRareOrdinalSend(Ordinal(0)))
)
}

#[test]
fn rare_ordinals_are_not_sent_as_fee() {
let utxos = vec![(outpoint(1), vec![(15_000, 25_000), (0, 100)])];

pretty_assert_eq!(
TransactionBuilder::build_transaction(
utxos.into_iter().collect(),
Ordinal(24_000),
recipient(),
vec![change(0), change(1),],
),
Err(Error::AccidentalRareOrdinalSend(Ordinal(0)))
)
}
}