def _try_to_mine(self): fee = float(BankSettings.objects.all()[0].fee) valid_transactions = [] for tid, not_mined_transaction in self.shell.blockchain.not_mined_transactions.items(): if (len(valid_transactions) < self.shell.block_size - 1 and not_mined_transaction.is_valid(self.shell.blockchain.all_utxos, self.shell.blockchain.minimum_transaction, fee)): valid_transactions.append(not_mined_transaction) if len(valid_transactions) < self.shell.block_size - 1: return block = Block(self.shell.blockchain.last_block_hash()) value = float(BankSettings.objects.all()[0].reward) + fee * (self.shell.block_size - 1) coinbase = Transaction('', self.bank.wallet.get_keys()[0], value=value, inputs=[]) block.add_transaction(transaction=coinbase, all_utxos=self.shell.blockchain.all_utxos, minimum_transaction=self.shell.blockchain.minimum_transaction, fee=fee, should_check=True, is_coinbase=True) self.shell.blockchain.append_transaction(coinbase) for valid_transaction in valid_transactions: block.add_transaction(valid_transaction, self.shell.blockchain.all_utxos, self.shell.blockchain.minimum_transaction, fee=fee, should_check=True) block.mine(BankSettings.objects.all()[0].difficulty) print('block mined: {}.'.format(block.calculate_hash())) self.shell.blockchain.append_block(block)
def mineBlock(self, t1, t2): prevHash = None depth = 0 if (len(self.blockchain) > 0): prevHash = self.calcPrevHash(self.blockchain[len(self.blockchain) - 1]) depth = len(self.blockchain) b = Block(t1, t2, prevHash, depth) b.mine() return b
def append_new_block(self, mine=True): block = Block(self.last_block_hash()) if mine: block.mine(self.difficulty) print("Block mined: " + block.hash) self.append_block(block) return block
def play(self): if self.attack_phase == AttackPhase.DOUBLE_SPEND: self.double_spend_block = self.chain.mine(True) self.blocks_mined.append(self.double_spend_block) self.adversarial_nipopow.chain.append(self.double_spend_block) self.attack_phase = AttackPhase.STITCH return None # withhold double spending block if self.attack_phase == AttackPhase.STITCH: self.stitch_block = self.honest_chain.mine(True) self.stitch_block.set_thorny_interlink([self.double_spend_block]) self.last_good_honest_block = self.stitch_block self.blocks_mined.append(self.stitch_block) self.adversarial_nipopow.chain.append(self.stitch_block) self.attack_phase = AttackPhase.GROW return self.honest_chain if self.attack_phase == AttackPhase.GROW: # discard = True if self.need_bypass: b = Block.mine(self.honest_chain[-1]) b.adversarial = True b.set_thorny_interlink([self.last_good_honest_block]) self.blocks_mined.append(b) if b.level < self.bypass_level: # Our block is of low enough level to stay under the radar # so we can use it for bypassing # Bypass any blocks between last_good_honest_block and b, # as they are blocks of a higher level than what we want. self.last_good_honest_block = b self.honest_chain.append(b) self.adversarial_nipopow.chain.append(b) # Bypass was successful. self.need_bypass = False # discard = False return self.honest_chain else: # Unfortunately, we mined a block of high level and this # cannot be used for bypassing (as b would then be included # by the honest prover into their proof), so we have to discard # this block and try again # discard = True pass # if not discard: # return b if self.attack_phase == AttackPhase.SUFFIX: b = self.chain.mine(True) self.blocks_mined.append(b) self.adversarial_nipopow.chain.append(b) self.suffix_size += 1 if self.suffix_size >= k and self.growth_completed(): self.attack_phase = AttackPhase.DONE return None # withhold suffix
def test_block(): genesis_block = Block( **{ 'version': 0, 'previous_block_hash': None, 'merkle_tree_hash': None, 'timestamp': 1507593600, 'nbits': 504382016, 'nonce': 0 }) block_with_nonce = genesis_block.mine() assert block_with_nonce.nonce != genesis_block.nonce assert block_with_nonce._base_hash == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\r\xdcY@B\x10\x1e' assert block_with_nonce.nonce == 144875
txouts=[TxOut(value=5000000, pubkey=address_1)]) ] genesis_block = Block( **{ 'version': 0, 'previous_block_hash': None, 'merkle_tree_hash': b'\x18\xff\x02\xdd\xbe\x1c5\xef\xc7M\xc4J\xa8G\xcf\r&\t\xf1\xde/\x05\xfd\xed\xeb\xc4\xcf\xb7k\x1e\xbd\xb6', 'timestamp': 1507593600, 'nbits': 504382016, 'nonce': 0, 'txns': genesis_transactions }) block_with_nonce = genesis_block.mine() def test_serialization(): from serialization import namedtuple_cls_registry assert len(namedtuple_cls_registry) > 0 json_str = genesis_block.serialize() deserialized_block = Block.deserialize(json_str) assert hasattr(deserialized_block, 'mine') assert deserialized_block.version == 0 def test_merkle_tree(): # let's generate a merkle tree hash from a list of transactions
def append_block(self, block: Block, mine_block=True): if mine_block: block.mine(self.difficulty) print("Block mined: " + block.hash) self.blocks.append(block)