github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_devmode_consensus/tests.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import time 17 import unittest 18 import logging 19 20 from test_journal.mock import MockStateViewFactory 21 from test_client_request_handlers.mocks import MockBlockStore 22 from sawtooth_validator.journal.block_cache import BlockCache 23 from sawtooth_validator.journal.consensus.dev_mode.dev_mode_consensus \ 24 import BlockPublisher 25 from sawtooth_validator.protobuf.block_pb2 import BlockHeader 26 from sawtooth_validator.state.settings_view import SettingsView 27 from sawtooth_validator.protobuf.setting_pb2 import Setting 28 29 LOGGER = logging.getLogger(__name__) 30 31 32 class TestCheckPublishBlock(unittest.TestCase): 33 def create_block_header(self, signer_public_key=None): 34 return BlockHeader(signer_public_key=signer_public_key) 35 36 def create_state_view_factory(self, values): 37 state_db = {} 38 if values is not None: 39 for key, value in values.items(): 40 state_db[SettingsView.setting_address(key)] = \ 41 TestCheckPublishBlock._setting_entry(key, repr(value)) 42 43 return MockStateViewFactory(state_db) 44 45 def test_default_settings(self): 46 factory = self.create_state_view_factory(values=None) 47 48 dev_mode = \ 49 BlockPublisher( 50 block_cache=BlockCache(block_store=MockBlockStore()), 51 state_view_factory=factory, 52 batch_publisher=None, 53 data_dir=None, 54 config_dir=None, 55 validator_id='Validator_001') 56 57 block_header = self.create_block_header() 58 59 self.assertTrue(dev_mode.check_publish_block(block_header)) 60 61 def test_min_wait_time(self): 62 # non zero value of min wait time 63 factory = self.create_state_view_factory( 64 {"sawtooth.consensus.min_wait_time": 1 65 }) 66 dev_mode = \ 67 BlockPublisher( 68 block_cache=BlockCache(block_store=MockBlockStore()), 69 state_view_factory=factory, 70 batch_publisher=None, 71 data_dir=None, 72 config_dir=None, 73 validator_id='Validator_001') 74 block_header = self.create_block_header() 75 76 dev_mode.initialize_block(block_header) 77 78 self.assertFalse(dev_mode.check_publish_block(block_header)) 79 time.sleep(1) 80 self.assertTrue(dev_mode.check_publish_block(block_header)) 81 82 def test_max_wait_time(self): 83 pass 84 85 def test_min_and_max_wait_time(self): 86 pass 87 88 def test_in_valid_block_publisher_list(self): 89 factory = self.create_state_view_factory({ 90 "sawtooth.consensus.valid_block_publisher": ["name"] 91 92 }) 93 dev_mode = \ 94 BlockPublisher( 95 block_cache=BlockCache(block_store=MockBlockStore()), 96 state_view_factory=factory, 97 batch_publisher=None, 98 data_dir=None, 99 config_dir=None, 100 validator_id='Validator_001') 101 block_header = self.create_block_header("name") 102 self.assertTrue(dev_mode.check_publish_block(block_header)) 103 104 def test_not_in_valid_block_publisher_list(self): 105 factory = self.create_state_view_factory({}) 106 dev_mode = \ 107 BlockPublisher( 108 block_cache=BlockCache(block_store=MockBlockStore()), 109 state_view_factory=factory, 110 batch_publisher=None, 111 data_dir=None, 112 config_dir=None, 113 validator_id='Validator_001') 114 block_header = self.create_block_header("name") 115 self.assertTrue(dev_mode.check_publish_block(block_header)) 116 117 @staticmethod 118 def _setting_entry(key, value): 119 return Setting( 120 entries=[Setting.Entry(key=key, value=value)] 121 ).SerializeToString()