Skip to content

Commit ae972a5

Browse files
committed
Merge #9580: Fix various minor linearization script issues
b9d95bd Fix various minor linearization script issues (Douglas Roark)
2 parents e30d928 + b9d95bd commit ae972a5

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

contrib/linearize/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ Required configuration file settings:
3232
* `output`: Output directory for linearized `blocks/blkNNNNN.dat` output.
3333

3434
Optional config file setting for linearize-data:
35-
* `file_timestamp`: Set each file's last-modified time to that of the most
36-
recent block in that file.
35+
* `debug_output`: Some printouts may not always be desired. If true, such output
36+
will be printed.
37+
* `file_timestamp`: Set each file's last-accessed and last-modified times,
38+
respectively, to the current time and to the timestamp of the most recent block
39+
written to the script's blockchain.
3740
* `genesis`: The hash of the genesis block in the blockchain.
3841
* `input`: bitcoind blocks/ directory containing blkNNNNN.dat
3942
* `hashlist`: text file containing list of block hashes created by
4043
linearize-hashes.py.
4144
* `max_out_sz`: Maximum size for files created by the `output_file` option.
4245
(Default: `1000*1000*1000 bytes`)
4346
* `netmagic`: Network magic number.
47+
* `out_of_order_cache_sz`: If out-of-order blocks are being read, the block can
48+
be written to a cache so that the blockchain doesn't have to be seeked again.
49+
This option specifies the cache size. (Default: `100*1000*1000 bytes`)
4450
* `rev_hash_bytes`: If true, the block hash list written by linearize-hashes.py
4551
will be byte-reversed when read by linearize-data.py. See the linearize-hashes
4652
entry for more information.

contrib/linearize/example-linearize.cfg

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# bitcoind RPC settings (linearize-hashes)
32
rpcuser=someuser
43
rpcpassword=somepassword
@@ -21,6 +20,9 @@ input=/home/example/.bitcoin/blocks
2120
#genesis=000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943
2221
#input=/home/example/.bitcoin/testnet3/blocks
2322

23+
# "output" option causes blockchain files to be written to the given location,
24+
# with "output_file" ignored. If not used, "output_file" is used instead.
25+
# output=/home/example/blockchain_directory
2426
output_file=/home/example/Downloads/bootstrap.dat
2527
hashlist=hashlist.txt
2628

@@ -29,3 +31,12 @@ out_of_order_cache_sz = 100000000
2931

3032
# Do we want the reverse the hash bytes coming from getblockhash?
3133
rev_hash_bytes = False
34+
35+
# On a new month, do we want to set the access and modify times of the new
36+
# blockchain file?
37+
file_timestamp = 0
38+
# Do we want to split the blockchain files given a new month or specific height?
39+
split_timestamp = 0
40+
41+
# Do we want debug printouts?
42+
debug_output = False

contrib/linearize/linearize-data.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,32 @@ def writeBlock(self, inhdr, blk_hdr, rawblock):
134134
if not self.fileOutput and ((self.outsz + blockSizeOnDisk) > self.maxOutSz):
135135
self.outF.close()
136136
if self.setFileTime:
137-
os.utime(outFname, (int(time.time()), highTS))
137+
os.utime(self.outFname, (int(time.time()), self.highTS))
138138
self.outF = None
139139
self.outFname = None
140140
self.outFn = self.outFn + 1
141141
self.outsz = 0
142142

143143
(blkDate, blkTS) = get_blk_dt(blk_hdr)
144144
if self.timestampSplit and (blkDate > self.lastDate):
145-
print("New month " + blkDate.strftime("%Y-%m") + " @ " + hash_str)
146-
lastDate = blkDate
147-
if outF:
148-
outF.close()
149-
if setFileTime:
150-
os.utime(outFname, (int(time.time()), highTS))
145+
print("New month " + blkDate.strftime("%Y-%m") + " @ " + self.hash_str)
146+
self.lastDate = blkDate
147+
if self.outF:
148+
self.outF.close()
149+
if self.setFileTime:
150+
os.utime(self.outFname, (int(time.time()), self.highTS))
151151
self.outF = None
152152
self.outFname = None
153153
self.outFn = self.outFn + 1
154154
self.outsz = 0
155155

156156
if not self.outF:
157157
if self.fileOutput:
158-
outFname = self.settings['output_file']
158+
self.outFname = self.settings['output_file']
159159
else:
160-
outFname = os.path.join(self.settings['output'], "blk%05d.dat" % self.outFn)
161-
print("Output file " + outFname)
162-
self.outF = open(outFname, "wb")
160+
self.outFname = os.path.join(self.settings['output'], "blk%05d.dat" % self.outFn)
161+
print("Output file " + self.outFname)
162+
self.outF = open(self.outFname, "wb")
163163

164164
self.outF.write(inhdr)
165165
self.outF.write(blk_hdr)
@@ -223,13 +223,16 @@ def run(self):
223223
blk_hdr = self.inF.read(80)
224224
inExtent = BlockExtent(self.inFn, self.inF.tell(), inhdr, blk_hdr, inLen)
225225

226-
hash_str = calc_hash_str(blk_hdr)
227-
if not hash_str in blkmap:
228-
print("Skipping unknown block " + hash_str)
226+
self.hash_str = calc_hash_str(blk_hdr)
227+
if not self.hash_str in blkmap:
228+
# Because blocks can be written to files out-of-order as of 0.10, the script
229+
# may encounter blocks it doesn't know about. Treat as debug output.
230+
if settings['debug_output'] == 'true':
231+
print("Skipping unknown block " + self.hash_str)
229232
self.inF.seek(inLen, os.SEEK_CUR)
230233
continue
231234

232-
blkHeight = self.blkmap[hash_str]
235+
blkHeight = self.blkmap[self.hash_str]
233236
self.blkCountIn += 1
234237

235238
if self.blkCountOut == blkHeight:
@@ -295,12 +298,15 @@ def run(self):
295298
settings['max_out_sz'] = 1000 * 1000 * 1000
296299
if 'out_of_order_cache_sz' not in settings:
297300
settings['out_of_order_cache_sz'] = 100 * 1000 * 1000
301+
if 'debug_output' not in settings:
302+
settings['debug_output'] = 'false'
298303

299304
settings['max_out_sz'] = int(settings['max_out_sz'])
300305
settings['split_timestamp'] = int(settings['split_timestamp'])
301306
settings['file_timestamp'] = int(settings['file_timestamp'])
302307
settings['netmagic'] = unhexlify(settings['netmagic'].encode('utf-8'))
303308
settings['out_of_order_cache_sz'] = int(settings['out_of_order_cache_sz'])
309+
settings['debug_output'] = settings['debug_output'].lower()
304310

305311
if 'output_file' not in settings and 'output' not in settings:
306312
print("Missing output file / directory")

0 commit comments

Comments
 (0)