Skip to content
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

refactor: double hash #206

Merged
merged 8 commits into from
Oct 12, 2023
Merged

refactor: double hash #206

merged 8 commits into from
Oct 12, 2023

Conversation

andreivladbrg
Copy link
Member

Closes #204 and #205

Copy link
Member

@PaulRBerg PaulRBerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have stopped my review when I saw the binarySearch and sort functions implemented in our code base.

Both OpenZeppelin and Solady offer implementations for this:

There's no point in maintaining mirror implementations of the same code, so we should just import and use one of these two libraries

@andreivladbrg
Copy link
Member Author

Firstly, there is no function for sort implemented by either OpenZeppelin or solady, please review carefully what that function does. Secondly, both versions you mentioned use a uint256 parameter, not bytes32, tho this isn't ultimately the big problem because conversion is possible. The main issue with these functions is that they require a storage parameter, not a memory or calldata parameter, which is what we need.

Was getting this error:
image

@PaulRBerg
Copy link
Member

There are mupltiple sort functions available in Solady - see LibSort.

Then, it is true that the searchSorted function require a storage variable, but why is that a problem?

You can just define a storage array in the Defaults contract, like this:

bytes32[] public leaves = new bytes32[](RECIPIENTS_COUNT);

constructor() {
    leaves[0] = MerkleBuilder.computeLeaf(INDEX1, users.recipient1.addr, CLAIM_AMOUNT);
    leaves[1] = MerkleBuilder.computeLeaf(INDEX2, users.recipient2.addr, CLAIM_AMOUNT);
    leaves[2] = MerkleBuilder.computeLeaf(INDEX3, users.recipient3.addr, CLAIM_AMOUNT);
    leaves[3] = MerkleBuilder.computeLeaf(INDEX4, users.recipient4.addr, CLAIM_AMOUNT);
}

In general, I would like us to prefer to import code rather than copy-paste it, so that we don't have to (i) understand it and (ii) maintain it.

@andreivladbrg
Copy link
Member Author

There are mupltiple sort functions available in Solady - see LibSort.

Did not know about LibSort

(i) understand it and (ii) maintain it.

makes sense, going to remove them

@PaulRBerg
Copy link
Member

Perfect, thanks!

andreivladbrg and others added 3 commits October 10, 2023 18:44
chore: improve writing in comments
test: improve variable names
test: remove "_initMerkleTree"
PaulRBerg
PaulRBerg previously approved these changes Oct 12, 2023
Copy link
Member

@PaulRBerg PaulRBerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, let's just add the explanatory comment mentioned below

test/fork/merkle-streamer/MerkleStreamerLL.t.sol Outdated Show resolved Hide resolved
@andreivladbrg
Copy link
Member Author

I see that you want to remove the _initMerkleTree function, I believe it would be better to keep it because we would have all the logic inside the MERKLE STREAMER header "scope" so that when you read those constants/variables you don't have to switch back and forth to constructor

@PaulRBerg
Copy link
Member

I insist on keeping those declarations in the constructor, because they pertain to initializing immutable variables. See my latest commit.

I've also noticed that the constructor was defined in the middle of the contract, and so I've moved it to the top.

I think it's looking good now!

@andreivladbrg
Copy link
Member Author

I insist on keeping those declarations in the constructor, because they pertain to initializing immutable variables

well, in case of LEAVES arrays cannot be constant/immutable

@PaulRBerg
Copy link
Member

in case of LEAVES arrays cannot be constant/immutable

Yes, but they are treated as immutables. Let's keep the code like this

Copy link
Member

@PaulRBerg PaulRBerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@andreivladbrg andreivladbrg merged commit c58bf3e into main Oct 12, 2023
@PaulRBerg PaulRBerg deleted the refactor/double-hash branch October 12, 2023 12:50
andreivladbrg added a commit that referenced this pull request Dec 16, 2023
* refactor: double hash the leaf in claim function

* test: add binarySearch function in MerkleBuilder

test: add sort function in Merkle Builder
test: sort tree leaves in MerkleStreamer tests

* test: update Precompiles bytecode

* test: use solady to sort merkle tree

* test: uppercase constants

chore: improve writing in comments
test: improve variable names
test: remove "_initMerkleTree"

* test: add comment explaining why the Merkle tree leaves are sorted

* test: reorder functions in Defaults

---------

Co-authored-by: Paul Razvan Berg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adapt leaf hashing algorithm for MerkleStreamer
2 participants