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

Replace Tree generic with Arc<dyn TreeViewer> #7615

Closed
mattsse opened this issue Apr 13, 2024 · 12 comments · Fixed by #7810
Closed

Replace Tree generic with Arc<dyn TreeViewer> #7615

mattsse opened this issue Apr 13, 2024 · 12 comments · Fixed by #7810
Assignees
Labels
C-enhancement New feature or request D-good-first-issue Nice and easy! A great choice to get started

Comments

@mattsse
Copy link
Collaborator

mattsse commented Apr 13, 2024

Describe the feature

This generic doesn't have any performance benefits because it's just just for simple get calls, which will also be refactored shortly.

It would make the setup a lot more convenient if we'd use Arc< dyn TreeViewer> here instead. Treeviewer trait is object safe so this should be very simple.

/// The blockchain tree instance.
tree: Tree,

This would let us cut down on a bunch of bloated types in the node-builder

type RethFullProviderType<DB, Evm> =
BlockchainProvider<DB, ShareableBlockchainTree<DB, EvmProcessorFactory<Evm>>>;

and allows us to be less constraint during the node-builder setup

Additional context

No response

@mattsse mattsse added C-enhancement New feature or request S-needs-triage This issue needs to be labelled D-good-first-issue Nice and easy! A great choice to get started and removed S-needs-triage This issue needs to be labelled labels Apr 13, 2024
@KyrylR
Copy link
Contributor

KyrylR commented Apr 13, 2024

May I?

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 13, 2024

assigned

@KyrylR
Copy link
Contributor

KyrylR commented Apr 17, 2024

Hello! I just started working on this issue and noticed the following structures:

impl<DB, Tree> BlockchainTreeEngine for BlockchainProvider<DB, Tree>
where
    DB: Send + Sync,
    Tree: BlockchainTreeEngine,
{
    // ...
}

impl<DB, Tree> BlockchainTreePendingStateProvider for BlockchainProvider<DB, Tree>
where
    DB: Send + Sync,
    Tree: BlockchainTreePendingStateProvider,
{
    fn find_pending_state_provider(
        &self,
        block_hash: BlockHash,
    ) -> Option<Box<dyn BundleStateDataProvider>> {
        self.tree.find_pending_state_provider(block_hash)
    }
}

impl<DB, Tree> CanonStateSubscriptions for BlockchainProvider<DB, Tree>
where
    DB: Send + Sync,
    Tree: CanonStateSubscriptions,
{
    fn subscribe_to_canonical_state(&self) -> CanonStateNotifications {
        self.tree.subscribe_to_canonical_state()
    }
}

If we intend to change to Arc<dyn TreeViewer> (as far as I understand), like this:

#[derive(Clone, Debug)]
pub struct BlockchainProvider<DB> {
    /// Provider type used to access the database.
    database: ProviderFactory<DB>,
    /// The blockchain tree instance.
    tree: Arc<dyn BlockchainTreeViewer>,
    /// Tracks the chain info wrt forkchoice updates
    chain_info: ChainInfoTracker,
}

Correct me if I'm wrong.

From what I can see:

The BlockchainTreeEngine trait implementation performs write operations on the blockchain tree; therefore, by its nature, it is incompatible with BlockchainTreeViewer.

The BlockchainTreePendingStateProvider and CanonStateSubscriptions trait implementations provide read-only operations related to accessing the pending state and subscribing to canonical state updates, respectively. Therefore, it requires a new trait to be created or modification of an existing one. (I believe the latter option could introduce some conflicts, so it's less feasible.)

Maybe I need to create a new trait, TreeViewer?

UPD: And yeah, a tree viewer should definitely not contain BlockchainTreeEngine, as it's a viewer. 😅

cc @mattsse

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 17, 2024

this is a good point, I think what we want is a trait that unifies all the functions we need for the BlockchainProvider so that we have a single trait we can use as dyn

@KyrylR
Copy link
Contributor

KyrylR commented Apr 17, 2024

Got it, so I'll proceed with the creation of a unified trait

But what do we do with BlockchainTreeEngine? Just remove it?

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 17, 2024

sg!
I'd like to rename this to something like PendingStateInfo, because we're gonna change the tree interface very soon

BlockchainTreeEngine

if unused can be removed

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 22, 2024

@KyrylR just checking in
do you have bandwidth for this? :) lmk if you need more pointers

this will become a blocker shortly

@KyrylR
Copy link
Contributor

KyrylR commented Apr 23, 2024

Hello, sorry for the delay
I am going to open a PR today, really wanna give it a try

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 23, 2024

cool!

@KyrylR
Copy link
Contributor

KyrylR commented Apr 23, 2024

I have a few more questions

Right now I am heading in the direction with such a trait:

pub trait TreeViewer:
    BlockchainTreeViewer
    + BlockchainTreePendingStateProvider
    + CanonStateSubscriptions
    + BlockchainTreeEngine
{
}

So, we have such a structure in the end:

#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct BlockchainProvider<DB> {
    /// Provider type used to access the database.
    database: ProviderFactory<DB>,
    /// The blockchain tree instance.
    tree: Arc<dyn TreeViewer>,
    /// Tracks the chain info wrt forkchoice updates
    chain_info: ChainInfoTracker,
}

Is this the intended way? For me, it feels not right

I am not a full-time Rust dev, so I could still miss some obvious details

By the keyword dyn, I assume it should be a trait, but I'm still struggling with its definition

I cannot define anything alongside BlockchainTreeViewer, as from interfaces/src/blockchain_tree, I cannot (and I believe should not) access the BlockchainTreePendingStateProvider and CanonStateSubscriptions

I'd like to rename this to something like PendingStateInfo

Are you referring to the TreeViewer that I intend to create?

Off-topic: Is it okay to tag you when I have a question? Or is a simple message enough?

cc @mattsse

@mattsse
Copy link
Collaborator Author

mattsse commented Apr 23, 2024

Off-topic: Is it okay to tag you when I have a question? Or is a simple message enough?

anytime, can also DM on tg @mattsse

this looks okay, we need to combine all traits we use in the the impls, with a helper trait like you did,
then we can add a blanket impl for all T : BlockchainTreeViewer + BlockchainTreePendingStateProvider + CanonStateSubscriptions + BlockchainTreeEngine

this is only a temporary solution but is what we want

@KyrylR
Copy link
Contributor

KyrylR commented Apr 23, 2024

Got it, the last point where I encounter a problem is with ShareableBlockchainTree:

pub struct ShareableBlockchainTree<DB, EF> {
    /// BlockchainTree
    pub tree: Arc<RwLock<BlockchainTree<DB, EF>>>,
}

I am trying to reuse it like this:

let blockchain_tree = Arc::new(ShareableBlockchainTree::new(tree));

// Set up the blockchain provider
let blockchain_db =
    BlockchainProvider::new(provider_factory.clone(), blockchain_tree.clone())?;

But the problem that I see is the structure of this code is Arc -> Arc -> BlockchainTree

Is this ok?

cc @mattsse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement New feature or request D-good-first-issue Nice and easy! A great choice to get started
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants