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.004';
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 use constant {
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 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub _blueprints
34 {
35 352 ▶ my ($class) = @_;
Subroutine _blueprints: called CC=2 SCAR=7.0
36  
37 352 state $blueprints = {};
38 352 ▶ return $blueprints->{$class} //= $class->_build_blueprints;
Condition66%2/3
$blueprints->{$class} //= $class->_build_blueprints
l!l&&r!l&&!r
326260
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 {
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 = (
Subroutine _build_blueprints: called CC=9 SCAR=22.1
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 {
155 1394 ▶ my ($class, $ops, $type, $parts) = @_;
Subroutine _check_blueprint: called CC=25 SCAR=32.2
156  
157 1394 my $parts_size = @{$parts};
158 1394 my $pos = 0;
159  
160 1394 my $address;
161 my $segwit_version;
162  
163 1394 foreach my $part (@{$parts}) {
164 2012 my ($kind, $lookup) = @{$part};
165 2012 my $op_data = $ops->[$pos];
166 2012 ▶ return undef unless $op_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data 27 1985 2012
167  
168 1985 ▶ if ($kind == KIND_OPCODE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($kind == 1) { } 384 1601 1985
elsif ($kind == 4 or $kind == 5) { } 524 1077 1601
elsif ($kind == 3 or $kind == 2) { } 1053 24 1077
elsif ($kind == 6) { } 24 0 24
169 384 ▶ return undef unless $lookup == $op_data->[0]->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $lookup == $op_data->[0]->code 235 149 384
170 }
171 ▶ elsif ($kind == KIND_ADDRESS || $kind == KIND_DATA) {
Condition100%3/3
$kind == 4 or $kind == 5
l!l&&r!l&&!r
4181061077
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 524 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 32 492 524
173 492 ▶ return undef unless $lookup->{length $op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{length $op_data->[2]} 177 315 492
174 315 ▶ 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 311 315
175  
176 311 ▶ $address = $op_data->[2]
Branch: 1 branch
BranchTrueFalseTotal
if $kind == 4 281 30 311
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
7697724
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 1053 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 222 831 1053
181 831 ▶ return undef unless $lookup->{$op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{$op_data->[2]} 362 469 831
182 469 ▶ 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 469 469
183  
184 # numify bigint on 32 bit arch
185 469 ▶ $segwit_version = '' . Bitcoin::Crypto::Script::Runner->to_int($op_data->[2])
Branch: 1 branch
BranchTrueFalseTotal
if $kind == 2 416 53 469
186 if $kind == KIND_SEGWIT_VERSION;
187 }
188 elsif ($kind == KIND_DATA_REPEATED) {
189 24 my $count = 0;
190 24 while (1) {
191 65 ▶ return undef unless $op_data && $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data and $op_data->[0]->pushop 3 62 65
Condition66%2/3
$op_data and $op_data->[0]->pushop
!ll&&!rl&&r
0362
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 62 ▶ 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 60 62
193 60 ▶ last unless $lookup->{length $op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{length $op_data->[2]} 19 41 60
194  
195 41 $pos += 1;
196 41 $op_data = $ops->[$pos];
197 41 $count += 1;
198 }
199  
200 19 ▶ return undef unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 0 19 19
201 19 ▶ 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 17 19
202  
203 # check the same opcode again with next blueprint part
204 17 next;
205 }
206  
207 929 ++$pos;
208 }
209  
210 328 ▶ return undef unless $pos == @{$ops};
Branch: 1 branch
BranchTrueFalseTotal
unless $pos == @{$ops;} 18 310 328
211  
212 310 return $class->new(
213 type => $type,
214 address => $address,
215 segwit_version => $segwit_version,
216 );
217 }
218  
219 sub check
220 {
221 359 ▶ my ($class, $script) = @_;
Subroutine check: called CC=4 SCAR=13.9
Pod undocumented
222  
223 359 my $compiler = $script->_compiler;
224 359 ▶ if (!$compiler->has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compiler->has_errors) 352 7 359
225 352 my $operations = $compiler->operations;
226  
227 352 foreach my $variant (@{$class->_blueprints}) {
228 1394 my $recognized = $class->_check_blueprint($operations, @{$variant});
229 1394 ▶ return $recognized if defined $recognized;
Branch: 1 branch
BranchTrueFalseTotal
if defined $recognized 310 1084 1394
230 }
231 }
232  
233 # unknown
234 49 return $class->new;
235 }
236  
237 64 1;
238  
↑ Summary Crypto/Transaction.pm »