def test_consensus_store_set_get(self, mock_lmdb):
        """Verify that externally visible state (len, etc.) of the consensus
        state store after set is expected.  Verify that retrieving a
        previously set consensus state object results in the same values
        set.
        """
        # Make LMDB return empty dict
        my_dict = {}
        mock_lmdb.return_value = my_dict

        store = \
            consensus_state_store.ConsensusStateStore(
                data_dir=tempfile.gettempdir(),
                validator_id='0123456789abcdef')

        # Verify the length is zero and doesn't contain key
        self.assertEqual(len(store), 0)
        self.assertTrue('key' not in store)

        # Store consensus state
        state = consensus_state.ConsensusState()
        state.total_block_claim_count = 2
        state.set_validator_state(
            validator_id='Bond, James Bond',
            validator_state=consensus_state.ValidatorState(
                key_block_claim_count=1,
                poet_public_key='skeleton key',
                total_block_claim_count=2))

        store['key'] = state

        # Verify the length and contains key
        self.assertEqual(len(store), 1)
        self.assertEqual(len(my_dict), 1)
        self.assertTrue('key' in store)
        self.assertTrue('key' in my_dict)

        # Retrieve the state and verify equality
        retrieved_state = store['key']

        validator_state = \
            retrieved_state.get_validator_state(
                validator_id='Bond, James Bond')

        self.assertEqual(validator_state.key_block_claim_count, 1)
        self.assertEqual(validator_state.poet_public_key, 'skeleton key')
        self.assertEqual(validator_state.total_block_claim_count, 2)

        # Delete the key and then verify length and does not contain key
        del store['key']
        self.assertEqual(len(store), 0)
        self.assertEqual(len(my_dict), 0)
        self.assertTrue('key' not in store)
        self.assertTrue('key' not in my_dict)

        with self.assertRaises(KeyError):
            _ = store['key']
Exemple #2
0
    def test_nonexistent_key(self, mock_lmdb):
        """Verify that retrieval of a non-existent key raises the appropriate
        exception.
        """
        # Make LMDB return None for all keys
        mock_lmdb.return_value.__getitem__.return_value = None
        store = \
            consensus_state_store.ConsensusStateStore(
                data_dir=tempfile.gettempdir(),
                validator_id='0123456789abcdef')

        with self.assertRaises(KeyError):
            _ = store['bad key']
Exemple #3
0
    def test_malformed_consensus_state(self, mock_lmdb):
        """Verify the a malformed consensus state that cannot be properly
        deserialized to a consensus state object raises the appropriate
        exception.
        """
        # Make LMDB return CBOR serialization of a non-dict
        mock_lmdb.return_value.__getitem__.return_value = cbor.dumps('bad')
        store = \
            consensus_state_store.ConsensusStateStore(
                data_dir=tempfile.gettempdir(),
                validator_id='0123456789abcdef')

        with self.assertRaises(KeyError):
            _ = store['bad key']
Exemple #4
0
    def test_verify_db_creation(self, mock_lmdb):
        """Verify that the underlying consensus state store LMDB file is
        created underneath the provided data directory and with the correct
        file creation flag.
        """
        consensus_state_store.ConsensusStateStore(
            data_dir=tempfile.gettempdir(), validator_id='0123456789abcdef')

        # Verify that the database is created in the directory provided
        # and that it is created with the anydb flag for open if exists,
        # create if doesn't exist
        (filename, flag), _ = mock_lmdb.call_args

        self.assertTrue(filename.startswith(tempfile.gettempdir() + os.sep))
        self.assertEqual(flag, 'c')
Exemple #5
0
    def test_consensus_store_set_get(self, mock_lmdb):
        """Verify that externally visible state (len, etc.) of the consensus
        state store after set is expected.  Verify that retrieving a
        previously set consensus state object results in the same values
        set.
        """
        # Make LMDB return empty dict
        my_dict = {}
        mock_lmdb.return_value = my_dict

        mock_poet_settings_view = mock.Mock()
        mock_poet_settings_view.target_wait_time = 30.0
        mock_poet_settings_view.initial_wait_time = 3000.0
        mock_poet_settings_view.minimum_wait_time = 1.0
        mock_poet_settings_view.population_estimate_sample_size = 50

        store = \
            consensus_state_store.ConsensusStateStore(
                data_dir=tempfile.gettempdir(),
                validator_id='0123456789abcdef')

        # Verify the length is zero and doesn't contain key
        self.assertEqual(len(store), 0)
        self.assertTrue('key' not in store)

        # Store consensus state
        state = consensus_state.ConsensusState()
        store['key'] = state

        # Verify the length and contains key
        self.assertEqual(len(store), 1)
        self.assertEqual(len(my_dict), 1)
        self.assertTrue('key' in store)
        self.assertTrue('key' in my_dict)

        # Retrieve the state and verify equality
        retrieved_state = store['key']

        self.assertEqual(state.aggregate_local_mean,
                         retrieved_state.aggregate_local_mean)
        self.assertEqual(state.total_block_claim_count,
                         retrieved_state.total_block_claim_count)

        # Have a validator claim a block and update the store
        wait_certificate = mock.Mock()
        wait_certificate.duration = 3.1415
        wait_certificate.local_mean = 5.0
        validator_info = \
            ValidatorInfo(
                id='validator_001',
                signup_info=SignUpInfo(
                    poet_public_key='key_001'))
        state.validator_did_claim_block(
            validator_info=validator_info,
            wait_certificate=wait_certificate,
            poet_settings_view=mock_poet_settings_view)
        store['key'] = state

        # Verify the length and contains key
        self.assertEqual(len(store), 1)
        self.assertEqual(len(my_dict), 1)
        self.assertTrue('key' in store)
        self.assertTrue('key' in my_dict)

        # Retrieve the state and verify equality
        retrieved_state = store['key']

        self.assertEqual(state.aggregate_local_mean,
                         retrieved_state.aggregate_local_mean)
        self.assertEqual(state.total_block_claim_count,
                         retrieved_state.total_block_claim_count)

        validator_state = \
            retrieved_state.get_validator_state(
                validator_info=validator_info)
        retrieved_validator_state = \
            retrieved_state.get_validator_state(
                validator_info=validator_info)

        self.assertEqual(validator_state.key_block_claim_count,
                         retrieved_validator_state.key_block_claim_count)
        self.assertEqual(validator_state.poet_public_key,
                         retrieved_validator_state.poet_public_key)
        self.assertEqual(validator_state.total_block_claim_count,
                         retrieved_validator_state.total_block_claim_count)

        # Delete the key and then verify length and does not contain key
        del store['key']
        self.assertEqual(len(store), 0)
        self.assertEqual(len(my_dict), 0)
        self.assertTrue('key' not in store)
        self.assertTrue('key' not in my_dict)

        with self.assertRaises(KeyError):
            _ = store['key']
    def do_consensus_store_set_get():
        my_dict = {}
        mock_lmdb.return_value = my_dict

        mock_poet_settings_view = mock.Mock()
        mock_poet_settings_view.target_wait_time = 30.0
        mock_poet_settings_view.initial_wait_time = 3000.0
        mock_poet_settings_view.population_estimate_sample_size = 50

        store = \
            consensus_state_store.ConsensusStateStore(
                data_dir=tempfile.gettempdir(),
                validator_id='0123456789abcdef')

        # Store consensus state
        state = consensus_state.ConsensusState()
        store['key'] = state

        # Retrieve the state and verify equality
        retrieved_state = store['key']

        testConsensusStateStore.assertEqual(
            state.aggregate_local_mean, retrieved_state.aggregate_local_mean)
        testConsensusStateStore.assertEqual(
            state.total_block_claim_count,
            retrieved_state.total_block_claim_count)

        # Have a validator claim a block and update the store
        wait_certificate = mock.Mock()
        wait_certificate.duration = 3.1415
        wait_certificate.local_mean = 5.0
        validator_info = \
            ValidatorInfo(
                id='validator_001',
                signup_info=SignUpInfo(
                    poet_public_key='key_001'))
        state.validator_did_claim_block(
            validator_info=validator_info,
            wait_certificate=wait_certificate,
            poet_settings_view=mock_poet_settings_view)
        store['key'] = state

        # Verify the length and contains key
        testConsensusStateStore.assertEqual(len(store), 1)
        testConsensusStateStore.assertEqual(len(my_dict), 1)
        testConsensusStateStore.assertTrue('key' in store)
        testConsensusStateStore.assertTrue('key' in my_dict)

        # Retrieve the state and verify equality
        retrieved_state = store['key']

        testConsensusStateStore.assertEqual(
            state.aggregate_local_mean, retrieved_state.aggregate_local_mean)
        testConsensusStateStore.assertEqual(
            state.total_block_claim_count,
            retrieved_state.total_block_claim_count)

        validator_state = \
            retrieved_state.get_validator_state(
                validator_info=validator_info)
        retrieved_validator_state = \
            retrieved_state.get_validator_state(
                validator_info=validator_info)

        testConsensusStateStore.assertEqual(
            validator_state.key_block_claim_count,
            retrieved_validator_state.key_block_claim_count)
        testConsensusStateStore.assertEqual(
            validator_state.poet_public_key,
            retrieved_validator_state.poet_public_key)
        testConsensusStateStore.assertEqual(
            validator_state.total_block_claim_count,
            retrieved_validator_state.total_block_claim_count)

        # Delete the key and then verify length and does not contain key
        del store['key']
        testConsensusStateStore.assertEqual(len(store), 0)
        testConsensusStateStore.assertEqual(len(my_dict), 0)
        testConsensusStateStore.assertTrue('key' not in store)
        testConsensusStateStore.assertTrue('key' not in my_dict)