Skip to content

Commit

Permalink
Merge branch 'fix-leadingzeros' into gislik
Browse files Browse the repository at this point in the history
* fix-leadingzeros:
  Fixing leading zeros for hex encoded quantities in tx trufflesuite#166
  Complete removal
  log reason string on transaction failure
  • Loading branch information
gislik committed Sep 4, 2018
2 parents 3d24ce5 + b12a29a commit 88592d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
27 changes: 4 additions & 23 deletions lib/statemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ StateManager.prototype.printTransactionReceipt = function(tx_hash, error, callba

if (error) {
self.logger.log(" Runtime Error: " + error.error);
if (error.reason) {
self.logger.log(" Revert reason: " + error.reason);
}
}

self.logger.log("");
Expand Down Expand Up @@ -877,29 +880,7 @@ StateManager.prototype.createFakeTransactionWithCorrectNonce = function(rawTx, f
`tx has nonce of: ${to.number(tx.nonce)}`))
}
}

// If we're calling a contract, check to make sure the address specified is a contract address
if (_transactionIsContractCall(rawTx)) {
self.getCode(to.hex(rawTx.to), 'latest', function(err, code) {
if (err) {
callback(err);
} else if (code === '0x0') {
callback(new TXRejectedError(`Attempting to run transaction which calls a contract function, but recipient address ${to.hex(rawTx.to)} is not a contract address`))
} else {
callback(null, tx)
}
});
} else {
callback(null, tx)
}
callback(null, tx)
});
}

// returns true when transaction has a non-null, non-empty to and data field
var _transactionIsContractCall = function(rawTx) {
let recipient = to.hex(rawTx.to || '0x0')
let data = to.hex(rawTx.data || '0x0')

return recipient !== '0x0' && data !== '0x0'
}
module.exports = StateManager;
16 changes: 8 additions & 8 deletions lib/utils/txhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ module.exports = {
}
var resultJSON = {
hash: to.hex(tx.hash()),
nonce: to.hex(tx.nonce),
nonce: to.rpcQuantityHexString(tx.nonce),
blockHash: to.hex(block.hash()),
blockNumber: to.hex(block.header.number),
transactionIndex: to.hex(transactionIndex),
from: to.hex(tx.from),
to: to.hex(tx.to),
value: to.hex(tx.value),
gas: to.hex(tx.gasLimit),
gasPrice: to.hex(tx.gasPrice),
input: to.hex(tx.data),
value: to.rpcQuantityHexString(tx.value),
gas: to.rpcQuantityHexString(tx.gasLimit),
gasPrice: to.rpcQuantityHexString(tx.gasPrice),
input: to.rpcQuantityHexString(tx.data),
};

if (tx.v && tx.v.length > 0 &&
tx.r && tx.r.length > 0 &&
tx.s && tx.s.length > 0) {
resultJSON.v = to.hex(tx.v);
resultJSON.r = to.hex(tx.r);
resultJSON.s = to.hex(tx.s);
resultJSON.v = to.rpcQuantityHexString(tx.v);
resultJSON.r = to.rpcQuantityHexString(tx.r);
resultJSON.s = to.rpcQuantityHexString(tx.s);
}

return resultJSON;
Expand Down
49 changes: 49 additions & 0 deletions test/hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function noLeadingZeros(method, result, path) {
}
}

function noLeadingZerosTx(tx) {
const fields = ['nonce', 'gasPrice', 'gas', 'value', 'input'];
for (var i in fields) {
const field = fields[i]
if (typeof tx[field] === "undefined") {
assert.fail(`Field ${field} should not be undefined`)
}
assert.equal(tx[field], to.rpcQuantityHexString(tx[field]), assert `Quantity field ${field} in transaction is missing or has leading zeroes.`);
}
}


describe("JSON-RPC Response", function() {
var web3 = new Web3();
var provider = Ganache.provider();
Expand Down Expand Up @@ -126,4 +138,41 @@ describe("JSON-RPC Response", function() {
});
});
});

it("should not have leading zeros in rpc tx quantity hex strings", function(done) {
var request = {
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [
{
"from": accounts[0],
"to": accounts[1],
"value": "0x100000000",
"gas": "0x015F90",
"gasPrice": "0x01",
}
],
"id": 1
};
provider.sendAsync(request, function() {
// force nonce to be at least 1 (since 0 encodes to 0x0)
provider.sendAsync(request, function(err, result) {
request = {
"jsonrpc": "2.0",
"method": "eth_getTransactionByHash",
"params": [
result.result
],
"id": 2
};

provider.sendAsync(request, function(err, result) {

noLeadingZerosTx(result.result);
done();

});
});
});
});
});
12 changes: 0 additions & 12 deletions test/transaction_rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,6 @@ describe("Transaction rejection", function() {
}, /sender doesn't have enough funds to send tx/, done)
});

it("should reject contract transaction if 'to' is not a contract address", function(done) {
let params = {
to: '0x0000000000000000000000001234000000000000'
}

testTransactionForRejection(
params,
new RegExp(`Attempting to run transaction which calls a contract function, but recipient address ${params.to} is not a contract address`),
done
)
});

function testTransactionForRejection(paramsOverride, messageRegex, done) {
let params = Object.assign({
from: accounts[0],
Expand Down

0 comments on commit 88592d0

Please sign in to comment.