def test_rollback_tx_metadata(self): alice_xmss = get_alice_xmss() tx1 = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], message_data=None, fee=0, xmss_pk=alice_xmss.pk) block = Block.create(dev_config=config.dev, block_number=5, prev_headerhash=b'', prev_timestamp=10, transactions=[tx1], miner_address=b'', seed_height=0, seed_hash=None) TransactionMetadata.update_tx_metadata(self.state, block=block, batch=None) tx_metadata = TransactionMetadata.get_tx_metadata( self.state, tx1.txhash) self.assertEqual(tx_metadata[0].to_json(), tx1.to_json()) TransactionMetadata.rollback_tx_metadata(self.state, block, None) self.assertIsNone( TransactionMetadata.get_tx_metadata(self.state, tx1.txhash))
def test_update_tx_metadata(self): with set_qrl_dir('no_data'): with State() as state: alice_xmss = get_alice_xmss() tx = TransferTransaction.create(addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block_number = 5 state.put_tx_metadata(tx, block_number, 10000, None) tx_metadata = state.get_tx_metadata(tx.txhash) self.assertEqual(tx_metadata[0].to_json(), tx.to_json()) self.assertEqual(tx_metadata[1], block_number)
def test_prepare_address_list(self): block = Block.create(block_number=10, prev_headerhash=b'', prev_timestamp=10, transactions=[], miner_address=get_some_address(1)) # Test Case: without any transactions of block self.assertEqual(self.state.prepare_address_list(block), {config.dev.coinbase_address, get_some_address(1)}) alice_xmss = get_alice_xmss() block = Block.create( block_number=10, prev_headerhash=b'', prev_timestamp=10, transactions=[ TransferTransaction.create( addrs_to=[get_some_address(2), get_some_address(3)], amounts=[100, 100], fee=0, xmss_pk=alice_xmss.pk) ], miner_address=get_some_address(1)) # Test Case, with one Transaction self.assertEqual( self.state.prepare_address_list(block), { config.dev.coinbase_address, get_some_address(1), get_some_address(2), get_some_address(3), alice_xmss.address })
def test_put_tx_metadata(self): self.assertIsNone(self.state.get_tx_metadata(b'test1')) alice_xmss = get_alice_xmss() tx = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block_number = 5 self.state.put_tx_metadata(tx, block_number, 10000, None) tx_metadata = self.state.get_tx_metadata(tx.txhash) self.assertEqual(tx_metadata[0].to_json(), tx.to_json()) self.assertEqual(tx_metadata[1], block_number)
def test_update_tx_metadata(self): alice_xmss = get_alice_xmss() tx = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], message_data=None, fee=0, xmss_pk=alice_xmss.pk) block_number = 5 TransactionMetadata.put_tx_metadata(self.state, tx, block_number, 10000, None) tx_metadata = TransactionMetadata.get_tx_metadata( self.state, tx.txhash) self.assertEqual(tx_metadata[0].to_json(), tx.to_json()) self.assertEqual(tx_metadata[1], block_number)
def test_get_last_txs(self): with set_qrl_dir('no_data'): with State() as state: self.assertEqual(state.get_last_txs(), []) alice_xmss = get_alice_xmss() block = Block() tx1 = TransferTransaction.create(addrs_to=[get_some_address(0), get_some_address(1)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx1.pbdata]) state._update_last_tx(block, None) last_txns = state.get_last_txs() # Test Case: When there is only 1 last txns self.assertEqual(len(last_txns), 1) self.assertEqual(last_txns[0].to_json(), tx1.to_json())
def test_get_last_txs(self): self.assertEqual(LastTransactions.get_last_txs(self.state), []) alice_xmss = get_alice_xmss() block = Block() tx1 = TransferTransaction.create( addrs_to=[get_some_address(0), get_some_address(1)], amounts=[1, 2], message_data=None, fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx1.pbdata]) LastTransactions._update_last_tx(self.state, block, None) last_txns = LastTransactions.get_last_txs(self.state) # Test Case: When there is only 1 last txns self.assertEqual(len(last_txns), 1) self.assertEqual(last_txns[0].to_json(), tx1.to_json())
def test_remove_last_tx(self): # Test Case: When there is no last txns self.assertEqual(self.state.get_last_txs(), []) alice_xmss = get_alice_xmss() block = Block() tx1 = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx1.pbdata]) self.state._update_last_tx(block, None) last_txns = self.state.get_last_txs() self.assertEqual(last_txns[0].to_json(), tx1.to_json()) self.state._remove_last_tx(block, None) last_txns = self.state.get_last_txs() self.assertEqual(last_txns, [])
def test_rollback_tx_metadata(self): alice_xmss = get_alice_xmss() tx1 = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block = Block.create(block_number=5, prev_headerhash=b'', prev_timestamp=10, transactions=[tx1], miner_address=b'') self.state.update_tx_metadata(block=block, batch=None) tx_metadata = self.state.get_tx_metadata(tx1.txhash) self.assertEqual(tx_metadata[0].to_json(), tx1.to_json()) self.state.rollback_tx_metadata(block, None) self.assertIsNone(self.state.get_tx_metadata(tx1.txhash))
def test_notification(self): source = Mock() source.factory = Mock() source.factory.master_mr = Mock() source.factory.master_mr.isRequested = Mock() source.factory.add_unprocessed_txn = Mock() channel = Observable(source) self.tx_manager = P2PTxManagement() self.tx_manager.new_channel(channel) tx = TransferTransaction.create(addrs_to=[get_some_address()], amounts=[1], fee=10, xmss_pk=bytes(67)) event = qrllegacy_pb2.LegacyMessage( func_name=qrllegacy_pb2.LegacyMessage.TX, txData=tx.pbdata) channel.notify(event, force_delivery=True) source.factory.master_mr.isRequested.assert_called() source.factory.add_unprocessed_txn.assert_called()
def test_update_last_tx(self): alice_xmss = get_alice_xmss() # Test Case: When there is no last txns self.assertEqual(self.state.get_last_txs(), []) block = Block() tx1 = TransferTransaction.create( addrs_to=[get_some_address(1), get_some_address(2)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx1.pbdata]) self.state._update_last_tx(block, None) last_txns = self.state.get_last_txs() # Test Case: When there is only 1 last txns self.assertEqual(len(last_txns), 1) self.assertEqual(last_txns[0].to_json(), tx1.to_json()) block = Block() tx2 = TransferTransaction.create( addrs_to=[get_some_address(2), get_some_address(3)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) tx3 = TransferTransaction.create( addrs_to=[get_some_address(4), get_some_address(5)], amounts=[1, 2], fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx2.pbdata, tx3.pbdata]) self.state._update_last_tx(block, None) last_txns = self.state.get_last_txs() # Test Case: When there are 3 last txns self.assertEqual(len(last_txns), 3) self.assertEqual(last_txns[0].to_json(), tx3.to_json()) self.assertEqual(last_txns[1].to_json(), tx2.to_json()) self.assertEqual(last_txns[2].to_json(), tx1.to_json())