Guest User

Untitled

a guest
Jul 27th, 2017
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Electrum - lightweight Bitcoin client
  4. # Copyright (C) 2012 [email protected]
  5. #
  6. # Permission is hereby granted, free of charge, to any person
  7. # obtaining a copy of this software and associated documentation files
  8. # (the "Software"), to deal in the Software without restriction,
  9. # including without limitation the rights to use, copy, modify, merge,
  10. # publish, distribute, sublicense, and/or sell copies of the Software,
  11. # and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25.  
  26.  
  27.  
  28. import os
  29. import util
  30. import bitcoin
  31. from bitcoin import *
  32.  
  33. MAX_TARGET = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
  34.  
  35. def bits_to_target(bits):
  36. if bits == 0:
  37. return 0
  38. size = bits >> 24
  39. assert size <= 0x1d
  40.  
  41. word = bits & 0x00ffffff
  42. assert 0x8000 <= word <= 0x7fffff
  43.  
  44. if size <= 3:
  45. return word >> (8 * (3 - size))
  46. else:
  47. return word << (8 * (size - 3))
  48.  
  49. def target_to_bits(target):
  50. if target == 0:
  51. return 0
  52. target = min(target, MAX_TARGET)
  53. size = (target.bit_length() + 7) / 8
  54. mask64 = 0xffffffffffffffff
  55. if size <= 3:
  56. compact = (target & mask64) << (8 * (3 - size))
  57. else:
  58. compact = (target >> (8 * (size - 3))) & mask64
  59.  
  60. if compact & 0x00800000:
  61. compact >>= 8
  62. size += 1
  63. assert compact == (compact & 0x007fffff)
  64. assert size < 256
  65. return compact | size << 24
  66.  
  67.  
  68. class Blockchain(util.PrintError):
  69. '''Manages blockchain headers and their verification'''
  70. def __init__(self, config, network):
  71. self.config = config
  72. self.network = network
  73. self.cur_chunk = None
  74. self.local_height = 0
  75. self.set_local_height()
  76.  
  77. def height(self):
  78. return self.local_height
  79.  
  80. def init(self):
  81. import threading
  82. if os.path.exists(self.path()):
  83. self.downloading_headers = False
  84. return
  85. self.downloading_headers = True
  86. t = threading.Thread(target = self.init_headers_file)
  87. t.daemon = True
  88. t.start()
  89.  
  90. def verify_header(self, header, prev_header, bits):
  91. prev_hash = self.hash_header(prev_header)
  92. assert prev_hash == header.get('prev_block_hash'), "prev hash mismatch: %s vs %s" % (prev_hash, header.get('prev_block_hash'))
  93. if bitcoin.TESTNET or bitcoin.NOLNET: return
  94. assert bits == header.get('bits'), "bits mismatch: %s vs %s" % (bits, header.get('bits'))
  95. _hash = self.hash_header(header)
  96. target = bits_to_target(bits)
  97. assert int('0x' + _hash, 16) <= target, "insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target)
  98.  
  99. def verify_chain(self, chain):
  100. first_header = chain[0]
  101. prev_header = self.read_header(first_header.get('block_height') - 1)
  102. for header in chain:
  103. height = header.get('block_height')
  104. bits, target = self.get_target(height / 2016, chain)
  105. self.verify_header(header, prev_header, bits)
  106. prev_header = header
  107.  
  108. def verify_chunk(self, index, data):
  109. self.cur_chunk = data
  110. self.cur_chunk_index = index
  111. num = len(data) / 80
  112. prev_header = None
  113. if index != 0:
  114. prev_header = self.read_header(index*2016 - 1)
  115. for i in range(num):
  116. raw_header = data[i*80:(i+1) * 80]
  117. header = self.deserialize_header(raw_header)
  118. bits = self.get_bits(header['block_height'])
  119. self.verify_header(header, prev_header, bits)
  120. prev_header = header
  121. self.cur_chunk = None
  122.  
  123. def serialize_header(self, res):
  124. s = int_to_hex(res.get('version'), 4) \
  125. + rev_hex(res.get('prev_block_hash')) \
  126. + rev_hex(res.get('merkle_root')) \
  127. + int_to_hex(int(res.get('timestamp')), 4) \
  128. + int_to_hex(int(res.get('bits')), 4) \
  129. + int_to_hex(int(res.get('nonce')), 4)
  130. return s
  131.  
  132. def deserialize_header(self, s):
  133. hex_to_int = lambda s: int('0x' + s[::-1].encode('hex'), 16)
  134. h = {}
  135. h['version'] = hex_to_int(s[0:4])
  136. h['prev_block_hash'] = hash_encode(s[4:36])
  137. h['merkle_root'] = hash_encode(s[36:68])
  138. h['timestamp'] = hex_to_int(s[68:72])
  139. h['bits'] = hex_to_int(s[72:76])
  140. h['nonce'] = hex_to_int(s[76:80])
  141. return h
  142.  
  143. def hash_header(self, header):
  144. if header is None:
  145. return '0' * 64
  146. return hash_encode(Hash(self.serialize_header(header).decode('hex')))
  147.  
  148. def path(self):
  149. return util.get_headers_path(self.config)
  150.  
  151. def init_headers_file(self):
  152. filename = self.path()
  153. try:
  154. import urllib, socket
  155. socket.setdefaulttimeout(30)
  156. self.print_error("downloading ", bitcoin.HEADERS_URL)
  157. urllib.urlretrieve(bitcoin.HEADERS_URL, filename + '.tmp')
  158. os.rename(filename + '.tmp', filename)
  159. self.print_error("done.")
  160. except Exception:
  161. self.print_error("download failed. creating file", filename)
  162. open(filename, 'wb+').close()
  163. self.downloading_headers = False
  164. self.set_local_height()
  165. self.print_error("%d blocks" % self.local_height)
  166.  
  167. def save_chunk(self, index, chunk):
  168. filename = self.path()
  169. f = open(filename, 'rb+')
  170. f.seek(index * 2016 * 80)
  171. h = f.write(chunk)
  172. f.close()
  173. self.set_local_height()
  174.  
  175. def save_header(self, header):
  176. data = self.serialize_header(header).decode('hex')
  177. assert len(data) == 80
  178. height = header.get('block_height')
  179. filename = self.path()
  180. f = open(filename, 'rb+')
  181. f.seek(height * 80)
  182. h = f.write(data)
  183. f.close()
  184. self.set_local_height()
  185.  
  186. def set_local_height(self):
  187. name = self.path()
  188. if os.path.exists(name):
  189. h = os.path.getsize(name)/80 - 1
  190. if self.local_height != h:
  191. self.local_height = h
  192.  
  193. def read_header(self, block_height):
  194. name = self.path()
  195. if os.path.exists(name):
  196. f = open(name, 'rb')
  197. f.seek(block_height * 80)
  198. h = f.read(80)
  199. f.close()
  200. if len(h) == 80:
  201. h = self.deserialize_header(h)
  202. return h
  203. def get_median_time_past(self, height):
  204. times = [self.read_header(h)['timestamp']
  205. for h in range(max(0, height - 10), height + 1)]
  206. return sorted(times)[len(times) // 2]
  207.  
  208. def get_bits(self, height):
  209. '''Return bits for the given height.'''
  210. if bitcoin.TESTNET:
  211. return 0
  212. # Difficulty adjustment interval?
  213. if height % 2016 == 0:
  214. return self.get_new_bits(height)
  215. prior = self.read_header(height - 1)
  216. bits = prior['bits']
  217. # Can't go below minimum, so early bail
  218. if bits == MAX_BITS:
  219. return bits
  220. mtp_6blocks = (self.get_median_time_past(height - 1)
  221. - self.get_median_time_past(height - 7))
  222. if mtp_6blocks < 12 * 3600:
  223. return bits
  224. # If it took over 12hrs to produce the last 6 blocks, increase the
  225. # target by 25% (reducing difficulty by 20%).
  226. target = bits_to_target(bits)
  227. target += target >> 2
  228. return target_to_bits(target)
  229.  
  230. def get_new_bits(self, height):
  231. assert height % 2016 == 0
  232. # Genesis
  233. if height == 0:
  234. return MAX_BITS
  235. first = self.read_header(height - 2016)
  236. prior = self.read_header(height - 1)
  237. prior_target = bits_to_target(prior['bits'])
  238.  
  239. target_span = 14 * 24 * 60 * 60
  240. span = prior['timestamp'] - first['timestamp']
  241. span = min(max(span, target_span / 4), target_span * 4)
  242. new_target = (prior_target * span) / target_span
  243. return target_to_bits(new_target)
  244.  
  245.  
  246.  
  247.  
  248. def get_target(self, index, chain=None):
  249. if index == 0:
  250. return 0x1d00ffff, MAX_TARGET
  251. first = self.read_header((index-1) * 2016)
  252. last = self.read_header(index*2016 - 1)
  253. if last is None:
  254. for h in chain:
  255. if h.get('block_height') == index*2016 - 1:
  256. last = h
  257. assert last is not None
  258. # bits to target
  259. bits = last.get('bits')
  260. bitsN = (bits >> 24) & 0xff
  261. assert bitsN >= 0x03 and bitsN <= 0x1d, "First part of bits should be in [0x03, 0x1d]"
  262. bitsBase = bits & 0xffffff
  263. assert bitsBase >= 0x8000 and bitsBase <= 0x7fffff, "Second part of bits should be in [0x8000, 0x7fffff]"
  264. target = bitsBase << (8 * (bitsN-3))
  265. # new target
  266. nActualTimespan = last.get('timestamp') - first.get('timestamp')
  267. nTargetTimespan = 14 * 24 * 60 * 60
  268. nActualTimespan = max(nActualTimespan, nTargetTimespan / 4)
  269. nActualTimespan = min(nActualTimespan, nTargetTimespan * 4)
  270. new_target = min(MAX_TARGET, (target*nActualTimespan) / nTargetTimespan)
  271. # convert new target to bits
  272. c = ("%064x" % new_target)[2:]
  273. while c[:2] == '00' and len(c) > 6:
  274. c = c[2:]
  275. bitsN, bitsBase = len(c) / 2, int('0x' + c[:6], 16)
  276. if bitsBase >= 0x800000:
  277. bitsN += 1
  278. bitsBase >>= 8
  279. new_bits = bitsN << 24 | bitsBase
  280. return new_bits, bitsBase << (8 * (bitsN-3))
  281.  
  282. def connect_header(self, chain, header):
  283. '''Builds a header chain until it connects. Returns True if it has
  284. successfully connected, False if verification failed, otherwise the
  285. height of the next header needed.'''
  286. chain.append(header) # Ordered by decreasing height
  287. previous_height = header['block_height'] - 1
  288. previous_header = self.read_header(previous_height)
  289.  
  290. # Missing header, request it
  291. if not previous_header:
  292. return previous_height
  293.  
  294. # Does it connect to my chain?
  295. prev_hash = self.hash_header(previous_header)
  296. if prev_hash != header.get('prev_block_hash'):
  297. self.print_error("reorg")
  298. return previous_height
  299.  
  300. # The chain is complete. Reverse to order by increasing height
  301. chain.reverse()
  302. try:
  303. self.verify_chain(chain)
  304. self.print_error("new height:", previous_height + len(chain))
  305. for header in chain:
  306. self.save_header(header)
  307. return True
  308. except BaseException as e:
  309. self.print_error(str(e))
  310. return False
  311.  
  312. def connect_chunk(self, idx, hexdata):
  313. try:
  314. data = hexdata.decode('hex')
  315. self.verify_chunk(idx, data)
  316. self.print_error("validated chunk %d" % idx)
  317. self.save_chunk(idx, data)
  318. return idx + 1
  319. except BaseException as e:
  320. self.print_error('verify_chunk failed', str(e))
  321. return idx - 1
Advertisement
Add Comment
Please, Sign In to add comment