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
• 27,620 points
22 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,040 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 64 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 7, 2018 in Blockchain by Shashank
• 10,330 points
34 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,070 points
46 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
84 views
0 votes
1 answer
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
• 27,620 points
51 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,040 points
117 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,040 points
32 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.