2023-01-30 09:25:46 +00:00
|
|
|
use monero_serai::{
|
2023-03-11 10:47:25 +00:00
|
|
|
wallet::{TransactionError, extra::MAX_ARBITRARY_DATA_SIZE},
|
2023-01-30 09:25:46 +00:00
|
|
|
transaction::Transaction,
|
|
|
|
};
|
2022-12-09 15:58:11 +00:00
|
|
|
|
|
|
|
mod runner;
|
|
|
|
|
|
|
|
test!(
|
2023-01-30 09:25:46 +00:00
|
|
|
add_single_data_less_than_max,
|
2022-12-09 15:58:11 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, addr| async move {
|
2023-03-11 10:47:25 +00:00
|
|
|
let arbitrary_data = vec![b'\0'; MAX_ARBITRARY_DATA_SIZE - 1];
|
2022-12-09 15:58:11 +00:00
|
|
|
|
|
|
|
// make sure we can add to tx
|
|
|
|
let result = builder.add_data(arbitrary_data.clone());
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
builder.add_payment(addr, 5);
|
|
|
|
(builder.build().unwrap(), (arbitrary_data,))
|
|
|
|
},
|
2023-01-06 09:33:17 +00:00
|
|
|
|_, tx: Transaction, mut scanner: Scanner, data: (Vec<u8>,)| async move {
|
2022-12-09 15:58:11 +00:00
|
|
|
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
|
|
|
assert_eq!(output.commitment().amount, 5);
|
2023-01-01 09:18:23 +00:00
|
|
|
assert_eq!(output.arbitrary_data()[0], data.0);
|
2022-12-09 15:58:11 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
2023-01-30 09:25:46 +00:00
|
|
|
add_multiple_data_less_than_max,
|
2022-12-09 15:58:11 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, addr| async move {
|
2023-03-11 10:47:25 +00:00
|
|
|
let data = vec![b'\0'; MAX_ARBITRARY_DATA_SIZE - 1];
|
2022-12-09 15:58:11 +00:00
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
// Add tx multiple times
|
2022-12-09 15:58:11 +00:00
|
|
|
for _ in 0 .. 5 {
|
2023-01-01 09:18:23 +00:00
|
|
|
let result = builder.add_data(data.clone());
|
2022-12-09 15:58:11 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.add_payment(addr, 5);
|
2023-01-01 09:18:23 +00:00
|
|
|
(builder.build().unwrap(), data)
|
2022-12-09 15:58:11 +00:00
|
|
|
},
|
2023-01-06 09:33:17 +00:00
|
|
|
|_, tx: Transaction, mut scanner: Scanner, data: Vec<u8>| async move {
|
2022-12-09 15:58:11 +00:00
|
|
|
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
|
|
|
assert_eq!(output.commitment().amount, 5);
|
2023-01-01 09:18:23 +00:00
|
|
|
assert_eq!(output.arbitrary_data(), vec![data; 5]);
|
2022-12-09 15:58:11 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
2023-01-30 09:25:46 +00:00
|
|
|
add_single_data_more_than_max,
|
2022-12-09 15:58:11 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, addr| async move {
|
2023-03-11 10:47:25 +00:00
|
|
|
// Make a data that is bigger than the maximum
|
|
|
|
let mut data = vec![b'a'; MAX_ARBITRARY_DATA_SIZE + 1];
|
2022-12-09 15:58:11 +00:00
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
// Make sure we get an error if we try to add it to the TX
|
|
|
|
assert_eq!(builder.add_data(data.clone()), Err(TransactionError::TooMuchData));
|
2022-12-09 15:58:11 +00:00
|
|
|
|
2023-03-11 10:47:25 +00:00
|
|
|
// Reduce data size and retry. The data will now be 255 bytes long (including the added
|
|
|
|
// marker), exactly
|
2023-01-01 09:18:23 +00:00
|
|
|
data.pop();
|
|
|
|
assert!(builder.add_data(data.clone()).is_ok());
|
2022-12-09 15:58:11 +00:00
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
builder.add_payment(addr, 5);
|
|
|
|
(builder.build().unwrap(), data)
|
|
|
|
},
|
2023-01-06 09:33:17 +00:00
|
|
|
|_, tx: Transaction, mut scanner: Scanner, data: Vec<u8>| async move {
|
2023-01-01 09:18:23 +00:00
|
|
|
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
|
|
|
assert_eq!(output.commitment().amount, 5);
|
|
|
|
assert_eq!(output.arbitrary_data(), vec![data]);
|
2022-12-09 15:58:11 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|