-
Notifications
You must be signed in to change notification settings - Fork 242
encodeParam and encodeParams functions #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from 9 commits
37b2612
85dda17
e3b49ea
bc0e525
a97c86d
fee7fc1
ecf2f5c
8542235
a85c5aa
b3ba05d
79100ce
9b4f8f3
b056828
0aa8d3c
e96f7c0
1fa2b95
da862e2
480b2f6
bdeee78
04c2048
2530b15
45727e3
97327f3
c44a025
b647866
aa08e27
b05c010
9c84dc3
dbce97e
dd2f8f2
22ec764
93e5ba1
ad1b7f7
3a8fb64
c4e0fb1
a026e22
5c61d10
c37d4fd
3d8c54f
9734f9c
8cf6039
3c909ef
584f4bc
05d3d26
d2268c8
722e25e
dbb0e06
db9342f
b18c1a5
37f8eff
9ff0bda
032f577
7366642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,31 @@ contract ACLSyntaxSugar { | |
|
|
||
|
|
||
| contract ACLHelpers { | ||
|
|
||
| enum Op { NONE, EQ, NEQ, GT, LT, GTE, LTE, RET, NOT, AND, OR, XOR, IF_ELSE } // op types | ||
|
|
||
| struct Param { | ||
| uint8 id; | ||
| uint8 op; | ||
| uint240 value; // even though value is an uint240 it can store addresses | ||
| // in the case of 32 byte hashes losing 2 bytes precision isn't a huge deal | ||
| // op and id take less than 1 byte each so it can be kept in 1 sstore | ||
| } | ||
|
|
||
| function encodeParams(Param[] params) internal pure returns (uint256[]) { | ||
| uint256[] memory encodedParams = new uint256[](params.length); | ||
|
|
||
| for (uint i = 0; i < params.length; i++) { | ||
| encodedParams[i] = encodeParam(params[i]); | ||
| } | ||
|
|
||
| return encodedParams; | ||
| } | ||
|
|
||
| function encodeParam(Param param) internal pure returns (uint256) { | ||
| return uint256(param.id) << 248 | uint256(param.op) << 240 | param.value; | ||
| } | ||
|
||
|
|
||
| function decodeParamOp(uint256 _x) internal pure returns (uint8 b) { | ||
| return uint8(_x >> (8 * 30)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| pragma solidity 0.4.24; | ||
|
|
||
| import "./helpers/Assert.sol"; | ||
| import "../acl/ACLSyntaxSugar.sol"; | ||
|
|
||
|
|
||
| contract TestACLHelpers is ACLHelpers { | ||
|
|
||
| function testEncodeParam() public { | ||
| Param memory param = Param({ | ||
| id: 2, | ||
| op: uint8(Op.EQ), | ||
| value: 5294967297 | ||
| }); | ||
|
|
||
| uint256 encodedParam = encodeParam(param); | ||
|
|
||
| (uint32 id, uint32 op, uint32 value) = decodeParamsList(encodedParam); | ||
|
|
||
| Assert.equal(uint256(param.id), uint256(id), "Encoded id is not equal"); | ||
| Assert.equal(uint256(param.op), uint256(op), "Encoded op is not equal"); | ||
| Assert.equal(uint256(param.value), uint256(value), "Encoded value is not equal"); | ||
| } | ||
|
|
||
| function testEncodeParams() public { | ||
| Param[] memory params = new Param[](2); | ||
|
|
||
| params[0] = Param({ | ||
| id: 1, | ||
| op: uint8(Op.EQ), | ||
| value: 5294967297 | ||
| }); | ||
|
|
||
| params[1] = Param({ | ||
| id: 2, | ||
| op: uint8(Op.EQ), | ||
| value: 5294967297 | ||
| }); | ||
|
|
||
|
|
||
| uint256[] memory encodedParam = encodeParams(params); | ||
|
|
||
| (uint32 id0, uint32 op0, uint32 value0) = decodeParamsList(encodedParam[0]); | ||
|
||
|
|
||
| Assert.equal(uint256(params[0].id), uint256(id0), "Encoded id is not equal"); | ||
| Assert.equal(uint256(params[0].op), uint256(op0), "Encoded op is not equal"); | ||
| Assert.equal(uint256(params[0].value), uint256(value0), "Encoded value is not equal"); | ||
|
|
||
| (uint32 id1, uint32 op1, uint32 value1) = decodeParamsList(encodedParam[1]); | ||
|
|
||
| Assert.equal(uint256(params[1].id), uint256(id1), "Encoded id is not equal"); | ||
| Assert.equal(uint256(params[1].op), uint256(op1), "Encoded op is not equal"); | ||
| Assert.equal(uint256(params[1].value), uint256(value1), "Encoded value is not equal"); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's test
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think it's a good idea, but let's wait for @sohkai opinion, maybe there is a reason I don't know for them to be there.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I didn't notice they were only in the tests. I think moving them could be helpful; AFAIK they're not simply because we only used them in test functionality. We should either add comments to explain how they work or make |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const runSolidityTest = require('./helpers/runSolidityTest') | ||
|
|
||
| runSolidityTest('TestACLHelpers') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about putting this in another contract, e.g.
ACLParams?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I agree they fit more inside
ACLParams