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 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
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
175 changes: 128 additions & 47 deletions build/app.js

Large diffs are not rendered by default.

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.