From cdb4aa7555b674836bd8486f06c8238177c68d45 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Sun, 14 Apr 2019 15:01:16 +0200 Subject: [PATCH] Hush snapshot script --- bin/hush_snapshot | 74 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/bin/hush_snapshot b/bin/hush_snapshot index 757a7cb..39dbc52 100755 --- a/bin/hush_snapshot +++ b/bin/hush_snapshot @@ -4,26 +4,70 @@ use warnings; use Data::Dumper; use JSON::Any; -# send to this many addresses per send -my $BATCH = 100; -my $fee = "0.0"; +my $N = 100; # this many receivers per z_sendmany, TODO: add our zaddr +my $fee = "0.0001"; my $minconf = 1; -my $FROM = "t1balser"; +my $FROM = "RHushEyeDm7XwtaTWtyCbjGQumYyV8vMjn"; +my $j = JSON::Any->new; +my $json = qx/hush-cli getsnapshot/; +my $snapshot= $j->decode($json); +my $height = $snapshot->{start_height} == $snapshot->{ending_height} ? $snapshot->{start_height} : die("Inconsistent block height from getsnapshot!"); +my $convert = $ENV{HOME}."/git/hush3/contrib/convert_address.py"; +my $cache = $ENV{HOME}."/git/hush/contrib/v3/snapshot-1554926009.csv"; +my $fname = "snapshot_$height-" . time() . ".csv"; -my $j = JSON::Any->new; -my $json = qx/hush-cli getsnapshot/; warn Dumper [ 'getsnapshot=', $json ] if $ENV{DEBUG}; +open(my $csv, ">", $fname) or die $!; -my $snapshot = $j->decode($json); -my $addresses = $snapshot->{addresses}; -for my $addr (@$addresses) { - my $address = $addr->{addr}; - my $amount = $addr->{amount}; - print <{addresses}; +my $ignored = 0; +my $batches; +my $total_amount=0.0; +my $Kaddrs = {}; +# use a previous snapshot to greatly speedup another snapshot +# by avoiding shelling out to convert address formats +if (-e $cache) { + open(my $fh, "<", $cache) or die $!; + while (my $line = <$fh>) { + my ($taddr,$kaddr) = split /,/, $line; + $Kaddrs->{$taddr} = $kaddr; + } + my $num_kaddrs = keys %$Kaddrs; + print "# Cache $cache processed with $num_kaddrs addresses\n"; +} else { + print "# Cache $cache not found\n"; } +my $i = 1; # batch index +my $a = 1; # addresses index +for my $addr (@$addresses) { + my $address = $addr->{addr}; + my $amount = $addr->{amount}; + $total_amount+= $amount; + # use cached addr if possible + my $kaddr = $Kaddrs->{$address} || qx/$convert $address/; + chomp $kaddr; + print $csv "$address,$kaddr,$amount\n"; + $batches->{$i} ||= []; + if ($a % $N == 0) { + my $recipients = $j->encode($batches->{$i}); + $recipients =~ s/"/\\"/g; + print qq!hush-cli z_sendmany $FROM "$recipients" $minconf $fee\n!; + $i++; # next batch + #last; + } else { + if ($address ~~ [@ignores]) { + $ignored++; + print "# ignoring $address\n"; + } else { + push @{ $batches->{$i} }, { "address" => $kaddr, "amount" => $amount }; + } + } + $a++; +} +close($csv); +my $size = -s $fname; +print "# Created $fname , $size bytes, $a addresses, $total_amount HUSH\n";