Skip to content

Commit

Permalink
Add json output support to showblkhead
Browse files Browse the repository at this point in the history
  • Loading branch information
periodic1236 committed Feb 27, 2014
1 parent ba3b154 commit 93abae0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
53 changes: 45 additions & 8 deletions src/showblkhead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@ bool load_blk_header(block_header_type& blk_header, const std::string& filename)
return true;
}

int main(int argc, char** argv)
void show_blk_header(const block_header_type& blk_header)
{
std::string filename = "-";
if (argc == 2)
filename = argv[1];
block_header_type blk_header;
if (!load_blk_header(blk_header, filename))
return -1;
// Show details.
std::cout << "hash: " << hash_block_header(blk_header) << std::endl;
std::cout << "version: " << blk_header.version << std::endl;
std::cout << "previous_block_hash: "
Expand All @@ -53,6 +46,50 @@ int main(int argc, char** argv)
std::cout << "timestamp: " << blk_header.timestamp << std::endl;
std::cout << "bits: " << blk_header.bits << std::endl;
std::cout << "nonce: " << blk_header.nonce << std::endl;
}

void json_show_blk_header(const block_header_type& blk_header)
{
std::cout << "{" << std::endl;
std::cout << " \"hash\": \"" << hash_block_header(blk_header)
<< "\"," << std::endl;
std::cout << " \"version\": " << blk_header.version << "," << std::endl;
std::cout << " \"previous_block_hash\": \""
<< blk_header.previous_block_hash << "\"," << std::endl;
std::cout << " \"merkle\": \"" << blk_header.merkle << "\"," << std::endl;
std::cout << " \"timestamp\": " << blk_header.timestamp
<< "," << std::endl;
std::cout << " \"bits\": " << blk_header.bits << "," << std::endl;
std::cout << " \"nonce\": " << blk_header.nonce << std::endl;
std::cout << "}" << std::endl;
}

int main(int argc, char** argv)
{
std::string filename = "-";
bool json_output = false;
for (size_t i = 1; i < argc; ++i)
{
const std::string arg = argv[i];
if (arg == "-j" || arg == "--json")
{
json_output = true;
continue;
}
if (filename != "-")
{
std::cerr << "Usage: sx showblkhead [-j] FILENAME" << std::endl;
return -1;
}
filename = arg;
}
block_header_type blk_header;
if (!load_blk_header(blk_header, filename))
return -1;
if (json_output)
json_show_blk_header(blk_header);
else
show_blk_header(blk_header);
return 0;
}

26 changes: 13 additions & 13 deletions src/sx.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,22 @@
"Show the details of a block header.",

"""\
Usage: sx showblkhead FILENAME
Usage: sx showblkhead [-j] FILENAME
'showblkhead' allows inspecting of block headers.
$ sx showblkhead headerfile.blk
hash: 4d25b18ed094ad68f75f21692d8540f45ceb90b240a521b8f191e95d8b6b8bb0
version: 1 locktime: 0
Input:
previous output:
97e06e49dfdd26c5a904670971ccf4c7fe7d9da53cb379bf9b442fc9427080b3:0
script: sequence: 4294967295
Output:
value: 90000
script: dup hash160 [ 18c0bd8d1818f1bf99cb1df2269c645318ef7b73 ] equalverify
checksig
address: 13Ft7SkreJY9D823NPm4t6D1cBqLYTJtAe
-j, --json Enable json parseable output.
Example:
$ sx fetch-block-header 210000 | sx showblkhead
hash: 000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e
version: 2
previous_block_hash: 00000000000000f3819164645360294b5dee7f2e846001ac9f41a70b7a9a3de1
merkle: 3cdd40a60823b1c7356d0987078e9426724c5b3ab439c2d80ad2bdd620e603d8
timestamp: 1354116278
bits: 436527338
nonce: 4069828196
\
"""
),
Expand Down

0 comments on commit 93abae0

Please sign in to comment.