0

I have read some references to create ethereum transactions by a BACK-END server, all in low level, for example, requiring that I control the nonce by myself. Are there safe ways to avoid this control be assuming by default that every call can increment the nonce by one and there is no parallelism? What libs allows me that control and works in a more hogh-level way?

  • If you're using NodeJS on your server, then those two JS libs that you've mentioned on your tags are the right ones for you. – goodvibration Nov 6 '19 at 20:10
0

According to the Ethereum Yellow paper:

nonce: A scalar value equal to the number of transactions sent by the sender; formally Tn.

In this case, what I have done in the past is first make a request to get the number of transactions then use this value + 1 as the nonce for the next transaction.

Here's an example using web3data.js.

Now if you get this error from the client, -32000: Nonce too low, then you know that another transaction has gone through and you must re-submit your transaction.

Let me know if that's helpful in any way.

0

You can't really be certain unless you store locally the latest known nonce, and secure access to that value using locks.

The reason is that several threads could get the current number of transactions for that address (in other words, the nonce) at the exact same time, and they all would end up broadcasting different transactions with the same nonce.

Say you have a function that handles a private-only variable n. You must increment its value and return it. For instance, in Java this would suffice:

public class TransactionCount {
    private long n;

    public TransactionCount(long n) {
        this.n = n;
    }

    public synchronized long next() {
        this.n += 1L;
        return this.n;
    }
}

Notice the synchronized keyword that guarantees that only once thread can access at a given point in time, while if others try they'll have to wait, hence obtaining a different value of n.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.