From 3eef0ba4cf1f1206aa9d5b5715de50689ea4b19d Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sun, 30 Jul 2017 20:41:08 +0900 Subject: [PATCH] Rename block_full to block. Retain the raw block in the named tuple. --- lib/coins.py | 23 ++++++++++------------- server/block_processor.py | 34 ++++++++++++++++++---------------- tests/test_blocks.py | 12 ++++++------ 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/coins.py b/lib/coins.py index 83b7be4..af3d9af 100644 --- a/lib/coins.py +++ b/lib/coins.py @@ -46,7 +46,7 @@ from server.daemon import Daemon, DashDaemon, LegacyRPCDaemon from server.session import ElectrumX, DashElectrumX -Block = namedtuple("Block", "header transactions") +Block = namedtuple("Block", "raw header transactions") class CoinError(Exception): @@ -270,12 +270,11 @@ class Coin(object): return block[:cls.static_header_len(height)] @classmethod - def block_full(cls, block, height): - '''Returns (header, [(deserialized_tx, tx_hash), ...]) given a - block and its height.''' - header = cls.block_header(block, height) - txs = cls.DESERIALIZER(block[len(header):]).read_tx_block() - return Block(header, txs) + def block(cls, raw_block, height): + '''Return a Block namedtuple given a raw block and its height.''' + header = cls.block_header(raw_block, height) + txs = cls.DESERIALIZER(raw_block[len(header):]).read_tx_block() + return Block(raw_block, header, txs) @classmethod def decimal_value(cls, value): @@ -703,14 +702,12 @@ class FairCoin(Coin): ] @classmethod - def block_full(cls, block, height): - '''Returns (header, [(deserialized_tx, tx_hash), ...]) given a - block and its height.''' - + def block(cls, raw_block, height): + '''Return a Block namedtuple given a raw block and its height.''' if height > 0: - return super().block_full(block, height) + return super().block(raw_block, height) else: - return Block(cls.block_header(block, height), []) + return Block(raw_block, cls.block_header(raw_block, height), []) @classmethod def electrum_header(cls, header, height): diff --git a/server/block_processor.py b/server/block_processor.py index 157d226..bbeba85 100644 --- a/server/block_processor.py +++ b/server/block_processor.py @@ -225,21 +225,23 @@ class BlockProcessor(server.db.DB): self.open_dbs() self.caught_up_event.set() - async def check_and_advance_blocks(self, blocks, first): - '''Process the list of blocks passed. Detects and handles reorgs.''' - self.prefetcher.processing_blocks(blocks) + async def check_and_advance_blocks(self, raw_blocks, first): + '''Process the list of raw blocks passed. Detects and handles + reorgs. + ''' + self.prefetcher.processing_blocks(raw_blocks) if first != self.height + 1: # If we prefetched two sets of blocks and the first caused # a reorg this will happen when we try to process the # second. It should be very rare. self.logger.warning('ignoring {:,d} blocks starting height {:,d}, ' - 'expected {:,d}' - .format(len(blocks), first, self.height + 1)) + 'expected {:,d}'.format(len(raw_blocks), first, + self.height + 1)) return - blocks = [self.coin.block_full(block, first + n) - for n, block in enumerate(blocks)] - headers = [b.header for b in blocks] + blocks = [self.coin.block(raw_block, first + n) + for n, raw_block in enumerate(raw_blocks)] + headers = [block.header for block in blocks] hprevs = [self.coin.header_prevhash(h) for h in headers] 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 - def backup_blocks(self, blocks): - '''Backup the blocks and flush. + def backup_blocks(self, raw_blocks): + '''Backup the raw blocks and flush. The blocks should be in order of decreasing height, starting at. self.height. A flush is performed once the blocks are backed up. ''' self.assert_flushed() - assert self.height >= len(blocks) + assert self.height >= len(raw_blocks) coin = self.coin - for block in blocks: + for raw_block in raw_blocks: # Check and update self.tip - block_full = coin.block_full(block, self.height) - header_hash = coin.header_hash(block_full.header) + block = coin.block(raw_block, self.height) + header_hash = coin.header_hash(block.header) if header_hash != self.tip: raise ChainError('backup block {} not tip {} at height {:,d}' .format(hash_to_str(header_hash), hash_to_str(self.tip), self.height)) - self.tip = coin.header_prevhash(block_full.header) - self.backup_txs(block_full.transactions) + self.tip = coin.header_prevhash(block.header) + self.backup_txs(block.transactions) self.height -= 1 self.tx_counts.pop() diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 769a4c5..d4ba141 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -58,11 +58,11 @@ def block_details(request): def test_block(block_details): coin, block_info = block_details - block = unhexlify(block_info['block']) - h, txs = coin.block_full(block, block_info['height']) + raw_block = unhexlify(block_info['block']) + block = coin.block(raw_block, block_info['height']) - assert coin.header_hash(h) == hex_str_to_hash(block_info['hash']) - assert coin.header_prevhash(h) == hex_str_to_hash(block_info['previousblockhash']) - for n, tx in enumerate(txs): - _, txid = tx + assert coin.header_hash(block.header) == hex_str_to_hash(block_info['hash']) + assert (coin.header_prevhash(block.header) + == hex_str_to_hash(block_info['previousblockhash'])) + for n, (tx, txid) in enumerate(block.transactions): assert txid == hex_str_to_hash(block_info['tx'][n])