github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/families/battleship/sawtooth_battleship/processor/main.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 hashlib
    17  import sys
    18  import argparse
    19  import pkg_resources
    20  
    21  from sawtooth_sdk.processor.core import TransactionProcessor
    22  from sawtooth_sdk.processor.config import get_log_dir
    23  from sawtooth_sdk.processor.config import get_log_config
    24  from sawtooth_sdk.processor.log import init_console_logging
    25  from sawtooth_sdk.processor.log import log_configuration
    26  from sawtooth_battleship.processor.handler import BattleshipTransactionHandler
    27  
    28  
    29  DISTRIBUTION_NAME = 'sawtooth-battleship'
    30  
    31  
    32  def parse_args(args):
    33      parser = argparse.ArgumentParser(
    34          formatter_class=argparse.RawTextHelpFormatter)
    35  
    36      parser.add_argument(
    37          '-C', '--connect',
    38          default='tcp://localhost:4004',
    39          help='Endpoint for the validator connection')
    40  
    41      parser.add_argument('-v', '--verbose',
    42                          action='count',
    43                          default=0,
    44                          help='Increase output sent to stderr')
    45  
    46      try:
    47          version = pkg_resources.get_distribution(DISTRIBUTION_NAME).version
    48      except pkg_resources.DistributionNotFound:
    49          version = 'UNKNOWN'
    50  
    51      parser.add_argument(
    52          '-V', '--version',
    53          action='version',
    54          version=(DISTRIBUTION_NAME + ' (Hyperledger Sawtooth) version {}')
    55          .format(version),
    56          help='print version information')
    57  
    58      return parser.parse_args(args)
    59  
    60  
    61  def main(args=None):
    62      if args is None:
    63          args = sys.argv[1:]
    64      opts = parse_args(args)
    65      processor = None
    66      try:
    67          processor = TransactionProcessor(url=opts.connect)
    68          log_config = get_log_config(filename="battleship_log_config.toml")
    69  
    70          # If no toml, try loading yaml
    71          if log_config is None:
    72              log_config = get_log_config(filename="battleship_log_config.yaml")
    73  
    74          if log_config is not None:
    75              log_configuration(log_config=log_config)
    76          else:
    77              log_dir = get_log_dir()
    78              # use the transaction processor zmq identity for filename
    79              log_configuration(
    80                  log_dir=log_dir,
    81                  name="battleship-" + str(processor.zmq_id)[2:-1])
    82  
    83          init_console_logging(verbose_level=opts.verbose)
    84  
    85          # The prefix should eventually be looked up from the
    86          # validator's namespace registry.
    87          battle_ship_prefix = \
    88              hashlib.sha512('battleship'.encode("utf-8")).hexdigest()[0:6]
    89          handler = \
    90              BattleshipTransactionHandler(namespace_prefix=battle_ship_prefix)
    91  
    92          processor.add_handler(handler)
    93  
    94          processor.start()
    95      except KeyboardInterrupt:
    96          pass
    97      except Exception as e:  # pylint: disable=broad-except
    98          print("Error: {}".format(e))
    99      finally:
   100          if processor is not None:
   101              processor.stop()