def __init__(self, *args, **kwargs): super(TestMultiSigSpend, self).__init__(*args, **kwargs) with set_xrd_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() self.random = get_alice_xmss(4) self.random_signer = get_bob_xmss(4) self.signatories = [ self.alice.address, self.bob.address, self.random.address ] self.weights = [20, 30, 10] self.threshold = 30
def test_validate_slave_has_insufficient_permissions(self, m_logger): """ Master's AddressState says the Slave has permission 0. But Slave's AddressState says the Slave is good for permission 2. Therefore the Slave does not have enough permissions. """ bob = get_bob_xmss() # Let's say Alice is Bob's master. self.params["master_addr"] = self.alice.address self.params["xmss_pk"] = bob.pk tx = MessageTransaction.create(**self.params) tx.sign(self.alice) # The master's state says the slave can have these permissions. self.m_addr_state.slave_pks_access_type = {str(tx.PK): 0} # The signing slave's state can be 0 (full permissions) or 1 (mining only) but only 0 is used for now. # Let's give an invalid number. self.m_addr_from_pk_state.slave_pks_access_type = {str(tx.PK): 2} result = tx.validate_slave(self.m_addr_state, self.m_addr_from_pk_state) self.assertFalse(result) # Let's give a valid number, that matches what the master's state says (0) self.m_addr_from_pk_state.slave_pks_access_type = {str(tx.PK): 0} result = tx.validate_slave(self.m_addr_state, self.m_addr_from_pk_state) self.assertTrue(result)
def setUp(self): with set_qrl_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() alice_address_state = OptimizedAddressState.get_default( self.alice.address) alice_address_state.pbdata.balance = 100 self.addresses_state = { self.alice.address: alice_address_state, self.bob.address: OptimizedAddressState.get_default(self.bob.address) } self.params = { "token_txhash": b'I declare the TEST token', "addrs_to": [self.bob.address], "amounts": [100], "fee": 1, "xmss_pk": self.alice.pk } self.unused_chain_manager_mock = Mock(autospec=ChainManager, name='unused ChainManager')
def test_update_token_metadata(self): with set_data_dir('no_data'): with State() as state: alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_transaction = get_token_transaction(alice_xmss, bob_xmss) state.create_token_metadata(token_transaction) transfer_token_transaction = TransferTokenTransaction.create( addr_from=bob_xmss.address, token_txhash=token_transaction.txhash, addrs_to=[alice_xmss.address], amounts=[100000000], fee=1, xmss_pk=bob_xmss.pk) state.update_token_metadata(transfer_token_transaction) token_metadata = state.get_token_metadata( token_transaction.txhash) self.assertEqual(len(token_metadata.transfer_token_tx_hashes), 2) self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash) self.assertEqual(token_metadata.transfer_token_tx_hashes[1], transfer_token_transaction.txhash)
def test_relayTransferTokenTxn(self): with set_xrd_dir("wallet_ver1"): walletd = WalletD() service = WalletAPIService(walletd) resp = service.AddNewAddress(xrdwallet_pb2.AddNewAddressReq(), context=None) qaddress = resp.address addr_state = AddressState.get_default(walletd.qaddress_to_address(qaddress)) walletd._public_stub.GetAddressState = Mock( return_value=xrd_pb2.GetAddressStateResp(state=addr_state.pbdata)) walletd._public_stub.IsSlave = Mock( return_value=xrd_pb2.IsSlaveResp(result=True)) walletd._public_stub.GetOTS = Mock( return_value=xrd_pb2.GetOTSResp(next_unused_ots_index=0, unused_ots_index_found=True)) alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses_to = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] walletd._public_stub.PushTransaction = Mock( return_value=xrd_pb2.PushTransactionResp(error_code=xrd_pb2.PushTransactionResp.SUBMITTED)) resp = service.RelayTransferTokenTxn(xrdwallet_pb2.RelayTransferTokenTxnReq(addresses_to=qaddresses_to, amounts=amounts, token_txhash='', fee=100000000, master_address=None, signer_address=qaddress, ots_index=0), context=None) self.assertEqual(resp.code, 0) self.assertIsNotNone(resp.tx)
def test_put_multi_sig_addresses_state(self): alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() random_xmss = get_random_xmss() signatories = [alice_xmss.address, bob_xmss.address] weights = [20, 20] threshold = 21 multi_sig_tx = MultiSigCreate.create(signatories, weights, threshold, 0, random_xmss.pk) multi_sig_tx.sign(random_xmss) multi_sig_address_state = MultiSigAddressState.get_default( multi_sig_tx.txhash, signatories, weights, threshold) multi_sig_addresses_state = { multi_sig_address_state.address: multi_sig_address_state } AddressState.put_addresses_state(self.state, multi_sig_addresses_state) multi_sig_address_state2 = MultiSigAddressState.get_multi_sig_address_state_by_address( self.state._db, MultiSigAddressState.generate_multi_sig_address( multi_sig_tx.txhash)) self.assertEqual(multi_sig_address_state.pbdata, multi_sig_address_state2.pbdata)
def test_relayTransferTokenTxn(self): with set_qrl_dir("wallet_ver1"): walletd = WalletD() service = WalletAPIService(walletd) resp = service.AddNewAddress(qrlwallet_pb2.AddNewAddressReq(), context=None) qaddress = resp.address alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses_to = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] walletd._public_stub.PushTransaction = Mock( return_value=qrl_pb2.PushTransactionResp( error_code=qrl_pb2.PushTransactionResp.SUBMITTED)) resp = service.RelayTransferTokenTxn( qrlwallet_pb2.RelayTransferTokenTxnReq( addresses_to=qaddresses_to, amounts=amounts, token_txhash='', fee=100000000, master_address=None, signer_address=qaddress, ots_index=0), context=None) self.assertEqual(resp.code, 0) self.assertIsNotNone(resp.tx)
def test_get_all_address_state(self): with set_qrl_dir('no_data'): with State() as state: addresses_state = state.get_all_address_state() self.assertEqual(len(addresses_state), 0) alice_xmss = get_alice_xmss() alice_address = alice_xmss.address address_state = state.get_address_state(alice_address) addresses_state = { alice_address: address_state } self.assertTrue(isinstance(address_state.address, bytes)) state.put_addresses_state(addresses_state) addresses_state = state.get_all_address_state() self.assertEqual(len(addresses_state), 1) bob_xmss = get_bob_xmss() bob_address = bob_xmss.address address_state = state.get_address_state(bob_address) addresses_state = { bob_address: address_state } self.assertTrue(isinstance(address_state.address, bytes)) state.put_addresses_state(addresses_state) addresses_state = state.get_all_address_state() self.assertEqual(len(addresses_state), 2)
def test_relayTokenTxnBySlave(self): with set_xrd_dir("wallet_ver1"): walletd = WalletD() walletd._public_stub.PushTransaction = Mock( return_value=xrd_pb2.PushTransactionResp(error_code=xrd_pb2.PushTransactionResp.SUBMITTED)) service = WalletAPIService(walletd) resp = service.AddNewAddressWithSlaves(xrdwallet_pb2.AddNewAddressWithSlavesReq(), context=None) qaddress = resp.address addr_state = AddressState.get_default(walletd.qaddress_to_address(qaddress)) slaves = walletd.get_slave_list(qaddress) addr_state.add_slave_pks_access_type(bytes(hstr2bin(slaves[0][0].pk)), 0) walletd._public_stub.GetAddressState = Mock( return_value=xrd_pb2.GetAddressStateResp(state=addr_state.pbdata)) alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] resp = service.RelayTokenTxnBySlave( xrdwallet_pb2.RelayTokenTxnBySlaveReq(symbol=b'xrd', name=b'Quantum Resistant Ledger', owner=alice_xmss.qaddress, decimals=5, addresses=qaddresses, amounts=amounts, fee=100000000, master_address=qaddress), context=None) self.assertEqual(resp.code, 0) self.assertIsNotNone(resp.tx)
def test_validate_custom(self, m_logger): """ SlaveTransaction._validate_custom() checks for the following things: 1. if you specify more than 100 slave_pks at once 2. if len(slave_pks) != len(access_types) 3. access_types can only be 0, 1 """ # We're going to need all the XMSS trees we can get here bob = get_bob_xmss() # Too many slave_pks with patch('qrl.core.txs.SlaveTransaction.config', autospec=True) as m_config: m_config.dev.transaction_multi_output_limit = 2 params = self.params.copy() params["slave_pks"] = [self.alice.pk, bob.pk, self.slave.pk] params["access_types"] = [0, 0, 0] with self.assertRaises(ValueError): SlaveTransaction.create(**params) # Unequal length slave_pks and access_types params = self.params.copy() params["slave_pks"] = [self.slave.pk] params["access_types"] = [0, 1] with self.assertRaises(ValueError): SlaveTransaction.create(**params) # access_type is a weird, undefined number params = self.params.copy() params["access_types"] = [5] with self.assertRaises(ValueError): SlaveTransaction.create(**params)
def test_remove_transfer_token_metadata(self): with set_qrl_dir('no_data'): with State() as state: alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_transaction = get_token_transaction(alice_xmss, bob_xmss) state.create_token_metadata(token_transaction) transfer_token = TransferTokenTransaction.create(token_txhash=token_transaction.txhash, addrs_to=[alice_xmss.address], amounts=[100000000], fee=1, xmss_pk=bob_xmss.pk) transfer_token.sign(alice_xmss) state.update_token_metadata(transfer_token) token_metadata = state.get_token_metadata(transfer_token.token_txhash) self.assertIn(transfer_token.txhash, token_metadata.transfer_token_tx_hashes) state.remove_transfer_token_metadata(transfer_token) token_metadata = state.get_token_metadata(transfer_token.token_txhash) self.assertNotIn(transfer_token.txhash, token_metadata.transfer_token_tx_hashes)
def test_relay_transfer_token_txn(self): with set_qrl_dir("wallet_ver1"): walletd = WalletD() qaddress = walletd.add_new_address(height=4) walletd.encrypt_wallet(self.passphrase) walletd.unlock_wallet(self.passphrase) alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses_to = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] walletd._public_stub.PushTransaction = Mock( return_value=qrl_pb2.PushTransactionResp( error_code=qrl_pb2.PushTransactionResp.SUBMITTED)) tx = walletd.relay_transfer_token_txn(qaddresses_to=qaddresses_to, amounts=amounts, token_txhash='', fee=100000000, master_qaddress=None, signer_address=qaddress, ots_index=0) self.assertIsNotNone(tx) walletd.lock_wallet() with self.assertRaises(ValueError): walletd.relay_transfer_token_txn(qaddresses_to=qaddresses_to, amounts=amounts, token_txhash='', fee=100000000, master_qaddress=None, signer_address=qaddress, ots_index=0)
def __init__(self, *args, **kwargs): super(TestMessageTransaction, self).__init__(*args, **kwargs) self.alice = get_alice_xmss() self.bob = get_bob_xmss() self.alice.set_ots_index(10) self.maxDiff = None
def test_relayTransferTokenTxnBySlave(self): with set_qrl_dir("wallet_ver1"): walletd = WalletD() walletd._public_stub.PushTransaction = Mock( return_value=qrl_pb2.PushTransactionResp(error_code=qrl_pb2.PushTransactionResp.SUBMITTED)) service = WalletAPIService(walletd) resp = service.AddNewAddressWithSlaves(qrlwallet_pb2.AddNewAddressWithSlavesReq(), context=None) qaddress = resp.address addr_state = AddressState.get_default(walletd.qaddress_to_address(qaddress)) slaves = walletd.get_slave_list(qaddress) addr_state.add_slave_pks_access_type(bytes(hstr2bin(slaves[0][0].pk)), 0) walletd._public_stub.GetAddressState = Mock( return_value=qrl_pb2.GetAddressStateResp(state=addr_state.pbdata)) alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses_to = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] resp = service.RelayTransferTokenTxnBySlave( qrlwallet_pb2.RelayTransferTokenTxnBySlaveReq(addresses_to=qaddresses_to, amounts=amounts, token_txhash='', fee=100000000, master_address=qaddress), context=None) self.assertEqual(resp.code, 0) self.assertIsNotNone(resp.tx)
def test_relayTokenTxn(self): with set_qrl_dir("wallet_ver1"): walletd = WalletD() service = WalletAPIService(walletd) resp = service.AddNewAddress(qrlwallet_pb2.AddNewAddressReq(), context=None) qaddress = resp.address alice_xmss = get_alice_xmss(4) bob_xmss = get_bob_xmss(4) qaddresses = [alice_xmss.qaddress, bob_xmss.qaddress] amounts = [1000000000, 1000000000] walletd._public_stub.PushTransaction = Mock( return_value=qrl_pb2.PushTransactionResp( error_code=qrl_pb2.PushTransactionResp.SUBMITTED)) resp = service.RelayTokenTxn(qrlwallet_pb2.RelayTokenTxnReq( symbol=b'QRL', name=b'Quantum Resistant Ledger', owner=alice_xmss.qaddress, decimals=5, addresses=qaddresses, amounts=amounts, fee=100000000, master_address=None, signer_address=qaddress, ots_index=0), context=None) self.assertEqual(resp.code, 0) self.assertIsNotNone(resp.tx)
def setUp(self): with set_xrd_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() alice_address_state = OptimizedAddressState.get_default( self.alice.address) alice_address_state.pbdata.balance = 100 self.addresses_state = { self.alice.address: alice_address_state, self.bob.address: OptimizedAddressState.get_default(self.bob.address) } self.params = { "symbol": b'xrd', "name": b'Quantum Resistant Ledger', "owner": self.alice.address, "decimals": 15, "initial_balances": [], "fee": 1, "xmss_pk": self.alice.pk } self.unused_chain_manager_mock = Mock(autospec=ChainManager, name='unused ChainManager')
def test_remove_transfer_token_metadata(self): alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_transaction = get_token_transaction(alice_xmss, bob_xmss) TokenMetadata.create_token_metadata(self.state, token_transaction, None) transfer_token = TransferTokenTransaction.create( token_txhash=token_transaction.txhash, addrs_to=[alice_xmss.address], amounts=[100000000], fee=1, xmss_pk=bob_xmss.pk) transfer_token.sign(alice_xmss) TokenMetadata.update_token_metadata(self.state, transfer_token, None) token_metadata = TokenMetadata.get_token_metadata( self.state, transfer_token.token_txhash) self.assertIn(transfer_token.txhash, token_metadata.transfer_token_tx_hashes) TokenMetadata.remove_transfer_token_metadata(self.state, transfer_token, None) token_metadata = TokenMetadata.get_token_metadata( self.state, transfer_token.token_txhash) self.assertNotIn(transfer_token.txhash, token_metadata.transfer_token_tx_hashes)
def test_validate_slave_signing_xmss_state_has_no_slave_permissions_in_state( self, m_logger): bob = get_bob_xmss() # Let's say Alice is Bob's master. self.params["master_addr"] = self.alice.address self.params["xmss_pk"] = bob.pk # We need to add extra data to the mock AddressState. tx = MessageTransaction.create(**self.params) tx.sign(self.alice) state_container = StateContainer(addresses_state=dict(), tokens=Indexer(b'token', None), slaves=Indexer(b'slave', None), lattice_pk=Indexer( b'lattice_pk', None), multi_sig_spend_txs=dict(), votes_stats=dict(), block_number=1, total_coin_supply=1000, current_dev_config=config.dev, write_access=True, my_db=self.state._db, batch=None) result = tx.validate_slave(state_container) self.assertFalse(result)
def setUp(self): with set_qrl_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() self.alice.set_ots_index(10) self.maxDiff = None
def __init__(self, *args, **kwargs): super(TestTokenTransaction, self).__init__(*args, **kwargs) self.alice = get_alice_xmss() self.bob = get_bob_xmss() self._decimals = 15 self.alice.set_ots_index(10) self.maxDiff = None
def test_check_stale_txn(self, m_is_full_transaction_pool, m_config): """ Stale Transactions are Transactions that were supposed to go into block 5, but for some reason didn't make it. They languish in TransactionPool until check_stale_txn() checks the Pool and updates the tx_info to make them go into a higher block. For each stale transaction, P2PFactory.broadcast_tx() will be called. """ # Redefine at what point should txs be considered stale m_config.user.stale_transaction_threshold = 2 bob_xmss = get_bob_xmss(4) alice_xmss = get_alice_xmss(4) tx1 = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[1000000], message_data=None, fee=1, xmss_pk=alice_xmss.pk) tx1.sign(alice_xmss) tx2 = TransferTransaction.create(addrs_to=[bob_xmss.address], amounts=[10000], message_data=None, fee=1, xmss_pk=alice_xmss.pk) tx2.sign(alice_xmss) m_broadcast_tx = Mock( name='Mock Broadcast TX function (in P2PFactory)') self.txpool.add_tx_to_pool(tx1, 5) self.txpool.add_tx_to_pool(tx2, 5) self.txpool.set_broadcast_tx(m_broadcast_tx) with set_qrl_dir('no_data'): state = State() chain_manager = ChainManager(state) self.txpool.check_stale_txn(chain_manager.new_state_container, chain_manager.update_state_container, 8) self.assertEqual(m_broadcast_tx.call_count, 0) m = MockFunction() bob_address_state = OptimizedAddressState.get_default( bob_xmss.address) bob_address_state.pbdata.balance = 1000000000000 m.put(bob_xmss.address, bob_address_state) chain_manager.get_optimized_address_state = m.get tx3 = TransferTransaction.create(addrs_to=[alice_xmss.address], amounts=[10000], message_data=None, fee=1, xmss_pk=bob_xmss.pk) tx3.sign(bob_xmss) self.txpool.add_tx_to_pool(tx3, 5) self.txpool.check_stale_txn(chain_manager.new_state_container, chain_manager.update_state_container, 8) self.assertEqual(m_broadcast_tx.call_count, 1)
def test_getTotalBalance(self): db_state = Mock(spec=State) xmss1 = get_alice_xmss() xmss2 = get_alice_xmss(4) xmss3 = get_bob_xmss(4) address_state1 = AddressState.create(address=xmss1.address, nonce=25, balance=1000, ots_bitfield=[b'\x00'] * config.dev.ots_bitfield_size, tokens=dict(), slave_pks_access_type=dict(), ots_counter=0) address_state2 = AddressState.create(address=xmss2.address, nonce=25, balance=2000, ots_bitfield=[b'\x00'] * config.dev.ots_bitfield_size, tokens=dict(), slave_pks_access_type=dict(), ots_counter=0) address_state3 = AddressState.create(address=xmss3.address, nonce=25, balance=3000, ots_bitfield=[b'\x00'] * config.dev.ots_bitfield_size, tokens=dict(), slave_pks_access_type=dict(), ots_counter=0) m = MockFunction() m.put(xmss1.address, address_state1) m.put(xmss2.address, address_state2) m.put(xmss3.address, address_state3) db_state.get_address_state = m.get p2p_factory = Mock(spec=P2PFactory) chain_manager = ChainManager(db_state) qrlnode = QRLNode(mining_address=b'') qrlnode.set_chain_manager(chain_manager) qrlnode._p2pfactory = p2p_factory qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetAddressStateReq() service.GetAddressState(request=request, context=context) context.set_code.assert_called() context.set_details.assert_called() context = Mock(spec=ServicerContext) request = qrl_pb2.GetTotalBalanceReq() request.addresses.extend([xmss1.address, xmss2.address, xmss3.address]) response = service.GetTotalBalance(request=request, context=context) context.set_code.assert_not_called() self.assertEqual(6000, response.balance)
def test_transferCoins_get_unsigned(self): with set_data_dir('no_data'): with State() as db_state: with set_wallet_dir("test_wallet"): p2p_factory = Mock(spec=P2PFactory) p2p_factory.pow = Mock(spec=POW) chain_manager = ChainManager(db_state) qrlnode = QRLNode(db_state, slaves=[]) qrlnode.set_chain_manager(chain_manager) qrlnode._p2pfactory = p2p_factory qrlnode._pow = p2p_factory.pow qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) alice = get_alice_xmss() bob = get_bob_xmss() request = qrl_pb2.TransferCoinsReq( address_from=alice.address, addresses_to=[bob.address], amounts=[101], fee=12, xmss_pk=alice.pk) response = service.TransferCoins(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertIsNotNone(response) self.assertIsNotNone(response.transaction_unsigned) self.assertEqual( 'transfer', response.transaction_unsigned.WhichOneof( 'transactionType')) self.assertEqual(alice.address, response.transaction_unsigned.addr_from) self.assertEqual(12, response.transaction_unsigned.fee) self.assertEqual(alice.pk, response.transaction_unsigned.public_key) self.assertEqual(0, response.transaction_unsigned.nonce) self.assertEqual(b'', response.transaction_unsigned.signature) self.assertEqual( b'', response.transaction_unsigned.transaction_hash) self.assertEqual( bob.address, response.transaction_unsigned.transfer.addrs_to[0]) self.assertEqual( 101, response.transaction_unsigned.transfer.amounts[0])
def __init__(self, qrlnode, time_mock, ntp_mock): required_height = ceil(log(self.MAXNUMBLOCKS, 2)) required_height = int(required_height + required_height % 2) self.qrlnode = qrlnode self.time_mock = time_mock self.ntp_mock = ntp_mock self.alice_xmss = get_alice_xmss(xmss_height=required_height) self.bob_xmss = get_bob_xmss()
def test_getStakers(self): db_state = Mock(spec=State) db_state.stake_validators_tracker = Mock(spec=StakeValidatorsTracker) db_state.stake_validators_tracker.sv_dict = dict() p2p_factory = Mock(spec=P2PFactory) buffered_chain = Mock(spec=BufferedChain) buffered_chain.tx_pool = Mock() buffered_chain.get_block = Mock() buffered_chain._chain = Mock() qrlnode = QRLNode(db_state) qrlnode.set_p2pfactory(p2p_factory) qrlnode.set_chain(buffered_chain) service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetStakersReq(filter=qrl_pb2.GetStakersReq.CURRENT, offset=0, quantity=3) response = service.GetStakers(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertEqual(0, len(response.stakers)) # Add a few validators stake_tx = StakeTransaction.create(1, get_alice_xmss(), get_bob_xmss().pk(), sha256(b'terminator')) expected_address = bytes(get_alice_xmss().get_address().encode()) db_state.get_address = MagicMock( return_value=AddressState.create(address=expected_address, nonce=1, balance=100, pubhashes=[], tokens=dict())) db_state.get_address_tx_hashes = MagicMock(return_value=[]) validator1 = StakeValidator.create(100, stake_tx) db_state.stake_validators_tracker.sv_dict[ validator1.address] = validator1 request = qrl_pb2.GetStakersReq(filter=qrl_pb2.GetStakersReq.CURRENT, offset=0, quantity=3) response = service.GetStakers(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertEqual(1, len(response.stakers)) self.assertEqual(expected_address, response.stakers[0].address_state.address)
def __init__(self, *args, **kwargs): super(TestTokenTransaction, self).__init__(*args, **kwargs) with set_qrl_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() self._decimals = 15 self.alice.set_ots_index(10) self.maxDiff = None
def __init__(self, *args, **kwargs): super(TestSimpleTransaction, self).__init__(*args, **kwargs) with set_xrd_dir('no_data'): self.state = State() self.alice = get_alice_xmss() self.bob = get_bob_xmss() self.slave = get_slave_xmss() self.alice.set_ots_index(10) self.maxDiff = None
def test_remove_token_metadata(self): alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_tx = get_token_transaction(alice_xmss, bob_xmss) self.state.create_token_metadata(token_tx) token_metadata = self.state.get_token_metadata(token_tx.txhash) self.assertEqual(token_metadata.token_txhash, token_tx.txhash) self.state.remove_token_metadata(token_tx) self.assertIsNone(self.state.get_token_metadata(token_tx.txhash))
def test_create_token_metadata(self): with set_data_dir('no_data'): with State() as state: alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_transaction = get_token_transaction(alice_xmss, bob_xmss) state.create_token_metadata(token_transaction) token_metadata = state.get_token_metadata(token_transaction.txhash) self.assertEqual(token_metadata.token_txhash, token_transaction.txhash) self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash)
def setUp(self): self.alice = get_alice_xmss() self.bob = get_bob_xmss() self.alice.set_ots_index(10) self.maxDiff = None self.params = { "message_hash": b'Test Message', "fee": 1, "xmss_pk": self.alice.pk }
def test_update_token_metadata(self): with set_data_dir('no_data'): with State() as state: alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() token_transaction = get_token_transaction(alice_xmss, bob_xmss) state.create_token_metadata(token_transaction) transfer_token_transaction = TransferTokenTransaction.create(addr_from=bob_xmss.get_address(), token_txhash=token_transaction.txhash, addr_to=alice_xmss.get_address(), amount=100000000, fee=1, xmss_pk=bob_xmss.pk(), xmss_ots_index=bob_xmss.get_index()) state.update_token_metadata(transfer_token_transaction) token_metadata = state.get_token_metadata(token_transaction.txhash) self.assertEqual(len(token_metadata.transfer_token_tx_hashes), 2) self.assertEqual(token_metadata.transfer_token_tx_hashes[0], token_transaction.txhash) self.assertEqual(token_metadata.transfer_token_tx_hashes[1], transfer_token_transaction.txhash)
def test_add_block(self): """ Testing add_block, with fork logic :return: """ with set_data_dir('no_data'): with State() as state: state.get_measurement = MagicMock(return_value=10000000) alice_xmss = get_alice_xmss() bob_xmss = get_bob_xmss() genesis_block = GenesisBlock() chain_manager = ChainManager(state) chain_manager.load(genesis_block) chain_manager._difficulty_tracker = Mock() dt = DifficultyTracker() tmp_difficulty = StringToUInt256('2') tmp_boundary = dt.get_boundary(tmp_difficulty) chain_manager._difficulty_tracker.get = MagicMock(return_value=(tmp_difficulty, tmp_boundary)) block = state.get_block(genesis_block.headerhash) self.assertIsNotNone(block) slave_tx = SlaveTransaction.create(addr_from=alice_xmss.get_address(), slave_pks=[bob_xmss.pk()], access_types=[0], fee=0, xmss_pk=alice_xmss.pk(), xmss_ots_index=alice_xmss.get_index()) slave_tx.sign(alice_xmss) slave_tx._data.nonce = 2 self.assertTrue(slave_tx.validate()) with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 # Very high to get an easy difficulty block_1 = Block.create(mining_nonce=10, block_number=1, prevblock_headerhash=genesis_block.headerhash, transactions=[slave_tx], signing_xmss=alice_xmss, master_address=alice_xmss.get_address(), nonce=1) while not chain_manager.validate_mining_nonce(block_1, False): block_1.set_mining_nonce(block_1.mining_nonce + 1) result = chain_manager.add_block(block_1) self.assertTrue(result) self.assertEqual(chain_manager.last_block, block_1) alice_state = chain_manager.get_address(alice_xmss.get_address()) self.assertEqual(len(alice_state.slave_pks_access_type), 1) self.assertTrue(str(bob_xmss.pk()) in alice_state.slave_pks_access_type) with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1715270948 # Very high to get an easy difficulty block = Block.create(mining_nonce=15, block_number=1, prevblock_headerhash=genesis_block.headerhash, transactions=[], signing_xmss=bob_xmss, master_address=bob_xmss.get_address(), nonce=1) while not chain_manager.validate_mining_nonce(block, False): block.set_mining_nonce(block.mining_nonce + 1) result = chain_manager.add_block(block) self.assertTrue(result) self.assertEqual(chain_manager.last_block, block_1) block = state.get_block(block.headerhash) self.assertIsNotNone(block) with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1815270948 # Very high to get an easy difficulty block_2 = Block.create(mining_nonce=15, block_number=2, prevblock_headerhash=block.headerhash, transactions=[], signing_xmss=bob_xmss, master_address=bob_xmss.get_address(), nonce=2) while not chain_manager.validate_mining_nonce(block_2, False): block_2.set_mining_nonce(block_2.mining_nonce + 1) result = chain_manager.add_block(block_2) self.assertTrue(result) self.assertEqual(chain_manager.last_block.block_number, block_2.block_number) self.assertEqual(chain_manager.last_block.to_json(), block_2.to_json())
def test_orphan_block(self): """ Testing add_block logic in case of orphan_blocks :return: """ with mock.patch('qrl.core.config.DevConfig') as devconfig: devconfig.genesis_difficulty = 2 devconfig.minimum_minting_delay = 10 with set_data_dir('no_data'): with State() as state: # FIXME: Move state to temporary directory state.get_measurement = MagicMock(return_value=10000000) genesis_block = GenesisBlock() chain_manager = ChainManager(state) chain_manager.load(genesis_block) chain_manager._difficulty_tracker = Mock() dt = DifficultyTracker() tmp_difficulty = StringToUInt256('2') tmp_boundary = dt.get_boundary(tmp_difficulty) chain_manager._difficulty_tracker.get = MagicMock(return_value=(tmp_difficulty, tmp_boundary)) block = state.get_block(genesis_block.headerhash) self.assertIsNotNone(block) alice_xmss = get_alice_xmss() with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 # Very high to get an easy difficulty block_1 = Block.create(mining_nonce=10, block_number=1, prevblock_headerhash=genesis_block.headerhash, transactions=[], signing_xmss=alice_xmss, master_address=alice_xmss.get_address(), nonce=1) block_1.set_mining_nonce(10) while not chain_manager.validate_mining_nonce(block_1, False): block_1.set_mining_nonce(block_1.mining_nonce + 1) result = chain_manager.add_block(block_1) self.assertTrue(result) self.assertEqual(chain_manager.last_block, block_1) bob_xmss = get_bob_xmss() with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 + devconfig.minimum_minting_delay * 2 block = Block.create(mining_nonce=18, block_number=1, prevblock_headerhash=genesis_block.headerhash, transactions=[], signing_xmss=bob_xmss, master_address=bob_xmss.get_address(), nonce=1) block.set_mining_nonce(18) while not chain_manager.validate_mining_nonce(block, False): block.set_mining_nonce(block.mining_nonce + 1) with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 + devconfig.minimum_minting_delay * 3 block_2 = Block.create(mining_nonce=17, block_number=2, prevblock_headerhash=block.headerhash, transactions=[], signing_xmss=bob_xmss, master_address=bob_xmss.get_address(), nonce=2) block_2.set_mining_nonce(17) result = chain_manager.add_block(block_2) self.assertTrue(result) result = chain_manager.add_block(block) self.assertTrue(result) block = state.get_block(block.headerhash) self.assertIsNotNone(block) self.assertEqual(chain_manager.last_block.block_number, block_1.block_number) self.assertEqual(chain_manager.last_block.headerhash, block_1.headerhash)