Browse Source

Rename block_full to block.

Retain the raw block in the named tuple.
master
Neil Booth 8 years ago
parent
commit
3eef0ba4cf
  1. 23
      lib/coins.py
  2. 34
      server/block_processor.py
  3. 12
      tests/test_blocks.py

23
lib/coins.py

@ -46,7 +46,7 @@ from server.daemon import Daemon, DashDaemon, LegacyRPCDaemon
from server.session import ElectrumX, DashElectrumX from server.session import ElectrumX, DashElectrumX
Block = namedtuple("Block", "header transactions") Block = namedtuple("Block", "raw header transactions")
class CoinError(Exception): class CoinError(Exception):
@ -270,12 +270,11 @@ class Coin(object):
return block[:cls.static_header_len(height)] return block[:cls.static_header_len(height)]
@classmethod @classmethod
def block_full(cls, block, height): def block(cls, raw_block, height):
'''Returns (header, [(deserialized_tx, tx_hash), ...]) given a '''Return a Block namedtuple given a raw block and its height.'''
block and its height.''' header = cls.block_header(raw_block, height)
header = cls.block_header(block, height) txs = cls.DESERIALIZER(raw_block[len(header):]).read_tx_block()
txs = cls.DESERIALIZER(block[len(header):]).read_tx_block() return Block(raw_block, header, txs)
return Block(header, txs)
@classmethod @classmethod
def decimal_value(cls, value): def decimal_value(cls, value):
@ -703,14 +702,12 @@ class FairCoin(Coin):
] ]
@classmethod @classmethod
def block_full(cls, block, height): def block(cls, raw_block, height):
'''Returns (header, [(deserialized_tx, tx_hash), ...]) given a '''Return a Block namedtuple given a raw block and its height.'''
block and its height.'''
if height > 0: if height > 0:
return super().block_full(block, height) return super().block(raw_block, height)
else: else:
return Block(cls.block_header(block, height), []) return Block(raw_block, cls.block_header(raw_block, height), [])
@classmethod @classmethod
def electrum_header(cls, header, height): def electrum_header(cls, header, height):

34
server/block_processor.py

@ -225,21 +225,23 @@ class BlockProcessor(server.db.DB):
self.open_dbs() self.open_dbs()
self.caught_up_event.set() self.caught_up_event.set()
async def check_and_advance_blocks(self, blocks, first): async def check_and_advance_blocks(self, raw_blocks, first):
'''Process the list of blocks passed. Detects and handles reorgs.''' '''Process the list of raw blocks passed. Detects and handles
self.prefetcher.processing_blocks(blocks) reorgs.
'''
self.prefetcher.processing_blocks(raw_blocks)
if first != self.height + 1: if first != self.height + 1:
# If we prefetched two sets of blocks and the first caused # If we prefetched two sets of blocks and the first caused
# a reorg this will happen when we try to process the # a reorg this will happen when we try to process the
# second. It should be very rare. # second. It should be very rare.
self.logger.warning('ignoring {:,d} blocks starting height {:,d}, ' self.logger.warning('ignoring {:,d} blocks starting height {:,d}, '
'expected {:,d}' 'expected {:,d}'.format(len(raw_blocks), first,
.format(len(blocks), first, self.height + 1)) self.height + 1))
return return
blocks = [self.coin.block_full(block, first + n) blocks = [self.coin.block(raw_block, first + n)
for n, block in enumerate(blocks)] for n, raw_block in enumerate(raw_blocks)]
headers = [b.header for b in blocks] headers = [block.header for block in blocks]
hprevs = [self.coin.header_prevhash(h) for h in headers] hprevs = [self.coin.header_prevhash(h) for h in headers]
chain = [self.tip] + [self.coin.header_hash(h) for h in headers[:-1]] chain = [self.tip] + [self.coin.header_hash(h) for h in headers[:-1]]
@ -562,26 +564,26 @@ class BlockProcessor(server.db.DB):
return undo_info return undo_info
def backup_blocks(self, blocks): def backup_blocks(self, raw_blocks):
'''Backup the blocks and flush. '''Backup the raw blocks and flush.
The blocks should be in order of decreasing height, starting at. The blocks should be in order of decreasing height, starting at.
self.height. A flush is performed once the blocks are backed up. self.height. A flush is performed once the blocks are backed up.
''' '''
self.assert_flushed() self.assert_flushed()
assert self.height >= len(blocks) assert self.height >= len(raw_blocks)
coin = self.coin coin = self.coin
for block in blocks: for raw_block in raw_blocks:
# Check and update self.tip # Check and update self.tip
block_full = coin.block_full(block, self.height) block = coin.block(raw_block, self.height)
header_hash = coin.header_hash(block_full.header) header_hash = coin.header_hash(block.header)
if header_hash != self.tip: if header_hash != self.tip:
raise ChainError('backup block {} not tip {} at height {:,d}' raise ChainError('backup block {} not tip {} at height {:,d}'
.format(hash_to_str(header_hash), .format(hash_to_str(header_hash),
hash_to_str(self.tip), self.height)) hash_to_str(self.tip), self.height))
self.tip = coin.header_prevhash(block_full.header) self.tip = coin.header_prevhash(block.header)
self.backup_txs(block_full.transactions) self.backup_txs(block.transactions)
self.height -= 1 self.height -= 1
self.tx_counts.pop() self.tx_counts.pop()

12
tests/test_blocks.py

@ -58,11 +58,11 @@ def block_details(request):
def test_block(block_details): def test_block(block_details):
coin, block_info = block_details coin, block_info = block_details
block = unhexlify(block_info['block']) raw_block = unhexlify(block_info['block'])
h, txs = coin.block_full(block, block_info['height']) block = coin.block(raw_block, block_info['height'])
assert coin.header_hash(h) == hex_str_to_hash(block_info['hash']) assert coin.header_hash(block.header) == hex_str_to_hash(block_info['hash'])
assert coin.header_prevhash(h) == hex_str_to_hash(block_info['previousblockhash']) assert (coin.header_prevhash(block.header)
for n, tx in enumerate(txs): == hex_str_to_hash(block_info['previousblockhash']))
_, txid = tx for n, (tx, txid) in enumerate(block.transactions):
assert txid == hex_str_to_hash(block_info['tx'][n]) assert txid == hex_str_to_hash(block_info['tx'][n])

Loading…
Cancel
Save