github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_journal/mock_consensus.py (about)

     1  # Copyright 2016 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  # pylint: disable=abstract-method
    17  # pylint: disable=arguments-differ
    18  # pylint: disable=useless-super-delegation
    19  
    20  from sawtooth_validator.journal.consensus.consensus \
    21      import BlockPublisherInterface
    22  from sawtooth_validator.journal.consensus.consensus \
    23      import BlockVerifierInterface
    24  from sawtooth_validator.journal.consensus.consensus \
    25      import ForkResolverInterface
    26  
    27  
    28  class BlockPublisher(BlockPublisherInterface):
    29      """ MockConsensus BlockPublisher
    30      """
    31  
    32      def __init__(self,
    33                   block_cache=None,
    34                   state_view_factory=None,
    35                   batch_publisher=None,
    36                   data_dir=None,
    37                   config_dir=None,
    38                   validator_id=None):
    39          super().__init__(
    40              block_cache,
    41              state_view_factory,
    42              batch_publisher,
    43              data_dir,
    44              config_dir,
    45              validator_id)
    46  
    47      def initialize_block(self, block_header):
    48          """
    49          Args:
    50              block_header (BlockHeader): the block_header to initialize.
    51          Returns:
    52              Boolean: True if the block should become a candidate
    53          """
    54          return True
    55  
    56      def check_publish_block(self, block_header):
    57          """Initialize the candidate block_header.
    58          Args:
    59              block_header (block_header): the block_header to check.
    60          Returns:
    61              Boolean: True if the candidate block_header should be built.
    62          """
    63          return True
    64  
    65      def finalize_block(self, block_header, weight=0):
    66          """Finalize a block_header to be claimed.
    67  
    68          Args:
    69              block_header: The candidate block_header to be finalized.
    70          Returns:
    71              Boolean: True if the candidate block should be claimed.
    72          """
    73          block_header.consensus = "test_mode:{}".format(weight).encode()
    74          return True
    75  
    76  
    77  class BlockVerifier(BlockVerifierInterface):
    78      """MockConsensus BlockVerifier implementation
    79      """
    80  
    81      def __init__(self,
    82                   block_cache,
    83                   state_view_factory,
    84                   data_dir,
    85                   config_dir,
    86                   validator_id):
    87          super().__init__(
    88              block_cache,
    89              state_view_factory,
    90              data_dir,
    91              config_dir,
    92              validator_id)
    93  
    94      def verify_block(self, block_wrapper):
    95          return block_wrapper.consensus.startswith(b"test_mode")
    96  
    97  
    98  class ForkResolver(ForkResolverInterface):
    99      """MockConsensus ForkResolver implementation
   100      """
   101  
   102      def __init__(self,
   103                   block_cache,
   104                   state_view_factory,
   105                   data_dir,
   106                   config_dir,
   107                   validator_id):
   108          super().__init__(
   109              block_cache,
   110              state_view_factory,
   111              data_dir,
   112              config_dir,
   113              validator_id)
   114  
   115      def compare_forks(self, cur_fork_head, new_fork_head):
   116          """
   117  
   118          Args:
   119              cur_fork_head (BlockWrapper): The current head of the block chain.
   120              new_fork_head (BlockWrapper): The head of the fork that is being
   121              evaluated.
   122          Returns:
   123              bool: True if the new chain should replace the current chain.
   124              False if the new chain should be discarded.
   125          """
   126          new_num = new_fork_head.block_num
   127          new_weight = 0
   128          if new_fork_head.consensus:
   129              new_weight = int(new_fork_head.consensus.decode().split(':')[1])
   130          cur_num = cur_fork_head.block_num
   131          cur_weight = 0
   132          if cur_fork_head.consensus:
   133              cur_weight = int(cur_fork_head.consensus.decode().split(':')[1])
   134  
   135          # chains are ordered by length first, then weight
   136          if new_num == cur_num:
   137              return new_weight > cur_weight
   138  
   139          return new_num > cur_num