diff --git a/Cargo.lock b/Cargo.lock index eb972663b0..0a3ac2c43a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1598,7 +1598,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "env_logger 0.10.0", + "env_logger", "futures 0.3.28", "log", "rand 0.7.3", @@ -1609,6 +1609,7 @@ dependencies = [ "tari_dan_common_types", "tari_dan_storage", "tari_engine_types", + "tari_epoch_manager", "tari_mmr", "tari_shutdown", "tari_state_store_sqlite", @@ -2788,19 +2789,6 @@ dependencies = [ "syn 2.0.18", ] -[[package]] -name = "env_logger" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "env_logger" version = "0.10.0" @@ -2989,12 +2977,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - [[package]] name = "funty" version = "2.0.0" @@ -3770,6 +3752,7 @@ dependencies = [ "tari_crypto", "tari_dan_common_types", "tari_dan_engine", + "tari_dan_storage", "tari_dan_wallet_daemon", "tari_dan_wallet_sdk", "tari_engine_types", @@ -5609,19 +5592,6 @@ dependencies = [ "nibble_vec", ] -[[package]] -name = "rand" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" -dependencies = [ - "fuchsia-cprng", - "libc", - "rand_core 0.3.1", - "rdrand", - "winapi", -] - [[package]] name = "rand" version = "0.7.3" @@ -5666,21 +5636,6 @@ dependencies = [ "rand_core 0.6.4", ] -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", -] - -[[package]] -name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - [[package]] name = "rand_core" version = "0.5.1" @@ -5765,15 +5720,6 @@ dependencies = [ "yasna", ] -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", -] - [[package]] name = "redox_syscall" version = "0.2.16" @@ -5858,15 +5804,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - [[package]] name = "rend" version = "0.4.0" @@ -7395,8 +7332,10 @@ dependencies = [ "tari_common_types", "tari_dan_common_types", "tari_dan_storage", + "tari_epoch_manager", "tari_mmr", "tari_shutdown", + "tari_transaction", "thiserror", "tokio", ] @@ -7597,13 +7536,13 @@ dependencies = [ "tari_core", "tari_crypto", "tari_dan_common_types", - "tari_dan_core", "tari_dan_engine", "tari_dan_storage", "tari_dan_storage_sqlite", "tari_engine_types", "tari_epoch_manager", "tari_shutdown", + "tari_state_store_sqlite", "tari_template_builtin", "tari_template_lib", "tari_transaction", @@ -7617,7 +7556,6 @@ dependencies = [ name = "tari_dan_common_types" version = "0.50.0-pre.0" dependencies = [ - "anyhow", "digest 0.9.0", "newtype-ops", "prost 0.9.0", @@ -7628,45 +7566,12 @@ dependencies = [ "tari_bor", "tari_common", "tari_common_types", - "tari_core", "tari_crypto", "tari_engine_types", "tari_mmr", "tari_utilities", ] -[[package]] -name = "tari_dan_core" -version = "0.50.0-pre.0" -dependencies = [ - "anyhow", - "async-trait", - "blake2 0.9.2", - "digest 0.9.0", - "futures 0.3.28", - "log", - "rand 0.8.5", - "serde", - "tari_base_node_client", - "tari_common", - "tari_common_types", - "tari_comms", - "tari_core", - "tari_crypto", - "tari_dan_common_types", - "tari_dan_engine", - "tari_dan_storage", - "tari_engine_types", - "tari_epoch_manager", - "tari_mmr", - "tari_shutdown", - "tari_test_utils", - "tari_transaction", - "tari_utilities", - "thiserror", - "tokio", -] - [[package]] name = "tari_dan_engine" version = "0.50.0-pre.0" @@ -7696,6 +7601,18 @@ dependencies = [ "wasmer-middlewares", ] +[[package]] +name = "tari_dan_p2p" +version = "0.50.0-pre.0" +dependencies = [ + "async-trait", + "serde", + "tari_comms", + "tari_consensus", + "tari_dan_common_types", + "tari_transaction", +] + [[package]] name = "tari_dan_storage" version = "0.50.0-pre.0" @@ -7744,39 +7661,10 @@ dependencies = [ "tari_crypto", "tari_dan_common_types", "tari_dan_storage", - "tari_engine_types", - "tari_transaction", "tari_utilities", "thiserror", ] -[[package]] -name = "tari_dan_tests" -version = "0.50.0-pre.0" -dependencies = [ - "anyhow", - "env_logger 0.9.3", - "log", - "rand 0.7.3", - "tari_common_types", - "tari_comms", - "tari_core", - "tari_crypto", - "tari_dan_common_types", - "tari_dan_core", - "tari_dan_engine", - "tari_dan_storage", - "tari_dan_storage_sqlite", - "tari_engine_types", - "tari_epoch_manager", - "tari_shutdown", - "tari_template_lib", - "tari_transaction", - "tari_utilities", - "tempdir", - "tokio", -] - [[package]] name = "tari_dan_wallet_cli" version = "0.50.0-pre.0" @@ -7862,6 +7750,7 @@ dependencies = [ "tari_common_types", "tari_crypto", "tari_dan_common_types", + "tari_dan_storage", "tari_dan_wallet_storage_sqlite", "tari_engine_types", "tari_key_manager", @@ -7887,6 +7776,7 @@ dependencies = [ "serde_json", "tari_common_types", "tari_dan_common_types", + "tari_dan_storage", "tari_dan_wallet_sdk", "tari_engine_types", "tari_template_lib", @@ -7923,7 +7813,6 @@ dependencies = [ "anyhow", "async-trait", "log", - "serde", "tari_base_node_client", "tari_common_types", "tari_comms", @@ -7932,6 +7821,7 @@ dependencies = [ "tari_dan_common_types", "tari_dan_storage", "tari_dan_storage_sqlite", + "tari_mmr", "tari_shutdown", "thiserror", "tokio", @@ -7952,7 +7842,6 @@ dependencies = [ "async-trait", "axum", "axum-jrpc", - "chrono", "ciborium", "clap 3.2.25", "config", @@ -7960,11 +7849,9 @@ dependencies = [ "diesel_migrations", "futures 0.3.28", "include_dir", - "lazy_static", "lmdb-zero", "log", "log4rs 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "mini-moka", "prost 0.9.0", "rand 0.8.5", "reqwest", @@ -7977,12 +7864,11 @@ dependencies = [ "tari_common_types", "tari_comms", "tari_comms_logging", - "tari_core", "tari_crypto", "tari_dan_app_utilities", "tari_dan_common_types", - "tari_dan_core", "tari_dan_engine", + "tari_dan_p2p", "tari_dan_storage", "tari_dan_storage_sqlite", "tari_engine_types", @@ -7991,15 +7877,14 @@ dependencies = [ "tari_indexer_lib", "tari_p2p", "tari_shutdown", + "tari_state_store_sqlite", "tari_storage", - "tari_template_builtin", "tari_template_lib", "tari_transaction", "tari_validator_node_client", "tari_validator_node_rpc", "thiserror", "tokio", - "tokio-stream", "tonic 0.6.2", "tower", "tower-http", @@ -8015,7 +7900,6 @@ dependencies = [ "serde", "serde_json", "serde_with", - "tari_common_types", "tari_dan_common_types", "tari_engine_types", "tari_transaction", @@ -8212,7 +8096,7 @@ dependencies = [ "tari_common_types", "tari_dan_common_types", "tari_dan_storage", - "tari_utilities", + "tari_transaction", "thiserror", "time 0.3.22", ] @@ -8305,7 +8189,6 @@ dependencies = [ name = "tari_transaction" version = "0.50.0-pre.0" dependencies = [ - "rand 0.7.3", "serde", "tari_common_types", "tari_crypto", @@ -8354,20 +8237,16 @@ dependencies = [ "async-trait", "axum", "axum-jrpc", - "bytes 1.4.0", - "chrono", "clap 3.2.25", "config", "futures 0.3.28", "include_dir", "json5 0.2.8", - "lazy_static", "libsqlite3-sys", "lmdb-zero", "log", "log4rs 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess", - "mini-moka", "prost 0.9.0", "rand 0.7.3", "reqwest", @@ -8380,20 +8259,22 @@ dependencies = [ "tari_common_types", "tari_comms", "tari_comms_logging", + "tari_consensus", "tari_core", "tari_crypto", "tari_dan_app_utilities", "tari_dan_common_types", - "tari_dan_core", "tari_dan_engine", + "tari_dan_p2p", "tari_dan_storage", "tari_dan_storage_sqlite", "tari_engine_types", "tari_epoch_manager", + "tari_indexer_lib", "tari_p2p", "tari_shutdown", + "tari_state_store_sqlite", "tari_storage", - "tari_template_builtin", "tari_template_lib", "tari_transaction", "tari_validator_node_client", @@ -8402,7 +8283,6 @@ dependencies = [ "thiserror", "time 0.3.22", "tokio", - "tokio-stream", "tonic 0.6.2", "tower", "tower-http", @@ -8471,9 +8351,10 @@ dependencies = [ "tari_common_types", "tari_comms", "tari_comms_rpc_macros", + "tari_consensus", "tari_crypto", "tari_dan_common_types", - "tari_dan_core", + "tari_dan_p2p", "tari_dan_storage", "tari_engine_types", "tari_template_lib", @@ -8576,16 +8457,6 @@ dependencies = [ "wee_alloc", ] -[[package]] -name = "tempdir" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" -dependencies = [ - "rand 0.4.6", - "remove_dir_all", -] - [[package]] name = "tempfile" version = "3.6.0" diff --git a/Cargo.toml b/Cargo.toml index 152ab8ff75..840f0c1725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,11 @@ members = [ "clients/base_node_client", "clients/validator_node_client", "clients/wallet_daemon_client", - "dan_layer/core", "dan_layer/consensus", "dan_layer/consensus_tests", "dan_layer/epoch_manager", "dan_layer/indexer_lib", - "dan_layer/integration_tests", + "dan_layer/p2p", "dan_layer/state_store_sqlite", "dan_layer/storage_lmdb", "dan_layer/storage_sqlite", diff --git a/applications/tari_dan_app_utilities/Cargo.toml b/applications/tari_dan_app_utilities/Cargo.toml index e0c32d7c4a..9bc2396c58 100644 --- a/applications/tari_dan_app_utilities/Cargo.toml +++ b/applications/tari_dan_app_utilities/Cargo.toml @@ -12,7 +12,7 @@ tari_crypto = { version = "0.17" } tari_shutdown = { git = "https://github.com/tari-project/tari.git", tag = "v0.51.0-pre.4" } tari_dan_common_types = { path = "../../dan_layer/common_types" } -tari_dan_core = { path = "../../dan_layer/core" } +tari_state_store_sqlite = { path = "../../dan_layer/state_store_sqlite" } tari_dan_engine = { path = "../../dan_layer/engine" } tari_dan_storage = { path = "../../dan_layer/storage" } tari_dan_storage_sqlite = { path = "../../dan_layer/storage_sqlite" } diff --git a/applications/tari_dan_app_utilities/src/base_layer_scanner.rs b/applications/tari_dan_app_utilities/src/base_layer_scanner.rs index 9bdf4629be..7f0d00003b 100644 --- a/applications/tari_dan_app_utilities/src/base_layer_scanner.rs +++ b/applications/tari_dan_app_utilities/src/base_layer_scanner.rs @@ -37,32 +37,28 @@ use tari_core::transactions::transaction_components::{ ValidatorNodeRegistration, }; use tari_crypto::tari_utilities::ByteArray; -use tari_dan_common_types::{optional::Optional, ShardId}; -use tari_dan_core::consensus_constants::ConsensusConstants; +use tari_dan_common_types::{optional::Optional, Epoch, NodeHeight}; use tari_dan_storage::{ + consensus_models::{Block, SubstateRecord}, global::{GlobalDb, MetadataKey}, - ShardStore, - ShardStoreWriteTransaction, + StateStore, StorageError, }; -use tari_dan_storage_sqlite::{ - error::SqliteStorageError, - global::SqliteGlobalDbAdapter, - sqlite_shard_store_factory::SqliteShardStore, -}; +use tari_dan_storage_sqlite::{error::SqliteStorageError, global::SqliteGlobalDbAdapter}; use tari_engine_types::{ confidential::UnclaimedConfidentialOutput, - substate::{Substate, SubstateAddress, SubstateValue}, -}; -use tari_epoch_manager::{ - base_layer::{EpochManagerError, EpochManagerHandle}, - EpochManager, + substate::{SubstateAddress, SubstateValue}, }; +use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerError, EpochManagerReader}; use tari_shutdown::ShutdownSignal; +use tari_state_store_sqlite::SqliteStateStore; use tari_template_lib::models::{EncryptedData, TemplateAddress, UnclaimedConfidentialOutputAddress}; use tokio::{task, task::JoinHandle, time}; -use crate::template_manager::interface::{TemplateManagerError, TemplateManagerHandle, TemplateRegistration}; +use crate::{ + consensus_constants::ConsensusConstants, + template_manager::interface::{TemplateManagerError, TemplateManagerHandle, TemplateRegistration}, +}; const LOG_TARGET: &str = "tari::dan::base_layer_scanner"; @@ -73,7 +69,7 @@ pub fn spawn( template_manager: TemplateManagerHandle, shutdown: ShutdownSignal, consensus_constants: ConsensusConstants, - shard_store: SqliteShardStore, + shard_store: SqliteStateStore, scan_base_layer: bool, base_layer_scanning_interval: Duration, ) -> JoinHandle> { @@ -106,9 +102,10 @@ pub struct BaseLayerScanner { template_manager: TemplateManagerHandle, shutdown: ShutdownSignal, consensus_constants: ConsensusConstants, - shard_store: SqliteShardStore, + state_store: SqliteStateStore, scan_base_layer: bool, base_layer_scanning_interval: Duration, + has_attempted_scan: bool, } impl BaseLayerScanner { @@ -119,7 +116,7 @@ impl BaseLayerScanner { template_manager: TemplateManagerHandle, shutdown: ShutdownSignal, consensus_constants: ConsensusConstants, - state_store: SqliteShardStore, + state_store: SqliteStateStore, scan_base_layer: bool, base_layer_scanning_interval: Duration, ) -> Self { @@ -134,9 +131,10 @@ impl BaseLayerScanner { template_manager, shutdown, consensus_constants, - shard_store: state_store, + state_store, scan_base_layer, base_layer_scanning_interval, + has_attempted_scan: false, } } @@ -200,7 +198,7 @@ impl BaseLayerScanner { self.sync_blockchain().await?; }, BlockchainProgression::Reorged => { - warn!( + error!( target: LOG_TARGET, "⚠️ Base layer reorg detected. Rescanning from genesis." ); @@ -211,9 +209,16 @@ impl BaseLayerScanner { }, BlockchainProgression::NoProgress => { trace!(target: LOG_TARGET, "No new blocks to scan."); + // If no progress has been made since restarting, we still need to tell the epoch manager that scanning + // is done + if !self.has_attempted_scan { + self.epoch_manager.notify_scanning_complete().await?; + } }, } + self.has_attempted_scan = false; + Ok(()) } @@ -319,7 +324,7 @@ impl BaseLayerScanner { output_hash, output.commitment.as_public_key() ); - self.register_burnt_utxo(&output)?; + self.register_burnt_utxo(&output).await?; }, } } @@ -356,23 +361,39 @@ impl BaseLayerScanner { Ok(()) } - fn register_burnt_utxo(&mut self, output: &TransactionOutput) -> Result<(), BaseLayerScannerError> { + async fn register_burnt_utxo(&mut self, output: &TransactionOutput) -> Result<(), BaseLayerScannerError> { let address = SubstateAddress::UnclaimedConfidentialOutput( UnclaimedConfidentialOutputAddress::try_from_commitment(output.commitment.as_bytes()).map_err(|e| // Technically impossible, but anyway BaseLayerScannerError::InvalidSideChainUtxoResponse(format!("Invalid commitment: {}", e)))?, ); - let substate = Substate::new( - 0, - SubstateValue::UnclaimedConfidentialOutput(UnclaimedConfidentialOutput { - commitment: output.commitment.clone(), - encrypted_data: EncryptedData(output.encrypted_data.to_bytes()), - }), - ); - let shard_id = ShardId::from_address(&address, 0); - self.shard_store - .with_write_tx(|tx| tx.save_burnt_utxo(&substate, address, shard_id)) + let substate = SubstateValue::UnclaimedConfidentialOutput(UnclaimedConfidentialOutput { + commitment: output.commitment.clone(), + encrypted_data: EncryptedData(output.encrypted_data.to_bytes()), + }); + let epoch = self.epoch_manager.current_epoch().await?; + self.state_store + .with_write_tx(|tx| { + let genesis = Block::genesis(epoch); + + SubstateRecord { + address, + version: 0, + substate_value: substate, + state_hash: Default::default(), + created_by_transaction: Default::default(), + created_justify: *genesis.justify().id(), + created_block: *genesis.id(), + created_height: NodeHeight::zero(), + destroyed_by_transaction: None, + destroyed_justify: None, + destroyed_by_block: None, + created_at_epoch: Epoch(0), + destroyed_at_epoch: None, + } + .create(tx) + }) .map_err(|source| BaseLayerScannerError::CouldNotRegisterBurntUtxo { commitment: Box::new(output.commitment.clone()), source, diff --git a/dan_layer/core/src/consensus_constants.rs b/applications/tari_dan_app_utilities/src/consensus_constants.rs similarity index 100% rename from dan_layer/core/src/consensus_constants.rs rename to applications/tari_dan_app_utilities/src/consensus_constants.rs diff --git a/applications/tari_dan_app_utilities/src/lib.rs b/applications/tari_dan_app_utilities/src/lib.rs index 7edd4cb40c..85634fb299 100644 --- a/applications/tari_dan_app_utilities/src/lib.rs +++ b/applications/tari_dan_app_utilities/src/lib.rs @@ -21,5 +21,6 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pub mod base_layer_scanner; -pub mod payload_processor; +pub mod consensus_constants; pub mod template_manager; +pub mod transaction_executor; diff --git a/applications/tari_dan_app_utilities/src/transaction_executor.rs b/applications/tari_dan_app_utilities/src/transaction_executor.rs new file mode 100644 index 0000000000..c4510dc7e0 --- /dev/null +++ b/applications/tari_dan_app_utilities/src/transaction_executor.rs @@ -0,0 +1,101 @@ +// Copyright 2023 The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +use std::sync::Arc; + +use tari_common_types::types::PublicKey; +use tari_crypto::tari_utilities::ByteArray; +use tari_dan_common_types::services::template_provider::TemplateProvider; +use tari_dan_engine::{ + fees::{FeeModule, FeeTable}, + packager::LoadedTemplate, + runtime::{AuthParams, ConsensusContext, RuntimeModule}, + state_store::{memory::MemoryStateStore, StateStoreError}, + transaction::{TransactionError, TransactionProcessor}, +}; +use tari_dan_storage::consensus_models::ExecutedTransaction; +use tari_engine_types::commit_result::{ExecuteResult, FinalizeResult, RejectReason}; +use tari_template_lib::{crypto::RistrettoPublicKeyBytes, prelude::NonFungibleAddress}; +use tari_transaction::Transaction; + +pub trait TransactionExecutor { + type Error: Send + Sync + 'static; + + fn execute( + &self, + transaction: Transaction, + state_store: MemoryStateStore, + consensus_context: ConsensusContext, + ) -> Result; +} + +#[derive(Debug, Clone)] +pub struct TariDanTransactionProcessor { + template_provider: Arc, + fee_table: FeeTable, +} + +impl TariDanTransactionProcessor { + pub fn new(template_provider: TTemplateProvider, fee_table: FeeTable) -> Self { + Self { + template_provider: Arc::new(template_provider), + fee_table, + } + } +} + +impl TransactionExecutor for TariDanTransactionProcessor +where TTemplateProvider: TemplateProvider