Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

BUG: fix nonce for concurrency tasks #193

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
869 changes: 529 additions & 340 deletions enigma-js/lib/enigma-js.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enigma-js/lib/enigma-js.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions enigma-js/lib/enigma-js.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enigma-js/lib/enigma-js.min.js.map

Large diffs are not rendered by default.

231 changes: 140 additions & 91 deletions enigma-js/lib/enigma-js.node.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enigma-js/lib/enigma-js.node.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enigma-js/lib/enigma-js.node.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enigma-js/lib/enigma-js.node.min.js.map

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion enigma-js/src/Enigma.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class Enigma {
const emitter = new EventEmitter();
(async () => {
// TODO: never larger that 53-bit?
const nonce = parseInt(await this.enigmaContract.methods.getUserTaskDeployments(sender).call());
const nonce = await this.getNonce(sender, isContractDeploymentTask);
const scAddr = isContractDeploymentTask ? utils.generateScAddr(sender, nonce) : scAddrOrPreCode;
let preCode;
let preCodeGzip;
Expand Down Expand Up @@ -717,6 +717,32 @@ export default class Enigma {
return seed;
}

/**
* Return the nonce for the current task, the maximum of the nonce returned by the contract and the
* one we are tracking locally to avoid conflicts in concurrency.
*
* @param {string} sender - ETH address for task sender
* @param {boolean} trueNonce - if this is a contract deployment use a true nonce from the contract
* @return {Number} nonce
*/
async getNonce(sender, trueNonce = false) {
const isBrowser = typeof window !== 'undefined';
const remoteNonce = parseInt(await this.enigmaContract.methods.getUserTaskDeployments(sender).call());
let nonce;
if (trueNonce) {
nonce = remoteNonce;
} else {
const localNonce = isBrowser ? parseInt(window.localStorage.getItem('nonce-'+sender)) :
parseInt(this.taskKeyLocalStorage['nonce-'+sender]);
// If localNonce undefined, return remoteNonce, otherwise Math.max() returns NaN
nonce = localNonce ? Math.max(remoteNonce, localNonce) : remoteNonce;
}
// Store the next nonce we will use, to match what the query from the contract will return
isBrowser ? window.localStorage.setItem('nonce-'+sender, nonce+1) :
this.taskKeyLocalStorage['nonce-'+sender] = nonce+1;
return nonce;
}

/**
* Create a task to deploy a secret contract - creates base task, creates task record, and sends task to the
* Enigma network. This is the most efficient and likely most common method for creating and deploying a secret
Expand Down
2 changes: 0 additions & 2 deletions enigma-js/test/Enigma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,6 @@ describe('Enigma tests', () => {
{t: 'bytes', v: task.encryptedAbiEncodedArgs},
);
expect(enigma.web3.eth.accounts.recover(msg, task.userTaskSig)).toEqual(stakingAccounts[0]);
expect(task.nonce).toEqual(6);
expect(task.receipt).toBeTruthy();
expect(task.transactionHash).toBeTruthy();
expect(task.taskId).toBeTruthy();
Expand Down Expand Up @@ -1939,7 +1938,6 @@ describe('Enigma tests', () => {
{t: 'bytes', v: task.encryptedAbiEncodedArgs},
);
expect(enigma.web3.eth.accounts.recover(msg, task.userTaskSig)).toEqual(stakingAccounts[0]);
expect(task.nonce).toEqual(9);
expect(task.receipt).toBeTruthy();
expect(task.transactionHash).toBeTruthy();
expect(task.taskId).toBeTruthy();
Expand Down