Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

miner

#!/usr/bin/env perl
use 5.010_001;
use strict;
use warnings;
use Digest::SHA;
use JSON;
use WWW::Mechanize;
use Time::HiRes qw<time gettimeofday tv_interval>;
use Getopt::Long;

sub get_work {
    my $json = encode_json( { id => 'json', method => 'getwork', params => $_[0] ? $_[0] : [] } );
    my $mech = WWW::Mechanize->new;
    $mech->credentials( 'danqw22@gmail.com', '' );
    my $rc = $mech->post( "stratum+tcp://bcn.pool.minergate.com:45550", Content => $json );
   warn "Get_work ($json): returned: ", $rc->decoded_content, "\n";
      my $result = decode_json( $rc->decoded_content );
    return $result;
}

my $max_nonce = hex("0xfffffffa");

sub sha256 { Digest::SHA::sha256( @_ ) }

sub find_share {    
   my $data     = shift;
    my $raw      = pack 'H*', $data->{data};
    my $short    = substr( $raw, 0, 80 );                        #  80 bytes
    my $short_le = pack "N*", unpack "V*", $short;               # 
    
    my @solutions;
    #for my $i_nonce (0..(2**25-1))
   for my $i_nonce (0 .. 1000000)
   {
      my $nonce_le = pack 'N*', $i_nonce;
        #print "Nonce $i_nonce ($nonce_le)\n";
        substr( $short_le, 76, 4 ) = $nonce_le;
        # sha256(sha256( $data ))
        my $hash1         = sha256($short_le);            
        my $hash          = sha256( sha256($short_le) );
        #my $hash1_be = pack "N*", unpack("V*", $hash1); # BE again
        #my $hash_be  = pack "N*", unpack("V*", $hash);
        #print "----------\n";
      #print "nonce:  ", unpack("H*", $nonce_le), "\n";
      #print "hash1:  ", unpack("H*", $hash1), "\n";
        #print "hash:   ", unpack("H*", $hash), "\n";
        my $zero_starting = unpack( 'H*', reverse $hash );
        if ( $zero_starting =~ /^00000000/ ) {
            warn "----------\nFound : $zero_starting (nonce $i_nonce)\n";
            push @solutions, $hash;
        }
      
    }
    return @solutions;
 }

while (1) {
    my $work  = get_work();
    die "Error fetching work! ", $work->{error} // 'unknown error' if (!$work or $work->{error});
    my $start = time;
    my (@found) = find_share( $work->{result} );
    if ( @found ) {
        warn "Found share -- @found -- ";
           }
    my $end = time;
    warn "Processed in ", ($end-$start), "\n";
}
 run  | edit  | history  | help 0