Skip to content

Commit d6ed85a

Browse files
committed
new builder getter output_sizes
1 parent 15da1a7 commit d6ed85a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

rust/src/tx_builder.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl TransactionBuilder {
523523
}
524524
}
525525

526-
fn build_and_size(&self) -> (TransactionBody, usize) {
526+
fn build_and_size(&self) -> Result<(TransactionBody, usize), JsError> {
527527
let fee = self.fee.ok_or_else(|| JsError::from_str("Fee not specified"))?;
528528
let built = TransactionBody {
529529
inputs: TransactionInputs(self.inputs.iter().map(|ref tx_builder_input| tx_builder_input.input.clone()).collect()),
@@ -548,15 +548,19 @@ impl TransactionBuilder {
548548
// we must build a tx with fake data (of correct size) to check the final Transaction size
549549
let full_tx = fake_full_tx(self, built)?;
550550
let full_tx_size = full_tx.to_bytes().len();
551-
return (full_tx.body, full_tx_size);
551+
return Ok((full_tx.body, full_tx_size));
552552
}
553553

554-
pub fn full_size(&self) -> usize {
555-
return self.build_and_size().1;
554+
pub fn full_size(&self) -> Result<usize, JsError> {
555+
return self.build_and_size().map(|r| { r.1 });
556+
}
557+
558+
pub fn output_sizes(&self) -> Vec<usize> {
559+
return self.outputs.0.iter().map(|o| { o.to_bytes().len() }).collect();
556560
}
557561

558562
pub fn build(&self) -> Result<TransactionBody, JsError> {
559-
let (body, full_tx_size) = self.build_and_size();
563+
let (body, full_tx_size) = self.build_and_size()?;
560564
if full_tx_size > self.max_tx_size as usize {
561565
Err(JsError::from_str(&format!(
562566
"Maximum transaction size of {} exceeded. Found: {}",
@@ -658,6 +662,8 @@ mod tests {
658662
tx_builder.get_explicit_input().unwrap().checked_add(&tx_builder.get_implicit_input().unwrap()).unwrap(),
659663
tx_builder.get_explicit_output().unwrap().checked_add(&Value::new(&tx_builder.get_fee_if_set().unwrap())).unwrap()
660664
);
665+
assert_eq!(tx_builder.full_size().unwrap(), 283);
666+
assert_eq!(tx_builder.output_sizes(), vec![61, 65]);
661667
let _final_tx = tx_builder.build(); // just test that it doesn't throw
662668
}
663669

@@ -1346,7 +1352,14 @@ mod tests {
13461352
fn build_tx_no_useless_multiasset() {
13471353
let linear_fee = LinearFee::new(&to_bignum(44), &to_bignum(155381));
13481354
let mut tx_builder =
1349-
TransactionBuilder::new(&linear_fee, &to_bignum(1000000), &to_bignum(500000000), &to_bignum(2000000));
1355+
TransactionBuilder::new(
1356+
&linear_fee,
1357+
&to_bignum(1000000),
1358+
&to_bignum(500000000),
1359+
&to_bignum(2000000),
1360+
MAX_OUTPUT_SIZE,
1361+
MAX_TX_SIZE,
1362+
);
13501363

13511364
let policy_id = &PolicyID::from([0u8; 28]);
13521365
let name = AssetName::new(vec![0u8, 1, 2, 3]).unwrap();

0 commit comments

Comments
 (0)