Browse Source

mmgen-blocks-info: allow range spec of type <blocknum>+<num blocks>

The MMGen Project 4 years ago
parent
commit
cc02c489ea
1 changed files with 44 additions and 14 deletions
  1. 44 14
      mmnode-blocks-info

+ 44 - 14
mmnode-blocks-info

@@ -115,7 +115,7 @@ class BlocksInfo:
 							break
 							break
 				yield ls + self.fields[name].fs + rs
 				yield ls + self.fields[name].fs + rs
 
 
-		self.get_block_range()
+		self.get_block_range(cmd_args)
 
 
 		fnames     = get_fields()
 		fnames     = get_fields()
 		self.fvals = list(self.fields[name] for name in fnames)
 		self.fvals = list(self.fields[name] for name in fnames)
@@ -140,15 +140,22 @@ class BlocksInfo:
 		else:
 		else:
 			self.miner_pats = None
 			self.miner_pats = None
 
 
-	def get_block_range(self):
+	def get_block_range(self,args):
 
 
-		if not cmd_args:
+		if not args:
 			first = last = c.blockcount
 			first = last = c.blockcount
 		else:
 		else:
-			arg = cmd_args[0]
-			if arg.startswith('+') and is_int(arg[1:]):
-				last = c.blockcount
-				first = last - int(arg[1:]) + 1
+			arg = args[0]
+			ps = arg.split('+')
+			if len(ps) == 2 and is_int(ps[1]):
+				if not ps[0]:
+					last = c.blockcount
+					first = last - int(arg[1:]) + 1
+				elif is_int(ps[0]):
+					first = int(ps[0])
+					last = first + int(ps[1]) - 1
+				else:
+					opts.usage()
 			elif is_int(arg):
 			elif is_int(arg):
 				first = last = int(arg)
 				first = last = int(arg)
 			else:
 			else:
@@ -157,11 +164,11 @@ class BlocksInfo:
 				except:
 				except:
 					opts.usage()
 					opts.usage()
 
 
-		if first > last:
-			die(2,f'{first}-{last}: invalid block range')
+			if first > last:
+				die(2,f'{first}-{last}: invalid block range')
 
 
-		if last > c.blockcount:
-			die(2,f'Requested block number ({last}) greater than current block height')
+			if last > c.blockcount:
+				die(2,f'Requested block number ({last}) greater than current block height')
 
 
 		self.first = first
 		self.first = first
 		self.last = last
 		self.last = last
@@ -294,7 +301,7 @@ opts_data = {
 	],
 	],
 	'text': {
 	'text': {
 		'desc':    'Display information about a block or range of blocks',
 		'desc':    'Display information about a block or range of blocks',
-		'usage':   '[opts] +<last N blocks>|<block num>[-<block num>]',
+		'usage':   '[opts] [<block num>]+<N blocks>|<block num>[-<block num>]',
 		'options': """
 		'options': """
 -h, --help            Print this help message
 -h, --help            Print this help message
 --, --longhelp        Print help message for long options (common options)
 --, --longhelp        Print help message for long options (common options)
@@ -315,10 +322,33 @@ If the requested range ends at the current chain tip, an estimate of the next
 difficulty adjustment is also displayed. The estimate is based on the average
 difficulty adjustment is also displayed. The estimate is based on the average
 Block Discovery Interval from the beginning of the current 2016-block period.
 Block Discovery Interval from the beginning of the current 2016-block period.
 
 
-AVAILABLE FIELDS: {}
+AVAILABLE FIELDS: {f}
+
+EXAMPLES:
+
+    # Display default info for current block:
+    {p}
+
+    # Display default info for blocks 1-200
+    {p} 1-200
+
+    # Display default info for 20 blocks beginning from block 600000
+    {p} 600000+20
+
+    # Display info for block 152817, adding miner field:
+    {p} --miner-info 152817
+
+    # Display info for last 10 blocks, adding 'inputs' and 'nTx' fields:
+    {p} -o +inputs,nTx +10
+
+    # Display 'block', 'date', 'version' and 'hash' fields for blocks 0-10:
+    # Note: these are the only supported fields for the Genesis Block
+    {p} -o block,date,version,hash 0-10
 
 
 This program requires a txindex-enabled daemon for correct operation.
 This program requires a txindex-enabled daemon for correct operation.
-""".format(fmt_list(BlocksInfo.fields,fmt='bare'))
+""".format(
+		f = fmt_list(BlocksInfo.fields,fmt='bare'),
+		p = g.prog_name )
 	}
 	}
 }
 }