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)
示例#2
0
    def test_getHeight(self):
        with set_xrd_dir('no_data'):
            db_state = State()
            alice_xmss = get_alice_xmss()
            optimized_address_state = OptimizedAddressState.create(address=alice_xmss.address,
                                                                   nonce=25,
                                                                   balance=10,
                                                                   ots_bitfield_used_page=0,
                                                                   transaction_hash_count=0,
                                                                   tokens_count=0,
                                                                   lattice_pk_count=0,
                                                                   slaves_count=0,
                                                                   multi_sig_address_count=0)
            addresses_state = {optimized_address_state.address: optimized_address_state}
            AddressState.put_addresses_state(db_state, addresses_state)

            p2p_factory = Mock(spec=P2PFactory)
            chain_manager = ChainManager(db_state)
            chain_manager._last_block = Mock()
            chain_manager._last_block.block_number = 100

            xrdnode = xrdNode(mining_address=b'')
            xrdnode.set_chain_manager(chain_manager)
            xrdnode._p2pfactory = p2p_factory
            xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1']

            service = PublicAPIService(xrdnode)
            context = Mock(spec=ServicerContext)
            request = xrd_pb2.GetHeightReq()
            response = service.GetHeight(request=request, context=context)
            self.assertEqual(response.height, 100)
示例#3
0
 def test_put_addresses_state(self):
     with set_xrd_dir('no_data'):
         with State() as state:
             alice_xmss = get_alice_xmss()
             alice_state = OptimizedAddressState.get_default(
                 alice_xmss.address)
             addresses_state = {
                 alice_state.address: alice_state,
                 b'test1': OptimizedAddressState.get_default(b'test1')
             }
             AddressState.put_addresses_state(state, addresses_state, None)
             alice_state2 = OptimizedAddressState.get_optimized_address_state(
                 state, alice_xmss.address)
             self.assertEqual(alice_state.serialize(),
                              alice_state2.serialize())
             test_state = OptimizedAddressState.get_optimized_address_state(
                 state, b'test1')
             self.assertEqual(
                 test_state.serialize(),
                 OptimizedAddressState.get_default(b'test1').serialize())
示例#4
0
    def test_getOTS(self):
        with set_xrd_dir('no_data'):
            db_state = State()
            alice_xmss = get_alice_xmss()
            optimized_address_state = OptimizedAddressState.create(address=alice_xmss.address,
                                                                   nonce=25,
                                                                   balance=10,
                                                                   ots_bitfield_used_page=0,
                                                                   transaction_hash_count=0,
                                                                   tokens_count=0,
                                                                   lattice_pk_count=0,
                                                                   slaves_count=0,
                                                                   multi_sig_address_count=0)
            addresses_state = {optimized_address_state.address: optimized_address_state}
            AddressState.put_addresses_state(db_state, addresses_state)

            p2p_factory = Mock(spec=P2PFactory)
            chain_manager = ChainManager(db_state)

            xrdnode = xrdNode(mining_address=b'')
            xrdnode.set_chain_manager(chain_manager)
            xrdnode._p2pfactory = p2p_factory
            xrdnode._peer_addresses = ['127.0.0.1', '192.168.1.1']

            service = PublicAPIService(xrdnode)

            context = Mock(spec=ServicerContext)
            request = xrd_pb2.GetOTSReq(address=alice_xmss.address)
            response = service.GetOTS(request=request, context=context)
            context.set_code.assert_not_called()

            self.assertEqual(0, response.next_unused_ots_index)

            paginated_bitfield = PaginatedBitfield(True, db_state._db)
            paginated_bitfield.set_ots_key(addresses_state, optimized_address_state.address, 0)
            paginated_bitfield.put_addresses_bitfield(None)

            response = service.GetOTS(request=request, context=context)
            context.set_code.assert_not_called()

            self.assertEqual(1, response.next_unused_ots_index)
    def test_get_optimized_address_state(self):
        alice_xmss = get_alice_xmss()

        alice_address = alice_xmss.address
        address_state = OptimizedAddressState.get_optimized_address_state(self.state, alice_address)
        self.assertTrue(isinstance(address_state.address, bytes))

        alice_address = bytearray(alice_xmss.address)
        with self.assertRaises(TypeError):
            OptimizedAddressState.get_optimized_address_state(self.state, alice_address)

        alice_address = alice_xmss.address
        address_state = OptimizedAddressState.get_optimized_address_state(self.state, alice_address)
        addresses_state = {
            alice_address: address_state
        }
        self.assertTrue(isinstance(address_state.address, bytes))
        AddressState.put_addresses_state(self.state, addresses_state)

        address_state = OptimizedAddressState.get_optimized_address_state(self.state, alice_address)
        self.assertTrue(isinstance(address_state.address, bytes))
示例#6
0
    def test_revert_multi_sig_create_txn(self):
        addresses_state = {
            self.alice.address:
            OptimizedAddressState.get_default(self.alice.address),
            self.bob.address:
            OptimizedAddressState.get_default(self.bob.address),
            self.random.address:
            OptimizedAddressState.get_default(self.random.address),
            self.random_signer.address:
            OptimizedAddressState.get_default(self.random_signer.address),
        }
        addresses_state[self.random_signer.address].pbdata.balance = 200
        tx = MultiSigCreate.create(self.signatories, self.weights,
                                   self.threshold, 1, self.random_signer.pk)
        tx.sign(self.random_signer)

        state_container = StateContainer(addresses_state=addresses_state,
                                         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=100,
                                         current_dev_config=config.dev,
                                         write_access=True,
                                         my_db=self.state._db,
                                         batch=None)
        self.assertFalse(
            state_container.paginated_bitfield.load_bitfield_and_ots_key_reuse(
                self.random_signer.address, tx.ots_key))

        tx.apply(self.state, state_container)

        self.assertEqual(200 - tx.fee,
                         addresses_state[self.random_signer.address].balance)

        storage_key = state_container.paginated_tx_hash.generate_key(
            self.random_signer.address, 1)
        self.assertEqual(
            [tx.txhash],
            state_container.paginated_tx_hash.key_value[storage_key])
        for signatory_address in self.signatories:
            storage_key = state_container.paginated_tx_hash.generate_key(
                signatory_address, 1)
            self.assertEqual(
                [tx.txhash],
                state_container.paginated_tx_hash.key_value[storage_key])

        self.assertTrue(
            state_container.paginated_bitfield.load_bitfield_and_ots_key_reuse(
                self.random_signer.address, tx.ots_key))
        AddressState.put_addresses_state(self.state, addresses_state)
        state_container.paginated_multisig_address.put_paginated_data(None)
        multi_sig_addresses_state = MultiSigAddressState.get_multi_sig_address_state_by_address(
            self.state._db,
            MultiSigAddressState.generate_multi_sig_address(tx.txhash))

        self.assertEqual(self.signatories,
                         multi_sig_addresses_state.signatories)
        self.assertEqual(self.weights, multi_sig_addresses_state.weights)
        self.assertEqual(self.threshold, multi_sig_addresses_state.threshold)

        for signatory_address in self.signatories:
            multi_sig_addresses = state_container.paginated_multisig_address.get_paginated_data(
                signatory_address, 1)
            self.assertEqual(len(multi_sig_addresses), 1)

        tx.revert(self.state, state_container)
        state_container.paginated_multisig_address.put_paginated_data(None)
        self.assertFalse(
            state_container.paginated_bitfield.load_bitfield_and_ots_key_reuse(
                self.random_signer.address, tx.ots_key))
        self.assertIsNone(
            MultiSigAddressState.get_multi_sig_address_state_by_address(
                self.state._db,
                MultiSigAddressState.generate_multi_sig_address(tx.txhash)))

        for signatory_address in self.signatories:
            multi_sig_addresses = state_container.paginated_multisig_address.get_paginated_data(
                signatory_address, 1)
            self.assertEqual(len(multi_sig_addresses), 0)