This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from attente/fix-transaction-count-leading-zeros
Add tests for leading zeros in hex values
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
var assert = require("assert"); | ||
var Web3 = require("web3"); | ||
var TestRPC = require("../index.js"); | ||
var to = require("../lib/utils/to.js"); | ||
|
||
describe("to.hexWithoutLeadingZeroes", function() { | ||
it("should print '0x0' for input 0", function(done) { | ||
assert.equal(to.hexWithoutLeadingZeroes(0), "0x0"); | ||
done(); | ||
}); | ||
|
||
it("should print '0x0' for input '0'", function(done) { | ||
assert.equal(to.hexWithoutLeadingZeroes("0"), "0x0"); | ||
done(); | ||
}); | ||
|
||
it("should print '0x0' for input '000'", function(done) { | ||
assert.equal(to.hexWithoutLeadingZeroes("000"), "0x0"); | ||
done(); | ||
}); | ||
|
||
it("should print '0x0' for input '0x000'", function(done) { | ||
assert.equal(to.hexWithoutLeadingZeroes("0x000"), "0x0"); | ||
done(); | ||
}); | ||
|
||
it("should print '0x20' for input '0x0020'", function(done) { | ||
assert.equal(to.hexWithoutLeadingZeroes("0x0020"), "0x20"); | ||
done(); | ||
}); | ||
}); | ||
|
||
function noLeadingZeros(result) { | ||
if (typeof result === "string") { | ||
if (/^0x/.test(result)) { | ||
assert.equal(result, to.hexWithoutLeadingZeroes(result)); | ||
} | ||
} else if (typeof result === "object") { | ||
for (var key in result) { | ||
if (result.hasOwnProperty(key)) { | ||
noLeadingZeros(result[key]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
describe("JSON-RPC Response", function() { | ||
var web3 = new Web3(); | ||
var provider = TestRPC.provider(); | ||
web3.setProvider(provider); | ||
|
||
var accounts; | ||
before(function(done) { | ||
web3.eth.getAccounts(function(err, accs) { | ||
if (err) return done(err); | ||
accounts = accs; | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should not have leading zeros in hex strings", function(done) { | ||
var request = { | ||
"jsonrpc": "2.0", | ||
"method": "eth_getTransactionCount", | ||
"params": [ | ||
accounts[0], | ||
"pending" | ||
], | ||
"id": 1 | ||
}; | ||
|
||
provider.sendAsync(request, function(err, result) { | ||
noLeadingZeros(result); | ||
|
||
request = { | ||
"jsonrpc": "2.0", | ||
"method": "eth_sendTransaction", | ||
"params": [ | ||
{ | ||
"from": accounts[0], | ||
"to": accounts[1], | ||
"value": "0x100000000" | ||
} | ||
], | ||
"id": 2 | ||
}; | ||
|
||
provider.sendAsync(request, function(err, result) { | ||
noLeadingZeros(result); | ||
|
||
request = { | ||
"jsonrpc": "2.0", | ||
"method": "eth_getTransactionCount", | ||
"params": [ | ||
accounts[0], | ||
"pending" | ||
], | ||
"id": 3 | ||
}; | ||
|
||
provider.sendAsync(request, function(err, result) { | ||
noLeadingZeros(result); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |