github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_responder/mock.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  from sawtooth_validator.journal.block_wrapper import BlockWrapper
    16  
    17  
    18  class MockGossip():
    19      def __init__(self):
    20          self.broadcasted = {}
    21          self.sent = {}
    22  
    23      def broadcast(self, message, message_type, exclude):
    24          if message_type in self.broadcasted:
    25              self.broadcasted[message_type] += [message]
    26          else:
    27              self.broadcasted[message_type] = [message]
    28  
    29      def send(self, message_type, message_data, connection_id):
    30          if connection_id in self.sent:
    31              self.sent[connection_id] += [(message_type, message_data)]
    32          else:
    33              self.sent[connection_id] = [(message_type, message_data)]
    34  
    35      def clear(self):
    36          self.broadcasted = {}
    37          self.sent = {}
    38  
    39  
    40  class MockCompleter():
    41      def __init__(self):
    42          self.store = {}
    43  
    44      def add_block(self, block):
    45          self.store[block.header_signature] = BlockWrapper(block)
    46  
    47      def add_batch(self, batch):
    48          self.store[batch.header_signature] = batch
    49          for txn in batch.transactions:
    50              self.store[txn.header_signature] = batch
    51  
    52      def get_chain_head(self):
    53          pass
    54  
    55      def get_block(self, block_id):
    56          return self.store.get(block_id)
    57  
    58      def get_batch(self, batch_id):
    59          return self.store.get(batch_id)
    60  
    61      def get_batch_by_transaction(self, transaction_id):
    62          return self.store.get(transaction_id)