Skip to content

refactor(ethexe): split db interfaces to read and write #4705

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
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethexe/blob-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use alloy::{
rpc::types::beacon::sidecar::BeaconBlobBundle,
};
use anyhow::{anyhow, Result};
use ethexe_common::db::{CodesStorage, OnChainStorage};
use ethexe_common::db::{CodesStorageRead, OnChainStorageRead};
use ethexe_db::Database;
use futures::{
future::BoxFuture,
Expand Down
2 changes: 1 addition & 1 deletion ethexe/blob-loader/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use crate::{BlobData, BlobLoaderEvent, BlobLoaderService};
use anyhow::{anyhow, Result};
use ethexe_common::db::OnChainStorage;
use ethexe_common::db::OnChainStorageRead;
use ethexe_db::Database;
use futures::{future::BoxFuture, stream::FusedStream, FutureExt, Stream};
use gprimitives::CodeId;
Expand Down
79 changes: 30 additions & 49 deletions ethexe/common/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,87 +21,68 @@
// TODO #4547: move types to another module(s)

use crate::{
events::BlockEvent, gear::StateTransition, BlockHeader, CodeBlobInfo, Schedule,
StateHashWithQueueSize,
};
use alloc::{
collections::{BTreeMap, BTreeSet, VecDeque},
vec::Vec,
events::BlockEvent, gear::StateTransition, BlockHeader, CodeBlobInfo, ProgramStates, Schedule,
};
use alloc::{collections::VecDeque, vec::Vec};
use gear_core::{
code::InstrumentedCode,
ids::{ActorId, CodeId},
};
use gprimitives::H256;

pub trait BlockMetaStorage: Send + Sync {
pub trait BlockMetaStorageRead {
fn block_prepared(&self, block_hash: H256) -> bool;
fn set_block_prepared(&self, block_hash: H256);

fn block_computed(&self, block_hash: H256) -> bool;
fn set_block_computed(&self, block_hash: H256);

fn block_commitment_queue(&self, block_hash: H256) -> Option<VecDeque<H256>>;
fn set_block_commitment_queue(&self, block_hash: H256, queue: VecDeque<H256>);

fn block_codes_queue(&self, block_hash: H256) -> Option<VecDeque<CodeId>>;
fn set_block_codes_queue(&self, block_hash: H256, queue: VecDeque<CodeId>);

fn previous_not_empty_block(&self, block_hash: H256) -> Option<H256>;
fn set_previous_not_empty_block(&self, block_hash: H256, prev_commitment: H256);

fn block_program_states(
&self,
block_hash: H256,
) -> Option<BTreeMap<ActorId, StateHashWithQueueSize>>;
fn set_block_program_states(
&self,
block_hash: H256,
map: BTreeMap<ActorId, StateHashWithQueueSize>,
);

fn block_program_states(&self, block_hash: H256) -> Option<ProgramStates>;
fn block_outcome(&self, block_hash: H256) -> Option<Vec<StateTransition>>;
fn set_block_outcome(&self, block_hash: H256, outcome: Vec<StateTransition>);
fn block_outcome_is_empty(&self, block_hash: H256) -> Option<bool>;

fn block_schedule(&self, block_hash: H256) -> Option<Schedule>;
fn set_block_schedule(&self, block_hash: H256, map: Schedule);

fn latest_computed_block(&self) -> Option<(H256, BlockHeader)>;
}

pub trait BlockMetaStorageWrite {
fn set_block_prepared(&self, block_hash: H256);
fn set_block_computed(&self, block_hash: H256);
fn set_block_commitment_queue(&self, block_hash: H256, queue: VecDeque<H256>);
fn set_block_codes_queue(&self, block_hash: H256, queue: VecDeque<CodeId>);
fn set_previous_not_empty_block(&self, block_hash: H256, prev_commitment: H256);
fn set_block_program_states(&self, block_hash: H256, map: ProgramStates);
fn set_block_outcome(&self, block_hash: H256, outcome: Vec<StateTransition>);
fn set_block_schedule(&self, block_hash: H256, map: Schedule);
fn set_latest_computed_block(&self, block_hash: H256, header: BlockHeader);
}

pub trait CodesStorage: Send + Sync {
pub trait CodesStorageRead {
fn original_code_exists(&self, code_id: CodeId) -> bool;

fn original_code(&self, code_id: CodeId) -> Option<Vec<u8>>;
fn set_original_code(&self, code: &[u8]) -> CodeId;

fn program_code_id(&self, program_id: ActorId) -> Option<CodeId>;
fn set_program_code_id(&self, program_id: ActorId, code_id: CodeId);
fn program_ids(&self) -> BTreeSet<ActorId>;

fn instrumented_code_exists(&self, runtime_id: u32, code_id: CodeId) -> bool;
fn instrumented_code(&self, runtime_id: u32, code_id: CodeId) -> Option<InstrumentedCode>;
fn set_instrumented_code(&self, runtime_id: u32, code_id: CodeId, code: InstrumentedCode);

fn code_valid(&self, code_id: CodeId) -> Option<bool>;
}

pub trait CodesStorageWrite {
fn set_original_code(&self, code: &[u8]) -> CodeId;
fn set_program_code_id(&self, program_id: ActorId, code_id: CodeId);
fn set_instrumented_code(&self, runtime_id: u32, code_id: CodeId, code: InstrumentedCode);
fn set_code_valid(&self, code_id: CodeId, valid: bool);
}

pub trait OnChainStorage: Send + Sync {
pub trait OnChainStorageRead {
fn block_header(&self, block_hash: H256) -> Option<BlockHeader>;
fn set_block_header(&self, block_hash: H256, header: BlockHeader);

fn block_events(&self, block_hash: H256) -> Option<Vec<BlockEvent>>;
fn set_block_events(&self, block_hash: H256, events: &[BlockEvent]);

fn code_blob_info(&self, code_id: CodeId) -> Option<CodeBlobInfo>;
fn set_code_blob_info(&self, code_id: CodeId, code_info: CodeBlobInfo);

fn block_is_synced(&self, block_hash: H256) -> bool;
fn set_block_is_synced(&self, block_hash: H256);

fn latest_synced_block_height(&self) -> Option<u32>;
}

pub trait OnChainStorageWrite {
fn set_block_header(&self, block_hash: H256, header: BlockHeader);
fn set_block_events(&self, block_hash: H256, events: &[BlockEvent]);
fn set_code_blob_info(&self, code_id: CodeId, code_info: CodeBlobInfo);
fn set_block_is_synced(&self, block_hash: H256);
fn set_latest_synced_block_height(&self, height: u32);
}
2 changes: 2 additions & 0 deletions ethexe/common/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use gprimitives::{ActorId, MessageId, H256};
use parity_scale_codec::{Decode, Encode};
use sha3::Digest as _;

pub type ProgramStates = BTreeMap<ActorId, StateHashWithQueueSize>;

#[derive(Debug, Clone, Default, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockHeader {
Expand Down
11 changes: 8 additions & 3 deletions ethexe/compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use anyhow::{anyhow, Result};
use ethexe_common::{
db::{BlockMetaStorage, CodesStorage, OnChainStorage},
db::{BlockMetaStorageRead, BlockMetaStorageWrite, CodesStorageRead, OnChainStorageRead},
events::{BlockEvent, RouterEvent},
gear::CodeCommitment,
SimpleBlockData,
Expand Down Expand Up @@ -239,12 +239,16 @@ impl ComputeService {
}
}

struct ChainHeadProcessContext<DB: OnChainStorage + BlockMetaStorage> {
struct ChainHeadProcessContext<
DB: OnChainStorageRead + BlockMetaStorageWrite + BlockMetaStorageRead,
> {
db: DB,
processor: Processor,
}

impl<DB: OnChainStorage + BlockMetaStorage> ChainHeadProcessContext<DB> {
impl<DB: OnChainStorageRead + BlockMetaStorageWrite + BlockMetaStorageRead>
ChainHeadProcessContext<DB>
{
async fn process(mut self, head: H256) -> Result<BlockProcessed> {
let chain = Self::collect_not_computed_blocks_chain(&self.db, head)?;

Expand Down Expand Up @@ -393,6 +397,7 @@ mod tests {
use std::collections::HashMap;

use super::*;
use ethexe_common::db::OnChainStorageWrite;

// Create new code with a unique nonce
fn create_new_code(nonce: u32) -> Vec<u8> {
Expand Down
2 changes: 1 addition & 1 deletion ethexe/consensus/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use crate::{BatchCommitmentValidationReply, BatchCommitmentValidationRequest};
use ethexe_common::{
db::{BlockMetaStorage, CodesStorage, OnChainStorage},
db::{BlockMetaStorageWrite, CodesStorageWrite, OnChainStorageRead, OnChainStorageWrite},
ecdsa::{PrivateKey, PublicKey, SignedData},
gear::{BlockCommitment, CodeCommitment, Message, StateTransition},
Address, BlockHeader, CodeBlobInfo, Digest, ProducerBlock, SimpleBlockData,
Expand Down
10 changes: 5 additions & 5 deletions ethexe/consensus/src/validator/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
use anyhow::{anyhow, ensure, Result};
use derive_more::{Debug, Display};
use ethexe_common::{
db::{BlockMetaStorage, CodesStorage, OnChainStorage},
db::{BlockMetaStorageRead, CodesStorageRead, OnChainStorageRead},
ecdsa::SignedData,
gear::CodeCommitment,
Address, Digest, SimpleBlockData, ToDigest,
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Participant {
.map(|signature| BatchCommitmentValidationReply { digest, signature })
}

fn validate_code_commitment<DB: OnChainStorage + CodesStorage>(
fn validate_code_commitment<DB: OnChainStorageRead + CodesStorageRead>(
db: &DB,
request: CodeCommitment,
) -> Result<()> {
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Participant {
Ok(())
}

fn validate_block_commitment<DB: BlockMetaStorage + OnChainStorage>(
fn validate_block_commitment<DB: BlockMetaStorageRead + OnChainStorageRead>(
db: &DB,
request: BlockCommitmentValidationRequest,
) -> Result<()> {
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Participant {

/// Verify whether `pred_hash` is a predecessor of `block_hash` in the chain.
fn verify_is_predecessor(
db: &impl OnChainStorage,
db: &impl OnChainStorageRead,
block_hash: H256,
pred_hash: H256,
max_distance: Option<u32>,
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Participant {
mod tests {
use super::*;
use crate::{mock::*, validator::mock::*};
use ethexe_common::{db::OnChainStorage, BlockHeader};
use ethexe_common::{db::OnChainStorageWrite, BlockHeader};
use ethexe_db::Database;

#[test]
Expand Down
3 changes: 2 additions & 1 deletion ethexe/consensus/src/validator/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::ConsensusEvent;
use anyhow::{anyhow, Result};
use derive_more::{Debug, Display};
use ethexe_common::{
db::{BlockMetaStorage, CodesStorage, OnChainStorage},
db::{BlockMetaStorageRead, CodesStorageRead, OnChainStorageRead},
gear::{BatchCommitment, BlockCommitment, CodeCommitment},
Address, CodeBlobInfo, ProducerBlock, SimpleBlockData,
};
Expand Down Expand Up @@ -253,6 +253,7 @@ enum AggregationError {
mod tests {
use super::*;
use crate::{mock::*, validator::mock::*};
use ethexe_common::db::BlockMetaStorageWrite;
use std::vec;

#[tokio::test]
Expand Down
Loading
Loading