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

Fix tx_builder capacity overflow issue #129

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading