question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about duplicate nonce

See original GitHub issue

A question. I continue to try and make a custom nonce, because even when ratelimit is enabled, it regularly produces a duplicate nonce error.

If the Yobit exchange default ratelimit nonce is defined in milliseconds by default as we discussed in #2153, then I do not understand how this could happen in any case - even programmer error.

In any case, I tried to fix it by using this;

// A: custom nonce value
class MyOKCoinUSD extends \ccxt\okcoinusd {
    public function __construct ($options = array ()) {
        parent::__construct (array_merge (array ('i' => 1), $options));
    }
    public function nonce () {
        return $this->i++;
    }
}

As well as by this;

// B: milliseconds nonce
class MyZaif extends \ccxt\zaif {
    public function __construct ($options = array ()) {
        parent::__construct (array_merge (array ('i' => 1), $options));
    }
    public function nonce () {
        return $this->milliseconds ();
    }
}

As per https://github.com/ccxt/ccxt/wiki/Manual (Overriding The Nonce)

Btw, the same page it says for the ratelimit option that “This parameter is not used for now (reserved for future).” (incorrect?)

In any case, both methods above fail. If I use the second, it gives this;

Assert! ccxt\ExchangeError: yobit unknown "error" value => {"success":0,"error":"invalid nonce (max 2147483646), please generate new key"} in /home/roel/ccxt/php/liqui.php:736

If I use the first, I am not sure how to set the custom nonce (I am reading/storing it from/to a file), but if I use the code as-is, it produces;

Assert! ccxt\ExchangeError: yobit unknown "error" value => {"success":0,"error":"invalid nonce (has already been used)"} in /home/roel/ccxt/php/liqui.php:736

Even if I set ‘i’=>1 to a much larger random number which never have been used.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
forexhillcommented, Mar 8, 2018

For anyone wanting to read/store a nonce from a nonce.dat file (which can be created and pre-set with a number using a standard text editor);

// Custom nonce value read from file
class _yobit extends \ccxt\yobit{
  public function __construct($options=array()){
    if(file_exists('nonce.dat')){$noncearray=file('nonce.dat',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);$nonce=$noncearray[0];$nonce++;}else{$nonce=1;}  // Assume we have new keys if nonce.dat does not exist. 
    if(file_exists('nonce.dat')){if(file_exists('nonce.old')){unlink('nonce.old');} rename('nonce.dat','nonce.old');}
    file_put_contents('nonce.dat',$nonce,LOCK_EX);  // File contains latest used nonce.
    parent::__construct(array_merge(array('i'=>$nonce),$options));
  }
  public function nonce(){
    if(file_exists('nonce.dat')){$noncearray=file('nonce.dat',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);$nonce=$noncearray[0];$nonce++;}else{$nonce=1;}  // Assume we have new keys if nonce.dat does not exist. 
    if(file_exists('nonce.dat')){if(file_exists('nonce.old')){unlink('nonce.old');} rename('nonce.dat','nonce.old');}
    file_put_contents('nonce.dat',$nonce,LOCK_EX);  // File contains latest used nonce.
    $this->i=$nonce;
    return $this->i;
  }
}

[…then init exchange…]

$exchange=new _yobit(array('apiKey'=>'{yourkey}','secret'=>'{yoursecret}','enableRateLimit'=>true,'rateLimit'=>1000,'timeout'=>20000,));  

Code provided as-is without any warranties.

1reaction
kroitorcommented, Mar 8, 2018

@forexhill you don’t really need a custom procedure with a new key. You can just remove the overrided procedure and use the default nonce as is with a new key…

Now, as for your questions:

I continue to try and make a custom nonce, because even when ratelimit is enabled, it regularly produces a duplicate nonce error.

I would agree with @mkutny , whenever you find yourself in such a situation, the best way is to make sure you fully understand the nature of the bug, before trying to fix anything.

If the Yobit exchange default ratelimit nonce is defined in milliseconds by default as we discussed in #2153, then I do not understand how this could happen in any case - even programmer error.

Right, and this is something that is the most important aspect to investigate, all other measures are applied prematurely.

In any case, I tried to fix it by using this;

Redefining a nonce won’t solve a reappearing nonce conflict in general case, unfortunately, so, the fix isn’t fixing it really.

Btw, the same page it says for the ratelimit option that “This parameter is not used for now (reserved for future).” (incorrect?)

Outdated somewhat… Needs to be constantly updated, I’m doing whatever I can to keep it up, really.

has already been used

Random nonces won’t work in general. The nonce not only has to be unique, but also has to be greater than the previous nonce, so a random number will sooner or later hit a nonce smaller than the previously used value, and Yobit will confuse you with their non-intuitive error messaging {"success":0,"error":"invalid nonce (has already been used)"}, which in fact means an absolutely different reason: “you’re sending a nonce that is smaller than the biggest nonce I’ve seen so far with this api key” (this is what Yobit is trying to tell you, really).


Anyway, there’s a Troubleshooting procedure and if you follow if from top to bottom literally one point after the other, you will inevitably try a new key among the first troubleshooting steps. That is something you should have tried in the first place. And this is what, most likely, fixed the issue for you. However, if you run into the same issue again, I’d recommend to stop right there, do some preparation and debugging and in case of a difficulty with it, report the code and verbose output, as usually, we will be happy to help.

Thx, closing it for now, if you don’t mind. Feel free to reopen it if needed or just ask questions if any.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate transaction with same nonce
If transaction 0x506d was included in a block, any other transaction from the same address and the same nonce is void.
Read more >
UsingWW: The "duplicate nonce" error is back - WeBWorK
I click on a problem, answer one of the questions, click grade, and the grade is reported to be successfully sent back to...
Read more >
Duplicate nonce in BrainTree using Dropin UI - Stack Overflow
The problem is that each transaction needs a unique nonce, but if I submit the Dropin UI twice w/in 2-3 mins I get...
Read more >
401 Error - Duplicate nonce to one already received (Status 401
If VitalSource receives more than one request to open an eTextbook with the same oauth_nonce value, a user will see this error. In...
Read more >
How To Push Through A Stuck Transaction - Set Help Center
5. That nonce value is basically denoting the position of this transaction in the sequential order of transactions from your wallet. What clearing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found