Crypto/Transaction/UTXO.pm

filter: Statementstmt 100% 60 / 60 Branchbran 100% 8 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 100% 91 / 91 CC 6 SCAR 6.2
CC
6
Cov
100%
get
4.0
register
3.0
BEGIN
1.0
CRAP 1.9
« Crypto/PSBT/FieldType.pm ↑ Summary Crypto/Network.pm »
1 package Bitcoin::Crypto::Transaction::UTXO;
2 23 $Bitcoin::Crypto::Transaction::UTXO::VERSION = '4.005';
3 25 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use Bitcoin::Crypto::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Bitcoin::Crypto::Transaction::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Bitcoin::Crypto::Util::Internal qw(to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 my %utxos;
16 my $loader;
17  
18 23 has param 'txid' => (
19 coerce => ByteStrLen [32],
20 );
21  
22 # NOTE: ideally, utxo should point to a transaction, and transaction should
23 # point to a block
24 23 has option 'block' => (
25 isa => InstanceOf ['Bitcoin::Crypto::Block'],
26 );
27  
28 23 has param 'output_index' => (
29 isa => IntMaxBits [32],
30 );
31  
32 23 has param 'output' => (
33 coerce => (InstanceOf ['Bitcoin::Crypto::Transaction::Output'])
34 ->plus_coercions(HashRef q{ Bitcoin::Crypto::Transaction::Output->new($_) }),
35 );
36  
37 sub register
38 94 ▶{
Subroutine register: called CC=3 SCAR=11.0
Pod documented
39 94 my ($self) = @_;
40  
41 # Do not store NULLDATA UTXOs
42 94 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if $self->output->is_standard and $self->output->locking_script->type eq "NULLDATA" 2 92 94
Condition100%3/3
$self->output->is_standard and $self->output->locking_script->type eq "NULLDATA"
!ll&&!rl&&r
21712
MC/DC100%2/2
$self->output->is_standard and $self->output->locking_script->type eq "NULLDATA"
$self->output->is_standard $self->output->locking_script->type eq "NULLDATA"
Truth table100%3/3
A and B
A= $self->output->is_standardB= $self->output->locking_script->type eq "NULLDATA"
ABresult
0X0
100
111
43 if $self->output->is_standard && $self->output->locking_script->type eq 'NULLDATA';
44  
45 92 $utxos{$self->txid . $self->output_index} = $self;
46 92 return $self;
47 }
48  
49 sub unregister
50 3 ▶{
Subroutine unregister: called CC=1 SCAR=0.0
Pod documented
51 3 my ($self) = @_;
52  
53 3 delete $utxos{$self->txid . $self->output_index};
54 3 return $self;
55 }
56  
57 23 signature_for get => (
58 method => !!1,
59 positional => [ByteStr, PositiveOrZeroInt],
60 );
61  
62 sub get
63 125 ▶{
Subroutine get: called CC=4 SCAR=13.9
64 125 my ($class, $txid, $outid) = @_;
65  
66 125 my $utxo = $utxos{$txid . $outid};
67  
68 115 ▶ if (!$utxo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($utxo) 55 58 113
69  
70 57 ▶ $utxo = $loader->($txid, $outid)
Branch: 1 branch
BranchTrueFalseTotal
if defined $loader 51 4 55
71 if defined $loader;
72  
73 57 ▶ Bitcoin::Crypto::Exception::UTXO->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $utxo 10 45 55
74 sprintf(
75 "no UTXO registered for transaction id %s and output index %s",
76 to_format [hex => $txid],
77 $outid
78 )
79 ) unless $utxo;
80  
81 47 $utxo->register;
82 }
83  
84 105 return $utxo;
85 }
86  
87 23 signature_for set_loader => (
88 method => !!1,
89 positional => [Maybe [CodeRef]],
90 );
91  
92 sub set_loader
93 {
94 2 $loader = pop;
95 2 return;
96 }
97  
98 sub unload
99 2 ▶{
Subroutine unload: called CC=1 SCAR=0.0
Pod documented
100 2 my @result = values %utxos;
101 2 %utxos = ();
102  
103 2 return \@result;
104 }
105  
106 sub registered_count
107 4 ▶{
Subroutine registered_count: called CC=1 SCAR=0.0
Pod documented
108 4 return scalar keys %utxos;
109 }
110  
111 23 signature_for extract => (
112 method => !!1,
113 positional => [ByteStr],
114 );
115  
116 sub extract
117 {
118 2 my ($class, $serialized_tx) = @_;
119  
120 2 my $tx = Bitcoin::Crypto::Transaction->from_serialized($serialized_tx);
121 2 $tx->update_utxos;
122  
123 2 return;
124 }
125  
126 23 1;
127  
128 __END__
« Crypto/PSBT/FieldType.pm ↑ Summary Crypto/Network.pm »