def test_last_block(self): with set_wallet_dir("test_wallet"): with State() as state: self.assertIsNotNone(state) chain = Chain(state) alice_xmss = get_alice_xmss() staking_address = bytes(alice_xmss.get_address().encode()) address_state_dict = dict() address_state_dict[staking_address] = AddressState.create(address=staking_address, nonce=0, balance=100, pubhashes=[]) tmp_block1 = Block.create(staking_address=staking_address, block_number=0, reveal_hash=bytes(), prevblock_headerhash=bytes(), transactions=[], duplicate_transactions=OrderedDict(), vote=VoteMetadata(), signing_xmss=alice_xmss, nonce=address_state_dict[staking_address].nonce + 1) res = chain.add_block(tmp_block1, address_state_dict, None) address_state_dict[staking_address].increase_nonce() address_state_dict[staking_address].balance += tmp_block1.block_reward self.assertTrue(res) self.assertEqual(0, chain.height) # FIXME: wrong name, it is not height but max_index last_block = chain.get_last_block() self.assertEqual(tmp_block1, last_block)
def test_add_vote1(self): alice_xmss = get_alice_xmss() slave_xmss = XMSS(alice_xmss.height, alice_xmss.get_seed()) headerhash = b'ffff' vote = Vote.create(addr_from=alice_xmss.get_address().encode(), blocknumber=0, headerhash=headerhash, xmss=slave_xmss) vote.sign(slave_xmss) stake_amount = 101.5012 vote_metadata = VoteMetadata() self.assertNotIn(vote.txfrom, vote_metadata.stake_validator_vote) vote_metadata.add_vote(vote=vote, stake_amount=stake_amount) self.assertIn(vote.txfrom, vote_metadata.stake_validator_vote) self.assertEqual(stake_amount, vote_metadata.total_stake_amount)
def test_add_vote2(self): validator_xmss1 = get_alice_xmss() slave_xmss1 = XMSS(validator_xmss1.height, validator_xmss1.get_seed()) stake_amount1 = 101.5012 headerhash1 = b'ffff' vote1 = Vote.create(addr_from=validator_xmss1.get_address().encode(), blocknumber=0, headerhash=headerhash1, xmss=slave_xmss1) vote1.sign(slave_xmss1) vote_metadata = VoteMetadata() vote_metadata.add_vote(vote=vote1, stake_amount=stake_amount1) self.assertIn(vote1.txfrom, vote_metadata.stake_validator_vote) validator_xmss2 = get_random_xmss() slave_xmss2 = XMSS(validator_xmss2.height, validator_xmss2.get_seed()) stake_amount2 = 10000 headerhash2 = b'ffff' vote2 = Vote.create(addr_from=validator_xmss2.get_address().encode(), blocknumber=0, headerhash=headerhash2, xmss=slave_xmss2) vote2.sign(slave_xmss2) vote_metadata.add_vote(vote=vote2, stake_amount=stake_amount2) self.assertIn(vote2.txfrom, vote_metadata.stake_validator_vote) total_stake_amount = stake_amount1 + stake_amount2 self.assertEqual(total_stake_amount, vote_metadata.total_stake_amount)
def test_add_many_and_save(self): with set_wallet_dir("test_wallet"): with State() as state: self.assertIsNotNone(state) chain = Chain(state) alice_xmss = get_alice_xmss() staking_address = bytes(alice_xmss.get_address().encode()) with patch('qrl.core.config.dev.disk_writes_after_x_blocks'): qrl.core.config.dev.disk_writes_after_x_blocks = 4 prev = bytes() address_state_dict = dict() address_state_dict[staking_address] = AddressState.create( address=staking_address, nonce=0, balance=100, pubhashes=[]) for i in range(10): tmp_block1 = Block.create( staking_address=staking_address, block_number=i, reveal_hash=bytes(), prevblock_headerhash=prev, transactions=[], duplicate_transactions=OrderedDict(), vote=VoteMetadata(), signing_xmss=alice_xmss, nonce=address_state_dict[staking_address].nonce + 1) prev = tmp_block1.headerhash res = chain.add_block(tmp_block1, address_state_dict, StakeValidatorsTracker(), b'1001', alice_xmss) address_state_dict[staking_address].increase_nonce() address_state_dict[ staking_address].balance += tmp_block1.block_reward self.assertEqual( i, chain.height ) # FIXME: wrong name, it is not height but max_index self.assertTrue(res) print(qrl.core.config.dev.disk_writes_after_x_blocks)
def test_getLatestData(self): blocks = [] txs = [] for i in range(1, 4): for j in range(1, 3): txs.append( TransferTransaction.create(addr_from=qrladdress('source'), addr_to=qrladdress('dest'), amount=i * 100 + j, fee=j, xmss_pk=get_alice_xmss().pk(), xmss_ots_index=i)) blocks.append( Block.create(staking_address=qrladdress('staking_addr'), block_number=i, reveal_hash=sha256(b'reveal'), prevblock_headerhash=sha256(b'reveal'), transactions=txs, duplicate_transactions=OrderedDict(), vote=VoteMetadata(), signing_xmss=get_alice_xmss(), nonce=i)) txpool = [] for j in range(10, 15): txpool.append( TransferTransaction.create(addr_from=qrladdress('source'), addr_to=qrladdress('dest'), amount=1000 + j, fee=j, xmss_pk=get_alice_xmss().pk(), xmss_ots_index=j)) db_state = Mock(spec=State) p2p_factory = Mock(spec=P2PFactory) buffered_chain = Mock(spec=BufferedChain) buffered_chain.tx_pool = Mock() buffered_chain.tx_pool.transaction_pool = txpool buffered_chain.get_block = Mock() buffered_chain.get_block.side_effect = blocks buffered_chain.height = len(blocks) buffered_chain._chain = Mock() buffered_chain._chain.blockchain = blocks qrlnode = QRLNode(db_state) qrlnode.set_p2pfactory(p2p_factory) qrlnode.set_chain(buffered_chain) service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetLatestDataReq(filter=qrl_pb2.GetLatestDataReq.ALL, offset=1, quantity=3) response = service.GetLatestData(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() # Verify blockheaders self.assertEqual(2, len(response.blockheaders)) self.assertEqual(1, response.blockheaders[0].header.block_number) self.assertEqual(2, response.blockheaders[1].header.block_number) # Verify transactions self.assertEqual(3, len(response.transactions)) self.assertEqual(1, response.transactions[0].transfer.fee) self.assertEqual(2, response.transactions[1].transfer.fee) self.assertEqual(1, response.transactions[2].transfer.fee) # 302 should have been skipped self.assertEqual(301, response.transactions[0].transfer.amount) self.assertEqual(202, response.transactions[1].transfer.amount) self.assertEqual(201, response.transactions[2].transfer.amount) # Verify transactions_unconfirmed self.assertEqual(3, len(response.transactions_unconfirmed)) self.assertEqual(1013, response.transactions_unconfirmed[0].transfer.amount) self.assertEqual(1012, response.transactions_unconfirmed[1].transfer.amount) self.assertEqual(1011, response.transactions_unconfirmed[2].transfer.amount)
def test_getObject(self): SOME_ODD_HASH = sha256(b'this should not be found') SOME_ADDR1 = b'Q' + sha256(b'address1') SOME_ADDR2 = b'Q' + sha256(b'address2') db_state = Mock(spec=State) p2p_factory = Mock(spec=P2PFactory) buffered_chain = Mock(spec=BufferedChain) buffered_chain.tx_pool = Mock() buffered_chain.tx_pool.transaction_pool = [] qrlnode = QRLNode(db_state) qrlnode.set_p2pfactory(p2p_factory) qrlnode.set_chain(buffered_chain) qrlnode._peer_addresses = ['127.0.0.1', '192.168.1.1'] service = PublicAPIService(qrlnode) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() context.set_details.assert_not_called() self.assertFalse(response.found) # Find an address db_state.get_address = MagicMock(return_value=AddressState.create( address=SOME_ADDR1, nonce=25, balance=10, pubhashes=[sha256(b'a'), sha256(b'b')], tokens=dict())) db_state.get_address_tx_hashes = MagicMock( return_value=[sha256(b'0'), sha256(b'1')]) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = SOME_ODD_HASH response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertFalse(response.found) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = SOME_ADDR1 response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.address_state) self.assertEqual(SOME_ADDR1, response.address_state.address) self.assertEqual(25, response.address_state.nonce) self.assertEqual(10, response.address_state.balance) self.assertEqual([sha256(b'a'), sha256(b'b')], response.address_state.pubhashes) self.assertEqual([sha256(b'0'), sha256(b'1')], response.address_state.transaction_hashes) # Find a transaction db_state.address_used = MagicMock(return_value=False) tx1 = TransferTransaction.create(addr_from=SOME_ADDR1, addr_to=SOME_ADDR2, amount=125, fee=19, xmss_pk=sha256(b'pk'), xmss_ots_index=13) buffered_chain.tx_pool.transaction_pool = [tx1] context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = tx1.txhash response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.transaction) self.assertEqual(qrl_pb2.Transaction.TRANSFER, response.transaction.type) self.assertEqual(SOME_ADDR1, response.transaction.addr_from) self.assertEqual(sha256(b'pk'), response.transaction.public_key) self.assertEqual(tx1.txhash, response.transaction.transaction_hash) self.assertEqual(13, response.transaction.ots_key) self.assertEqual(b'', response.transaction.signature) self.assertEqual(SOME_ADDR2, response.transaction.transfer.addr_to) self.assertEqual(125, response.transaction.transfer.amount) self.assertEqual(19, response.transaction.transfer.fee) # Find a block buffered_chain.get_block = MagicMock(return_value=Block.create( staking_address=qrladdress('staking_addr'), block_number=1, reveal_hash=sha256(b'reveal'), prevblock_headerhash=sha256(b'reveal'), transactions=[], duplicate_transactions=OrderedDict(), vote=VoteMetadata(), signing_xmss=get_alice_xmss(), nonce=1)) context = Mock(spec=ServicerContext) request = qrl_pb2.GetObjectReq() request.query = bytes(str2bin('1')) response = service.GetObject(request=request, context=context) context.set_code.assert_not_called() self.assertTrue(response.found) self.assertIsNotNone(response.block) self.assertEqual(1, response.block.header.block_number)