Skip to content

Commit

Permalink
Merge pull request #24 from raindust/feat/executor
Browse files Browse the repository at this point in the history
Implement standard transaction executor
  • Loading branch information
yz89 authored Sep 4, 2024
2 parents d7d3e92 + 975f2a8 commit 919928b
Show file tree
Hide file tree
Showing 61 changed files with 1,061 additions and 210 deletions.
350 changes: 204 additions & 146 deletions Cargo.lock

Large diffs are not rendered by default.

42 changes: 40 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ debug = true
split-debuginfo = "packed"

[workspace]
members = ["example", "interface", "storage", "svm/cli", "svm/executor"]
members = [
"example",
"executor",
"interface",
"storage",
"svm/cli",
"svm/executor",
"validator",
]
resolver = "2"

[workspace.package]
Expand Down Expand Up @@ -54,7 +62,9 @@ solana-metrics = { version = "2.0.7" }
spl-token = "=6.0.0"
spl-token-2022 = "=4.0.0"

rollups-interface = { path = "interface" }
igloo-interface = { path = "interface" }
igloo-storage = { path = "storage" }
igloo-validator = { path = "validator" }
svm-executor = { path = "svm/executor" }

[patch.crates-io]
Expand All @@ -64,3 +74,31 @@ solana-curve25519 = { git = "https://github.com/anza-xyz/agave.git", rev = "11b8
solana-program = { git = "https://github.com/anza-xyz/agave.git", rev = "11b87c1ba32c1895898f2a17502417fccdf1a4c8" }
solana-zk-sdk = { git = "https://github.com/anza-xyz/agave.git", rev = "11b87c1ba32c1895898f2a17502417fccdf1a4c8" }
solana-zk-token-sdk = { git = "https://github.com/anza-xyz/agave.git", rev = "11b87c1ba32c1895898f2a17502417fccdf1a4c8" }

## patches to use local versions, uncomment if needed
# solana-program = { path = "../agave/sdk/program" }
# solana-zk-sdk = { path = "../agave/zk-sdk" }
# solana-zk-token-sdk = { path = "../agave/zk-token-sdk" }

## patches to use local versions, uncomment if needed
# solana-bpf-loader-program = { path = "../agave/programs/bpf_loader" }
# solana-compute-budget = { path = "../agave/compute-budget" }
# solana-program-runtime = { path = "../agave/program-runtime" }
# solana-sdk = { path = "../agave/sdk" }
# solana-svm = { path = "../agave/svm" }
# solana-system-program = { path = "../agave/programs/system" }
# solana-logger = { path = "../agave/logger" }
# solana-test-validator = { path = "../agave/test-validator" }
# solana-ledger = { path = "../agave/ledger" }
# solana-runtime = { path = "../agave/runtime" }
# solana-entry = { path = "../agave/entry" }
# solana-accounts-db = { path = "../agave/accounts-db" }
# solana-core = { path = "../agave/core" }
# solana-stake-program = { path = "../agave/programs/stake" }
# solana-measure = { path = "../agave/measure" }
# solana-gossip = { path = "../agave/gossip" }
# solana-streamer = { path = "../agave/streamer" }
# solana-rpc = { path = "../agave/rpc" }
# solana-transaction-status = { path = "../agave/transaction-status" }
# solana-account-decoder = { path = "../agave/account-decoder" }
# solana-metrics = { path = "../agave/metrics" }
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license.workspace = true
edition.workspace = true

[dependencies]
rollups-interface = { workspace = true }
igloo-interface = { workspace = true }
svm-executor = { workspace = true }

solana-sdk = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion example/src/derive/da.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::l1::attribute::PayloadAttributeImpl;
use rollups_interface::derive::DaDerive;
use igloo_interface::derive::DaDerive;
use std::sync::Arc;
use tokio::sync::{mpsc::Receiver, RwLock};

Expand Down
2 changes: 1 addition & 1 deletion example/src/derive/instant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::derive::InstantDerive;
use igloo_interface::derive::InstantDerive;
use tokio::sync::mpsc::{error::TryRecvError, Receiver};

use crate::l1::{attribute::PayloadAttributeImpl, block::L1BlockInfoImpl};
Expand Down
2 changes: 1 addition & 1 deletion example/src/l1/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use super::{head::L1HeadImpl, L1Hash, L1Height, L1Timestamp};
use crate::l2::tx::L2Transaction;
use rollups_interface::l1::{Epoch, PayloadAttribute};
use igloo_interface::l1::{Epoch, PayloadAttribute};

#[derive(Clone)]
pub struct EpochInfo {
Expand Down
2 changes: 1 addition & 1 deletion example/src/l1/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l1::BatchInfo;
use igloo_interface::l1::BatchInfo;

use super::L1Hash;

Expand Down
2 changes: 1 addition & 1 deletion example/src/l1/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use rollups_interface::l1::L1BlockInfo;
use igloo_interface::l1::L1BlockInfo;

use crate::l2::tx::L2Transaction;

Expand Down
2 changes: 1 addition & 1 deletion example/src/l1/head.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l1::L1Head;
use igloo_interface::l1::L1Head;

use super::{L1Hash, L1Height, L1Timestamp};

Expand Down
2 changes: 1 addition & 1 deletion example/src/l1/tx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l1::DepositTransaction;
use igloo_interface::l1::DepositTransaction;
use solana_sdk::pubkey::Pubkey;

pub struct DepositTx {
Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l2::{Block, BlockPayload, Entry};
use igloo_interface::l2::{Block, BlockPayload, Entry};
use solana_sdk::transaction::VersionedTransaction;

use super::head::L2HeadImpl;
Expand Down
4 changes: 1 addition & 3 deletions example/src/l2/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use solana_sdk::signature::Keypair;
use std::{path::Path, sync::Arc};
use tokio::sync::{mpsc::Sender, RwLock};

use rollups_interface::l2::{
stream::TransactionStream, Block, Engine, EngineApi, L2Head, Producer,
};
use igloo_interface::l2::{stream::TransactionStream, Block, Engine, EngineApi, L2Head, Producer};

use crate::l1::attribute::PayloadAttributeImpl;

Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/head.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l2::L2Head;
use igloo_interface::l2::L2Head;

use super::{L2Hash, L2Height, L2Timestamp};

Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::Arc,
};

use rollups_interface::l2::{Block, L2Head};
use igloo_interface::l2::{Block, L2Head};
use tokio::sync::RwLock;

use super::{block::BlockImpl, head::L2HeadImpl, L2Hash, L2Height};
Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/producer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Ok, Result};
use rollups_interface::{
use igloo_interface::{
l1::PayloadAttribute,
l2::{executor::Init, Entry, Producer},
};
Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/stream.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l2::stream::{BatchSettings, TransactionStream};
use igloo_interface::l2::stream::{BatchSettings, TransactionStream};
use std::sync::Arc;
use tokio::sync::RwLock;

Expand Down
2 changes: 1 addition & 1 deletion example/src/l2/tx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollups_interface::l2::Transaction;
use igloo_interface::l2::Transaction;
use solana_sdk::pubkey::Pubkey;

use crate::l1::tx::DepositTx;
Expand Down
4 changes: 2 additions & 2 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use derive::{da::DaDeriveImpl, instant::InstantDeriveImpl};
use igloo_interface::{l2::Engine, runner::Runner};
use l2::batcher::Batcher;
use mock::{chain::MockLayer1, stream::TxServer};
use rollups_interface::{l2::Engine, runner::Runner};
use runner::SimpleRunner;
use std::path::Path;
use tokio::sync::mpsc::channel;
Expand All @@ -27,7 +27,7 @@ async fn main() -> Result<()> {
let da_driver = DaDeriveImpl::default();

let (attribute_sender, attribute_receiver) = channel(1024);
let mut runner = SimpleRunner::new(Path::new("/tmp/rollups-example"), attribute_sender)?;
let mut runner = SimpleRunner::new(Path::new("/tmp/igloo-example"), attribute_sender)?;

runner.register_instant(instanct_driver);
runner.register_da(da_driver.clone());
Expand Down
2 changes: 1 addition & 1 deletion example/src/mock/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::l2::{stream::SharedStream, tx::L2Transaction};
use rollups_interface::l2::stream::TransactionStream;
use igloo_interface::l2::stream::TransactionStream;
use solana_sdk::{signature::Keypair, signer::Signer};

pub struct TxServer {
Expand Down
2 changes: 1 addition & 1 deletion example/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use anyhow::Result;
use rollups_interface::{
use igloo_interface::{
derive::{DaDerive, InstantDerive},
l1::{Epoch, L1BlockInfo, L1Head},
l2::{EngineApi, L2Head},
Expand Down
29 changes: 29 additions & 0 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "igloo-executor"
version.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true
license.workspace = true
edition.workspace = true

[dependencies]
igloo-interface = { workspace = true }
igloo-storage = { workspace = true }
igloo-validator = { workspace = true }
tempfile = { workspace = true }

thiserror = { workspace = true }
tokio = { workspace = true }
log = { workspace = true }
crossbeam-channel = { workspace = true }

solana-ledger = { workspace = true }
solana-accounts-db = { workspace = true }
solana-sdk = { workspace = true }
solana-svm = { workspace = true }
solana-runtime = { workspace = true }
solana-core = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
5 changes: 5 additions & 0 deletions executor/src/defs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use solana_sdk::transaction::SanitizedTransaction;

pub struct BlockPayload {
pub transactions: Vec<SanitizedTransaction>,
}
113 changes: 113 additions & 0 deletions executor/src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::{
defs::BlockPayload,
processor::{bank::BankProcessor, Processor},
Result,
};
use igloo_interface::l2::{bank::BankOperations, storage::StorageOperations};
use igloo_storage::{
blockstore::txs::CommitBatch, config::GlobalConfig, ledger::SlotInfo, RollupStorage,
};
use igloo_validator::{
settings::{Settings, Switchs},
BankValidator, TransactionChecks,
};
use solana_sdk::clock::Slot;
use std::{borrow::Cow, path::Path};

pub mod storage;

pub struct Engine {
storage: RollupStorage,
finalized: Slot,
validator_settings: Settings,
}

impl Engine {
pub fn new(ledger_path: &Path) -> Result<Self> {
let mut config = GlobalConfig::new(ledger_path)?;
config.keypairs.set_default_path(ledger_path);
Self::new_with_config(config)
}

pub fn new_for_test(ledger_path: &Path) -> Result<Self> {
let config = GlobalConfig::new_temp(&ledger_path)?;
Self::new_with_config(config)
}

pub fn new_with_config(config: GlobalConfig) -> Result<Self> {
let settings = Settings {
max_age: 150, // set default max_age from solana
switchs: Switchs {
tx_sanity_check: true,
txs_conflict_check: true,
},
};
Self::new_with_validator_settings(config, settings)
}

pub fn new_with_validator_settings(
config: GlobalConfig,
validator_settings: Settings,
) -> Result<Self> {
let mut storage = RollupStorage::new(config)?;
storage.init()?;

let finalized = storage.get_root();
Ok(Self {
storage,
finalized,
validator_settings,
})
}

pub async fn close(self) -> Result<()> {
self.storage.close().await?;
Ok(())
}

/// Check the block before processing
pub fn check_block(&self, block: &BlockPayload, settins: Option<Switchs>) -> Result<()> {
let switchs = settins.unwrap_or(self.validator_settings.switchs.clone());
let validator = BankValidator::new(self.storage.current_bank(), Default::default());
if switchs.tx_sanity_check {
validator.transactions_sanity_check(&block.transactions)?;
}
if switchs.txs_conflict_check {
validator.transactions_conflict_check(&block.transactions)?;
}
Ok(())
}

pub async fn new_block(&mut self, block: BlockPayload) -> Result<SlotInfo> {
self.storage.bump()?;

let processor =
BankProcessor::new(self.storage.current_bank(), self.validator_settings.clone());
let results = processor.process(Cow::Borrowed(&block.transactions))?;
self.storage
.commit(
results.into(),
CommitBatch::new(Cow::Borrowed(&block.transactions)),
)
.await?;

let info = self.storage.get_slot_info(self.storage.current_height())?;
info!("New block: {:?}", info);
Ok(info)
}

pub fn confirm(&mut self, block: Slot) -> Result<()> {
self.storage.confirm(block)?;
Ok(())
}

pub fn reorg(&mut self, reset_to: Slot) -> Result<()> {
self.storage.reorg(reset_to, Some(self.finalized))?;
Ok(())
}

pub fn finalize(&mut self, block: Slot) -> Result<()> {
self.storage.set_root(block, None)?;
Ok(())
}
}
9 changes: 9 additions & 0 deletions executor/src/engine/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use igloo_storage::RollupStorage;

use super::Engine;

impl Engine {
pub fn store(&self) -> &RollupStorage {
&self.storage
}
}
12 changes: 12 additions & 0 deletions executor/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
StorageError(#[from] igloo_storage::Error),

#[error(transparent)]
ValidatorError(#[from] igloo_validator::Error),
}
14 changes: 14 additions & 0 deletions executor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub mod defs;
pub mod engine;
pub mod error;
pub mod processor;
#[cfg(test)]
mod tests;

pub use {
engine::Engine,
error::{Error, Result},
};

#[macro_use]
extern crate log;
Loading

0 comments on commit 919928b

Please sign in to comment.