Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Audit the use of 0x, 0x0, and quantity and fix (#197)
Browse files Browse the repository at this point in the history
Fixes #579
Fixes #582
Fixes #907
Fixes #59
Fixes #425
  • Loading branch information
davidmurdoch committed Oct 19, 2018
1 parent 52aebd8 commit 9a4cb88
Show file tree
Hide file tree
Showing 17 changed files with 458 additions and 288 deletions.
4 changes: 2 additions & 2 deletions lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ BlockchainDouble.prototype.getQueuedNonce = function(address, callback) {
//tx.from==address
if (to.hex(tx.from) != to.hex(address)) return;

var pending_nonce = to.number(tx.nonce);
var pending_nonce = to.number(tx.nonce) || 0;
//If this is the first queued nonce for this address we found,
//or it's higher than the previous highest, note it.
if (nonce===null || pending_nonce > nonce) {
Expand All @@ -370,7 +370,7 @@ BlockchainDouble.prototype.getQueuedNonce = function(address, callback) {

//If we found a queued transaction nonce, return one higher
//than the highest we found
if (nonce!=null) return callback(null, nonce+1);
if (nonce!=null) return callback(null, Buffer.from([nonce + 1]));

this.stateTrie.get(address, function(err, val) {
if (err) return callback(err);
Expand Down
12 changes: 6 additions & 6 deletions lib/statemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ StateManager.prototype.queueRawTransaction = function(rawTx, callback) {
gasPrice: tx.gasPrice.toString('hex'),
value: tx.value.toString('hex'),
data: tx.data.toString('hex'),
nonce: tx.nonce.toString('hex'),
nonce: tx.nonce.toString('hex')
}

if (tx.v && tx.v.length > 0 &&
Expand Down Expand Up @@ -620,7 +620,7 @@ StateManager.prototype.processCall = function (from, rawTx, blockNumber, callbac
return callback(err);
}

var result = '0x0'
var result = '0x'
if (!results.error && results.vm.return) {
result = to.hex(results.vm.return);
} else if (results.error) {
Expand All @@ -642,7 +642,7 @@ StateManager.prototype.processGasEstimate = function (from, rawTx, blockNumber,
if (err) {
return callback(err);
}
var result = '0x0'
var result = '0x'
if (!results.error) {
result = results.gasRefund ? to.hex(results.gasUsed.add(results.gasRefund)) : to.hex(results.gasUsed);
} else {
Expand Down Expand Up @@ -874,10 +874,10 @@ StateManager.prototype.createFakeTransactionWithCorrectNonce = function(rawTx, f
// account for transactions waiting in the tx queue
tx.nonce = to.hex(expectedNonce);
} else {
if (to.number(rawTx.nonce) !== to.number(expectedNonce)) {
if (!tx.nonce.equals(expectedNonce)) {
return callback(new TXRejectedError(`the tx doesn't have the correct nonce. ` +
`account has nonce of: ${to.number(expectedNonce)} ` +
`tx has nonce of: ${to.number(tx.nonce)}`))
`account has nonce of: ${to.number(expectedNonce) || 0} ` +
`tx has nonce of: ${to.number(tx.nonce) || 0}`))
}
}
callback(null, tx)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/forkedblockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ ForkedBlockchain.prototype.fetchAccountFromFallback = function(address, block_nu
balance: balance
});

account.exists = code != "0x0" || balance != "0x0" || nonce != "0x0";
account.exists = code != "0x" || balance != "0x0" || nonce != "0x0";

// This puts the code on the trie, keyed by the hash of the code.
// It does not actually link an account to code in the trie.
Expand Down
19 changes: 13 additions & 6 deletions lib/utils/to.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
// Note: Do not use to.hex() when you really mean utils.addHexPrefix().
hex: function(val) {
if (typeof val == "string") {
if (val.indexOf("0x") == 0) {
return val;
if (val.indexOf("0x") === 0) {
return val.trim();
} else {
val = new utils.BN(val);
}
Expand All @@ -32,7 +32,8 @@ module.exports = {
val = this.hex(val);
val = "0x" + val.replace("0x", "").replace(/^0+/, "");

if (val == "0x") {
// RPC Quantities must represent `0` as `0x0`
if (val === "0x") {
val = "0x0";
}

Expand All @@ -57,6 +58,11 @@ module.exports = {
return "0x" + val;
},

nullableRpcDataHexString: function(val, length) {
const rpcDataHex = this.rpcDataHexString(val, length);
return rpcDataHex === "0x" ? null : rpcDataHex;
},

hexWithZeroPadding: function(val) {
val = this.hex(val);
const digits = val.replace("0x", "");
Expand All @@ -71,10 +77,11 @@ module.exports = {
return val;
}
if (typeof val == "string") {
if (val.indexOf('0x') != 0) {
return parseInt(val)
if (val.indexOf('0x') !== 0) {
return parseInt(val, 10)
}
}
return utils.bufferToInt(utils.toBuffer(val));
var bufVal = utils.toBuffer(val);
return utils.bufferToInt(bufVal);
}
};
10 changes: 5 additions & 5 deletions lib/utils/txhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ module.exports = {
}
}
var resultJSON = {
hash: to.rpcDataHexString(tx.hash()),
hash: to.nullableRpcDataHexString(tx.hash()),
nonce: to.rpcQuantityHexString(tx.nonce),
blockHash: to.rpcDataHexString(block.hash()),
blockHash: to.nullableRpcDataHexString(block.hash()),
blockNumber: to.rpcQuantityHexString(block.header.number),
transactionIndex: to.rpcQuantityHexString(transactionIndex),
from: to.rpcDataHexString(tx.from),
to: to.rpcDataHexString(tx.to),
from: to.nullableRpcDataHexString(tx.from),
to: to.nullableRpcDataHexString(tx.to),
value: to.rpcQuantityHexString(tx.value),
gas: to.rpcQuantityHexString(tx.gasLimit),
gasPrice: to.rpcQuantityHexString(tx.gasPrice),
input: to.rpcDataHexString(tx.data),
input: to.nullableRpcDataHexString(tx.data),
};

if (tx.v && tx.v.length > 0 &&
Expand Down

0 comments on commit 9a4cb88

Please sign in to comment.