-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feat (cheatcodes): Introducing ipfs cid v0 to calculate file content #10348
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
Open
Ayushdubey86
wants to merge
17
commits into
foundry-rs:master
Choose a base branch
from
Ayushdubey86:feat-(cheatcodes)-introducing-IPFS-CID-V0-to-calculate-file-content
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6846ce7
Update Cargo.lock
Ayushdubey86 ea2b6b4
Update Cargo.toml
Ayushdubey86 3be499e
Update Cargo.toml
Ayushdubey86 05c58df
Update cheatcodes.json
Ayushdubey86 dbae685
Update vm.rs
Ayushdubey86 52e187e
Update fs.rs
Ayushdubey86 11043b0
Update fs.rs
Ayushdubey86 d8b9e54
Update Vm.sol
Ayushdubey86 f020635
Update Cargo.toml
zerosnacks e2c0690
fmt
Ayushdubey86 52d707b
Update fs.rs
Ayushdubey86 a319dff
Merge branch 'master' into feat-(cheatcodes)-introducing-IPFS-CID-V0-…
Ayushdubey86 6247228
Fix cargo cheats and fmt
grandizzy 7513acb
Merge branch 'master' into feat-(cheatcodes)-introducing-IPFS-CID-V0-…
Ayushdubey86 ca89033
test case
Ayushdubey86 7a1dd8f
test file
Ayushdubey86 bd557a4
test case changes
Ayushdubey86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,19 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity ^0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "cheats/Vm.sol"; | ||
|
||
contract IpfsCidV0Test is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
function testIpfsCidV0() public { | ||
string memory filePath = "testdata/fixtures/File/test.txt"; | ||
|
||
bytes32 cid = vm.ipfsCidV0(filePath); | ||
|
||
bytes32 expectedCid = 0x94da694df5cf2e139206cddcdd6f855baa45e519c5fdbc2e6aa1cf803cfd65d5; | ||
|
||
assertEq(cid, expectedCid); | ||
} | ||
} |
This file contains hidden or 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,5 @@ | ||
--- | ||
title: TestTitle | ||
discussions: TestDiscussion | ||
author: TestAuthor | ||
--- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
does this have to be a path? It just reads the file then hashes it, we could just accept a
bytes
directly.Also, sha256 exists in the EVM itself (precompile 0x00...02), why can this not be a simple utility function written in Solidity where it is needed?
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 point — accepting bytes directly would give more flexibility.
It depends on the user though — some might prefer to pass a filepath (especially for testing existing files).
Maybe we can support both?
I'm fine with either approach for now — happy to go with what you think makes the most sense! @sakulstra @grandizzy
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.
No, too strong opinion.
If it accepted string or similar, we would build our own wrapped which is perfectly fine either.
For us the main usecase is hashing content in external files which is why i suggested path, but i guess there could be usecases that would require hashing strings from solidity. So perhaps string input is more flexible as we could just use
vm.ipfsCidV0 (vm.readFile(path))
and others can skip the file reading and hash content immediately?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.
@DaniPopes for sha256,
That’s a good point, and normally sha256() in Solidity would be sufficient — but this cheatcode targets a different use case.
We need to compute the SHA-256 hash of an off-chain file in the testing environment, prefix it per the multihash spec (IPFS CIDv0), and then pass that result into the EVM. Since Solidity's sha256() only operates on in-memory bytes at runtime, it can’t handle file reads or multihash formatting.
So this utility is kinda necessary
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.
this looks to be more complex than initial draft, the only way I was able to get same CID v0 as with
npx ipfs-only-hash
was to use https://crates.io/crates/ipfs-cid crate which chunks it and hash last chunk https://github.com/omarabid/ipfs-cid/blob/e60fae3ee3222916da0c1dfec5eda1a4c26dec5b/src/lib.rs#L8 This uses an archived / unmaintained crate https://github.com/rs-ipfs/rust-ipfs and adds in bunch of other dependencies which would like to avoid. If we cannot get to a clean impl (that doesn't require pulling in so many deps) we should hold off on this for now.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 we can do for now is, users can easily wrap
vm.readFile(path)
with a futureipfsCidV0(bytes)
if needed, and for now, hashing files directly can be done externally if necessary. can we finalize this approach for now , later open another issue, if need arises?