| 1 | package Bitcoin::Crypto::Script::Compiler; | ||||||||||||||||||||||||||||
| 2 | 68 | $Bitcoin::Crypto::Script::Compiler::VERSION = '4.006'; | |||||||||||||||||||||||||||
| 3 | 68 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 68 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 68 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 68 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 68 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 68 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 68 | ▶ | use Bitcoin::Crypto::Helpers qw(die_no_trace); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 68 | ▶ | use Bitcoin::Crypto::Script::Compiler::Opcode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 68 | has param 'operations' => ( | |||||||||||||||||||||||||||
| 17 | isa => ArrayRef [ArrayRef], | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 68 | has field 'unconditionally_valid' => ( | |||||||||||||||||||||||||||
| 21 | isa => Bool, | ||||||||||||||||||||||||||||
| 22 | writer => -hidden, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | has field 'errors' => ( | ||||||||||||||||||||||||||||
| 26 | 1056 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 68 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 68 | has field 'opcode_count' => ( | |||||||||||||||||||||||||||
| 30 | isa => PositiveOrZeroInt, | ||||||||||||||||||||||||||||
| 31 | writer => -hidden, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub has_errors | ||||||||||||||||||||||||||||
| 35 | 1096 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
has_errors: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 1096 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1096 | return @{$self->errors} > 0; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub assert_valid | ||||||||||||||||||||||||||||
| 42 | 697 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
assert_valid: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 697 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 697 | ▶ | return if $self->unconditionally_valid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 610 | ▶ | 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 | 1056 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 1056 | my ($class, $script) = @_; | |||||||||||||||||||||||||||
| 55 | 1056 | my @ops; | |||||||||||||||||||||||||||
| 56 | 1056 | my $non_push_opcodes = 0; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1056 | my $self = $class->new( | |||||||||||||||||||||||||||
| 59 | operations => \@ops | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1056 | my $raw_script = $script->to_serialized; | |||||||||||||||||||||||||||
| 63 | 1056 | my %context = ( | |||||||||||||||||||||||||||
| 64 | serialized => $raw_script, | ||||||||||||||||||||||||||||
| 65 | position => 0, | ||||||||||||||||||||||||||||
| 66 | offset => 0, | ||||||||||||||||||||||||||||
| 67 | size => length $raw_script, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # set cache for opcode instances to avoid costly method call on every | ||||||||||||||||||||||||||||
| 71 | # encountered opcode | ||||||||||||||||||||||||||||
| 72 | 1056 | state $opcode_cache = {}; | |||||||||||||||||||||||||||
| 73 | 1056 | my $opcode_class = $script->opcode_class; | |||||||||||||||||||||||||||
| 74 | 1056 | ▶ | my $this_cache = $opcode_cache->{$opcode_class} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opcode_cache->{$opcode_class} //= {}
MC/DC100%1/1
$opcode_cache->{$opcode_class} //= {}
$opcode_cache->{$opcode_class}
Truth table100%2/2
A
A= $opcode_cache->{$opcode_class}
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1056 | ▶ | while ($context{offset} < $context{size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 2813 | try { | |||||||||||||||||||||||||||
| 78 | 2813 | my $this_byte = substr $context{serialized}, $context{offset}++, 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2813 | ▶ | my $opcode = $this_cache->{$this_byte} //= $opcode_class->get_opcode_by_code(ord $this_byte); | ||||||||||||||||||||||||||
|
Condition66%2/3
$this_cache->{$this_byte} //= $opcode_class->get_opcode_by_code(ord $this_byte)
MC/DC0%0/2
$this_cache->{$this_byte} //= $opcode_class->get_opcode_by_code(ord $this_byte)
$this_cache->{$this_byte}
$opcode_class->get_opcode_by_code(ord $this_byte)
Truth table66%2/3
A //= B
A= $this_cache->{$this_byte}B= $opcode_class->get_opcode_by_code(ord $this_byte)
| |||||||||||||||||||||||||||||
| 81 | 2813 | my $compiled_op = Bitcoin::Crypto::Script::Compiler::Opcode->new($opcode, $this_byte); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2813 | push @ops, $compiled_op; | |||||||||||||||||||||||||||
| 84 | 2813 | $non_push_opcodes += $opcode->non_push_opcode; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2813 | ▶ | if (my $on_compilation = $opcode->on_compilation) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2124 | $on_compilation->($self, $compiled_op, \%context); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2615 | $context{position}++; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | catch ($ex) { | ||||||||||||||||||||||||||||
| 93 | 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")
| |||||||||||||||||||||||||||||
| 94 | 198 | $ex->set_script([map { $_->opcode->name } @ops]); | |||||||||||||||||||||||||||
| 95 | 198 | $ex->set_error_position($context{position}); | |||||||||||||||||||||||||||
| 96 | 198 | push @{$self->errors}, $ex; | |||||||||||||||||||||||||||
| 97 | 198 | $context{position}++; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 0 | die $ex; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | push @{$self->errors}, | |||||||||||||||||||||||||||
| 106 | Bitcoin::Crypto::Exception::ScriptCompilation->new(message => 'not enough bytes of data in the script') | ||||||||||||||||||||||||||||
| 107 | 1056 | ▶ | unless $context{offset} == $context{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | push @{$self->errors}, | |||||||||||||||||||||||||||
| 110 | Bitcoin::Crypto::Exception::ScriptCompilation->new(message => 'some OP_IFs were not closed') | ||||||||||||||||||||||||||||
| 111 | 1056 | ▶ | if $context{branch}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1056 | $self->_set_opcode_count($non_push_opcodes); | |||||||||||||||||||||||||||
| 114 | 1056 | return $self; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _compile_data_push | ||||||||||||||||||||||||||||
| 118 | 944 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_compile_data_push: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | 944 | my ($self, $context, $size) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 944 | ▶ | $self->_invalid_script( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 'no PUSHDATA size in the script' | ||||||||||||||||||||||||||||
| 123 | ) unless defined $size; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # we may go past the script end - that's okay | ||||||||||||||||||||||||||||
| 126 | 943 | $context->{offset} += $size; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 943 | return substr $context->{serialized}, $context->{offset} - $size, $size; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub _invalid_script | ||||||||||||||||||||||||||||
| 132 | 198 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_invalid_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 198 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 198 | Bitcoin::Crypto::Exception::ScriptCompilation->raise($error); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub _unconditionally_valid_script | ||||||||||||||||||||||||||||
| 139 | 174 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_unconditionally_valid_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 174 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 174 | $self->_set_unconditionally_valid(!!1); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 68 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||