def __init__(self, left, right, value=None): if value is None: self.value = byte_tools.sha256(left.value + right.value) else: self.value = value self.left = left self.right = right
def finalize_protoblock(self, response): # Finalise tx reconciliation missing_tx_ids = [] missing_txs = [] for segment in response: segment_txs = segment[1] if segment[0] == b'\x00': # Don't decode the flag next_id = segment[0] else: next_id = self.id_encoding_scheme.decode(segment[0]) for tx in segment_txs: missing_txs.append(tx) tx_id = bt.sha256(tx.encode()) missing_tx_ids.append(tx_id) if next_id in self.proto_block: self.proto_block.insert(self.proto_block.index(next_id), tx_id) elif next_id == b'\x00': self.proto_block.append(tx_id) # Add missing txs to mempool self.add_to_txpool(dict(zip(missing_tx_ids, missing_txs))) # Reconstruct block self.partial_tree = PartialMerkleTree.from_leaf_values( self.proto_block) # TODO: Wasteful, fix this
def __init__(self, mempool, block_tx_ids, ip, port): # Initialise network parameters self.ip = ip self.port = port self.server = None #Initialise transaction pool self.txpool = dict( zip([bt.sha256(tx.encode()) for tx in mempool], mempool)) self.txpool[bt.empty_hash] = "" self.proto_block = None #Initialise leafs self.partial_tree = PartialMerkleTree.from_leaf_values(block_tx_ids) # Initialise encoding parameters # TODO: Implement better encodings see self.setup_id_encoding, self.setup_pair_encoding and byte_tools.py self.id_encoding_scheme = None self.pair_encoding_scheme = None self.id_encoding_size = 8 self.pair_encoding_size = 3 # TODO: This should dynamically change as we progress in height
import blocks_util import byte_tools as bt from node import Node import time # Initialise Alice's txpool, currently modeled by a orphaned block # Ordered by arrival at node alice_mempool = blocks_util.getTxsFromBlock(blocks_util.actual) # Initialise Alice's block, currently modeled by a slice of her txpool block_size = 2048 offset = 1 alice_block_tx_ids = [bt.sha256(tx.encode()) for tx in alice_mempool][offset:block_size+offset] alice = Node(alice_mempool, alice_block_tx_ids, 'localhost', 99) alice.init_server() alice.open_connection('localhost', 100) time.sleep(1) # Estimated percentage in the block which of transactions missing from receivers txpool est_missing_tx_perc = 0.1 # Estimated percentage of missing pairs at height 1 est_missing_pair_perc = 0.001 alice.send_block(est_missing_tx_perc, est_missing_pair_perc)
import blocks_util import byte_tools as bt from node import Node # Initialise Bobs txpool, currently modeled by a block # Ordered by arrival at node bob_mempool = blocks_util.getTxsFromBlock(blocks_util.actual) # Initialise Bob's block (Not really needed/will be discarded) block_size = 2048 bob_block_tx_ids = [bt.sha256(tx.encode()) for tx in bob_mempool][:block_size] bob = Node(bob_mempool, bob_block_tx_ids, 'localhost', 100) bob.init_server() bob.open_connection('localhost', 99) bob.listen_for_blocks()