File Coverage

blib/lib/Bitcoin/Crypto/Block.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto::Block;
2             $Bitcoin::Crypto::Block::VERSION = '2.000_01'; # TRIAL
3             $Bitcoin::Crypto::Block::VERSION = '2.00001';
4 6     6   113 use v5.10;
  6         29  
5 6     6   45 use strict;
  6         19  
  6         140  
6 6     6   34 use warnings;
  6         23  
  6         233  
7              
8 6     6   596 use Moo;
  6         7608  
  6         48  
9 6     6   4417 use Mooish::AttributeBuilder -standard;
  6         1960  
  6         53  
10 6     6   1573 use Type::Params -sigs;
  6         124842  
  6         66  
11              
12 6     6   4411 use Bitcoin::Crypto::Types qw(Object PositiveInt PositiveOrZeroInt InstanceOf);
  6         17  
  6         62  
13              
14             has param 'timestamp' => (
15             isa => PositiveInt,
16             default => sub { scalar time },
17             );
18              
19             has param 'height' => (
20             isa => PositiveOrZeroInt,
21             );
22              
23             has option 'previous' => (
24             isa => InstanceOf ['Bitcoin::Crypto::Block'],
25             );
26              
27             signature_for median_time_past => (
28             method => Object,
29             positional => [],
30             );
31              
32             sub median_time_past
33             {
34             my ($self) = @_;
35              
36             my @stamps;
37              
38             my $current = $self;
39             for my $count (1 .. 11) {
40             push @stamps, $current->timestamp;
41              
42             # NOTE: since we do not expect full blockchain to be available, exit
43             # the loop early if we didn't get full 11 blocks required for MTP.
44             # Should this warn?
45             last unless $current->has_previous;
46             $current = $current->previous;
47             }
48              
49             @stamps = sort { $a <=> $b } @stamps;
50             return $stamps[int(@stamps / 2)];
51             }
52              
53             1;
54              
55             __END__
56             =head1 NAME
57              
58             Bitcoin::Crypto::Block - Stripped down block instance
59              
60             =head1 SYNOPSIS
61              
62             use Bitcoin::Crypto qw(btc_block);
63              
64             my $block = btc_block->new(
65             timestamp => 1697298600,
66             height => 812164,
67             );
68              
69             my $next_block = btc_block->new(
70             timestamp => 1697299200,
71             height => 812165,
72             previous => $block,
73             );
74              
75             print $next_block->median_time_past;
76              
77              
78             =head1 DESCRIPTION
79              
80             This is a block instance required for locktime and sequence checks in
81             transactions. It is used in L<Bitcoin::Crypto::Transaction/verify> and
82             L<Bitcoin::Crypto::Transaction::UTXO/block>.
83              
84             Bitcoin::Crypto does not contain any real blocks implementation. This class
85             provides the bare minimum required for checking locktime and sequence.
86              
87             =head1 INTERFACE
88              
89             =head2 Attributes
90              
91             =head3 height
92              
93             An integer height of the block. Required.
94              
95             I<Available in the constructor>.
96              
97             =head3 timestamp
98              
99             An integer timestamp of the block. Default - now.
100              
101             I<Available in the constructor>.
102              
103             =head3 previous
104              
105             An optional instance of the previous block.
106              
107             I<Available in the constructor>.
108              
109             =head2 Methods
110              
111             =head3 new
112              
113             $block = $class->new(%args)
114              
115             This is a standard Moo constructor, which can be used to create the object. It
116             takes arguments specified in L</Attributes>.
117              
118             Returns class instance.
119              
120             =head3 median_time_past
121              
122             $mtp = $object->median_time_past()
123              
124             This method returns the median time past described in BIP113 (median timestamp
125             of the past 11 blocks).
126              
127             Since this block implementation is as basic as it gets, it will happily
128             calculate median time past from less than 11 blocks, if there aren't enough
129             blocks chained via L</previous>.
130              
131             =head1 SEE ALSO
132              
133             =over
134              
135             =item L<Bitcoin::Crypto::Transaction>
136              
137             =item L<Bitcoin::Crypto::Transaction::UTXO>
138              
139             =back
140              
141             =cut
142