Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nonce revert on sendAsync error #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 8 additions & 7 deletions app/hooked-web3-provider.es6
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var factory = function(web3) {

// We can't support *all* synchronous methods because we have to call out to
// a transaction signer. So removing the ability to serve any.
send(payload, callback) {
send(payload) {
var requests = payload;
if (!(requests instanceof Array)) {
requests = [requests];
Expand All @@ -24,19 +24,20 @@ var factory = function(web3) {
}
}

var finishedWithRewrite = () => {
return super.send(payload, callback);
};

return this.rewritePayloads(0, requests, {}, finishedWithRewrite);
return super.send(payload);
}

// Catch the requests at the sendAsync level, rewriting all sendTransaction
// methods to sendRawTransaction, calling out to the transaction_signer to
// get the data for sendRawTransaction.
sendAsync(payload, callback) {
var backupNonces = Object.assign({}, this.global_nonces);
var finishedWithRewrite = () => {
super.sendAsync(payload, callback);
super.sendAsync(payload, function(err, res) {
if (err || res.error)
this.global_nonces = backupNonces;
return callback(err, res);
}.bind(this));
};

var requests = payload;
Expand Down
32 changes: 15 additions & 17 deletions build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5200,7 +5200,7 @@ var module = module || undefined;

var __provisioner = {
provision_contracts: function(scope) {
scope.__contracts = JSON.parse("{\n \"Example\": {\n \"source\": \"/Users/tim/Documents/workspace/Consensys/hooked-web3-provider/contracts/Example.sol\",\n \"binary\": \"0x60606040525b5b60888060136000396000f30060606040526000357c010000000000000000000000000000000000000000000000000000000090048063130d6dc514604157806360fe47b114606057603f565b005b604a6004506071565b6040518082815260200191505060405180910390f35b606f600480359060200150607a565b005b60006000505481565b806000600050819055505b5056\",\n \"abi\": [\n {\n \"inputs\": [],\n \"constant\": true,\n \"name\": \"test_value\",\n \"outputs\": [\n {\n \"type\": \"uint256\",\n \"name\": \"\"\n }\n ],\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"type\": \"uint256\",\n \"name\": \"new_value\"\n }\n ],\n \"constant\": false,\n \"name\": \"set\",\n \"outputs\": [],\n \"type\": \"function\"\n },\n {\n \"inputs\": [],\n \"type\": \"constructor\"\n }\n ]\n }\n}");
scope.__contracts = JSON.parse("{\n \"Example\": {\n \"source\": \"/Users/ck/consensys/noncense/persona-integration/myhooked/contracts/Example.sol\"\n }\n}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ckeenan. I don't think this change should have made it in. Can you tell me what version of Truffle you're using? Regardless, the build should be run using the latest version, as both the before and after have issues with the contract's source path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tcoulter was using 0.1.0, rebuilding now w latest and removing truffle from package.json


for (var key in scope.__contracts) {
var contract = scope.__contracts[key];
Expand Down Expand Up @@ -50046,9 +50046,7 @@ var factory = function factory(web3) {

_createClass(HookedWeb3Provider, [{
key: "send",
value: function send(payload, callback) {
var _this = this;

value: function send(payload) {
var requests = payload;
if (!(requests instanceof Array)) {
requests = [requests];
Expand Down Expand Up @@ -50081,11 +50079,7 @@ var factory = function factory(web3) {
}
}

var finishedWithRewrite = function finishedWithRewrite() {
return _get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "send", _this).call(_this, payload, callback);
};

return this.rewritePayloads(0, requests, {}, finishedWithRewrite);
return _get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "send", this).call(this, payload);
}

// Catch the requests at the sendAsync level, rewriting all sendTransaction
Expand All @@ -50094,10 +50088,14 @@ var factory = function factory(web3) {
}, {
key: "sendAsync",
value: function sendAsync(payload, callback) {
var _this2 = this;
var _this = this;

var backupNonces = Object.assign({}, this.global_nonces);
var finishedWithRewrite = function finishedWithRewrite() {
_get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "sendAsync", _this2).call(_this2, payload, callback);
_get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "sendAsync", _this).call(_this, payload, (function (err, res) {
if (err || res.error) this.global_nonces = backupNonces;
return callback(err, res);
}).bind(_this));
};

var requests = payload;
Expand All @@ -50114,7 +50112,7 @@ var factory = function factory(web3) {
}, {
key: "rewritePayloads",
value: function rewritePayloads(index, requests, session_nonces, finished) {
var _this3 = this;
var _this2 = this;

if (index >= requests.length) {
return finished();
Expand All @@ -50127,7 +50125,7 @@ var factory = function factory(web3) {
if (err != null) {
return finished(err);
}
return _this3.rewritePayloads(index + 1, requests, session_nonces, finished);
return _this2.rewritePayloads(index + 1, requests, session_nonces, finished);
};

// If this isn't a transaction we can modify, ignore it.
Expand Down Expand Up @@ -50158,7 +50156,7 @@ var factory = function factory(web3) {
// hence the need for global_nonces.
// We call directly to our own sendAsync method, because the web3 provider
// is not guaranteed to be set.
_this3.sendAsync({
_this2.sendAsync({
jsonrpc: '2.0',
method: 'eth_getTransactionCount',
params: [sender, "pending"],
Expand Down Expand Up @@ -50186,18 +50184,18 @@ var factory = function factory(web3) {
// Note that if our session nonce is lower than what we have cached
// across all transactions (and not just this batch) use our cached
// version instead, even if
var final_nonce = Math.max(nonce, _this3.global_nonces[sender] || 0);
var final_nonce = Math.max(nonce, _this2.global_nonces[sender] || 0);

// Update the transaction parameters.
tx_params.nonce = web3.toHex(final_nonce);

// Update caches.
session_nonces[sender] = final_nonce + 1;
_this3.global_nonces[sender] = final_nonce + 1;
_this2.global_nonces[sender] = final_nonce + 1;

// If our transaction signer does represent the address,
// sign the transaction ourself and rewrite the payload.
_this3.transaction_signer.signTransaction(tx_params, function (err, raw_tx) {
_this2.transaction_signer.signTransaction(tx_params, function (err, raw_tx) {
if (err != null) {
return next(err);
}
Expand Down
30 changes: 14 additions & 16 deletions build/hooked-web3-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ var factory = function factory(web3) {

_createClass(HookedWeb3Provider, [{
key: "send",
value: function send(payload, callback) {
var _this = this;

value: function send(payload) {
var requests = payload;
if (!(requests instanceof Array)) {
requests = [requests];
Expand Down Expand Up @@ -68,11 +66,7 @@ var factory = function factory(web3) {
}
}

var finishedWithRewrite = function finishedWithRewrite() {
return _get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "send", _this).call(_this, payload, callback);
};

return this.rewritePayloads(0, requests, {}, finishedWithRewrite);
return _get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "send", this).call(this, payload);
}

// Catch the requests at the sendAsync level, rewriting all sendTransaction
Expand All @@ -81,10 +75,14 @@ var factory = function factory(web3) {
}, {
key: "sendAsync",
value: function sendAsync(payload, callback) {
var _this2 = this;
var _this = this;

var backupNonces = Object.assign({}, this.global_nonces);
var finishedWithRewrite = function finishedWithRewrite() {
_get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "sendAsync", _this2).call(_this2, payload, callback);
_get(Object.getPrototypeOf(HookedWeb3Provider.prototype), "sendAsync", _this).call(_this, payload, (function (err, res) {
if (err || res.error) this.global_nonces = backupNonces;
return callback(err, res);
}).bind(_this));
};

var requests = payload;
Expand All @@ -101,7 +99,7 @@ var factory = function factory(web3) {
}, {
key: "rewritePayloads",
value: function rewritePayloads(index, requests, session_nonces, finished) {
var _this3 = this;
var _this2 = this;

if (index >= requests.length) {
return finished();
Expand All @@ -114,7 +112,7 @@ var factory = function factory(web3) {
if (err != null) {
return finished(err);
}
return _this3.rewritePayloads(index + 1, requests, session_nonces, finished);
return _this2.rewritePayloads(index + 1, requests, session_nonces, finished);
};

// If this isn't a transaction we can modify, ignore it.
Expand Down Expand Up @@ -145,7 +143,7 @@ var factory = function factory(web3) {
// hence the need for global_nonces.
// We call directly to our own sendAsync method, because the web3 provider
// is not guaranteed to be set.
_this3.sendAsync({
_this2.sendAsync({
jsonrpc: '2.0',
method: 'eth_getTransactionCount',
params: [sender, "pending"],
Expand Down Expand Up @@ -173,18 +171,18 @@ var factory = function factory(web3) {
// Note that if our session nonce is lower than what we have cached
// across all transactions (and not just this batch) use our cached
// version instead, even if
var final_nonce = Math.max(nonce, _this3.global_nonces[sender] || 0);
var final_nonce = Math.max(nonce, _this2.global_nonces[sender] || 0);

// Update the transaction parameters.
tx_params.nonce = web3.toHex(final_nonce);

// Update caches.
session_nonces[sender] = final_nonce + 1;
_this3.global_nonces[sender] = final_nonce + 1;
_this2.global_nonces[sender] = final_nonce + 1;

// If our transaction signer does represent the address,
// sign the transaction ourself and rewrite the payload.
_this3.transaction_signer.signTransaction(tx_params, function (err, raw_tx) {
_this2.transaction_signer.signTransaction(tx_params, function (err, raw_tx) {
if (err != null) {
return next(err);
}
Expand Down
2 changes: 1 addition & 1 deletion build/hooked-web3-provider.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
"author": "Tim Coulter",
"main": "build/hooked-web3-provider.js",
"private": false,
"scripts": {
"build": "truffle build"
},
"repository": {
"type": "git",
"url": "https://github.com/consensys/hooked-web3-provider.git"
},
"devDependencies": {
"jquery": "*",
"ethereumjs-accounts": "git+https://github.com/tcoulter/ethereumjs-accounts.git",
"truffle": "*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truffle isn't built to be a local dependency stored in node_modules. It really should be, but that's a feature for later. We should remove it from package.json

"bignumber.js": "*"
},
"license": "MIT License. Copyright Consensys LLC, and authors. All rights reserved."
Expand Down