github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/block.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 argparse 17 from sawtooth_cli import format_utils as fmt 18 from sawtooth_cli.rest_client import RestClient 19 from sawtooth_cli.exceptions import CliException 20 from sawtooth_cli.parent_parsers import base_http_parser 21 from sawtooth_cli.parent_parsers import base_list_parser 22 from sawtooth_cli.parent_parsers import base_show_parser 23 24 25 def add_block_parser(subparsers, parent_parser): 26 """Adds arguments parsers for the block list and block show commands 27 28 Args: 29 subparsers: Add parsers to this subparser object 30 parent_parser: The parent argparse.ArgumentParser object 31 """ 32 parser = subparsers.add_parser( 33 'block', 34 description='Provides subcommands to display information about the ' 35 'blocks in the current blockchain.', 36 help='Displays information on blocks in the current blockchain') 37 38 grand_parsers = parser.add_subparsers( 39 title='subcommands', 40 dest='subcommand') 41 42 grand_parsers.required = True 43 44 description = ( 45 'Displays information for all blocks on the current ' 46 'blockchain, including the block id and number, public keys all ' 47 'of allsigners, and number of transactions and batches.') 48 49 list_parser = grand_parsers.add_parser( 50 'list', 51 help='Displays information for all blocks on the current blockchain', 52 description=description, 53 parents=[base_http_parser(), base_list_parser()], 54 formatter_class=argparse.RawDescriptionHelpFormatter) 55 56 list_parser.add_argument( 57 '-n', 58 '--count', 59 default=100, 60 type=int, 61 help='the number of blocks to list', 62 ) 63 64 description = ( 65 'Displays information about the specified block on ' 66 'the current blockchain') 67 68 show_parser = grand_parsers.add_parser( 69 'show', 70 help=description, 71 description=description + '.', 72 parents=[base_http_parser(), base_show_parser()], 73 formatter_class=argparse.RawDescriptionHelpFormatter) 74 show_parser.add_argument( 75 'block_id', 76 type=str, 77 help='id (header_signature) of the block') 78 79 80 def do_block(args): 81 """Runs the block list or block show command, printing output to the 82 console 83 84 Args: 85 args: The parsed arguments sent to the command at runtime 86 """ 87 rest_client = RestClient(args.url, args.user) 88 89 if args.subcommand == 'list': 90 block_generator = rest_client.list_blocks() 91 blocks = [] 92 left = args.count 93 for block in block_generator: 94 blocks.append(block) 95 left -= 1 96 if left <= 0: 97 break 98 99 keys = ('num', 'block_id', 'batches', 'txns', 'signer') 100 headers = tuple(k.upper() if k != 'batches' else 'BATS' for k in keys) 101 102 def parse_block_row(block): 103 batches = block.get('batches', []) 104 txns = [t for b in batches for t in b['transactions']] 105 return ( 106 block['header'].get('block_num', 0), 107 block['header_signature'], 108 len(batches), 109 len(txns), 110 block['header']['signer_public_key']) 111 112 if args.format == 'default': 113 fmt.print_terminal_table(headers, blocks, parse_block_row) 114 115 elif args.format == 'csv': 116 fmt.print_csv(headers, blocks, parse_block_row) 117 118 elif args.format == 'json' or args.format == 'yaml': 119 data = [{k: d for k, d in zip(keys, parse_block_row(b))} 120 for b in blocks] 121 122 if args.format == 'yaml': 123 fmt.print_yaml(data) 124 elif args.format == 'json': 125 fmt.print_json(data) 126 else: 127 raise AssertionError('Missing handler: {}'.format(args.format)) 128 129 else: 130 raise AssertionError('Missing handler: {}'.format(args.format)) 131 132 if args.subcommand == 'show': 133 output = rest_client.get_block(args.block_id) 134 135 if args.key: 136 if args.key in output: 137 output = output[args.key] 138 elif args.key in output['header']: 139 output = output['header'][args.key] 140 else: 141 raise CliException( 142 'key "{}" not found in block or header'.format(args.key)) 143 144 if args.format == 'yaml': 145 fmt.print_yaml(output) 146 elif args.format == 'json': 147 fmt.print_json(output) 148 else: 149 raise AssertionError('Missing handler: {}'.format(args.format))