2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-01-02 18:12:05 +01:00
|
|
|
# Copyright (c) 2015-2017 The Bitcoin Core developers
|
2016-09-11 23:25:51 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""BlockStore and TxStore helper classes."""
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2016-03-19 21:36:32 +01:00
|
|
|
from .mininode import *
|
|
|
|
from io import BytesIO
|
2016-09-29 11:21:33 +02:00
|
|
|
import dbm.dumb as dbmd
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2017-02-24 23:54:19 +01:00
|
|
|
logger = logging.getLogger("TestFramework.blockstore")
|
|
|
|
|
2017-10-17 03:46:23 +02:00
|
|
|
class BlockStore():
|
2017-01-18 00:34:40 +01:00
|
|
|
"""BlockStore helper class.
|
|
|
|
|
|
|
|
BlockStore keeps a map of blocks and implements helper functions for
|
|
|
|
responding to getheaders and getdata, and for constructing a getheaders
|
|
|
|
message.
|
|
|
|
"""
|
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
def __init__(self, datadir):
|
2016-09-29 11:21:33 +02:00
|
|
|
self.blockDB = dbmd.open(datadir + "/blocks", 'c')
|
2016-03-19 20:58:06 +01:00
|
|
|
self.currentBlock = 0
|
2015-08-05 23:47:34 +02:00
|
|
|
self.headers_map = dict()
|
2016-05-31 20:21:40 +02:00
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
def close(self):
|
|
|
|
self.blockDB.close()
|
|
|
|
|
2016-05-31 20:21:40 +02:00
|
|
|
def erase(self, blockhash):
|
|
|
|
del self.blockDB[repr(blockhash)]
|
|
|
|
|
|
|
|
# lookup an entry and return the item as raw bytes
|
2015-04-28 18:39:47 +02:00
|
|
|
def get(self, blockhash):
|
2016-05-31 20:21:40 +02:00
|
|
|
value = None
|
2015-04-28 18:39:47 +02:00
|
|
|
try:
|
2016-05-31 20:21:40 +02:00
|
|
|
value = self.blockDB[repr(blockhash)]
|
2015-04-28 18:39:47 +02:00
|
|
|
except KeyError:
|
|
|
|
return None
|
2016-05-31 20:21:40 +02:00
|
|
|
return value
|
|
|
|
|
|
|
|
# lookup an entry and return it as a CBlock
|
|
|
|
def get_block(self, blockhash):
|
|
|
|
ret = None
|
|
|
|
serialized_block = self.get(blockhash)
|
|
|
|
if serialized_block is not None:
|
|
|
|
f = BytesIO(serialized_block)
|
|
|
|
ret = CBlock()
|
|
|
|
ret.deserialize(f)
|
|
|
|
ret.calc_sha256()
|
2015-04-28 18:39:47 +02:00
|
|
|
return ret
|
|
|
|
|
2015-08-05 23:47:34 +02:00
|
|
|
def get_header(self, blockhash):
|
|
|
|
try:
|
|
|
|
return self.headers_map[blockhash]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
# Note: this pulls full blocks out of the database just to retrieve
|
|
|
|
# the headers -- perhaps we could keep a separate data structure
|
|
|
|
# to avoid this overhead.
|
|
|
|
def headers_for(self, locator, hash_stop, current_tip=None):
|
|
|
|
if current_tip is None:
|
|
|
|
current_tip = self.currentBlock
|
2015-08-05 23:47:34 +02:00
|
|
|
current_block_header = self.get_header(current_tip)
|
|
|
|
if current_block_header is None:
|
2015-04-28 18:39:47 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
response = msg_headers()
|
2015-08-05 23:47:34 +02:00
|
|
|
headersList = [ current_block_header ]
|
2015-04-28 18:39:47 +02:00
|
|
|
maxheaders = 2000
|
|
|
|
while (headersList[0].sha256 not in locator.vHave):
|
|
|
|
prevBlockHash = headersList[0].hashPrevBlock
|
2015-08-05 23:47:34 +02:00
|
|
|
prevBlockHeader = self.get_header(prevBlockHash)
|
|
|
|
if prevBlockHeader is not None:
|
|
|
|
headersList.insert(0, prevBlockHeader)
|
2015-04-28 18:39:47 +02:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
headersList = headersList[:maxheaders] # truncate if we have too many
|
|
|
|
hashList = [x.sha256 for x in headersList]
|
|
|
|
index = len(headersList)
|
|
|
|
if (hash_stop in hashList):
|
|
|
|
index = hashList.index(hash_stop)+1
|
|
|
|
response.headers = headersList[:index]
|
|
|
|
return response
|
|
|
|
|
|
|
|
def add_block(self, block):
|
|
|
|
block.calc_sha256()
|
|
|
|
try:
|
|
|
|
self.blockDB[repr(block.sha256)] = bytes(block.serialize())
|
2018-03-29 17:47:06 +02:00
|
|
|
except TypeError:
|
2017-02-24 23:54:19 +01:00
|
|
|
logger.exception("Unexpected error")
|
2015-04-28 18:39:47 +02:00
|
|
|
self.currentBlock = block.sha256
|
2015-08-05 23:47:34 +02:00
|
|
|
self.headers_map[block.sha256] = CBlockHeader(block)
|
|
|
|
|
|
|
|
def add_header(self, header):
|
|
|
|
self.headers_map[header.sha256] = header
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2016-05-31 20:21:40 +02:00
|
|
|
# lookup the hashes in "inv", and return p2p messages for delivering
|
|
|
|
# blocks found.
|
2015-04-28 18:39:47 +02:00
|
|
|
def get_blocks(self, inv):
|
|
|
|
responses = []
|
|
|
|
for i in inv:
|
2017-10-12 05:25:18 +02:00
|
|
|
if (i.type == 2 or i.type == (2 | (1 << 30))): # MSG_BLOCK or MSG_WITNESS_BLOCK
|
2016-05-31 20:21:40 +02:00
|
|
|
data = self.get(i.hash)
|
|
|
|
if data is not None:
|
|
|
|
# Use msg_generic to avoid re-serialization
|
|
|
|
responses.append(msg_generic(b"block", data))
|
2015-04-28 18:39:47 +02:00
|
|
|
return responses
|
|
|
|
|
|
|
|
def get_locator(self, current_tip=None):
|
|
|
|
if current_tip is None:
|
|
|
|
current_tip = self.currentBlock
|
|
|
|
r = []
|
|
|
|
counter = 0
|
|
|
|
step = 1
|
2016-05-31 20:21:40 +02:00
|
|
|
lastBlock = self.get_block(current_tip)
|
2015-04-28 18:39:47 +02:00
|
|
|
while lastBlock is not None:
|
|
|
|
r.append(lastBlock.hashPrevBlock)
|
|
|
|
for i in range(step):
|
2016-05-31 20:21:40 +02:00
|
|
|
lastBlock = self.get_block(lastBlock.hashPrevBlock)
|
2015-04-28 18:39:47 +02:00
|
|
|
if lastBlock is None:
|
|
|
|
break
|
|
|
|
counter += 1
|
|
|
|
if counter > 10:
|
|
|
|
step *= 2
|
|
|
|
locator = CBlockLocator()
|
|
|
|
locator.vHave = r
|
|
|
|
return locator
|
|
|
|
|
2017-10-17 03:46:23 +02:00
|
|
|
class TxStore():
|
2015-04-28 18:39:47 +02:00
|
|
|
def __init__(self, datadir):
|
2016-09-29 11:21:33 +02:00
|
|
|
self.txDB = dbmd.open(datadir + "/transactions", 'c')
|
2015-04-28 18:39:47 +02:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.txDB.close()
|
|
|
|
|
2016-05-31 20:21:40 +02:00
|
|
|
# lookup an entry and return the item as raw bytes
|
2015-04-28 18:39:47 +02:00
|
|
|
def get(self, txhash):
|
2016-05-31 20:21:40 +02:00
|
|
|
value = None
|
2015-04-28 18:39:47 +02:00
|
|
|
try:
|
2016-05-31 20:21:40 +02:00
|
|
|
value = self.txDB[repr(txhash)]
|
2015-04-28 18:39:47 +02:00
|
|
|
except KeyError:
|
|
|
|
return None
|
2016-05-31 20:21:40 +02:00
|
|
|
return value
|
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
def add_transaction(self, tx):
|
|
|
|
tx.calc_sha256()
|
|
|
|
try:
|
|
|
|
self.txDB[repr(tx.sha256)] = bytes(tx.serialize())
|
2018-03-29 17:47:06 +02:00
|
|
|
except TypeError:
|
2017-02-24 23:54:19 +01:00
|
|
|
logger.exception("Unexpected error")
|
2015-04-28 18:39:47 +02:00
|
|
|
|
|
|
|
def get_transactions(self, inv):
|
|
|
|
responses = []
|
|
|
|
for i in inv:
|
2017-10-12 05:25:18 +02:00
|
|
|
if (i.type == 1 or i.type == (1 | (1 << 30))): # MSG_TX or MSG_WITNESS_TX
|
2015-04-28 18:39:47 +02:00
|
|
|
tx = self.get(i.hash)
|
|
|
|
if tx is not None:
|
2016-05-31 20:21:40 +02:00
|
|
|
responses.append(msg_generic(b"tx", tx))
|
2015-04-28 18:39:47 +02:00
|
|
|
return responses
|