Skip to content

Commit

Permalink
fix tx_builder capacity overflow issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 19, 2024
1 parent 441aa78 commit 89b5f19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, u64};

use ckb_dao_utils::pack_dao_data;
use ckb_hash::blake2b_256;
Expand Down Expand Up @@ -182,6 +182,31 @@ fn test_transfer_from_sighash() {
ctx.verify(tx, FEE_RATE).unwrap();
}

#[test]
fn test_transfer_capacity_overflow() {
let sender = build_sighash_script(ACCOUNT1_ARG);
let receiver = build_sighash_script(ACCOUNT2_ARG);
let ctx = init_context(Vec::new(), vec![(sender.clone(), Some(100 * ONE_CKB))]);

let large_amount: u64 = u64::MAX;
let output = CellOutput::new_builder()
.capacity((large_amount).pack())
.lock(receiver)
.build();
let builder = CapacityTransferBuilder::new(vec![(output.clone(), Bytes::default())]);
let placeholder_witness = WitnessArgs::new_builder()
.lock(Some(Bytes::from(vec![0u8; 65])).pack())
.build();
let balancer =
CapacityBalancer::new_simple(sender.clone(), placeholder_witness.clone(), FEE_RATE);

let unlockers: HashMap<ScriptId, Box<dyn ScriptUnlocker>> = HashMap::default();
let mut cell_collector = ctx.to_live_cells_context();
let res = builder.build_unlocked(&mut cell_collector, &ctx, &ctx, &ctx, &balancer, &unlockers);
assert!(res.is_err());
assert!(res.unwrap_err().to_string().contains("capacity not enough"));
}

#[test]
fn test_transfer_from_multisig() {
let lock_args = vec![
Expand Down
7 changes: 6 additions & 1 deletion src/tx_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,12 @@ fn rebalance_tx_capacity(
need_more_capacity = min_fee - fee;
}
Err(TransactionFeeError::CapacityOverflow(delta)) => {
need_more_capacity = delta + min_fee;
need_more_capacity = delta.checked_add(min_fee).ok_or_else(|| {
BalanceTxCapacityError::CapacityNotEnough(format!(
"need more capacity, value={}",
HumanCapacity(delta)
))
})?;
}
Err(err) => {
return Err(err.into());
Expand Down

0 comments on commit 89b5f19

Please sign in to comment.