Ensure the size of our locking script never changes

This commit is contained in:
Thomas Eizinger 2021-08-12 11:28:00 +10:00
parent 4405dbcf9c
commit 148fdb8d0a
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
4 changed files with 35 additions and 8 deletions

View File

@ -80,6 +80,7 @@ get-port = "3"
hyper = "0.14"
monero-harness = { path = "../monero-harness" }
port_check = "0.1"
proptest = "1"
serde_cbor = "0.11"
spectral = "0.6"
tempfile = "3"

View File

@ -7,11 +7,11 @@ use ::bitcoin::{OutPoint, TxIn, TxOut, Txid};
use anyhow::{bail, Result};
use bdk::database::BatchDatabase;
use bitcoin::Script;
use ecdsa_fun::fun::Point;
use miniscript::{Descriptor, DescriptorTrait};
use rand::thread_rng;
use serde::{Deserialize, Serialize};
const SCRIPT_SIZE: usize = 34;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TxLock {
inner: PartiallySignedTransaction,
@ -112,12 +112,7 @@ impl TxLock {
/// Calculate the size of the script used by this transaction.
pub fn script_size() -> usize {
build_shared_output_descriptor(
Point::random(&mut thread_rng()),
Point::random(&mut thread_rng()),
)
.script_pubkey()
.len()
SCRIPT_SIZE
}
pub fn script_pubkey(&self) -> Script {
@ -245,6 +240,17 @@ mod tests {
result.expect_err("PSBT to be invalid");
}
proptest::proptest! {
#[test]
fn estimated_tx_lock_script_size_never_changes(a in crate::proptest::ecdsa_fun::point(), b in crate::proptest::ecdsa_fun::point()) {
proptest::prop_assume!(a != b);
let computed_size = build_shared_output_descriptor(a, b).script_pubkey().len();
assert_eq!(computed_size, SCRIPT_SIZE);
}
}
/// Helper function that represents Bob's action of constructing the PSBT.
///
/// Extracting this allows us to keep the tests concise.

View File

@ -32,3 +32,6 @@ pub mod tor;
pub mod tracing_ext;
mod monero_ext;
#[cfg(test)]
mod proptest;

17
swap/src/proptest.rs Normal file
View File

@ -0,0 +1,17 @@
use proptest::prelude::*;
pub mod ecdsa_fun {
use super::*;
use ::ecdsa_fun::fun::marker::{Mark, NonZero, Normal};
use ::ecdsa_fun::fun::{Point, Scalar, G};
pub fn point() -> impl Strategy<Value = Point> {
scalar().prop_map(|mut scalar| Point::from_scalar_mul(&G, &mut scalar).mark::<Normal>())
}
pub fn scalar() -> impl Strategy<Value = Scalar> {
prop::array::uniform32(0..255u8).prop_filter_map("generated the 0 element", |bytes| {
Scalar::from_bytes_mod_order(bytes).mark::<NonZero>()
})
}
}