2014-08-23 18:46:12 -07:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# linearize-data.py: Construct a linear, no-fork version of the chain.
|
|
|
|
#
|
2014-12-16 17:47:57 -08:00
|
|
|
# Copyright (c) 2013-2014 The Bitcoin Core developers
|
2014-12-12 20:09:33 -08:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-08-23 18:46:12 -07:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#
|
|
|
|
|
2014-10-06 08:55:55 -07:00
|
|
|
from __future__ import print_function, division
|
2014-08-23 18:46:12 -07:00
|
|
|
import json
|
|
|
|
import struct
|
|
|
|
import re
|
2014-08-24 08:37:14 -07:00
|
|
|
import os
|
2014-08-23 18:46:12 -07:00
|
|
|
import base64
|
|
|
|
import httplib
|
|
|
|
import sys
|
|
|
|
import hashlib
|
2014-08-23 19:59:16 -07:00
|
|
|
import datetime
|
2014-08-24 08:37:14 -07:00
|
|
|
import time
|
2014-10-06 08:55:55 -07:00
|
|
|
from collections import namedtuple
|
2014-08-23 18:46:12 -07:00
|
|
|
|
|
|
|
settings = {}
|
|
|
|
|
|
|
|
def uint32(x):
|
|
|
|
return x & 0xffffffffL
|
|
|
|
|
|
|
|
def bytereverse(x):
|
|
|
|
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) |
|
|
|
|
(((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
|
|
|
|
|
|
|
|
def bufreverse(in_buf):
|
|
|
|
out_words = []
|
|
|
|
for i in range(0, len(in_buf), 4):
|
|
|
|
word = struct.unpack('@I', in_buf[i:i+4])[0]
|
|
|
|
out_words.append(struct.pack('@I', bytereverse(word)))
|
|
|
|
return ''.join(out_words)
|
|
|
|
|
|
|
|
def wordreverse(in_buf):
|
|
|
|
out_words = []
|
|
|
|
for i in range(0, len(in_buf), 4):
|
|
|
|
out_words.append(in_buf[i:i+4])
|
|
|
|
out_words.reverse()
|
|
|
|
return ''.join(out_words)
|
|
|
|
|
2014-08-23 19:59:16 -07:00
|
|
|
def calc_hdr_hash(blk_hdr):
|
2014-08-23 18:46:12 -07:00
|
|
|
hash1 = hashlib.sha256()
|
|
|
|
hash1.update(blk_hdr)
|
|
|
|
hash1_o = hash1.digest()
|
|
|
|
|
|
|
|
hash2 = hashlib.sha256()
|
|
|
|
hash2.update(hash1_o)
|
|
|
|
hash2_o = hash2.digest()
|
|
|
|
|
|
|
|
return hash2_o
|
|
|
|
|
2014-08-23 19:59:16 -07:00
|
|
|
def calc_hash_str(blk_hdr):
|
|
|
|
hash = calc_hdr_hash(blk_hdr)
|
2014-08-23 18:46:12 -07:00
|
|
|
hash = bufreverse(hash)
|
|
|
|
hash = wordreverse(hash)
|
|
|
|
hash_str = hash.encode('hex')
|
|
|
|
return hash_str
|
|
|
|
|
2014-08-23 22:40:40 -07:00
|
|
|
def get_blk_dt(blk_hdr):
|
2014-08-23 19:59:16 -07:00
|
|
|
members = struct.unpack("<I", blk_hdr[68:68+4])
|
2014-08-24 08:37:14 -07:00
|
|
|
nTime = members[0]
|
|
|
|
dt = datetime.datetime.fromtimestamp(nTime)
|
2014-08-23 22:40:40 -07:00
|
|
|
dt_ym = datetime.datetime(dt.year, dt.month, 1)
|
2014-08-24 08:37:14 -07:00
|
|
|
return (dt_ym, nTime)
|
2014-08-23 19:59:16 -07:00
|
|
|
|
2014-08-23 18:46:12 -07:00
|
|
|
def get_block_hashes(settings):
|
|
|
|
blkindex = []
|
|
|
|
f = open(settings['hashlist'], "r")
|
|
|
|
for line in f:
|
|
|
|
line = line.rstrip()
|
|
|
|
blkindex.append(line)
|
|
|
|
|
|
|
|
print("Read " + str(len(blkindex)) + " hashes")
|
|
|
|
|
|
|
|
return blkindex
|
|
|
|
|
2014-10-06 08:55:55 -07:00
|
|
|
def mkblockmap(blkindex):
|
2014-08-23 18:46:12 -07:00
|
|
|
blkmap = {}
|
2014-10-06 08:55:55 -07:00
|
|
|
for height,hash in enumerate(blkindex):
|
|
|
|
blkmap[hash] = height
|
2014-08-23 18:46:12 -07:00
|
|
|
return blkmap
|
|
|
|
|
2014-10-06 08:55:55 -07:00
|
|
|
# Block header and extent on disk
|
|
|
|
BlockExtent = namedtuple('BlockExtent', ['fn', 'offset', 'inhdr', 'blkhdr', 'size'])
|
|
|
|
|
|
|
|
class BlockDataCopier:
|
|
|
|
def __init__(self, settings, blkindex, blkmap):
|
|
|
|
self.settings = settings
|
|
|
|
self.blkindex = blkindex
|
|
|
|
self.blkmap = blkmap
|
|
|
|
|
|
|
|
self.inFn = 0
|
|
|
|
self.inF = None
|
|
|
|
self.outFn = 0
|
|
|
|
self.outsz = 0
|
|
|
|
self.outF = None
|
|
|
|
self.outFname = None
|
|
|
|
self.blkCountIn = 0
|
|
|
|
self.blkCountOut = 0
|
|
|
|
|
|
|
|
self.lastDate = datetime.datetime(2000, 1, 1)
|
|
|
|
self.highTS = 1408893517 - 315360000
|
|
|
|
self.timestampSplit = False
|
|
|
|
self.fileOutput = True
|
|
|
|
self.setFileTime = False
|
|
|
|
self.maxOutSz = settings['max_out_sz']
|
|
|
|
if 'output' in settings:
|
|
|
|
self.fileOutput = False
|
|
|
|
if settings['file_timestamp'] != 0:
|
|
|
|
self.setFileTime = True
|
|
|
|
if settings['split_timestamp'] != 0:
|
|
|
|
self.timestampSplit = True
|
|
|
|
# Extents and cache for out-of-order blocks
|
|
|
|
self.blockExtents = {}
|
|
|
|
self.outOfOrderData = {}
|
|
|
|
self.outOfOrderSize = 0 # running total size for items in outOfOrderData
|
|
|
|
|
|
|
|
def writeBlock(self, inhdr, blk_hdr, rawblock):
|
|
|
|
if not self.fileOutput and ((self.outsz + self.inLen) > self.maxOutSz):
|
|
|
|
self.outF.close()
|
|
|
|
if self.setFileTime:
|
2014-08-24 08:37:14 -07:00
|
|
|
os.utime(outFname, (int(time.time()), highTS))
|
2014-10-06 08:55:55 -07:00
|
|
|
self.outF = None
|
|
|
|
self.outFname = None
|
|
|
|
self.outFn = outFn + 1
|
|
|
|
self.outsz = 0
|
2014-08-23 19:59:16 -07:00
|
|
|
|
2014-08-24 08:37:14 -07:00
|
|
|
(blkDate, blkTS) = get_blk_dt(blk_hdr)
|
2014-10-06 08:55:55 -07:00
|
|
|
if self.timestampSplit and (blkDate > self.lastDate):
|
2014-08-24 08:37:14 -07:00
|
|
|
print("New month " + blkDate.strftime("%Y-%m") + " @ " + hash_str)
|
|
|
|
lastDate = blkDate
|
|
|
|
if outF:
|
|
|
|
outF.close()
|
|
|
|
if setFileTime:
|
|
|
|
os.utime(outFname, (int(time.time()), highTS))
|
2014-10-06 08:55:55 -07:00
|
|
|
self.outF = None
|
|
|
|
self.outFname = None
|
|
|
|
self.outFn = self.outFn + 1
|
|
|
|
self.outsz = 0
|
|
|
|
|
|
|
|
if not self.outF:
|
|
|
|
if self.fileOutput:
|
|
|
|
outFname = self.settings['output_file']
|
2014-08-23 18:46:12 -07:00
|
|
|
else:
|
2014-10-06 08:55:55 -07:00
|
|
|
outFname = "%s/blk%05d.dat" % (self.settings['output'], outFn)
|
2014-12-07 07:55:53 -08:00
|
|
|
print("Output file " + outFname)
|
2014-10-06 08:55:55 -07:00
|
|
|
self.outF = open(outFname, "wb")
|
|
|
|
|
|
|
|
self.outF.write(inhdr)
|
|
|
|
self.outF.write(blk_hdr)
|
|
|
|
self.outF.write(rawblock)
|
|
|
|
self.outsz = self.outsz + len(inhdr) + len(blk_hdr) + len(rawblock)
|
|
|
|
|
|
|
|
self.blkCountOut = self.blkCountOut + 1
|
|
|
|
if blkTS > self.highTS:
|
|
|
|
self.highTS = blkTS
|
|
|
|
|
|
|
|
if (self.blkCountOut % 1000) == 0:
|
|
|
|
print('%i blocks scanned, %i blocks written (of %i, %.1f%% complete)' %
|
|
|
|
(self.blkCountIn, self.blkCountOut, len(self.blkindex), 100.0 * self.blkCountOut / len(self.blkindex)))
|
|
|
|
|
|
|
|
def inFileName(self, fn):
|
|
|
|
return "%s/blk%05d.dat" % (self.settings['input'], fn)
|
|
|
|
|
|
|
|
def fetchBlock(self, extent):
|
|
|
|
'''Fetch block contents from disk given extents'''
|
|
|
|
with open(self.inFileName(extent.fn), "rb") as f:
|
|
|
|
f.seek(extent.offset)
|
|
|
|
return f.read(extent.size)
|
|
|
|
|
|
|
|
def copyOneBlock(self):
|
|
|
|
'''Find the next block to be written in the input, and copy it to the output.'''
|
|
|
|
extent = self.blockExtents.pop(self.blkCountOut)
|
|
|
|
if self.blkCountOut in self.outOfOrderData:
|
|
|
|
# If the data is cached, use it from memory and remove from the cache
|
|
|
|
rawblock = self.outOfOrderData.pop(self.blkCountOut)
|
|
|
|
self.outOfOrderSize -= len(rawblock)
|
|
|
|
else: # Otherwise look up data on disk
|
|
|
|
rawblock = self.fetchBlock(extent)
|
|
|
|
|
|
|
|
self.writeBlock(extent.inhdr, extent.blkhdr, rawblock)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while self.blkCountOut < len(self.blkindex):
|
|
|
|
if not self.inF:
|
|
|
|
fname = self.inFileName(self.inFn)
|
2014-12-07 07:55:53 -08:00
|
|
|
print("Input file " + fname)
|
2014-10-06 08:55:55 -07:00
|
|
|
try:
|
|
|
|
self.inF = open(fname, "rb")
|
|
|
|
except IOError:
|
|
|
|
print("Premature end of block data")
|
|
|
|
return
|
|
|
|
|
|
|
|
inhdr = self.inF.read(8)
|
|
|
|
if (not inhdr or (inhdr[0] == "\0")):
|
|
|
|
self.inF.close()
|
|
|
|
self.inF = None
|
|
|
|
self.inFn = self.inFn + 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
inMagic = inhdr[:4]
|
|
|
|
if (inMagic != self.settings['netmagic']):
|
2015-05-20 15:27:51 -07:00
|
|
|
print("Invalid magic: " + inMagic.encode('hex'))
|
2014-10-06 08:55:55 -07:00
|
|
|
return
|
|
|
|
inLenLE = inhdr[4:]
|
|
|
|
su = struct.unpack("<I", inLenLE)
|
|
|
|
inLen = su[0] - 80 # length without header
|
|
|
|
blk_hdr = self.inF.read(80)
|
|
|
|
inExtent = BlockExtent(self.inFn, self.inF.tell(), inhdr, blk_hdr, inLen)
|
|
|
|
|
|
|
|
hash_str = calc_hash_str(blk_hdr)
|
|
|
|
if not hash_str in blkmap:
|
|
|
|
print("Skipping unknown block " + hash_str)
|
|
|
|
self.inF.seek(inLen, os.SEEK_CUR)
|
|
|
|
continue
|
|
|
|
|
|
|
|
blkHeight = self.blkmap[hash_str]
|
|
|
|
self.blkCountIn += 1
|
|
|
|
|
|
|
|
if self.blkCountOut == blkHeight:
|
|
|
|
# If in-order block, just copy
|
|
|
|
rawblock = self.inF.read(inLen)
|
|
|
|
self.writeBlock(inhdr, blk_hdr, rawblock)
|
|
|
|
|
|
|
|
# See if we can catch up to prior out-of-order blocks
|
|
|
|
while self.blkCountOut in self.blockExtents:
|
|
|
|
self.copyOneBlock()
|
|
|
|
|
|
|
|
else: # If out-of-order, skip over block data for now
|
|
|
|
self.blockExtents[blkHeight] = inExtent
|
|
|
|
if self.outOfOrderSize < self.settings['out_of_order_cache_sz']:
|
|
|
|
# If there is space in the cache, read the data
|
|
|
|
# Reading the data in file sequence instead of seeking and fetching it later is preferred,
|
|
|
|
# but we don't want to fill up memory
|
|
|
|
self.outOfOrderData[blkHeight] = self.inF.read(inLen)
|
|
|
|
self.outOfOrderSize += inLen
|
|
|
|
else: # If no space in cache, seek forward
|
|
|
|
self.inF.seek(inLen, os.SEEK_CUR)
|
|
|
|
|
|
|
|
print("Done (%i blocks written)" % (self.blkCountOut))
|
2014-08-23 18:46:12 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) != 2:
|
2014-10-06 08:55:55 -07:00
|
|
|
print("Usage: linearize-data.py CONFIG-FILE")
|
2014-08-23 18:46:12 -07:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
f = open(sys.argv[1])
|
|
|
|
for line in f:
|
|
|
|
# skip comment lines
|
|
|
|
m = re.search('^\s*#', line)
|
|
|
|
if m:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# parse key=value lines
|
|
|
|
m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
|
|
|
|
if m is None:
|
|
|
|
continue
|
|
|
|
settings[m.group(1)] = m.group(2)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if 'netmagic' not in settings:
|
|
|
|
settings['netmagic'] = 'f9beb4d9'
|
2015-05-20 15:27:51 -07:00
|
|
|
if 'genesis' not in settings:
|
|
|
|
settings['genesis'] = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
|
2014-08-23 18:46:12 -07:00
|
|
|
if 'input' not in settings:
|
|
|
|
settings['input'] = 'input'
|
|
|
|
if 'hashlist' not in settings:
|
|
|
|
settings['hashlist'] = 'hashlist.txt'
|
2014-08-24 08:37:14 -07:00
|
|
|
if 'file_timestamp' not in settings:
|
|
|
|
settings['file_timestamp'] = 0
|
2014-08-23 22:40:40 -07:00
|
|
|
if 'split_timestamp' not in settings:
|
|
|
|
settings['split_timestamp'] = 0
|
2014-08-23 19:59:16 -07:00
|
|
|
if 'max_out_sz' not in settings:
|
|
|
|
settings['max_out_sz'] = 1000L * 1000 * 1000
|
2014-10-06 08:55:55 -07:00
|
|
|
if 'out_of_order_cache_sz' not in settings:
|
|
|
|
settings['out_of_order_cache_sz'] = 100 * 1000 * 1000
|
2014-08-23 18:46:12 -07:00
|
|
|
|
2014-08-23 19:59:16 -07:00
|
|
|
settings['max_out_sz'] = long(settings['max_out_sz'])
|
2014-08-23 22:40:40 -07:00
|
|
|
settings['split_timestamp'] = int(settings['split_timestamp'])
|
2014-08-24 08:37:14 -07:00
|
|
|
settings['file_timestamp'] = int(settings['file_timestamp'])
|
2014-08-23 18:46:12 -07:00
|
|
|
settings['netmagic'] = settings['netmagic'].decode('hex')
|
2014-10-06 08:55:55 -07:00
|
|
|
settings['out_of_order_cache_sz'] = int(settings['out_of_order_cache_sz'])
|
2014-08-23 18:46:12 -07:00
|
|
|
|
|
|
|
if 'output_file' not in settings and 'output' not in settings:
|
|
|
|
print("Missing output file / directory")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
blkindex = get_block_hashes(settings)
|
2014-10-06 08:55:55 -07:00
|
|
|
blkmap = mkblockmap(blkindex)
|
2014-08-23 18:46:12 -07:00
|
|
|
|
2015-05-20 15:27:51 -07:00
|
|
|
if not settings['genesis'] in blkmap:
|
2014-12-08 03:55:12 -08:00
|
|
|
print("Genesis block not found in hashlist")
|
2014-08-23 18:46:12 -07:00
|
|
|
else:
|
2014-10-06 08:55:55 -07:00
|
|
|
BlockDataCopier(settings, blkindex, blkmap).run()
|
2014-08-23 18:46:12 -07:00
|
|
|
|
|
|
|
|