Crypto/Script/Compiler.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 94.4% 17 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 3 totaltotal 92.6% 113 / 122 CC 11 SCAR 12.5
CC
11
Cov
96%
compile
8.0
assert_valid
3.0
_compile_data_push
2.0
CRAP 3.5
« Crypto/DerivationPath.pm ↑ Summary Crypto/Key/Private.pm »
1 package Bitcoin::Crypto::Script::Compiler;
2 64 $Bitcoin::Crypto::Script::Compiler::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 Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 64 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 64 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 64 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 64 ▶use Bitcoin::Crypto::Helpers qw(die_no_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 64 ▶use Bitcoin::Crypto::Script::Compiler::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 64 has param 'operations' => (
17 isa => ArrayRef [ArrayRef],
18 );
19  
20 64 has field 'unconditionally_valid' => (
21 isa => Bool,
22 writer => -hidden,
23 );
24  
25 has field 'errors' => (
26 882 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 64 );
28  
29 64 has field 'opcode_count' => (
30 isa => PositiveOrZeroInt,
31 writer => -hidden,
32 );
33  
34 sub has_errors
35 {
36 890 ▶ my $self = shift;
Subroutine has_errors: called CC=1 SCAR=0.0
Pod undocumented
37  
38 890 return @{$self->errors} > 0;
39 }
40  
41 sub assert_valid
42 {
43 607 ▶ my $self = shift;
Subroutine assert_valid: called CC=3 SCAR=11.0
Pod undocumented
44  
45 607 ▶ return if $self->unconditionally_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->unconditionally_valid 87 520 607
46 520 ▶ return unless $self->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_errors 489 31 520
47  
48 # show just the first error
49 31 die $self->errors->[0];
50 }
51  
52 sub compile
53 {
54 882 ▶ my ($class, $script) = @_;
Subroutine compile: called CC=8 SCAR=20.8
Pod undocumented
55 882 my $opcode_class = $script->opcode_class;
56 882 my @ops;
57 882 my $non_push_opcodes = 0;
58 882 my @debug_ops;
59  
60 882 my $self = $class->new(
61 operations => \@ops
62 );
63  
64 882 my $raw_script = $script->to_serialized;
65 882 my %context = (
66 serialized => $raw_script,
67 position => 0,
68 offset => 0,
69 size => length $raw_script,
70 );
71  
72 882 ▶ while ($context{offset} < $context{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context{"offset"} < $context{"size"}) 2259 882 3141
73 2259 try {
74 2259 my $this_byte = substr $context{serialized}, $context{offset}++, 1;
75  
76 2259 my $opcode = $opcode_class->get_opcode_by_code(ord $this_byte);
77 2259 push @debug_ops, $opcode->name;
78 2259 my $compiled_op = Bitcoin::Crypto::Script::Compiler::Opcode->new($opcode, $this_byte);
79  
80 2259 push @ops, $compiled_op;
81 2259 ▶ $non_push_opcodes++ if $opcode->non_push_opcode;
Branch: 1 branch
BranchTrueFalseTotal
if $opcode->non_push_opcode 969 1290 2259
82  
83 2259 ▶ if ($opcode->has_on_compilation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode->has_on_compilation) 1747 512 2259
84 1747 $opcode->on_compilation->($self, $compiled_op, \%context);
85 }
86  
87 2061 $context{position}++;
88 }
89 catch ($ex) {
90 198 ▶ if (blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::ScriptCompilation')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ex) and $ex->isa('Bitcoin::Crypto::Exception::ScriptCompilation')) { } 198 0 198
Condition33%1/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::ScriptCompilation")
!ll&&!rl&&r
00198
MC/DC0%0/2
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::ScriptCompilation")
builtin::blessed($ex) $ex->isa("Bitcoin::Crypto::Exception::ScriptCompilation")
Truth table33%1/3
A and B
A= builtin::blessed($ex)B= $ex->isa("Bitcoin::Crypto::Exception::ScriptCompilation")
ABresult
0X0
100
111
91 198 $ex->set_script(\@debug_ops);
92 198 $ex->set_error_position($context{position});
93 198 push @{$self->errors}, $ex;
94 198 $context{position}++;
95 }
96 else {
97 0 die $ex;
98 }
99 }
100 }
101  
102 7 push @{$self->errors},
103 Bitcoin::Crypto::Exception::ScriptCompilation->new(message => 'not enough bytes of data in the script')
104 882 ▶ unless $context{offset} == $context{size};
Branch: 1 branch
BranchTrueFalseTotal
unless $context{"offset"} == $context{"size"} 7 875 882
105  
106 3 push @{$self->errors},
107 Bitcoin::Crypto::Exception::ScriptCompilation->new(message => 'some OP_IFs were not closed')
108 882 ▶ if $context{branch};
Branch: 1 branch
BranchTrueFalseTotal
if $context{"branch"} 3 879 882
109  
110 882 $self->_set_opcode_count($non_push_opcodes);
111 882 return $self;
112 }
113  
114 sub _compile_data_push
115 {
116 708 ▶ my ($self, $context, $size) = @_;
Subroutine _compile_data_push: called CC=2 SCAR=6.9
117  
118 708 ▶ $self->_invalid_script(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 1 707 708
119 'no PUSHDATA size in the script'
120 ) unless defined $size;
121  
122 # we may go past the script end - that's okay
123 707 $context->{offset} += $size;
124  
125 707 return substr $context->{serialized}, $context->{offset} - $size, $size;
126 }
127  
128 sub _invalid_script
129 {
130 198 ▶ my ($self, $error) = @_;
Subroutine _invalid_script: called CC=1 SCAR=0.0
131  
132 198 Bitcoin::Crypto::Exception::ScriptCompilation->raise($error);
133 }
134  
135 sub _unconditionally_valid_script
136 {
137 174 ▶ my ($self, $error) = @_;
Subroutine _unconditionally_valid_script: called CC=1 SCAR=0.0
138  
139 174 $self->_set_unconditionally_valid(!!1);
140 }
141  
142 64 1;
143  
« Crypto/DerivationPath.pm ↑ Summary Crypto/Key/Private.pm »