-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.lisp
212 lines (181 loc) · 8.41 KB
/
file.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#|
Copyright 2014-2015 Guillaume LE VAILLANT
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
If not, see <http://www.gnu.org/licenses/>.
|#
(in-package :peercoin-blockchain-parser)
;;; Basic data types
(defun file-read-bytes (stream bytes)
"Read a number of BYTES from a STREAM."
(let ((data (make-array bytes :element-type '(unsigned-byte 8)))
n)
(setf n (read-sequence data stream))
(unless (= n bytes)
(error "Not enough data"))
data))
(defun file-read-unsigned-integer (stream bytes)
"Read an integer of several BYTES (little-endian) from a STREAM."
(loop
:with value = 0
:for low-bit :from 0 :to (* 8 (1- bytes)) :by 8
:do (setf (ldb (byte 8 low-bit) value) (read-byte stream))
:finally (return value)))
(defun file-read-variable-length-unsigned-integer (stream)
"Read a variable length integer (little-endian) from a STREAM."
(let ((d (read-byte stream)))
(cond ((< d 253) d)
((= d 253) (file-read-unsigned-integer stream 2))
((= d 254) (file-read-unsigned-integer stream 4))
((= d 255) (file-read-unsigned-integer stream 8)))))
;;; Hash
(defun file-compute-data-hash (stream offset length)
"Compute the double sha256 hash of LENGTH bytes of data from STREAM after skipping OFFSET bytes from STREAM."
(let (data)
(file-position stream (+ (file-position stream) offset))
(setf data (file-read-bytes stream length))
(sha256d data)))
;;; Objects
(defgeneric file-read-object (object stream)
(:documentation "Read an OBJECT from a STREAM."))
(defmethod file-read-object ((object input) stream)
(with-slots (transaction-hash transaction-index script-length script sequence-number) object
(setf transaction-hash (file-read-bytes stream 32)
transaction-index (file-read-unsigned-integer stream 4)
script-length (file-read-variable-length-unsigned-integer stream)
script (file-read-bytes stream script-length)
sequence-number (file-read-unsigned-integer stream 4))))
(defmethod file-read-object ((object output) stream)
(with-slots (value script-length script) object
(setf value (file-read-unsigned-integer stream 8)
script-length (file-read-variable-length-unsigned-integer stream)
script (file-read-bytes stream script-length))))
(defmethod file-read-object ((object transaction) stream)
(with-slots (version timestamp input-count inputs output-count outputs lock-time) object
(setf version (file-read-unsigned-integer stream 4)
timestamp (file-read-unsigned-integer stream 4))
;; Read inputs
(setf input-count (file-read-variable-length-unsigned-integer stream)
inputs (make-array input-count))
(dotimes (i input-count)
(setf (aref inputs i) (make-instance 'input))
(file-read-object (aref inputs i) stream))
;; Read outputs
(setf output-count (file-read-variable-length-unsigned-integer stream)
outputs (make-array output-count))
(dotimes (i output-count)
(setf (aref outputs i) (make-instance 'output))
(file-read-object (aref outputs i) stream)
(setf (index (aref outputs i)) i))
(setf lock-time (file-read-unsigned-integer stream 4))))
(defmethod file-read-object ((object blk) stream)
(with-slots (header-length version previous-hash merkle-root timestamp bits nonce transaction-count transactions signature-length signature)
object
(setf header-length (file-read-unsigned-integer stream 4)
version (file-read-unsigned-integer stream 4)
previous-hash (file-read-bytes stream 32)
merkle-root (file-read-bytes stream 32)
timestamp (file-read-unsigned-integer stream 4)
bits (file-read-unsigned-integer stream 4)
nonce (file-read-unsigned-integer stream 4))
;; Read transactions
(setf transaction-count (file-read-variable-length-unsigned-integer stream)
transactions (make-array transaction-count))
(let (start-position end-position transaction-hash)
(dotimes (i transaction-count)
(setf (aref transactions i) (make-instance 'transaction))
(setf start-position (file-position stream))
(file-read-object (aref transactions i) stream)
(setf end-position (file-position stream))
(file-position stream start-position)
(setf transaction-hash (file-compute-data-hash stream 0 (- end-position start-position)))
(setf (hash (aref transactions i)) transaction-hash)
(file-position stream end-position)))
(setf signature-length (file-read-variable-length-unsigned-integer stream)
signature (file-read-bytes stream signature-length))))
;;; Parser
(defconstant +peercoin-magic-id+ #xe5e9e8e6)
(defconstant +peercoin-testnet-magic-id+ #xefc0f2cb)
(defun find-magic-id (stream &optional backwards)
"Find the magic ID."
(do ((position (file-position stream)))
((or (minusp position) (> position (file-length stream))))
(file-position stream position)
(if (= (file-read-unsigned-integer stream 4) (if *testnet* +peercoin-testnet-magic-id+ +peercoin-magic-id+))
(return position)
(if backwards
(decf position)
(incf position)))))
(defun file-get-block (h &optional raw)
"Get a block in the blockchain given its hash."
(with-open-file (stream *file-blockchain*
:direction :input
:element-type '(unsigned-byte 8))
(do ((position (find-magic-id stream)
(find-magic-id stream))
(hbin (reverse (hex-to-bin h)))
blk
blk-hash)
((null position))
(incf position 4) ; After magic ID
(file-position stream position)
(setf blk-hash (file-compute-data-hash stream 4 80))
(when (equalp blk-hash hbin)
(setf blk (make-instance 'blk))
(file-position stream position)
(handler-case (file-read-object blk stream)
(t (e) (return e)))
(setf (hash blk) blk-hash)
(if raw
(let ((position-end (file-position stream))
data)
(file-position stream (- position 4)) ; Before magic ID
(setf data (file-read-bytes stream (- position-end (- position 4))))
(return data))
(return blk))))))
(defun file-parse-blockchain (start-callback block-callback end-callback &optional backwards)
"Call START-CALLBACK, parse the blockchain and pass every block to the BLOCK-CALLBACK function, and pass a list containing the hashes of the blocks to END-CALLBACK."
(with-open-file (stream *file-blockchain*
:direction :input
:element-type '(unsigned-byte 8))
(let (last-block block-hashes)
(when start-callback
(funcall start-callback))
(when backwards
(file-position stream (- (file-length stream) 4)))
(do ((position (find-magic-id stream backwards)
(find-magic-id stream backwards))
blk
blk-hash)
((null position))
(incf position 4) ; After magic ID
(file-position stream position)
(setf blk-hash (file-compute-data-hash stream 4 80))
(when (or (null last-block)
(equalp (previous-hash last-block) blk-hash))
(push blk-hash block-hashes)
(setf blk (make-instance 'blk))
(file-position stream position)
(handler-case (file-read-object blk stream)
(t (e) (return e)))
(setf (hash blk) blk-hash)
(when block-callback
(funcall block-callback blk))
(setf last-block blk))
(if backwards
(progn
(when (minusp (- position 5))
(return))
(file-position stream (- position 5))) ; Before magic ID
(when (> (+ (file-position stream) 84) (file-length stream))
(return))))
(when end-callback
(funcall end-callback block-hashes)))))