1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace AndKom \PhpBitcoinBlockchain ;
6+
7+ use AndKom \BCDataStream \Reader ;
8+
9+ /**
10+ * Class BlockInfo
11+ * @package AndKom\PhpBitcoinBlockchain
12+ */
13+ class BlockInfo
14+ {
15+ const BLOCK_VALID_UNKNOWN = 0 ;
16+ const BLOCK_VALID_HEADER = 1 ;
17+ const BLOCK_VALID_TREE = 2 ;
18+ const BLOCK_VALID_TRANSACTIONS = 3 ;
19+ const BLOCK_VALID_CHAIN = 4 ;
20+ const BLOCK_VALID_SCRIPTS = 5 ;
21+ const BLOCK_VALID_MASK = self ::BLOCK_VALID_HEADER | self ::BLOCK_VALID_TREE | self ::BLOCK_VALID_TRANSACTIONS | self ::BLOCK_VALID_CHAIN | self :: BLOCK_VALID_SCRIPTS ;
22+
23+ const BLOCK_HAVE_DATA = 8 ;
24+ const BLOCK_HAVE_UNDO = 16 ;
25+ const BLOCK_HAVE_MASK = self ::BLOCK_HAVE_DATA | self ::BLOCK_HAVE_UNDO ;
26+
27+ const BLOCK_FAILED_VALID = 32 ;
28+ const BLOCK_FAILED_CHILD = 64 ;
29+ const BLOCK_FAILED_MASK = self ::BLOCK_FAILED_VALID | self ::BLOCK_FAILED_CHILD ;
30+
31+ const BLOCK_OPT_WITNESS = 128 ;
32+
33+ /**
34+ * @var int
35+ */
36+ public $ version ;
37+
38+ /**
39+ * @var int
40+ */
41+ public $ height ;
42+
43+ /**
44+ * @var int
45+ */
46+ public $ status ;
47+
48+ /**
49+ * @var int
50+ */
51+ public $ txCount ;
52+
53+ /**
54+ * @var int
55+ */
56+ public $ file ;
57+
58+ /**
59+ * @var int
60+ */
61+ public $ dataPos ;
62+
63+ /**
64+ * @var int
65+ */
66+ public $ undoPos ;
67+
68+ /**
69+ * @var Header
70+ */
71+ public $ header ;
72+
73+ /**
74+ * @param Reader $reader
75+ * @return BlockInfo
76+ */
77+ static public function parse (Reader $ reader ): self
78+ {
79+ $ block = new self ;
80+ $ block ->version = $ reader ->readVarInt ();
81+ $ block ->height = $ reader ->readVarInt ();
82+ $ block ->status = $ reader ->readVarInt ();
83+ $ block ->txCount = $ reader ->readVarInt ();
84+
85+ if ($ block ->status & (static ::BLOCK_HAVE_DATA | static ::BLOCK_HAVE_UNDO )) {
86+ $ block ->file = $ reader ->readVarInt ();
87+ }
88+
89+ if ($ block ->status & static ::BLOCK_HAVE_DATA ) {
90+ $ block ->dataPos = $ reader ->readVarInt ();
91+ }
92+
93+ if ($ block ->status & static ::BLOCK_HAVE_UNDO ) {
94+ $ block ->undoPos = $ reader ->readVarInt ();
95+ }
96+
97+ $ block ->header = Header::parse ($ reader );
98+
99+ return $ block ;
100+ }
101+
102+ /**
103+ * @return string
104+ * @throws Exception
105+ */
106+ public function getFileName (): string
107+ {
108+ if (is_null ($ this ->file )) {
109+ throw new Exception ('Unknown block file number. ' );
110+ }
111+
112+ return sprintf ('blk%05d.dat ' , $ this ->file );
113+ }
114+ }
0 commit comments