Crypto/Role/PSBT/Combiner.pm

filter: Statementstmt 100% 61 / 61 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 80.1% 85 / 106 CC 18 SCAR 16.3
CC
18
Cov
85%
_compare_field_lists
7.1
combine
7.0
_resolve_conflict_PSBT_GLOBAL_TX_MODIFIABLE
4.8
CRAP 5.1
« Crypto/Script/Runner.pm ↑ Summary Crypto/Script/Opcode.pm »
1 package Bitcoin::Crypto::Role::PSBT::Combiner;
2 10 $Bitcoin::Crypto::Role::PSBT::Combiner::VERSION = '4.006';
3 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Mooish::Base -standard, -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Bitcoin::Crypto::Util qw(to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _resolve_conflict_PSBT_GLOBAL_TX_MODIFIABLE
12 1 ▶{
Subroutine _resolve_conflict_PSBT_GLOBAL_TX_MODIFIABLE: called CC=4 SCAR=15.8
13 1 my ($self, $field, $other_field) = @_;
14  
15 1 my $v1 = $field->value;
16 1 my $v2 = $other_field->value;
17  
18 1 ▶ $v1->{inputs_modifiable} &&= $v2->{inputs_modifiable};
Condition33%1/3
$v1->{"inputs_modifiable"} &&= $v2->{"inputs_modifiable"}
!ll&&!rl&&r
010
MC/DC0%0/2
$v1->{"inputs_modifiable"} &&= $v2->{"inputs_modifiable"}
$v1->{"inputs_modifiable"} $v2->{"inputs_modifiable"}
Truth table33%1/3
A &&= B
A= $v1->{"inputs_modifiable"}B= $v2->{"inputs_modifiable"}
ABresult
0X0
100
111
19 1 ▶ $v1->{outputs_modifiable} &&= $v2->{outputs_modifiable};
Condition33%1/3
$v1->{"outputs_modifiable"} &&= $v2->{"outputs_modifiable"}
!ll&&!rl&&r
100
MC/DC0%0/2
$v1->{"outputs_modifiable"} &&= $v2->{"outputs_modifiable"}
$v1->{"outputs_modifiable"} $v2->{"outputs_modifiable"}
Truth table33%1/3
A &&= B
A= $v1->{"outputs_modifiable"}B= $v2->{"outputs_modifiable"}
ABresult
0X0
100
111
20 1 ▶ $v1->{has_sighash_single} ||= $v2->{has_sighash_single};
Condition33%1/3
$v1->{"has_sighash_single"} ||= $v2->{"has_sighash_single"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$v1->{"has_sighash_single"} ||= $v2->{"has_sighash_single"}
$v1->{"has_sighash_single"} $v2->{"has_sighash_single"}
Truth table33%1/3
A ||= B
A= $v1->{"has_sighash_single"}B= $v2->{"has_sighash_single"}
ABresult
1X1
011
000
21  
22 1 $field->set_value($v1);
23 }
24  
25 sub _resolve_conflict
26 1 ▶{
Subroutine _resolve_conflict: called CC=3 SCAR=12.0
27 1 my ($self, $field, $other_field) = @_;
28  
29 1 my $type = $field->type->name;
30 1 my $method = $self->can("_resolve_conflict_$type");
31  
32 # only resolve conflicts of certain fields, reject other conflicts with an exception
33 1 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 2 branches
BranchTrueFalseTotal
unless $method 0 1 1
$field->type->has_key_data ? : 0 0 0
34 'combining PSBTs failed - conflicting values for field ' . $field->type->name
35 . ($field->type->has_key_data ? ' and key ' . to_format [hex => $field->raw_key] : '')
36 ) unless $method;
37  
38 1 $self->$method($field, $other_field);
39 }
40  
41 # compares field lists for one field type and possibly one index
42 sub _compare_field_lists
43 52 ▶{
Subroutine _compare_field_lists: called CC=7 SCAR=19.6
44 52 my ($self, $list_new, $list_old) = @_;
45 52 my @missing_fields;
46  
47 52 foreach my $field (@{$list_new}) {
48 52 my $uses_key = $field->type->has_key_data;
49 52 my $found = !!0;
50 52 foreach my $other_field (@{$list_old}) {
51 25 ▶ next if $uses_key && $field->raw_key ne $other_field->raw_key;
Branch: 1 branch
BranchTrueFalseTotal
if $uses_key and $field->raw_key ne $other_field->raw_key 0 25 25
Condition33%1/3
$uses_key and $field->raw_key ne $other_field->raw_key
!ll&&!rl&&r
2500
MC/DC0%0/2
$uses_key and $field->raw_key ne $other_field->raw_key
$uses_key $field->raw_key ne $other_field->raw_key
Truth table33%1/3
A and B
A= $uses_keyB= $field->raw_key ne $other_field->raw_key
ABresult
0X0
100
111
52  
53 # NOTE: resolve conflict on $other_field as first argument - it
54 # must've already be cloned and belong to new psbt, so it must be
55 # changed directly instead of cloning
56 25 ▶ $self->_resolve_conflict($other_field, $field)
Branch: 1 branch
BranchTrueFalseTotal
if $field->raw_value ne $other_field->raw_value 1 24 25
57 if $field->raw_value ne $other_field->raw_value;
58  
59 25 $found = !!1;
60 25 last;
61 }
62  
63 52 ▶ push @missing_fields, $field
Branch: 1 branch
BranchTrueFalseTotal
unless $found 27 25 52
64 unless $found;
65 }
66  
67 52 return @missing_fields;
68 }
69  
70 10 signature_for combine => (
71 method => !!1,
72 positional => [
73 ArrayRef [InstanceOf ['Bitcoin::Crypto::PSBT']],
74 {slurpy => !!1},
75 ],
76 );
77  
78 sub combine
79 5 ▶{
Subroutine combine: called CC=7 SCAR=19.5
80 5 my ($self, $others) = @_;
81  
82 5 my @list = @{$others};
83 5 ▶ unshift @list, $self if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 3 2 5
84 5 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @list > 1 2 3 5
85 'combining PSBTs failed - need at least two PSBTs'
86 ) unless @list > 1;
87  
88 3 my $id = $list[0]->get_id;
89 3 my $new_psbt = $self->new(strict => $list[0]->strict);
90 3 foreach my $psbt (@list) {
91 6 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $psbt->get_id eq $id 0 6 6
92 'combining PSBTs failed - not the same PSBT'
93 ) unless $psbt->get_id eq $id;
94  
95 6 foreach my $field_info ($psbt->list_fields) {
96 52 my ($type, $index) = @{$field_info};
97 52 my @fields = $psbt->get_all_fields($type, $index);
98 52 my @existing_fields = $new_psbt->get_all_fields($type, $index);
99  
100 52 my @to_add = $self->_compare_field_lists(\@fields, \@existing_fields);
101 52 foreach my $field (@to_add) {
102 27 $new_psbt->add_field($field->clone, $index);
103 }
104 }
105 }
106  
107 3 return $new_psbt->check;
108 }
109  
110 10 1;
111  
« Crypto/Script/Runner.pm ↑ Summary Crypto/Script/Opcode.pm »