user9111069
user9111069

Reputation:

How big should be "nonce" be in a blockchain?

I am attempting to create my own blockchain, in order to understand how they work. I wanted to know how big the nonce should be (e.g. 8 digits or something like that). I am using a PoW (proof-of-work) algorithm in my blockchain. This blockchain uses the SHA256 hashing algorithm.

I am using the C programming language in order to implement this blockchain.

Here is what the block looks like in code:

struct Block
{
    /*Block structure*/
    char* hash;
    char* prev_hash;
    char* from;
    char* to;
    long long int nonce;
    double amount;
    time_t timestamp;
};

Upvotes: 0

Views: 1126

Answers (3)

Devalex
Devalex

Reputation: 1

Maximal an unsigned 8byte but 4bytes is more then enough

Upvotes: 0

tadman
tadman

Reputation: 211700

You want a nonce large enough that there's likely to be a value in that number range which satisfies your difficulty conditions, but not so large that it's a waste of space.

Bitcoin uses a 32-bit value which by itself isn't sufficiently large. That's why there's other factors in the hash which can be altered to generate a wider range of inputs, plus the "extranonce" section of the transaction block.

When designing your chain make sure you don't create impossible to satisfy conditions. There may be situations where even a 64-bit value is insufficiently large to get a satisfactory hash out, so make sure there's other things that can be adjusted, re-ordered, or incremented as necessary.

A 32-bit value might seem really large, but in actuality it's nothing. Modern Bitcoin mining hardware can do in excess of 10 terahashes per second. That means trying all 32-bit values takes around 0.4 milliseconds.

If you don't have an extranonce you'll need to use a 64-bit value because that can't be exhaustively tested, and when combined with a timestamp you should have enough variation on the input that a satisfactory hash can always be found.

Upvotes: 3

mgul
mgul

Reputation: 742

A 4-bytes unsigned int would suffice. I don't know how you plan to design your blockchain but you normally don't have to have too big values for the nonce because you keep changing the block as new transactions come in

Upvotes: 0

Related Questions