Crypto/Script/Recognition.pm

filter: Statementstmt 98.8% 89 / 90 Branchbran 92.3% 48 / 52 Conditioncond 83.3% 15 / 18 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 92.3% 170 / 184 CC 37 SCAR 27.5
CC
37
Cov
95%
_check_blueprint
25.1
_build_blueprints
9.1
check
4.0
CRAP 15.6
↑ Summary Crypto/Transaction.pm »
1 package Bitcoin::Crypto::Script::Recognition;
2 64 $Bitcoin::Crypto::Script::Recognition::VERSION = '4.005';
3 64 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 64 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 64 ▶use Bitcoin::Crypto::Helpers qw(standard_push);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 64 ▶use Bitcoin::Crypto::Script::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 64 ▶use Bitcoin::Crypto::Script::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 64 has param 'type' => (
13 default => undef,
14 );
15  
16 64 has param 'address' => (
17 default => undef,
18 );
19  
20 64 has param 'segwit_version' => (
21 default => undef,
22 );
23  
24 64 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 64 KIND_OPCODE => 1,
26 KIND_SEGWIT_VERSION => 2,
27 KIND_NUMBER => 3,
28 KIND_ADDRESS => 4,
29 KIND_DATA => 5,
30 KIND_DATA_REPEATED => 6,
31 64 };
32  
33 sub _blueprints
34 353 ▶{
Subroutine _blueprints: called CC=2 SCAR=7.0
35 353 my ($class) = @_;
36  
37 353 state $blueprints = {};
38 353 ▶ return $blueprints->{$class} //= $class->_build_blueprints;
Condition66%2/3
$blueprints->{$class} //= $class->_build_blueprints
l!l&&r!l&&!r
327260
MC/DC0%0/2
$blueprints->{$class} //= $class->_build_blueprints
$blueprints->{$class} $class->_build_blueprints
Truth table66%2/3
A //= B
A= $blueprints->{$class}B= $class->_build_blueprints
ABresult
1X1
011
000
39 }
40  
41 sub _build_blueprints
42 26 ▶{
Subroutine _build_blueprints: called CC=9 SCAR=22.1
43 # blueprints for standard transaction types
44 # constant size script types should be placed first so they are thrown away sooner
45 # more common script types should be placed first so they can be found faster
46 26 my @blueprints = (
47 [
48 P2TR => [
49 [KIND_SEGWIT_VERSION, 1],
50 [KIND_ADDRESS, 32],
51 ]
52 ],
53  
54 [
55 P2WPKH => [
56 [KIND_SEGWIT_VERSION, 0],
57 [KIND_ADDRESS, 20],
58 ],
59 ],
60  
61 [
62 P2WSH => [
63 [KIND_SEGWIT_VERSION, 0],
64 [KIND_ADDRESS, 32],
65 ]
66 ],
67  
68 [
69 P2PKH => [
70 'OP_DUP',
71 'OP_HASH160',
72 [KIND_ADDRESS, 20],
73 'OP_EQUALVERIFY',
74 'OP_CHECKSIG',
75 ]
76 ],
77  
78 [
79 P2SH => [
80 'OP_HASH160',
81 [KIND_ADDRESS, 20],
82 'OP_EQUAL',
83 ]
84 ],
85  
86 [
87 P2PK => [
88 [KIND_DATA, 33, 65],
89 'OP_CHECKSIG',
90 ]
91 ],
92  
93 [
94 'UNKNOWN_SEGWIT' => [
95 [KIND_SEGWIT_VERSION, 0 .. 16],
96 [KIND_DATA, 2 .. 40],
97 ],
98 ],
99  
100 [
101 NULLDATA => [
102 'OP_RETURN',
103 [KIND_ADDRESS, 1 .. 80],
104 ]
105 ],
106  
107 [
108 P2MS => [
109 [KIND_NUMBER, 0 .. 20],
110 [KIND_DATA_REPEATED, 33, 65],
111 [KIND_NUMBER, 0 .. 20],
112 'OP_CHECKMULTISIG',
113 ]
114 ],
115 );
116  
117 # pre-process blueprints for faster execution
118 26 foreach my $variant (@blueprints) {
119 234 my ($type, $parts) = @$variant;
120  
121 234 foreach my $part (@$parts) {
122 624 ▶ if (ref $part) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $part) { } 390 234 624
123 390 my ($kind, @vars) = @$part;
124  
125 390 ▶ if ($kind == KIND_ADDRESS || $kind == KIND_DATA) {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind == 4 or $kind == 5) { } 208 182 390
elsif ($kind == 6) { } 26 156 182
elsif ($kind == 3 or $kind == 2) { } 156 0 156
Condition100%3/3
$kind == 4 or $kind == 5
l!l&&r!l&&!r
15652182
MC/DC100%2/2
$kind == 4 or $kind == 5
$kind == 4 $kind == 5
Truth table100%3/3
A or B
A= $kind == 4B= $kind == 5
ABresult
1X1
011
000
126  
127 # no special handling
128 }
129 elsif ($kind == KIND_DATA_REPEATED) {
130  
131 # no special handling
132 }
133 ▶ elsif ($kind == KIND_NUMBER || $kind == KIND_SEGWIT_VERSION) {
Condition66%2/3
$kind == 3 or $kind == 2
l!l&&r!l&&!r
521040
MC/DC0%0/2
$kind == 3 or $kind == 2
$kind == 3 $kind == 2
Truth table66%2/3
A or B
A= $kind == 3B= $kind == 2
ABresult
1X1
011
000
134 156 @vars = map { Bitcoin::Crypto::Script::Runner->from_int($_) } @vars;
135 }
136 else {
137 0 die "invalid blueprint kind: $kind";
138 }
139  
140 390 my %lookup = map { $_ => !!1 } @vars;
141 390 $part = [$kind, \%lookup];
142 }
143 else {
144 234 my $opcode = Bitcoin::Crypto::Script::Opcode->get_opcode_by_name($part);
145 234 $part = [KIND_OPCODE, $opcode->code];
146 }
147 }
148 }
149  
150 26 return \@blueprints;
151 }
152  
153 sub _check_blueprint
154 1403 ▶{
Subroutine _check_blueprint: called CC=25 SCAR=32.2
155 1403 my ($class, $ops, $type, $parts) = @_;
156  
157 1403 my $parts_size = @{$parts};
158 1403 my $pos = 0;
159  
160 1403 my $address;
161 my $segwit_version;
162  
163 1403 foreach my $part (@{$parts}) {
164 2025 my ($kind, $lookup) = @{$part};
165 2025 my $op_data = $ops->[$pos];
166 2025 ▶ return undef unless $op_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data 27 1998 2025
167  
168 1998 ▶ if ($kind == KIND_OPCODE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($kind == 1) { } 388 1610 1998
elsif ($kind == 4 or $kind == 5) { } 526 1084 1610
elsif ($kind == 3 or $kind == 2) { } 1059 25 1084
elsif ($kind == 6) { } 25 0 25
169 388 ▶ return undef unless $lookup == $op_data->[0]->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $lookup == $op_data->[0]->code 238 150 388
170 }
171 ▶ elsif ($kind == KIND_ADDRESS || $kind == KIND_DATA) {
Condition100%3/3
$kind == 4 or $kind == 5
l!l&&r!l&&!r
4181081084
MC/DC100%2/2
$kind == 4 or $kind == 5
$kind == 4 $kind == 5
Truth table100%3/3
A or B
A= $kind == 4B= $kind == 5
ABresult
1X1
011
000
172 526 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 32 494 526
173 494 ▶ return undef unless $lookup->{length $op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{length $op_data->[2]} 178 316 494
174 316 ▶ return undef unless standard_push($op_data->[0]->name, $op_data->[2]);
Branch: 1 branch
BranchTrueFalseTotal
unless standard_push($op_data->[0]->name, $op_data->[2]) 4 312 316
175  
176 312 ▶ $address = $op_data->[2]
Branch: 1 branch
BranchTrueFalseTotal
if $kind == 4 281 31 312
177 if $kind == KIND_ADDRESS;
178 }
179 ▶ elsif ($kind == KIND_NUMBER || $kind == KIND_SEGWIT_VERSION) {
Condition100%3/3
$kind == 3 or $kind == 2
l!l&&r!l&&!r
7898125
MC/DC100%2/2
$kind == 3 or $kind == 2
$kind == 3 $kind == 2
Truth table100%3/3
A or B
A= $kind == 3B= $kind == 2
ABresult
1X1
011
000
180 1059 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 222 837 1059
181 837 ▶ return undef unless $lookup->{$op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{$op_data->[2]} 365 472 837
182 472 ▶ return undef unless standard_push($op_data->[0]->name, $op_data->[2]);
Branch: 1 branch
BranchTrueFalseTotal
unless standard_push($op_data->[0]->name, $op_data->[2]) 0 472 472
183  
184 # numify bigint on 32 bit arch
185 472 ▶ $segwit_version = '' . Bitcoin::Crypto::Script::Runner->to_int($op_data->[2])
Branch: 1 branch
BranchTrueFalseTotal
if $kind == 2 417 55 472
186 if $kind == KIND_SEGWIT_VERSION;
187 }
188 elsif ($kind == KIND_DATA_REPEATED) {
189 25 my $count = 0;
190 25 while (1) {
191 68 ▶ return undef unless $op_data && $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data and $op_data->[0]->pushop 3 65 68
Condition66%2/3
$op_data and $op_data->[0]->pushop
!ll&&!rl&&r
0365
MC/DC50%1/2
$op_data and $op_data->[0]->pushop
$op_data $op_data->[0]->pushop
Truth table66%2/3
A and B
A= $op_dataB= $op_data->[0]->pushop
ABresult
0X0
100
111
192 65 ▶ return undef unless standard_push($op_data->[0]->name, $op_data->[2]);
Branch: 1 branch
BranchTrueFalseTotal
unless standard_push($op_data->[0]->name, $op_data->[2]) 2 63 65
193 63 ▶ last unless $lookup->{length $op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{length $op_data->[2]} 20 43 63
194  
195 43 $pos += 1;
196 43 $op_data = $ops->[$pos];
197 43 $count += 1;
198 }
199  
200 20 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 0 20 20
201 20 ▶ return undef unless Bitcoin::Crypto::Script::Runner->from_int($count) eq $op_data->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless "Bitcoin::Crypto::Script::Runner"->from_int($count) eq $op_data->[2] 2 18 20
202  
203 # check the same opcode again with next blueprint part
204 18 next;
205 }
206  
207 934 ++$pos;
208 }
209  
210 330 ▶ return undef unless $pos == @{$ops};
Branch: 1 branch
BranchTrueFalseTotal
unless $pos == @{$ops;} 19 311 330
211  
212 311 return $class->new(
213 type => $type,
214 address => $address,
215 segwit_version => $segwit_version,
216 );
217 }
218  
219 sub check
220 360 ▶{
Subroutine check: called CC=4 SCAR=13.9
Pod undocumented
221 360 my ($class, $script) = @_;
222  
223 360 my $compiler = $script->_compiler;
224 360 ▶ if (!$compiler->has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compiler->has_errors) 353 7 360
225 353 my $operations = $compiler->operations;
226  
227 353 foreach my $variant (@{$class->_blueprints}) {
228 1403 my $recognized = $class->_check_blueprint($operations, @{$variant});
229 1403 ▶ return $recognized if defined $recognized;
Branch: 1 branch
BranchTrueFalseTotal
if defined $recognized 311 1092 1403
230 }
231 }
232  
233 # unknown
234 49 return $class->new;
235 }
236  
237 64 1;
238  
↑ Summary Crypto/Transaction.pm »