Skip to content

Instantly share code, notes, and snippets.

@midnightmagic
Created October 10, 2014 23:59
Show Gist options
  • Save midnightmagic/e995d41492a426db94fa to your computer and use it in GitHub Desktop.
Save midnightmagic/e995d41492a426db94fa to your computer and use it in GitHub Desktop.
Messy example of serial RPC bitcoin connections in Perl
#!/usr/bin/perl
use JSON;
use Data::Dumper;
use JSON::RPC::Client;
use POSIX;
use LWP;
if ($#ARGV + 1 < 1) {
# print "Current $ARGV[0] replacing with 0 due to ".$#ARGV." < 1\n";
$ARGV[0]="0";
}
$js=new JSON;
$state="start";
$numfromaddresses=0;
@fromaddresses=();
$numtoaddresses=0;
@toaddresses=();
$|=1;
open h, "bitcoin-cli getinfo |" or die "Unable to retrieve bitcoin-cli getinfo";
while(<h>) {
$p = $p.$_;
}
close h;
#print $p;
# 1 00 000 000
$nowtime=time();
chomp $nowtime;
$json_getinfo = $js->allow_nonref->utf8->relaxed->decode($p);
$totblocks=$json_getinfo->{"blocks"};
sub getblock {
$b = shift;
local $p;
open h, "bitcoin-cli getblock $b |" or die "Unable to retrieve bitcoin-cli getblock";
while(<h>) {
$p = $p.$_;
}
close h;
return $js->allow_nonref->utf8->relaxed->decode($p);
}
sub getblockhash {
$i = shift;
local $p;
my $ua = LWP::UserAgent->new;
$ua->agent('LWP batch');
my $req = HTTP::Request->new(POST => 'http://127.0.0.1:8332/');
$req->content_type('application/json');
$req->authorization_basic('username','password');
$jsonstuff = JSON->new->encode([
{"id"=>"0", "method"=>"getblockhash", "params"=>[$i],},
]);
$req->content($jsonstuff);
my $res = $ua->request($req);
my $otherres = JSON->new->utf8(1)->decode($res->{"_content"});
$p=@{$otherres}[0]->{result};
# chomp $p; chomp $p;
return $p;
}
sub sumvout {
local $b = shift;
local @t = $b->{"tx"};
local $sum=0;
local $p="";
local $v;
local $id=0;
local @spinner=('/','-','\\','|');
local @methodlist=();
# local @spinner=('.','o','O');
$spinc=0; $spind=0;
print $spind;
# print Dumper(@{$b->{"tx"}}); exit;
# print "\n\nAttempting $i...\n\n";
my $ua = LWP::UserAgent->new;
$ua->agent('LWP batch');
my $req = HTTP::Request->new(POST => 'http://127.0.0.1:8332/');
$req->content_type('application/json');
$req->authorization_basic('username','password');
foreach $i(@{$b->{"tx"}}) {
push @methodlist, {"id"=>$id++, "method"=>"getrawtransaction", "params"=>[$i, 1]};
}
# print Dumper(\@methodlist);
$jsonstuff = JSON->new->encode(\@methodlist);
$req->content($jsonstuff);
my $res = $ua->request($req);
my $otherres = JSON->new->utf8(1)->decode($res->{"_content"});
# $p=@{$otherres}[0]->{result};
# print Dumper($otherres); exit;
# print "And total items is: $#{$otherres} \n"; exit;
for ($i=0; $i < $#{$otherres}; $i++) {
# print Dumper(@{$otherres}[1]); exit;
foreach $j(@{@{$otherres}[$i]->{"result"}->{"vout"}}) {
$sum += $j->{"value"};
}
}
# $tx=$js->allow_nonref->utf8->relaxed->decode($p);
# $spinc++; if ($spinc%50 == 0) { $spinc=0; $spind++; if ($spind > $#spinner) { $spind=0; } }
# print "\b".$spinner[$spind];
# foreach $j(@{$tx->{"vout"}}) {
# $sum += $j->{"value"};
# }
# $p="";
return $sum;
}
$fin=0; $totvout=0;
$curtot=0; $block=getblock(getblockhash($totblocks)); $cursor=$totblocks;
#print Dumper($block); exit;
print "Scanning...$cursor ";
while ($fin == 0) {
$curstamp=$block->{"time"};
if ($curstamp < $nowtime - $ARGV[0]) {
print "\n\nHit time barrier $curstamp < $nowtime - $ARGV[0].\n";
$fin=1; last;
}
$sumvout=sumvout($block);
$curtot+=$sumvout;
$prevblock=$block->{"previousblockhash"};
if ($blockptr == "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") {
print "\n\nHit genesis.\n";
$fin=1; last;
}
$block=getblock($prevblock);
$cursor--; print "\rScanning...$cursor($ARGV[0]?".($curstamp-($nowtime-$ARGV[0])).")=>$curtot \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
}
print "\nDone: $curtot total vout\n";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment