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: Clean up the majority of the stack trace port implementation #614

Merged
merged 44 commits into from
Sep 2, 2024

Conversation

Xanewok
Copy link
Contributor

@Xanewok Xanewok commented Aug 21, 2024

The current base has already ported the JS code 1:1. This PR aims to reduce the exported N-API surface and simplify some JSisms.

Summary

  • this removes the last uses of napi-rs's ClassInstance/Reference and thus also our ClassInstanceRef helpers
  • the relationships are naively modeled with Rc<RefCell<...>> for now
    • the ClassInstanceRef::borrow(_mut) are just replaced with the actual RefCell methods
    • as before, the mutation points are well isolated and safe but this could use a rewrite in terms of ownership. I'm leaving this for later, as I wanted to focus on strictly decoupling it from the N-API/napi-rs
    • specifically, because we now have cyclic references between the types and there's no GC anymore to break the cycle/rescue us from the leaks
    • EDIT: The strong ref cycle is now broken via 3b551a0. I confirmed with valgrind that the changes before lead to a leak and with the commit we do not leak anymore: https://gist.github.com/Xanewok/90d7aca888f8541081b2e7c1c703d6ca
  • we now prefer using directly OpCode from revm rather than the previously ported/hand-rolled Opcode from JS
  • this moves the now napi-decoupled compiler.rs, model.rs, source_map.rs and library_utils.rs back to the edr_solidity crate
    • this replaces the previous implementation of ContractsIdentifier with what was more or less naively ported but is integrated with the rest of the impl and working. I left the unit tests to show that the old impl behaves correctly (and the test suite more or less gets us that too) but it'd be nice to also revisit this implementation and see if we can bring back the RadixTree if we find that API/impl better (sorry @fvictorio, PTAL as you authored that code IIRC?)
    • breaking: The N-API-exported new VmTraceDecoder() now does not accept any arguments and internally already constructs a ContractsIdentifier.

Caveats/future work

  1. We will need to bump the base to use the Hardhat v2.22.9 soon.

  2. I left the MessageTrace and the SolidityStackTrace types as these are still carried across the FFI boundary and I didn't want to change the architecture/interfacing points for now. EDIT: This is out of the scope for now.

Ownership changes

Introduced in 3b551a0.

EDR TS data flow with init - final(4)

Copy link

changeset-bot bot commented Aug 21, 2024

⚠️ No Changeset found

Latest commit: 5311522

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Xanewok Xanewok had a problem deploying to github-action-benchmark August 21, 2024 20:17 — with GitHub Actions Failure
@Xanewok Xanewok added this to the Solidity tracing port milestone Aug 26, 2024
@Xanewok Xanewok had a problem deploying to github-action-benchmark August 26, 2024 14:27 — with GitHub Actions Error
@Xanewok Xanewok had a problem deploying to github-action-benchmark August 26, 2024 14:42 — with GitHub Actions Error
@Xanewok Xanewok had a problem deploying to github-action-benchmark August 26, 2024 14:50 — with GitHub Actions Error
@Xanewok Xanewok temporarily deployed to github-action-benchmark August 26, 2024 15:05 — with GitHub Actions Inactive
Base automatically changed from refactor/port-solidity-stack-traces to main August 27, 2024 19:09
@Xanewok Xanewok force-pushed the refactor/clean-up-stack-trace-napi branch from bb21830 to c43c940 Compare August 28, 2024 20:48
@Xanewok Xanewok had a problem deploying to github-action-benchmark August 28, 2024 20:48 — with GitHub Actions Failure
@Xanewok
Copy link
Contributor Author

Xanewok commented Aug 28, 2024

I broke the ref cycle and now the memory leaks should be gone, see the OP. The only thing that's left for now is adding more documentation.

@Xanewok Xanewok force-pushed the refactor/clean-up-stack-trace-napi branch from c43c940 to 9ca5997 Compare August 29, 2024 10:48
@Xanewok Xanewok temporarily deployed to github-action-benchmark August 29, 2024 10:48 — with GitHub Actions Inactive
@Xanewok Xanewok temporarily deployed to github-action-benchmark August 29, 2024 11:47 — with GitHub Actions Inactive
@Xanewok Xanewok temporarily deployed to github-action-benchmark August 29, 2024 12:36 — with GitHub Actions Inactive
@Xanewok Xanewok requested review from fvictorio and a team August 29, 2024 12:37
Copy link
Member

@Wodann Wodann left a comment

Choose a reason for hiding this comment

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

Another large change that looks to be steering the code is the right direction. Awesome work!

At a high-level, I think the change to break the circular dependency makes sense. For future work, I can't help but wonder whether we can get away with an arena at the top level and Arcs and Weak for the actual data, instead of requiring a hashmap lookup.

I have some questions to better understand and one concern that I wanted to discuss.

In terms of individual changes, they are too many for me able to look at every line of code in-depth. Are there any parts that require more scrutiny? In that case, would it make sense to have a call to discuss those changes?

crates/edr_napi/src/trace/error_inferrer.rs Show resolved Hide resolved
crates/edr_napi/src/trace/solidity_tracer.rs Show resolved Hide resolved
crates/edr_napi/src/trace/vm_trace_decoder.rs Show resolved Hide resolved
crates/edr_solidity/src/build_model.rs Outdated Show resolved Hide resolved
crates/edr_solidity/src/compiler.rs Show resolved Hide resolved
crates/edr_solidity/src/source_map.rs Show resolved Hide resolved
patches/gen-hardhat-patches.sh Outdated Show resolved Hide resolved
@Xanewok Xanewok had a problem deploying to github-action-benchmark August 30, 2024 09:43 — with GitHub Actions Error
@Xanewok Xanewok temporarily deployed to github-action-benchmark August 30, 2024 10:02 — with GitHub Actions Inactive
@fvictorio
Copy link
Member

this replaces the previous implementation of ContractsIdentifier with what was more or less naively ported but is integrated with the rest of the impl and working [...] it'd be nice to also revisit this implementation and see if we can bring back the RadixTree if we find that API/impl better (sorry @fvictorio, PTAL as you authored that code IIRC?)

@alcuadrado implemented those optimizations in a Hardhat branch that was never merged/released. I used that branch as the starting point in my aborted migration attempt. But I think what you did (keeping the 1-1 port from main) is fine, since we know it works and we have proper benchmarks.

Copy link
Member

@fvictorio fvictorio left a comment

Choose a reason for hiding this comment

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

I tested this with several real repositories and everything worked fine.

@Xanewok Xanewok temporarily deployed to github-action-benchmark September 2, 2024 09:51 — with GitHub Actions Inactive
@Xanewok
Copy link
Contributor Author

Xanewok commented Sep 2, 2024

I have reverted the patch like in #545, so this only contains the updated logic but does not exercise it locally when running tests, however the CI was green with the patch applied, so this should be good to go and @fvictorio tested this as well using some real-life repositories.

The next step will be to update Hardhat (9ca5997 is a good starting point) to use this new logic once we release EDR with these changes.

Thanks everyone for the reviews! 🎉

@Xanewok Xanewok added this pull request to the merge queue Sep 2, 2024
Merged via the queue into main with commit f3a1766 Sep 2, 2024
37 checks passed
@Xanewok Xanewok deleted the refactor/clean-up-stack-trace-napi branch September 2, 2024 15:34
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
no changeset needed This PR doesn't require a changeset
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants