Skip to content

Commit 10bc388

Browse files
committed
Fixed tx size calculation when required signers are used. Beta version bump.
1 parent d5b93bb commit 10bc388

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cardano-serialization-lib",
3-
"version": "10.2.0-beta.4",
3+
"version": "10.2.0-beta.5",
44
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
55
"scripts": {
66
"rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; wasm-pack pack) && npm run js:flowgen",

rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cardano-serialization-lib"
3-
version = "10.2.0-beta.4"
3+
version = "10.2.0-beta.5"
44
edition = "2018"
55
authors = ["EMURGO"]
66
license = "MIT"

rust/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ impl Certificates {
230230
pub type RequiredSigners = Ed25519KeyHashes;
231231
pub type RequiredSignersSet = BTreeSet<Ed25519KeyHash>;
232232

233+
impl From<&Ed25519KeyHashes> for RequiredSignersSet {
234+
fn from(keys: &Ed25519KeyHashes) -> Self {
235+
keys.0.iter().fold(BTreeSet::new(), |mut set, k| {
236+
set.insert(k.clone());
237+
set
238+
})
239+
}
240+
}
241+
233242
#[wasm_bindgen]
234243
#[derive(Clone)]
235244
pub struct TransactionBody {
@@ -614,10 +623,15 @@ to_from_bytes!(Ed25519KeyHashes);
614623

615624
#[wasm_bindgen]
616625
impl Ed25519KeyHashes {
626+
617627
pub fn new() -> Self {
618628
Self(Vec::new())
619629
}
620630

631+
pub(crate) fn from_vec(vec: Vec<Ed25519KeyHash>) -> Self {
632+
Self(vec)
633+
}
634+
621635
pub fn len(&self) -> usize {
622636
self.0.len()
623637
}

rust/src/tx_builder.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fn fake_raw_key_public() -> PublicKey {
7171
fn count_needed_vkeys(tx_builder: &TransactionBuilder) -> usize {
7272
let mut input_hashes: RequiredSignersSet = RequiredSignersSet::from(&tx_builder.inputs);
7373
input_hashes.extend(RequiredSignersSet::from(&tx_builder.collateral));
74+
input_hashes.extend(RequiredSignersSet::from(&tx_builder.required_signers));
7475
if let Some(scripts) = &tx_builder.mint_scripts {
7576
input_hashes.extend(RequiredSignersSet::from(scripts));
7677
}
@@ -5221,5 +5222,49 @@ mod tests {
52215222
assert_eq!(rs.get(1), s3);
52225223
assert_eq!(rs.get(2), s2);
52235224
}
5225+
5226+
#[test]
5227+
fn test_required_signers_are_added_to_the_witness_estimate() {
5228+
5229+
fn count_fake_witnesses_with_required_signers(keys: &Ed25519KeyHashes) -> usize {
5230+
let mut tx_builder = create_reallistic_tx_builder();
5231+
tx_builder.set_fee(&to_bignum(42));
5232+
tx_builder.add_input(
5233+
&create_base_address(0),
5234+
&TransactionInput::new(&fake_tx_hash(0), 0),
5235+
&Value::new(&to_bignum(10_000_000)),
5236+
);
5237+
5238+
keys.0.iter().for_each(|k| {
5239+
tx_builder.add_required_signer(k);
5240+
});
5241+
5242+
let tx: Transaction = fake_full_tx(&tx_builder, tx_builder.build().unwrap()).unwrap();
5243+
tx.witness_set.vkeys.unwrap().len()
5244+
}
5245+
5246+
assert_eq!(count_fake_witnesses_with_required_signers(
5247+
&Ed25519KeyHashes::new(),
5248+
), 1);
5249+
5250+
assert_eq!(count_fake_witnesses_with_required_signers(
5251+
&Ed25519KeyHashes::from_vec(vec![fake_key_hash(1)]),
5252+
), 2);
5253+
5254+
assert_eq!(count_fake_witnesses_with_required_signers(
5255+
&Ed25519KeyHashes::from_vec(vec![fake_key_hash(1), fake_key_hash(2)]),
5256+
), 3);
5257+
5258+
// This case still produces only 3 fake signatures, because the same key is already used in the input address
5259+
assert_eq!(count_fake_witnesses_with_required_signers(
5260+
&Ed25519KeyHashes::from_vec(vec![fake_key_hash(1), fake_key_hash(2), fake_key_hash(0)]),
5261+
), 3);
5262+
5263+
// When a different key is used - 4 fake witnesses are produced
5264+
assert_eq!(count_fake_witnesses_with_required_signers(
5265+
&Ed25519KeyHashes::from_vec(vec![fake_key_hash(1), fake_key_hash(2), fake_key_hash(3)]),
5266+
), 4);
5267+
}
5268+
52245269
}
52255270

0 commit comments

Comments
 (0)