Skip to content

feat(nano): Add reentrancy protection by default#1335

Merged
msbrogli merged 1 commit intomasterfrom
feat/nano-reentrancy-protection
Aug 18, 2025
Merged

feat(nano): Add reentrancy protection by default#1335
msbrogli merged 1 commit intomasterfrom
feat/nano-reentrancy-protection

Conversation

@msbrogli
Copy link
Member

@msbrogli msbrogli commented Jul 23, 2025

Motivation

Add a reentrancy protection enabled by default at the method level.

Acceptance Criteria

  1. Add new argument allow_reentrancy: bool to @public(...) decorator.
  2. Add _validate_reentrancy() to runner.
  3. Proxy calls are considered internal calls, so no reentrancy must be checked.

Checklist

  • If you are requesting a merge into master, confirm this code is production-ready and can be included in future releases as soon as it gets merged

@msbrogli msbrogli requested a review from jansegre as a code owner July 23, 2025 18:23
@msbrogli msbrogli self-assigned this Jul 23, 2025
@msbrogli msbrogli moved this from Todo to In Progress (WIP) in Hathor Network Jul 23, 2025
@github-actions
Copy link

github-actions bot commented Jul 23, 2025

🐰 Bencher Report

Branchfeat/nano-reentrancy-protection
Testbedubuntu-22.04
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
minutes (m)
(Result Δ%)
Lower Boundary
minutes (m)
(Limit %)
Upper Boundary
minutes (m)
(Limit %)
sync-v2 (up to 20000 blocks)📈 view plot
🚷 view threshold
1.82 m
(+9.75%)Baseline: 1.66 m
1.49 m
(82.00%)
1.82 m
(99.77%)
🐰 View full continuous benchmarking report in Bencher

@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from 050ea49 to c8dd71c Compare July 23, 2025 18:38
@msbrogli msbrogli moved this from In Progress (WIP) to In Progress (Done) in Hathor Network Jul 23, 2025
jansegre
jansegre previously approved these changes Jul 23, 2025
@github-project-automation github-project-automation bot moved this from In Progress (Done) to In Review (WIP) in Hathor Network Jul 23, 2025
@glevco glevco moved this from In Review (WIP) to In Review (Done) in Hathor Network Jul 23, 2025
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch 2 times, most recently from 02564c6 to 4e6f979 Compare July 24, 2025 02:21
if allow_reentrancy:
return

for call_record in self._call_info.stack:
Copy link
Member Author

@msbrogli msbrogli Jul 24, 2025

Choose a reason for hiding this comment

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

The current code runs in O(n) time, where n is the maximum allowed depth (currently set to 100). We could optimize it to run in O(1) time by maintaining counters in a dict[tuple[ContractId, method_name], int]. Is this optimization worth implementing? Should we reduce the maximum allowed depth?

Copy link
Contributor

Choose a reason for hiding this comment

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

100 equality comparisons is too cheap, I don't think it's a problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

It can get up to 1+2+3+...+100 = 100*101/2 = 5050 comparisons, actually. But I guess it's still not much and shouldn't be a problem. We can always optimize it later. Maybe just leave a note about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jansegre What do you think? This check runs in O(n) where n is the stack size. So, the overall algorithm runs in O(n^2) where n is the maximum stack size it reaches during execution. is it worth the effort to reduce the time complexity to O(n)? Any DoS attack vectors here?

Copy link
Member

Choose a reason for hiding this comment

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

I feel like this can be optimized, but I don't think it should be a blocker. Do we have any idea about the order of magnitude of time it takes when n=100? Being proportional to ~10.000 times a single verification should be manageable I think, but certainly not ideal.

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, I don't think this is a blocker to merge this PR.

I think it's OK to add a task to optimize it later. I just think it's possible to avoid looking at the whole stack every time, maybe memoizing the last call on the same stack somehow so each check doesn't need to go up the stack again, only up to the last call.

@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from 4e6f979 to 031fc41 Compare July 24, 2025 02:30
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch 3 times, most recently from 10536dc to 89ac14b Compare July 30, 2025 18:13
@msbrogli msbrogli moved this from In Review (Done) to In Progress (Done) in Hathor Network Jul 30, 2025
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from 89ac14b to 7c572c3 Compare July 30, 2025 18:14
@codecov
Copy link

codecov bot commented Jul 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.61%. Comparing base (1927f37) to head (7c572c3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1335      +/-   ##
==========================================
- Coverage   85.66%   85.61%   -0.06%     
==========================================
  Files         424      424              
  Lines       32120    32131      +11     
  Branches     4997     5001       +4     
==========================================
- Hits        27516    27508       -8     
- Misses       3604     3615      +11     
- Partials     1000     1008       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

glevco
glevco previously approved these changes Jul 30, 2025
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test to cover the behavior change from my thread (the reenter example), that is, cover that reentrancy is prevented even if the called method is not the same.

@msbrogli msbrogli moved this from In Progress (Done) to In Review (WIP) in Hathor Network Jul 30, 2025
jansegre
jansegre previously approved these changes Jul 31, 2025
@msbrogli msbrogli dismissed stale reviews from jansegre and glevco via 387a74f August 18, 2025 15:36
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from 7c572c3 to 387a74f Compare August 18, 2025 15:36
@msbrogli msbrogli moved this from In Review (WIP) to In Review (Done) in Hathor Network Aug 18, 2025
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from 387a74f to dc6b1d7 Compare August 18, 2025 15:56
@msbrogli msbrogli force-pushed the feat/nano-reentrancy-protection branch from dc6b1d7 to 93ded98 Compare August 18, 2025 16:00
@msbrogli msbrogli merged commit 93ded98 into master Aug 18, 2025
5 of 7 checks passed
@msbrogli msbrogli deleted the feat/nano-reentrancy-protection branch August 18, 2025 16:26
@github-project-automation github-project-automation bot moved this from In Review (Done) to Waiting to be deployed in Hathor Network Aug 18, 2025
This was referenced Aug 20, 2025
@jansegre jansegre moved this from Waiting to be deployed to Done in Hathor Network Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants