Skip to content

Commit

Permalink
v2 payload data length retreival
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdiallam committed Feb 24, 2019
1 parent 57c1886 commit cd39a6a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.0.0
### Added
- `payloadLen` returns the length of the data payload

### Changed/Breaking
- `size` to `rlpLen` returns the size of the rlp payload

## 1.2.3
### Added
- `toUintStrict` strict uint conversion. Encoded data must be 32 bytes in length
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Transformations(All take an RLPItem as an arg):
5. `toUintStrict(RLPItem) uint` : returns the encoded uint. Encoded data must be padded to 32 bytes.
6. `toBoolean(RLPItem) bool`: returns the encoded boolean
7. `toRlpBytes(RLPItem) bytes `: returns the raw rlp encoded byte form
8. `size(RLPItem) uint` : returns the byte length of the rlp item
8. `rlpLen(RLPItem) uint` : returns the byte length of the rlp item
9. `payloadLen(RLPItem) uint` : returns the byte length of the data payload

**Note**: The reader contract only provides only these conversion functions. All other solidity data types can be derived from
this base. For example, a `bytes32` encoded data type is equivalent to `bytes32(toUint(RLPItem))`. Start with a uint and convert from there.
Expand Down
9 changes: 7 additions & 2 deletions contracts/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ contract Helper {
return _itemLength(memPtr);
}

function size(bytes memory item) public pure returns (uint) {
function rlpLen(bytes memory item) public pure returns (uint) {
RLPReader.RLPItem memory rlpItem = item.toRlpItem();
return rlpItem.size();
return rlpItem.rlpLen();
}

function payloadLen(bytes memory item) public pure returns (uint) {
RLPReader.RLPItem memory rlpItem = item.toRlpItem();
return rlpItem.payloadLen();
}

function numItems(bytes memory item) public pure returns (uint) {
Expand Down
9 changes: 8 additions & 1 deletion contracts/RLPReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ library RLPReader {
/*
* @param item RLP encoded bytes
*/
function size(RLPItem memory item) internal pure returns (uint) {
function rlpLen(RLPItem memory item) internal pure returns (uint) {
return item.len;
}

/*
* @param item RLP encoded bytes
*/
function payloadLen(RLPItem memory item) internal pure returns (uint) {
return item.len - _payloadOffset(item.memPtr);
}

/*
* @param item RLP encoded list in bytes
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "solidity-rlp",
"author": "Hamdi Allam",
"version": "1.2.3",
"version": "2.0.0",
"description": "solidity rlp encoder/decoder",
"keywords": [
"solidity",
Expand Down
13 changes: 13 additions & 0 deletions test/basic-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ contract("RLPReader", async (accounts) => {
assert(result.toNumber() == 1, "Incorrect calculate rlp item byte length for empty list");
});

it("detects the payload length of encoded data", async () => {
let result;

result = await helper.payloadLen.call(toHex(rlp.encode(1)));
assert(result.toNumber() == 1, "incorrect payload length of a single byte encoding");

result = await helper.payloadLen.call(toHex(rlp.encode(toHex(Array(36).fill(0).join('')))));
assert(result.toNumber() == 18, "incorrect payload length of a 18 bytes");

result = await helper.payloadLen.call(toHex(rlp.encode(toHex(Array(200).fill(0).join('')))));
assert(result.toNumber() == 100, "incorrect payload length of a 100 bytes");
});

it("detects the correct amount of items in a list", async () => {
let assertString = "Number of items in an rlp encoded list wrongly detected: ";
let str = [1, 2, 3];
Expand Down

0 comments on commit cd39a6a

Please sign in to comment.