2013-09-01 09:16:15 -07:00
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2012 thomasv@ecdsa.org
|
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation files
|
|
|
|
# (the "Software"), to deal in the Software without restriction,
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
2013-09-01 09:16:15 -07:00
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
2013-09-01 09:16:15 -07:00
|
|
|
#
|
2016-02-23 02:36:42 -08:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2015-05-17 06:54:20 -07:00
|
|
|
import os
|
2017-07-24 06:54:42 -07:00
|
|
|
import threading
|
2018-01-12 22:55:50 -08:00
|
|
|
import struct
|
|
|
|
from io import BytesIO
|
2017-07-24 06:54:42 -07:00
|
|
|
|
2017-01-22 10:25:24 -08:00
|
|
|
from . import util
|
|
|
|
from . import bitcoin
|
|
|
|
from .bitcoin import *
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2018-01-12 22:55:50 -08:00
|
|
|
from .equihash import is_gbp_valid
|
|
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2015-12-11 03:37:40 -08:00
|
|
|
MAX_TARGET = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2017-05-29 00:03:39 -07:00
|
|
|
def serialize_header(res):
|
2018-01-12 22:55:50 -08:00
|
|
|
r = b''
|
|
|
|
r += struct.pack("<i", res.get('version'))
|
2018-01-14 09:04:59 -08:00
|
|
|
r += str_to_hash(res.get('prev_block_hash'))
|
|
|
|
r += str_to_hash(res.get('merkle_root'))
|
|
|
|
r += str_to_hash(res.get('hash_reserved'))
|
2018-01-12 22:55:50 -08:00
|
|
|
r += struct.pack("<I", res.get('timestamp'))
|
|
|
|
r += struct.pack("<I", res.get('bits'))
|
2018-01-14 09:04:59 -08:00
|
|
|
r += str_to_hash(res.get('nonce'))
|
|
|
|
r += ser_char_vector(str_to_hash(res.get('n_solution')))
|
2018-01-12 22:55:50 -08:00
|
|
|
return r
|
|
|
|
|
|
|
|
def deserialize_header(f, height):
|
2017-05-29 00:03:39 -07:00
|
|
|
h = {}
|
2018-01-12 22:55:50 -08:00
|
|
|
h['version'] = struct.unpack("<I", f.read(4))[0]
|
2018-01-14 09:04:59 -08:00
|
|
|
h['prev_block_hash'] = hash_to_str(f.read(32))
|
|
|
|
h['merkle_root'] = hash_to_str(f.read(32))
|
|
|
|
h['hash_reserved'] = hash_to_str(f.read(32))
|
2018-01-12 22:55:50 -08:00
|
|
|
h['timestamp'] = struct.unpack("<I", f.read(4))[0]
|
|
|
|
h['bits'] = struct.unpack("<I", f.read(4))[0]
|
2018-01-14 09:04:59 -08:00
|
|
|
h['nonce'] = hash_to_str(f.read(32))
|
|
|
|
h['n_solution'] = hash_to_str(deser_char_vector(f))
|
2017-05-29 00:03:39 -07:00
|
|
|
h['block_height'] = height
|
|
|
|
return h
|
|
|
|
|
2018-01-14 09:04:59 -08:00
|
|
|
def sha256_header(header):
|
|
|
|
return uint256_from_bytes(Hash(serialize_header(header)))
|
2018-01-12 22:55:50 -08:00
|
|
|
|
2017-05-29 00:03:39 -07:00
|
|
|
def hash_header(header):
|
|
|
|
if header is None:
|
|
|
|
return '0' * 64
|
|
|
|
if header.get('prev_block_hash') is None:
|
2018-01-14 09:04:59 -08:00
|
|
|
header['prev_block_hash'] = '00'*64
|
|
|
|
return hash_to_str(Hash(serialize_header(header)))
|
2017-05-29 00:03:39 -07:00
|
|
|
|
|
|
|
|
2017-07-15 08:20:06 -07:00
|
|
|
blockchains = {}
|
|
|
|
|
2017-12-12 02:10:50 -08:00
|
|
|
def read_blockchains(config):
|
|
|
|
blockchains[0] = Blockchain(config, 0, None)
|
2017-07-20 12:28:27 -07:00
|
|
|
fdir = os.path.join(util.get_headers_dir(config), 'forks')
|
2017-07-20 06:29:22 -07:00
|
|
|
if not os.path.exists(fdir):
|
|
|
|
os.mkdir(fdir)
|
|
|
|
l = filter(lambda x: x.startswith('fork_'), os.listdir(fdir))
|
2017-07-18 09:10:22 -07:00
|
|
|
l = sorted(l, key = lambda x: int(x.split('_')[1]))
|
2017-07-19 02:26:13 -07:00
|
|
|
for filename in l:
|
|
|
|
checkpoint = int(filename.split('_')[2])
|
2017-07-20 06:05:47 -07:00
|
|
|
parent_id = int(filename.split('_')[1])
|
2017-12-12 02:10:50 -08:00
|
|
|
b = Blockchain(config, checkpoint, parent_id)
|
2017-12-13 04:43:51 -08:00
|
|
|
h = b.read_header(b.checkpoint)
|
|
|
|
if b.parent().can_connect(h, check_height=False):
|
|
|
|
blockchains[b.checkpoint] = b
|
|
|
|
else:
|
|
|
|
util.print_error("cannot connect", filename)
|
2017-07-15 08:20:06 -07:00
|
|
|
return blockchains
|
|
|
|
|
2017-07-17 10:32:10 -07:00
|
|
|
def check_header(header):
|
2017-07-15 08:20:06 -07:00
|
|
|
if type(header) is not dict:
|
|
|
|
return False
|
|
|
|
for b in blockchains.values():
|
2017-07-16 23:44:09 -07:00
|
|
|
if b.check_header(header):
|
2017-07-15 08:20:06 -07:00
|
|
|
return b
|
|
|
|
return False
|
|
|
|
|
2017-07-17 10:32:10 -07:00
|
|
|
def can_connect(header):
|
|
|
|
for b in blockchains.values():
|
|
|
|
if b.can_connect(header):
|
|
|
|
return b
|
|
|
|
return False
|
2017-07-15 08:20:06 -07:00
|
|
|
|
|
|
|
|
2015-09-06 05:40:00 -07:00
|
|
|
class Blockchain(util.PrintError):
|
2017-02-04 06:48:13 -08:00
|
|
|
"""
|
|
|
|
Manages blockchain headers and their verification
|
|
|
|
"""
|
2017-07-13 07:23:41 -07:00
|
|
|
|
2017-12-12 02:10:50 -08:00
|
|
|
def __init__(self, config, checkpoint, parent_id):
|
2013-09-01 09:16:15 -07:00
|
|
|
self.config = config
|
2017-05-29 00:03:39 -07:00
|
|
|
self.catch_up = None # interface catching up
|
2017-07-19 02:26:13 -07:00
|
|
|
self.checkpoint = checkpoint
|
2017-12-12 02:10:50 -08:00
|
|
|
self.checkpoints = bitcoin.NetworkConstants.CHECKPOINTS
|
2017-07-20 06:05:47 -07:00
|
|
|
self.parent_id = parent_id
|
2017-07-24 06:54:42 -07:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
with self.lock:
|
|
|
|
self.update_size()
|
2017-07-20 06:05:47 -07:00
|
|
|
|
|
|
|
def parent(self):
|
|
|
|
return blockchains[self.parent_id]
|
2017-07-16 23:44:09 -07:00
|
|
|
|
2017-07-18 09:10:22 -07:00
|
|
|
def get_max_child(self):
|
2017-03-15 04:13:20 -07:00
|
|
|
children = list(filter(lambda y: y.parent_id==self.checkpoint, blockchains.values()))
|
2017-07-18 09:10:22 -07:00
|
|
|
return max([x.checkpoint for x in children]) if children else None
|
|
|
|
|
2017-07-19 00:29:20 -07:00
|
|
|
def get_checkpoint(self):
|
2017-07-18 09:10:22 -07:00
|
|
|
mc = self.get_max_child()
|
2017-07-19 00:29:20 -07:00
|
|
|
return mc if mc is not None else self.checkpoint
|
|
|
|
|
|
|
|
def get_branch_size(self):
|
|
|
|
return self.height() - self.get_checkpoint() + 1
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self.get_hash(self.get_checkpoint()).lstrip('00')[0:10]
|
2017-07-18 09:10:22 -07:00
|
|
|
|
2017-07-16 23:44:09 -07:00
|
|
|
def check_header(self, header):
|
|
|
|
header_hash = hash_header(header)
|
|
|
|
height = header.get('block_height')
|
|
|
|
return header_hash == self.get_hash(height)
|
2015-05-08 04:43:42 -07:00
|
|
|
|
2017-07-24 06:54:42 -07:00
|
|
|
def fork(parent, header):
|
|
|
|
checkpoint = header.get('block_height')
|
2017-12-12 02:10:50 -08:00
|
|
|
self = Blockchain(parent.config, checkpoint, parent.checkpoint)
|
2017-07-19 02:14:11 -07:00
|
|
|
open(self.path(), 'w+').close()
|
2017-07-24 06:54:42 -07:00
|
|
|
self.save_header(header)
|
2017-07-15 04:51:40 -07:00
|
|
|
return self
|
|
|
|
|
2015-05-08 04:43:42 -07:00
|
|
|
def height(self):
|
2017-07-16 23:44:09 -07:00
|
|
|
return self.checkpoint + self.size() - 1
|
|
|
|
|
|
|
|
def size(self):
|
2017-07-24 06:54:42 -07:00
|
|
|
with self.lock:
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
def update_size(self):
|
2017-07-19 02:14:11 -07:00
|
|
|
p = self.path()
|
2018-01-14 09:04:59 -08:00
|
|
|
self._size = 0
|
|
|
|
if os.path.exists(p):
|
|
|
|
with open(p, 'rb') as f:
|
|
|
|
f.seek(0, 2)
|
|
|
|
eof = f.tell()
|
|
|
|
f.seek(0, 0)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
f.seek(bitcoin.BASIC_HEADER_SIZE, 1)
|
|
|
|
vs = read_vector_size(f)
|
|
|
|
f.seek(vs, 1)
|
|
|
|
if f.tell() < eof:
|
|
|
|
self._size += 1
|
|
|
|
except:
|
|
|
|
break
|
2013-09-02 08:49:12 -07:00
|
|
|
|
2018-01-12 22:55:50 -08:00
|
|
|
def verify_header(self, header, prev_header, bits, target, nonce, n_solution):
|
|
|
|
prev_hash = self.sha256_header(prev_header)
|
|
|
|
_powhash = self.sha256_header(header)
|
2017-03-23 03:58:56 -07:00
|
|
|
if prev_hash != header.get('prev_block_hash'):
|
|
|
|
raise BaseException("prev hash mismatch: %s vs %s" % (prev_hash, header.get('prev_block_hash')))
|
2017-11-09 08:47:57 -08:00
|
|
|
if bitcoin.NetworkConstants.TESTNET:
|
2017-03-23 03:58:56 -07:00
|
|
|
return
|
2017-12-05 09:03:07 -08:00
|
|
|
bits = self.target_to_bits(target)
|
2017-03-23 03:58:56 -07:00
|
|
|
if bits != header.get('bits'):
|
|
|
|
raise BaseException("bits mismatch: %s vs %s" % (bits, header.get('bits')))
|
2018-01-12 22:55:50 -08:00
|
|
|
if int('0x' + _powhash, 16) > target:
|
|
|
|
raise BaseException("insufficient proof of work: %s vs target %s" % (int('0x' + _powhash, 16), target))
|
|
|
|
if not is_gbp_valid(nonce, n_solution):
|
|
|
|
raise BaseException("Equihash invalid")
|
2015-12-11 03:37:40 -08:00
|
|
|
|
2015-12-12 21:33:06 -08:00
|
|
|
def verify_chunk(self, index, data):
|
2018-01-12 22:55:50 -08:00
|
|
|
num = len(data) / 1484
|
|
|
|
prev_header = None
|
|
|
|
if index != 0:
|
|
|
|
prev_header = self.read_header(index * 2016 - 1)
|
|
|
|
headers = {}
|
2013-09-01 09:16:15 -07:00
|
|
|
for i in range(num):
|
2018-01-12 22:55:50 -08:00
|
|
|
raw_header = data[i * 1484:(i + 1) * 1484]
|
|
|
|
header = self.deserialize_header(raw_header, index * 2016 + i)
|
|
|
|
headers[header.get('block_height')] = header
|
|
|
|
nonce, n_solution = headers.get('nonce'), header.get('n_solution')
|
|
|
|
bits, target = self.get_target(index * 2016 + i, headers)
|
|
|
|
self.verify_header(header, prev_header, bits, target, nonce, n_solution)
|
|
|
|
prev_header = header
|
2014-10-21 10:05:51 -07:00
|
|
|
|
2013-09-01 09:16:15 -07:00
|
|
|
def path(self):
|
2017-05-29 00:03:39 -07:00
|
|
|
d = util.get_headers_dir(self.config)
|
2017-07-20 06:29:22 -07:00
|
|
|
filename = 'blockchain_headers' if self.parent_id is None else os.path.join('forks', 'fork_%d_%d'%(self.parent_id, self.checkpoint))
|
2017-07-19 02:26:13 -07:00
|
|
|
return os.path.join(d, filename)
|
2013-09-01 09:16:15 -07:00
|
|
|
|
|
|
|
def save_chunk(self, index, chunk):
|
|
|
|
filename = self.path()
|
2018-01-14 09:04:59 -08:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
d = self._height_to_offset(f, index * 2016 - self.checkpoint)
|
2017-07-16 23:44:09 -07:00
|
|
|
if d < 0:
|
|
|
|
chunk = chunk[-d:]
|
|
|
|
d = 0
|
2018-01-13 08:09:25 -08:00
|
|
|
self.write(chunk, d, index > len(self.checkpoints))
|
2017-07-21 00:02:58 -07:00
|
|
|
self.swap_with_parent()
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2017-07-18 09:10:22 -07:00
|
|
|
def swap_with_parent(self):
|
2017-07-21 00:02:58 -07:00
|
|
|
if self.parent_id is None:
|
|
|
|
return
|
|
|
|
parent_branch_size = self.parent().height() - self.checkpoint + 1
|
|
|
|
if parent_branch_size >= self.size():
|
|
|
|
return
|
2017-07-20 06:05:47 -07:00
|
|
|
self.print_error("swap", self.checkpoint, self.parent_id)
|
|
|
|
parent_id = self.parent_id
|
2017-07-18 09:10:22 -07:00
|
|
|
checkpoint = self.checkpoint
|
2017-07-20 06:05:47 -07:00
|
|
|
parent = self.parent()
|
2017-07-25 01:03:15 -07:00
|
|
|
with open(self.path(), 'rb') as f:
|
|
|
|
my_data = f.read()
|
|
|
|
with open(parent.path(), 'rb') as f:
|
2018-01-14 09:04:59 -08:00
|
|
|
self._height_to_offset
|
|
|
|
offset = self._height_to_offset(f, checkpoint - parent.checkpoint)
|
|
|
|
length = self._height_to_offset(f, parent_branch_size, offset)
|
|
|
|
f.seek(offset)
|
|
|
|
parent_data = f.read(length)
|
|
|
|
|
2017-07-25 01:03:15 -07:00
|
|
|
self.write(parent_data, 0)
|
2018-01-14 09:04:59 -08:00
|
|
|
parent.write(my_data, checkpoint - parent.checkpoint)
|
2017-07-20 02:36:59 -07:00
|
|
|
# store file path
|
|
|
|
for b in blockchains.values():
|
|
|
|
b.old_path = b.path()
|
2017-07-19 02:14:11 -07:00
|
|
|
# swap parameters
|
2017-07-20 06:05:47 -07:00
|
|
|
self.parent_id = parent.parent_id; parent.parent_id = parent_id
|
2017-07-18 09:10:22 -07:00
|
|
|
self.checkpoint = parent.checkpoint; parent.checkpoint = checkpoint
|
2017-07-25 09:47:36 -07:00
|
|
|
self._size = parent._size; parent._size = parent_branch_size
|
2017-07-20 02:36:59 -07:00
|
|
|
# move files
|
|
|
|
for b in blockchains.values():
|
|
|
|
if b in [self, parent]: continue
|
|
|
|
if b.old_path != b.path():
|
|
|
|
self.print_error("renaming", b.old_path, b.path())
|
|
|
|
os.rename(b.old_path, b.path())
|
2017-07-18 21:46:37 -07:00
|
|
|
# update pointers
|
|
|
|
blockchains[self.checkpoint] = self
|
|
|
|
blockchains[parent.checkpoint] = parent
|
2017-07-18 09:10:22 -07:00
|
|
|
|
2018-01-14 09:04:59 -08:00
|
|
|
def _height_to_offset(self, f, height, start=0):
|
|
|
|
pos = f.tell()
|
|
|
|
f.seek(0, 2)
|
|
|
|
eof = f.tell()
|
|
|
|
f.seek(start, 0)
|
|
|
|
for i in range(height):
|
|
|
|
f.seek(bitcoin.BASIC_HEADER_SIZE, 1)
|
|
|
|
vs = read_vector_size(f)
|
|
|
|
f.seek(vs, 1)
|
|
|
|
if f.tell() >= eof:
|
|
|
|
raise Exception('Out of file')
|
|
|
|
f.seek(pos, 0)
|
|
|
|
return f.tell()
|
|
|
|
|
|
|
|
def write(self, data, delta, truncate):
|
2017-07-16 23:44:09 -07:00
|
|
|
filename = self.path()
|
2017-07-24 06:54:42 -07:00
|
|
|
with self.lock:
|
|
|
|
with open(filename, 'rb+') as f:
|
2018-01-14 09:04:59 -08:00
|
|
|
f.seek(0, 2)
|
|
|
|
eof = f.tell()
|
|
|
|
offset = self._height_to_offset(f, delta)
|
2017-07-25 01:03:15 -07:00
|
|
|
f.seek(offset)
|
2018-01-14 09:04:59 -08:00
|
|
|
if truncate and offset < eof:
|
|
|
|
f.truncate()
|
2017-07-24 06:54:42 -07:00
|
|
|
f.write(data)
|
2017-07-25 01:03:15 -07:00
|
|
|
f.flush()
|
|
|
|
os.fsync(f.fileno())
|
2017-07-24 06:54:42 -07:00
|
|
|
self.update_size()
|
2017-07-25 01:03:15 -07:00
|
|
|
|
|
|
|
def save_header(self, header):
|
|
|
|
delta = header.get('block_height') - self.checkpoint
|
2018-01-14 09:04:59 -08:00
|
|
|
data = serialize_header(header)
|
2017-07-25 01:03:15 -07:00
|
|
|
assert delta == self.size()
|
2018-01-14 09:04:59 -08:00
|
|
|
self.write(data, delta)
|
2017-07-21 00:02:58 -07:00
|
|
|
self.swap_with_parent()
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2017-07-13 07:23:41 -07:00
|
|
|
def read_header(self, height):
|
2017-07-20 06:05:47 -07:00
|
|
|
assert self.parent_id != self.checkpoint
|
2017-08-01 02:00:12 -07:00
|
|
|
if height < 0:
|
|
|
|
return
|
2017-07-15 08:20:06 -07:00
|
|
|
if height < self.checkpoint:
|
2017-07-20 06:05:47 -07:00
|
|
|
return self.parent().read_header(height)
|
2017-07-19 05:26:44 -07:00
|
|
|
if height > self.height():
|
|
|
|
return
|
2018-01-12 22:55:50 -08:00
|
|
|
|
2018-01-14 09:04:59 -08:00
|
|
|
delta = height - self.checkpoint
|
2013-09-01 09:16:15 -07:00
|
|
|
name = self.path()
|
|
|
|
if os.path.exists(name):
|
2018-01-14 09:04:59 -08:00
|
|
|
f = open(name, 'rb')
|
|
|
|
for i in range(delta):
|
|
|
|
f.seek(bitcoin.BASIC_HEADER_SIZE, 1)
|
|
|
|
vs = read_vector_size(f)
|
|
|
|
f.seek(vs, 1)
|
|
|
|
h = deserialize_header(f, height)
|
|
|
|
f.close()
|
|
|
|
|
2018-01-12 22:55:50 -08:00
|
|
|
return h
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2017-05-29 00:03:39 -07:00
|
|
|
def get_hash(self, height):
|
2018-01-14 09:04:59 -08:00
|
|
|
return self.hash_header(self.read_header(height))
|
|
|
|
|
|
|
|
def hash_header(self, header):
|
|
|
|
return hash_header(header)
|
2017-03-25 03:09:24 -07:00
|
|
|
|
2017-07-25 09:57:34 -07:00
|
|
|
def get_target(self, index):
|
2017-12-05 09:03:07 -08:00
|
|
|
# compute target from chunk x, used in chunk x+1
|
2017-11-09 08:47:57 -08:00
|
|
|
if bitcoin.NetworkConstants.TESTNET:
|
2018-01-08 12:48:09 -08:00
|
|
|
return 0
|
2017-12-05 09:03:07 -08:00
|
|
|
if index == -1:
|
2018-01-08 12:48:09 -08:00
|
|
|
return MAX_TARGET
|
2017-12-05 09:03:07 -08:00
|
|
|
if index < len(self.checkpoints):
|
|
|
|
h, t = self.checkpoints[index]
|
|
|
|
return t
|
|
|
|
# new target
|
|
|
|
first = self.read_header(index * 2016)
|
|
|
|
last = self.read_header(index * 2016 + 2015)
|
2015-11-19 09:43:57 -08:00
|
|
|
bits = last.get('bits')
|
2017-12-05 09:03:07 -08:00
|
|
|
target = self.bits_to_target(bits)
|
|
|
|
nActualTimespan = last.get('timestamp') - first.get('timestamp')
|
|
|
|
nTargetTimespan = 14 * 24 * 60 * 60
|
|
|
|
nActualTimespan = max(nActualTimespan, nTargetTimespan // 4)
|
|
|
|
nActualTimespan = min(nActualTimespan, nTargetTimespan * 4)
|
|
|
|
new_target = min(MAX_TARGET, (target * nActualTimespan) // nTargetTimespan)
|
|
|
|
return new_target
|
|
|
|
|
|
|
|
def bits_to_target(self, bits):
|
2015-11-19 09:43:57 -08:00
|
|
|
bitsN = (bits >> 24) & 0xff
|
2017-03-23 03:58:56 -07:00
|
|
|
if not (bitsN >= 0x03 and bitsN <= 0x1d):
|
|
|
|
raise BaseException("First part of bits should be in [0x03, 0x1d]")
|
2015-11-19 09:43:57 -08:00
|
|
|
bitsBase = bits & 0xffffff
|
2017-03-23 03:58:56 -07:00
|
|
|
if not (bitsBase >= 0x8000 and bitsBase <= 0x7fffff):
|
|
|
|
raise BaseException("Second part of bits should be in [0x8000, 0x7fffff]")
|
2017-12-05 09:03:07 -08:00
|
|
|
return bitsBase << (8 * (bitsN-3))
|
|
|
|
|
|
|
|
def target_to_bits(self, target):
|
|
|
|
c = ("%064x" % target)[2:]
|
2015-11-19 09:43:57 -08:00
|
|
|
while c[:2] == '00' and len(c) > 6:
|
2013-09-01 09:16:15 -07:00
|
|
|
c = c[2:]
|
2017-02-04 06:48:13 -08:00
|
|
|
bitsN, bitsBase = len(c) // 2, int('0x' + c[:6], 16)
|
2015-11-19 09:43:57 -08:00
|
|
|
if bitsBase >= 0x800000:
|
|
|
|
bitsN += 1
|
|
|
|
bitsBase >>= 8
|
2017-12-05 09:03:07 -08:00
|
|
|
return bitsN << 24 | bitsBase
|
2013-09-01 09:16:15 -07:00
|
|
|
|
2017-07-20 22:52:38 -07:00
|
|
|
def can_connect(self, header, check_height=True):
|
2018-01-14 09:04:59 -08:00
|
|
|
import pdb; pdb.set_trace()
|
2017-07-18 12:32:34 -07:00
|
|
|
height = header['block_height']
|
2017-07-20 22:52:38 -07:00
|
|
|
if check_height and self.height() != height - 1:
|
2018-01-14 09:04:59 -08:00
|
|
|
self.print_error("cannot connect at height", height)
|
2017-07-18 12:32:34 -07:00
|
|
|
return False
|
2017-07-19 08:23:46 -07:00
|
|
|
if height == 0:
|
2017-11-09 08:47:57 -08:00
|
|
|
return hash_header(header) == bitcoin.NetworkConstants.GENESIS
|
2017-12-05 09:03:07 -08:00
|
|
|
try:
|
|
|
|
prev_hash = self.get_hash(height - 1)
|
|
|
|
except:
|
2017-03-31 03:45:09 -07:00
|
|
|
return False
|
2015-05-17 06:54:20 -07:00
|
|
|
if prev_hash != header.get('prev_block_hash'):
|
2017-03-31 03:45:09 -07:00
|
|
|
return False
|
2018-01-12 22:55:50 -08:00
|
|
|
nonce, n_solution = headers.get('nonce'), header.get('n_solution')
|
|
|
|
bits, target = self.get_target(index * 2016 + i, headers)
|
2015-12-11 03:37:40 -08:00
|
|
|
try:
|
2018-01-12 22:55:50 -08:00
|
|
|
self.verify_header(header, prev_header, bits, target, nonce, n_solution)
|
|
|
|
except:
|
2015-12-11 03:37:40 -08:00
|
|
|
return False
|
2017-03-31 03:45:09 -07:00
|
|
|
return True
|
2015-05-17 06:54:20 -07:00
|
|
|
|
2015-12-12 21:33:06 -08:00
|
|
|
def connect_chunk(self, idx, hexdata):
|
2015-05-17 06:54:20 -07:00
|
|
|
try:
|
2017-02-04 06:48:13 -08:00
|
|
|
data = bfh(hexdata)
|
2015-12-12 21:33:06 -08:00
|
|
|
self.verify_chunk(idx, data)
|
2017-05-29 00:03:39 -07:00
|
|
|
#self.print_error("validated chunk %d" % idx)
|
2015-12-12 21:33:06 -08:00
|
|
|
self.save_chunk(idx, data)
|
2017-03-31 03:45:09 -07:00
|
|
|
return True
|
2015-12-11 03:37:40 -08:00
|
|
|
except BaseException as e:
|
|
|
|
self.print_error('verify_chunk failed', str(e))
|
2017-03-31 03:45:09 -07:00
|
|
|
return False
|
2017-12-05 09:03:07 -08:00
|
|
|
|
|
|
|
def get_checkpoints(self):
|
|
|
|
# for each chunk, store the hash of the last block and the target after the chunk
|
|
|
|
cp = []
|
|
|
|
n = self.height() // 2016
|
|
|
|
for index in range(n):
|
|
|
|
h = self.get_hash((index+1) * 2016 -1)
|
|
|
|
target = self.get_target(index)
|
|
|
|
cp.append((h, target))
|
|
|
|
return cp
|