Running blockchain script in a one liner

0 votes

I know how to use this blockchain script on the server, but I don't know how to minify it into a single function (only one return), best would be one liner:

String.prototype.hashCode = function(){
    if(this.length ==0){
        return 0;
    }else{
        return parseInt(this.split('').map(function(char){
            return char.charCodeAt(0);
        }).reduce(function(current, previous){
            return previous + current;
        }))+ (this.substr(1, this.length)).hashCode();
    }
};
nonce = 0;
while(true){
    hash = (transaction + nonce).hashCode() % 1234;
    if (hash ==0){
        break;
    }
    nonce++;
}
return nonce;

Transaction is a string. I tried to replace this with hash, but since hashcode function calls itself, I don't know how to manage this loop without function. Can somebody tell me how to do this?

Sep 18, 2018 in Blockchain by digger
• 26,740 points
512 views

1 answer to this question.

0 votes

This is a direct translation of the original code

function findNonce( data ){
var buffer, i, hash, nonce = -1;
    data = '' + ( data || '' );
    do {
        hash = 0;
        nonce++;
        buffer = data + nonce;
        i = buffer.length;
        while(i--) hash += buffer.charCodeAt(i) * (i+1);
    } while ( hash % 1234 );
    return nonce;
};

And, once minified,

function findNonce(n){var e,o,r,t=-1;n=""+(n||"");do for(r=0,t++,e=n+t,o=e.length;o--;)r+=e.charCodeAt(o)*(o+1);while(r%1234);return t}

But if better performance is required this should be optimized, calculating first the hash for the input string and then doing only the calcs for each nonce value.

function findNonceFast( data ){
var preHash, noncePos, nonce = -1, code; 
    function hashCalc( s, hash, pos ){
        var i = s.length; while(i--) hash += s.charCodeAt(i) * (i+pos+1);
        return hash;
    };
    data =  '' + ( data || '' );
    preHash = hashCalc( data, 0, 0 );
    noncePos = data.length;
    do {
        nonce++;
        code = hashCalc( ''+nonce, preHash, noncePos );
    } while (code % 1234)
    return nonce;
};

Minified

function findNonceFast(n){function r(n,r,t){for(var e=n.length;e--;)r+=n.charCodeAt(e)*(e+t+1);return r}var t,e,o,a=-1;n=""+(n||""),t=r(n,0,0),e=n.length;do a++,o=r(""+a,t,e);while(o%1234);return a}
answered Sep 18, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,790 points

edited Mar 26, 2018 by Christine 1,568 views
+1 vote
1 answer

Can a data be altered in a blockchain without changing its hash?

Yes, it should work. The authentication practice ...READ MORE

answered Apr 8, 2018 in Blockchain by Shashank
• 10,400 points
895 views
+1 vote
1 answer

How Blockchain work in a mobile app?

At present there is no provision to ...READ MORE

answered Apr 10, 2018 in Blockchain by Johnathon
• 9,090 points
727 views
+1 vote
1 answer

How does a blockchain work in hyperledger framework?

Hyperledger Composer simplifies application development on top ...READ MORE

answered Apr 19, 2018 in Blockchain by Christine
• 15,790 points
1,070 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,004 views
+1 vote
1 answer
0 votes
1 answer

Invalid opcode error in a simple Solidity contract and script

You don't need to call .at() if you're using .new(). ...READ MORE

answered Sep 14, 2018 in Blockchain by slayer
• 29,370 points
4,819 views
0 votes
1 answer

CompleteableFuture in a loop contruct in a private Ethereum Blockchain

You might be able to benefit from ...READ MORE

answered Sep 14, 2018 in Blockchain by slayer
• 29,370 points
619 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP