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

Structured concurrency: Part 1 #1214

Merged
merged 6 commits into from
Sep 30, 2024
Merged

Conversation

rakanalh
Copy link
Contributor

@rakanalh rakanalh commented Sep 22, 2024

Description

From task_scope's documentation

What is Strucutured Concurrency?

Structured Concurrency is a programming paradigm that lets asynchronous operations run only within certain scopes so that they form an operation stack like a regular function call stack. As the parent operation waits until all children complete, Structured Concurrency helps local reasoning of concurrent programs.
Out-of-task concurrency considered harmful

Most of the asynchronous programs are composed of async functions and in-task concurrency primitives such as select and join, and this makes such futures automatically well-structured. As "futures do nothing unless polled," executions of inner (child) operations are very explicit (usually at the point of awaits). Moreover, canceling a future is done by dropping it, which will reclaim resources used for the operation, including the inner futures. This drop-chain propagates the cancellation to the very end of the operation stack.

Out-of-task concurrencies such as spawn, however, breaks the structure. They allow us to start a new execution unit that can escape from the parent stack. Although frameworks provide a way to join on the spawned task, they don't propagate cancellation properly. If you drop the spawning task, the spawned task may outlive the parent indefinitely.

Changes in this PR

This PR restructures the ethereum-rpc crate SubscriptionManager so that the list of subscriptions is preserved for as long as they are active, while processing new head and new logs notifications in a single worker which notifies all subscribers about new heads and new logs. That is, replacing the functionality of spawning an unmanaged task per subscription.

The PR also keeps track of the handles for the 3 spawned tasks:

  1. new_heads_notifier
  2. new_logs_notifier
  3. soft_confirmation_event_handler

So that when SubscriptionManager is dropped, the tasks are also stopped. This could be improved in the future where these components can react the OS signals / cancellation tokens.

Initiative

Based on this initiative, i'd like to introduce a no tokio::spawn rule without storing the handle somewhere, where the spawned task is managed and is cancellable.

Linked Issues

Copy link

codecov bot commented Sep 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.6%. Comparing base (94664da) to head (c89e4f4).
Report is 1 commits behind head on nightly.

Additional details and impacted files
Files with missing lines Coverage Δ
crates/ethereum-rpc/src/lib.rs 88.9% <100.0%> (-0.1%) ⬇️
crates/ethereum-rpc/src/subscription.rs 100.0% <100.0%> (+4.0%) ⬆️
crates/ethereum-rpc/src/trace.rs 78.9% <100.0%> (+0.3%) ⬆️

... and 1 file with indirect coverage changes

@rakanalh rakanalh force-pushed the rakanalh/structured-concurrency-pt1 branch from 8629073 to dc66a06 Compare September 22, 2024 16:18
@rakanalh rakanalh force-pushed the rakanalh/structured-concurrency-pt1 branch from dc66a06 to c15282c Compare September 23, 2024 10:20
Copy link
Contributor

@kpp kpp left a comment

Choose a reason for hiding this comment

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

That's a nice cleanup. The only thing I question is why you decided to move from broadcast to mpsc.

@rakanalh
Copy link
Contributor Author

That's a nice cleanup. The only thing I question is why you decided to move from broadcast to mpsc.

broadcast is meant for multiple receivers. In this case, the node (for example sequencer) would send a new soft confirmation event to one receiver which is the new_heads_worker and the worker would notify all subscriptions.

Copy link
Contributor

@kpp kpp left a comment

Choose a reason for hiding this comment

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

We may use for ... { x.send(y) } in log subscription but new_heads looks better with broadcast.

crates/ethereum-rpc/src/subscription.rs Show resolved Hide resolved
@eyusufatik eyusufatik added the HOLD-MERGE PR is not draft but should not be merged yet label Sep 24, 2024
@yaziciahmet
Copy link
Contributor

LGTM other than a small nit I left above.

@yaziciahmet
Copy link
Contributor

My only concern is that there is a high possibility that the soft_confirmation_tx will need multiple receivers eventually. Only receiver is SubscriptionManager right now, but I see it more fit to be broadcast as that event channel has no specific context and should notify anyone who wants to subscribe.

This comment might be irrelevant if we were to add another EventManager layer for chain-specific events, but it doesn't seem necessary in the near future as almost all the new incoming information is dependent on a new soft confirmation, and the specifics of the requested notification can be derived from that new soft confirmation.

@rakanalh
Copy link
Contributor Author

My only concern is that there is a high possibility that the soft_confirmation_tx will need multiple receivers eventually. Only receiver is SubscriptionManager right now, but I see it more fit to be broadcast as that event channel has no specific context and should notify anyone who wants to subscribe.

We shouldn't make it use broadcast until we have a second receiver. Why use broadcast for something that has 1 receiver, if that's the case then mpsc would be the way to go for now. If we keep assuming future use cases then we build up tech-debt.

@rakanalh
Copy link
Contributor Author

I restored using broadcast since pruning is one use case of another receiver.

@rakanalh rakanalh mentioned this pull request Sep 25, 2024
@rakanalh rakanalh merged commit 97b6e5d into nightly Sep 30, 2024
14 checks passed
@rakanalh rakanalh deleted the rakanalh/structured-concurrency-pt1 branch September 30, 2024 11:31
@eyusufatik eyusufatik mentioned this pull request Oct 7, 2024
82 tasks
eyusufatik added a commit that referenced this pull request Oct 9, 2024
* Structured concurrency: Part 1 (#1214)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix typo (#1246)

* Structured Concurrency: Part 2 (#1216)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix spawns in DaService

* Add TaskManager

* Use task manager in sequencer

* Document TaskManager

* Handle shutdown event

* Use TaskTracker

* Add comment about using a cancellation token

* Use JoinHandles instead of TaskTracker

* Use TaskManager in fullnode and prover

* Improve bitcoin-da service

* Force spawned tasks to accept a cancellation token

* Use biased polling

* Satisfy clippy

* Address PR feedback

* Fix checks

* Pin foundry (#1253)

* Pin foundary

* Add comment

* Remove puclihs mock da block script

* Use default block time on error (#1244)

* Use default block time on error

* Constant target_block_time

* Remove import

* Dump logs and cleanup on assert failures (#1252)

* Fix estiamte gas l1 fee issue when metamask max amount is selected (#1261)

* Fix estiamte gas l1 fee issue when metamask max amount is selected

* Fix tests

* Remove unnecessary comment

* Fix hive (#1263)

* Fix configs

* Remove publish mock block from docker

* Update port for hive

* Rename config in hive

* Refactor eth estimate gas l1 fee issue (#1264)

* Refactor eth estimate gas l1 fee issue

* Nits

* Fix bug

* Refactor

* Do not ignore resources

* Fix

---------

Co-authored-by: Roman Proskuryakoff <[email protected]>

* Pruning skeleton (#1229)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix spawns in DaService

* Add TaskManager

* Use task manager in sequencer

* Document TaskManager

* Handle shutdown event

* Use TaskTracker

* Add comment about using a cancellation token

* Use JoinHandles instead of TaskTracker

* Use TaskManager in fullnode and prover

* Improve bitcoin-da service

* Force spawned tasks to accept a cancellation token

* Use biased polling

* Satisfy clippy

* WIP

* Add pruning tables

* Pruning skeleton implementation

* Use pruner in nodes

* Use biased polling based on order

* WIP

* Fix how config is done

* Derive default

* Add logs

* Let the tasks finish without panicing

* Use pruning config in fullnode and prover

* Add simple run test

* Use option instead of PruningMode

* Unneccessary changes

* l2_receiver

* Cleanup prints

* Use last pruned block in calculation

* Implement pruning criteria

* Lint and add comment

* Set the last_pruned_block to up_to_block value

* Don't store config internally

* Remove from constructor

* Should not change

* Move config value

* Remove pruning from sequencer / prover

* Derive SequencerClient (#1269)

* Derive SequencerClient

* Renames

* Lint

* Improve estimate gas and create access list rpcs (#1265)

* Remove unnecessary transact_to conversion

* unwrap_or_default

* More concise block_env initialization

* Actually get highest gas limit from request and block env

* One liner set

* Allow unused

* Cleanup prepare_call_env

* Fix tests & lint

* Replace unwrap

* Set state to block in eth_call

* Consume request in prepare_call_env

* Lint

* Set state before reading config

* Remove unnecessary clone

* Replace allow unused with feature native gate

* Add sys txs to evm tests (#1255)

* modify config_push_contracts

* modify call_multiple_test

* modify tests in call_tests.rs

* moving common functions to utils.rs

* minor fixes

* updated lock files

* modify tests

---------

Co-authored-by: Esad Yusuf Atik <[email protected]>

* make prover config arg not optional (#1278)

* Pin to 27.1 (#1279)

* E2E tests using citrea-e2e (#1277)

* E2E tests using citrea-e2e

* Lint

* Set CITREA path

* With github.workspace

* Target citrea-e2e main

* Update citrea-e2e rev

* Use debug build

* Update citrea-e2e

* Prover generate input rpc (#1280)

* WIP Implement prover generate rpcg

* It compiles but stf is modified

* Context

* Fix Context: Send

* Merge fix

* Move function from common to prover

* Remove code duplicate and unnecessary log

* Add optional parameter to break commitments into groups

* Return input as string

---------

Co-authored-by: Roman Proskuryakoff <[email protected]>

* Move node configs to citrea-common (#1286)

* move node configs to a seperate crate

* fix udeps

* remove native feature

* move SequencerConfig

* move config to citrea-common

* fix use statements

* Update e2e test framework and fix tests (#1305)

* Update e2e test framework and fix tests

* Fix bug

* Update ci binary env key

* Test if new fix works

* Update bitcoincore-rpc version

* Dprint

* Try against fix prover config rev

* Target main HEAD rev

---------

Co-authored-by: jfldde <[email protected]>

* build and publish a new image for every commit to the nightly branch (#1309)

* Get fee recommendation from mempool space (#1302)

* Get fee recommendation from mempool space

* Fix bug

* Construct mempool space endpoint by network

---------

Co-authored-by: Esad Yusuf Atik <[email protected]>

* new path fix for nightly (#1310)

* Enable pending block tag in simulation endpoints (#1303)

* treat pending tag same as latest

* fix lint

* return new sealed block for pending

* handle pending tag externally

* revert enabling pending in some endpoints

* get blockenv instead of sealed block

* address review comments

* implement tests for eth_call, eth_estimateGas, eth_createAccessList pending blocks

* rename tests

---------

Co-authored-by: eyusufatik <[email protected]>

* Implement state and block overrides (#1270)

* Implement state and block overrides

* Update comment

* apply_block_overrides func

* apply_state_overrides func

* Use a single func for replacing account storage

* Clippy

* Address feedback

Use Reth's BlockOverrides with saturating_to to convert back to u64

* Test for block overrides

* Test for state overrides

* Comment tests

* Create a fresh working set

* Remove box

* remove alloy-serde

---------

Co-authored-by: eyusufatik <[email protected]>

* Use spawn_blocking for da queue (#1311)

* Fetch smart fee only if none (#1312)

* update run doc and changelog (#1315)

---------

Co-authored-by: Rakan Al-Huneiti <[email protected]>
Co-authored-by: Ahmet Yazıcı <[email protected]>
Co-authored-by: Erce Can Bektüre <[email protected]>
Co-authored-by: jfldde <[email protected]>
Co-authored-by: Erce Can Bektüre <[email protected]>
Co-authored-by: Roman Proskuryakoff <[email protected]>
Co-authored-by: Ege Okan Ünaldı <[email protected]>
Co-authored-by: Çetin <[email protected]>
eyusufatik added a commit that referenced this pull request Oct 9, 2024
* Structured concurrency: Part 1 (#1214)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix typo (#1246)

* Structured Concurrency: Part 2 (#1216)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix spawns in DaService

* Add TaskManager

* Use task manager in sequencer

* Document TaskManager

* Handle shutdown event

* Use TaskTracker

* Add comment about using a cancellation token

* Use JoinHandles instead of TaskTracker

* Use TaskManager in fullnode and prover

* Improve bitcoin-da service

* Force spawned tasks to accept a cancellation token

* Use biased polling

* Satisfy clippy

* Address PR feedback

* Fix checks

* Pin foundry (#1253)

* Pin foundary

* Add comment

* Remove puclihs mock da block script

* Use default block time on error (#1244)

* Use default block time on error

* Constant target_block_time

* Remove import

* Dump logs and cleanup on assert failures (#1252)

* Fix estiamte gas l1 fee issue when metamask max amount is selected (#1261)

* Fix estiamte gas l1 fee issue when metamask max amount is selected

* Fix tests

* Remove unnecessary comment

* Fix hive (#1263)

* Fix configs

* Remove publish mock block from docker

* Update port for hive

* Rename config in hive

* Refactor eth estimate gas l1 fee issue (#1264)

* Refactor eth estimate gas l1 fee issue

* Nits

* Fix bug

* Refactor

* Do not ignore resources

* Fix

---------

Co-authored-by: Roman Proskuryakoff <[email protected]>

* Pruning skeleton (#1229)

* Implement managed tasks for SubscriptionManager

* Fix clippy

* Add comment about trace task

* Use broadcast instead of mpsc for l2 blocks events

* Fix spawns in DaService

* Add TaskManager

* Use task manager in sequencer

* Document TaskManager

* Handle shutdown event

* Use TaskTracker

* Add comment about using a cancellation token

* Use JoinHandles instead of TaskTracker

* Use TaskManager in fullnode and prover

* Improve bitcoin-da service

* Force spawned tasks to accept a cancellation token

* Use biased polling

* Satisfy clippy

* WIP

* Add pruning tables

* Pruning skeleton implementation

* Use pruner in nodes

* Use biased polling based on order

* WIP

* Fix how config is done

* Derive default

* Add logs

* Let the tasks finish without panicing

* Use pruning config in fullnode and prover

* Add simple run test

* Use option instead of PruningMode

* Unneccessary changes

* l2_receiver

* Cleanup prints

* Use last pruned block in calculation

* Implement pruning criteria

* Lint and add comment

* Set the last_pruned_block to up_to_block value

* Don't store config internally

* Remove from constructor

* Should not change

* Move config value

* Remove pruning from sequencer / prover

* Derive SequencerClient (#1269)

* Derive SequencerClient

* Renames

* Lint

* Improve estimate gas and create access list rpcs (#1265)

* Remove unnecessary transact_to conversion

* unwrap_or_default

* More concise block_env initialization

* Actually get highest gas limit from request and block env

* One liner set

* Allow unused

* Cleanup prepare_call_env

* Fix tests & lint

* Replace unwrap

* Set state to block in eth_call

* Consume request in prepare_call_env

* Lint

* Set state before reading config

* Remove unnecessary clone

* Replace allow unused with feature native gate

* Add sys txs to evm tests (#1255)

* modify config_push_contracts

* modify call_multiple_test

* modify tests in call_tests.rs

* moving common functions to utils.rs

* minor fixes

* updated lock files

* modify tests

---------

Co-authored-by: Esad Yusuf Atik <[email protected]>

* make prover config arg not optional (#1278)

* Pin to 27.1 (#1279)

* E2E tests using citrea-e2e (#1277)

* E2E tests using citrea-e2e

* Lint

* Set CITREA path

* With github.workspace

* Target citrea-e2e main

* Update citrea-e2e rev

* Use debug build

* Update citrea-e2e

* Prover generate input rpc (#1280)

* WIP Implement prover generate rpcg

* It compiles but stf is modified

* Context

* Fix Context: Send

* Merge fix

* Move function from common to prover

* Remove code duplicate and unnecessary log

* Add optional parameter to break commitments into groups

* Return input as string

---------

Co-authored-by: Roman Proskuryakoff <[email protected]>

* Move node configs to citrea-common (#1286)

* move node configs to a seperate crate

* fix udeps

* remove native feature

* move SequencerConfig

* move config to citrea-common

* fix use statements

* Update e2e test framework and fix tests (#1305)

* Update e2e test framework and fix tests

* Fix bug

* Update ci binary env key

* Test if new fix works

* Update bitcoincore-rpc version

* Dprint

* Try against fix prover config rev

* Target main HEAD rev

---------

Co-authored-by: jfldde <[email protected]>

* build and publish a new image for every commit to the nightly branch (#1309)

* Get fee recommendation from mempool space (#1302)

* Get fee recommendation from mempool space

* Fix bug

* Construct mempool space endpoint by network

---------

Co-authored-by: Esad Yusuf Atik <[email protected]>

* new path fix for nightly (#1310)

* Enable pending block tag in simulation endpoints (#1303)

* treat pending tag same as latest

* fix lint

* return new sealed block for pending

* handle pending tag externally

* revert enabling pending in some endpoints

* get blockenv instead of sealed block

* address review comments

* implement tests for eth_call, eth_estimateGas, eth_createAccessList pending blocks

* rename tests

---------

Co-authored-by: eyusufatik <[email protected]>

* Implement state and block overrides (#1270)

* Implement state and block overrides

* Update comment

* apply_block_overrides func

* apply_state_overrides func

* Use a single func for replacing account storage

* Clippy

* Address feedback

Use Reth's BlockOverrides with saturating_to to convert back to u64

* Test for block overrides

* Test for state overrides

* Comment tests

* Create a fresh working set

* Remove box

* remove alloy-serde

---------

Co-authored-by: eyusufatik <[email protected]>

* Use spawn_blocking for da queue (#1311)

* Fetch smart fee only if none (#1312)

* update run doc and changelog (#1315)

---------

Co-authored-by: Rakan Al-Huneiti <[email protected]>
Co-authored-by: Ahmet Yazıcı <[email protected]>
Co-authored-by: Erce Can Bektüre <[email protected]>
Co-authored-by: jfldde <[email protected]>
Co-authored-by: Erce Can Bektüre <[email protected]>
Co-authored-by: Roman Proskuryakoff <[email protected]>
Co-authored-by: Ege Okan Ünaldı <[email protected]>
Co-authored-by: Çetin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HOLD-MERGE PR is not draft but should not be merged yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants