github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/state.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 base64 import b64decode 18 from sawtooth_cli import format_utils as fmt 19 from sawtooth_cli.rest_client import RestClient 20 from sawtooth_cli.exceptions import CliException 21 from sawtooth_cli.parent_parsers import base_http_parser 22 from sawtooth_cli.parent_parsers import base_list_parser 23 24 25 def add_state_parser(subparsers, parent_parser): 26 """Adds arguments parsers for the state list and state 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 'state', 34 help='Displays information on the entries in state', 35 description='Provides subcommands to display information about the ' 36 'state entries in the current blockchain state.') 37 38 grand_parsers = parser.add_subparsers( 39 title='subcommands', 40 dest='subcommand') 41 42 grand_parsers.required = True 43 44 list_parser = grand_parsers.add_parser( 45 'list', 46 description='Lists all state entries in the current blockchain.', 47 parents=[base_http_parser(), base_list_parser()], 48 formatter_class=argparse.RawDescriptionHelpFormatter) 49 50 list_parser.add_argument( 51 'subtree', 52 type=str, 53 nargs='?', 54 default=None, 55 help='address of a subtree to filter the list by') 56 57 list_parser.add_argument( 58 '--head', 59 action='store', 60 default=None, 61 help='specify the id of the block to set as the chain head') 62 63 show_parser = grand_parsers.add_parser( 64 'show', 65 description='Displays information for the specified state address in ' 66 'the current blockchain.', 67 parents=[base_http_parser()], 68 formatter_class=argparse.RawDescriptionHelpFormatter) 69 70 show_parser.add_argument( 71 'address', 72 type=str, 73 help='address of the leaf') 74 75 show_parser.add_argument( 76 '--head', 77 action='store', 78 default=None, 79 help='specify the id of the block to set as the chain head') 80 81 82 def do_state(args): 83 """Runs the batch list or batch show command, printing output to the 84 console 85 86 Args: 87 args: The parsed arguments sent to the command at runtime 88 """ 89 rest_client = RestClient(args.url, args.user) 90 91 if args.subcommand == 'list': 92 response = rest_client.list_state(args.subtree, args.head) 93 leaves = response['data'] 94 head = response['head'] 95 keys = ('address', 'size', 'data') 96 headers = tuple(k.upper() for k in keys) 97 98 def parse_leaf_row(leaf, decode=True): 99 decoded = b64decode(leaf['data']) 100 return ( 101 leaf['address'], 102 len(decoded), 103 str(decoded) if decode else leaf['data']) 104 105 if args.format == 'default': 106 fmt.print_terminal_table(headers, leaves, parse_leaf_row) 107 print('HEAD BLOCK: "{}"'.format(head)) 108 109 elif args.format == 'csv': 110 fmt.print_csv(headers, leaves, parse_leaf_row) 111 print('(data for head block: "{}")'.format(head)) 112 113 elif args.format == 'json' or args.format == 'yaml': 114 state_data = { 115 'head': head, 116 'data': [{k: d for k, d in zip(keys, parse_leaf_row(l, False))} 117 for l in leaves]} 118 119 if args.format == 'yaml': 120 fmt.print_yaml(state_data) 121 elif args.format == 'json': 122 fmt.print_json(state_data) 123 else: 124 raise AssertionError('Missing handler: {}'.format(args.format)) 125 126 else: 127 raise AssertionError('Missing handler: {}'.format(args.format)) 128 129 if args.subcommand == 'show': 130 output = rest_client.get_leaf(args.address, args.head) 131 if output is not None: 132 print('DATA: "{}"'.format(b64decode(output['data']))) 133 print('HEAD: "{}"'.format(output['head'])) 134 else: 135 raise CliException('No data available at {}'.format(args.address))