Skip to content

Commit

Permalink
support 0x80 as a false boolean encoding (#6)
Browse files Browse the repository at this point in the history
* support 0x80 as a false boolean encoding

* fix travis

* add back in ganache into travis

* bump truffle/ganache versions

* commit to a single truffle config file

* specify ganache port in travis

* show ganache output

* use node verison 12 and supress output

* fix travis.yml
  • Loading branch information
hamdiallam authored Aug 25, 2020
1 parent e681e25 commit 2e98786
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
language: node_js

node_js: "node"
node_js:
- 12

before_script:
- npm install -g truffle@5.0.1 ganache-cli@6.1.8
- npm install -g truffle@5.1.41 ganache-cli@6.10.1
- npm install

script:
- ganache-cli > /dev/null &
- ganache-cli -p 8545 > /dev/null &
- sleep 5
- rm -rf build
- truffle migrate
- truffle test
- npm run test
12 changes: 10 additions & 2 deletions contracts/RLPReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ library RLPReader {
return result;
}

// any non-zero byte is considered true
// any non-zero byte except "0x80" is considered true
function toBoolean(RLPItem memory item) internal pure returns (bool) {
require(item.len == 1);
uint result;
Expand All @@ -144,7 +144,15 @@ library RLPReader {
result := byte(0, mload(memPtr))
}

return result == 0 ? false : true;
// SEE Github Issue #5.
// Summary: Most commonly used RLP libraries (i.e Geth) will encode
// "0" as "0x80" instead of as "0". We handle this edge case explicitly
// here.
if (result == 0 || result == STRING_SHORT_START) {
return false;
} else {
return true;
}
}

function toAddress(RLPItem memory item) internal pure returns (address) {
Expand Down
22 changes: 15 additions & 7 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions test/basic-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ contract("RLPReader", async (accounts) => {

// toBoolean
result = await helper.toBoolean.call(toHex(rlp.encode(1)));
assert(result == true, "Incorrect toBoolean conversion");
assert(result == true, "Incorrect toBoolean true conversion");

result = await helper.toBoolean.call(toHex(rlp.encode(0)));
assert(result == false, "Incorrect toBoolean false conversion");

// Mix of data types
str = [accounts[0], 1, 65537];
Expand Down Expand Up @@ -282,7 +285,7 @@ contract("RLPReader", async (accounts) => {
totalDifficulty: '10690776258913596267754',
};
const rlpHeader = toRLPHeader(block);
const result = await helper.toBlockHeader.call(rlpHeader, {});
const result = await helper.toBlockHeader.call(rlpHeader);
assert(result.parentHash == block.parentHash, "parentHash not equal");
assert(result.sha3Uncles == block.sha3Uncles, "sha3Uncles not equal");
assert(result.stateRoot == block.stateRoot, "stateRoot not equal");
Expand Down
7 changes: 7 additions & 0 deletions truffle-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: 'localhost',
port: 8545,
network_id: '*'
}
}
};
11 changes: 0 additions & 11 deletions truffle.js

This file was deleted.

0 comments on commit 2e98786

Please sign in to comment.