You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.4 KiB
171 lines
5.4 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
8 years ago
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
||
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""BlockStore and TxStore helper classes."""
|
||
10 years ago
|
|
||
9 years ago
|
from .mininode import *
|
||
|
from io import BytesIO
|
||
8 years ago
|
import dbm.dumb as dbmd
|
||
10 years ago
|
|
||
8 years ago
|
logger = logging.getLogger("TestFramework.blockstore")
|
||
|
|
||
10 years ago
|
class BlockStore(object):
|
||
8 years ago
|
"""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.
|
||
|
"""
|
||
|
|
||
10 years ago
|
def __init__(self, datadir):
|
||
8 years ago
|
self.blockDB = dbmd.open(datadir + "/blocks", 'c')
|
||
9 years ago
|
self.currentBlock = 0
|
||
9 years ago
|
self.headers_map = dict()
|
||
9 years ago
|
|
||
10 years ago
|
def close(self):
|
||
|
self.blockDB.close()
|
||
|
|
||
9 years ago
|
def erase(self, blockhash):
|
||
|
del self.blockDB[repr(blockhash)]
|
||
|
|
||
|
# lookup an entry and return the item as raw bytes
|
||
10 years ago
|
def get(self, blockhash):
|
||
9 years ago
|
value = None
|
||
10 years ago
|
try:
|
||
9 years ago
|
value = self.blockDB[repr(blockhash)]
|
||
10 years ago
|
except KeyError:
|
||
|
return None
|
||
9 years ago
|
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()
|
||
10 years ago
|
return ret
|
||
|
|
||
9 years ago
|
def get_header(self, blockhash):
|
||
|
try:
|
||
|
return self.headers_map[blockhash]
|
||
|
except KeyError:
|
||
|
return None
|
||
|
|
||
10 years ago
|
# 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
|
||
9 years ago
|
current_block_header = self.get_header(current_tip)
|
||
|
if current_block_header is None:
|
||
10 years ago
|
return None
|
||
|
|
||
|
response = msg_headers()
|
||
9 years ago
|
headersList = [ current_block_header ]
|
||
10 years ago
|
maxheaders = 2000
|
||
|
while (headersList[0].sha256 not in locator.vHave):
|
||
|
prevBlockHash = headersList[0].hashPrevBlock
|
||
9 years ago
|
prevBlockHeader = self.get_header(prevBlockHash)
|
||
|
if prevBlockHeader is not None:
|
||
|
headersList.insert(0, prevBlockHeader)
|
||
10 years ago
|
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())
|
||
|
except TypeError as e:
|
||
8 years ago
|
logger.exception("Unexpected error")
|
||
10 years ago
|
self.currentBlock = block.sha256
|
||
9 years ago
|
self.headers_map[block.sha256] = CBlockHeader(block)
|
||
|
|
||
|
def add_header(self, header):
|
||
|
self.headers_map[header.sha256] = header
|
||
10 years ago
|
|
||
9 years ago
|
# lookup the hashes in "inv", and return p2p messages for delivering
|
||
|
# blocks found.
|
||
10 years ago
|
def get_blocks(self, inv):
|
||
|
responses = []
|
||
|
for i in inv:
|
||
|
if (i.type == 2): # MSG_BLOCK
|
||
9 years ago
|
data = self.get(i.hash)
|
||
|
if data is not None:
|
||
|
# Use msg_generic to avoid re-serialization
|
||
|
responses.append(msg_generic(b"block", data))
|
||
10 years ago
|
return responses
|
||
|
|
||
|
def get_locator(self, current_tip=None):
|
||
|
if current_tip is None:
|
||
|
current_tip = self.currentBlock
|
||
|
r = []
|
||
|
counter = 0
|
||
|
step = 1
|
||
9 years ago
|
lastBlock = self.get_block(current_tip)
|
||
10 years ago
|
while lastBlock is not None:
|
||
|
r.append(lastBlock.hashPrevBlock)
|
||
|
for i in range(step):
|
||
9 years ago
|
lastBlock = self.get_block(lastBlock.hashPrevBlock)
|
||
10 years ago
|
if lastBlock is None:
|
||
|
break
|
||
|
counter += 1
|
||
|
if counter > 10:
|
||
|
step *= 2
|
||
|
locator = CBlockLocator()
|
||
|
locator.vHave = r
|
||
|
return locator
|
||
|
|
||
|
class TxStore(object):
|
||
|
def __init__(self, datadir):
|
||
8 years ago
|
self.txDB = dbmd.open(datadir + "/transactions", 'c')
|
||
10 years ago
|
|
||
|
def close(self):
|
||
|
self.txDB.close()
|
||
|
|
||
9 years ago
|
# lookup an entry and return the item as raw bytes
|
||
10 years ago
|
def get(self, txhash):
|
||
9 years ago
|
value = None
|
||
10 years ago
|
try:
|
||
9 years ago
|
value = self.txDB[repr(txhash)]
|
||
10 years ago
|
except KeyError:
|
||
|
return None
|
||
9 years ago
|
return value
|
||
|
|
||
|
def get_transaction(self, txhash):
|
||
|
ret = None
|
||
|
serialized_tx = self.get(txhash)
|
||
|
if serialized_tx is not None:
|
||
|
f = BytesIO(serialized_tx)
|
||
|
ret = CTransaction()
|
||
|
ret.deserialize(f)
|
||
|
ret.calc_sha256()
|
||
10 years ago
|
return ret
|
||
|
|
||
|
def add_transaction(self, tx):
|
||
|
tx.calc_sha256()
|
||
|
try:
|
||
|
self.txDB[repr(tx.sha256)] = bytes(tx.serialize())
|
||
|
except TypeError as e:
|
||
8 years ago
|
logger.exception("Unexpected error")
|
||
10 years ago
|
|
||
|
def get_transactions(self, inv):
|
||
|
responses = []
|
||
|
for i in inv:
|
||
|
if (i.type == 1): # MSG_TX
|
||
|
tx = self.get(i.hash)
|
||
|
if tx is not None:
|
||
9 years ago
|
responses.append(msg_generic(b"tx", tx))
|
||
10 years ago
|
return responses
|