| 1 | package Bitcoin::Crypto::Script::Compiler; | ||||||||||||||||||||||||||||
| 2 | 64 | $Bitcoin::Crypto::Script::Compiler::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 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 | 885 | ▶ | 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 | 896 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
has_errors: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 896 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 896 | return @{$self->errors} > 0; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub assert_valid | ||||||||||||||||||||||||||||
| 42 | 612 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
assert_valid: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 612 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 612 | ▶ | return if $self->unconditionally_valid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 525 | ▶ | return unless $self->has_errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # show just the first error | ||||||||||||||||||||||||||||
| 49 | 31 | die $self->errors->[0]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub compile | ||||||||||||||||||||||||||||
| 53 | 885 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 885 | my ($class, $script) = @_; | |||||||||||||||||||||||||||
| 55 | 885 | my $opcode_class = $script->opcode_class; | |||||||||||||||||||||||||||
| 56 | 885 | my @ops; | |||||||||||||||||||||||||||
| 57 | 885 | my $non_push_opcodes = 0; | |||||||||||||||||||||||||||
| 58 | 885 | my @debug_ops; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 885 | my $self = $class->new( | |||||||||||||||||||||||||||
| 61 | operations => \@ops | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 885 | my $raw_script = $script->to_serialized; | |||||||||||||||||||||||||||
| 65 | 885 | my %context = ( | |||||||||||||||||||||||||||
| 66 | serialized => $raw_script, | ||||||||||||||||||||||||||||
| 67 | position => 0, | ||||||||||||||||||||||||||||
| 68 | offset => 0, | ||||||||||||||||||||||||||||
| 69 | size => length $raw_script, | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 885 | ▶ | while ($context{offset} < $context{size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2271 | try { | |||||||||||||||||||||||||||
| 74 | 2271 | my $this_byte = substr $context{serialized}, $context{offset}++, 1; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2271 | my $opcode = $opcode_class->get_opcode_by_code(ord $this_byte); | |||||||||||||||||||||||||||
| 77 | 2271 | push @debug_ops, $opcode->name; | |||||||||||||||||||||||||||
| 78 | 2271 | my $compiled_op = Bitcoin::Crypto::Script::Compiler::Opcode->new($opcode, $this_byte); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2271 | push @ops, $compiled_op; | |||||||||||||||||||||||||||
| 81 | 2271 | ▶ | $non_push_opcodes++ if $opcode->non_push_opcode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2271 | ▶ | if ($opcode->has_on_compilation) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1758 | $opcode->on_compilation->($self, $compiled_op, \%context); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2073 | $context{position}++; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | catch ($ex) { | ||||||||||||||||||||||||||||
| 90 | 198 | ▶ | if (blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::ScriptCompilation')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::ScriptCompilation")
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")
| |||||||||||||||||||||||||||||
| 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 | 885 | ▶ | unless $context{offset} == $context{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | push @{$self->errors}, | |||||||||||||||||||||||||||
| 107 | Bitcoin::Crypto::Exception::ScriptCompilation->new(message => 'some OP_IFs were not closed') | ||||||||||||||||||||||||||||
| 108 | 885 | ▶ | if $context{branch}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 885 | $self->_set_opcode_count($non_push_opcodes); | |||||||||||||||||||||||||||
| 111 | 885 | return $self; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub _compile_data_push | ||||||||||||||||||||||||||||
| 115 | 715 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_compile_data_push: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 715 | my ($self, $context, $size) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 715 | ▶ | $self->_invalid_script( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 'no PUSHDATA size in the script' | ||||||||||||||||||||||||||||
| 120 | ) unless defined $size; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # we may go past the script end - that's okay | ||||||||||||||||||||||||||||
| 123 | 714 | $context->{offset} += $size; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 714 | return substr $context->{serialized}, $context->{offset} - $size, $size; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub _invalid_script | ||||||||||||||||||||||||||||
| 129 | 198 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_invalid_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 198 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 198 | Bitcoin::Crypto::Exception::ScriptCompilation->raise($error); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub _unconditionally_valid_script | ||||||||||||||||||||||||||||
| 136 | 174 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_unconditionally_valid_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | 174 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 174 | $self->_set_unconditionally_valid(!!1); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 64 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||