Exemple #1
0
    def test_add_2(self):
        destroy_state()
        with State() as state:
            with set_wallet_dir("test_wallet"):
                chain = Chain(state)
                buffered_chain = BufferedChain(chain)

                alice_xmss = get_alice_xmss()
                slave_xmss = XMSS(alice_xmss.height, alice_xmss.get_seed())
                staking_address = bytes(alice_xmss.get_address().encode())

                h0 = sha256(b'hashchain_seed')
                h1 = sha256(h0)

                with mocked_genesis() as custom_genesis:
                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(
                            address=alice_xmss.get_address(),
                            balance=700000000000000)
                    ])

                    res = buffered_chain.add_block(block=custom_genesis)
                    self.assertTrue(res)

                    stake_transaction = StakeTransaction.create(
                        activation_blocknumber=1,
                        xmss=alice_xmss,
                        slavePK=slave_xmss.pk(),
                        hashchain_terminator=h1)
                    vote = Vote.create(
                        addr_from=alice_xmss.get_address().encode(),
                        blocknumber=0,
                        headerhash=custom_genesis.headerhash,
                        xmss=slave_xmss)
                    vote.sign(slave_xmss)
                    buffered_chain.add_vote(vote)
                    vote_metadata = buffered_chain.get_consensus(0)

                    # FIXME: The test needs private access.. This is an API issue
                    stake_transaction._data.nonce = 1

                    stake_transaction.sign(alice_xmss)

                    chain.pstate.stake_validators_tracker.add_sv(
                        balance=700000000000000,
                        stake_txn=stake_transaction,
                        blocknumber=1)
                    sv = chain.pstate.stake_validators_tracker.sv_dict[
                        staking_address]
                    self.assertEqual(0, sv.nonce)

                    tmp_block = Block.create(
                        staking_address=bytes(
                            alice_xmss.get_address().encode()),
                        block_number=1,
                        reveal_hash=h0,
                        prevblock_headerhash=custom_genesis.headerhash,
                        transactions=[stake_transaction],
                        duplicate_transactions=OrderedDict(),
                        vote=vote_metadata,
                        signing_xmss=alice_xmss,
                        nonce=1)

                    res = buffered_chain.add_block(block=tmp_block)
                    self.assertTrue(res)
Exemple #2
0
    def test_add_3(self):
        destroy_state()
        with State() as state:
            with set_wallet_dir("test_wallet"):
                chain = Chain(state)
                buffered_chain = BufferedChain(chain)

                alice_xmss = get_alice_xmss()
                slave_xmss = XMSS(alice_xmss.height, alice_xmss.get_seed())

                staking_address = bytes(alice_xmss.get_address().encode())

                # FIXME: Replace this with a call to create a hash_chain
                h0 = sha256(b'hashchain_seed')
                h1 = sha256(h0)
                h2 = sha256(h1)
                h3 = sha256(h2)

                with mocked_genesis() as custom_genesis:
                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(
                            address=alice_xmss.get_address(),
                            balance=700000000000000)
                    ])

                    res = buffered_chain.add_block(block=GenesisBlock())
                    self.assertTrue(res)
                    stake_transaction = StakeTransaction.create(
                        activation_blocknumber=1,
                        xmss=alice_xmss,
                        slavePK=slave_xmss.pk(),
                        hashchain_terminator=h3)
                    stake_transaction._data.nonce = 1  # FIXME: The test needs private access.. This is an API issue
                    stake_transaction.sign(alice_xmss)

                    vote = Vote.create(
                        addr_from=alice_xmss.get_address().encode(),
                        blocknumber=0,
                        headerhash=GenesisBlock().headerhash,
                        xmss=slave_xmss)
                    vote.sign(slave_xmss)
                    buffered_chain.add_vote(vote)
                    vote_metadata = buffered_chain.get_consensus(0)

                    chain.pstate.stake_validators_tracker.add_sv(
                        balance=700000000000000,
                        stake_txn=stake_transaction,
                        blocknumber=1)

                    sv = chain.pstate.stake_validators_tracker.sv_dict[
                        staking_address]
                    self.assertEqual(0, sv.nonce)

                    tmp_block1 = Block.create(
                        staking_address=staking_address,
                        block_number=1,
                        reveal_hash=h2,
                        prevblock_headerhash=GenesisBlock().headerhash,
                        transactions=[stake_transaction],
                        duplicate_transactions=OrderedDict(),
                        vote=vote_metadata,
                        signing_xmss=slave_xmss,
                        nonce=1)

                    res = buffered_chain.add_block(block=tmp_block1)
                    self.assertTrue(res)

                    # Need to move forward the time to align with block times
                    with mock.patch('qrl.core.ntp.getTime') as time_mock:
                        time_mock.return_value = tmp_block1.timestamp + config.dev.minimum_minting_delay

                        vote = Vote.create(
                            addr_from=alice_xmss.get_address().encode(),
                            blocknumber=1,
                            headerhash=tmp_block1.headerhash,
                            xmss=slave_xmss)
                        vote.sign(slave_xmss)
                        buffered_chain.add_vote(vote)
                        vote_metadata = buffered_chain.get_consensus(1)

                        tmp_block2 = Block.create(
                            staking_address=staking_address,
                            block_number=2,
                            reveal_hash=h1,
                            prevblock_headerhash=tmp_block1.headerhash,
                            transactions=[],
                            duplicate_transactions=OrderedDict(),
                            vote=vote_metadata,
                            signing_xmss=slave_xmss,
                            nonce=2)

                    res = buffered_chain.add_block(block=tmp_block2)
                    self.assertTrue(res)

                    # Need to move forward the time to align with block times
                    with mock.patch('qrl.core.ntp.getTime') as time_mock:
                        time_mock.return_value = tmp_block2.timestamp + config.dev.minimum_minting_delay

                        vote = Vote.create(
                            addr_from=alice_xmss.get_address().encode(),
                            blocknumber=2,
                            headerhash=tmp_block2.headerhash,
                            xmss=slave_xmss)
                        vote.sign(slave_xmss)
                        buffered_chain.add_vote(vote)
                        vote_metadata = buffered_chain.get_consensus(2)

                        tmp_block3 = Block.create(
                            staking_address=staking_address,
                            block_number=3,
                            reveal_hash=h0,
                            prevblock_headerhash=tmp_block2.headerhash,
                            transactions=[],
                            duplicate_transactions=OrderedDict(),
                            vote=vote_metadata,
                            signing_xmss=slave_xmss,
                            nonce=3)

                    res = buffered_chain.add_block(block=tmp_block3)
                    self.assertTrue(res)
Exemple #3
0
    def test_add_4(self):
        destroy_state()
        with State() as state:
            with set_wallet_dir("test_wallet"):
                chain = Chain(state)
                buffered_chain = BufferedChain(chain)

                alice_xmss = get_alice_xmss()
                slave_xmss = XMSS(alice_xmss.height, alice_xmss.get_seed())
                random_xmss1 = get_random_xmss()
                random_xmss2 = get_random_xmss()
                staking_address = bytes(alice_xmss.get_address().encode())

                # FIXME: Replace this with a call to create a hash_chain
                h0 = sha256(b'hashchain_seed')
                h1 = sha256(h0)
                h2 = sha256(h1)
                h3 = sha256(h2)
                h4 = sha256(h3)

                with mocked_genesis() as custom_genesis:
                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(
                            address=alice_xmss.get_address(),
                            balance=700000000000000)
                    ])

                    res = buffered_chain.add_block(block=GenesisBlock())
                    self.assertTrue(res)
                    stake_transaction = StakeTransaction.create(
                        activation_blocknumber=1,
                        xmss=alice_xmss,
                        slavePK=slave_xmss.pk(),
                        hashchain_terminator=h4)
                    stake_transaction._data.nonce = 1  # FIXME: The test needs private access.. This is an API issue
                    stake_transaction.sign(alice_xmss)

                    vote = Vote.create(
                        addr_from=alice_xmss.get_address().encode(),
                        blocknumber=0,
                        headerhash=GenesisBlock().headerhash,
                        xmss=slave_xmss)
                    vote.sign(slave_xmss)
                    buffered_chain.add_vote(vote)
                    vote_metadata = buffered_chain.get_consensus(0)

                    chain.pstate.stake_validators_tracker.add_sv(
                        balance=700000000000000,
                        stake_txn=stake_transaction,
                        blocknumber=1)

                    sv = chain.pstate.stake_validators_tracker.sv_dict[
                        staking_address]
                    self.assertEqual(0, sv.nonce)

                    # Token Transaction to create a token for test
                    token_transaction = get_token_transaction(
                        random_xmss1, random_xmss2)
                    token_transaction._data.nonce = 1
                    token_transaction.sign(random_xmss1)

                    # Transfer Token Transaction
                    transfer_token1 = TransferTokenTransaction.create(
                        addr_from=random_xmss1.get_address().encode(),
                        token_txhash=token_transaction.txhash,
                        addr_to=alice_xmss.get_address().encode(),
                        amount=100000000,
                        fee=1,
                        xmss_pk=random_xmss1.pk(),
                        xmss_ots_index=random_xmss1.get_index())
                    transfer_token1._data.nonce = 2
                    transfer_token1.sign(random_xmss1)

                    transfer_token2 = TransferTokenTransaction.create(
                        addr_from=random_xmss2.get_address().encode(),
                        token_txhash=token_transaction.txhash,
                        addr_to=alice_xmss.get_address().encode(),
                        amount=200000000,
                        fee=1,
                        xmss_pk=random_xmss2.pk(),
                        xmss_ots_index=random_xmss2.get_index())
                    transfer_token2._data.nonce = 1
                    transfer_token2.sign(random_xmss2)

                    # Transfer Coin Transaction
                    transfer_transaction = TransferTransaction.create(
                        addr_from=random_xmss1.get_address().encode(),
                        addr_to=random_xmss2.get_address().encode(),
                        amount=10,
                        fee=1,
                        xmss_pk=random_xmss1.pk(),
                        xmss_ots_index=random_xmss1.get_index())
                    transfer_transaction._data.nonce = 3
                    transfer_transaction.sign(random_xmss1)

                    tmp_block1 = Block.create(
                        staking_address=staking_address,
                        block_number=1,
                        reveal_hash=h3,
                        prevblock_headerhash=GenesisBlock().headerhash,
                        transactions=[stake_transaction, token_transaction],
                        duplicate_transactions=OrderedDict(),
                        vote=vote_metadata,
                        signing_xmss=slave_xmss,
                        nonce=1)

                    res = buffered_chain.add_block(block=tmp_block1)
                    self.assertTrue(res)

                    # Need to move forward the time to align with block times
                    with mock.patch('qrl.core.ntp.getTime') as time_mock:
                        time_mock.return_value = tmp_block1.timestamp + config.dev.minimum_minting_delay

                        vote = Vote.create(
                            addr_from=alice_xmss.get_address().encode(),
                            blocknumber=1,
                            headerhash=tmp_block1.headerhash,
                            xmss=slave_xmss)
                        vote.sign(slave_xmss)
                        buffered_chain.add_vote(vote)
                        vote_metadata = buffered_chain.get_consensus(1)

                        tmp_block2 = Block.create(
                            staking_address=staking_address,
                            block_number=2,
                            reveal_hash=h2,
                            prevblock_headerhash=tmp_block1.headerhash,
                            transactions=[
                                transfer_token1, transfer_token2,
                                transfer_transaction
                            ],
                            duplicate_transactions=OrderedDict(),
                            vote=vote_metadata,
                            signing_xmss=slave_xmss,
                            nonce=2)

                    res = buffered_chain.add_block(block=tmp_block2)
                    self.assertTrue(res)

                    # Need to move forward the time to align with block times
                    with mock.patch('qrl.core.ntp.getTime') as time_mock:
                        time_mock.return_value = tmp_block2.timestamp + config.dev.minimum_minting_delay

                        vote = Vote.create(
                            addr_from=alice_xmss.get_address().encode(),
                            blocknumber=2,
                            headerhash=tmp_block2.headerhash,
                            xmss=slave_xmss)
                        vote.sign(slave_xmss)
                        buffered_chain.add_vote(vote)
                        vote_metadata = buffered_chain.get_consensus(2)

                        tmp_block3 = Block.create(
                            staking_address=staking_address,
                            block_number=3,
                            reveal_hash=h1,
                            prevblock_headerhash=tmp_block2.headerhash,
                            transactions=[],
                            duplicate_transactions=OrderedDict(),
                            vote=vote_metadata,
                            signing_xmss=slave_xmss,
                            nonce=3)

                    res = buffered_chain.add_block(block=tmp_block3)
                    self.assertTrue(res)

                    chain = buffered_chain._chain
                    random_xmss1_state = chain.pstate._get_address_state(
                        random_xmss1.get_address().encode())
                    random_xmss2_state = chain.pstate._get_address_state(
                        random_xmss2.get_address().encode())

                    self.assertEqual(
                        random_xmss1_state.tokens[bin2hstr(
                            token_transaction.txhash).encode()], 400000000)
                    self.assertEqual(
                        random_xmss2_state.tokens[bin2hstr(
                            token_transaction.txhash).encode()], 200000000)

                    # Need to move forward the time to align with block times
                    with mock.patch('qrl.core.ntp.getTime') as time_mock:
                        time_mock.return_value = tmp_block3.timestamp + config.dev.minimum_minting_delay

                        vote = Vote.create(
                            addr_from=alice_xmss.get_address().encode(),
                            blocknumber=3,
                            headerhash=tmp_block3.headerhash,
                            xmss=slave_xmss)
                        vote.sign(slave_xmss)
                        buffered_chain.add_vote(vote)
                        vote_metadata = buffered_chain.get_consensus(3)

                        tmp_block4 = Block.create(
                            staking_address=staking_address,
                            block_number=4,
                            reveal_hash=h0,
                            prevblock_headerhash=tmp_block3.headerhash,
                            transactions=[],
                            duplicate_transactions=OrderedDict(),
                            vote=vote_metadata,
                            signing_xmss=slave_xmss,
                            nonce=4)

                    res = buffered_chain.add_block(block=tmp_block4)
                    self.assertTrue(res)

                    token_metadata = buffered_chain.get_token_metadata(
                        token_transaction.txhash)

                    self.assertEqual(token_metadata.token_txhash,
                                     token_transaction.txhash)
                    self.assertEqual(
                        len(token_metadata.transfer_token_tx_hashes), 3)
                    self.assertEqual(
                        token_metadata.transfer_token_tx_hashes[0],
                        token_transaction.txhash)

                    random_xmss1_state = chain.pstate._get_address_state(
                        random_xmss1.get_address().encode())
                    random_xmss2_state = chain.pstate._get_address_state(
                        random_xmss2.get_address().encode())
                    alice_state = chain.pstate._get_address_state(
                        alice_xmss.get_address().encode())

                    self.assertEqual(
                        random_xmss1_state.tokens[bin2hstr(
                            token_transaction.txhash).encode()], 300000000)
                    self.assertEqual(
                        random_xmss2_state.tokens[bin2hstr(
                            token_transaction.txhash).encode()], 0)
                    self.assertEqual(
                        alice_state.tokens[bin2hstr(
                            token_transaction.txhash).encode()], 300000000)
                    self.assertEqual(random_xmss1_state.balance,
                                     config.dev.default_account_balance - 13)
                    self.assertEqual(random_xmss2_state.balance,
                                     config.dev.default_account_balance + 9)
Exemple #4
0
    def test_add_4(self, mock_difficulty_tracker_get):
        with set_qrl_dir('wallet_ver1'):
            with State() as state:
                with mocked_genesis() as custom_genesis:
                    chain_manager = ChainManager(state)

                    chain_manager._difficulty_tracker = Mock()
                    tmp_difficulty = StringToUInt256('2')
                    tmp_target = DifficultyTracker.get_target(tmp_difficulty)
                    mock_difficulty_tracker_get.return_value = [
                        tmp_difficulty, tmp_target
                    ]

                    alice_xmss = get_alice_xmss()
                    slave_xmss = XMSS(
                        XmssFast(alice_xmss.seed, alice_xmss.height))
                    random_xmss1 = get_random_xmss()
                    random_kyber1 = Kyber()
                    random_dilithium1 = Dilithium()
                    random_xmss2 = get_random_xmss()
                    random_kyber2 = Kyber()
                    random_dilithium2 = Dilithium()

                    message = b'Hello World How are you?'
                    prf512_seed = b'10192'

                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(address=random_xmss1.address,
                                               balance=65000000000000000)
                    ])
                    custom_genesis.genesis_balance.extend([
                        qrl_pb2.GenesisBalance(address=random_xmss2.address,
                                               balance=65000000000000000)
                    ])
                    chain_manager.load(custom_genesis)

                    with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
                        time_mock.return_value = 1615270948

                        lattice_public_key_txn = LatticePublicKey.create(
                            fee=1,
                            kyber_pk=random_kyber1.getPK(),
                            dilithium_pk=random_dilithium1.getPK(),
                            xmss_pk=random_xmss1.pk)
                        lattice_public_key_txn._data.nonce = 1
                        lattice_public_key_txn.sign(random_xmss1)
                        genesis_block = GenesisBlock()
                        tmp_block1 = Block.create(
                            block_number=1,
                            prev_block_headerhash=genesis_block.headerhash,
                            prev_block_timestamp=genesis_block.timestamp,
                            transactions=[lattice_public_key_txn],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block1.blockheader, False):
                            tmp_block1.set_nonces(tmp_block1.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block1)
                        self.assertTrue(res)

                        # Need to move forward the time to align with block times
                        time_mock.return_value += config.dev.minimum_minting_delay * 2

                        encrypted_eph_message = create_ephemeral_channel(
                            msg_id=lattice_public_key_txn.txhash,
                            ttl=time_mock.return_value,
                            ttr=0,
                            addr_from=random_xmss2.address,
                            kyber_pk=random_kyber2.getPK(),
                            kyber_sk=random_kyber2.getSK(),
                            receiver_kyber_pk=random_kyber1.getPK(),
                            dilithium_pk=random_dilithium2.getPK(),
                            dilithium_sk=random_dilithium2.getSK(),
                            prf512_seed=prf512_seed,
                            data=message,
                            nonce=1)

                        chain_manager.state.update_ephemeral(
                            encrypted_eph_message)
                        eph_metadata = chain_manager.state.get_ephemeral_metadata(
                            lattice_public_key_txn.txhash)

                        # Decrypting Payload

                        encrypted_eph_message = eph_metadata.encrypted_ephemeral_message_list[
                            0]
                        encrypted_payload = encrypted_eph_message.payload

                        random_kyber1.kem_decode(
                            encrypted_eph_message.channel.enc_aes256_symkey)
                        aes_key = bytes(random_kyber1.getMyKey())
                        myAES = AES(aes_key)
                        decrypted_payload = myAES.decrypt(encrypted_payload)
                        ephemeral_channel_payload = EphemeralChannelPayload.from_json(
                            decrypted_payload)

                        self.assertEqual(ephemeral_channel_payload.prf512_seed,
                                         b'10192')
                        self.assertEqual(ephemeral_channel_payload.data,
                                         b'Hello World How are you?')

                        # TODO (cyyber): Add Ephemeral Testing code using Naive RNG

                        tmp_block2 = Block.create(
                            block_number=2,
                            prev_block_headerhash=tmp_block1.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block2.blockheader, False):
                            tmp_block2.set_nonces(tmp_block2.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block2)
                        self.assertTrue(res)

                        # Need to move forward the time to align with block times
                        time_mock.return_value += config.dev.minimum_minting_delay * 2

                        tmp_block3 = Block.create(
                            block_number=3,
                            prev_block_headerhash=tmp_block2.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block3.blockheader, False):
                            tmp_block3.set_nonces(tmp_block3.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block3)
                        self.assertTrue(res)

                        time_mock.return_value += config.dev.minimum_minting_delay

                        tmp_block4 = Block.create(
                            block_number=4,
                            prev_block_headerhash=tmp_block3.headerhash,
                            prev_block_timestamp=tmp_block1.timestamp,
                            transactions=[],
                            miner_address=slave_xmss.address)

                        #  Mine the nonce
                        while not PoWValidator().validate_mining_nonce(
                                state, tmp_block4.blockheader, False):
                            tmp_block4.set_nonces(tmp_block4.mining_nonce + 1,
                                                  0)

                        res = chain_manager.add_block(block=tmp_block4)
                        self.assertTrue(res)

                        address_state = chain_manager.get_address(
                            random_xmss1.address)

                        self.assertEqual(
                            address_state.latticePK_list[0].kyber_pk,
                            lattice_public_key_txn.kyber_pk)
                        self.assertEqual(
                            address_state.latticePK_list[0].dilithium_pk,
                            lattice_public_key_txn.dilithium_pk)

                        self.assertEqual(address_state.address,
                                         lattice_public_key_txn.addr_from)

                        random_xmss1_state = chain_manager.get_address(
                            random_xmss1.address)

                        self.assertEqual(64999999999999999,
                                         random_xmss1_state.balance)