diff --git a/Cargo.lock b/Cargo.lock index 7576421ce..c05ebcfde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9223,7 +9223,6 @@ dependencies = [ "blake2", "chacha20poly1305", "digest", - "log", "rand", "tari_crypto", "tari_engine_types", @@ -9347,7 +9346,6 @@ dependencies = [ "digest", "hex", "lazy_static", - "log", "rand", "serde", "serde_json", 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 3930dcbae..dfcb08ee3 100644 --- a/applications/tari_dan_app_utilities/src/base_layer_scanner.rs +++ b/applications/tari_dan_app_utilities/src/base_layer_scanner.rs @@ -44,7 +44,7 @@ use tari_crypto::{ ristretto::RistrettoPublicKey, tari_utilities::{hex::Hex, ByteArray}, }; -use tari_dan_common_types::{optional::Optional, shard::Shard, Epoch, NodeAddressable, NodeHeight}; +use tari_dan_common_types::{optional::Optional, shard::Shard, Epoch, NodeAddressable, NodeHeight, ShardGroup}; use tari_dan_storage::{ consensus_models::{Block, SubstateRecord}, global::{GlobalDb, MetadataKey}, @@ -459,7 +459,11 @@ impl BaseLayerScanner { }); self.state_store .with_write_tx(|tx| { - let genesis = Block::genesis(self.network, Epoch(0), Shard::zero()); + let genesis = Block::genesis( + self.network, + Epoch(0), + ShardGroup::all_shards(self.consensus_constants.num_preshards), + ); // TODO: This should be proposed in a block... SubstateRecord { diff --git a/applications/tari_dan_app_utilities/src/consensus_constants.rs b/applications/tari_dan_app_utilities/src/consensus_constants.rs index 1c943b526..ca050872c 100644 --- a/applications/tari_dan_app_utilities/src/consensus_constants.rs +++ b/applications/tari_dan_app_utilities/src/consensus_constants.rs @@ -20,12 +20,18 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use std::time::Duration; + +use tari_common::configuration::Network; +use tari_dan_common_types::NumPreshards; + #[derive(Clone, Debug)] pub struct ConsensusConstants { pub base_layer_confirmations: u64, pub committee_size: u32, pub max_base_layer_blocks_ahead: u64, pub max_base_layer_blocks_behind: u64, + pub num_preshards: NumPreshards, pub pacemaker_max_base_time: std::time::Duration, } @@ -36,7 +42,19 @@ impl ConsensusConstants { committee_size: 7, max_base_layer_blocks_ahead: 5, max_base_layer_blocks_behind: 5, - pacemaker_max_base_time: std::time::Duration::from_secs(10), + num_preshards: NumPreshards::SixtyFour, + pacemaker_max_base_time: Duration::from_secs(10), + } + } +} + +impl From for ConsensusConstants { + fn from(network: Network) -> Self { + match network { + Network::MainNet => unimplemented!("Mainnet consensus constants not implemented"), + Network::StageNet | Network::NextNet | Network::LocalNet | Network::Igor | Network::Esmeralda => { + Self::devnet() + }, } } } diff --git a/applications/tari_indexer/src/bootstrap.rs b/applications/tari_indexer/src/bootstrap.rs index d067ee915..3102ac89f 100644 --- a/applications/tari_indexer/src/bootstrap.rs +++ b/applications/tari_indexer/src/bootstrap.rs @@ -117,6 +117,7 @@ pub async fn spawn_services( let validator_node_client_factory = TariValidatorNodeRpcClientFactory::new(networking.clone()); let (epoch_manager, _) = tari_epoch_manager::base_layer::spawn_service( EpochManagerConfig { + num_preshards: consensus_constants.num_preshards, base_layer_confirmations: consensus_constants.base_layer_confirmations, committee_size: consensus_constants .committee_size diff --git a/applications/tari_indexer/src/event_scanner.rs b/applications/tari_indexer/src/event_scanner.rs index 442daeed8..67928b3d4 100644 --- a/applications/tari_indexer/src/event_scanner.rs +++ b/applications/tari_indexer/src/event_scanner.rs @@ -27,7 +27,8 @@ use log::*; use tari_bor::decode; use tari_common::configuration::Network; use tari_crypto::tari_utilities::message_format::MessageFormat; -use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, PeerAddress}; +use tari_dan_app_utilities::consensus_constants::ConsensusConstants; +use tari_dan_common_types::{committee::Committee, Epoch, NumPreshards, PeerAddress, ShardGroup}; use tari_dan_p2p::proto::rpc::{GetTransactionResultRequest, PayloadResultStatus, SyncBlocksRequest}; use tari_dan_storage::consensus_models::{Block, BlockId, Decision, TransactionRecord}; use tari_engine_types::{ @@ -128,15 +129,15 @@ impl EventScanner { let current_epoch = self.epoch_manager.current_epoch().await?; let current_committees = self.epoch_manager.get_committees(current_epoch).await?; - for (shard, mut committee) in current_committees { + for (shard_group, mut committee) in current_committees { info!( target: LOG_TARGET, - "Scanning committee epoch={}, shard={}", + "Scanning committee epoch={}, sg={}", current_epoch, - shard + shard_group ); let new_blocks = self - .get_new_blocks_from_committee(shard, &mut committee, current_epoch) + .get_new_blocks_from_committee(shard_group, &mut committee, current_epoch) .await?; info!( target: LOG_TARGET, @@ -409,24 +410,27 @@ impl EventScanner { .collect() } - fn build_genesis_block_id(&self) -> BlockId { - let start_block = Block::zero_block(self.network); + fn build_genesis_block_id(&self, num_preshards: NumPreshards) -> BlockId { + // TODO: this should return the actual genesis for the shard group and epoch + let start_block = Block::zero_block(self.network, num_preshards); *start_block.id() } #[allow(unused_assignments)] async fn get_new_blocks_from_committee( &self, - shard: Shard, + shard_group: ShardGroup, committee: &mut Committee, epoch: Epoch, ) -> Result, anyhow::Error> { // We start scanning from the last scanned block for this commitee - let start_block_id = { - let mut tx = self.substate_store.create_read_tx()?; - tx.get_last_scanned_block_id(epoch, shard)? - }; - let start_block_id = start_block_id.unwrap_or(self.build_genesis_block_id()); + let start_block_id = self + .substate_store + .with_read_tx(|tx| tx.get_last_scanned_block_id(epoch, shard_group))?; + let start_block_id = start_block_id.unwrap_or_else(|| { + let consensus_constants = ConsensusConstants::from(self.network); + self.build_genesis_block_id(consensus_constants.num_preshards) + }); committee.shuffle(); let mut last_block_id = start_block_id; @@ -436,16 +440,16 @@ impl EventScanner { "Scanning new blocks since {} from (epoch={}, shard={})", last_block_id, epoch, - shard + shard_group ); for member in committee.members() { debug!( target: LOG_TARGET, - "Trying to get blocks from VN {} (epoch={}, shard={})", + "Trying to get blocks from VN {} (epoch={}, shard_group={})", member, epoch, - shard + shard_group ); let resp = self.get_blocks_from_vn(member, start_block_id).await; @@ -454,27 +458,27 @@ impl EventScanner { // TODO: try more than 1 VN per commitee info!( target: LOG_TARGET, - "Got {} blocks from VN {} (epoch={}, shard={})", + "Got {} blocks from VN {} (epoch={}, shard_group={})", blocks.len(), member, epoch, - shard, + shard_group, ); if let Some(block) = blocks.last() { last_block_id = *block.id(); } // Store the latest scanned block id in the database for future scans - self.save_scanned_block_id(epoch, shard, last_block_id)?; + self.save_scanned_block_id(epoch, shard_group, last_block_id)?; return Ok(blocks); }, Err(e) => { // We do nothing on a single VN failure, we only log it warn!( target: LOG_TARGET, - "Could not get blocks from VN {} (epoch={}, shard={}): {}", + "Could not get blocks from VN {} (epoch={}, shard_group={}): {}", member, epoch, - shard, + shard_group, e ); }, @@ -484,22 +488,25 @@ impl EventScanner { // We don't raise an error if none of the VNs have blocks, the scanning will retry eventually warn!( target: LOG_TARGET, - "Could not get blocks from any of the VNs of the committee (epoch={}, shard={})", + "Could not get blocks from any of the VNs of the committee (epoch={}, shard_group={})", epoch, - shard + shard_group ); Ok(vec![]) } - fn save_scanned_block_id(&self, epoch: Epoch, shard: Shard, last_block_id: BlockId) -> Result<(), anyhow::Error> { + fn save_scanned_block_id( + &self, + epoch: Epoch, + shard_group: ShardGroup, + last_block_id: BlockId, + ) -> Result<(), anyhow::Error> { let row = NewScannedBlockId { epoch: epoch.0 as i64, - shard: i64::from(shard.as_u32()), + shard_group: shard_group.encode_as_u32() as i32, last_block_id: last_block_id.as_bytes().to_vec(), }; - let mut tx = self.substate_store.create_write_tx()?; - tx.save_scanned_block_id(row)?; - tx.commit()?; + self.substate_store.with_write_tx(|tx| tx.save_scanned_block_id(row))?; Ok(()) } diff --git a/applications/tari_indexer/src/substate_storage_sqlite/migrations/2023-05-19-165832_add_version_to_events_table/up.sql b/applications/tari_indexer/src/substate_storage_sqlite/migrations/2023-05-19-165832_add_version_to_events_table/up.sql index 11b7b4f94..f7f7a98d4 100644 --- a/applications/tari_indexer/src/substate_storage_sqlite/migrations/2023-05-19-165832_add_version_to_events_table/up.sql +++ b/applications/tari_indexer/src/substate_storage_sqlite/migrations/2023-05-19-165832_add_version_to_events_table/up.sql @@ -2,7 +2,7 @@ alter table events add column version integer not null; alter table events - add column component_address string null; + add column component_address text null; -- drop previous index drop index unique_events_indexer; diff --git a/applications/tari_indexer/src/substate_storage_sqlite/migrations/2024-04-04-201211_create_scanned_block_ids/up.sql b/applications/tari_indexer/src/substate_storage_sqlite/migrations/2024-04-04-201211_create_scanned_block_ids/up.sql index 2478178ff..77a058d50 100644 --- a/applications/tari_indexer/src/substate_storage_sqlite/migrations/2024-04-04-201211_create_scanned_block_ids/up.sql +++ b/applications/tari_indexer/src/substate_storage_sqlite/migrations/2024-04-04-201211_create_scanned_block_ids/up.sql @@ -1,16 +1,16 @@ --- Latests scanned blocks, separatedly by committee (epoch + shard) --- Used mostly for effient scanning of events in the whole network +-- Latest scanned blocks, separately by committee (epoch + shard) +-- Used mostly for efficient scanning of events in the whole network create table scanned_block_ids ( id integer not NULL primary key AUTOINCREMENT, epoch bigint not NULL, - shard bigint not null, + shard_group integer not null, last_block_id blob not null ); -- There should only be one last scanned block by committee (epoch + shard) -create unique index scanned_block_ids_unique_commitee on scanned_block_ids (epoch, shard); +create unique index scanned_block_ids_unique_committee on scanned_block_ids (epoch, shard_group); -- DB index for faster retrieval of the latest block by committee -create index scanned_block_ids_commitee on scanned_block_ids (epoch, shard); \ No newline at end of file +create index scanned_block_ids_committee on scanned_block_ids (epoch, shard_group); \ No newline at end of file diff --git a/applications/tari_indexer/src/substate_storage_sqlite/models/events.rs b/applications/tari_indexer/src/substate_storage_sqlite/models/events.rs index f98d9bf37..6300b29b5 100644 --- a/applications/tari_indexer/src/substate_storage_sqlite/models/events.rs +++ b/applications/tari_indexer/src/substate_storage_sqlite/models/events.rs @@ -133,7 +133,7 @@ impl TryFrom for tari_engine_types::events::Event { pub struct ScannedBlockId { pub id: i32, pub epoch: i64, - pub shard: i64, + pub shard_group: i32, pub last_block_id: Vec, } @@ -141,6 +141,6 @@ pub struct ScannedBlockId { #[diesel(table_name = scanned_block_ids)] pub struct NewScannedBlockId { pub epoch: i64, - pub shard: i64, + pub shard_group: i32, pub last_block_id: Vec, } diff --git a/applications/tari_indexer/src/substate_storage_sqlite/schema.rs b/applications/tari_indexer/src/substate_storage_sqlite/schema.rs index 1b1feca87..a1475924e 100644 --- a/applications/tari_indexer/src/substate_storage_sqlite/schema.rs +++ b/applications/tari_indexer/src/substate_storage_sqlite/schema.rs @@ -1,24 +1,11 @@ // @generated automatically by Diesel CLI. diesel::table! { - non_fungible_indexes (id) { - id -> Integer, - resource_address -> Text, - idx -> Integer, - non_fungible_address -> Text, - } -} - -diesel::table! { - substates (id) { + event_payloads (id) { id -> Integer, - address -> Text, - version -> BigInt, - data -> Text, - tx_hash -> Text, - template_address -> Nullable, - module_name -> Nullable, - timestamp -> BigInt, + payload_key -> Text, + payload_value -> Text, + event_id -> Integer, } } @@ -36,11 +23,11 @@ diesel::table! { } diesel::table! { - event_payloads (id) { + non_fungible_indexes (id) { id -> Integer, - payload_key -> Text, - payload_value -> Text, - event_id -> Integer, + resource_address -> Text, + idx -> Integer, + non_fungible_address -> Text, } } @@ -48,11 +35,30 @@ diesel::table! { scanned_block_ids (id) { id -> Integer, epoch -> BigInt, - shard -> BigInt, + shard_group -> Integer, last_block_id -> Binary, } } +diesel::table! { + substates (id) { + id -> Integer, + address -> Text, + version -> BigInt, + data -> Text, + tx_hash -> Text, + template_address -> Nullable, + module_name -> Nullable, + timestamp -> BigInt, + } +} + diesel::joinable!(event_payloads -> events (event_id)); -diesel::allow_tables_to_appear_in_same_query!(substates, non_fungible_indexes, events, event_payloads); +diesel::allow_tables_to_appear_in_same_query!( + event_payloads, + events, + non_fungible_indexes, + scanned_block_ids, + substates, +); diff --git a/applications/tari_indexer/src/substate_storage_sqlite/sqlite_substate_store_factory.rs b/applications/tari_indexer/src/substate_storage_sqlite/sqlite_substate_store_factory.rs index d117502fa..f8cb3d1bb 100644 --- a/applications/tari_indexer/src/substate_storage_sqlite/sqlite_substate_store_factory.rs +++ b/applications/tari_indexer/src/substate_storage_sqlite/sqlite_substate_store_factory.rs @@ -39,7 +39,7 @@ use diesel::{ use diesel_migrations::{EmbeddedMigrations, MigrationHarness}; use log::*; use tari_crypto::tari_utilities::hex::to_hex; -use tari_dan_common_types::{shard::Shard, substate_type::SubstateType, Epoch}; +use tari_dan_common_types::{substate_type::SubstateType, Epoch, ShardGroup}; use tari_dan_storage::{consensus_models::BlockId, StorageError}; use tari_dan_storage_sqlite::{error::SqliteStorageError, SqliteTransaction}; use tari_engine_types::substate::SubstateId; @@ -230,7 +230,11 @@ pub trait SubstateStoreReadTransaction { limit: u32, ) -> Result, StorageError>; fn event_exists(&mut self, event: NewEvent) -> Result; - fn get_last_scanned_block_id(&mut self, epoch: Epoch, shard: Shard) -> Result, StorageError>; + fn get_last_scanned_block_id( + &mut self, + epoch: Epoch, + shard_group: ShardGroup, + ) -> Result, StorageError>; } impl SubstateStoreReadTransaction for SqliteSubstateStoreReadTransaction<'_> { @@ -597,14 +601,18 @@ impl SubstateStoreReadTransaction for SqliteSubstateStoreReadTransaction<'_> { Ok(exists) } - fn get_last_scanned_block_id(&mut self, epoch: Epoch, shard: Shard) -> Result, StorageError> { + fn get_last_scanned_block_id( + &mut self, + epoch: Epoch, + shard_group: ShardGroup, + ) -> Result, StorageError> { use crate::substate_storage_sqlite::schema::scanned_block_ids; let row: Option = scanned_block_ids::table .filter( scanned_block_ids::epoch .eq(epoch.0 as i64) - .and(scanned_block_ids::shard.eq(i64::from(shard.as_u32()))), + .and(scanned_block_ids::shard_group.eq(shard_group.encode_as_u32() as i32)), ) .first(self.connection()) .optional() @@ -795,7 +803,7 @@ impl SubstateStoreWriteTransaction for SqliteSubstateStoreWriteTransaction<'_> { diesel::insert_into(scanned_block_ids::table) .values(&new) - .on_conflict((scanned_block_ids::epoch, scanned_block_ids::shard)) + .on_conflict((scanned_block_ids::epoch, scanned_block_ids::shard_group)) .do_update() .set(new.clone()) .execute(&mut *self.connection()) @@ -805,7 +813,7 @@ impl SubstateStoreWriteTransaction for SqliteSubstateStoreWriteTransaction<'_> { debug!( target: LOG_TARGET, - "Added new scanned block id {} for epoch {} and shard {:?}", to_hex(&new.last_block_id), new.epoch, new.shard + "Added new scanned block id {} for epoch {} and shard {:?}", to_hex(&new.last_block_id), new.epoch, new.shard_group ); Ok(()) diff --git a/applications/tari_validator_node/src/bootstrap.rs b/applications/tari_validator_node/src/bootstrap.rs index 3cdbb556b..9e6131789 100644 --- a/applications/tari_validator_node/src/bootstrap.rs +++ b/applications/tari_validator_node/src/bootstrap.rs @@ -49,7 +49,7 @@ use tari_dan_app_utilities::{ template_manager::{implementation::TemplateManager, interface::TemplateManagerHandle}, transaction_executor::TariDanTransactionProcessor, }; -use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, PeerAddress}; +use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, NumPreshards, PeerAddress, ShardGroup}; use tari_dan_engine::fees::FeeTable; use tari_dan_p2p::TariMessagingSpec; use tari_dan_storage::{ @@ -184,21 +184,21 @@ pub async fn spawn_services( // Connect to shard db let state_store = SqliteStateStore::connect(&format!("sqlite://{}", config.validator_node.state_db_path().display()))?; - state_store.with_write_tx(|tx| bootstrap_state(tx, config.network))?; + state_store.with_write_tx(|tx| bootstrap_state(tx, config.network, consensus_constants.num_preshards))?; info!(target: LOG_TARGET, "Epoch manager initializing"); + let epoch_manager_config = EpochManagerConfig { + base_layer_confirmations: consensus_constants.base_layer_confirmations, + committee_size: consensus_constants + .committee_size + .try_into() + .context("committee size must be non-zero")?, + validator_node_sidechain_id: config.validator_node.validator_node_sidechain_id.clone(), + num_preshards: consensus_constants.num_preshards, + }; // Epoch manager let (epoch_manager, join_handle) = tari_epoch_manager::base_layer::spawn_service( - // TODO: We should be able to pass consensus constants here. However, these are currently located in dan_core - // which depends on epoch_manager, so would be a circular dependency. - EpochManagerConfig { - base_layer_confirmations: consensus_constants.base_layer_confirmations, - committee_size: consensus_constants - .committee_size - .try_into() - .context("committee size must be non-zero")?, - validator_node_sidechain_id: config.validator_node.validator_node_sidechain_id.clone(), - }, + epoch_manager_config, global_db.clone(), base_node_client.clone(), keypair.public_key().clone(), @@ -275,6 +275,7 @@ pub async fn spawn_services( let gossip = Gossip::new(networking.clone(), rx_gossip_messages); let (mempool, join_handle) = mempool::spawn( + consensus_constants.num_preshards, gossip, epoch_manager.clone(), create_mempool_transaction_validator(&config.validator_node, template_manager.clone()), @@ -441,7 +442,7 @@ async fn spawn_p2p_rpc( Ok(()) } -fn bootstrap_state(tx: &mut TTx, network: Network) -> Result<(), StorageError> +fn bootstrap_state(tx: &mut TTx, network: Network, num_preshards: NumPreshards) -> Result<(), StorageError> where TTx: StateStoreWriteTransaction + Deref, TTx::Target: StateStoreReadTransaction, @@ -464,7 +465,7 @@ where None, None, ); - create_substate(tx, network, PUBLIC_IDENTITY_RESOURCE_ADDRESS, value)?; + create_substate(tx, network, num_preshards, PUBLIC_IDENTITY_RESOURCE_ADDRESS, value)?; let mut xtr_resource = Resource::new( ResourceType::Confidential, @@ -489,7 +490,7 @@ where state: cbor!({"vault" => XTR_FAUCET_VAULT_ADDRESS}).unwrap(), }, }; - create_substate(tx, network, XTR_FAUCET_COMPONENT_ADDRESS, value)?; + create_substate(tx, network, num_preshards, XTR_FAUCET_COMPONENT_ADDRESS, value)?; xtr_resource.increase_total_supply(Amount::MAX); let value = Vault::new(ResourceContainer::Confidential { @@ -500,10 +501,16 @@ where locked_revealed_amount: Default::default(), }); - create_substate(tx, network, XTR_FAUCET_VAULT_ADDRESS, value)?; + create_substate(tx, network, num_preshards, XTR_FAUCET_VAULT_ADDRESS, value)?; } - create_substate(tx, network, CONFIDENTIAL_TARI_RESOURCE_ADDRESS, xtr_resource)?; + create_substate( + tx, + network, + num_preshards, + CONFIDENTIAL_TARI_RESOURCE_ADDRESS, + xtr_resource, + )?; Ok(()) } @@ -511,6 +518,7 @@ where fn create_substate( tx: &mut TTx, network: Network, + num_preshards: NumPreshards, substate_id: TId, value: TVal, ) -> Result<(), StorageError> @@ -521,7 +529,7 @@ where TId: Into, TVal: Into, { - let genesis_block = Block::genesis(network, Epoch(0), Shard::zero()); + let genesis_block = Block::genesis(network, Epoch(0), ShardGroup::all_shards(num_preshards)); let substate_id = substate_id.into(); let id = VersionedSubstateId::new(substate_id, 0); SubstateRecord { diff --git a/applications/tari_validator_node/src/consensus/block_transaction_executor.rs b/applications/tari_validator_node/src/consensus/block_transaction_executor.rs index a7cd42bab..d6bad7cd5 100644 --- a/applications/tari_validator_node/src/consensus/block_transaction_executor.rs +++ b/applications/tari_validator_node/src/consensus/block_transaction_executor.rs @@ -1,7 +1,7 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use indexmap::IndexMap; use log::info; @@ -13,10 +13,11 @@ use tari_dan_app_utilities::transaction_executor::TransactionExecutor; use tari_dan_common_types::{optional::Optional, Epoch}; use tari_dan_engine::state_store::{memory::MemoryStateStore, new_memory_store, AtomicDb, StateWriter}; use tari_dan_storage::{ - consensus_models::{ExecutedTransaction, TransactionRecord}, + consensus_models::{ExecutedTransaction, SubstateLockFlag, TransactionRecord, VersionedSubstateIdLockIntent}, StateStore, }; use tari_engine_types::{ + commit_result::{ExecuteResult, FinalizeResult, RejectReason, TransactionResult}, substate::{Substate, SubstateId}, virtual_substate::{VirtualSubstate, VirtualSubstateId, VirtualSubstates}, }; @@ -138,10 +139,38 @@ where store: &PendingSubstateStore, current_epoch: Epoch, ) -> Result { - let id: tari_transaction::TransactionId = *transaction.id(); + let id = *transaction.id(); // Get the latest input substates - let inputs = self.resolve_substates::(&transaction, store)?; + let inputs = match self.resolve_substates::(&transaction, store) { + Ok(inputs) => inputs, + Err(err) => { + // TODO: Hacky - if a transaction uses DOWNed/non-existent inputs we error here. This changes the hard + // error to a propose REJECT. So that we have involved shards, we use the inputs as resolved inputs and + // assume v0 if version is not provided. + let inputs = transaction + .all_inputs_iter() + .map(|input| VersionedSubstateId::new(input.substate_id, input.version.unwrap_or(0))) + .map(|id| VersionedSubstateIdLockIntent::new(id, SubstateLockFlag::Write)) + .collect(); + return Ok(ExecutedTransaction::new( + transaction, + ExecuteResult { + finalize: FinalizeResult { + transaction_hash: id.into_array().into(), + events: vec![], + logs: vec![], + execution_results: vec![], + result: TransactionResult::Reject(RejectReason::ExecutionFailure(err.to_string())), + fee_receipt: Default::default(), + }, + }, + inputs, + vec![], + Duration::from_secs(0), + )); + }, + }; info!(target: LOG_TARGET, "Transaction {} executing. Inputs: {:?}", id, inputs); // Create a memory db with all the input substates, needed for the transaction execution diff --git a/applications/tari_validator_node/src/consensus/mod.rs b/applications/tari_validator_node/src/consensus/mod.rs index 22f7076d9..0576672af 100644 --- a/applications/tari_validator_node/src/consensus/mod.rs +++ b/applications/tari_validator_node/src/consensus/mod.rs @@ -81,9 +81,17 @@ pub async fn spawn( let transaction_pool = TransactionPool::new(); let (tx_hotstuff_events, _) = broadcast::channel(100); + let hs_config = HotstuffConfig { + network, + max_base_layer_blocks_behind: consensus_constants.max_base_layer_blocks_behind, + max_base_layer_blocks_ahead: consensus_constants.max_base_layer_blocks_ahead, + num_preshards: consensus_constants.num_preshards, + pacemaker_max_base_time: consensus_constants.pacemaker_max_base_time, + }; + let hotstuff_worker = HotstuffWorker::::new( + hs_config, local_addr, - network, inbound_messaging, outbound_messaging, rx_new_transactions, @@ -96,11 +104,6 @@ pub async fn spawn( tx_hotstuff_events.clone(), hooks, shutdown_signal.clone(), - HotstuffConfig { - max_base_layer_blocks_behind: consensus_constants.max_base_layer_blocks_behind, - max_base_layer_blocks_ahead: consensus_constants.max_base_layer_blocks_ahead, - pacemaker_max_base_time: consensus_constants.pacemaker_max_base_time, - }, ); let current_view = hotstuff_worker.pacemaker().current_view().clone(); diff --git a/applications/tari_validator_node/src/p2p/rpc/service_impl.rs b/applications/tari_validator_node/src/p2p/rpc/service_impl.rs index 986dc7ca1..a865f28fe 100644 --- a/applications/tari_validator_node/src/p2p/rpc/service_impl.rs +++ b/applications/tari_validator_node/src/p2p/rpc/service_impl.rs @@ -51,7 +51,6 @@ use tari_dan_storage::{ EpochCheckpoint, HighQc, LockedBlock, - QuorumCertificate, StateTransitionId, SubstateRecord, TransactionRecord, @@ -314,11 +313,10 @@ impl ValidatorNodeRpcService for ValidatorNodeRpcServiceImpl { .map(|hqc| hqc.get_quorum_certificate(tx)) .transpose() }) - .map_err(RpcStatus::log_internal_error(LOG_TARGET))? - .unwrap_or_else(QuorumCertificate::genesis); + .map_err(RpcStatus::log_internal_error(LOG_TARGET))?; Ok(Response::new(GetHighQcResponse { - high_qc: Some((&high_qc).into()), + high_qc: high_qc.as_ref().map(Into::into), })) } @@ -359,7 +357,7 @@ impl ValidatorNodeRpcService for ValidatorNodeRpcServiceImpl { let checkpoint = self .shard_state_store - .with_read_tx(|tx| EpochCheckpoint::generate(tx, prev_epoch, local_committee_info.shard())) + .with_read_tx(|tx| EpochCheckpoint::generate(tx, prev_epoch, local_committee_info.shard_group())) .optional() .map_err(RpcStatus::log_internal_error(LOG_TARGET))?; @@ -373,20 +371,19 @@ impl ValidatorNodeRpcService for ValidatorNodeRpcServiceImpl { let (sender, receiver) = mpsc::channel(10); - let last_state_transition_for_chain = - StateTransitionId::new(Epoch(req.start_epoch), Shard::from(req.start_shard), req.start_seq); + let start_epoch = Epoch(req.start_epoch); + let start_shard = Shard::from(req.start_shard); + let last_state_transition_for_chain = StateTransitionId::new(start_epoch, start_shard, req.start_seq); - // TODO: validate that we can provide the required sync data - let current_shard = Shard::from(req.current_shard); - let current_epoch = Epoch(req.current_epoch); - info!(target: LOG_TARGET, "๐ŸŒpeer initiated sync with this node ({current_epoch}, {current_shard})"); + let end_epoch = Epoch(req.current_epoch); + info!(target: LOG_TARGET, "๐ŸŒpeer initiated sync with this node ({}, {}, seq={}) to {}", start_epoch, start_shard, req.start_seq, end_epoch); task::spawn( StateSyncTask::new( self.shard_state_store.clone(), sender, last_state_transition_for_chain, - current_epoch, + end_epoch, ) .run(), ); diff --git a/applications/tari_validator_node/src/p2p/rpc/state_sync_task.rs b/applications/tari_validator_node/src/p2p/rpc/state_sync_task.rs index 24322886a..494d0baea 100644 --- a/applications/tari_validator_node/src/p2p/rpc/state_sync_task.rs +++ b/applications/tari_validator_node/src/p2p/rpc/state_sync_task.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: BSD-3-Clause use log::*; -use tari_dan_common_types::Epoch; +use tari_dan_common_types::{optional::Optional, Epoch}; use tari_dan_p2p::proto::rpc::SyncStateResponse; use tari_dan_storage::{ consensus_models::{StateTransition, StateTransitionId}, @@ -43,7 +43,7 @@ impl StateSyncTask { pub async fn run(mut self) -> Result<(), ()> { let mut buffer = Vec::with_capacity(BATCH_SIZE); let mut current_state_transition_id = self.start_state_transition_id; - let mut counter = 0; + let mut counter = 0usize; loop { match self.fetch_next_batch(&mut buffer, current_state_transition_id) { Ok(Some(last_state_transition_id)) => { @@ -57,6 +57,7 @@ impl StateSyncTask { // )))) // .await?; + info!(target: LOG_TARGET, "๐ŸŒsync complete ({}). {} update(s) sent.", current_state_transition_id, counter); // Finished return Ok(()); }, @@ -92,7 +93,9 @@ impl StateSyncTask { ) -> Result, StorageError> { self.store.with_read_tx(|tx| { let state_transitions = - StateTransition::get_n_after(tx, BATCH_SIZE, current_state_transition_id, self.current_epoch)?; + StateTransition::get_n_after(tx, BATCH_SIZE, current_state_transition_id, self.current_epoch) + .optional()? + .unwrap_or_default(); let Some(last) = state_transitions.last() else { return Ok(None); diff --git a/applications/tari_validator_node/src/p2p/services/mempool/gossip.rs b/applications/tari_validator_node/src/p2p/services/mempool/gossip.rs index 2bb9ce0b9..931680754 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/gossip.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/gossip.rs @@ -4,7 +4,7 @@ use std::collections::HashSet; use log::*; -use tari_dan_common_types::{shard::Shard, Epoch, PeerAddress, SubstateAddress}; +use tari_dan_common_types::{Epoch, NumPreshards, PeerAddress, ShardGroup, SubstateAddress}; use tari_dan_p2p::{proto, DanMessage}; use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerReader}; @@ -14,14 +14,16 @@ const LOG_TARGET: &str = "tari::validator_node::mempool::gossip"; #[derive(Debug)] pub(super) struct MempoolGossip { + num_preshards: NumPreshards, epoch_manager: EpochManagerHandle, gossip: Gossip, - is_subscribed: Option, + is_subscribed: Option, } impl MempoolGossip { - pub fn new(epoch_manager: EpochManagerHandle, outbound: Gossip) -> Self { + pub fn new(num_preshards: NumPreshards, epoch_manager: EpochManagerHandle, outbound: Gossip) -> Self { Self { + num_preshards, epoch_manager, gossip: outbound, is_subscribed: None, @@ -38,7 +40,7 @@ impl MempoolGossip { pub async fn subscribe(&mut self, epoch: Epoch) -> Result<(), MempoolError> { let committee_shard = self.epoch_manager.get_local_committee_info(epoch).await?; match self.is_subscribed { - Some(b) if b == committee_shard.shard() => { + Some(b) if b == committee_shard.shard_group() => { return Ok(()); }, Some(_) => { @@ -48,9 +50,13 @@ impl MempoolGossip { } self.gossip - .subscribe_topic(format!("transactions-{}", committee_shard.shard())) + .subscribe_topic(format!( + "transactions-{}-{}", + committee_shard.shard_group().start(), + committee_shard.shard_group().end() + )) .await?; - self.is_subscribed = Some(committee_shard.shard()); + self.is_subscribed = Some(committee_shard.shard_group()); Ok(()) } @@ -65,7 +71,7 @@ impl MempoolGossip { pub async fn forward_to_local_replicas(&mut self, epoch: Epoch, msg: DanMessage) -> Result<(), MempoolError> { let committee = self.epoch_manager.get_local_committee_info(epoch).await?; - let topic = format!("transactions-{}", committee.shard()); + let topic = format!("transactions-{}", committee.shard_group()); debug!( target: LOG_TARGET, "forward_to_local_replicas: topic: {}", topic, @@ -82,15 +88,15 @@ impl MempoolGossip { epoch: Epoch, substate_addresses: HashSet, msg: T, - exclude_shard: Option, + exclude_shard_group: Option, ) -> Result<(), MempoolError> { let n = self.epoch_manager.get_num_committees(epoch).await?; let committee_shard = self.epoch_manager.get_local_committee_info(epoch).await?; - let local_shard = committee_shard.shard(); + let local_shard_group = committee_shard.shard_group(); let shards = substate_addresses .into_iter() - .map(|s| s.to_shard(n)) - .filter(|b| exclude_shard.as_ref() != Some(b) && b != &local_shard) + .map(|s| s.to_shard_group(self.num_preshards, n)) + .filter(|sg| exclude_shard_group.as_ref() != Some(sg) && sg != &local_shard_group) .collect::>(); let msg = proto::network::DanMessage::from(&msg.into()); @@ -167,7 +173,7 @@ impl MempoolGossip { pub async fn gossip_to_foreign_replicas>( &mut self, epoch: Epoch, - shards: HashSet, + addresses: HashSet, msg: T, ) -> Result<(), MempoolError> { // let committees = self.epoch_manager.get_committees_by_shards(epoch, shards).await?; @@ -203,7 +209,7 @@ impl MempoolGossip { // // self.outbound.broadcast(selected_members.iter(), msg).await?; - self.forward_to_foreign_replicas(epoch, shards, msg, None).await?; + self.forward_to_foreign_replicas(epoch, addresses, msg, None).await?; Ok(()) } diff --git a/applications/tari_validator_node/src/p2p/services/mempool/initializer.rs b/applications/tari_validator_node/src/p2p/services/mempool/initializer.rs index e07e70ecc..735fe9518 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/initializer.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/initializer.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use log::*; -use tari_dan_common_types::PeerAddress; +use tari_dan_common_types::{NumPreshards, PeerAddress}; use tari_epoch_manager::base_layer::EpochManagerHandle; use tari_state_store_sqlite::SqliteStateStore; use tari_transaction::Transaction; @@ -42,6 +42,7 @@ use crate::{ const LOG_TARGET: &str = "tari::dan::validator_node::mempool"; pub fn spawn( + num_preshards: NumPreshards, gossip: Gossip, epoch_manager: EpochManagerHandle, transaction_validator: TValidator, @@ -59,6 +60,7 @@ where #[cfg(feature = "metrics")] let metrics = PrometheusMempoolMetrics::new(metrics_registry); let mempool = MempoolService::new( + num_preshards, rx_mempool_request, gossip, epoch_manager, diff --git a/applications/tari_validator_node/src/p2p/services/mempool/service.rs b/applications/tari_validator_node/src/p2p/services/mempool/service.rs index b7b8f1594..2d435a3de 100644 --- a/applications/tari_validator_node/src/p2p/services/mempool/service.rs +++ b/applications/tari_validator_node/src/p2p/services/mempool/service.rs @@ -23,7 +23,7 @@ use std::{collections::HashSet, fmt::Display, iter}; use log::*; -use tari_dan_common_types::{optional::Optional, shard::Shard, PeerAddress, SubstateAddress}; +use tari_dan_common_types::{optional::Optional, NumPreshards, PeerAddress, ShardGroup, SubstateAddress}; use tari_dan_p2p::{DanMessage, NewTransactionMessage}; use tari_dan_storage::{consensus_models::TransactionRecord, StateStore}; use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerEvent, EpochManagerReader}; @@ -48,6 +48,7 @@ const LOG_TARGET: &str = "tari::validator_node::mempool::service"; #[derive(Debug)] pub struct MempoolService { + num_preshards: NumPreshards, transactions: HashSet, mempool_requests: mpsc::Receiver, epoch_manager: EpochManagerHandle, @@ -63,6 +64,7 @@ impl MempoolService where TValidator: Validator { pub(super) fn new( + num_preshards: NumPreshards, mempool_requests: mpsc::Receiver, gossip: Gossip, epoch_manager: EpochManagerHandle, @@ -72,7 +74,8 @@ where TValidator: Validator Self { Self { - gossip: MempoolGossip::new(epoch_manager.clone(), gossip), + num_preshards, + gossip: MempoolGossip::new(num_preshards, epoch_manager.clone(), gossip), transactions: Default::default(), mempool_requests, epoch_manager, @@ -202,29 +205,25 @@ where TValidator: Validator, should_propagate: bool, - sender_shard: Option, + sender_shard_group: Option, ) -> Result<(), MempoolError> { #[cfg(feature = "metrics")] self.metrics.on_transaction_received(&transaction); @@ -282,10 +281,7 @@ where TValidator: Validator; leaf_hashes: Array; decision: QuorumDecision; diff --git a/bindings/src/types/ShardGroup.ts b/bindings/src/types/ShardGroup.ts new file mode 100644 index 000000000..92d3e1ead --- /dev/null +++ b/bindings/src/types/ShardGroup.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Shard } from "./Shard"; + +export interface ShardGroup { + start: Shard; + end_inclusive: Shard; +} diff --git a/bindings/tsconfig.json b/bindings/tsconfig.json index a27fffd81..3e27698b9 100644 --- a/bindings/tsconfig.json +++ b/bindings/tsconfig.json @@ -4,7 +4,7 @@ "target": "ESNext", "moduleResolution": "Bundler", "declaration": true, - "outDir": "./dist", + "outDir": "./dist" }, - "include": ["src/**/*"], + "include": ["src/**/*"] } diff --git a/dan_layer/common_types/src/committee.rs b/dan_layer/common_types/src/committee.rs index c97f61c44..e8143397f 100644 --- a/dan_layer/common_types/src/committee.rs +++ b/dan_layer/common_types/src/committee.rs @@ -6,13 +6,15 @@ use std::{borrow::Borrow, cmp, ops::RangeInclusive}; use rand::{rngs::OsRng, seq::SliceRandom}; use serde::{Deserialize, Serialize}; use tari_common_types::types::PublicKey; -#[cfg(feature = "ts")] -use ts_rs::TS; -use crate::{shard::Shard, Epoch, SubstateAddress}; +use crate::{shard::Shard, Epoch, NumPreshards, ShardGroup, SubstateAddress}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Default, Hash)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct Committee { // TODO: not pub #[cfg_attr(feature = "ts", ts(type = "Array<[TAddr, string]>"))] @@ -160,56 +162,62 @@ impl FromIterator> for Committee { /// Represents a "slice" of the 256-bit shard space #[derive(Debug, Clone, Copy, Serialize, Deserialize)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct CommitteeInfo { + num_shards: NumPreshards, + num_shard_group_members: u32, num_committees: u32, - num_members: u32, - #[cfg_attr(feature = "ts", ts(type = "number"))] - shard: Shard, + shard_group: ShardGroup, } impl CommitteeInfo { - pub fn new(num_committees: u32, num_members: u32, shard: Shard) -> Self { + pub fn new( + num_shards: NumPreshards, + num_shard_group_members: u32, + num_committees: u32, + shard_group: ShardGroup, + ) -> Self { Self { + num_shards, + num_shard_group_members, num_committees, - num_members, - shard, + shard_group, } } /// Returns $n - f$ where n is the number of committee members and f is the tolerated failure nodes. pub fn quorum_threshold(&self) -> u32 { - self.num_members - self.max_failures() + self.num_shard_group_members - self.max_failures() } /// Returns the maximum number of failures $f$ that can be tolerated by this committee. pub fn max_failures(&self) -> u32 { - let len = self.num_members; + let len = self.num_shard_group_members; if len == 0 { return 0; } (len - 1) / 3 } - pub fn num_committees(&self) -> u32 { - self.num_committees - } - - pub fn num_members(&self) -> u32 { - self.num_members + pub fn num_shards(&self) -> NumPreshards { + self.num_shards } - pub fn shard(&self) -> Shard { - self.shard + pub fn shard_group(&self) -> ShardGroup { + self.shard_group } pub fn to_substate_address_range(&self) -> RangeInclusive { - self.shard.to_substate_address_range(self.num_committees) + self.shard_group.to_substate_address_range(self.num_shards) } pub fn includes_substate_address(&self, substate_address: &SubstateAddress) -> bool { - let s = substate_address.to_shard(self.num_committees); - self.shard == s + let s = substate_address.to_shard(self.num_shards); + self.shard_group.contains(&s) } pub fn includes_all_substate_addresses, B: Borrow>( @@ -240,25 +248,45 @@ impl CommitteeInfo { .filter(|substate_address| self.includes_substate_address(substate_address.borrow())) } - /// Calculates the number of distinct shards for a given shard set - pub fn count_distinct_shards, I: IntoIterator>(&self, shards: I) -> usize { - shards + /// Calculates the number of distinct shards for the given addresses + pub fn count_distinct_shards, I: IntoIterator>(&self, addresses: I) -> usize { + addresses + .into_iter() + .map(|addr| addr.borrow().to_shard(self.num_shards)) + .collect::>() + .len() + } + + /// Calculates the number of distinct shard groups for the given addresses + pub fn count_distinct_shard_groups, I: IntoIterator>( + &self, + addresses: I, + ) -> usize { + addresses .into_iter() - .map(|shard| shard.borrow().to_shard(self.num_committees)) + .map(|addr| addr.borrow().to_shard_group(self.num_shards, self.num_committees)) .collect::>() .len() } } #[derive(Debug, Clone, Serialize)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct NetworkCommitteeInfo { pub epoch: Epoch, pub committees: Vec>, } #[derive(Debug, Clone, Serialize)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct CommitteeShardInfo { #[cfg_attr(feature = "ts", ts(type = "number"))] pub shard: Shard, diff --git a/dan_layer/common_types/src/hashing.rs b/dan_layer/common_types/src/hashing.rs index 5254f498d..efaaa5846 100644 --- a/dan_layer/common_types/src/hashing.rs +++ b/dan_layer/common_types/src/hashing.rs @@ -36,6 +36,10 @@ pub fn block_hasher() -> TariHasher { dan_hasher("Block") } +pub fn state_root_hasher() -> TariHasher { + dan_hasher("JmtStateRoots") +} + pub fn quorum_certificate_hasher() -> TariHasher { dan_hasher("QuorumCertificate") } diff --git a/dan_layer/common_types/src/lib.rs b/dan_layer/common_types/src/lib.rs index 757d3cc19..9105b5631 100644 --- a/dan_layer/common_types/src/lib.rs +++ b/dan_layer/common_types/src/lib.rs @@ -13,9 +13,11 @@ pub mod hashing; pub mod optional; mod node_height; -pub mod shard; pub use node_height::NodeHeight; +pub mod shard; +mod shard_group; +pub use shard_group::*; mod validator_metadata; pub use validator_metadata::{vn_node_hash, ValidatorMetadata}; @@ -31,6 +33,8 @@ pub mod substate_type; mod peer_address; pub use peer_address::*; +mod num_preshards; +pub use num_preshards::*; pub mod uint; pub use tari_engine_types::serde_with; diff --git a/dan_layer/common_types/src/num_preshards.rs b/dan_layer/common_types/src/num_preshards.rs new file mode 100644 index 000000000..9d72a1ef1 --- /dev/null +++ b/dan_layer/common_types/src/num_preshards.rs @@ -0,0 +1,78 @@ +// Copyright 2024 The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +use std::{error::Error, fmt::Display}; + +use serde::{Deserialize, Serialize}; + +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] +#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] +pub enum NumPreshards { + One = 1, + Two = 2, + Four = 4, + Eight = 8, + Sixteen = 16, + ThirtyTwo = 32, + SixtyFour = 64, + OneTwentyEight = 128, + TwoFiftySix = 256, +} + +impl NumPreshards { + pub const MAX: Self = Self::TwoFiftySix; + + pub fn as_u32(self) -> u32 { + self as u32 + } + + pub fn is_one(self) -> bool { + self == Self::One + } +} + +impl TryFrom for NumPreshards { + type Error = InvalidNumPreshards; + + fn try_from(value: u32) -> Result { + match value { + 1 => Ok(Self::One), + 2 => Ok(Self::Two), + 4 => Ok(Self::Four), + 8 => Ok(Self::Eight), + 16 => Ok(Self::Sixteen), + 32 => Ok(Self::ThirtyTwo), + 64 => Ok(Self::SixtyFour), + 128 => Ok(Self::OneTwentyEight), + 256 => Ok(Self::TwoFiftySix), + _ => Err(InvalidNumPreshards(value)), + } + } +} + +impl From for u32 { + fn from(num_preshards: NumPreshards) -> u32 { + num_preshards.as_u32() + } +} + +impl Display for NumPreshards { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub struct InvalidNumPreshards(u32); + +impl Error for InvalidNumPreshards {} + +impl Display for InvalidNumPreshards { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} is not a valid number of pre-shards", self.0) + } +} diff --git a/dan_layer/common_types/src/shard.rs b/dan_layer/common_types/src/shard.rs index c085d4593..5e5752477 100644 --- a/dan_layer/common_types/src/shard.rs +++ b/dan_layer/common_types/src/shard.rs @@ -4,14 +4,16 @@ use std::{fmt::Display, ops::RangeInclusive}; use serde::{Deserialize, Serialize}; -#[cfg(feature = "ts")] -use ts_rs::TS; -use crate::{uint::U256, SubstateAddress}; +use crate::{uint::U256, NumPreshards, SubstateAddress}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct Shard(#[cfg_attr(feature = "ts", ts(type = "number"))] u32); impl Shard { @@ -19,73 +21,76 @@ impl Shard { Shard(0) } - pub fn as_u32(self) -> u32 { + pub const fn is_zero(&self) -> bool { + self.0 == 0 + } + + pub const fn as_u32(self) -> u32 { self.0 } - pub fn to_substate_address_range(self, num_shards: u32) -> RangeInclusive { - if num_shards <= 1 { + pub fn to_substate_address_range(self, num_shards: NumPreshards) -> RangeInclusive { + if num_shards.is_one() { return RangeInclusive::new(SubstateAddress::zero(), SubstateAddress::max()); } - // There will never be close to 2^31-1 committees but the calculation below will overflow/panic if - // num_shards.leading_zeros() == 0. - let num_shards = num_shards.min(crate::substate_address::MAX_NUM_SHARDS); + let num_shards = num_shards.as_u32(); let shard_u256 = U256::from(self.0); - if num_shards.is_power_of_two() { - let shard_size = U256::MAX >> num_shards.trailing_zeros(); - if self.0 == 0 { - return RangeInclusive::new(SubstateAddress::zero(), SubstateAddress::from_u256(shard_size)); - } - - // Add one to each start to account for remainder - let start = shard_u256 * shard_size; - - if self.0 == num_shards - 1 { - return RangeInclusive::new(SubstateAddress::from_u256(start + shard_u256), SubstateAddress::max()); - } - - let next_shard = shard_u256 + 1; - let end = next_shard * shard_size; - return RangeInclusive::new( - SubstateAddress::from_u256(start + shard_u256), - SubstateAddress::from_u256(end + shard_u256), - ); - } - - let num_shards_next_pow2 = num_shards.next_power_of_two(); - // Half the next power of two i.e. num_shards rounded down to previous power of two - let num_shards_prev_pow2 = num_shards_next_pow2 >> 1; - let num_shards_next_pow2 = U256::from(num_shards_next_pow2); - // Power of two division using bit shifts - let half_shard_size = U256::MAX >> num_shards_next_pow2.trailing_zeros(); - + // Power of two integer division using bit shifts + let shard_size = U256::MAX >> num_shards.trailing_zeros(); if self.0 == 0 { - return RangeInclusive::new(SubstateAddress::zero(), SubstateAddress::from_u256(half_shard_size)); + return RangeInclusive::new(SubstateAddress::zero(), SubstateAddress::from_u256(shard_size - 1)); } - // Calculate size of shard at previous power of two - let full_shard_size = U256::MAX >> num_shards_prev_pow2.trailing_zeros(); - // The "extra" half shards in the space - let num_half_shards = num_shards % num_shards_prev_pow2; - - let start = U256::from(self.0.min(num_half_shards * 2)) * half_shard_size + - U256::from(self.0.saturating_sub(num_half_shards * 2)) * full_shard_size; + // Add one to each start to account for remainder + let start = shard_u256 * shard_size; if self.0 == num_shards - 1 { - return RangeInclusive::new(SubstateAddress::from_u256(start + shard_u256), SubstateAddress::max()); + return RangeInclusive::new( + SubstateAddress::from_u256(start + shard_u256 - 1), + SubstateAddress::max(), + ); } - let next_shard = self.0 + 1; - let end = U256::from(next_shard.min(num_half_shards * 2)) * half_shard_size + - U256::from(next_shard.saturating_sub(num_half_shards * 2)) * full_shard_size; - + let end = start + shard_size; RangeInclusive::new( - SubstateAddress::from_u256(start + shard_u256), - SubstateAddress::from_u256(end + shard_u256), + SubstateAddress::from_u256(start + shard_u256 - 1), + SubstateAddress::from_u256(end + shard_u256 - 1), ) + + // let num_shards_next_pow2 = num_shards.next_power_of_two(); + // // Half the next power of two i.e. num_shards rounded down to previous power of two + // let num_shards_prev_pow2 = num_shards_next_pow2 >> 1; + // let num_shards_next_pow2 = U256::from(num_shards_next_pow2); + // // Power of two division using bit shifts + // let half_shard_size = U256::MAX >> num_shards_next_pow2.trailing_zeros(); + // + // if self.0 == 0 { + // return RangeInclusive::new(SubstateAddress::zero(), SubstateAddress::from_u256(half_shard_size)); + // } + // + // // Calculate size of shard at previous power of two + // let full_shard_size = U256::MAX >> num_shards_prev_pow2.trailing_zeros(); + // // The "extra" half shards in the space + // let num_half_shards = num_shards % num_shards_prev_pow2; + // + // let start = U256::from(self.0.min(num_half_shards * 2)) * half_shard_size + + // U256::from(self.0.saturating_sub(num_half_shards * 2)) * full_shard_size; + // + // if self.0 == num_shards - 1 { + // return RangeInclusive::new(SubstateAddress::from_u256(start + shard_u256), SubstateAddress::max()); + // } + // + // let next_shard = self.0 + 1; + // let end = U256::from(next_shard.min(num_half_shards * 2)) * half_shard_size + + // U256::from(next_shard.saturating_sub(num_half_shards * 2)) * full_shard_size; + // + // RangeInclusive::new( + // SubstateAddress::from_u256(start + shard_u256), + // SubstateAddress::from_u256(end + shard_u256), + // ) } } @@ -108,7 +113,7 @@ impl PartialEq for u32 { impl Display for Shard { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_u32()) + write!(f, "Shard({})", self.as_u32()) } } @@ -123,11 +128,13 @@ mod test { #[test] fn committee_is_properly_computed() { // TODO: clean this up a bit, I wrote this very hastily - let power_of_twos = iter::successors(Some(1), |x| Some(x * 2)).take(10); + let power_of_twos = iter::successors(Some(1u32), |x| Some(x * 2)) + .take(8) + .map(|v| NumPreshards::try_from(v).unwrap()); let mut split_map = IndexMap::<_, Vec<_>>::new(); for num_of_shards in power_of_twos { let mut last_end = U256::ZERO; - for shard_index in 0..num_of_shards { + for shard_index in 0..num_of_shards.as_u32() { let shard = Shard::from(shard_index); let range = shard.to_substate_address_range(num_of_shards); if shard_index == 0 { @@ -140,7 +147,7 @@ mod test { ); } last_end = range.end().to_u256(); - split_map.entry(num_of_shards).or_default().push(range); + split_map.entry(num_of_shards.as_u32()).or_default().push(range); } assert_eq!(last_end, U256::MAX, "Last shard should end at U256::MAX"); } @@ -185,6 +192,6 @@ mod test { } // Check that we didnt break early - assert_eq!(i, 9); + assert_eq!(i, 7); } } diff --git a/dan_layer/common_types/src/shard_group.rs b/dan_layer/common_types/src/shard_group.rs new file mode 100644 index 000000000..e5e1bbd4d --- /dev/null +++ b/dan_layer/common_types/src/shard_group.rs @@ -0,0 +1,149 @@ +// Copyright 2024 The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +use std::{ + fmt::{Display, Formatter}, + iter, + ops::RangeInclusive, +}; + +use serde::{Deserialize, Serialize}; + +use crate::{shard::Shard, uint::U256, NumPreshards, SubstateAddress}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] +pub struct ShardGroup { + start: Shard, + end_inclusive: Shard, +} + +impl ShardGroup { + pub fn new + Copy>(start: T, end_inclusive: T) -> Self { + let start = start.into(); + let end_inclusive = end_inclusive.into(); + assert!( + start <= end_inclusive, + "INVARIANT: start shard must be less than or equal to end_inclusive" + ); + Self { start, end_inclusive } + } + + pub fn all_shards(num_preshards: NumPreshards) -> Self { + Self::new(Shard::zero(), Shard::from(num_preshards.as_u32() - 1)) + } + + pub const fn len(&self) -> usize { + (self.end_inclusive.as_u32() + 1 - self.start.as_u32()) as usize + } + + pub const fn is_empty(&self) -> bool { + // Can never be empty because start <= end_inclusive (self.len() >= 1) + false + } + + /// Encodes the shard group as a u32. Little endian layout: (0)(0)(start)(end). + pub fn encode_as_u32(&self) -> u32 { + // ShardGroup fits into a u16 because even for max NumPreshards (NumPreshards::TwoFiftySix), the maximum + // possible shard is 255 (the first shard is 0) We therefore encode it into the last two (LS) + // bytes of the u32. The first two (MS) bytes of the u32 are always 0. + // + // A u32 is used because there is no reason not to, and it may give some wiggle room for potential future data + // to be encoded without any performance difference on most architectures. + let mut n = self.start.as_u32() << 8; + n |= self.end_inclusive.as_u32(); + n + } + + pub fn decode_from_u32(n: u32) -> Option { + if n > 0xFFFF { + return None; + } + + let start = n >> 8; + let end = n & 0xFF; + Some(Self::new(start, end)) + } + + pub fn shard_iter(&self) -> impl Iterator + '_ { + iter::successors(Some(self.start), move |&shard| { + if shard == self.end_inclusive { + None + } else { + Some(Shard::from(shard.as_u32() + 1)) + } + }) + } + + pub fn start(&self) -> Shard { + self.start + } + + pub fn end(&self) -> Shard { + self.end_inclusive + } + + pub fn contains(&self, shard: &Shard) -> bool { + self.as_range().contains(shard) + } + + pub fn as_range(&self) -> RangeInclusive { + self.start..=self.end_inclusive + } + + pub fn to_substate_address_range(self, num_shards: NumPreshards) -> RangeInclusive { + if num_shards.is_one() { + return SubstateAddress::zero()..=SubstateAddress::max(); + } + + let shard_size = U256::MAX >> num_shards.as_u32().trailing_zeros(); + let start = if self.start.is_zero() { + U256::ZERO + } else { + shard_size * U256::from(self.start.as_u32()) + U256::from(self.start.as_u32() - 1) + }; + if self.end_inclusive == num_shards.as_u32() - 1 { + return SubstateAddress::from_u256(start)..=SubstateAddress::max(); + } + + let end = + shard_size * U256::from(self.end_inclusive.as_u32()) + shard_size + U256::from(self.end_inclusive.as_u32()); + SubstateAddress::from_u256(start)..=SubstateAddress::from_u256(end - 1) + } +} + +impl Display for ShardGroup { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ShardGroup[{}, {}]", self.start, self.end_inclusive) + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn encode_decode() { + let sg = ShardGroup::new(123, 234); + let n = sg.encode_as_u32(); + let sg2 = ShardGroup::decode_from_u32(n).unwrap(); + assert_eq!(sg, sg2); + assert_eq!(ShardGroup::decode_from_u32(0), Some(ShardGroup::new(0, 0))); + assert_eq!(ShardGroup::decode_from_u32(0xFFFF), Some(ShardGroup::new(0xFF, 0xFF))); + assert_eq!(ShardGroup::decode_from_u32(0xFFFF + 1), None); + assert_eq!(ShardGroup::decode_from_u32(u32::MAX), None); + } + + #[test] + fn to_substate_address_range() { + let sg = ShardGroup::new(0, 63); + let range = sg.to_substate_address_range(NumPreshards::SixtyFour); + assert_eq!(*range.start(), SubstateAddress::zero()); + assert_eq!(*range.end(), SubstateAddress::max()); + } +} diff --git a/dan_layer/common_types/src/substate_address.rs b/dan_layer/common_types/src/substate_address.rs index 529a0b05f..c4b799c24 100644 --- a/dan_layer/common_types/src/substate_address.rs +++ b/dan_layer/common_types/src/substate_address.rs @@ -2,12 +2,10 @@ // SPDX-License-Identifier: BSD-3-Clause use std::{ - cmp, cmp::Ordering, fmt, fmt::{Display, Formatter}, mem, - ops::RangeInclusive, str::FromStr, }; @@ -22,12 +20,7 @@ use tari_engine_types::{ }; use tari_template_lib::{models::ObjectKey, Hash}; -use crate::{shard::Shard, uint::U256}; - -/// This is u16::MAX / 2 as a u32 = 32767 shards. Any number of shards greater than this will be clamped to this value. -/// This is done to limit the number of addresses that are added to the final shard to allow the same shard boundaries. -/// TODO: Change num_shards to a u16 -pub(super) const MAX_NUM_SHARDS: u32 = 0x0000_0000_0000_ffff >> 1; +use crate::{shard::Shard, uint::U256, NumPreshards, ShardGroup}; #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] #[cfg_attr( @@ -140,65 +133,90 @@ impl SubstateAddress { /// Calculates and returns the shard number that this SubstateAddress belongs. /// A shard is a division of the 256-bit shard space where the boundary of the division if always a power of two. - pub fn to_shard(&self, num_shards: u32) -> Shard { - if num_shards <= 1 || self.is_zero() { + pub fn to_shard(&self, num_shards: NumPreshards) -> Shard { + if num_shards.as_u32() == 1 || self.is_zero() { return Shard::from(0u32); } let addr_u256 = self.to_u256(); - if num_shards.is_power_of_two() { - let shard_size = U256::MAX >> num_shards.trailing_zeros(); - let mut shard = Shard::from( - u32::try_from(addr_u256 / shard_size) - .expect("to_shard: num_shards is a u32, so this cannot fail") - .min(num_shards - 1), - ); - // On boundary - if addr_u256 % shard_size == 0 { - shard = shard.as_u32().saturating_sub(1).into(); - } - return shard; + let num_shards = num_shards.as_u32(); + let shard_size = U256::MAX >> num_shards.trailing_zeros(); + Shard::from( + u32::try_from(addr_u256 / shard_size) + .expect("to_shard: num_shards is a u32, so this cannot fail") + .min(num_shards - 1), + ) + + // // 2^15-1 shards with 40 vns per shard = 1,310,680 validators. This limit exists to prevent next_power_of_two + // // from panicking. + // let num_shards = num_shards.min(MAX_NUM_SHARDS); + // + // // Round down to the next power of two. + // let next_shards_pow_two = num_shards.next_power_of_two(); + // let half_shard_size = U256::MAX >> next_shards_pow_two.trailing_zeros(); + // let mut next_pow_2_shard = + // u32::try_from(addr_u256 / half_shard_size).expect("to_shard: num_shards is a u32, so this cannot fail"); + // + // // On boundary + // if addr_u256 % half_shard_size == 0 { + // next_pow_2_shard = next_pow_2_shard.saturating_sub(1); + // } + // + // // Half the next power of two i.e. num_shards rounded down to previous power of two + // let num_shards_pow_two = next_shards_pow_two >> 1; + // // The "extra" half shards in the space + // let num_half_shards = num_shards % num_shards_pow_two; + // + // // Shard that we would be in if num_shards was a power of two + // let shard = next_pow_2_shard / 2; + // + // // If the shard is higher than all half shards, + // let shard = if shard >= num_half_shards { + // // then add those half shards in + // shard + num_half_shards + // } else { + // // otherwise, we use the shard number we'd be in if num_shards was the next power of two + // next_pow_2_shard + // }; + // + // // u256::MAX address needs to be clamped to the last shard index + // cmp::min(shard, num_shards - 1).into() + } + + pub fn to_shard_group(&self, num_shards: NumPreshards, num_committees: u32) -> ShardGroup { + // number of committees can never exceed number of shards + let num_committees = num_committees.min(num_shards.as_u32()); + if num_committees <= 1 { + return ShardGroup::new(Shard::zero(), Shard::from(num_shards.as_u32() - 1)); } - // 2^15-1 shards with 40 vns per shard = 1,310,680 validators. This limit exists to prevent next_power_of_two - // from panicking. - let num_shards = num_shards.min(MAX_NUM_SHARDS); + let shards_per_committee = num_shards.as_u32() / num_committees; + let mut shards_per_committee_rem = num_shards.as_u32() % num_committees; - // Round down to the next power of two. - let next_shards_pow_two = num_shards.next_power_of_two(); - let half_shard_size = U256::MAX >> next_shards_pow_two.trailing_zeros(); - let mut next_pow_2_shard = - u32::try_from(addr_u256 / half_shard_size).expect("to_shard: num_shards is a u32, so this cannot fail"); + let shard = self.to_shard(num_shards).as_u32(); - // On boundary - if addr_u256 % half_shard_size == 0 { - next_pow_2_shard = next_pow_2_shard.saturating_sub(1); + let mut start = 0u32; + let mut end = shards_per_committee; + if shards_per_committee_rem > 0 { + end += 1; } + loop { + if end > shard { + break; + } + start += shards_per_committee; + if shards_per_committee_rem > 0 { + start += 1; + shards_per_committee_rem -= 1; + } - // Half the next power of two i.e. num_shards rounded down to previous power of two - let num_shards_pow_two = next_shards_pow_two >> 1; - // The "extra" half shards in the space - let num_half_shards = num_shards % num_shards_pow_two; - - // Shard that we would be in if num_shards was a power of two - let shard = next_pow_2_shard / 2; - - // If the shard is higher than all half shards, - let shard = if shard >= num_half_shards { - // then add those half shards in - shard + num_half_shards - } else { - // otherwise, we use the shard number we'd be in if num_shards was the next power of two - next_pow_2_shard - }; - - // u256::MAX address needs to be clamped to the last shard index - cmp::min(shard, num_shards - 1).into() - } + end = start + shards_per_committee; + if shards_per_committee_rem > 0 { + end += 1; + } + } - pub fn to_address_range(&self, num_shards: u32) -> RangeInclusive { - let shard = self.to_shard(num_shards); - shard.to_substate_address_range(num_shards) + ShardGroup::new(start, end - 1) } } @@ -268,7 +286,7 @@ impl FromStr for SubstateAddress { mod tests { use std::{ iter, - ops::{Bound, RangeBounds}, + ops::{Bound, RangeBounds, RangeInclusive}, }; use rand::{rngs::OsRng, RngCore}; @@ -287,198 +305,133 @@ mod tests { #[test] fn to_committee_shard_and_shard_range_match() { let address = address_at(1, 8); - let shard = address.to_shard(6); + let shard = address.to_shard(NumPreshards::Eight); assert_eq!(shard, 1); - let range = Shard::from(0).to_substate_address_range(2); - assert_range(range, SubstateAddress::zero()..address_at(1, 2)); - let range = shard.to_substate_address_range(2); - assert_range(range, address_at(1, 2)..=SubstateAddress::max()); - let range = shard.to_substate_address_range(6); - assert_range(range, address_at(1, 8)..address_at(2, 8)); - } - - #[test] - fn shard_range() { - let range = SubstateAddress::zero().to_address_range(2); + let range = Shard::from(0).to_substate_address_range(NumPreshards::Two); assert_range(range, SubstateAddress::zero()..address_at(1, 2)); - let range = SubstateAddress::max().to_address_range(2); + let range = Shard::from(1).to_substate_address_range(NumPreshards::Two); assert_range(range, address_at(1, 2)..=SubstateAddress::max()); - // num_shards is a power of two - let power_of_twos = - iter::successors(Some(MAX_NUM_SHARDS.next_power_of_two() >> 1), |&x| Some(x >> 1)).take(15 - 2); - for power_of_two in power_of_twos { - for i in 0..power_of_two { - let range = address_at(i, power_of_two).to_address_range(power_of_two); - if i == 0 { - assert_range(range, SubstateAddress::zero()..address_at(1, power_of_two)); - } else if i >= power_of_two - 1 { - assert_range(range, address_at(i, power_of_two)..=SubstateAddress::max()); - } else { - assert_range(range, address_at(i, power_of_two)..address_at(i + 1, power_of_two)); - } - } + for n in 0..7 { + let range = Shard::from(n).to_substate_address_range(NumPreshards::Eight); + assert_range(range, address_at(n, 8)..address_at(n + 1, 8)); } - // Half shards - let range = address_at(0, 8).to_address_range(6); - assert_range(range, SubstateAddress::zero()..address_at(1, 8)); - let range = address_at(1, 8).to_address_range(6); - assert_range(range, address_at(1, 8)..address_at(2, 8)); - let range = address_at(2, 8).to_address_range(6); - assert_range(range, address_at(2, 8)..address_at(3, 8)); - let range = address_at(3, 8).to_address_range(6); - assert_range(range, address_at(3, 8)..address_at(4, 8)); - // Whole shards - let range = address_at(4, 8).to_address_range(6); - assert_range(range, address_at(4, 8)..address_at(6, 8)); - let range = address_at(5, 8).to_address_range(6); - assert_range(range, address_at(4, 8)..address_at(6, 8)); - let range = address_at(6, 8).to_address_range(6); - assert_range(range, address_at(6, 8)..=SubstateAddress::max()); - let range = address_at(7, 8).to_address_range(6); - assert_range(range, address_at(6, 8)..=SubstateAddress::max()); - let range = address_at(8, 8).to_address_range(6); - assert_range(range, address_at(6, 8)..=SubstateAddress::max()); - - let range = plus_one(address_at(1, 4)).to_address_range(20); - // The half shards will split at intervals of END_SHARD_MAX / 32 - assert_range(range, address_at(8, 32)..address_at(10, 32)); - - let range = plus_one(divide_floor(SubstateAddress::max(), 2)).to_address_range(10); - assert_range(range, address_at(8, 16)..address_at(10, 16)); - - let range = address_at(42, 256).to_address_range(256); - assert_range(range, address_at(42, 256)..address_at(43, 256)); - let range = address_at(128, 256).to_address_range(256); - assert_range(range, address_at(128, 256)..address_at(129, 256)); - } + let range = Shard::from(7).to_substate_address_range(NumPreshards::Eight); + assert_range(range, address_at(7, 8)..=address_at(8, 8)); + } + + // #[test] + // fn shard_range() { + // let range = SubstateAddress::zero().to_address_range(2); + // assert_range(range, SubstateAddress::zero()..address_at(1, 2)); + // let range = SubstateAddress::max().to_address_range(2); + // assert_range(range, address_at(1, 2)..=SubstateAddress::max()); + // + // // num_shards is a power of two + // let power_of_twos = + // iter::successors(Some(MAX_NUM_SHARDS.next_power_of_two() >> 1), |&x| Some(x >> 1)).take(15 - 2); + // for power_of_two in power_of_twos { + // for i in 0..power_of_two { + // let range = address_at(i, power_of_two).to_address_range(power_of_two); + // if i == 0 { + // assert_range(range, SubstateAddress::zero()..address_at(1, power_of_two)); + // } else if i >= power_of_two - 1 { + // assert_range(range, address_at(i, power_of_two)..=SubstateAddress::max()); + // } else { + // assert_range(range, address_at(i, power_of_two)..address_at(i + 1, power_of_two)); + // } + // } + // } + // + // // Half shards + // let range = address_at(0, 8).to_address_range(6); + // assert_range(range, SubstateAddress::zero()..address_at(1, 8)); + // let range = address_at(1, 8).to_address_range(6); + // assert_range(range, address_at(1, 8)..address_at(2, 8)); + // let range = address_at(2, 8).to_address_range(6); + // assert_range(range, address_at(2, 8)..address_at(3, 8)); + // let range = address_at(3, 8).to_address_range(6); + // assert_range(range, address_at(3, 8)..address_at(4, 8)); + // // Whole shards + // let range = address_at(4, 8).to_address_range(6); + // assert_range(range, address_at(4, 8)..address_at(6, 8)); + // let range = address_at(5, 8).to_address_range(6); + // assert_range(range, address_at(4, 8)..address_at(6, 8)); + // let range = address_at(6, 8).to_address_range(6); + // assert_range(range, address_at(6, 8)..=SubstateAddress::max()); + // let range = address_at(7, 8).to_address_range(6); + // assert_range(range, address_at(6, 8)..=SubstateAddress::max()); + // let range = address_at(8, 8).to_address_range(6); + // assert_range(range, address_at(6, 8)..=SubstateAddress::max()); + // + // let range = plus_one(address_at(1, 4)).to_address_range(20); + // // The half shards will split at intervals of END_SHARD_MAX / 32 + // assert_range(range, address_at(8, 32)..address_at(10, 32)); + // + // let range = plus_one(divide_floor(SubstateAddress::max(), 2)).to_address_range(10); + // assert_range(range, address_at(8, 16)..address_at(10, 16)); + // + // let range = address_at(42, 256).to_address_range(256); + // assert_range(range, address_at(42, 256)..address_at(43, 256)); + // let range = address_at(128, 256).to_address_range(256); + // assert_range(range, address_at(128, 256)..address_at(129, 256)); + // } #[test] fn to_shard() { - // Edge cases - let shard = SubstateAddress::max().to_shard(0); - assert_eq!(shard, 0); - let shard = SubstateAddress::zero().to_shard(0); + let shard = SubstateAddress::zero().to_shard(NumPreshards::Two); assert_eq!(shard, 0); - let shard = SubstateAddress::max().to_shard(u32::MAX); - assert_eq!(shard, u32::from(u16::MAX >> 1) - 1); - - let shard = SubstateAddress::zero().to_shard(2); - assert_eq!(shard, 0); - let shard = address_at(1, 2).to_shard(2); + let shard = address_at(1, 2).to_shard(NumPreshards::Two); assert_eq!(shard, 1); - let shard = plus_one(address_at(1, 2)).to_shard(2); + let shard = plus_one(address_at(1, 2)).to_shard(NumPreshards::Two); assert_eq!(shard, 1); - let shard = SubstateAddress::max().to_shard(2); + let shard = SubstateAddress::max().to_shard(NumPreshards::Two); assert_eq!(shard, 1); for i in 0..=32 { - let shard = divide_shard_space(i, 32).to_shard(1); + let shard = divide_shard_space(i, 32).to_shard(NumPreshards::One); assert_eq!(shard, 0); } // 2 shards, exactly half of the physical shard space for i in 0..=8 { - let shard = divide_shard_space(i, 16).to_shard(2); + let shard = divide_shard_space(i, 16).to_shard(NumPreshards::Two); assert_eq!(shard, 0, "{shard} is not 0 for i: {i}"); } for i in 9..16 { - let shard = divide_shard_space(i, 16).to_shard(2); + let shard = divide_shard_space(i, 16).to_shard(NumPreshards::Two); assert_eq!(shard, 1, "{shard} is not 1 for i: {i}"); } // If the number of shards is a power of two, then to_shard should always return the equally divided // shard number. We test this for the first u16::MAX power of twos. // At boundary - for power_of_two in iter::successors(Some(1), |&x| Some(x * 2)).take(16) { + for power_of_two in iter::successors(Some(1), |&x| Some(x * 2)).take(8) { for i in 1..power_of_two { - let shard = divide_shard_space(i, power_of_two).to_shard(power_of_two); - assert_eq!( - shard, - i - 1, - "Got: {shard}, Expected: {i} for power_of_two: {power_of_two}" - ); + let shard = divide_shard_space(i, power_of_two).to_shard(power_of_two.try_into().unwrap()); + assert_eq!(shard, i, "Got: {shard}, Expected: {i} for power_of_two: {power_of_two}"); } } // +1 boundary - for power_of_two in iter::successors(Some(1), |&x| Some(x * 2)).take(16) { + for power_of_two in iter::successors(Some(1), |&x| Some(x * 2)).take(8) { for i in 0..power_of_two { - let shard = plus_one(address_at(i, power_of_two)).to_shard(power_of_two); + let shard = plus_one(address_at(i, power_of_two)).to_shard(power_of_two.try_into().unwrap()); assert_eq!(shard, i, "Got: {shard}, Expected: {i} for power_of_two: {power_of_two}"); } } - let shard = address_at(0, 8).to_shard(6); - assert_eq!(shard, 0); - let shard = minus_one(address_at(1, 8)).to_shard(6); - assert_eq!(shard, 0); - let shard = address_at(1, 8).to_shard(6); - assert_eq!(shard, 1); - let shard = address_at(2, 8).to_shard(6); - assert_eq!(shard, 2); - let shard = plus_one(address_at(1, 8)).to_shard(6); - assert_eq!(shard, 1); - - let shard = plus_one(address_at(0, 8)).to_shard(6); - assert_eq!(shard, 0); - let shard = plus_one(address_at(1, 8)).to_shard(6); - assert_eq!(shard, 1); - let shard = plus_one(address_at(2, 8)).to_shard(6); - assert_eq!(shard, 2); - let shard = plus_one(address_at(3, 8)).to_shard(6); - assert_eq!(shard, 3); - let shard = plus_one(address_at(4, 8)).to_shard(6); - assert_eq!(shard, 4); - let shard = plus_one(address_at(5, 8)).to_shard(6); - assert_eq!(shard, 4); - let shard = plus_one(address_at(6, 8)).to_shard(6); - assert_eq!(shard, 5); - let shard = plus_one(address_at(7, 8)).to_shard(6); - assert_eq!(shard, 5); - let shard = minus_one(address_at(8, 8)).to_shard(6); - assert_eq!(shard, 5); - let shard = SubstateAddress::max().to_shard(6); - assert_eq!(shard, 5); - - let shard = plus_one(address_at(0, 8)).to_shard(5); - assert_eq!(shard, 0); - let shard = plus_one(address_at(1, 8)).to_shard(5); - assert_eq!(shard, 1); - let shard = plus_one(address_at(2, 8)).to_shard(5); - assert_eq!(shard, 2); - let shard = plus_one(address_at(3, 8)).to_shard(5); - assert_eq!(shard, 2); - let shard = plus_one(address_at(4, 8)).to_shard(5); - assert_eq!(shard, 3); - let shard = plus_one(address_at(5, 8)).to_shard(5); - assert_eq!(shard, 3); - let shard = plus_one(address_at(6, 8)).to_shard(5); - assert_eq!(shard, 4); - let shard = plus_one(address_at(7, 8)).to_shard(5); - assert_eq!(shard, 4); - let shard = minus_one(address_at(8, 8)).to_shard(5); - assert_eq!(shard, 4); - let shard = SubstateAddress::max().to_shard(5); - assert_eq!(shard, 4); - - let shard = plus_one(address_at(1, 4)).to_shard(20); - // 1/4 * 20 = 5 + 4 half shards in the start of the shard space, - 1 for index - assert_eq!(shard, 5 + 4 - 1); - let shard = divide_floor(SubstateAddress::max(), 2).to_shard(10); - // 8 / 2 = 4 + 2 half shards in the start of the shard space, +1 on boundary, - 1 for index - assert_eq!(shard, 4 + 2 + 1 - 1); - let shard = divide_floor(SubstateAddress::max(), 2).to_shard(256); + let shard = divide_floor(SubstateAddress::max(), 2).to_shard(NumPreshards::TwoFiftySix); assert_eq!(shard, 128); } #[test] fn max_committees() { - let shard = SubstateAddress::max().to_shard(MAX_NUM_SHARDS); + let shard = SubstateAddress::max().to_shard(NumPreshards::MAX); // When we have n committees, the last committee is n-1. - assert_eq!(shard, (u32::from(u16::MAX) >> 1) - 1); + assert_eq!(shard, NumPreshards::MAX.as_u32() - 1); } /// Returns the address of the floor division of the shard space @@ -497,7 +450,7 @@ mod tests { /// Returns the start address of the shard with given num_shards fn address_at(shard: u32, num_shards: u32) -> SubstateAddress { // + shard: For each shard we add 1 to account for remainder - add(divide_shard_space(shard, num_shards), shard) + add(divide_shard_space(shard, num_shards), shard.saturating_sub(1)) } fn divide_floor(shard: SubstateAddress, by: u32) -> SubstateAddress { @@ -512,8 +465,8 @@ mod tests { add(address, 1) } - fn add(shard: SubstateAddress, v: u32) -> SubstateAddress { - SubstateAddress::from_u256(shard.to_u256().saturating_add(U256::from(v))) + fn add(address: SubstateAddress, v: u32) -> SubstateAddress { + SubstateAddress::from_u256(address.to_u256().saturating_add(U256::from(v))) } fn assert_range>(range: RangeInclusive, expected: R) { @@ -544,4 +497,178 @@ mod tests { end, ); } + + mod to_shard_group { + use super::*; + + #[test] + fn it_returns_the_correct_shard_group() { + let group = SubstateAddress::zero().to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(1)); + + let group = plus_one(address_at(0, 4)).to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(1)); + + let group = address_at(1, 4).to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(1)); + + let group = address_at(2, 4).to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(2)..=Shard::from(3)); + + let group = address_at(3, 4).to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(2)..=Shard::from(3)); + + let group = SubstateAddress::max().to_shard_group(NumPreshards::Four, 2); + assert_eq!(group.as_range(), Shard::from(2)..=Shard::from(3)); + + let group = minus_one(address_at(1, 64)).to_shard_group(NumPreshards::SixtyFour, 16); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(3)); + let group = address_at(4, 64).to_shard_group(NumPreshards::SixtyFour, 16); + assert_eq!(group.as_range(), Shard::from(4)..=Shard::from(7)); + + let group = address_at(8, 64).to_shard_group(NumPreshards::SixtyFour, 2); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(31)); + let group = address_at(5, 8).to_shard_group(NumPreshards::SixtyFour, 2); + assert_eq!(group.as_range(), Shard::from(32)..=Shard::from(63)); + + // On boundary + let group = address_at(0, 8).to_shard_group(NumPreshards::SixtyFour, 2); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(31)); + let group = address_at(4, 8).to_shard_group(NumPreshards::SixtyFour, 2); + assert_eq!(group.as_range(), Shard::from(32)..=Shard::from(63)); + + let group = address_at(8, 8).to_shard_group(NumPreshards::SixtyFour, 2); + assert_eq!(group.as_range(), Shard::from(32)..=Shard::from(63)); + + let group = plus_one(address_at(3, 64)).to_shard_group(NumPreshards::SixtyFour, 32); + assert_eq!(group.as_range(), Shard::from(2)..=Shard::from(3)); + + let group = plus_one(address_at(3, 64)).to_shard_group(NumPreshards::SixtyFour, 32); + assert_eq!(group.as_range(), Shard::from(2)..=Shard::from(3)); + + let group = address_at(16, 64).to_shard_group(NumPreshards::SixtyFour, 32); + assert_eq!(group.as_range(), Shard::from(16)..=Shard::from(17)); + + let group = minus_one(address_at(1, 4)).to_shard_group(NumPreshards::SixtyFour, 64); + assert_eq!(group.as_range(), Shard::from(16)..=Shard::from(16)); + + let group = address_at(66, 256).to_shard_group(NumPreshards::SixtyFour, 16); + assert_eq!(group.as_range(), Shard::from(16)..=Shard::from(19)); + } + + #[test] + fn it_returns_the_correct_shard_group_generic() { + let all_num_shards_except_1 = [2, 4, 8, 16, 32, 64, 128, 256] + .into_iter() + .map(|n| NumPreshards::try_from(n).unwrap()); + + // Note: this test does not calculate the correct assertions if you change this constant. + const NUM_COMMITTEES: u32 = 2; + for num_shards in all_num_shards_except_1 { + for at in 0..num_shards.as_u32() { + let group = address_at(at, num_shards.as_u32()).to_shard_group(num_shards, NUM_COMMITTEES); + if at < num_shards.as_u32() / NUM_COMMITTEES { + assert_eq!( + group.as_range(), + Shard::from(0)..=Shard::from((num_shards.as_u32() / NUM_COMMITTEES) - 1), + "Failed at {at} for num_shards={num_shards}" + ); + } else { + assert_eq!( + group.as_range(), + Shard::from(num_shards.as_u32() / NUM_COMMITTEES)..=Shard::from(num_shards.as_u32() - 1), + "Failed at {at} for num_shards={num_shards}" + ); + } + } + } + } + + #[test] + fn it_returns_the_correct_shard_group_for_odd_num_committees() { + // All shard groups except the last have 3 shards each + + let group = address_at(0, 64).to_shard_group(NumPreshards::SixtyFour, 3); + // First shard group gets an extra shard to cover the remainder + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(21)); + assert_eq!(group.len(), 22); + let group = address_at(31, 64).to_shard_group(NumPreshards::SixtyFour, 3); + assert_eq!(group.as_range(), Shard::from(22)..=Shard::from(42)); + assert_eq!(group.len(), 21); + let group = address_at(50, 64).to_shard_group(NumPreshards::SixtyFour, 3); + assert_eq!(group.as_range(), Shard::from(43)..=Shard::from(63)); + assert_eq!(group.len(), 21); + + let group = address_at(3, 64).to_shard_group(NumPreshards::SixtyFour, 7); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(9)); + assert_eq!(group.len(), 10); + let group = address_at(11, 64).to_shard_group(NumPreshards::SixtyFour, 7); + assert_eq!(group.as_range(), Shard::from(10)..=Shard::from(18)); + assert_eq!(group.len(), 9); + let group = address_at(22, 64).to_shard_group(NumPreshards::SixtyFour, 7); + assert_eq!(group.as_range(), Shard::from(19)..=Shard::from(27)); + assert_eq!(group.len(), 9); + let group = address_at(60, 64).to_shard_group(NumPreshards::SixtyFour, 7); + assert_eq!(group.as_range(), Shard::from(55)..=Shard::from(63)); + assert_eq!(group.len(), 9); + let group = address_at(64, 64).to_shard_group(NumPreshards::SixtyFour, 7); + assert_eq!(group.as_range(), Shard::from(55)..=Shard::from(63)); + assert_eq!(group.len(), 9); + let group = SubstateAddress::zero().to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(2)); + + let group = address_at(1, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(2)); + + let group = address_at(1, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(0)..=Shard::from(2)); + + let group = address_at(3, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(3)..=Shard::from(5)); + + let group = address_at(4, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(3)..=Shard::from(5)); + + let group = address_at(5, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(3)..=Shard::from(5)); + // + let group = address_at(6, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(6)..=Shard::from(7)); + + let group = address_at(7, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(6)..=Shard::from(7)); + let group = address_at(8, 8).to_shard_group(NumPreshards::Eight, 3); + assert_eq!(group.as_range(), Shard::from(6)..=Shard::from(7)); + + // Committee = 5 + let group = address_at(4, 8).to_shard_group(NumPreshards::Eight, 5); + assert_eq!(group.as_range(), Shard::from(4)..=Shard::from(5)); + + let group = address_at(7, 8).to_shard_group(NumPreshards::Eight, 5); + assert_eq!(group.as_range(), Shard::from(7)..=Shard::from(7)); + + let group = address_at(8, 8).to_shard_group(NumPreshards::Eight, 5); + assert_eq!(group.as_range(), Shard::from(7)..=Shard::from(7)); + } + } + + mod shard_group_to_substate_address_range { + use super::*; + + #[test] + fn it_works() { + let range = ShardGroup::new(0, 9).to_substate_address_range(NumPreshards::Sixteen); + assert_range(range, SubstateAddress::zero()..address_at(10, 16)); + + let range = ShardGroup::new(1, 15).to_substate_address_range(NumPreshards::Sixteen); + // Last shard always includes SubstateAddress::max + assert_range(range, address_at(1, 16)..=address_at(16, 16)); + + let range = ShardGroup::new(1, 8).to_substate_address_range(NumPreshards::Sixteen); + assert_range(range, address_at(1, 16)..address_at(9, 16)); + + let range = ShardGroup::new(8, 15).to_substate_address_range(NumPreshards::Sixteen); + assert_range(range, address_at(8, 16)..=address_at(16, 16)); + } + } } diff --git a/dan_layer/consensus/src/block_validations.rs b/dan_layer/consensus/src/block_validations.rs index b315afa25..88ea5f1ac 100644 --- a/dan_layer/consensus/src/block_validations.rs +++ b/dan_layer/consensus/src/block_validations.rs @@ -13,17 +13,16 @@ use crate::{ pub async fn check_proposal( block: &Block, - network: Network, epoch_manager: &TConsensusSpec::EpochManager, vote_signing_service: &TConsensusSpec::SignatureService, leader_strategy: &TConsensusSpec::LeaderStrategy, - _config: &HotstuffConfig, + config: &HotstuffConfig, ) -> Result<(), HotStuffError> { // TODO: in order to do the base layer block has validation, we need to ensure that we have synced to the tip. // If not, we need some strategy for "parking" the blocks until we are at least at the provided hash or the // tip. Without this, the check has a race condition between the base layer scanner and consensus. // check_base_layer_block_hash::(block, epoch_manager, config).await?; - check_network(block, network)?; + check_network(block, config.network)?; check_hash_and_height(block)?; let committee_for_block = epoch_manager .get_committee_by_validator_public_key(block.epoch(), block.proposed_by()) @@ -182,15 +181,14 @@ pub async fn check_quorum_certificate( let vn = epoch_manager .get_validator_node_by_public_key(qc.epoch(), signature.public_key()) .await?; - let actual_shard = epoch_manager + let committee_info = epoch_manager .get_committee_info_for_substate(qc.epoch(), vn.shard_key) - .await? - .shard(); - if actual_shard != qc.shard() { + .await?; + if committee_info.shard_group() != qc.shard_group() { return Err(ProposalValidationError::ValidatorNotInCommittee { validator: signature.public_key().to_string(), - expected_shard: qc.shard().to_string(), - actual_shard: actual_shard.to_string(), + expected_shard: qc.shard_group().to_string(), + actual_shard: committee_info.shard_group().to_string(), } .into()); } diff --git a/dan_layer/consensus/src/hotstuff/block_change_set.rs b/dan_layer/consensus/src/hotstuff/block_change_set.rs index 650486c7d..fa7a55326 100644 --- a/dan_layer/consensus/src/hotstuff/block_change_set.rs +++ b/dan_layer/consensus/src/hotstuff/block_change_set.rs @@ -4,7 +4,7 @@ use std::ops::Deref; use indexmap::IndexMap; -use tari_dan_common_types::Epoch; +use tari_dan_common_types::{shard::Shard, Epoch}; use tari_dan_storage::{ consensus_models::{ Block, @@ -20,13 +20,13 @@ use tari_dan_storage::{ TransactionPoolRecord, TransactionPoolStage, TransactionPoolStatusUpdate, + VersionedStateHashTreeDiff, }, StateStoreReadTransaction, StateStoreWriteTransaction, StorageError, }; use tari_engine_types::substate::SubstateId; -use tari_state_tree::StateHashTreeDiff; use tari_transaction::TransactionId; #[derive(Debug, Clone)] @@ -42,7 +42,7 @@ pub struct ProposedBlockChangeSet { block: LeafBlock, quorum_decision: Option, block_diff: Vec, - state_tree_diff: StateHashTreeDiff, + state_tree_diffs: IndexMap, substate_locks: IndexMap>, transaction_changes: IndexMap, } @@ -55,7 +55,7 @@ impl ProposedBlockChangeSet { block_diff: Vec::new(), substate_locks: IndexMap::new(), transaction_changes: IndexMap::new(), - state_tree_diff: StateHashTreeDiff::default(), + state_tree_diffs: IndexMap::new(), } } @@ -63,11 +63,13 @@ impl ProposedBlockChangeSet { self.quorum_decision = None; self.block_diff = Vec::new(); self.transaction_changes.clear(); + self.state_tree_diffs.clear(); + self.substate_locks.clear(); self } - pub fn set_state_tree_diff(&mut self, diff: StateHashTreeDiff) -> &mut Self { - self.state_tree_diff = diff; + pub fn set_state_tree_diffs(&mut self, diffs: IndexMap) -> &mut Self { + self.state_tree_diffs = diffs; self } @@ -139,8 +141,10 @@ impl ProposedBlockChangeSet { // Store the block diff block_diff.insert(tx)?; - // Store the tree diff - PendingStateTreeDiff::new(*self.block.block_id(), self.block.height(), self.state_tree_diff).save(tx)?; + // Store the tree diffs for each effected shard + for (shard, diff) in self.state_tree_diffs { + PendingStateTreeDiff::create(tx, *self.block.block_id(), shard, diff)?; + } // Save locks SubstateRecord::insert_all_locks(tx, self.block.block_id, self.substate_locks)?; diff --git a/dan_layer/consensus/src/hotstuff/common.rs b/dan_layer/consensus/src/hotstuff/common.rs index d62dbee0d..a95b61411 100644 --- a/dan_layer/consensus/src/hotstuff/common.rs +++ b/dan_layer/consensus/src/hotstuff/common.rs @@ -1,25 +1,27 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::ops::ControlFlow; +use std::{collections::HashMap, ops::ControlFlow}; +use indexmap::IndexMap; use log::*; use tari_common::configuration::Network; use tari_common_types::types::FixedHash; -use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, NodeAddressable, NodeHeight}; -use tari_dan_storage::consensus_models::{Block, LeafBlock, PendingStateTreeDiff, QuorumCertificate}; -use tari_engine_types::substate::SubstateDiff; -use tari_state_tree::{ - Hash, - StagedTreeStore, - StateHashTreeDiff, - StateTreeError, - SubstateTreeChange, - TreeStoreReader, - Version, +use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, NodeAddressable, NodeHeight, ShardGroup}; +use tari_dan_storage::{ + consensus_models::{ + Block, + LeafBlock, + PendingStateTreeDiff, + QuorumCertificate, + SubstateChange, + VersionedStateHashTreeDiff, + }, + StateStoreReadTransaction, }; +use tari_state_tree::{Hash, StateTreeError}; -use crate::traits::LeaderStrategy; +use crate::{hotstuff::substate_store::ShardedStateTree, traits::LeaderStrategy}; const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::common"; @@ -32,7 +34,7 @@ pub const EXHAUST_DIVISOR: u64 = 20; // 5% pub fn calculate_last_dummy_block>( network: Network, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, high_qc: &QuorumCertificate, parent_merkle_root: FixedHash, new_height: NodeHeight, @@ -46,7 +48,7 @@ pub fn calculate_last_dummy_block( network: Network, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, high_qc: &QuorumCertificate, parent_merkle_root: FixedHash, new_height: NodeHeight, @@ -143,7 +145,7 @@ fn with_dummy_blocks( current_height, high_qc.clone(), epoch, - shard, + shard_group, parent_merkle_root, parent_timestamp, parent_base_layer_block_height, @@ -167,35 +169,22 @@ fn with_dummy_blocks( } } -pub fn diff_to_substate_changes(diff: &SubstateDiff) -> impl Iterator + '_ { - diff.down_iter() - .map(|(substate_id, _version)| SubstateTreeChange::Down { - id: substate_id.clone(), - }) - .chain(diff.up_iter().map(move |(substate_id, value)| SubstateTreeChange::Up { - id: substate_id.clone(), - value_hash: value.to_value_hash(), - })) -} - -pub fn calculate_state_merkle_diff, I: IntoIterator>( +pub fn calculate_state_merkle_root( tx: &TTx, - current_version: Version, - next_version: Version, - pending_tree_diffs: Vec, - substate_changes: I, -) -> Result<(Hash, StateHashTreeDiff), StateTreeError> { - debug!( - target: LOG_TARGET, - "Calculating state merkle diff from version {} to {} with {} pending diff(s)", - current_version, - next_version, - pending_tree_diffs.len(), - ); - let mut store = StagedTreeStore::new(tx); - store.apply_ordered_diffs(pending_tree_diffs.into_iter().map(|diff| diff.diff)); - let mut state_tree = tari_state_tree::SpreadPrefixStateTree::new(&mut store); - let state_root = - state_tree.put_substate_changes(Some(current_version).filter(|v| *v > 0), next_version, substate_changes)?; - Ok((state_root, store.into_diff())) + local_shard_group: ShardGroup, + pending_tree_diffs: HashMap>, + changes: &[SubstateChange], +) -> Result<(Hash, IndexMap), StateTreeError> { + let mut change_map = IndexMap::with_capacity(changes.len()); + + changes + .iter() + .filter(|ch| local_shard_group.contains(&ch.shard())) + .for_each(|ch| { + change_map.entry(ch.shard()).or_insert_with(Vec::new).push(ch.into()); + }); + + let mut sharded_tree = ShardedStateTree::new(tx).with_pending_diffs(pending_tree_diffs); + let state_root = sharded_tree.put_substate_tree_changes(change_map)?; + Ok((state_root, sharded_tree.into_versioned_tree_diffs())) } diff --git a/dan_layer/consensus/src/hotstuff/config.rs b/dan_layer/consensus/src/hotstuff/config.rs index 1b33991c8..b51e78d12 100644 --- a/dan_layer/consensus/src/hotstuff/config.rs +++ b/dan_layer/consensus/src/hotstuff/config.rs @@ -1,9 +1,16 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use std::time::Duration; + +use tari_common::configuration::Network; +use tari_dan_common_types::NumPreshards; + #[derive(Debug, Clone)] pub struct HotstuffConfig { + pub network: Network, pub max_base_layer_blocks_ahead: u64, pub max_base_layer_blocks_behind: u64, - pub pacemaker_max_base_time: std::time::Duration, + pub num_preshards: NumPreshards, + pub pacemaker_max_base_time: Duration, } diff --git a/dan_layer/consensus/src/hotstuff/error.rs b/dan_layer/consensus/src/hotstuff/error.rs index 38bcf848e..e560f93a1 100644 --- a/dan_layer/consensus/src/hotstuff/error.rs +++ b/dan_layer/consensus/src/hotstuff/error.rs @@ -10,6 +10,7 @@ use tari_dan_storage::{ use tari_epoch_manager::EpochManagerError; use tari_state_tree::StateTreeError; use tari_transaction::{TransactionId, VersionedSubstateIdError}; +use tokio::task::JoinError; use crate::{ hotstuff::substate_store::SubstateStoreError, @@ -22,6 +23,8 @@ pub enum HotStuffError { StorageError(#[from] StorageError), #[error("State tree error: {0}")] StateTreeError(#[from] StateTreeError), + #[error("Join error: {0}")] + JoinError(#[from] JoinError), #[error("Internal channel send error when {context}")] InternalChannelClosed { context: &'static str }, #[error("Inbound messaging error: {0}")] diff --git a/dan_layer/consensus/src/hotstuff/on_message_validate.rs b/dan_layer/consensus/src/hotstuff/on_message_validate.rs index 5c5346520..c0cb39c85 100644 --- a/dan_layer/consensus/src/hotstuff/on_message_validate.rs +++ b/dan_layer/consensus/src/hotstuff/on_message_validate.rs @@ -4,7 +4,6 @@ use std::collections::HashSet; use log::*; -use tari_common::configuration::Network; use tari_common_types::types::PublicKey; use tari_dan_common_types::{Epoch, NodeHeight}; use tari_dan_storage::{ @@ -28,7 +27,6 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::on_message_validate"; pub struct OnMessageValidate { local_validator_addr: TConsensusSpec::Addr, - network: Network, config: HotstuffConfig, store: TConsensusSpec::StateStore, epoch_manager: TConsensusSpec::EpochManager, @@ -44,7 +42,6 @@ pub struct OnMessageValidate { impl OnMessageValidate { pub fn new( local_validator_addr: TConsensusSpec::Addr, - network: Network, config: HotstuffConfig, store: TConsensusSpec::StateStore, epoch_manager: TConsensusSpec::EpochManager, @@ -55,7 +52,6 @@ impl OnMessageValidate { ) -> Self { Self { local_validator_addr, - network, config, store, epoch_manager, @@ -207,7 +203,6 @@ impl OnMessageValidate { async fn check_proposal(&self, block: &Block) -> Result<(), HotStuffError> { block_validations::check_proposal::( block, - self.network, &self.epoch_manager, &self.vote_signing_service, &self.leader_strategy, diff --git a/dan_layer/consensus/src/hotstuff/on_propose.rs b/dan_layer/consensus/src/hotstuff/on_propose.rs index 1ab71d0fa..4c890731e 100644 --- a/dan_layer/consensus/src/hotstuff/on_propose.rs +++ b/dan_layer/consensus/src/hotstuff/on_propose.rs @@ -8,7 +8,6 @@ use std::{ use indexmap::IndexMap; use log::*; -use tari_common::configuration::Network; use tari_common_types::types::{FixedHash, PublicKey}; use tari_crypto::tari_utilities::epoch_time::EpochTime; use tari_dan_common_types::{ @@ -46,9 +45,10 @@ use tari_transaction::TransactionId; use crate::{ hotstuff::{ - calculate_state_merkle_diff, + calculate_state_merkle_root, error::HotStuffError, - substate_store::{ChainScopedTreeStore, PendingSubstateStore}, + substate_store::PendingSubstateStore, + HotstuffConfig, EXHAUST_DIVISOR, }, messages::{HotstuffMessage, ProposalMessage}, @@ -64,7 +64,7 @@ use crate::{ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::on_local_propose"; pub struct OnPropose { - network: Network, + config: HotstuffConfig, store: TConsensusSpec::StateStore, epoch_manager: TConsensusSpec::EpochManager, transaction_pool: TransactionPool, @@ -77,7 +77,7 @@ impl OnPropose where TConsensusSpec: ConsensusSpec { pub fn new( - network: Network, + config: HotstuffConfig, store: TConsensusSpec::StateStore, epoch_manager: TConsensusSpec::EpochManager, transaction_pool: TransactionPool, @@ -86,7 +86,7 @@ where TConsensusSpec: ConsensusSpec outbound_messaging: TConsensusSpec::OutboundMessaging, ) -> Self { Self { - network, + config, store, epoch_manager, transaction_pool, @@ -175,7 +175,7 @@ where TConsensusSpec: ConsensusSpec // TODO: This is a hacky workaround, if the executed transaction has no shards after execution, we // remove it from the pool so that it does not get proposed again. Ideally we should be // able to catch this in transaction validation and propose ABORT. - if local_committee_info.count_distinct_shards(executed.involved_addresses_iter()) == 0 { + if local_committee_info.count_distinct_shard_groups(executed.involved_addresses_iter()) == 0 { self.transaction_pool.remove(tx, *executed.id())?; executed .set_abort("Transaction has no involved shards after execution") @@ -242,6 +242,7 @@ where TConsensusSpec: ConsensusSpec ) -> Result { let transaction = TransactionRecord::get(store.read_transaction(), transaction_id)?; + // TODO: check the failure cases for this. Some failures should not cause consensus to fail let executed = self .transaction_executor .execute(transaction.into_transaction(), store, current_epoch) @@ -276,10 +277,10 @@ where TConsensusSpec: ConsensusSpec executed_transactions.insert(*executed.id(), executed); } - let num_involved_shards = - local_committee_info.count_distinct_shards(tx_rec.evidence().substate_addresses_iter()); + let num_involved_shard_groups = + local_committee_info.count_distinct_shard_groups(tx_rec.evidence().substate_addresses_iter()); - if num_involved_shards == 0 { + if num_involved_shard_groups == 0 { warn!( target: LOG_TARGET, "Transaction {} has no involved shards, skipping...", @@ -291,7 +292,7 @@ where TConsensusSpec: ConsensusSpec // If the transaction is local only, propose LocalOnly. If the transaction is not new, it must have been // previously prepared in a multi-shard command (TBD if that a valid thing to do). - if num_involved_shards == 1 && !tx_rec.current_stage().is_new() { + if num_involved_shard_groups == 1 && !tx_rec.current_stage().is_new() { warn!( target: LOG_TARGET, "Transaction {} is local only but was not previously proposed as such. It is in stage {}", @@ -301,13 +302,13 @@ where TConsensusSpec: ConsensusSpec } // LOCAL-ONLY - if num_involved_shards == 1 && tx_rec.current_stage().is_new() { + if num_involved_shard_groups == 1 && tx_rec.current_stage().is_new() { info!( target: LOG_TARGET, "๐Ÿ ๏ธ Transaction {} is local only, proposing LocalOnly", tx_rec.transaction_id(), ); - let involved = NonZeroU64::new(num_involved_shards as u64).expect("involved is 1"); + let involved = NonZeroU64::new(num_involved_shard_groups as u64).expect("involved is 1"); let leader_fee = tx_rec.calculate_leader_fee(involved, EXHAUST_DIVISOR); let tx_atom = tx_rec.get_final_transaction_atom(leader_fee); if tx_atom.decision.is_commit() { @@ -397,7 +398,7 @@ where TConsensusSpec: ConsensusSpec // prepared. We can now propose to Accept it. We also propose the decision change which everyone // should agree with if they received the same foreign LocalPrepare. TransactionPoolStage::LocalPrepared => { - let involved = NonZeroU64::new(num_involved_shards as u64).ok_or_else(|| { + let involved = NonZeroU64::new(num_involved_shard_groups as u64).ok_or_else(|| { HotStuffError::InvariantError(format!( "Number of involved shards is zero for transaction {}", tx_rec.transaction_id(), @@ -461,7 +462,6 @@ where TConsensusSpec: ConsensusSpec } else { self.transaction_pool.get_batch_for_next_block(tx, TARGET_BLOCK_SIZE)? }; - let current_version = high_qc.block_height().as_u64(); let next_height = parent_block.height() + NodeHeight(1); let mut total_leader_fee = 0; @@ -477,7 +477,7 @@ where TConsensusSpec: ConsensusSpec foreign_proposal.base_layer_block_height <= base_layer_block_height && // If the foreign proposal is already pending, don't propose it again !pending_proposals.iter().any(|pending_proposal| { - pending_proposal.shard == foreign_proposal.shard && + pending_proposal.shard_group == foreign_proposal.shard_group && pending_proposal.block_id == foreign_proposal.block_id }) }) @@ -489,8 +489,7 @@ where TConsensusSpec: ConsensusSpec }; // batch is empty for is_empty, is_epoch_end and is_epoch_start blocks - let tree_store = ChainScopedTreeStore::new(epoch, local_committee_info.shard(), tx); - let mut substate_store = PendingSubstateStore::new(*parent_block.block_id(), tree_store); + let mut substate_store = PendingSubstateStore::new(tx, *parent_block.block_id(), self.config.num_preshards); let mut executed_transactions = HashMap::new(); for transaction in batch { if let Some(command) = self.transaction_pool_record_to_command( @@ -516,15 +515,13 @@ where TConsensusSpec: ConsensusSpec commands.iter().map(|c| c.to_string()).collect::>().join(",") ); - let pending_tree_diffs = - PendingStateTreeDiff::get_all_up_to_commit_block(tx, high_qc.epoch(), high_qc.shard(), high_qc.block_id())?; - let store = ChainScopedTreeStore::new(epoch, local_committee_info.shard(), tx); - let (state_root, _) = calculate_state_merkle_diff( - &store, - current_version, - next_height.as_u64(), + let pending_tree_diffs = PendingStateTreeDiff::get_all_up_to_commit_block(tx, high_qc.block_id())?; + + let (state_root, _) = calculate_state_merkle_root( + tx, + local_committee_info.shard_group(), pending_tree_diffs, - substate_store.diff().iter().map(|ch| ch.into()), + substate_store.diff(), )?; let non_local_shards = get_non_local_shards(substate_store.diff(), local_committee_info); @@ -539,12 +536,12 @@ where TConsensusSpec: ConsensusSpec foreign_indexes.sort_keys(); let mut next_block = Block::new( - self.network, + self.config.network, *parent_block.block_id(), high_qc, next_height, epoch, - local_committee_info.shard(), + local_committee_info.shard_group(), proposed_by, commands, state_root, @@ -568,8 +565,8 @@ pub fn get_non_local_shards(diff: &[SubstateChange], local_committee_info: &Comm .map(|ch| { ch.versioned_substate_id() .to_substate_address() - .to_shard(local_committee_info.num_committees()) + .to_shard(local_committee_info.num_shards()) }) - .filter(|shard| *shard != local_committee_info.shard()) + .filter(|shard| local_committee_info.shard_group().contains(shard)) .collect() } diff --git a/dan_layer/consensus/src/hotstuff/on_ready_to_vote_on_local_block.rs b/dan_layer/consensus/src/hotstuff/on_ready_to_vote_on_local_block.rs index 4898fad5e..3883cc483 100644 --- a/dan_layer/consensus/src/hotstuff/on_ready_to_vote_on_local_block.rs +++ b/dan_layer/consensus/src/hotstuff/on_ready_to_vote_on_local_block.rs @@ -13,7 +13,6 @@ use tari_dan_storage::{ BlockId, Command, Decision, - ForeignProposal, LastExecuted, LastVoted, LockedBlock, @@ -37,9 +36,11 @@ use tokio::sync::broadcast; use crate::{ hotstuff::{ block_change_set::{BlockDecision, ProposedBlockChangeSet}, + calculate_state_merkle_root, error::HotStuffError, event::HotstuffEvent, - substate_store::{ChainScopedTreeStore, PendingSubstateStore}, + substate_store::{PendingSubstateStore, ShardedStateTree}, + HotstuffConfig, ProposalValidationError, EXHAUST_DIVISOR, }, @@ -51,6 +52,7 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::on_ready_to_vote_on_lo #[derive(Debug, Clone)] pub struct OnReadyToVoteOnLocalBlock { local_validator_addr: TConsensusSpec::Addr, + config: HotstuffConfig, store: TConsensusSpec::StateStore, transaction_pool: TransactionPool, tx_events: broadcast::Sender, @@ -62,6 +64,7 @@ where TConsensusSpec: ConsensusSpec { pub fn new( validator_addr: TConsensusSpec::Addr, + config: HotstuffConfig, store: TConsensusSpec::StateStore, transaction_pool: TransactionPool, tx_events: broadcast::Sender, @@ -69,6 +72,7 @@ where TConsensusSpec: ConsensusSpec ) -> Self { Self { local_validator_addr: validator_addr, + config, store, transaction_pool, tx_events, @@ -191,8 +195,7 @@ where TConsensusSpec: ConsensusSpec // Store used for transactions that have inputs without specific versions. // It lives through the entire block so multiple transactions can be sequenced together in the same block - let tree_store = ChainScopedTreeStore::new(block.epoch(), block.shard(), tx); - let mut substate_store = PendingSubstateStore::new(*block.parent(), tree_store); + let mut substate_store = PendingSubstateStore::new(tx, *block.parent(), self.config.num_preshards); let mut proposed_block_change_set = ProposedBlockChangeSet::new(block.as_leaf_block()); if block.is_epoch_end() && block.commands().len() > 1 { @@ -206,11 +209,11 @@ where TConsensusSpec: ConsensusSpec for cmd in block.commands() { if let Some(foreign_proposal) = cmd.foreign_proposal() { - if !ForeignProposal::exists(tx, foreign_proposal)? { + if !foreign_proposal.exists(tx)? { warn!( target: LOG_TARGET, "โŒ Foreign proposal for block {block_id} from bucket {bucket} does not exist in the store", - block_id = foreign_proposal.block_id,bucket = foreign_proposal.shard + block_id = foreign_proposal.block_id,bucket = foreign_proposal.shard_group ); return Ok(proposed_block_change_set.no_vote()); } @@ -262,7 +265,7 @@ where TConsensusSpec: ConsensusSpec Command::LocalOnly(t) => { info!( target: LOG_TARGET, - "๐Ÿ‘จโ€๐Ÿ”ง LOCAL-ONLY: Executing deferred transaction {} in block {}", + "๐Ÿ‘จโ€๐Ÿ”ง LOCAL-ONLY: Executing transaction {} in block {}", tx_rec.transaction_id(), block, ); @@ -428,7 +431,7 @@ where TConsensusSpec: ConsensusSpec Command::Prepare(t) => { info!( target: LOG_TARGET, - "๐Ÿ‘จโ€๐Ÿ”ง PREPARE: Executing deferred transaction {} in block {}", + "๐Ÿ‘จโ€๐Ÿ”ง PREPARE: Executing transaction {} in block {}", tx_rec.transaction_id(), block, ); @@ -627,9 +630,9 @@ where TConsensusSpec: ConsensusSpec return Ok(proposed_block_change_set.no_vote()); } - let distinct_shards = - local_committee_info.count_distinct_shards(tx_rec.evidence().substate_addresses_iter()); - let distinct_shards = NonZeroU64::new(distinct_shards as u64).ok_or_else(|| { + let distinct_shard_groups = + local_committee_info.count_distinct_shard_groups(tx_rec.evidence().substate_addresses_iter()); + let distinct_shards = NonZeroU64::new(distinct_shard_groups as u64).ok_or_else(|| { HotStuffError::InvariantError(format!( "Distinct shards is zero for transaction {} in block {}", tx_rec.transaction_id(), @@ -715,11 +718,12 @@ where TConsensusSpec: ConsensusSpec block.total_leader_fee(), total_leader_fee ); - // TODO: investigate - // return Ok(proposed_block_change_set.no_vote()); + return Ok(proposed_block_change_set.no_vote()); } - let (expected_merkle_root, tree_diff) = substate_store.calculate_jmt_diff_for_block(block)?; + let pending = PendingStateTreeDiff::get_all_up_to_commit_block(tx, block.justify().block_id())?; + let (expected_merkle_root, tree_diffs) = + calculate_state_merkle_root(tx, block.shard_group(), pending, substate_store.diff())?; if expected_merkle_root != *block.merkle_root() { warn!( target: LOG_TARGET, @@ -734,7 +738,7 @@ where TConsensusSpec: ConsensusSpec let (diff, locks) = substate_store.into_parts(); proposed_block_change_set .set_block_diff(diff) - .set_state_tree_diff(tree_diff) + .set_state_tree_diffs(tree_diffs) .set_substate_locks(locks) .set_quorum_decision(QuorumDecision::Accept); @@ -874,6 +878,12 @@ where TConsensusSpec: ConsensusSpec "๐ŸŒณ Committing block {} with {} substate change(s)", block, diff.len() ); + // NOTE: this must happen before we commit the diff because the state transitions use this version + let pending = PendingStateTreeDiff::remove_by_block(tx, block.id())?; + let mut state_tree = ShardedStateTree::new(tx); + state_tree.commit_diffs(pending)?; + let tx = state_tree.into_transaction(); + let local_diff = diff.into_filtered(local_committee_info); block.commit_diff(tx, local_diff)?; @@ -893,11 +903,6 @@ where TConsensusSpec: ConsensusSpec // Remove locks for finalized transactions tx.substate_locks_remove_many_for_transactions(block.all_accepted_transactions_ids())?; - let pending = PendingStateTreeDiff::remove_by_block(tx, block.id())?; - let mut store = ChainScopedTreeStore::new(block.epoch(), block.shard(), tx); - let mut state_tree = tari_state_tree::SpreadPrefixStateTree::new(&mut store); - state_tree.commit_diff(pending.diff)?; - let total_transaction_fee = block.total_transaction_fee(); if total_transaction_fee > 0 { info!( diff --git a/dan_layer/consensus/src/hotstuff/on_receive_foreign_proposal.rs b/dan_layer/consensus/src/hotstuff/on_receive_foreign_proposal.rs index d9d14ebf8..f63936fb4 100644 --- a/dan_layer/consensus/src/hotstuff/on_receive_foreign_proposal.rs +++ b/dan_layer/consensus/src/hotstuff/on_receive_foreign_proposal.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: BSD-3-Clause use log::*; -use tari_dan_common_types::{committee::CommitteeInfo, optional::Optional, shard::Shard}; +use tari_dan_common_types::{committee::CommitteeInfo, optional::Optional, ShardGroup}; use tari_dan_storage::{ consensus_models::{ Block, @@ -54,7 +54,7 @@ where TConsensusSpec: ConsensusSpec info!( target: LOG_TARGET, - "๐Ÿ”ฅ Receive FOREIGN PROPOSAL for block {}, parent {}, height {} from {}", + "๐Ÿงฉ Receive FOREIGN PROPOSAL for block {}, parent {}, height {} from {}", block.id(), block.parent(), block.height(), @@ -66,17 +66,17 @@ where TConsensusSpec: ConsensusSpec .with_read_tx(|tx| ForeignReceiveCounters::get_or_default(tx))?; let vn = self.epoch_manager.get_validator_node(block.epoch(), &from).await?; - let committee_shard = self + let foreign_committee_info = self .epoch_manager .get_committee_info_for_substate(block.epoch(), vn.shard_key) .await?; - let local_shard = self.epoch_manager.get_local_committee_info(block.epoch()).await?; + let local_committee_info = self.epoch_manager.get_local_committee_info(block.epoch()).await?; if let Err(err) = self.validate_proposed_block( &from, &block, - committee_shard.shard(), - local_shard.shard(), + foreign_committee_info.shard_group(), + local_committee_info.shard_group(), &foreign_receive_counter, ) { warn!( @@ -89,14 +89,14 @@ where TConsensusSpec: ConsensusSpec return Ok(()); } - foreign_receive_counter.increment(&committee_shard.shard()); + foreign_receive_counter.increment_group(foreign_committee_info.shard_group()); let tx_ids = block .commands() .iter() .filter_map(|command| { if let Some(tx) = command.local_prepared() { - if !committee_shard.includes_any_shard(command.evidence().substate_addresses_iter()) { + if !foreign_committee_info.includes_any_shard(command.evidence().substate_addresses_iter()) { return None; } // We are interested in the commands that are for us, they will be in local prepared and one of the @@ -110,18 +110,16 @@ where TConsensusSpec: ConsensusSpec // The block height was validated earlier, so we can use the height only and not store the hash anymore let foreign_proposal = ForeignProposal::new( - committee_shard.shard(), + foreign_committee_info.shard_group(), *block.id(), tx_ids, block.base_layer_block_height(), ); - if self - .store - .with_read_tx(|tx| ForeignProposal::exists(tx, &foreign_proposal))? - { + + if self.store.with_read_tx(|tx| foreign_proposal.exists(tx))? { warn!( target: LOG_TARGET, - "๐Ÿ”ฅ FOREIGN PROPOSAL: Already received proposal for block {}", + "โŒ FOREIGN PROPOSAL: Already received proposal for block {}", block.id(), ); return Ok(()); @@ -130,7 +128,7 @@ where TConsensusSpec: ConsensusSpec self.store.with_write_tx(|tx| { foreign_receive_counter.save(tx)?; foreign_proposal.upsert(tx)?; - self.on_receive_foreign_block(tx, &block, &committee_shard) + self.on_receive_foreign_block(tx, &block, &foreign_committee_info, &local_committee_info) })?; // We could have ready transactions at this point, so if we're the leader for the next block we can propose @@ -144,16 +142,28 @@ where TConsensusSpec: ConsensusSpec tx: &mut ::WriteTransaction<'_>, block: &Block, foreign_committee_info: &CommitteeInfo, + local_committee_info: &CommitteeInfo, ) -> Result<(), HotStuffError> { let leaf = LeafBlock::get(&**tx)?; // We only want to save the QC once if applicable let mut is_qc_saved = false; + let mut command_count = 0usize; for cmd in block.commands() { let Some(t) = cmd.local_prepared() else { continue; }; + + if !local_committee_info.includes_any_shard(t.evidence.substate_addresses_iter()) { + continue; + } let Some(mut tx_rec) = self.transaction_pool.get(tx, leaf, &t.id).optional()? else { + // TODO: request the transaction + warn!( + target: LOG_TARGET, + "โš ๏ธ Foreign proposal received for shard applicable transaction {} but this transaction is unknown. TODO: request it.", + t.id + ); continue; }; @@ -166,6 +176,8 @@ where TConsensusSpec: ConsensusSpec continue; } + command_count += 1; + let remote_decision = t.decision; let local_decision = tx_rec.current_local_decision(); if remote_decision.is_abort() && local_decision.is_commit() { @@ -190,7 +202,7 @@ where TConsensusSpec: ConsensusSpec if tx_rec.current_stage().is_local_prepared() && tx_rec.evidence().all_shards_justified() { info!( target: LOG_TARGET, - "๐Ÿ”ฅ FOREIGN PROPOSAL: Transaction is ready for propose ACCEPT({}, {}) Local Stage: {}", + "๐Ÿงฉ FOREIGN PROPOSAL: Transaction is ready for propose ACCEPT({}, {}) Local Stage: {}", tx_rec.transaction_id(), tx_rec.current_decision(), tx_rec.current_stage() @@ -200,6 +212,20 @@ where TConsensusSpec: ConsensusSpec } } + info!( + target: LOG_TARGET, + "๐Ÿงฉ FOREIGN PROPOSAL: Processed {} commands from foreign block {}", + command_count, + block.id() + ); + if command_count == 0 { + warn!( + target: LOG_TARGET, + "โš ๏ธ FOREIGN PROPOSAL: No commands were applicable for foreign block {}. Ignoring.", + block.id() + ); + } + Ok(()) } @@ -207,8 +233,8 @@ where TConsensusSpec: ConsensusSpec &self, from: &TConsensusSpec::Addr, candidate_block: &Block, - _foreign_shard: Shard, - _local_shard: Shard, + _foreign_shard: ShardGroup, + _local_shard: ShardGroup, _foreign_receive_counter: &ForeignReceiveCounters, ) -> Result<(), ProposalValidationError> { // TODO: ignoring for now because this is currently broken diff --git a/dan_layer/consensus/src/hotstuff/on_receive_local_proposal.rs b/dan_layer/consensus/src/hotstuff/on_receive_local_proposal.rs index d6342cd03..1585f6c6e 100644 --- a/dan_layer/consensus/src/hotstuff/on_receive_local_proposal.rs +++ b/dan_layer/consensus/src/hotstuff/on_receive_local_proposal.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: BSD-3-Clause use log::*; -use tari_common::configuration::Network; use tari_dan_common_types::{ committee::{Committee, CommitteeInfo}, optional::Optional, @@ -33,6 +32,7 @@ use crate::{ error::HotStuffError, on_ready_to_vote_on_local_block::OnReadyToVoteOnLocalBlock, pacemaker_handle::PaceMakerHandle, + HotstuffConfig, HotstuffEvent, ProposalValidationError, }, @@ -44,7 +44,7 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::on_receive_local_propo pub struct OnReceiveLocalProposalHandler { local_validator_addr: TConsensusSpec::Addr, - network: Network, + config: HotstuffConfig, store: TConsensusSpec::StateStore, epoch_manager: TConsensusSpec::EpochManager, leader_strategy: TConsensusSpec::LeaderStrategy, @@ -71,12 +71,12 @@ impl OnReceiveLocalProposalHandler, proposer: Proposer, transaction_executor: TConsensusSpec::TransactionExecutor, - network: Network, + config: HotstuffConfig, hooks: TConsensusSpec::Hooks, ) -> Self { Self { local_validator_addr: local_validator_addr.clone(), - network, + config: config.clone(), store: store.clone(), epoch_manager, leader_strategy, @@ -88,6 +88,7 @@ impl OnReceiveLocalProposalHandler OnReceiveLocalProposalHandler((decision, valid_block)) }) - .await - .unwrap()?; + .await??; self.hooks .on_local_block_decide(&valid_block, block_decision.quorum_decision); @@ -208,12 +208,13 @@ impl OnReceiveLocalProposalHandler OnReceiveLocalProposalHandler OnSyncRequest { let blocks = Block::get_all_blocks_between( tx, leaf_block.epoch(), - leaf_block.shard(), + leaf_block.shard_group(), msg.high_qc.block_id(), leaf_block.id(), true, diff --git a/dan_layer/consensus/src/hotstuff/proposer.rs b/dan_layer/consensus/src/hotstuff/proposer.rs index 552f3d1f0..8e309381c 100644 --- a/dan_layer/consensus/src/hotstuff/proposer.rs +++ b/dan_layer/consensus/src/hotstuff/proposer.rs @@ -7,7 +7,7 @@ use log::{debug, info}; use tari_dan_storage::consensus_models::Block; use tari_epoch_manager::EpochManagerReader; -use super::HotStuffError; +use super::{HotStuffError, HotstuffConfig}; use crate::{ messages::{HotstuffMessage, ProposalMessage}, traits::{ConsensusSpec, OutboundMessaging}, @@ -15,6 +15,7 @@ use crate::{ #[derive(Clone)] pub struct Proposer { + config: HotstuffConfig, epoch_manager: TConsensusSpec::EpochManager, outbound_messaging: TConsensusSpec::OutboundMessaging, } @@ -25,10 +26,12 @@ impl Proposer where TConsensusSpec: ConsensusSpec { pub fn new( + config: HotstuffConfig, epoch_manager: TConsensusSpec::EpochManager, outbound_messaging: TConsensusSpec::OutboundMessaging, ) -> Self { Self { + config, epoch_manager, outbound_messaging, } @@ -38,23 +41,25 @@ where TConsensusSpec: ConsensusSpec let num_committees = self.epoch_manager.get_num_committees(block.epoch()).await?; let validator = self.epoch_manager.get_our_validator_node(block.epoch()).await?; - let local_shard = validator.shard_key.to_shard(num_committees); - let non_local_shards = block + let local_shard_group = validator + .shard_key + .to_shard_group(self.config.num_preshards, num_committees); + let non_local_shard_groups = block .commands() .iter() .filter_map(|c| c.local_prepared()) .flat_map(|p| p.evidence.substate_addresses_iter()) - .map(|addr| addr.to_shard(num_committees)) - .filter(|shard| *shard != local_shard) + .map(|addr| addr.to_shard_group(self.config.num_preshards, num_committees)) + .filter(|shard_group| local_shard_group != *shard_group) .collect::>(); - if non_local_shards.is_empty() { + if non_local_shard_groups.is_empty() { return Ok(()); } info!( target: LOG_TARGET, - "๐ŸŒฟ PROPOSING new locked block {} to {} foreign shards. justify: {} ({}), parent: {}", + "๐ŸŒฟ PROPOSING new locked block {} to {} foreign shard groups. justify: {} ({}), parent: {}", block, - non_local_shards.len(), + non_local_shard_groups.len(), block.justify().block_id(), block.justify().block_height(), block.parent() @@ -62,25 +67,28 @@ where TConsensusSpec: ConsensusSpec debug!( target: LOG_TARGET, "non_local_shards : [{}]", - non_local_shards.iter().map(|s|s.to_string()).collect::>().join(","), + non_local_shard_groups.iter().map(|s|s.to_string()).collect::>().join(","), ); - let non_local_committees = self - .epoch_manager - .get_committees_by_shards(block.epoch(), non_local_shards) - .await?; + + let mut addresses = HashSet::new(); + // TODO(perf): fetch only applicable committee addresses + let mut committees = self.epoch_manager.get_committees(block.epoch()).await?; + for shard_group in non_local_shard_groups { + addresses.extend( + committees + .remove(&shard_group) + .into_iter() + .flat_map(|c| c.into_iter().map(|(addr, _)| addr)), + ); + } info!( target: LOG_TARGET, "๐ŸŒฟ FOREIGN PROPOSE: Broadcasting locked block {} to {} foreign committees.", block, - non_local_committees.len(), + addresses.len(), ); self.outbound_messaging - .multicast( - non_local_committees - .values() - .flat_map(|c| c.iter().map(|(addr, _)| addr)), - HotstuffMessage::ForeignProposal(ProposalMessage { block }), - ) + .multicast(&addresses, HotstuffMessage::ForeignProposal(ProposalMessage { block })) .await?; Ok(()) } diff --git a/dan_layer/consensus/src/hotstuff/substate_store/mod.rs b/dan_layer/consensus/src/hotstuff/substate_store/mod.rs index 6db8650a1..b3831edd3 100644 --- a/dan_layer/consensus/src/hotstuff/substate_store/mod.rs +++ b/dan_layer/consensus/src/hotstuff/substate_store/mod.rs @@ -1,10 +1,12 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -mod chain_scoped_tree_store; mod error; mod pending_store; +mod sharded_state_tree; +mod sharded_store; -pub use chain_scoped_tree_store::*; pub use error::*; pub use pending_store::*; +pub use sharded_state_tree::*; +pub use sharded_store::*; diff --git a/dan_layer/consensus/src/hotstuff/substate_store/pending_store.rs b/dan_layer/consensus/src/hotstuff/substate_store/pending_store.rs index 0044fdc1c..5d93696a6 100644 --- a/dan_layer/consensus/src/hotstuff/substate_store/pending_store.rs +++ b/dan_layer/consensus/src/hotstuff/substate_store/pending_store.rs @@ -5,15 +5,12 @@ use std::{borrow::Cow, collections::HashMap}; use indexmap::IndexMap; use log::*; -use tari_common_types::types::FixedHash; -use tari_dan_common_types::{optional::Optional, SubstateAddress}; +use tari_dan_common_types::{optional::Optional, NumPreshards, SubstateAddress}; use tari_dan_storage::{ consensus_models::{ - Block, BlockDiff, BlockId, LockedSubstate, - PendingStateTreeDiff, SubstateChange, SubstateLockFlag, SubstateRecord, @@ -22,41 +19,39 @@ use tari_dan_storage::{ StateStore, StateStoreReadTransaction, }; -use tari_engine_types::substate::{hash_substate, Substate, SubstateId}; -use tari_state_tree::{StateHashTreeDiff, SubstateTreeChange}; +use tari_engine_types::substate::{Substate, SubstateDiff, SubstateId}; use tari_transaction::{TransactionId, VersionedSubstateId}; use super::error::SubstateStoreError; -use crate::{ - hotstuff::{calculate_state_merkle_diff, substate_store::chain_scoped_tree_store::ChainScopedTreeStore}, - traits::{ReadableSubstateStore, WriteableSubstateStore}, -}; +use crate::traits::{ReadableSubstateStore, WriteableSubstateStore}; const LOG_TARGET: &str = "tari::dan::hotstuff::substate_store::pending_store"; pub struct PendingSubstateStore<'a, 'tx, TStore: StateStore + 'a + 'tx> { - store: ChainScopedTreeStore<&'a TStore::ReadTransaction<'tx>>, + store: &'a TStore::ReadTransaction<'tx>, /// Map from substate address to the index in the diff list pending: HashMap, /// Append only list of changes ordered oldest to newest diff: Vec, new_locks: IndexMap>, parent_block: BlockId, + num_preshards: NumPreshards, } impl<'a, 'tx, TStore: StateStore + 'a> PendingSubstateStore<'a, 'tx, TStore> { - pub fn new(parent_block: BlockId, store: ChainScopedTreeStore<&'a TStore::ReadTransaction<'tx>>) -> Self { + pub fn new(store: &'a TStore::ReadTransaction<'tx>, parent_block: BlockId, num_preshards: NumPreshards) -> Self { Self { store, pending: HashMap::new(), diff: Vec::new(), new_locks: IndexMap::new(), parent_block, + num_preshards, } } pub fn read_transaction(&self) -> &'a TStore::ReadTransaction<'tx> { - self.store.transaction() + self.store } } @@ -106,6 +101,31 @@ impl<'a, 'tx, TStore: StateStore + 'a + 'tx> WriteableSubstateStore for PendingS Ok(()) } + + fn put_diff(&mut self, transaction_id: TransactionId, diff: &SubstateDiff) -> Result<(), Self::Error> { + for (id, version) in diff.down_iter() { + let id = VersionedSubstateId::new(id.clone(), *version); + let shard = id.to_substate_address().to_shard(self.num_preshards); + self.put(SubstateChange::Down { + id, + shard, + transaction_id, + })?; + } + + for (id, substate) in diff.up_iter() { + let id = VersionedSubstateId::new(id.clone(), substate.version()); + let shard = id.to_substate_address().to_shard(self.num_preshards); + self.put(SubstateChange::Up { + id, + shard, + substate: substate.clone(), + transaction_id, + })?; + } + + Ok(()) + } } impl<'a, 'tx, TStore: StateStore + 'a + 'tx> PendingSubstateStore<'a, 'tx, TStore> { @@ -131,35 +151,6 @@ impl<'a, 'tx, TStore: StateStore + 'a + 'tx> PendingSubstateStore<'a, 'tx, TStor Ok(substate.into_substate()) } - pub fn calculate_jmt_diff_for_block( - &mut self, - block: &Block, - ) -> Result<(FixedHash, StateHashTreeDiff), SubstateStoreError> { - let current_version = block.justify().block_height().as_u64(); - let next_version = block.height().as_u64(); - - let pending = PendingStateTreeDiff::get_all_up_to_commit_block( - self.read_transaction(), - block.epoch(), - block.shard(), - block.justify().block_id(), - )?; - - let changes = self.diff.iter().map(|ch| match ch { - SubstateChange::Up { id, substate, .. } => SubstateTreeChange::Up { - id: id.substate_id.clone(), - value_hash: hash_substate(substate.substate_value(), substate.version()), - }, - SubstateChange::Down { id, .. } => SubstateTreeChange::Down { - id: id.substate_id.clone(), - }, - }); - let (state_root, state_tree_diff) = - calculate_state_merkle_diff(&self.store, current_version, next_version, pending, changes)?; - - Ok((state_root, state_tree_diff)) - } - pub fn try_lock_all>( &mut self, transaction_id: TransactionId, @@ -291,6 +282,7 @@ impl<'a, 'tx, TStore: StateStore + 'a + 'tx> PendingSubstateStore<'a, 'tx, TStor // - it MUST NOT be locked as READ, WRITE or OUTPUT, unless // - if Same-Transaction OR Local-Only-Rules: // - it MAY be locked as WRITE or READ + // - it MUST NOT be locked as OUTPUT SubstateLockFlag::Output => { if !same_transaction && !has_local_only_rules { warn!( diff --git a/dan_layer/consensus/src/hotstuff/substate_store/sharded_state_tree.rs b/dan_layer/consensus/src/hotstuff/substate_store/sharded_state_tree.rs new file mode 100644 index 000000000..6f23872f3 --- /dev/null +++ b/dan_layer/consensus/src/hotstuff/substate_store/sharded_state_tree.rs @@ -0,0 +1,158 @@ +// Copyright 2024 The Tari Project +// SPDX-License-Identifier: BSD-3-Clause + +use std::collections::HashMap; + +use indexmap::IndexMap; +use log::debug; +use tari_dan_common_types::{hashing::state_root_hasher, shard::Shard}; +use tari_dan_storage::{ + consensus_models::{PendingStateTreeDiff, VersionedStateHashTreeDiff}, + StateStoreReadTransaction, + StateStoreWriteTransaction, +}; +use tari_state_tree::{ + Hash, + JmtStorageError, + SpreadPrefixStateTree, + StagedTreeStore, + StateHashTreeDiff, + StateTreeError, + SubstateTreeChange, + TreeStoreWriter, + Version, +}; + +use crate::hotstuff::substate_store::sharded_store::{ShardScopedTreeStoreReader, ShardScopedTreeStoreWriter}; + +const LOG_TARGET: &str = "tari::dan::consensus::sharded_state_tree"; + +pub struct ShardedStateTree { + tx: TTx, + pending_diffs: HashMap>, + sharded_tree_diffs: IndexMap, +} + +impl ShardedStateTree { + pub fn new(tx: TTx) -> Self { + Self { + tx, + pending_diffs: HashMap::new(), + sharded_tree_diffs: IndexMap::new(), + } + } + + pub fn with_pending_diffs(self, pending_diffs: HashMap>) -> Self { + Self { pending_diffs, ..self } + } + + pub fn transaction(&self) -> &TTx { + &self.tx + } + + pub fn into_transaction(self) -> TTx { + self.tx + } +} + +impl ShardedStateTree<&TTx> { + fn get_current_version(&self, shard: Shard) -> Result, StateTreeError> { + if let Some(version) = self + .pending_diffs + .get(&shard) + .and_then(|diffs| diffs.last()) + .map(|diff| diff.version) + { + return Ok(Some(version)); + } + + let maybe_version = self + .tx + .state_tree_versions_get_latest(shard) + .map_err(|e| StateTreeError::StorageError(JmtStorageError::UnexpectedError(e.to_string())))?; + Ok(maybe_version) + } + + pub fn into_versioned_tree_diffs(self) -> IndexMap { + self.sharded_tree_diffs + } + + pub fn put_substate_tree_changes( + &mut self, + changes: IndexMap>, + ) -> Result { + // This is here so that the state merkle root is all zeros for no changes (instead of being + // state_root_hasher().result()). + if changes.is_empty() { + return Ok(Hash::zero()); + } + + let mut state_roots = state_root_hasher(); + for (shard, changes) in changes { + let current_version = self.get_current_version(shard)?; + let next_version = current_version.unwrap_or(0) + 1; + + // Read only state store that is scoped to the shard + let scoped_store = ShardScopedTreeStoreReader::new(self.tx, shard); + // Staged store that tracks changes to the state tree + let mut store = StagedTreeStore::new(&scoped_store); + // Apply pending (not yet committed) diffs to the staged store + if let Some(diffs) = self.pending_diffs.remove(&shard) { + debug!(target: LOG_TARGET, "Applying {num_diffs} pending diff(s) to shard {shard} (version={version})", num_diffs = diffs.len(), version = diffs.last().map(|d| d.version).unwrap_or(0)); + for diff in diffs { + store.apply_pending_diff(diff.diff); + } + } + + // Apply state updates to the state tree that is backed by the staged shard-scoped store + let mut state_tree = SpreadPrefixStateTree::new(&mut store); + debug!(target: LOG_TARGET, "v{next_version} contains {} tree change(s) for shard {shard}", changes.len()); + let state_root = state_tree.put_substate_changes(current_version, next_version, changes)?; + state_roots.update(&state_root); + self.sharded_tree_diffs + .insert(shard, VersionedStateHashTreeDiff::new(next_version, store.into_diff())); + } + + // TODO: use a Merkle tree to generate a root for these hashes + Ok(state_roots.result()) + } +} + +impl ShardedStateTree<&mut TTx> { + pub fn commit_diffs(&mut self, diffs: IndexMap>) -> Result<(), StateTreeError> { + for (shard, pending_diffs) in diffs { + for pending_diff in pending_diffs { + let version = pending_diff.version; + let diff = pending_diff.diff; + self.commit_diff(shard, version, diff)?; + } + } + + Ok(()) + } + + pub fn commit_diff( + &mut self, + shard: Shard, + version: Version, + diff: StateHashTreeDiff, + ) -> Result<(), StateTreeError> { + let mut store = ShardScopedTreeStoreWriter::new(self.tx, shard); + + for stale_tree_node in diff.stale_tree_nodes { + debug!( + "(shard={shard}) Recording stale tree node: {}", + stale_tree_node.as_node_key() + ); + store.record_stale_tree_node(stale_tree_node)?; + } + + for (key, node) in diff.new_nodes { + debug!("(shard={shard}) Inserting node: {}", key); + store.insert_node(key, node)?; + } + + store.set_version(version)?; + Ok(()) + } +} diff --git a/dan_layer/consensus/src/hotstuff/substate_store/chain_scoped_tree_store.rs b/dan_layer/consensus/src/hotstuff/substate_store/sharded_store.rs similarity index 55% rename from dan_layer/consensus/src/hotstuff/substate_store/chain_scoped_tree_store.rs rename to dan_layer/consensus/src/hotstuff/substate_store/sharded_store.rs index 428965160..2fa8a54c0 100644 --- a/dan_layer/consensus/src/hotstuff/substate_store/chain_scoped_tree_store.rs +++ b/dan_layer/consensus/src/hotstuff/substate_store/sharded_store.rs @@ -3,64 +3,79 @@ use std::ops::Deref; -use tari_dan_common_types::{optional::Optional, shard::Shard, Epoch}; +use tari_dan_common_types::{optional::Optional, shard::Shard}; use tari_dan_storage::{StateStoreReadTransaction, StateStoreWriteTransaction}; use tari_state_tree::{Node, NodeKey, StaleTreeNode, TreeStoreReader, TreeStoreWriter, Version}; -/// Tree store that is scoped to a specific chain (epoch and shard) +/// Tree store that is scoped to a specific shard #[derive(Debug)] -pub struct ChainScopedTreeStore { - epoch: Epoch, +pub struct ShardScopedTreeStoreReader<'a, TTx> { shard: Shard, - tx: TTx, + tx: &'a TTx, } -impl ChainScopedTreeStore { - pub fn new(epoch: Epoch, shard: Shard, tx: TTx) -> Self { - Self { epoch, shard, tx } +impl<'a, TTx> ShardScopedTreeStoreReader<'a, TTx> { + pub fn new(tx: &'a TTx, shard: Shard) -> Self { + Self { shard, tx } } } -impl ChainScopedTreeStore { - pub fn transaction(&self) -> TTx { - self.tx.clone() - } -} - -impl<'a, TTx: StateStoreReadTransaction> TreeStoreReader for ChainScopedTreeStore<&'a TTx> { +impl<'a, TTx: StateStoreReadTransaction> TreeStoreReader for ShardScopedTreeStoreReader<'a, TTx> { fn get_node(&self, key: &NodeKey) -> Result, tari_state_tree::JmtStorageError> { self.tx - .state_tree_nodes_get(self.epoch, self.shard, key) + .state_tree_nodes_get(self.shard, key) .optional() .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))? .ok_or_else(|| tari_state_tree::JmtStorageError::NotFound(key.clone())) } } -impl<'a, TTx> TreeStoreReader for ChainScopedTreeStore<&'a mut TTx> +#[derive(Debug)] +pub struct ShardScopedTreeStoreWriter<'a, TTx> { + shard: Shard, + tx: &'a mut TTx, +} + +impl<'a, TTx: StateStoreWriteTransaction> ShardScopedTreeStoreWriter<'a, TTx> { + pub fn new(tx: &'a mut TTx, shard: Shard) -> Self { + Self { shard, tx } + } + + pub fn set_version(&mut self, version: Version) -> Result<(), tari_state_tree::JmtStorageError> { + self.tx + .state_tree_shard_versions_set(self.shard, version) + .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string())) + } + + pub fn transaction(&mut self) -> &mut TTx { + self.tx + } +} + +impl<'a, TTx> TreeStoreReader for ShardScopedTreeStoreWriter<'a, TTx> where TTx: StateStoreWriteTransaction + Deref, TTx::Target: StateStoreReadTransaction, { fn get_node(&self, key: &NodeKey) -> Result, tari_state_tree::JmtStorageError> { self.tx - .state_tree_nodes_get(self.epoch, self.shard, key) + .state_tree_nodes_get(self.shard, key) .optional() .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))? .ok_or_else(|| tari_state_tree::JmtStorageError::NotFound(key.clone())) } } -impl<'a, TTx: StateStoreWriteTransaction> TreeStoreWriter for ChainScopedTreeStore<&'a mut TTx> { +impl<'a, TTx: StateStoreWriteTransaction> TreeStoreWriter for ShardScopedTreeStoreWriter<'a, TTx> { fn insert_node(&mut self, key: NodeKey, node: Node) -> Result<(), tari_state_tree::JmtStorageError> { self.tx - .state_tree_nodes_insert(self.epoch, self.shard, key, node) + .state_tree_nodes_insert(self.shard, key, node) .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string())) } fn record_stale_tree_node(&mut self, node: StaleTreeNode) -> Result<(), tari_state_tree::JmtStorageError> { self.tx - .state_tree_nodes_mark_stale_tree_node(self.epoch, self.shard, node) + .state_tree_nodes_mark_stale_tree_node(self.shard, node) .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string())) } } diff --git a/dan_layer/consensus/src/hotstuff/vote_receiver.rs b/dan_layer/consensus/src/hotstuff/vote_receiver.rs index c89c3b474..32c46cb25 100644 --- a/dan_layer/consensus/src/hotstuff/vote_receiver.rs +++ b/dan_layer/consensus/src/hotstuff/vote_receiver.rs @@ -294,7 +294,7 @@ fn create_qc(vote_data: VoteData) -> QuorumCertificate { *block.id(), block.height(), block.epoch(), - block.shard(), + block.shard_group(), signatures, leaf_hashes, quorum_decision, diff --git a/dan_layer/consensus/src/hotstuff/worker.rs b/dan_layer/consensus/src/hotstuff/worker.rs index 1d23f9df8..6b53a3956 100644 --- a/dan_layer/consensus/src/hotstuff/worker.rs +++ b/dan_layer/consensus/src/hotstuff/worker.rs @@ -4,8 +4,7 @@ use std::fmt::{Debug, Formatter}; use log::*; -use tari_common::configuration::Network; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{Epoch, NodeHeight, ShardGroup}; use tari_dan_storage::{ consensus_models::{Block, BlockDiff, HighQc, LeafBlock, TransactionPool}, StateStore, @@ -48,7 +47,7 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::worker"; pub struct HotstuffWorker { local_validator_addr: TConsensusSpec::Addr, - network: Network, + config: HotstuffConfig, hooks: TConsensusSpec::Hooks, tx_events: broadcast::Sender, @@ -80,8 +79,8 @@ pub struct HotstuffWorker { impl HotstuffWorker { #[allow(clippy::too_many_arguments)] pub fn new( + config: HotstuffConfig, validator_addr: TConsensusSpec::Addr, - network: Network, inbound_messaging: TConsensusSpec::InboundMessaging, outbound_messaging: TConsensusSpec::OutboundMessaging, rx_new_transactions: mpsc::Receiver<(Transaction, usize)>, @@ -94,22 +93,22 @@ impl HotstuffWorker { tx_events: broadcast::Sender, hooks: TConsensusSpec::Hooks, shutdown: ShutdownSignal, - config: HotstuffConfig, ) -> Self { let (tx_missing_transactions, rx_missing_transactions) = mpsc::unbounded_channel(); let pacemaker = PaceMaker::new(config.pacemaker_max_base_time); let vote_receiver = VoteReceiver::new( - network, + config.network, state_store.clone(), leader_strategy.clone(), epoch_manager.clone(), signing_service.clone(), pacemaker.clone_handle(), ); - let proposer = Proposer::::new(epoch_manager.clone(), outbound_messaging.clone()); + let proposer = + Proposer::::new(config.clone(), epoch_manager.clone(), outbound_messaging.clone()); Self { local_validator_addr: validator_addr.clone(), - network, + config: config.clone(), tx_events: tx_events.clone(), rx_new_transactions, rx_missing_transactions, @@ -117,8 +116,7 @@ impl HotstuffWorker { on_inbound_message: OnInboundMessage::new(inbound_messaging, hooks.clone()), on_message_validate: OnMessageValidate::new( validator_addr.clone(), - network, - config, + config.clone(), state_store.clone(), epoch_manager.clone(), leader_strategy.clone(), @@ -145,7 +143,7 @@ impl HotstuffWorker { tx_events, proposer.clone(), transaction_executor.clone(), - network, + config.clone(), hooks.clone(), ), on_receive_foreign_proposal: OnReceiveForeignProposalHandler::new( @@ -156,7 +154,7 @@ impl HotstuffWorker { ), on_receive_vote: OnReceiveVoteHandler::new(vote_receiver.clone()), on_receive_new_view: OnReceiveNewViewHandler::new( - network, + config.network, state_store.clone(), leader_strategy.clone(), epoch_manager.clone(), @@ -174,7 +172,7 @@ impl HotstuffWorker { tx_missing_transactions, ), on_propose: OnPropose::new( - network, + config, state_store.clone(), epoch_manager.clone(), transaction_pool.clone(), @@ -206,7 +204,7 @@ impl HotstuffWorker { let current_epoch = self.epoch_manager.current_epoch().await?; let committee_info = self.epoch_manager.get_local_committee_info(current_epoch).await?; - self.create_zero_block_if_required(current_epoch, committee_info.shard())?; + self.create_zero_block_if_required(current_epoch, committee_info.shard_group())?; // Resume pacemaker from the last epoch/height let (current_epoch, current_height, high_qc) = self.state_store.with_read_tx(|tx| { @@ -674,10 +672,10 @@ impl HotstuffWorker { } } - fn create_zero_block_if_required(&self, epoch: Epoch, shard: Shard) -> Result<(), HotStuffError> { + fn create_zero_block_if_required(&self, epoch: Epoch, shard_group: ShardGroup) -> Result<(), HotStuffError> { self.state_store.with_write_tx(|tx| { // The parent for genesis blocks refer to this zero block - let zero_block = Block::zero_block(self.network); + let zero_block = Block::zero_block(self.config.network, self.config.num_preshards); if !zero_block.exists(&**tx)? { debug!(target: LOG_TARGET, "Creating zero block"); zero_block.justify().insert(tx)?; @@ -690,10 +688,10 @@ impl HotstuffWorker { zero_block.commit_diff(tx, BlockDiff::empty(*zero_block.id()))?; } - let genesis = Block::genesis(self.network, epoch, shard); + let genesis = Block::genesis(self.config.network, epoch, shard_group); if !genesis.exists(&**tx)? { info!(target: LOG_TARGET, "โœจCreating genesis block {genesis}"); - // No genesis.justify() insert because that is the zero block justify + genesis.justify().insert(tx)?; genesis.insert(tx)?; genesis.as_locked_block().set(tx)?; genesis.as_leaf_block().set(tx)?; diff --git a/dan_layer/consensus/src/traits/substate_store.rs b/dan_layer/consensus/src/traits/substate_store.rs index 32209017c..d58e609e6 100644 --- a/dan_layer/consensus/src/traits/substate_store.rs +++ b/dan_layer/consensus/src/traits/substate_store.rs @@ -18,24 +18,7 @@ pub trait ReadableSubstateStore { pub trait WriteableSubstateStore: ReadableSubstateStore { fn put(&mut self, change: SubstateChange) -> Result<(), Self::Error>; - fn put_diff(&mut self, transaction_id: TransactionId, diff: &SubstateDiff) -> Result<(), Self::Error> { - for (id, version) in diff.down_iter() { - self.put(SubstateChange::Down { - id: VersionedSubstateId::new(id.clone(), *version), - transaction_id, - })?; - } - - for (id, substate) in diff.up_iter() { - self.put(SubstateChange::Up { - id: VersionedSubstateId::new(id.clone(), substate.version()), - substate: substate.clone(), - transaction_id, - })?; - } - - Ok(()) - } + fn put_diff(&mut self, transaction_id: TransactionId, diff: &SubstateDiff) -> Result<(), Self::Error>; } pub trait SubstateStore: ReadableSubstateStore + WriteableSubstateStore {} diff --git a/dan_layer/consensus_tests/src/consensus.rs b/dan_layer/consensus_tests/src/consensus.rs index 957f4e86b..03e166e0e 100644 --- a/dan_layer/consensus_tests/src/consensus.rs +++ b/dan_layer/consensus_tests/src/consensus.rs @@ -12,7 +12,7 @@ use std::time::Duration; use tari_common_types::types::PrivateKey; use tari_consensus::hotstuff::HotStuffError; -use tari_dan_common_types::{optional::Optional, shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{optional::Optional, Epoch, NodeHeight}; use tari_dan_storage::{ consensus_models::{BlockId, Command, Decision, TransactionRecord, VersionedSubstateIdLockIntent}, StateStore, @@ -60,7 +60,7 @@ async fn single_transaction() { test.get_validator(&TestAddress::new("1")) .state_store .with_read_tx(|tx| { - let mut block = tx.blocks_get_tip(Epoch(1), Shard::from(0))?; + let mut block = tx.blocks_get_tip(Epoch(1), test.get_validator(&TestAddress::new("1")).shard_group)?; loop { block = block.get_parent(tx)?; if block.id().is_zero() { @@ -281,6 +281,7 @@ async fn multi_shard_propose_blocks_with_new_transactions_until_all_committed() async fn foreign_shard_decides_to_abort() { setup_logger(); let mut test = Test::builder() + // TODO: this timeout is required because there is a bug causing an unnecessary wait before proposing (see TransactionPool::has_uncommitted_transactions) .with_test_timeout(Duration::from_secs(60)) .add_committee(0, vec!["1", "3", "4"]) .add_committee(1, vec!["2", "5", "6"]) @@ -288,12 +289,12 @@ async fn foreign_shard_decides_to_abort() { .await; let tx1 = build_transaction(Decision::Commit, 1, 5, 2); - test.send_transaction_to_destination(TestNetworkDestination::Shard(0), tx1.clone()) + test.send_transaction_to_destination(TestNetworkDestination::Committee(0), tx1.clone()) .await; let tx2 = change_decision(tx1.clone().try_into().unwrap(), Decision::Abort); assert_eq!(tx1.id(), tx2.id()); assert!(tx2.current_decision().is_abort()); - test.send_transaction_to_destination(TestNetworkDestination::Shard(1), tx2.clone()) + test.send_transaction_to_destination(TestNetworkDestination::Committee(1), tx2.clone()) .await; test.start_epoch(Epoch(1)).await; @@ -382,11 +383,7 @@ async fn output_conflict_abort() { #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn single_shard_inputs_from_previous_outputs() { setup_logger(); - let mut test = Test::builder() - .debug_sql("/tmp/test{}.db") - .add_committee(0, vec!["1", "2"]) - .start() - .await; + let mut test = Test::builder().add_committee(0, vec!["1", "2"]).start().await; let tx1 = build_transaction(Decision::Commit, 1, 5, 2); let resulting_outputs = tx1.resulting_outputs().to_vec(); @@ -448,6 +445,7 @@ async fn single_shard_inputs_from_previous_outputs() { async fn multishard_inputs_from_previous_outputs() { setup_logger(); let mut test = Test::builder() + // TODO: investigate why there is a delay in multishard transactions .with_test_timeout(Duration::from_secs(60)) .add_committee(0, vec!["1", "2"]) .add_committee(1, vec!["3", "4"]) @@ -585,11 +583,7 @@ async fn single_shard_input_conflict() { #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn epoch_change() { setup_logger(); - let mut test = Test::builder() - .with_test_timeout(Duration::from_secs(60)) - .add_committee(0, vec!["1", "2"]) - .start() - .await; + let mut test = Test::builder().add_committee(0, vec!["1", "2"]).start().await; test.start_epoch(Epoch(1)).await; let mut remaining_txs = 10; @@ -603,7 +597,7 @@ async fn epoch_change() { test.start_epoch(Epoch(2)).await; } - if remaining_txs == 0 && test.is_transaction_pool_empty() { + if remaining_txs <= 0 && test.is_transaction_pool_empty() { break; } @@ -622,7 +616,7 @@ async fn epoch_change() { test.get_validator(&TestAddress::new("1")) .state_store .with_read_tx(|tx| { - let mut block = tx.blocks_get_tip(Epoch(1), Shard::from(0))?; + let mut block = tx.blocks_get_tip(Epoch(1), test.get_validator(&TestAddress::new("1")).shard_group)?; loop { block = block.get_parent(tx)?; if block.id().is_zero() { @@ -638,7 +632,7 @@ async fn epoch_change() { .unwrap(); test.assert_all_validators_at_same_height().await; - test.assert_all_validators_committed(); + // test.assert_all_validators_committed(); test.assert_clean_shutdown().await; log::info!("total messages sent: {}", test.network().total_messages_sent()); @@ -756,7 +750,7 @@ async fn single_shard_unversioned_inputs() { let mut test = Test::builder().add_committee(0, vec!["1", "2"]).start().await; // First get transaction in the mempool let inputs = test.create_substates_on_all_vns(1); - // Remove versions from inputs to allow deferred transactions + // Remove versions from inputs to test substate version resolution let unversioned_inputs = inputs .iter() .map(|i| SubstateRequirement::new(i.substate_id.clone(), None)); @@ -801,7 +795,7 @@ async fn single_shard_unversioned_inputs() { test.get_validator(&TestAddress::new("1")) .state_store .with_read_tx(|tx| { - let mut block = Some(tx.blocks_get_tip(Epoch(1), Shard::from(0))?); + let mut block = Some(tx.blocks_get_tip(Epoch(1), test.get_validator(&TestAddress::new("1")).shard_group)?); loop { block = block.as_ref().unwrap().get_parent(tx).optional()?; let Some(b) = block.as_ref() else { diff --git a/dan_layer/consensus_tests/src/substate_store.rs b/dan_layer/consensus_tests/src/substate_store.rs index 84deccf98..e45e44d5e 100644 --- a/dan_layer/consensus_tests/src/substate_store.rs +++ b/dan_layer/consensus_tests/src/substate_store.rs @@ -2,10 +2,10 @@ // SPDX-License-Identifier: BSD-3-Clause use tari_consensus::{ - hotstuff::substate_store::{ChainScopedTreeStore, PendingSubstateStore, SubstateStoreError}, + hotstuff::substate_store::{PendingSubstateStore, SubstateStoreError}, traits::{ReadableSubstateStore, WriteableSubstateStore}, }; -use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, PeerAddress}; +use tari_dan_common_types::{shard::Shard, NodeAddressable, PeerAddress}; use tari_dan_storage::{ consensus_models::{ BlockId, @@ -25,7 +25,7 @@ use tari_state_store_sqlite::SqliteStateStore; use tari_template_lib::models::{ComponentAddress, EntityId, ObjectKey}; use tari_transaction::VersionedSubstateId; -use crate::support::logging::setup_logger; +use crate::support::{logging::setup_logger, TEST_NUM_PRESHARDS}; type TestStore = SqliteStateStore; @@ -42,6 +42,7 @@ fn it_allows_substate_up_for_v0() { store .put(SubstateChange::Up { id: VersionedSubstateId::new(id.clone(), 1), + shard: Shard::zero(), transaction_id: tx_id(0), substate: Substate::new(1, value.clone()), }) @@ -51,6 +52,7 @@ fn it_allows_substate_up_for_v0() { .put(SubstateChange::Up { id: VersionedSubstateId::new(id.clone(), 0), transaction_id: tx_id(0), + shard: Shard::zero(), substate: Substate::new(0, value), }) .unwrap(); @@ -75,6 +77,7 @@ fn it_allows_down_then_up() { store .put(SubstateChange::Down { id: id.clone(), + shard: Shard::zero(), transaction_id: Default::default(), }) .unwrap(); @@ -82,6 +85,7 @@ fn it_allows_down_then_up() { store .put(SubstateChange::Up { id: id.to_next_version(), + shard: Shard::zero(), transaction_id: Default::default(), substate: new_substate(1, 1), }) @@ -104,6 +108,7 @@ fn it_fails_if_previous_version_is_not_down() { let err = store .put(SubstateChange::Up { id: id.to_next_version(), + shard: Shard::zero(), transaction_id: Default::default(), substate: new_substate(1, 1), }) @@ -223,8 +228,7 @@ fn create_store() -> TestStore { fn create_pending_store<'a, 'tx, TAddr: NodeAddressable>( tx: &'a as StateStore>::ReadTransaction<'tx>, ) -> PendingSubstateStore<'a, 'tx, SqliteStateStore> { - let tree_store = ChainScopedTreeStore::new(Epoch::zero(), Shard::zero(), tx); - PendingSubstateStore::new(BlockId::zero(), tree_store) + PendingSubstateStore::new(tx, BlockId::zero(), TEST_NUM_PRESHARDS) } fn new_substate_id(seed: u8) -> SubstateId { diff --git a/dan_layer/consensus_tests/src/support/epoch_manager.rs b/dan_layer/consensus_tests/src/support/epoch_manager.rs index d09273cfd..0a976b5eb 100644 --- a/dan_layer/consensus_tests/src/support/epoch_manager.rs +++ b/dan_layer/consensus_tests/src/support/epoch_manager.rs @@ -1,10 +1,7 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; +use std::{collections::HashMap, sync::Arc}; use async_trait::async_trait; use tari_common_types::types::{FixedHash, PublicKey}; @@ -12,13 +9,14 @@ use tari_dan_common_types::{ committee::{Committee, CommitteeInfo}, shard::Shard, Epoch, + ShardGroup, SubstateAddress, }; use tari_dan_storage::global::models::ValidatorNode; use tari_epoch_manager::{EpochManagerError, EpochManagerEvent, EpochManagerReader}; use tokio::sync::{broadcast, Mutex, MutexGuard}; -use crate::support::{address::TestAddress, helpers::random_substate_in_shard}; +use crate::support::{address::TestAddress, helpers::random_substate_in_shard_group, TEST_NUM_PRESHARDS}; #[derive(Debug, Clone)] pub struct TestEpochManager { @@ -82,16 +80,15 @@ impl TestEpochManager { copy } - pub async fn add_committees(&self, committees: HashMap>) { + pub async fn add_committees(&self, committees: HashMap>) { let mut state = self.state_lock().await; - let num_committees = committees.len() as u32; - for (shard, committee) in committees { + for (shard_group, committee) in committees { for (address, pk) in &committee.members { - let substate_address = random_substate_in_shard(shard, num_committees); + let substate_address = random_substate_in_shard_group(shard_group, TEST_NUM_PRESHARDS); state.validator_shards.insert( address.clone(), ( - shard, + shard_group, substate_address.to_substate_address(), pk.clone(), None, @@ -100,14 +97,16 @@ impl TestEpochManager { Epoch(1), ), ); - state.address_shard.insert(address.clone(), shard); + state.address_shard.insert(address.clone(), shard_group); } - state.committees.insert(shard, committee); + state.committees.insert(shard_group, committee); } } - pub async fn all_validators(&self) -> Vec<(TestAddress, Shard, SubstateAddress, PublicKey, u64, Epoch, Epoch)> { + pub async fn all_validators( + &self, + ) -> Vec<(TestAddress, ShardGroup, SubstateAddress, PublicKey, u64, Epoch, Epoch)> { self.state_lock() .await .validator_shards @@ -132,7 +131,7 @@ impl TestEpochManager { .collect() } - pub async fn all_committees(&self) -> HashMap> { + pub async fn all_committees(&self) -> HashMap> { self.state_lock().await.committees.clone() } } @@ -151,8 +150,8 @@ impl EpochManagerReader for TestEpochManager { substate_address: SubstateAddress, ) -> Result, EpochManagerError> { let state = self.state_lock().await; - let shard = substate_address.to_shard(state.committees.len() as u32); - Ok(state.committees[&shard].clone()) + let shard_group = substate_address.to_shard_group(TEST_NUM_PRESHARDS, state.committees.len() as u32); + Ok(state.committees[&shard_group].clone()) } async fn get_our_validator_node(&self, _epoch: Epoch) -> Result, EpochManagerError> { @@ -189,10 +188,22 @@ impl EpochManagerReader for TestEpochManager { async fn get_local_committee_info(&self, epoch: Epoch) -> Result { let our_vn = self.get_our_validator_node(epoch).await?; let num_committees = self.get_num_committees(epoch).await?; - let committee = self.get_committee_for_substate(epoch, our_vn.shard_key).await?; - let our_shard = our_vn.shard_key.to_shard(num_committees); - - Ok(CommitteeInfo::new(num_committees, committee.len() as u32, our_shard)) + let sg = our_vn.shard_key.to_shard_group(TEST_NUM_PRESHARDS, num_committees); + let num_shard_group_members = self + .inner + .lock() + .await + .committees + .get(&sg) + .map(|c| c.len()) + .unwrap_or(0); + + Ok(CommitteeInfo::new( + TEST_NUM_PRESHARDS, + num_shard_group_members as u32, + num_committees as u32, + sg, + )) } async fn current_epoch(&self) -> Result { @@ -219,8 +230,11 @@ impl EpochManagerReader for TestEpochManager { Ok(self.inner.lock().await.committees.len() as u32) } - async fn get_committees(&self, _epoch: Epoch) -> Result>, EpochManagerError> { - todo!() + async fn get_committees( + &self, + _epoch: Epoch, + ) -> Result>, EpochManagerError> { + Ok(self.inner.lock().await.committees.clone()) } async fn get_committee_info_by_validator_address( @@ -231,17 +245,17 @@ impl EpochManagerReader for TestEpochManager { todo!() } - async fn get_committees_by_shards( + async fn get_committees_by_shard_group( &self, _epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, EpochManagerError> { let state = self.state_lock().await; Ok(state .committees - .iter() - .filter(|(shard, _)| shards.contains(shard)) - .map(|(shard, committee)| (*shard, committee.clone())) + .get(&shard_group) + .into_iter() + .flat_map(|committee| shard_group.shard_iter().map(|s| (s, committee.clone()))) .collect()) } @@ -251,10 +265,22 @@ impl EpochManagerReader for TestEpochManager { substate_address: SubstateAddress, ) -> Result { let num_committees = self.get_num_committees(epoch).await?; - let committee = self.get_committee_for_substate(epoch, substate_address).await?; - let shard = substate_address.to_shard(num_committees); - - Ok(CommitteeInfo::new(num_committees, committee.len() as u32, shard)) + let sg = substate_address.to_shard_group(TEST_NUM_PRESHARDS, num_committees); + let num_members = self + .inner + .lock() + .await + .committees + .get(&sg) + .map(|c| c.len()) + .unwrap_or(0); + + Ok(CommitteeInfo::new( + TEST_NUM_PRESHARDS, + num_members as u32, + num_committees, + sg, + )) } // async fn get_committees_by_shards( @@ -318,10 +344,20 @@ pub struct TestEpochManagerState { pub last_block_of_current_epoch: FixedHash, pub is_epoch_active: bool, #[allow(clippy::type_complexity)] - pub validator_shards: - HashMap, u64, Epoch, Epoch)>, - pub committees: HashMap>, - pub address_shard: HashMap, + pub validator_shards: HashMap< + TestAddress, + ( + ShardGroup, + SubstateAddress, + PublicKey, + Option, + u64, + Epoch, + Epoch, + ), + >, + pub committees: HashMap>, + pub address_shard: HashMap, } impl Default for TestEpochManagerState { diff --git a/dan_layer/consensus_tests/src/support/harness.rs b/dan_layer/consensus_tests/src/support/harness.rs index 017aeba36..8f9506ddd 100644 --- a/dan_layer/consensus_tests/src/support/harness.rs +++ b/dan_layer/consensus_tests/src/support/harness.rs @@ -9,7 +9,7 @@ use std::{ use futures::{stream::FuturesUnordered, FutureExt, StreamExt}; use tari_consensus::hotstuff::HotstuffEvent; -use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, NodeHeight, NumPreshards, ShardGroup}; use tari_dan_storage::{ consensus_models::{BlockId, Decision, QcId, SubstateRecord, TransactionExecution, TransactionRecord}, StateStore, @@ -26,7 +26,7 @@ use tari_template_lib::models::ComponentAddress; use tari_transaction::{TransactionId, VersionedSubstateId}; use tokio::{sync::broadcast, task, time::sleep}; -use super::{create_execution_result_for_transaction, helpers, MessageFilter}; +use super::{create_execution_result_for_transaction, helpers, MessageFilter, TEST_NUM_PRESHARDS}; use crate::support::{ address::TestAddress, epoch_manager::TestEpochManager, @@ -93,7 +93,7 @@ impl Test { execution: TransactionExecution, ) -> &Self { for vn in self.validators.values() { - if dest.is_for(&vn.address, vn.shard) { + if dest.is_for(&vn.address, vn.shard_group, vn.num_committees) { vn.transaction_executions.insert(execution.clone()); } } @@ -304,7 +304,7 @@ impl Test { let committees = self.epoch_manager.all_committees().await; let mut attempts = 0usize; 'outer: loop { - for (shard, committee) in &committees { + for (shard_group, committee) in &committees { let mut blocks = self .validators .values() @@ -313,7 +313,7 @@ impl Test { .map(|v| { let block = v .state_store - .with_read_tx(|tx| tx.blocks_get_tip(current_epoch, *shard)) + .with_read_tx(|tx| tx.blocks_get_tip(current_epoch, *shard_group)) .unwrap(); (v.address.clone(), block) }); @@ -392,7 +392,7 @@ impl Test { } pub struct TestBuilder { - committees: HashMap>, + committees: HashMap>, sql_address: String, timeout: Option, debug_sql_file: Option, @@ -433,10 +433,10 @@ impl TestBuilder { self } - pub fn add_committee>(mut self, shard: T, addresses: Vec<&'static str>) -> Self { + pub fn add_committee(mut self, committee_num: u32, addresses: Vec<&'static str>) -> Self { let entry = self .committees - .entry(shard.into()) + .entry(committee_num) .or_insert_with(|| Committee::new(vec![])); for addr in addresses { @@ -453,26 +453,28 @@ impl TestBuilder { } async fn build_validators( - &self, leader_strategy: &RoundRobinLeaderStrategy, epoch_manager: &TestEpochManager, + sql_address: String, shutdown_signal: ShutdownSignal, ) -> (Vec, HashMap) { + let num_committees = epoch_manager.get_num_committees(Epoch(0)).await.unwrap(); epoch_manager .all_validators() .await .into_iter() - .map(|(address, bucket, shard, _, _, _, _)| { - let sql_address = self.sql_address.replace("{}", &address.0); + .map(|(address, shard_group, shard_addr, _, _, _, _)| { + let sql_address = sql_address.replace("{}", &address.0); let (sk, pk) = helpers::derive_keypair_from_address(&address); let (channels, validator) = Validator::builder() .with_sql_url(sql_address) .with_address_and_secret_key(address.clone(), sk) - .with_shard(shard) - .with_bucket(bucket) - .with_epoch_manager(epoch_manager.clone_for(address.clone(), pk, shard)) + .with_shard(shard_addr) + .with_shard_group(shard_group) + .with_epoch_manager(epoch_manager.clone_for(address.clone(), pk, shard_addr)) .with_leader_strategy(*leader_strategy) + .with_num_committees(num_committees) .spawn(shutdown_signal.clone()); (channels, (address, validator)) }) @@ -493,14 +495,15 @@ impl TestBuilder { self.sql_address = format!("sqlite://{sql_file}"); } + let committees = build_committees(self.committees); + let leader_strategy = RoundRobinLeaderStrategy::new(); let (tx_epoch_events, _) = broadcast::channel(10); let epoch_manager = TestEpochManager::new(tx_epoch_events); - epoch_manager.add_committees(self.committees.clone()).await; + epoch_manager.add_committees(committees).await; let shutdown = Shutdown::new(); - let (channels, validators) = self - .build_validators(&leader_strategy, &epoch_manager, shutdown.to_signal()) - .await; + let (channels, validators) = + Self::build_validators(&leader_strategy, &epoch_manager, self.sql_address, shutdown.to_signal()).await; let network = spawn_network(channels, shutdown.to_signal(), self.message_filter); Test { @@ -514,3 +517,47 @@ impl TestBuilder { } } } + +/// Converts a test committee number to a shard group. E.g. 0 is shard group 0 to 21, 1 is 22 to 42, etc. +pub fn committee_number_to_shard_group(num_shards: NumPreshards, target_group: u32, num_committees: u32) -> ShardGroup { + // number of committees can never exceed number of shards + assert!(num_committees <= num_shards.as_u32()); + if num_committees <= 1 { + return ShardGroup::new(Shard::zero(), Shard::from(num_shards.as_u32() - 1)); + } + + let shards_per_committee = num_shards.as_u32() / num_committees; + let mut shards_per_committee_rem = num_shards.as_u32() % num_committees; + + let mut start = 0u32; + let mut end = shards_per_committee; + if shards_per_committee_rem > 0 { + end += 1; + } + + for _group in 0..target_group { + start += shards_per_committee; + if shards_per_committee_rem > 0 { + start += 1; + shards_per_committee_rem -= 1; + } + + end = start + shards_per_committee; + if shards_per_committee_rem > 0 { + end += 1; + } + } + + ShardGroup::new(start, end - 1) +} + +fn build_committees(committees: HashMap>) -> HashMap> { + let num_committees = committees.len() as u32; + committees + .into_iter() + .map(|(num, committee)| { + let shard_group = committee_number_to_shard_group(TEST_NUM_PRESHARDS, num, num_committees); + (shard_group, committee) + }) + .collect() +} diff --git a/dan_layer/consensus_tests/src/support/helpers.rs b/dan_layer/consensus_tests/src/support/helpers.rs index 0326574c9..93150e9b1 100644 --- a/dan_layer/consensus_tests/src/support/helpers.rs +++ b/dan_layer/consensus_tests/src/support/helpers.rs @@ -1,27 +1,50 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use rand::{rngs::OsRng, Rng}; +use std::ops::RangeBounds; + +use rand::{rngs::OsRng, Rng, RngCore}; use tari_common_types::types::{PrivateKey, PublicKey}; use tari_crypto::keys::{PublicKey as _, SecretKey}; -use tari_dan_common_types::shard::Shard; +use tari_dan_common_types::{ + uint::{U256, U256_ZERO}, + NumPreshards, + ShardGroup, + SubstateAddress, +}; use tari_engine_types::substate::SubstateId; use tari_template_lib::models::{ComponentAddress, ComponentKey, EntityId, ObjectKey}; use tari_transaction::VersionedSubstateId; use crate::support::TestAddress; -pub(crate) fn random_substate_in_shard(shard: Shard, num_shards: u32) -> VersionedSubstateId { - let range = shard.to_substate_address_range(num_shards); - let size = range.end().to_u256() - range.start().to_u256(); - let middlish = range.start().to_u256() + size / 2; - let entity_id = EntityId::new(copy_fixed(&middlish.to_be_bytes()[0..EntityId::LENGTH])); +pub(crate) fn random_substate_in_shard_group(shard_group: ShardGroup, num_shards: NumPreshards) -> VersionedSubstateId { + let range = shard_group.to_substate_address_range(num_shards); + let middlish = random_substate_address_range(range); + let entity_id = EntityId::new(copy_fixed(&middlish.to_u256().to_be_bytes()[0..EntityId::LENGTH])); let rand_bytes = OsRng.gen::<[u8; ComponentKey::LENGTH]>(); let component_key = ComponentKey::new(copy_fixed(&rand_bytes)); let substate_id = SubstateId::Component(ComponentAddress::new(ObjectKey::new(entity_id, component_key))); VersionedSubstateId::new(substate_id, 0) } +fn random_substate_address_range>(range: R) -> SubstateAddress { + let start = match range.start_bound() { + std::ops::Bound::Included(addr) => addr.to_u256(), + std::ops::Bound::Excluded(addr) => addr.to_u256() + 1, + std::ops::Bound::Unbounded => U256_ZERO, + }; + let end = match range.end_bound() { + std::ops::Bound::Included(addr) => addr.to_u256(), + std::ops::Bound::Excluded(addr) => addr.to_u256() - 1, + std::ops::Bound::Unbounded => U256::MAX, + }; + let mut bytes = [0u8; 32]; + OsRng.fill_bytes(&mut bytes); + let rand = U256::from_le_bytes(bytes); + SubstateAddress::from_u256(start + (rand % (end - start))) +} + fn copy_fixed(bytes: &[u8]) -> [u8; SZ] { let mut out = [0u8; SZ]; out.copy_from_slice(bytes); diff --git a/dan_layer/consensus_tests/src/support/mod.rs b/dan_layer/consensus_tests/src/support/mod.rs index 2d1dd8057..b9add837f 100644 --- a/dan_layer/consensus_tests/src/support/mod.rs +++ b/dan_layer/consensus_tests/src/support/mod.rs @@ -4,6 +4,8 @@ // TODO: use all functions // #![allow(dead_code)] +pub const TEST_NUM_PRESHARDS: NumPreshards = NumPreshards::SixtyFour; + mod address; mod epoch_manager; mod executions_store; @@ -25,6 +27,7 @@ pub use harness::*; pub use leader_strategy::*; pub use network::*; pub use spec::*; +use tari_dan_common_types::NumPreshards; pub use transaction::*; pub use transaction_executor::*; pub use validator::*; diff --git a/dan_layer/consensus_tests/src/support/network.rs b/dan_layer/consensus_tests/src/support/network.rs index 68c5c623b..25dbea379 100644 --- a/dan_layer/consensus_tests/src/support/network.rs +++ b/dan_layer/consensus_tests/src/support/network.rs @@ -9,7 +9,7 @@ use std::{ use futures::{stream::FuturesUnordered, FutureExt, StreamExt}; use itertools::Itertools; use tari_consensus::messages::HotstuffMessage; -use tari_dan_common_types::shard::Shard; +use tari_dan_common_types::ShardGroup; use tari_dan_storage::consensus_models::TransactionRecord; use tari_shutdown::ShutdownSignal; use tari_state_store_sqlite::SqliteStateStore; @@ -23,7 +23,7 @@ use tokio::{ task, }; -use crate::support::{address::TestAddress, ValidatorChannels}; +use crate::support::{address::TestAddress, committee_number_to_shard_group, ValidatorChannels, TEST_NUM_PRESHARDS}; pub type MessageFilter = Box bool + Sync + Send + 'static>; @@ -37,7 +37,12 @@ pub fn spawn_network( .map(|c| { ( c.address.clone(), - (c.shard, c.tx_new_transactions.clone(), c.state_store.clone()), + ( + c.shard_group, + c.num_committees, + c.tx_new_transactions.clone(), + c.state_store.clone(), + ), ) }) .collect(); @@ -117,7 +122,7 @@ impl TestNetwork { } pub async fn go_offline(&self, destination: TestNetworkDestination) -> &Self { - if destination.is_bucket() { + if destination.is_shard() { unimplemented!("Sorry :/ taking a bucket offline is not yet supported in the test harness"); } self.offline_destinations.write().await.push(destination); @@ -153,28 +158,37 @@ pub enum TestNetworkDestination { All, Address(TestAddress), #[allow(dead_code)] - Shard(u32), + Committee(u32), } impl TestNetworkDestination { - pub fn is_for(&self, addr: &TestAddress, bucket: Shard) -> bool { + pub fn is_for(&self, addr: &TestAddress, shard_group: ShardGroup, num_committees: u32) -> bool { match self { TestNetworkDestination::All => true, TestNetworkDestination::Address(a) => a == addr, - TestNetworkDestination::Shard(b) => *b == bucket, + TestNetworkDestination::Committee(b) => { + committee_number_to_shard_group(TEST_NUM_PRESHARDS, *b, num_committees) == shard_group + }, } } - pub fn is_bucket(&self) -> bool { - matches!(self, TestNetworkDestination::Shard(_)) + pub fn is_shard(&self) -> bool { + matches!(self, TestNetworkDestination::Committee(_)) } } pub struct TestNetworkWorker { rx_new_transaction: Option>, #[allow(clippy::type_complexity)] - tx_new_transactions: - HashMap, SqliteStateStore)>, + tx_new_transactions: HashMap< + TestAddress, + ( + ShardGroup, + u32, // num_committees + mpsc::Sender<(Transaction, usize)>, + SqliteStateStore, + ), + >, tx_hs_message: HashMap>, #[allow(clippy::type_complexity)] rx_broadcast: Option, HotstuffMessage)>>>, @@ -224,8 +238,8 @@ impl TestNetworkWorker { .await .insert(*tx_record.transaction().id(), tx_record.clone()); - for (addr, (shard, tx_new_transaction_to_consensus, _)) in &tx_new_transactions { - if dest.is_for(addr, *shard) { + for (addr, (shard_group, num_committees, tx_new_transaction_to_consensus, _)) in &tx_new_transactions { + if dest.is_for(addr, *shard_group, *num_committees) { tx_new_transaction_to_consensus .send((tx_record.transaction().clone(), remaining)) .await @@ -296,7 +310,10 @@ impl TestNetworkWorker { } } // TODO: support for taking a whole committee bucket offline - if vn != from && self.is_offline_destination(&vn, u32::MAX.into()).await { + if vn != from && + self.is_offline_destination(&vn, ShardGroup::all_shards(TEST_NUM_PRESHARDS)) + .await + { continue; } @@ -321,7 +338,10 @@ impl TestNetworkWorker { } } log::debug!("โœ‰๏ธ Message {} from {} to {}", msg, from, to); - if from != to && self.is_offline_destination(&from, u32::MAX.into()).await { + if from != to && + self.is_offline_destination(&from, ShardGroup::all_shards(TEST_NUM_PRESHARDS)) + .await + { log::info!("๐Ÿ›‘ Discarding message {msg}. Leader {from} is offline"); return; } @@ -331,8 +351,9 @@ impl TestNetworkWorker { self.tx_hs_message.get(&to).unwrap().send((from, msg)).await.unwrap(); } - async fn is_offline_destination(&self, addr: &TestAddress, shard: Shard) -> bool { + async fn is_offline_destination(&self, addr: &TestAddress, shard: ShardGroup) -> bool { let lock = self.offline_destinations.read().await; - lock.iter().any(|d| d.is_for(addr, shard)) + // 99999 is not used TODO: support for taking entire shard group offline + lock.iter().any(|d| d.is_for(addr, shard, 99999)) } } diff --git a/dan_layer/consensus_tests/src/support/transaction.rs b/dan_layer/consensus_tests/src/support/transaction.rs index d7b2e3d7f..7cddc7d2f 100644 --- a/dan_layer/consensus_tests/src/support/transaction.rs +++ b/dan_layer/consensus_tests/src/support/transaction.rs @@ -21,7 +21,7 @@ use tari_engine_types::{ }; use tari_transaction::{Transaction, TransactionId, VersionedSubstateId}; -use crate::support::helpers::random_substate_in_shard; +use crate::support::{committee_number_to_shard_group, helpers::random_substate_in_shard_group, TEST_NUM_PRESHARDS}; pub fn build_transaction_from( tx: Transaction, @@ -111,9 +111,14 @@ pub fn build_transaction( // We create these outputs so that the test VNs dont have to have any UP substates // Equal potion of shards to each committee let outputs = (0..num_committees) - .flat_map(|shard| { - iter::repeat_with(move || random_substate_in_shard(shard.into(), num_committees)) - .take(total_num_outputs.div_ceil(num_committees as usize)) + .flat_map(|group_no| { + iter::repeat_with(move || { + random_substate_in_shard_group( + committee_number_to_shard_group(TEST_NUM_PRESHARDS, group_no, num_committees), + TEST_NUM_PRESHARDS, + ) + }) + .take(total_num_outputs.div_ceil(num_committees as usize)) }) .collect::>(); diff --git a/dan_layer/consensus_tests/src/support/validator/builder.rs b/dan_layer/consensus_tests/src/support/validator/builder.rs index eb0f48f5e..866580c39 100644 --- a/dan_layer/consensus_tests/src/support/validator/builder.rs +++ b/dan_layer/consensus_tests/src/support/validator/builder.rs @@ -1,6 +1,8 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use std::time::Duration; + use tari_common::configuration::Network; use tari_common_types::types::{PrivateKey, PublicKey}; use tari_consensus::{ @@ -8,7 +10,7 @@ use tari_consensus::{ traits::hooks::NoopHooks, }; use tari_crypto::keys::PublicKey as _; -use tari_dan_common_types::{shard::Shard, SubstateAddress}; +use tari_dan_common_types::{ShardGroup, SubstateAddress}; use tari_dan_storage::consensus_models::TransactionPool; use tari_shutdown::ShutdownSignal; use tari_state_store_sqlite::SqliteStateStore; @@ -26,6 +28,7 @@ use crate::support::{ TestConsensusSpec, Validator, ValidatorChannels, + TEST_NUM_PRESHARDS, }; pub struct ValidatorBuilder { @@ -33,9 +36,10 @@ pub struct ValidatorBuilder { pub secret_key: PrivateKey, pub public_key: PublicKey, pub shard_address: SubstateAddress, - pub shard: Shard, + pub shard_group: ShardGroup, pub sql_url: String, pub leader_strategy: RoundRobinLeaderStrategy, + pub num_committees: u32, pub epoch_manager: Option, pub transaction_executions: TestTransactionExecutionsStore, } @@ -47,7 +51,8 @@ impl ValidatorBuilder { secret_key: PrivateKey::default(), public_key: PublicKey::default(), shard_address: SubstateAddress::zero(), - shard: Shard::from(0), + num_committees: 0, + shard_group: ShardGroup::all_shards(TEST_NUM_PRESHARDS), sql_url: ":memory".to_string(), leader_strategy: RoundRobinLeaderStrategy::new(), epoch_manager: None, @@ -62,8 +67,8 @@ impl ValidatorBuilder { self } - pub fn with_bucket(&mut self, bucket: Shard) -> &mut Self { - self.shard = bucket; + pub fn with_shard_group(&mut self, shard_group: ShardGroup) -> &mut Self { + self.shard_group = shard_group; self } @@ -87,6 +92,11 @@ impl ValidatorBuilder { self } + pub fn with_num_committees(&mut self, num_committees: u32) -> &mut Self { + self.num_committees = num_committees; + self + } + pub fn spawn(&self, shutdown_signal: ShutdownSignal) -> (ValidatorChannels, Validator) { log::info!( "Spawning validator with address {} and public key {}", @@ -116,8 +126,14 @@ impl ValidatorBuilder { let transaction_executor = TestBlockTransactionProcessor::new(self.transaction_executions.clone()); let worker = HotstuffWorker::::new( + HotstuffConfig { + num_preshards: TEST_NUM_PRESHARDS, + max_base_layer_blocks_ahead: 5, + max_base_layer_blocks_behind: 5, + network: Network::LocalNet, + pacemaker_max_base_time: Duration::from_secs(10), + }, self.address.clone(), - Network::LocalNet, inbound_messaging, outbound_messaging, rx_new_transactions, @@ -130,11 +146,6 @@ impl ValidatorBuilder { tx_events.clone(), NoopHooks, shutdown_signal.clone(), - HotstuffConfig { - max_base_layer_blocks_ahead: 5, - max_base_layer_blocks_behind: 5, - pacemaker_max_base_time: std::time::Duration::from_secs(10), - }, ); let (tx_current_state, rx_current_state) = watch::channel(ConsensusCurrentState::default()); @@ -150,7 +161,8 @@ impl ValidatorBuilder { let channels = ValidatorChannels { address: self.address.clone(), - shard: self.shard, + shard_group: self.shard_group, + num_committees: self.num_committees, state_store: store.clone(), tx_new_transactions, tx_hs_message, @@ -161,7 +173,8 @@ impl ValidatorBuilder { let validator = Validator { address: self.address.clone(), shard_address: self.shard_address, - shard: self.shard, + shard_group: self.shard_group, + num_committees: self.num_committees, transaction_executions: self.transaction_executions.clone(), state_store: store, epoch_manager, diff --git a/dan_layer/consensus_tests/src/support/validator/instance.rs b/dan_layer/consensus_tests/src/support/validator/instance.rs index 5a48e1ff2..262252d1f 100644 --- a/dan_layer/consensus_tests/src/support/validator/instance.rs +++ b/dan_layer/consensus_tests/src/support/validator/instance.rs @@ -5,7 +5,7 @@ use tari_consensus::{ hotstuff::{ConsensusCurrentState, HotstuffEvent}, messages::HotstuffMessage, }; -use tari_dan_common_types::{shard::Shard, SubstateAddress}; +use tari_dan_common_types::{ShardGroup, SubstateAddress}; use tari_dan_storage::{consensus_models::LeafBlock, StateStore, StateStoreReadTransaction}; use tari_state_store_sqlite::SqliteStateStore; use tari_transaction::Transaction; @@ -24,7 +24,8 @@ use crate::support::{ pub struct ValidatorChannels { pub address: TestAddress, - pub shard: Shard, + pub shard_group: ShardGroup, + pub num_committees: u32, pub state_store: SqliteStateStore, pub tx_new_transactions: mpsc::Sender<(Transaction, usize)>, @@ -36,7 +37,8 @@ pub struct ValidatorChannels { pub struct Validator { pub address: TestAddress, pub shard_address: SubstateAddress, - pub shard: Shard, + pub shard_group: ShardGroup, + pub num_committees: u32, pub state_store: SqliteStateStore, pub transaction_executions: TestTransactionExecutionsStore, diff --git a/dan_layer/engine_types/Cargo.toml b/dan_layer/engine_types/Cargo.toml index fdcd8c200..4fb239773 100644 --- a/dan_layer/engine_types/Cargo.toml +++ b/dan_layer/engine_types/Cargo.toml @@ -28,7 +28,6 @@ serde = { workspace = true, default-features = true } serde_json = { workspace = true } thiserror = { workspace = true } ts-rs = { workspace = true, optional = true } -log = { workspace = true } [features] default = [] diff --git a/dan_layer/engine_types/src/confidential/withdraw.rs b/dan_layer/engine_types/src/confidential/withdraw.rs index 0cf8bc5c7..ecc27ee58 100644 --- a/dan_layer/engine_types/src/confidential/withdraw.rs +++ b/dan_layer/engine_types/src/confidential/withdraw.rs @@ -114,12 +114,6 @@ pub(crate) fn validate_confidential_withdraw<'a, I: IntoIterator for vn in &vns { validator_nodes.set_committee_shard( vn.shard_key, - vn.shard_key.to_shard(num_committees), + vn.shard_key.to_shard_group(self.config.num_preshards, num_committees), self.config.validator_node_sidechain_id.as_ref(), epoch, )?; @@ -419,7 +415,7 @@ impl epoch.as_u64() >= current_epoch.as_u64().saturating_sub(10) && epoch.as_u64() <= current_epoch.as_u64() } - pub fn get_committees(&self, epoch: Epoch) -> Result>, EpochManagerError> { + pub fn get_committees(&self, epoch: Epoch) -> Result>, EpochManagerError> { let mut tx = self.global_db.create_transaction()?; let mut validator_node_db = self.global_db.validator_nodes(&mut tx); Ok(validator_node_db.get_committees(epoch, self.config.validator_node_sidechain_id.as_ref())?) @@ -444,32 +440,38 @@ impl epoch: Epoch, substate_address: SubstateAddress, ) -> Result>, EpochManagerError> { - // retrieve the validator nodes for this epoch from database, sorted by shard_key - let vns = self.get_validator_nodes_per_epoch(epoch)?; - if vns.is_empty() { + let num_vns = self.get_total_validator_count(epoch)?; + if num_vns == 0 { return Err(EpochManagerError::NoCommitteeVns { substate_address, epoch, }); } - let num_committees = calculate_num_committees(vns.len() as u64, self.config.committee_size); + let num_committees = calculate_num_committees(num_vns, self.config.committee_size); if num_committees == 1 { - return Ok(vns); + // retrieve the validator nodes for this epoch from database, sorted by shard_key + return self.get_validator_nodes_per_epoch(epoch); } // A shard a equal slice of the shard space that a validator fits into - let shard = substate_address.to_shard(num_committees); + let shard_group = substate_address.to_shard_group(self.config.num_preshards, num_committees); - let mut shards = HashSet::new(); - shards.insert(shard); - let selected = self.get_committees_for_shards(epoch, shards)?; - let shard_vns = selected.get(&shard).map(|c| c.members.clone()).unwrap_or_default(); + // TODO(perf): fetch full validator node records for the shard group in single query (current O(n + 1) queries) + let committees = self.get_committees_for_shard_group(epoch, shard_group)?; let mut res = vec![]; - for (_, pub_key) in shard_vns { - if let Some(vn) = vns.iter().find(|vn| vn.public_key == pub_key) { - res.push(vn.clone()); + for (_, committee) in committees { + for pub_key in committee.public_keys() { + let vn = self.get_validator_node_by_public_key(epoch, pub_key)?.ok_or_else(|| { + EpochManagerError::ValidatorNodeNotRegistered { + address: TAddr::try_from_public_key(pub_key) + .map(|a| a.to_string()) + .unwrap_or_else(|| pub_key.to_string()), + epoch, + } + })?; + res.push(vn); } } Ok(res) @@ -557,12 +559,12 @@ impl } pub fn get_total_validator_count(&self, epoch: Epoch) -> Result { - self.get_validator_nodes_per_epoch(epoch)? - .len() - .try_into() - .map_err(|_| EpochManagerError::IntegerOverflow { - func: "get_total_validator_count", - }) + let mut tx = self.global_db.create_transaction()?; + let db_vns = self + .global_db + .validator_nodes(&mut tx) + .count(epoch, self.config.validator_node_sidechain_id.as_ref())?; + Ok(db_vns) } pub fn get_num_committees(&self, epoch: Epoch) -> Result { @@ -578,15 +580,23 @@ impl substate_address: SubstateAddress, ) -> Result { let num_committees = self.get_number_of_committees(epoch)?; - let shard = substate_address.to_shard(num_committees); + let shard_group = substate_address.to_shard_group(self.config.num_preshards, num_committees); let mut tx = self.global_db.create_transaction()?; let mut validator_node_db = self.global_db.validator_nodes(&mut tx); - let num_validators = - validator_node_db.count_in_bucket(epoch, self.config.validator_node_sidechain_id.as_ref(), shard)?; + let num_validators = validator_node_db.count_in_shard_group( + epoch, + self.config.validator_node_sidechain_id.as_ref(), + shard_group, + )?; let num_validators = u32::try_from(num_validators).map_err(|_| EpochManagerError::IntegerOverflow { func: "get_committee_shard", })?; - Ok(CommitteeInfo::new(num_committees, num_validators, shard)) + Ok(CommitteeInfo::new( + self.config.num_preshards, + num_validators, + num_committees, + shard_group, + )) } pub fn get_local_committee_info(&self, epoch: Epoch) -> Result { @@ -599,14 +609,14 @@ impl self.get_committee_info_for_substate(epoch, vn.shard_key) } - pub(crate) fn get_committees_for_shards( + pub(crate) fn get_committees_for_shard_group( &self, epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, EpochManagerError> { let mut tx = self.global_db.create_transaction()?; let mut validator_node_db = self.global_db.validator_nodes(&mut tx); - let committees = validator_node_db.get_committees_for_shards(epoch, shards)?; + let committees = validator_node_db.get_committees_for_shard_group(epoch, shard_group)?; Ok(committees) } diff --git a/dan_layer/epoch_manager/src/base_layer/config.rs b/dan_layer/epoch_manager/src/base_layer/config.rs index 49a40b331..e332d9335 100644 --- a/dan_layer/epoch_manager/src/base_layer/config.rs +++ b/dan_layer/epoch_manager/src/base_layer/config.rs @@ -4,10 +4,12 @@ use std::num::NonZeroU32; use tari_common_types::types::PublicKey; +use tari_dan_common_types::NumPreshards; #[derive(Debug, Clone)] pub struct EpochManagerConfig { pub base_layer_confirmations: u64, pub committee_size: NonZeroU32, pub validator_node_sidechain_id: Option, + pub num_preshards: NumPreshards, } diff --git a/dan_layer/epoch_manager/src/base_layer/epoch_manager_service.rs b/dan_layer/epoch_manager/src/base_layer/epoch_manager_service.rs index 8f94a6651..99949ac6d 100644 --- a/dan_layer/epoch_manager/src/base_layer/epoch_manager_service.rs +++ b/dan_layer/epoch_manager/src/base_layer/epoch_manager_service.rs @@ -240,9 +240,15 @@ impl EpochManagerRequest::GetNumCommittees { epoch, reply } => { handle(reply, self.inner.get_num_committees(epoch), context) }, - EpochManagerRequest::GetCommitteesForShards { epoch, shards, reply } => { - handle(reply, self.inner.get_committees_for_shards(epoch, shards), context) - }, + EpochManagerRequest::GetCommitteesForShardGroup { + epoch, + shard_group, + reply, + } => handle( + reply, + self.inner.get_committees_for_shard_group(epoch, shard_group), + context, + ), EpochManagerRequest::GetFeeClaimPublicKey { reply } => { handle(reply, self.inner.get_fee_claim_public_key(), context) }, diff --git a/dan_layer/epoch_manager/src/base_layer/handle.rs b/dan_layer/epoch_manager/src/base_layer/handle.rs index 13d345bbc..581c68222 100644 --- a/dan_layer/epoch_manager/src/base_layer/handle.rs +++ b/dan_layer/epoch_manager/src/base_layer/handle.rs @@ -1,7 +1,7 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use async_trait::async_trait; use tari_base_node_client::types::BaseLayerConsensusConstants; @@ -12,6 +12,7 @@ use tari_dan_common_types::{ shard::Shard, Epoch, NodeAddressable, + ShardGroup, SubstateAddress, }; use tari_dan_storage::global::models::ValidatorNode; @@ -157,7 +158,10 @@ impl EpochManagerHandle { rx.await.map_err(|_| EpochManagerError::ReceiveError)? } - pub async fn get_committees(&self, epoch: Epoch) -> Result>, EpochManagerError> { + pub async fn get_committees( + &self, + epoch: Epoch, + ) -> Result>, EpochManagerError> { let (tx, rx) = oneshot::channel(); self.tx_request .send(EpochManagerRequest::GetCommittees { epoch, reply: tx }) @@ -201,7 +205,10 @@ impl EpochManagerReader for EpochManagerHandle { rx.await.map_err(|_| EpochManagerError::ReceiveError)? } - async fn get_committees(&self, epoch: Epoch) -> Result>, EpochManagerError> { + async fn get_committees( + &self, + epoch: Epoch, + ) -> Result>, EpochManagerError> { let (tx, rx) = oneshot::channel(); self.tx_request .send(EpochManagerRequest::GetCommittees { epoch, reply: tx }) @@ -397,16 +404,16 @@ impl EpochManagerReader for EpochManagerHandle { rx.await.map_err(|_| EpochManagerError::ReceiveError)? } - async fn get_committees_by_shards( + async fn get_committees_by_shard_group( &self, epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, EpochManagerError> { let (tx, rx) = oneshot::channel(); self.tx_request - .send(EpochManagerRequest::GetCommitteesForShards { + .send(EpochManagerRequest::GetCommitteesForShardGroup { epoch, - shards, + shard_group, reply: tx, }) .await diff --git a/dan_layer/epoch_manager/src/base_layer/types.rs b/dan_layer/epoch_manager/src/base_layer/types.rs index 22f18cf6a..7599914d8 100644 --- a/dan_layer/epoch_manager/src/base_layer/types.rs +++ b/dan_layer/epoch_manager/src/base_layer/types.rs @@ -1,7 +1,7 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use tari_base_node_client::types::BaseLayerConsensusConstants; use tari_common_types::types::{FixedHash, PublicKey}; @@ -10,6 +10,7 @@ use tari_dan_common_types::{ committee::{Committee, CommitteeInfo}, shard::Shard, Epoch, + ShardGroup, SubstateAddress, }; use tari_dan_storage::global::models::ValidatorNode; @@ -77,7 +78,7 @@ pub enum EpochManagerRequest { }, GetCommittees { epoch: Epoch, - reply: Reply>>, + reply: Reply>>, }, GetCommitteeForSubstate { epoch: Epoch, @@ -125,9 +126,9 @@ pub enum EpochManagerRequest { epoch: Epoch, reply: Reply, }, - GetCommitteesForShards { + GetCommitteesForShardGroup { epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, reply: Reply>>, }, GetBaseLayerBlockHeight { diff --git a/dan_layer/epoch_manager/src/error.rs b/dan_layer/epoch_manager/src/error.rs index 82e6033a8..c07959d80 100644 --- a/dan_layer/epoch_manager/src/error.rs +++ b/dan_layer/epoch_manager/src/error.rs @@ -24,7 +24,7 @@ pub enum EpochManagerError { SqlLiteStorageError(anyhow::Error), #[error("No validator nodes found for current shard key")] ValidatorNodesNotFound, - #[error("No committee VNs found for shard {substate_address} and epoch {epoch}")] + #[error("No committee VNs found for address {substate_address} and epoch {epoch}")] NoCommitteeVns { substate_address: SubstateAddress, epoch: Epoch, diff --git a/dan_layer/epoch_manager/src/traits.rs b/dan_layer/epoch_manager/src/traits.rs index e7e082579..ef1c030f8 100644 --- a/dan_layer/epoch_manager/src/traits.rs +++ b/dan_layer/epoch_manager/src/traits.rs @@ -20,7 +20,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use async_trait::async_trait; use tari_common_types::types::{FixedHash, PublicKey}; @@ -29,6 +29,7 @@ use tari_dan_common_types::{ shard::Shard, Epoch, NodeAddressable, + ShardGroup, SubstateAddress, }; use tari_dan_storage::global::models::ValidatorNode; @@ -46,7 +47,10 @@ pub trait EpochManagerReader: Send + Sync { async fn get_all_validator_nodes(&self, epoch: Epoch) -> Result>, EpochManagerError>; - async fn get_committees(&self, epoch: Epoch) -> Result>, EpochManagerError>; + async fn get_committees( + &self, + epoch: Epoch, + ) -> Result>, EpochManagerError>; async fn get_committee_info_by_validator_address( &self, epoch: Epoch, @@ -110,10 +114,10 @@ pub trait EpochManagerReader: Send + Sync { async fn get_num_committees(&self, epoch: Epoch) -> Result; - async fn get_committees_by_shards( + async fn get_committees_by_shard_group( &self, epoch: Epoch, - shards: HashSet, + shards: ShardGroup, ) -> Result>, EpochManagerError>; async fn get_local_committee(&self, epoch: Epoch) -> Result, EpochManagerError> { diff --git a/dan_layer/p2p/proto/consensus.proto b/dan_layer/p2p/proto/consensus.proto index 6a25deaec..ed8a69ee4 100644 --- a/dan_layer/p2p/proto/consensus.proto +++ b/dan_layer/p2p/proto/consensus.proto @@ -46,7 +46,7 @@ message Block { QuorumCertificate justify = 3; uint64 height = 4; uint64 epoch = 5; - uint32 shard = 6; + uint32 shard_group = 6; bytes proposed_by = 7; bytes merkle_root = 8; repeated Command commands = 9; @@ -83,7 +83,7 @@ enum ForeignProposalState { } message ForeignProposal { - uint32 bucket = 1; + uint32 shard_group = 1; bytes block_id = 2; ForeignProposalState state = 3; uint64 mined_at = 4; @@ -122,7 +122,7 @@ message QuorumCertificate { repeated tari.dan.common.SignatureAndPublicKey signatures = 4; repeated bytes leaf_hashes = 6; QuorumDecision decision = 7; - uint32 shard = 8; + uint32 shard_group = 8; } message HotStuffTreeNode { diff --git a/dan_layer/p2p/proto/rpc.proto b/dan_layer/p2p/proto/rpc.proto index 3d21b72f4..49503860c 100644 --- a/dan_layer/p2p/proto/rpc.proto +++ b/dan_layer/p2p/proto/rpc.proto @@ -161,10 +161,6 @@ message GetVirtualSubstateResponse { repeated tari.dan.consensus.QuorumCertificate quorum_certificates = 2; } -message SubstateCreatedProof { - SubstateData substate = 1; - tari.dan.consensus.QuorumCertificate created_justify = 2; -} // Minimal substate data message SubstateData { @@ -174,12 +170,7 @@ message SubstateData { bytes created_transaction = 7; } -message SubstateDestroyedProof { - bytes substate_id = 1; - uint32 version = 2; - tari.dan.consensus.QuorumCertificate destroyed_justify = 3; - bytes destroyed_by_transaction = 4; -} + message SubstateUpdate { oneof update { @@ -188,6 +179,18 @@ message SubstateUpdate { } } +message SubstateCreatedProof { + SubstateData substate = 1; + // tari.dan.consensus.QuorumCertificate created_justify = 2; +} + +message SubstateDestroyedProof { + bytes substate_id = 1; + uint32 version = 2; + // tari.dan.consensus.QuorumCertificate destroyed_justify = 3; + bytes destroyed_by_transaction = 4; +} + message SyncBlocksRequest { bytes start_block_id = 1; tari.dan.common.Epoch up_to_epoch = 2; @@ -237,7 +240,6 @@ message SyncStateRequest { // The shard in the current shard-epoch that is requested. // This will limit the state transitions returned to those that fall within this shard-epoch. uint64 current_epoch = 4; - uint32 current_shard = 5; } message SyncStateResponse { diff --git a/dan_layer/p2p/src/conversions/consensus.rs b/dan_layer/p2p/src/conversions/consensus.rs index 25934facb..4ad119fc0 100644 --- a/dan_layer/p2p/src/conversions/consensus.rs +++ b/dan_layer/p2p/src/conversions/consensus.rs @@ -37,7 +37,7 @@ use tari_consensus::messages::{ VoteMessage, }; use tari_crypto::tari_utilities::ByteArray; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight, ValidatorMetadata}; +use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight, ShardGroup, ValidatorMetadata}; use tari_dan_storage::consensus_models::{ BlockId, Command, @@ -260,7 +260,7 @@ impl From<&tari_dan_storage::consensus_models::Block> for proto::consensus::Bloc network: value.network().as_byte().into(), height: value.height().as_u64(), epoch: value.epoch().as_u64(), - shard: value.shard().as_u32(), + shard_group: value.shard_group().encode_as_u32(), parent_id: value.parent().as_bytes().to_vec(), proposed_by: ByteArray::as_bytes(value.proposed_by()).to_vec(), merkle_root: value.merkle_root().as_slice().to_vec(), @@ -285,19 +285,25 @@ impl TryFrom for tari_dan_storage::consensus_models::Bl .map_err(|_| anyhow!("Block conversion: Invalid network byte {}", value.network))? .try_into()?; + let shard_group = ShardGroup::decode_from_u32(value.shard_group) + .ok_or_else(|| anyhow!("Block shard_group ({}) is not a valid", value.shard_group))?; + + let proposed_by = PublicKey::from_canonical_bytes(&value.proposed_by) + .map_err(|_| anyhow!("Block conversion: Invalid proposed_by"))?; + let justify = value + .justify + .ok_or_else(|| anyhow!("Block conversion: QC not provided"))? + .try_into()?; + if value.is_dummy { Ok(Self::dummy_block( network, value.parent_id.try_into()?, - PublicKey::from_canonical_bytes(&value.proposed_by) - .map_err(|_| anyhow!("Block conversion: Invalid proposed_by"))?, + proposed_by, NodeHeight(value.height), - value - .justify - .ok_or_else(|| anyhow!("Block conversion: QC not provided"))? - .try_into()?, + justify, Epoch(value.epoch), - Shard::from(value.shard), + shard_group, value.merkle_root.try_into()?, value.timestamp, value.base_layer_block_height, @@ -307,15 +313,11 @@ impl TryFrom for tari_dan_storage::consensus_models::Bl Ok(Self::new( network, value.parent_id.try_into()?, - value - .justify - .ok_or_else(|| anyhow!("Block conversion: QC not provided"))? - .try_into()?, + justify, NodeHeight(value.height), Epoch(value.epoch), - Shard::from(value.shard), - PublicKey::from_canonical_bytes(&value.proposed_by) - .map_err(|_| anyhow!("Block conversion: Invalid proposed_by"))?, + shard_group, + proposed_by, value .commands .into_iter() @@ -455,7 +457,7 @@ impl TryFrom for ForeignProposalState { impl From<&ForeignProposal> for proto::consensus::ForeignProposal { fn from(value: &ForeignProposal) -> Self { Self { - bucket: value.shard.as_u32(), + shard_group: value.shard_group.encode_as_u32(), block_id: value.block_id.as_bytes().to_vec(), state: proto::consensus::ForeignProposalState::from(value.state).into(), mined_at: value.proposed_height.map(|a| a.0).unwrap_or(0), @@ -470,7 +472,8 @@ impl TryFrom for ForeignProposal { fn try_from(value: proto::consensus::ForeignProposal) -> Result { Ok(ForeignProposal { - shard: Shard::from(value.bucket), + shard_group: ShardGroup::decode_from_u32(value.shard_group) + .ok_or_else(|| anyhow!("Block shard_group ({}) is not a valid", value.shard_group))?, block_id: BlockId::try_from(value.block_id)?, state: proto::consensus::ForeignProposalState::try_from(value.state) .map_err(|_| anyhow!("Invalid foreign proposal state value {}", value.state))? @@ -536,12 +539,11 @@ impl TryFrom for Evidence { impl From<&QuorumCertificate> for proto::consensus::QuorumCertificate { fn from(source: &QuorumCertificate) -> Self { - // TODO: unwrap Self { block_id: source.block_id().as_bytes().to_vec(), block_height: source.block_height().as_u64(), epoch: source.epoch().as_u64(), - shard: source.shard().as_u32(), + shard_group: source.shard_group().encode_as_u32(), signatures: source.signatures().iter().map(Into::into).collect(), leaf_hashes: source.leaf_hashes().iter().map(|h| h.to_vec()).collect(), decision: i32::from(source.decision().as_u8()), @@ -553,11 +555,13 @@ impl TryFrom for QuorumCertificate { type Error = anyhow::Error; fn try_from(value: proto::consensus::QuorumCertificate) -> Result { + let shard_group = ShardGroup::decode_from_u32(value.shard_group) + .ok_or_else(|| anyhow!("QC shard_group ({}) is not a valid", value.shard_group))?; Ok(Self::new( value.block_id.try_into()?, NodeHeight(value.block_height), Epoch(value.epoch), - Shard::from(value.shard), + shard_group, value .signatures .into_iter() @@ -612,6 +616,7 @@ impl TryFrom for SubstateRecord { substate_id: SubstateId::from_bytes(&value.substate_id)?, version: value.version, substate_value: SubstateValue::from_bytes(&value.substate)?, + // TODO: Should we add this to the proto? state_hash: Default::default(), created_at_epoch: Epoch(value.created_epoch), diff --git a/dan_layer/p2p/src/conversions/rpc.rs b/dan_layer/p2p/src/conversions/rpc.rs index 3d8297465..4b52d7a35 100644 --- a/dan_layer/p2p/src/conversions/rpc.rs +++ b/dan_layer/p2p/src/conversions/rpc.rs @@ -28,11 +28,11 @@ impl TryFrom for SubstateCreatedProof { .map(TryInto::try_into) .transpose()? .ok_or_else(|| anyhow!("substate not provided"))?, - created_qc: value - .created_justify - .map(TryInto::try_into) - .transpose()? - .ok_or_else(|| anyhow!("created_justify not provided"))?, + // created_qc: value + // .created_justify + // .map(TryInto::try_into) + // .transpose()? + // .ok_or_else(|| anyhow!("created_justify not provided"))?, }) } } @@ -41,7 +41,7 @@ impl From for proto::rpc::SubstateCreatedProof { fn from(value: SubstateCreatedProof) -> Self { Self { substate: Some(value.substate.into()), - created_justify: Some((&value.created_qc).into()), + // created_justify: Some((&value.created_qc).into()), } } } @@ -53,11 +53,11 @@ impl TryFrom for SubstateDestroyedProof { Ok(Self { substate_id: SubstateId::from_bytes(&value.substate_id)?, version: value.version, - justify: value - .destroyed_justify - .map(TryInto::try_into) - .transpose()? - .ok_or_else(|| anyhow!("destroyed_justify not provided"))?, + // justify: value + // .destroyed_justify + // .map(TryInto::try_into) + // .transpose()? + // .ok_or_else(|| anyhow!("destroyed_justify not provided"))?, destroyed_by_transaction: value.destroyed_by_transaction.try_into()?, }) } @@ -68,7 +68,7 @@ impl From for proto::rpc::SubstateDestroyedProof { Self { substate_id: value.substate_id.to_bytes(), version: value.version, - destroyed_justify: Some((&value.justify).into()), + // destroyed_justify: Some((&value.justify).into()), destroyed_by_transaction: value.destroyed_by_transaction.as_bytes().to_vec(), } } diff --git a/dan_layer/p2p/src/proto.rs b/dan_layer/p2p/src/proto.rs index ea42c5b76..96214535c 100644 --- a/dan_layer/p2p/src/proto.rs +++ b/dan_layer/p2p/src/proto.rs @@ -34,6 +34,7 @@ pub mod network { } pub mod rpc { + #![allow(clippy::large_enum_variant)] include!(concat!(env!("OUT_DIR"), "/tari.dan.rpc.rs")); } diff --git a/dan_layer/rpc_state_sync/src/error.rs b/dan_layer/rpc_state_sync/src/error.rs index 3e297d828..e98542bdb 100644 --- a/dan_layer/rpc_state_sync/src/error.rs +++ b/dan_layer/rpc_state_sync/src/error.rs @@ -8,6 +8,7 @@ use tari_dan_storage::{ }; use tari_epoch_manager::EpochManagerError; use tari_rpc_framework::{RpcError, RpcStatus}; +use tari_state_tree::JmtStorageError; use tari_validator_node_rpc::ValidatorNodeRpcClientError; #[derive(Debug, thiserror::Error)] @@ -34,12 +35,27 @@ pub enum CommsRpcConsensusSyncError { StateTreeError(#[from] tari_state_tree::StateTreeError), } +impl CommsRpcConsensusSyncError { + pub fn error_at_remote(self) -> Result { + match &self { + CommsRpcConsensusSyncError::InvalidResponse(_) | CommsRpcConsensusSyncError::RpcError(_) => Err(self), + _ => Ok(self), + } + } +} + impl From for HotStuffError { fn from(value: CommsRpcConsensusSyncError) -> Self { HotStuffError::SyncError(value.into()) } } +impl From for CommsRpcConsensusSyncError { + fn from(value: JmtStorageError) -> Self { + Self::StateTreeError(value.into()) + } +} + impl From for CommsRpcConsensusSyncError { fn from(value: RpcStatus) -> Self { Self::RpcError(value.into()) diff --git a/dan_layer/rpc_state_sync/src/manager.rs b/dan_layer/rpc_state_sync/src/manager.rs index 0fa1c34ba..91f484d39 100644 --- a/dan_layer/rpc_state_sync/src/manager.rs +++ b/dan_layer/rpc_state_sync/src/manager.rs @@ -1,13 +1,16 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::collections::{HashMap, HashSet}; +use std::cmp; use anyhow::anyhow; use async_trait::async_trait; use futures::StreamExt; use log::*; -use tari_consensus::traits::{ConsensusSpec, SyncManager, SyncStatus}; +use tari_consensus::{ + hotstuff::substate_store::{ShardScopedTreeStoreReader, ShardScopedTreeStoreWriter}, + traits::{ConsensusSpec, SyncManager, SyncStatus}, +}; use tari_dan_common_types::{committee::Committee, optional::Optional, shard::Shard, Epoch, NodeHeight, PeerAddress}; use tari_dan_p2p::proto::rpc::{GetCheckpointRequest, GetCheckpointResponse, SyncStateRequest}; use tari_dan_storage::{ @@ -15,17 +18,22 @@ use tari_dan_storage::{ Block, EpochCheckpoint, LeafBlock, + QcId, StateTransition, + StateTransitionId, SubstateCreatedProof, SubstateDestroyedProof, SubstateRecord, SubstateUpdate, }, StateStore, + StateStoreReadTransaction, StateStoreWriteTransaction, StorageError, }; +use tari_engine_types::substate::hash_substate; use tari_epoch_manager::EpochManagerReader; +use tari_state_tree::{Hash, SpreadPrefixStateTree, SubstateTreeChange, Version}; use tari_transaction::VersionedSubstateId; use tari_validator_node_rpc::{ client::{TariValidatorNodeRpcClientFactory, ValidatorNodeClientFactory}, @@ -88,20 +96,40 @@ where TConsensusSpec: ConsensusSpec } } + #[allow(clippy::too_many_lines)] async fn start_state_sync( &self, client: &mut ValidatorNodeRpcClient, + shard: Shard, checkpoint: EpochCheckpoint, - ) -> Result<(), CommsRpcConsensusSyncError> { + ) -> Result { let current_epoch = self.epoch_manager.current_epoch().await?; - let committee_info = self.epoch_manager.get_local_committee_info(current_epoch).await?; - let last_state_transition_id = self.state_store.with_read_tx(|tx| StateTransition::get_last_id(tx))?; + let last_state_transition_id = self + .state_store + .with_read_tx(|tx| StateTransition::get_last_id(tx, shard)) + .optional()? + .unwrap_or_else(|| StateTransitionId::initial(shard)); + + let persisted_version = self + .state_store + .with_read_tx(|tx| tx.state_tree_versions_get_latest(shard))? + .unwrap_or(0); + if current_epoch == last_state_transition_id.epoch() { info!(target: LOG_TARGET, "๐Ÿ›œAlready up to date. No need to sync."); - return Ok(()); + return Ok(persisted_version); } + let mut current_version = persisted_version; + + // Minimum epoch we should request is 1 since Epoch(0) is the genesis epoch. + let last_state_transition_id = StateTransitionId::new( + cmp::max(last_state_transition_id.epoch(), Epoch(1)), + last_state_transition_id.shard(), + last_state_transition_id.seq(), + ); + info!( target: LOG_TARGET, "๐Ÿ›œSyncing from state transition {last_state_transition_id}" @@ -113,7 +141,6 @@ where TConsensusSpec: ConsensusSpec start_shard: last_state_transition_id.shard().as_u32(), start_seq: last_state_transition_id.seq(), current_epoch: current_epoch.as_u64(), - current_shard: committee_info.shard().as_u32(), }) .await?; @@ -121,20 +148,60 @@ where TConsensusSpec: ConsensusSpec let msg = match result { Ok(msg) => msg, Err(err) if err.is_not_found() => { - return Ok(()); + return Ok(current_version); }, Err(err) => { return Err(err.into()); }, }; - info!(target: LOG_TARGET, "๐Ÿ›œ Next state updates batch of size {}", msg.transitions.len()); + if msg.transitions.is_empty() { + return Err(CommsRpcConsensusSyncError::InvalidResponse(anyhow!( + "Received empty state transition batch." + ))); + } self.state_store.with_write_tx(|tx| { + let mut next_version = msg.transitions.first().expect("non-empty batch already checked").state_tree_version; + + info!( + target: LOG_TARGET, + "๐Ÿ›œ Next state updates batch of size {} (v{}-v{})", + msg.transitions.len(), + current_version, + msg.transitions.last().unwrap().state_tree_version, + ); + + let mut store = ShardScopedTreeStoreWriter::new(tx, shard); + let mut tree_changes = vec![]; + + for transition in msg.transitions { let transition = StateTransition::try_from(transition).map_err(CommsRpcConsensusSyncError::InvalidResponse)?; - info!(target: LOG_TARGET, "๐Ÿ›œ Applied state update {transition}"); + if transition.id.shard() != shard { + return Err(CommsRpcConsensusSyncError::InvalidResponse(anyhow!( + "Received state transition for shard {} which is not the expected shard {}.", + transition.id.shard(), + shard + ))); + } + + if transition.state_tree_version < current_version { + return Err(CommsRpcConsensusSyncError::InvalidResponse(anyhow!( + "Received state transition with version {} that is not monotonically increasing (expected \ + >= {})", + transition.state_tree_version, + persisted_version + ))); + } + + if transition.id.epoch().is_zero() { + return Err(CommsRpcConsensusSyncError::InvalidResponse(anyhow!( + "Received state transition with epoch 0." + ))); + } + if transition.id.epoch() >= current_epoch { return Err(CommsRpcConsensusSyncError::InvalidResponse(anyhow!( "Received state transition for epoch {} which is at or ahead of our current epoch {}.", @@ -143,31 +210,52 @@ where TConsensusSpec: ConsensusSpec ))); } - self.commit_update(tx, &checkpoint, transition)?; + let change = match &transition.update { + SubstateUpdate::Create(create) => SubstateTreeChange::Up { + id: create.substate.substate_id.clone(), + value_hash: hash_substate(&create.substate.substate_value, create.substate.version), + }, + SubstateUpdate::Destroy(destroy) => SubstateTreeChange::Down { + id: destroy.substate_id.clone(), + }, + }; + + info!(target: LOG_TARGET, "๐Ÿ›œ Applying state update {transition} (v{} to v{})", current_version, transition.state_tree_version); + if next_version != transition.state_tree_version { + let mut state_tree = SpreadPrefixStateTree::new(&mut store); + state_tree.put_substate_changes(Some(current_version).filter(|v| *v > 0), next_version, tree_changes.drain(..))?; + current_version = next_version; + next_version = transition.state_tree_version; + } + tree_changes.push(change); + + self.commit_update(store.transaction(), &checkpoint, transition)?; } - // let current_version = block.justify().block_height().as_u64(); - // let next_version = block.height().as_u64(); - // - // let changes = updates.iter().map(|update| match update { - // SubstateUpdate::Create(create) => SubstateTreeChange::Up { - // id: create.substate.substate_id.clone(), - // value_hash: hash_substate(&create.substate.substate_value, create.substate.version), - // }, - // SubstateUpdate::Destroy(destroy) => SubstateTreeChange::Down { - // id: destroy.substate_id.clone(), - // }, - // }); - // - // let mut store = ChainScopedTreeStore::new(epoch, shard, tx); - // let mut tree = tari_state_tree::SpreadPrefixStateTree::new(&mut store); - // let _state_root = tree.put_substate_changes(current_version, next_version, changes)?; + if !tree_changes.is_empty() { + let mut state_tree = SpreadPrefixStateTree::new(&mut store); + state_tree.put_substate_changes(Some(current_version).filter(|v| *v > 0), next_version, tree_changes.drain(..))?; + } + current_version = next_version; + + if current_version > 0 { + store.set_version(current_version)?; + } Ok::<_, CommsRpcConsensusSyncError>(()) })?; } - Ok(()) + Ok(current_version) + } + + fn get_state_root_for_shard(&self, shard: Shard, version: Version) -> Result { + self.state_store.with_read_tx(|tx| { + let mut store = ShardScopedTreeStoreReader::new(tx, shard); + let state_tree = SpreadPrefixStateTree::new(&mut store); + let root_hash = state_tree.get_root_hash(version)?; + Ok(root_hash) + }) } pub fn commit_update( @@ -177,7 +265,7 @@ where TConsensusSpec: ConsensusSpec transition: StateTransition, ) -> Result<(), StorageError> { match transition.update { - SubstateUpdate::Create(SubstateCreatedProof { substate, created_qc }) => { + SubstateUpdate::Create(SubstateCreatedProof { substate }) => { SubstateRecord::new( substate.substate_id, substate.version, @@ -187,14 +275,15 @@ where TConsensusSpec: ConsensusSpec NodeHeight(0), *checkpoint.block().id(), substate.created_by_transaction, - *created_qc.id(), + // TODO: correct QC ID + QcId::zero(), + // *created_qc.id(), ) .create(tx)?; }, SubstateUpdate::Destroy(SubstateDestroyedProof { substate_id, version, - justify, destroyed_by_transaction, }) => { SubstateRecord::destroy( @@ -204,37 +293,33 @@ where TConsensusSpec: ConsensusSpec transition.id.epoch(), // TODO checkpoint.block().height(), - justify.id(), + &QcId::zero(), &destroyed_by_transaction, )?; }, } + Ok(()) } async fn get_sync_committees( &self, current_epoch: Epoch, - ) -> Result>, CommsRpcConsensusSyncError> { + ) -> Result)>, CommsRpcConsensusSyncError> { // We are behind at least one epoch. // We get the current substate range, and we asks committees from previous epoch in this range to give us // data. - let local_shard = self.epoch_manager.get_local_committee_info(current_epoch).await?; - let range = local_shard.to_substate_address_range(); + let local_info = self.epoch_manager.get_local_committee_info(current_epoch).await?; let prev_epoch = current_epoch.saturating_sub(Epoch(1)); info!(target: LOG_TARGET,"Previous epoch is {}", prev_epoch); - let prev_num_committee = self.epoch_manager.get_num_committees(prev_epoch).await?; - info!(target: LOG_TARGET,"Previous num committee {}", prev_num_committee); - let start = range.start().to_shard(prev_num_committee); - let end = range.end().to_shard(prev_num_committee); - info!(target: LOG_TARGET,"Start: {}, End: {}", start, end); let committees = self .epoch_manager - .get_committees_by_shards( - prev_epoch, - (start.as_u32()..=end.as_u32()).map(Shard::from).collect::>(), - ) + .get_committees_by_shard_group(prev_epoch, local_info.shard_group()) .await?; + + // TODO: not strictly necessary to sort by shard but easier on the eyes in logs + let mut committees = committees.into_iter().collect::>(); + committees.sort_by_key(|(k, _)| *k); Ok(committees) } } @@ -276,7 +361,7 @@ where TConsensusSpec: ConsensusSpec + Send + Sync + 'static // Sync data from each committee in range of the committee we're joining. // NOTE: we don't have to worry about substates in address range because shard boundaries are fixed. for (shard, mut committee) in prev_epoch_committees { - info!(target: LOG_TARGET, "๐Ÿ›œSyncing state for shard {shard} for epoch {}", current_epoch.saturating_sub(Epoch(1))); + info!(target: LOG_TARGET, "๐Ÿ›œSyncing state for {shard} and {}", current_epoch.saturating_sub(Epoch(1))); committee.shuffle(); for (addr, public_key) in committee { if our_vn.public_key == public_key { @@ -316,14 +401,22 @@ where TConsensusSpec: ConsensusSpec + Send + Sync + 'static }; info!(target: LOG_TARGET, "๐Ÿ›œ Checkpoint: {checkpoint}"); - if let Err(err) = self.start_state_sync(&mut client, checkpoint).await { - warn!( - target: LOG_TARGET, - "โš ๏ธFailed to sync state from {addr}: {err}. Attempting another peer if available" - ); - last_error = Some(err); - continue; + match self.start_state_sync(&mut client, shard, checkpoint).await { + Ok(current_version) => { + // TODO: validate this MR against the checkpoint + let merkle_root = self.get_state_root_for_shard(shard, current_version)?; + info!(target: LOG_TARGET, "๐Ÿ›œSynced state for {shard} to v{current_version} with root {merkle_root}"); + }, + Err(err) => { + warn!( + target: LOG_TARGET, + "โš ๏ธFailed to sync state from {addr}: {err}. Attempting another peer if available" + ); + last_error = Some(err); + continue; + }, } + break; } } diff --git a/dan_layer/state_store_sqlite/migrations/2023-06-08-091819_create_state_store/up.sql b/dan_layer/state_store_sqlite/migrations/2023-06-08-091819_create_state_store/up.sql index d1f7e3e43..2913003cc 100644 --- a/dan_layer/state_store_sqlite/migrations/2023-06-08-091819_create_state_store/up.sql +++ b/dan_layer/state_store_sqlite/migrations/2023-06-08-091819_create_state_store/up.sql @@ -19,7 +19,7 @@ create table blocks network text not NULL, height bigint not NULL, epoch bigint not NULL, - shard integer not NULL, + shard_group integer not NULL, proposed_by text not NULL, qc_id text not NULL, command_count bigint not NULL, @@ -50,7 +50,7 @@ create table parked_blocks network text not NULL, height bigint not NULL, epoch bigint not NULL, - shard integer not NULL, + shard_group integer not NULL, proposed_by text not NULL, justify text not NULL, command_count bigint not NULL, @@ -85,6 +85,7 @@ create table block_diffs transaction_id text NOT NULL, substate_id text NOT NULL, version int NOT NULL, + shard int NOT NULL, -- Up or Down change text NOT NULL, -- NULL for Down @@ -309,14 +310,14 @@ CREATE TABLE missing_transactions CREATE TABLE foreign_proposals ( id integer not NULL primary key AUTOINCREMENT, - bucket int not NULL, + shard_group integer not NULL, block_id text not NULL, state text not NULL, proposed_height bigint NULL, transactions text not NULL, base_layer_block_height bigint not NULL, created_at timestamp not NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE (bucket, block_id) + UNIQUE (shard_group, block_id) ); CREATE TABLE foreign_send_counters @@ -337,31 +338,59 @@ CREATE TABLE foreign_receive_counters CREATE TABLE state_tree ( id integer not NULL primary key AUTOINCREMENT, - epoch bigint not NULL, shard int not NULL, key text not NULL, node text not NULL, is_stale boolean not null default '0' ); --- Scoping by epoch,shard -CREATE INDEX state_tree_idx_epoch_shard_key on state_tree (epoch, shard); +-- Scoping by shard +CREATE INDEX state_tree_idx_shard_key on state_tree (shard) WHERE is_stale = false; -- Duplicate keys are not allowed -CREATE UNIQUE INDEX state_tree_uniq_idx_key on state_tree (epoch, shard, key); +CREATE UNIQUE INDEX state_tree_uniq_idx_key on state_tree (shard, key) WHERE is_stale = false; -- filtering out or by is_stale is used in every query CREATE INDEX state_tree_idx_is_stale on state_tree (is_stale); +create table state_tree_shard_versions +( + id integer not null primary key AUTOINCREMENT, + shard integer not NULL, + version bigint not NULL, + created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE shard_group_state_tree +( + id integer not NULL primary key AUTOINCREMENT, + epoch bigint not NULL, + key text not NULL, + node text not NULL, + is_stale boolean not null default '0' +); + +-- Scoping by shard +CREATE INDEX shard_group_state_tree_idx_shard_key on shard_group_state_tree (epoch) WHERE is_stale = false; +-- Duplicate keys are not allowed +CREATE UNIQUE INDEX shard_group_state_tree_uniq_idx_key on shard_group_state_tree (epoch, key) WHERE is_stale = false; +-- filtering out or by is_stale is used in every query +CREATE INDEX shard_group_state_tree_idx_is_stale on shard_group_state_tree (is_stale); + +-- One entry per shard +CREATE UNIQUE INDEX state_tree_uniq_shard_versions_shard on state_tree_shard_versions (shard); + CREATE TABLE pending_state_tree_diffs ( id integer not NULL primary key AUTOINCREMENT, block_id text not NULL, block_height bigint not NULL, + shard integer not NULL, + version bigint not NULL, diff_json text not NULL, created_at timestamp not NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (block_id) REFERENCES blocks (block_id) ); -CREATE UNIQUE INDEX pending_state_tree_diffs_uniq_idx_block_id on pending_state_tree_diffs (block_id); +CREATE UNIQUE INDEX pending_state_tree_diffs_uniq_idx_block_id_shard on pending_state_tree_diffs (block_id, shard); -- An append-only store of state transitions CREATE TABLE state_transitions @@ -382,6 +411,8 @@ CREATE TABLE state_transitions created_at timestamp not NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (substate_address) REFERENCES substates (address) ); +CREATE UNIQUE INDEX state_transitions_shard_seq on state_transitions (shard, seq); +CREATE INDEX state_transitions_epoch on state_transitions (epoch); -- Debug Triggers CREATE TABLE transaction_pool_history diff --git a/dan_layer/state_store_sqlite/src/error.rs b/dan_layer/state_store_sqlite/src/error.rs index 1ff0d22d1..5dac59b96 100644 --- a/dan_layer/state_store_sqlite/src/error.rs +++ b/dan_layer/state_store_sqlite/src/error.rs @@ -35,10 +35,6 @@ pub enum SqliteStorageError { operation: &'static str, details: String, }, - #[error("[{operation}] One or more substates were are write locked")] - SubstatesWriteLocked { operation: &'static str }, - #[error("[{operation}] lock error: {details}")] - SubstatesUnlock { operation: &'static str, details: String }, } impl From for StorageError { diff --git a/dan_layer/state_store_sqlite/src/reader.rs b/dan_layer/state_store_sqlite/src/reader.rs index b4811894f..1b120aefb 100644 --- a/dan_layer/state_store_sqlite/src/reader.rs +++ b/dan_layer/state_store_sqlite/src/reader.rs @@ -3,6 +3,7 @@ use std::{ borrow::Borrow, + cmp, collections::{HashMap, HashSet}, marker::PhantomData, ops::RangeInclusive, @@ -14,11 +15,12 @@ use diesel::{ dsl, query_builder::SqlQuery, sql_query, - sql_types::{BigInt, Integer, Text}, + sql_types::{BigInt, Text}, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, + OptionalExtension, QueryDsl, QueryableByName, RunQueryDsl, @@ -29,7 +31,7 @@ use indexmap::IndexMap; use log::*; use serde::{de::DeserializeOwned, Serialize}; use tari_common_types::types::{FixedHash, PublicKey}; -use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, SubstateAddress}; +use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, ShardGroup, SubstateAddress}; use tari_dan_storage::{ consensus_models::{ Block, @@ -213,32 +215,25 @@ impl<'a, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'a> SqliteState /// Returns the blocks from the start_block (inclusive) to the end_block (inclusive). fn get_block_ids_between( &self, - epoch: Epoch, - shard: Shard, start_block: &BlockId, end_block: &BlockId, ) -> Result, SqliteStorageError> { - debug!(target: LOG_TARGET, "get_block_ids_between: {epoch} {shard} start: {start_block}, end: {end_block}"); + debug!(target: LOG_TARGET, "get_block_ids_between: start: {start_block}, end: {end_block}"); let block_ids = sql_query( r#" WITH RECURSIVE tree(bid, parent) AS ( - SELECT block_id, parent_block_id FROM blocks where block_id = ? AND epoch = ? AND shard = ? + SELECT block_id, parent_block_id FROM blocks where block_id = ? UNION ALL SELECT block_id, parent_block_id FROM blocks JOIN tree ON block_id = tree.parent AND tree.bid != ? - WHERE epoch = ? AND shard = ? LIMIT 1000 ) SELECT bid FROM tree"#, ) .bind::(serialize_hex(end_block)) - .bind::(epoch.as_u64() as i64) - .bind::(shard.as_u32() as i32) .bind::(serialize_hex(start_block)) - .bind::(epoch.as_u64() as i64) - .bind::(shard.as_u32() as i32) .load_iter::(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "get_block_ids_that_change_state_between", @@ -255,7 +250,7 @@ impl<'a, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'a> SqliteState .collect() } - fn get_block_ids_that_change_state_between( + pub(crate) fn get_block_ids_that_change_state_between( &self, start_block: &BlockId, end_block: &BlockId, @@ -306,9 +301,21 @@ impl<'a, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'a> SqliteState Ok(count as u64) } - fn get_commit_block_id(&self) -> Result { + pub(crate) fn get_commit_block_id(&self) -> Result { + use crate::schema::blocks; + let locked = self.locked_block_get()?; - Ok(*locked.get_block(self)?.parent()) + + let block_id = blocks::table + .select(blocks::parent_block_id) + .filter(blocks::block_id.eq(serialize_hex(locked.block_id))) + .first::(self.connection()) + .map_err(|e| SqliteStorageError::DieselError { + operation: "get_commit_block_id", + source: e, + })?; + + deserialize_hex_try_from(&block_id) } pub fn substates_count(&self) -> Result { @@ -324,6 +331,32 @@ impl<'a, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'a> SqliteState Ok(count as u64) } + + pub fn blocks_get_tip(&self, epoch: Epoch, shard_group: ShardGroup) -> Result { + use crate::schema::{blocks, quorum_certificates}; + + let (block, qc) = blocks::table + .left_join(quorum_certificates::table.on(blocks::qc_id.eq(quorum_certificates::qc_id))) + .select((blocks::all_columns, quorum_certificates::all_columns.nullable())) + .filter(blocks::epoch.eq(epoch.as_u64() as i64)) + .filter(blocks::shard_group.eq(shard_group.encode_as_u32() as i32)) + .order_by(blocks::height.desc()) + .first::<(sql_models::Block, Option)>(self.connection()) + .map_err(|e| SqliteStorageError::DieselError { + operation: "blocks_get_tip", + source: e, + })?; + + let qc = qc.ok_or_else(|| SqliteStorageError::DbInconsistency { + operation: "blocks_get_tip", + details: format!( + "block {} references non-existent quorum certificate {}", + block.block_id, block.qc_id + ), + })?; + + block.try_convert(qc) + } } impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStoreReadTransaction @@ -433,7 +466,7 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor use crate::schema::foreign_proposals; let foreign_proposals = foreign_proposals::table - .filter(foreign_proposals::bucket.eq(foreign_proposal.shard.as_u32() as i32)) + .filter(foreign_proposals::shard_group.eq(foreign_proposal.shard_group.encode_as_u32() as i32)) .filter(foreign_proposals::block_id.eq(serialize_hex(foreign_proposal.block_id))) .filter(foreign_proposals::transactions.eq(serialize_json(&foreign_proposal.transactions)?)) .filter(foreign_proposals::base_layer_block_height.eq(foreign_proposal.base_layer_block_height as i64)) @@ -645,7 +678,7 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor use crate::schema::transaction_executions; // TODO: This gets slower as the chain progresses. - let block_ids = self.get_block_ids_that_change_state_between(&BlockId::zero(), from_block_id)?; + let block_ids = self.get_block_ids_between(&BlockId::zero(), from_block_id)?; let execution = transaction_executions::table .filter(transaction_executions::transaction_id.eq(serialize_hex(tx_id))) @@ -684,40 +717,19 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor block.try_convert(qc) } - fn blocks_get_tip(&self, epoch: Epoch, shard: Shard) -> Result { - use crate::schema::{blocks, quorum_certificates}; - - let (block, qc) = blocks::table - .left_join(quorum_certificates::table.on(blocks::qc_id.eq(quorum_certificates::qc_id))) - .select((blocks::all_columns, quorum_certificates::all_columns.nullable())) - .filter(blocks::epoch.eq(epoch.as_u64() as i64)) - .filter(blocks::shard.eq(shard.as_u32() as i32)) - .order_by(blocks::height.desc()) - .first::<(sql_models::Block, Option)>(self.connection()) - .map_err(|e| SqliteStorageError::DieselError { - operation: "blocks_get_tip", - source: e, - })?; - - let qc = qc.ok_or_else(|| SqliteStorageError::DbInconsistency { - operation: "blocks_get_tip", - details: format!( - "block {} references non-existent quorum certificate {}", - block.block_id, block.qc_id - ), - })?; - - block.try_convert(qc) - } - - fn blocks_get_last_n_in_epoch(&self, n: usize, epoch: Epoch, shard: Shard) -> Result, StorageError> { + fn blocks_get_last_n_in_epoch( + &self, + n: usize, + epoch: Epoch, + shard_group: ShardGroup, + ) -> Result, StorageError> { use crate::schema::{blocks, quorum_certificates}; let blocks = blocks::table .left_join(quorum_certificates::table.on(blocks::qc_id.eq(quorum_certificates::qc_id))) .select((blocks::all_columns, quorum_certificates::all_columns.nullable())) .filter(blocks::epoch.eq(epoch.as_u64() as i64)) - .filter(blocks::shard.eq(shard.as_u32() as i32)) + .filter(blocks::shard_group.eq(shard_group.encode_as_u32() as i32)) .filter(blocks::is_committed.eq(true)) .order_by(blocks::height.desc()) .limit(n as i64) @@ -746,14 +758,14 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor fn blocks_get_all_between( &self, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, start_block_id_exclusive: &BlockId, end_block_id_inclusive: &BlockId, include_dummy_blocks: bool, ) -> Result, StorageError> { use crate::schema::{blocks, quorum_certificates}; - let block_ids = self.get_block_ids_between(epoch, shard, start_block_id_exclusive, end_block_id_inclusive)?; + let block_ids = self.get_block_ids_between(start_block_id_exclusive, end_block_id_inclusive)?; if block_ids.is_empty() { return Ok(vec![]); } @@ -770,7 +782,7 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor let results = query .filter(blocks::epoch.eq(epoch.as_u64() as i64)) - .filter(blocks::shard.eq(shard.as_u32() as i32)) + .filter(blocks::shard_group.eq(shard_group.encode_as_u32() as i32)) .order_by(blocks::height.asc()) .get_results::<(sql_models::Block, Option)>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { @@ -1958,22 +1970,20 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor fn pending_state_tree_diffs_get_all_up_to_commit_block( &self, - epoch: Epoch, - shard: Shard, block_id: &BlockId, - ) -> Result, StorageError> { + ) -> Result>, StorageError> { use crate::schema::pending_state_tree_diffs; // Get the last committed block let committed_block_id = self.get_commit_block_id()?; - let block_ids = self.get_block_ids_between(epoch, shard, &committed_block_id, block_id)?; + let block_ids = self.get_block_ids_that_change_state_between(&committed_block_id, block_id)?; if block_ids.is_empty() { - return Ok(Vec::new()); + return Ok(HashMap::new()); } - let diffs = pending_state_tree_diffs::table + let diff_recs = pending_state_tree_diffs::table .filter(pending_state_tree_diffs::block_id.eq_any(block_ids)) .order_by(pending_state_tree_diffs::block_height.asc()) .get_results::(self.connection()) @@ -1982,7 +1992,20 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor source: e, })?; - diffs.into_iter().map(TryInto::try_into).collect() + let mut diffs = HashMap::new(); + for diff in diff_recs { + let shard = Shard::from(diff.shard as u32); + let diff = PendingStateTreeDiff::try_from(diff)?; + diffs + .entry(shard) + .or_insert_with(Vec::new)//PendingStateTreeDiff::default) + .push(diff); + } + // diffs + // .into_iter() + // .map(|diff| Ok((Shard::from(diff.shard as u32), diff.try_into()?))) + // .collect() + Ok(diffs) } fn state_transitions_get_n_after( @@ -1993,25 +2016,29 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor ) -> Result, StorageError> { use crate::schema::{state_transitions, substates}; - let start_id = state_transitions::table - .select(state_transitions::id) - .filter(state_transitions::epoch.eq(id.epoch().as_u64() as i64)) + // Never return epoch 0 state transitions + let min_epoch = Some(id.epoch().as_u64()).filter(|e| *e > 0).unwrap_or(1) as i64; + let (start_id, seq) = state_transitions::table + .select((state_transitions::id, state_transitions::seq)) + .filter(state_transitions::epoch.ge(min_epoch)) .filter(state_transitions::shard.eq(id.shard().as_u32() as i32)) - .filter(state_transitions::seq.eq(0i64)) - .order_by(state_transitions::id.asc()) - .first::(self.connection()) + .order_by(state_transitions::seq.asc()) + .first::<(i32, i64)>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "state_transitions_get_n_after", source: e, })?; - let start_id = start_id + (id.seq() as i32); + let offset = cmp::max(id.seq() as i64, seq); + let start_id = + start_id + i32::try_from(offset).expect("(likely invalid) seq no for transition is too large for SQLite"); let transitions = state_transitions::table .left_join(substates::table.on(state_transitions::substate_address.eq(substates::address))) .select((state_transitions::all_columns, substates::all_columns.nullable())) - .filter(state_transitions::id.gt(start_id)) + .filter(state_transitions::id.ge(start_id)) .filter(state_transitions::epoch.lt(end_epoch.as_u64() as i64)) + .filter(state_transitions::shard.eq(id.shard().as_u32() as i32)) .limit(n as i64) .get_results::<(sql_models::StateTransition, Option)>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { @@ -2031,36 +2058,31 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor .collect() } - fn state_transitions_get_last_id(&self) -> Result { + fn state_transitions_get_last_id(&self, shard: Shard) -> Result { use crate::schema::state_transitions; - let (seq, epoch, shard) = state_transitions::table - .select(( - state_transitions::seq, - state_transitions::epoch, - state_transitions::shard, - )) + let (seq, epoch) = state_transitions::table + .select((state_transitions::seq, state_transitions::epoch)) + .filter(state_transitions::shard.eq(shard.as_u32() as i32)) .order_by(state_transitions::epoch.desc()) .then_order_by(state_transitions::seq.desc()) - .first::<(i64, i64, i32)>(self.connection()) + .first::<(i64, i64)>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "state_transitions_get_last_id", source: e, })?; let epoch = Epoch(epoch as u64); - let shard = Shard::from(shard as u32); let seq = seq as u64; Ok(StateTransitionId::new(epoch, shard, seq)) } - fn state_tree_nodes_get(&self, epoch: Epoch, shard: Shard, key: &NodeKey) -> Result, StorageError> { + fn state_tree_nodes_get(&self, shard: Shard, key: &NodeKey) -> Result, StorageError> { use crate::schema::state_tree; let node = state_tree::table .select(state_tree::node) - .filter(state_tree::epoch.eq(epoch.as_u64() as i64)) .filter(state_tree::shard.eq(shard.as_u32() as i32)) .filter(state_tree::key.eq(key.to_string())) .filter(state_tree::is_stale.eq(false)) @@ -2076,6 +2098,23 @@ impl<'tx, TAddr: NodeAddressable + Serialize + DeserializeOwned + 'tx> StateStor Ok(node.into_node()) } + + fn state_tree_versions_get_latest(&self, shard: Shard) -> Result, StorageError> { + use crate::schema::state_tree_shard_versions; + + let version = state_tree_shard_versions::table + .select(state_tree_shard_versions::version) + .filter(state_tree_shard_versions::shard.eq(shard.as_u32() as i32)) + .order_by(state_tree_shard_versions::version.desc()) + .first::(self.connection()) + .optional() + .map_err(|e| SqliteStorageError::DieselError { + operation: "state_tree_versions_get_latest", + source: e, + })?; + + Ok(version.map(|v| v as Version)) + } } #[derive(QueryableByName)] diff --git a/dan_layer/state_store_sqlite/src/schema.rs b/dan_layer/state_store_sqlite/src/schema.rs index 4cbdbed54..c69b9ff95 100644 --- a/dan_layer/state_store_sqlite/src/schema.rs +++ b/dan_layer/state_store_sqlite/src/schema.rs @@ -7,6 +7,7 @@ diesel::table! { transaction_id -> Text, substate_id -> Text, version -> Integer, + shard -> Integer, change -> Text, state -> Nullable, created_at -> Timestamp, @@ -22,7 +23,7 @@ diesel::table! { network -> Text, height -> BigInt, epoch -> BigInt, - shard -> Integer, + shard_group -> Integer, proposed_by -> Text, qc_id -> Text, command_count -> BigInt, @@ -44,7 +45,7 @@ diesel::table! { diesel::table! { foreign_proposals (id) { id -> Integer, - bucket -> Integer, + shard_group -> Integer, block_id -> Text, state -> Text, proposed_height -> Nullable, @@ -164,7 +165,7 @@ diesel::table! { network -> Text, height -> BigInt, epoch -> BigInt, - shard -> Integer, + shard_group -> Integer, proposed_by -> Text, justify -> Text, command_count -> BigInt, @@ -185,6 +186,8 @@ diesel::table! { id -> Integer, block_id -> Text, block_height -> BigInt, + shard -> Integer, + version -> BigInt, diff_json -> Text, created_at -> Timestamp, } @@ -219,7 +222,6 @@ diesel::table! { diesel::table! { state_tree (id) { id -> Integer, - epoch -> BigInt, shard -> Integer, key -> Text, node -> Text, @@ -227,6 +229,15 @@ diesel::table! { } } +diesel::table! { + state_tree_shard_versions (id) { + id -> Integer, + shard -> Integer, + version -> BigInt, + created_at -> Timestamp, + } +} + diesel::table! { substate_locks (id) { id -> Integer, @@ -305,8 +316,8 @@ diesel::table! { original_decision -> Text, local_decision -> Nullable, remote_decision -> Nullable, - evidence -> Text, - transaction_fee -> BigInt, + evidence -> Nullable, + transaction_fee -> Nullable, leader_fee -> Nullable, global_exhaust_burn -> Nullable, stage -> Text, @@ -387,6 +398,7 @@ diesel::allow_tables_to_appear_in_same_query!( quorum_certificates, state_transitions, state_tree, + state_tree_shard_versions, substate_locks, substates, transaction_executions, diff --git a/dan_layer/state_store_sqlite/src/sql_models/block.rs b/dan_layer/state_store_sqlite/src/sql_models/block.rs index 3440a22eb..aa66de130 100644 --- a/dan_layer/state_store_sqlite/src/sql_models/block.rs +++ b/dan_layer/state_store_sqlite/src/sql_models/block.rs @@ -3,7 +3,7 @@ use diesel::{Queryable, QueryableByName}; use tari_common_types::types::PublicKey; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{Epoch, NodeHeight, ShardGroup}; use tari_dan_storage::{consensus_models, StorageError}; use tari_utilities::byte_array::ByteArray; use time::PrimitiveDateTime; @@ -23,7 +23,7 @@ pub struct Block { pub network: String, pub height: i64, pub epoch: i64, - pub shard: i32, + pub shard_group: i32, pub proposed_by: String, pub qc_id: String, pub command_count: i64, @@ -55,7 +55,12 @@ impl Block { qc.try_into()?, NodeHeight(self.height as u64), Epoch(self.epoch as u64), - Shard::from(self.shard as u32), + ShardGroup::decode_from_u32(self.shard_group as u32).ok_or_else(|| StorageError::DataInconsistency { + details: format!( + "Block id={} shard_group ({}) is not a valid", + self.id, self.shard_group as u32 + ), + })?, PublicKey::from_canonical_bytes(&deserialize_hex(&self.proposed_by)?).map_err(|_| { StorageError::DecodingError { operation: "try_convert", @@ -89,7 +94,7 @@ pub struct ParkedBlock { pub network: String, pub height: i64, pub epoch: i64, - pub shard: i32, + pub shard_group: i32, pub proposed_by: String, pub justify: String, pub command_count: i64, @@ -120,7 +125,12 @@ impl TryFrom for consensus_models::Block { deserialize_json(&value.justify)?, NodeHeight(value.height as u64), Epoch(value.epoch as u64), - Shard::from(value.shard as u32), + ShardGroup::decode_from_u32(value.shard_group as u32).ok_or_else(|| StorageError::DataInconsistency { + details: format!( + "Block at id={} shard_group ({}) is not a valid", + value.id, value.shard_group as u32 + ), + })?, PublicKey::from_canonical_bytes(&deserialize_hex(&value.proposed_by)?).map_err(|_| { StorageError::DecodingError { operation: "try_convert", diff --git a/dan_layer/state_store_sqlite/src/sql_models/block_diff.rs b/dan_layer/state_store_sqlite/src/sql_models/block_diff.rs index fee11a69c..ff31e9b7a 100644 --- a/dan_layer/state_store_sqlite/src/sql_models/block_diff.rs +++ b/dan_layer/state_store_sqlite/src/sql_models/block_diff.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: BSD-3-Clause use diesel::Queryable; +use tari_dan_common_types::shard::Shard; use tari_dan_storage::{consensus_models, consensus_models::BlockId, StorageError}; use tari_transaction::VersionedSubstateId; use time::PrimitiveDateTime; @@ -15,6 +16,7 @@ pub struct BlockDiff { pub transaction_id: String, pub substate_id: String, pub version: i32, + pub shard: i32, pub change: String, pub state: Option, pub created_at: PrimitiveDateTime, @@ -35,6 +37,7 @@ impl BlockDiff { })?; let id = VersionedSubstateId::new(substate_id, d.version as u32); let transaction_id = deserialize_hex_try_from(&d.transaction_id)?; + let shard = Shard::from(d.shard as u32); match d.change.as_str() { "Up" => { let state = d.state.ok_or(StorageError::DataInconsistency { @@ -42,11 +45,16 @@ impl BlockDiff { })?; Ok(consensus_models::SubstateChange::Up { id, + shard, transaction_id, substate: deserialize_json(&state)?, }) }, - "Down" => Ok(consensus_models::SubstateChange::Down { id, transaction_id }), + "Down" => Ok(consensus_models::SubstateChange::Down { + id, + transaction_id, + shard, + }), _ => Err(StorageError::DataInconsistency { details: format!("Invalid block diff change type: {}", d.change), }), diff --git a/dan_layer/state_store_sqlite/src/sql_models/bookkeeping.rs b/dan_layer/state_store_sqlite/src/sql_models/bookkeeping.rs index b30bb24a2..03431897a 100644 --- a/dan_layer/state_store_sqlite/src/sql_models/bookkeeping.rs +++ b/dan_layer/state_store_sqlite/src/sql_models/bookkeeping.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: BSD-3-Clause use diesel::Queryable; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{Epoch, NodeHeight, ShardGroup}; use tari_dan_storage::{ consensus_models::{self, QuorumDecision}, StorageError, @@ -40,7 +40,7 @@ impl TryFrom for consensus_models::HighQc { #[derive(Debug, Clone, Queryable)] pub struct ForeignProposal { pub id: i32, - pub bucket: i32, + pub shard_group: i32, pub block_id: String, pub state: String, pub mined_at: Option, @@ -54,7 +54,11 @@ impl TryFrom for consensus_models::ForeignProposal { fn try_from(value: ForeignProposal) -> Result { Ok(Self { - shard: Shard::from(value.bucket as u32), + shard_group: ShardGroup::decode_from_u32(value.shard_group as u32).ok_or_else(|| { + StorageError::DataInconsistency { + details: format!("Invalid shard group: {}", value.shard_group), + } + })?, block_id: deserialize_hex_try_from(&value.block_id)?, state: parse_from_string(&value.state)?, proposed_height: value.mined_at.map(|mined_at| NodeHeight(mined_at as u64)), diff --git a/dan_layer/state_store_sqlite/src/sql_models/pending_state_tree_diff.rs b/dan_layer/state_store_sqlite/src/sql_models/pending_state_tree_diff.rs index de87dcc1e..ab5afafa0 100644 --- a/dan_layer/state_store_sqlite/src/sql_models/pending_state_tree_diff.rs +++ b/dan_layer/state_store_sqlite/src/sql_models/pending_state_tree_diff.rs @@ -5,13 +5,15 @@ use diesel::Queryable; use tari_dan_storage::{consensus_models, StorageError}; use time::PrimitiveDateTime; -use crate::serialization::{deserialize_hex_try_from, deserialize_json}; +use crate::serialization::deserialize_json; #[derive(Debug, Clone, Queryable)] pub struct PendingStateTreeDiff { pub id: i32, pub block_id: String, pub block_height: i64, + pub shard: i32, + pub version: i64, pub diff: String, pub created_at: PrimitiveDateTime, } @@ -20,9 +22,8 @@ impl TryFrom for consensus_models::PendingStateTreeDiff { type Error = StorageError; fn try_from(value: PendingStateTreeDiff) -> Result { - let block_id = deserialize_hex_try_from(&value.block_id)?; - let block_height = value.block_height as u64; let diff = deserialize_json(&value.diff)?; - Ok(Self::new(block_id, block_height.into(), diff)) + let version = value.version as u64; + Ok(Self::load(version, diff)) } } diff --git a/dan_layer/state_store_sqlite/src/sql_models/state_transition.rs b/dan_layer/state_store_sqlite/src/sql_models/state_transition.rs index c0daef42e..734dcf502 100644 --- a/dan_layer/state_store_sqlite/src/sql_models/state_transition.rs +++ b/dan_layer/state_store_sqlite/src/sql_models/state_transition.rs @@ -5,14 +5,7 @@ use diesel::Queryable; use tari_dan_common_types::{shard::Shard, Epoch}; use tari_dan_storage::{ consensus_models, - consensus_models::{ - QuorumCertificate, - StateTransitionId, - SubstateCreatedProof, - SubstateData, - SubstateDestroyedProof, - SubstateUpdate, - }, + consensus_models::{StateTransitionId, SubstateCreatedProof, SubstateData, SubstateDestroyedProof, SubstateUpdate}, StorageError, }; use time::PrimitiveDateTime; @@ -42,18 +35,14 @@ impl StateTransition { let shard = Shard::from(self.shard as u32); let update = match self.transition.as_str() { - "UP" => { - SubstateUpdate::Create(SubstateCreatedProof { - substate: SubstateData { - substate_id: substate.substate_id, - version: substate.version, - substate_value: substate.substate_value, - created_by_transaction: substate.created_by_transaction, - }, - // TODO - created_qc: QuorumCertificate::genesis(), - }) - }, + "UP" => SubstateUpdate::Create(SubstateCreatedProof { + substate: SubstateData { + substate_id: substate.substate_id, + version: substate.version, + substate_value: substate.substate_value, + created_by_transaction: substate.created_by_transaction, + }, + }), "DOWN" => { if !substate.is_destroyed() { return Err(StorageError::DataInconsistency { @@ -68,8 +57,6 @@ impl StateTransition { destroyed_by_transaction: substate.destroyed().unwrap().by_transaction, substate_id: substate.substate_id, version: substate.version, - // TODO - justify: QuorumCertificate::genesis(), }) }, _ => { diff --git a/dan_layer/state_store_sqlite/src/tree_store.rs b/dan_layer/state_store_sqlite/src/tree_store.rs deleted file mode 100644 index d728d7f85..000000000 --- a/dan_layer/state_store_sqlite/src/tree_store.rs +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2024 The Tari Project -// SPDX-License-Identifier: BSD-3-Clause - -use std::ops::Deref; - -use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl}; -use tari_state_tree::{Node, NodeKey, StaleTreeNode, TreeNode, TreeStoreReader, TreeStoreWriter, Version}; - -use crate::{reader::SqliteStateStoreReadTransaction, writer::SqliteStateStoreWriteTransaction}; - -impl<'a, TAddr> TreeStoreReader for SqliteStateStoreReadTransaction<'a, TAddr> { - fn get_node(&self, key: &NodeKey) -> Result, tari_state_tree::JmtStorageError> { - use crate::schema::state_tree; - - let node = state_tree::table - .select(state_tree::node) - .filter(state_tree::key.eq(key.to_string())) - .filter(state_tree::is_stale.eq(false)) - .first::(self.connection()) - .optional() - .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))? - .ok_or_else(|| tari_state_tree::JmtStorageError::NotFound(key.clone()))?; - - let node = serde_json::from_str::(&node) - .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))?; - - Ok(node.into_node()) - } -} - -impl<'a, TAddr> TreeStoreReader for SqliteStateStoreWriteTransaction<'a, TAddr> { - fn get_node(&self, key: &NodeKey) -> Result, tari_state_tree::JmtStorageError> { - self.deref().get_node(key) - } -} - -impl<'a, TAddr> TreeStoreWriter for SqliteStateStoreWriteTransaction<'a, TAddr> { - fn insert_node(&mut self, key: NodeKey, node: Node) -> Result<(), tari_state_tree::JmtStorageError> { - use crate::schema::state_tree; - - let node = TreeNode::new_latest(node); - let node = serde_json::to_string(&node) - .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))?; - - let values = (state_tree::key.eq(key.to_string()), state_tree::node.eq(node)); - diesel::insert_into(state_tree::table) - .values(&values) - .on_conflict(state_tree::key) - .do_update() - .set(values.clone()) - .execute(self.connection()) - .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))?; - - Ok(()) - } - - fn record_stale_tree_node(&mut self, node: StaleTreeNode) -> Result<(), tari_state_tree::JmtStorageError> { - use crate::schema::state_tree; - let key = node.as_node_key(); - diesel::update(state_tree::table) - .filter(state_tree::key.eq(key.to_string())) - .set(state_tree::is_stale.eq(true)) - .execute(self.connection()) - .optional() - .map_err(|e| tari_state_tree::JmtStorageError::UnexpectedError(e.to_string()))? - .ok_or_else(|| tari_state_tree::JmtStorageError::NotFound(key.clone()))?; - - Ok(()) - } -} diff --git a/dan_layer/state_store_sqlite/src/writer.rs b/dan_layer/state_store_sqlite/src/writer.rs index 55aa88ab0..5ba56000c 100644 --- a/dan_layer/state_store_sqlite/src/writer.rs +++ b/dan_layer/state_store_sqlite/src/writer.rs @@ -8,11 +8,13 @@ use diesel::{ sql_types::Text, AsChangeset, ExpressionMethods, + NullableExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, SqliteConnection, }; +use indexmap::IndexMap; use log::*; use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight}; use tari_dan_storage::{ @@ -42,6 +44,7 @@ use tari_dan_storage::{ TransactionPoolStage, TransactionPoolStatusUpdate, TransactionRecord, + VersionedStateHashTreeDiff, Vote, }, StateStoreReadTransaction, @@ -151,7 +154,7 @@ impl<'a, TAddr: NodeAddressable> SqliteStateStoreWriteTransaction<'a, TAddr> { parked_blocks::merkle_root.eq(block.merkle_root().to_string()), parked_blocks::height.eq(block.height().as_u64() as i64), parked_blocks::epoch.eq(block.epoch().as_u64() as i64), - parked_blocks::shard.eq(block.shard().as_u32() as i32), + parked_blocks::shard_group.eq(block.shard_group().encode_as_u32() as i32), parked_blocks::proposed_by.eq(serialize_hex(block.proposed_by().as_bytes())), parked_blocks::command_count.eq(block.commands().len() as i64), parked_blocks::commands.eq(serialize_json(block.commands())?), @@ -202,7 +205,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta blocks::network.eq(block.network().to_string()), blocks::height.eq(block.height().as_u64() as i64), blocks::epoch.eq(block.epoch().as_u64() as i64), - blocks::shard.eq(block.shard().as_u32() as i32), + blocks::shard_group.eq(block.shard_group().encode_as_u32() as i32), blocks::proposed_by.eq(serialize_hex(block.proposed_by().as_bytes())), blocks::command_count.eq(block.commands().len() as i64), blocks::commands.eq(serialize_json(block.commands())?), @@ -290,6 +293,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta block_diffs::transaction_id.eq(serialize_hex(ch.transaction_id())), block_diffs::substate_id.eq(ch.versioned_substate_id().substate_id().to_string()), block_diffs::version.eq(ch.versioned_substate_id().version() as i32), + block_diffs::shard.eq(ch.shard().as_u32() as i32), block_diffs::change.eq(ch.as_change_string()), block_diffs::state.eq(ch.substate().map(serialize_json).transpose()?), )) @@ -520,7 +524,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta use crate::schema::foreign_proposals; let values = ( - foreign_proposals::bucket.eq(foreign_proposal.shard.as_u32() as i32), + foreign_proposals::shard_group.eq(foreign_proposal.shard_group.encode_as_u32() as i32), foreign_proposals::block_id.eq(serialize_hex(foreign_proposal.block_id)), foreign_proposals::state.eq(foreign_proposal.state.to_string()), foreign_proposals::proposed_height.eq(foreign_proposal.proposed_height.map(|h| h.as_u64() as i64)), @@ -529,10 +533,10 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta ); diesel::insert_into(foreign_proposals::table) - .values(&values) - .on_conflict((foreign_proposals::bucket, foreign_proposals::block_id)) + .values(values.clone()) + .on_conflict((foreign_proposals::shard_group, foreign_proposals::block_id)) .do_update() - .set(values.clone()) + .set(values) .execute(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "foreign_proposal_set", @@ -545,7 +549,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta use crate::schema::foreign_proposals; diesel::delete(foreign_proposals::table) - .filter(foreign_proposals::bucket.eq(foreign_proposal.shard.as_u32() as i32)) + .filter(foreign_proposals::shard_group.eq(foreign_proposal.shard_group.encode_as_u32() as i32)) .filter(foreign_proposals::block_id.eq(serialize_hex(foreign_proposal.block_id))) .execute(self.connection()) .map_err(|e| SqliteStorageError::DieselError { @@ -1350,7 +1354,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta let seq = state_transitions::table .select(dsl::max(state_transitions::seq)) - .filter(state_transitions::epoch.eq(substate.created_at_epoch.as_u64() as i64)) + .filter(state_transitions::shard.eq(substate.created_by_shard.as_u32() as i32)) .first::>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "substates_create", @@ -1358,6 +1362,8 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta })?; let next_seq = seq.map(|s| s + 1).unwrap_or(0); + // This means that we MUST do the state tree updates before inserting substates + let version = self.state_tree_versions_get_latest(substate.created_by_shard)?; let values = ( state_transitions::seq.eq(next_seq), state_transitions::epoch.eq(substate.created_at_epoch.as_u64() as i64), @@ -1367,7 +1373,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta state_transitions::version.eq(substate.version as i32), state_transitions::transition.eq("UP"), state_transitions::state_hash.eq(serialize_hex(substate.state_hash)), - state_transitions::state_version.eq(substate.created_height.as_u64() as i64), + state_transitions::state_version.eq(version.unwrap_or(0) as i64), ); diesel::insert_into(state_transitions::table) @@ -1414,7 +1420,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta let seq = state_transitions::table .select(dsl::max(state_transitions::seq)) - .filter(state_transitions::epoch.eq(epoch.as_u64() as i64)) + .filter(state_transitions::shard.eq(shard.as_u32() as i32)) .first::>(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "substates_create", @@ -1422,6 +1428,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta })?; let next_seq = seq.map(|s| s + 1).unwrap_or(0); + let version = self.state_tree_versions_get_latest(shard)?; let values = ( state_transitions::seq.eq(next_seq), state_transitions::epoch.eq(epoch.as_u64() as i64), @@ -1430,7 +1437,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta state_transitions::substate_id.eq(versioned_substate_id.substate_id.to_string()), state_transitions::version.eq(versioned_substate_id.version as i32), state_transitions::transition.eq("DOWN"), - state_transitions::state_version.eq(destroyed_block_height.as_u64() as i64), + state_transitions::state_version.eq(version.unwrap_or(0) as i64), ); diesel::insert_into(state_transitions::table) @@ -1447,35 +1454,54 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta fn pending_state_tree_diffs_remove_by_block( &mut self, block_id: &BlockId, - ) -> Result { + ) -> Result>, StorageError> { use crate::schema::pending_state_tree_diffs; - let diff = pending_state_tree_diffs::table + let diff_recs = pending_state_tree_diffs::table .filter(pending_state_tree_diffs::block_id.eq(serialize_hex(block_id))) - .first::(self.connection()) + .order_by(pending_state_tree_diffs::block_height.asc()) + .get_results::(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "pending_state_tree_diffs_remove_by_block", source: e, })?; diesel::delete(pending_state_tree_diffs::table) - .filter(pending_state_tree_diffs::id.eq(diff.id)) + .filter(pending_state_tree_diffs::id.eq_any(diff_recs.iter().map(|d| d.id))) .execute(self.connection()) .map_err(|e| SqliteStorageError::DieselError { operation: "pending_state_tree_diffs_remove_by_block", source: e, })?; - diff.try_into() + let mut diffs = IndexMap::new(); + for diff in diff_recs { + let shard = Shard::from(diff.shard as u32); + let diff = PendingStateTreeDiff::try_from(diff)?; + diffs.entry(shard).or_insert_with(Vec::new).push(diff); + } + + Ok(diffs) } - fn pending_state_tree_diffs_insert(&mut self, pending_diff: &PendingStateTreeDiff) -> Result<(), StorageError> { - use crate::schema::pending_state_tree_diffs; + fn pending_state_tree_diffs_insert( + &mut self, + block_id: BlockId, + shard: Shard, + diff: VersionedStateHashTreeDiff, + ) -> Result<(), StorageError> { + use crate::schema::{blocks, pending_state_tree_diffs}; let insert = ( - pending_state_tree_diffs::block_id.eq(serialize_hex(pending_diff.block_id)), - pending_state_tree_diffs::block_height.eq(pending_diff.block_height.as_u64() as i64), - pending_state_tree_diffs::diff_json.eq(serialize_json(&pending_diff.diff)?), + pending_state_tree_diffs::block_id.eq(serialize_hex(block_id)), + pending_state_tree_diffs::shard.eq(shard.as_u32() as i32), + pending_state_tree_diffs::block_height.eq(blocks::table + .select(blocks::height) + .filter(blocks::block_id.eq(serialize_hex(block_id))) + .single_value() + .assume_not_null()), + pending_state_tree_diffs::version.eq(diff.version as i64), + pending_state_tree_diffs::diff_json.eq(serialize_json(&diff.diff)?), ); diesel::insert_into(pending_state_tree_diffs::table) @@ -1489,13 +1515,7 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta Ok(()) } - fn state_tree_nodes_insert( - &mut self, - epoch: Epoch, - shard: Shard, - key: NodeKey, - node: Node, - ) -> Result<(), StorageError> { + fn state_tree_nodes_insert(&mut self, shard: Shard, key: NodeKey, node: Node) -> Result<(), StorageError> { use crate::schema::state_tree; let node = TreeNode::new_latest(node); @@ -1504,7 +1524,6 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta })?; let values = ( - state_tree::epoch.eq(epoch.as_u64() as i64), state_tree::shard.eq(shard.as_u32() as i32), state_tree::key.eq(key.to_string()), state_tree::node.eq(node), @@ -1512,25 +1531,25 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta diesel::insert_into(state_tree::table) .values(&values) .execute(self.connection()) - .map_err(|e| SqliteStorageError::DieselError { - operation: "state_tree_nodes_insert", - source: e, + .map_err(|e| { + SqliteStorageError::DbInconsistency { + operation: "state_tree_nodes_insert", + details: format!("Failed to insert node for key: {shard} - {key} {e}"), + } + // SqliteStorageError::DieselError { + // operation: "state_tree_nodes_insert", + // source: e, + // } })?; Ok(()) } - fn state_tree_nodes_mark_stale_tree_node( - &mut self, - epoch: Epoch, - shard: Shard, - node: StaleTreeNode, - ) -> Result<(), StorageError> { + fn state_tree_nodes_mark_stale_tree_node(&mut self, shard: Shard, node: StaleTreeNode) -> Result<(), StorageError> { use crate::schema::state_tree; let key = node.as_node_key(); - diesel::update(state_tree::table) - .filter(state_tree::epoch.eq(epoch.as_u64() as i64)) + let num_effected = diesel::update(state_tree::table) .filter(state_tree::shard.eq(shard.as_u32() as i32)) .filter(state_tree::key.eq(key.to_string())) .set(state_tree::is_stale.eq(true)) @@ -1540,6 +1559,35 @@ impl<'tx, TAddr: NodeAddressable + 'tx> StateStoreWriteTransaction for SqliteSta source: e, })?; + if num_effected == 0 { + return Err(StorageError::NotFound { + item: "state_tree_node".to_string(), + key: key.to_string(), + }); + } + + Ok(()) + } + + fn state_tree_shard_versions_set(&mut self, shard: Shard, version: Version) -> Result<(), StorageError> { + use crate::schema::state_tree_shard_versions; + + let values = ( + state_tree_shard_versions::shard.eq(shard.as_u32() as i32), + state_tree_shard_versions::version.eq(version as i64), + ); + + diesel::insert_into(state_tree_shard_versions::table) + .values(&values) + .on_conflict(state_tree_shard_versions::shard) + .do_update() + .set(state_tree_shard_versions::version.eq(version as i64)) + .execute(self.connection()) + .map_err(|e| SqliteStorageError::DieselError { + operation: "state_tree_shard_versions_increment", + source: e, + })?; + Ok(()) } } diff --git a/dan_layer/state_store_sqlite/tests/tests.rs b/dan_layer/state_store_sqlite/tests/tests.rs index 7e36d1a21..e970ed2de 100644 --- a/dan_layer/state_store_sqlite/tests/tests.rs +++ b/dan_layer/state_store_sqlite/tests/tests.rs @@ -3,7 +3,7 @@ use rand::{rngs::OsRng, RngCore}; use tari_common_types::types::FixedHash; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight}; +use tari_dan_common_types::{Epoch, NodeHeight}; use tari_dan_storage::{ consensus_models::{Block, Command, Decision, TransactionAtom, TransactionPoolStage, TransactionPoolStatusUpdate}, StateStore, @@ -31,6 +31,7 @@ fn create_tx_atom() -> TransactionAtom { } mod confirm_all_transitions { + use tari_dan_common_types::{NumPreshards, ShardGroup}; use super::*; @@ -46,7 +47,7 @@ mod confirm_all_transitions { let atom3 = create_tx_atom(); let network = Default::default(); - let zero_block = Block::zero_block(network); + let zero_block = Block::zero_block(network, NumPreshards::SixtyFour); zero_block.insert(&mut tx).unwrap(); let block1 = Block::new( network, @@ -54,7 +55,7 @@ mod confirm_all_transitions { zero_block.justify().clone(), NodeHeight(1), Epoch(0), - Shard::from(0), + ShardGroup::all_shards(NumPreshards::SixtyFour), Default::default(), // Need to have a command in, otherwise this block will not be included internally in the query because it // cannot cause a state change without any commands diff --git a/dan_layer/state_tree/Cargo.toml b/dan_layer/state_tree/Cargo.toml index 2be4945df..01a273ab3 100644 --- a/dan_layer/state_tree/Cargo.toml +++ b/dan_layer/state_tree/Cargo.toml @@ -17,6 +17,7 @@ hex = { workspace = true } thiserror = { workspace = true } serde = { workspace = true, features = ["derive"] } log = { workspace = true } +indexmap = { workspace = true, features = ["serde"] } [dev-dependencies] indexmap = { workspace = true } diff --git a/dan_layer/state_tree/src/jellyfish/mod.rs b/dan_layer/state_tree/src/jellyfish/mod.rs index e4b8bb4ca..1c6a6d89a 100644 --- a/dan_layer/state_tree/src/jellyfish/mod.rs +++ b/dan_layer/state_tree/src/jellyfish/mod.rs @@ -11,4 +11,5 @@ mod types; pub use types::*; mod store; + pub use store::*; diff --git a/dan_layer/state_tree/src/jellyfish/tree.rs b/dan_layer/state_tree/src/jellyfish/tree.rs index 7eeaee976..7448afa70 100644 --- a/dan_layer/state_tree/src/jellyfish/tree.rs +++ b/dan_layer/state_tree/src/jellyfish/tree.rs @@ -86,6 +86,8 @@ use std::{ marker::PhantomData, }; +use tari_dan_common_types::optional::Optional; + use super::{ store::TreeStoreReader, types::{ @@ -187,13 +189,14 @@ impl<'a, R: 'a + TreeStoreReader

, P: Clone> JellyfishMerkleTree<'a, R, P> { /// the batch is not reachable from public interfaces before being committed. pub fn batch_put_value_set( &self, - value_set: Vec<(&LeafKey, Option<&(Hash, P)>)>, + value_set: Vec<(LeafKey, Option<(Hash, P)>)>, node_hashes: Option<&HashMap>, persisted_version: Option, version: Version, ) -> Result<(Hash, TreeUpdateBatch

), JmtStorageError> { let deduped_and_sorted_kvs = value_set - .into_iter() + .iter() + .map(|(k, v)| (k, v.as_ref())) .collect::>() .into_iter() .collect::>(); @@ -273,7 +276,7 @@ impl<'a, R: 'a + TreeStoreReader

, P: Clone> JellyfishMerkleTree<'a, R, P> { if let Some(child) = child_option { new_created_children.insert(child_nibble, child); } else { - old_children.remove(&child_nibble); + old_children.swap_remove(&child_nibble); } } @@ -606,7 +609,11 @@ impl<'a, R: 'a + TreeStoreReader

, P: Clone> JellyfishMerkleTree<'a, R, P> { } pub fn get_root_hash(&self, version: Version) -> Result { - self.get_root_node(version).map(|n| n.hash()) + Ok(self + .get_root_node(version) + .map(|n| n.hash()) + .optional()? + .unwrap_or(SPARSE_MERKLE_PLACEHOLDER_HASH)) } pub fn get_leaf_count(&self, version: Version) -> Result { diff --git a/dan_layer/state_tree/src/jellyfish/types.rs b/dan_layer/state_tree/src/jellyfish/types.rs index d18ad6d59..b54ee3009 100644 --- a/dan_layer/state_tree/src/jellyfish/types.rs +++ b/dan_layer/state_tree/src/jellyfish/types.rs @@ -81,8 +81,9 @@ // Copyright (c) Aptos // SPDX-License-Identifier: Apache-2.0 -use std::{collections::HashMap, fmt, ops::Range}; +use std::{fmt, ops::Range}; +use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use tari_crypto::{hash_domain, tari_utilities::ByteArray}; use tari_dan_common_types::{ @@ -97,16 +98,16 @@ pub type Hash = tari_common_types::types::FixedHash; hash_domain!(SparseMerkleTree, "com.tari.dan.state_tree", 0); -fn hasher() -> TariHasher { - tari_hasher::("hash") +fn jmt_node_hasher() -> TariHasher { + tari_hasher::("JmtNode") } -pub fn hash(data: &T) -> Hash { - hasher().chain(data).result() +pub fn jmt_node_hash(data: &T) -> Hash { + jmt_node_hasher().chain(data).result() } -pub fn hash2(d1: &[u8], d2: &[u8]) -> Hash { - hasher().chain(d1).chain(d2).result() +pub fn jmt_node_hash2(d1: &[u8], d2: &[u8]) -> Hash { + jmt_node_hasher().chain(d1).chain(d2).result() } // SOURCE: https://github.com/aptos-labs/aptos-core/blob/1.0.4/types/src/proof/definition.rs#L182 @@ -273,7 +274,7 @@ impl SparseMerkleLeafNode { } pub fn hash(&self) -> Hash { - hash2(self.key.bytes.as_slice(), self.value_hash.as_slice()) + jmt_node_hash2(self.key.bytes.as_slice(), self.value_hash.as_slice()) } } @@ -291,7 +292,7 @@ impl SparseMerkleInternalNode { } fn hash(&self) -> Hash { - hash2(self.left_child.as_bytes(), self.right_child.as_bytes()) + jmt_node_hash2(self.left_child.as_bytes(), self.right_child.as_bytes()) } } @@ -842,7 +843,7 @@ impl Child { /// [`Children`] is just a collection of children belonging to a [`InternalNode`], indexed from 0 to /// 15, inclusive. -pub(crate) type Children = HashMap; +pub(crate) type Children = IndexMap; /// Represents a 4-level subtree with 16 children at the bottom level. Theoretically, this reduces /// IOPS to query a tree by 4x since we compress 4 levels in a standard Merkle tree into 1 node. @@ -858,7 +859,8 @@ pub struct InternalNode { impl InternalNode { /// Creates a new Internal node. - pub fn new(children: Children) -> Self { + pub fn new(mut children: Children) -> Self { + children.sort_keys(); let leaf_count = children.values().map(Child::leaf_count).sum(); Self { children, leaf_count } } @@ -882,9 +884,10 @@ impl InternalNode { } pub fn children_sorted(&self) -> impl Iterator { - let mut tmp = self.children.iter().collect::>(); - tmp.sort_by_key(|(nibble, _)| **nibble); - tmp.into_iter() + // let mut tmp = self.children.iter().collect::>(); + // tmp.sort_by_key(|(nibble, _)| **nibble); + // tmp.into_iter() + self.children.iter() } pub fn into_children(self) -> Children { @@ -1147,7 +1150,7 @@ impl LeafNode

{ /// changes within a sparse merkle tree (consider 2 trees, both containing a single element with /// the same value, but stored under different keys - we want their root hashes to differ). pub fn leaf_hash(&self) -> Hash { - hash2(self.leaf_key.bytes.as_slice(), self.value_hash.as_slice()) + jmt_node_hash2(self.leaf_key.bytes.as_slice(), self.value_hash.as_slice()) } } @@ -1240,6 +1243,9 @@ pub enum JmtStorageError { #[error("Unexpected error: {0}")] UnexpectedError(String), + + #[error("Attempted to insert node {0} that already exists")] + Conflict(NodeKey), } impl IsNotFoundError for JmtStorageError { diff --git a/dan_layer/state_tree/src/key_mapper.rs b/dan_layer/state_tree/src/key_mapper.rs index 344713f36..60ac6b0b0 100644 --- a/dan_layer/state_tree/src/key_mapper.rs +++ b/dan_layer/state_tree/src/key_mapper.rs @@ -13,7 +13,7 @@ pub struct SpreadPrefixKeyMapper; impl DbKeyMapper for SpreadPrefixKeyMapper { fn map_to_leaf_key(id: &SubstateId) -> LeafKey { - let hash = crate::jellyfish::hash(id); + let hash = crate::jellyfish::jmt_node_hash(id); LeafKey::new(hash.to_vec()) } } diff --git a/dan_layer/state_tree/src/staged_store.rs b/dan_layer/state_tree/src/staged_store.rs index cff18d48b..084bce300 100644 --- a/dan_layer/state_tree/src/staged_store.rs +++ b/dan_layer/state_tree/src/staged_store.rs @@ -1,13 +1,10 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -// Copyright 2024 The Tari Project -// SPDX-License-Identifier: BSD-3-Clause -// Copyright 2024 The Tari Project -// SPDX-License-Identifier: BSD-3-Clause - use std::collections::{HashMap, VecDeque}; +use log::debug; + use crate::{ JmtStorageError, Node, @@ -19,6 +16,8 @@ use crate::{ Version, }; +const LOG_TARGET: &str = "tari::dan::consensus::sharded_state_tree"; + pub struct StagedTreeStore<'s, S> { readable_store: &'s S, preceding_pending_state: HashMap>, @@ -36,10 +35,19 @@ impl<'s, S: TreeStoreReader> StagedTreeStore<'s, S> { } } - pub fn apply_ordered_diffs>(&mut self, diffs: I) { - for (key, node) in diffs.into_iter().flat_map(|diff| diff.new_nodes) { + pub fn apply_pending_diff(&mut self, diff: StateHashTreeDiff) { + self.preceding_pending_state.reserve(diff.new_nodes.len()); + for (key, node) in diff.new_nodes { + debug!(target: LOG_TARGET, "PENDING INSERT: node {}", key); self.preceding_pending_state.insert(key, node); } + + for stale in diff.stale_tree_nodes { + debug!(target: LOG_TARGET, "PENDING DELETE: node {}", stale.as_node_key()); + if self.preceding_pending_state.remove(stale.as_node_key()).is_some() { + debug!(target: LOG_TARGET, "PENDING DELETE: node {} removed", stale.as_node_key()); + } + } } pub fn into_diff(self) -> StateHashTreeDiff { @@ -65,7 +73,9 @@ impl<'s, S: TreeStoreReader> TreeStoreReader for StagedTreeSto impl<'s, S> TreeStoreWriter for StagedTreeStore<'s, S> { fn insert_node(&mut self, key: NodeKey, node: Node) -> Result<(), JmtStorageError> { - self.new_tree_nodes.insert(key, node); + if self.new_tree_nodes.insert(key.clone(), node).is_some() { + return Err(JmtStorageError::Conflict(key)); + } Ok(()) } diff --git a/dan_layer/state_tree/src/tree.rs b/dan_layer/state_tree/src/tree.rs index b1e4e9b92..fa58817a2 100644 --- a/dan_layer/state_tree/src/tree.rs +++ b/dan_layer/state_tree/src/tree.rs @@ -8,7 +8,7 @@ use tari_engine_types::substate::SubstateId; use crate::{ error::StateTreeError, - jellyfish::{Hash, JellyfishMerkleTree, LeafKey, SparseMerkleProofExt, TreeStore, Version}, + jellyfish::{Hash, JellyfishMerkleTree, SparseMerkleProofExt, TreeStore, Version}, key_mapper::{DbKeyMapper, SpreadPrefixKeyMapper}, Node, NodeKey, @@ -25,11 +25,6 @@ pub struct StateTree<'a, S, M> { _mapper: PhantomData, } -struct LeafChange { - key: LeafKey, - new_payload: Option<(Hash, Version)>, -} - impl<'a, S, M> StateTree<'a, S, M> { pub fn new(store: &'a mut S) -> Self { Self { @@ -50,6 +45,12 @@ impl<'a, S: TreeStoreReader, M: DbKeyMapper> StateTree<'a, S, M> { let (maybe_value, proof) = smt.get_with_proof_ext(key.as_ref(), version)?; Ok((maybe_value, proof)) } + + pub fn get_root_hash(&self, version: Version) -> Result { + let smt = JellyfishMerkleTree::new(self.store); + let root_hash = smt.get_root_hash(version)?; + Ok(root_hash) + } } impl<'a, S: TreeStore, M: DbKeyMapper> StateTree<'a, S, M> { @@ -98,26 +99,12 @@ fn calculate_substate_changes< let changes = changes .into_iter() .map(|ch| match ch { - SubstateTreeChange::Up { id, value_hash } => LeafChange { - key: M::map_to_leaf_key(&id), - new_payload: Some((value_hash, next_version)), - }, - SubstateTreeChange::Down { id } => LeafChange { - key: M::map_to_leaf_key(&id), - new_payload: None, - }, + SubstateTreeChange::Up { id, value_hash } => (M::map_to_leaf_key(&id), Some((value_hash, next_version))), + SubstateTreeChange::Down { id } => (M::map_to_leaf_key(&id), None), }) .collect::>(); - let (root_hash, update_result) = jmt.batch_put_value_set( - changes - .iter() - .map(|change| (&change.key, change.new_payload.as_ref())) - .collect(), - None, - current_version, - next_version, - )?; + let (root_hash, update_result) = jmt.batch_put_value_set(changes, None, current_version, next_version)?; Ok((root_hash, update_result)) } diff --git a/dan_layer/state_tree/tests/support.rs b/dan_layer/state_tree/tests/support.rs index d6f33319d..7dcf31c83 100644 --- a/dan_layer/state_tree/tests/support.rs +++ b/dan_layer/state_tree/tests/support.rs @@ -64,6 +64,16 @@ impl> HashTreeTester { .put_substate_changes(current_version, next_version, changes) .unwrap() } + + pub fn put_changes_at_version(&mut self, changes: impl IntoIterator) -> Hash { + let next_version = self + .current_version + .expect("call put_changes_at_version with None version"); + let current_version = self.current_version.unwrap(); + StateTree::<_, IdentityMapper>::new(&mut self.tree_store) + .put_substate_changes(Some(current_version), next_version, changes) + .unwrap() + } } impl HashTreeTester { diff --git a/dan_layer/state_tree/tests/test.rs b/dan_layer/state_tree/tests/test.rs index c038d736f..7fb093d3f 100644 --- a/dan_layer/state_tree/tests/test.rs +++ b/dan_layer/state_tree/tests/test.rs @@ -92,6 +92,18 @@ fn hash_computed_consistently_after_adding_higher_tier_sibling() { assert_eq!(root_after_adding_sibling, reference_root); } +#[test] +fn hash_allows_putting_in_same_version() { + let mut tester_1 = HashTreeTester::new_empty(); + tester_1.put_substate_changes(vec![change(1, Some(30))]); + tester_1.put_substate_changes(vec![change(2, Some(31))]); + // Append another change to the same version + let hash_1 = tester_1.put_changes_at_version(vec![change(3, Some(32))]); + let mut tester_2 = HashTreeTester::new_empty(); + let hash_2 = tester_2.put_substate_changes(vec![change(1, Some(30)), change(2, Some(31)), change(3, Some(32))]); + assert_eq!(hash_1, hash_2); +} + #[test] fn hash_differs_when_states_only_differ_by_node_key() { let mut tester_1 = HashTreeTester::new_empty(); diff --git a/dan_layer/storage/src/consensus_models/block.rs b/dan_layer/storage/src/consensus_models/block.rs index ec3ed0467..d3d5d1cff 100644 --- a/dan_layer/storage/src/consensus_models/block.rs +++ b/dan_layer/storage/src/consensus_models/block.rs @@ -22,6 +22,8 @@ use tari_dan_common_types::{ Epoch, NodeAddressable, NodeHeight, + NumPreshards, + ShardGroup, SubstateAddress, }; use tari_engine_types::substate::SubstateDiff; @@ -74,7 +76,7 @@ pub struct Block { justify: QuorumCertificate, height: NodeHeight, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, #[cfg_attr(feature = "ts", ts(type = "string"))] proposed_by: PublicKey, #[cfg_attr(feature = "ts", ts(type = "number"))] @@ -117,7 +119,7 @@ impl Block { justify: QuorumCertificate, height: NodeHeight, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, proposed_by: PublicKey, commands: BTreeSet, merkle_root: FixedHash, @@ -135,7 +137,7 @@ impl Block { justify, height, epoch, - shard, + shard_group, proposed_by, merkle_root, commands, @@ -163,7 +165,7 @@ impl Block { justify: QuorumCertificate, height: NodeHeight, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, proposed_by: PublicKey, commands: BTreeSet, merkle_root: FixedHash, @@ -186,7 +188,7 @@ impl Block { justify, height, epoch, - shard, + shard_group, proposed_by, merkle_root, commands, @@ -204,14 +206,14 @@ impl Block { } } - pub fn genesis(network: Network, epoch: Epoch, shard: Shard) -> Self { + pub fn genesis(network: Network, epoch: Epoch, shard_group: ShardGroup) -> Self { Self::new( network, BlockId::zero(), - QuorumCertificate::genesis(), - NodeHeight(0), + QuorumCertificate::genesis(epoch, shard_group), + NodeHeight::zero(), epoch, - shard, + shard_group, PublicKey::default(), Default::default(), // TODO: the merkle hash should be initialized to something committing to the previous state. @@ -226,15 +228,15 @@ impl Block { } /// This is the parent block for all genesis blocks. Its block ID is always zero. - pub fn zero_block(network: Network) -> Self { + pub fn zero_block(network: Network, num_preshards: NumPreshards) -> Self { Self { network, id: BlockId::zero(), parent: BlockId::zero(), - justify: QuorumCertificate::genesis(), - height: NodeHeight(0), - epoch: Epoch(0), - shard: Shard::from(0), + justify: QuorumCertificate::genesis(Epoch::zero(), ShardGroup::all_shards(num_preshards)), + height: NodeHeight::zero(), + epoch: Epoch::zero(), + shard_group: ShardGroup::all_shards(num_preshards), proposed_by: PublicKey::default(), merkle_root: FixedHash::zero(), commands: Default::default(), @@ -259,7 +261,7 @@ impl Block { height: NodeHeight, high_qc: QuorumCertificate, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, parent_merkle_root: FixedHash, parent_timestamp: u64, parent_base_layer_block_height: u64, @@ -272,7 +274,7 @@ impl Block { justify: high_qc, height, epoch, - shard, + shard_group, proposed_by, merkle_root: parent_merkle_root, commands: BTreeSet::new(), @@ -313,7 +315,7 @@ impl Block { .chain(&self.height) .chain(&self.total_leader_fee) .chain(&self.epoch) - .chain(&self.shard) + .chain(&self.shard_group) .chain(&self.proposed_by) .chain(&self.merkle_root) .chain(&self.is_dummy) @@ -428,8 +430,8 @@ impl Block { self.epoch } - pub fn shard(&self) -> Shard { - self.shard + pub fn shard_group(&self) -> ShardGroup { + self.shard_group } pub fn total_leader_fee(&self) -> u64 { @@ -518,14 +520,14 @@ impl Block { pub fn get_all_blocks_between( tx: &TTx, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, start_block_id_exclusive: &BlockId, end_block_id_inclusive: &BlockId, include_dummy_blocks: bool, ) -> Result, StorageError> { tx.blocks_get_all_between( epoch, - shard, + shard_group, start_block_id_exclusive, end_block_id_inclusive, include_dummy_blocks, @@ -612,6 +614,7 @@ impl Block { match change { SubstateChange::Up { id, + shard, transaction_id, substate, } => { @@ -619,7 +622,7 @@ impl Block { id.substate_id, id.version, substate.into_substate_value(), - self.shard(), + shard, self.epoch(), self.height(), *self.id(), @@ -628,11 +631,15 @@ impl Block { ) .create(tx)?; }, - SubstateChange::Down { id, transaction_id } => { + SubstateChange::Down { + id, + transaction_id, + shard, + } => { SubstateRecord::destroy( tx, id, - self.shard(), + shard, self.epoch(), self.height(), self.justify().id(), @@ -766,20 +773,20 @@ impl Block { // because the engine can never emit such a substate diff. if substate.created_by_transaction == transaction.id { updates.push(SubstateUpdate::Create(SubstateCreatedProof { - created_qc: substate.get_created_quorum_certificate(tx)?, + // created_qc: substate.get_created_quorum_certificate(tx)?, substate: substate.into(), })); } else { updates.push(SubstateUpdate::Destroy(SubstateDestroyedProof { substate_id: substate.substate_id.clone(), version: substate.version, - justify: QuorumCertificate::get(tx, &destroyed.justify)?, + // justify: QuorumCertificate::get(tx, &destroyed.justify)?, destroyed_by_transaction: destroyed.by_transaction, })); } } else { updates.push(SubstateUpdate::Create(SubstateCreatedProof { - created_qc: substate.get_created_quorum_certificate(tx)?, + // created_qc: substate.get_created_quorum_certificate(tx)?, substate: substate.into(), })); }; @@ -932,11 +939,12 @@ impl Display for Block { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, - "[{}, {}, {}, {} command(s)]", + "[{}, {}, {}, {} cmd(s), {}]", self.height(), self.epoch(), + self.shard_group(), + self.commands().len(), self.id(), - self.commands().len() ) } } diff --git a/dan_layer/storage/src/consensus_models/epoch_checkpoint.rs b/dan_layer/storage/src/consensus_models/epoch_checkpoint.rs index 601f79483..6b444fe7b 100644 --- a/dan_layer/storage/src/consensus_models/epoch_checkpoint.rs +++ b/dan_layer/storage/src/consensus_models/epoch_checkpoint.rs @@ -3,7 +3,7 @@ use std::fmt::Display; -use tari_dan_common_types::{shard::Shard, Epoch}; +use tari_dan_common_types::{Epoch, ShardGroup}; use crate::{ consensus_models::{Block, QuorumCertificate}, @@ -35,12 +35,12 @@ impl EpochCheckpoint { pub fn generate( tx: &TTx, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, ) -> Result { - let mut blocks = tx.blocks_get_last_n_in_epoch(3, epoch, shard)?; + let mut blocks = tx.blocks_get_last_n_in_epoch(3, epoch, shard_group)?; if blocks.is_empty() { return Err(StorageError::NotFound { - item: format!("EpochCheckpoint: No blocks found for epoch {epoch}, shard {shard}"), + item: format!("EpochCheckpoint: No blocks found for epoch {epoch}, shard group {shard_group}"), key: epoch.to_string(), }); } diff --git a/dan_layer/storage/src/consensus_models/foreign_proposal.rs b/dan_layer/storage/src/consensus_models/foreign_proposal.rs index 7c9b9c9ca..42ebc877e 100644 --- a/dan_layer/storage/src/consensus_models/foreign_proposal.rs +++ b/dan_layer/storage/src/consensus_models/foreign_proposal.rs @@ -8,16 +8,18 @@ use std::{ }; use serde::{Deserialize, Serialize}; -use tari_dan_common_types::{shard::Shard, NodeHeight}; +use tari_dan_common_types::{NodeHeight, ShardGroup}; use tari_transaction::TransactionId; -#[cfg(feature = "ts")] -use ts_rs::TS; use super::BlockId; use crate::{StateStoreReadTransaction, StateStoreWriteTransaction, StorageError}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub enum ForeignProposalState { New, Proposed, @@ -48,10 +50,14 @@ impl FromStr for ForeignProposalState { } #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct ForeignProposal { #[cfg_attr(feature = "ts", ts(type = "number"))] - pub shard: Shard, + pub shard_group: ShardGroup, #[cfg_attr(feature = "ts", ts(type = "string"))] pub block_id: BlockId, pub state: ForeignProposalState, @@ -64,13 +70,13 @@ pub struct ForeignProposal { impl ForeignProposal { pub fn new( - shard: Shard, + shard_group: ShardGroup, block_id: BlockId, transactions: Vec, base_layer_block_height: u64, ) -> Self { Self { - shard, + shard_group, block_id, state: ForeignProposalState::New, proposed_height: None, @@ -97,11 +103,8 @@ impl ForeignProposal { Ok(()) } - pub fn exists( - tx: &TTx, - foreign_proposal: &Self, - ) -> Result { - tx.foreign_proposal_exists(foreign_proposal) + pub fn exists(&self, tx: &TTx) -> Result { + tx.foreign_proposal_exists(self) } pub fn get_all_new(tx: &TTx) -> Result, StorageError> { diff --git a/dan_layer/storage/src/consensus_models/foreign_receive_counters.rs b/dan_layer/storage/src/consensus_models/foreign_receive_counters.rs index 4ef3d073f..b2c502133 100644 --- a/dan_layer/storage/src/consensus_models/foreign_receive_counters.rs +++ b/dan_layer/storage/src/consensus_models/foreign_receive_counters.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; -use tari_dan_common_types::{optional::Optional, shard::Shard}; +use tari_dan_common_types::{optional::Optional, shard::Shard, ShardGroup}; use crate::{StateStoreReadTransaction, StateStoreWriteTransaction, StorageError}; @@ -25,13 +25,15 @@ impl ForeignReceiveCounters { } } - pub fn increment(&mut self, bucket: &Shard) { - *self.counters.entry(*bucket).or_default() += 1; + pub fn increment_group(&mut self, shard_group: ShardGroup) { + for shard in shard_group.shard_iter() { + *self.counters.entry(shard).or_default() += 1; + } } /// Returns the counter for the provided shard. If the count does not exist, 0 is returned. - pub fn get_count(&self, bucket: &Shard) -> u64 { - self.counters.get(bucket).copied().unwrap_or_default() + pub fn get_count(&self, shard: &Shard) -> u64 { + self.counters.get(shard).copied().unwrap_or_default() } } diff --git a/dan_layer/storage/src/consensus_models/quorum_certificate.rs b/dan_layer/storage/src/consensus_models/quorum_certificate.rs index 523aca80c..54be7a739 100644 --- a/dan_layer/storage/src/consensus_models/quorum_certificate.rs +++ b/dan_layer/storage/src/consensus_models/quorum_certificate.rs @@ -10,12 +10,10 @@ use tari_dan_common_types::{ hashing::quorum_certificate_hasher, optional::Optional, serde_with, - shard::Shard, Epoch, NodeHeight, + ShardGroup, }; -#[cfg(feature = "ts")] -use ts_rs::TS; use crate::{ consensus_models::{Block, BlockId, HighQc, LastVoted, LeafBlock, QuorumDecision, ValidatorSignature}, @@ -27,7 +25,11 @@ use crate::{ const LOG_TARGET: &str = "tari::dan::storage::quorum_certificate"; #[derive(Debug, Clone, Deserialize, Serialize)] -#[cfg_attr(feature = "ts", derive(TS), ts(export, export_to = "../../bindings/src/types/"))] +#[cfg_attr( + feature = "ts", + derive(ts_rs::TS), + ts(export, export_to = "../../bindings/src/types/") +)] pub struct QuorumCertificate { #[cfg_attr(feature = "ts", ts(type = "string"))] qc_id: QcId, @@ -35,7 +37,7 @@ pub struct QuorumCertificate { block_id: BlockId, block_height: NodeHeight, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, signatures: Vec, #[serde(with = "serde_with::hex::vec")] #[cfg_attr(feature = "ts", ts(type = "Array"))] @@ -48,7 +50,7 @@ impl QuorumCertificate { block: BlockId, block_height: NodeHeight, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, signatures: Vec, mut leaf_hashes: Vec, decision: QuorumDecision, @@ -59,7 +61,7 @@ impl QuorumCertificate { block_id: block, block_height, epoch, - shard, + shard_group, signatures, leaf_hashes, decision, @@ -68,12 +70,12 @@ impl QuorumCertificate { qc } - pub fn genesis() -> Self { + pub fn genesis(epoch: Epoch, shard_group: ShardGroup) -> Self { Self::new( BlockId::zero(), NodeHeight::zero(), - Epoch(0), - Shard::from(0), + epoch, + shard_group, vec![], vec![], QuorumDecision::Accept, @@ -83,7 +85,7 @@ impl QuorumCertificate { pub fn calculate_id(&self) -> QcId { quorum_certificate_hasher() .chain(&self.epoch) - .chain(&self.shard) + .chain(&self.shard_group) .chain(&self.block_id) .chain(&self.block_height) .chain(&self.signatures) @@ -107,8 +109,8 @@ impl QuorumCertificate { self.epoch } - pub fn shard(&self) -> Shard { - self.shard + pub fn shard_group(&self) -> ShardGroup { + self.shard_group } pub fn leaf_hashes(&self) -> &[FixedHash] { diff --git a/dan_layer/storage/src/consensus_models/state_transition.rs b/dan_layer/storage/src/consensus_models/state_transition.rs index d5bba0600..311676e9c 100644 --- a/dan_layer/storage/src/consensus_models/state_transition.rs +++ b/dan_layer/storage/src/consensus_models/state_transition.rs @@ -8,6 +8,7 @@ use std::{ }; use tari_dan_common_types::{shard::Shard, Epoch}; +use tari_state_tree::Version; use crate::{consensus_models::SubstateUpdate, StateStoreReadTransaction, StorageError}; @@ -15,7 +16,7 @@ use crate::{consensus_models::SubstateUpdate, StateStoreReadTransaction, Storage pub struct StateTransition { pub id: StateTransitionId, pub update: SubstateUpdate, - pub state_tree_version: u64, + pub state_tree_version: Version, } impl StateTransition { @@ -28,8 +29,11 @@ impl StateTransition { tx.state_transitions_get_n_after(n, after_id, end_epoch) } - pub fn get_last_id(tx: &TTx) -> Result { - tx.state_transitions_get_last_id() + pub fn get_last_id( + tx: &TTx, + shard: Shard, + ) -> Result { + tx.state_transitions_get_last_id(shard) } } @@ -52,6 +56,10 @@ impl StateTransitionId { Self { epoch, shard, seq } } + pub fn initial(shard: Shard) -> Self { + Self::new(Epoch(1), shard, 0) + } + pub fn from_bytes(mut bytes: &[u8]) -> Option { if bytes.len() < Self::BYTE_SIZE { return None; @@ -89,7 +97,7 @@ impl Display for StateTransitionId { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, - "StateTransition(epoch = {}, shard = {}, seq = {})", + "StateTransition({}, {}, seq = {})", self.epoch(), self.shard(), self.seq() diff --git a/dan_layer/storage/src/consensus_models/state_tree_diff.rs b/dan_layer/storage/src/consensus_models/state_tree_diff.rs index f3dd81fad..59d55dc84 100644 --- a/dan_layer/storage/src/consensus_models/state_tree_diff.rs +++ b/dan_layer/storage/src/consensus_models/state_tree_diff.rs @@ -4,26 +4,23 @@ // Copyright 2023 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::ops::Deref; +use std::{collections::HashMap, ops::Deref}; -use tari_dan_common_types::{shard::Shard, Epoch, NodeHeight}; +use indexmap::IndexMap; +use tari_dan_common_types::shard::Shard; +use tari_state_tree::{StateHashTreeDiff, Version}; use crate::{consensus_models::BlockId, StateStoreReadTransaction, StateStoreWriteTransaction, StorageError}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct PendingStateTreeDiff { - pub block_id: BlockId, - pub block_height: NodeHeight, - pub diff: tari_state_tree::StateHashTreeDiff, + pub version: Version, + pub diff: StateHashTreeDiff, } impl PendingStateTreeDiff { - pub fn new(block_id: BlockId, block_height: NodeHeight, diff: tari_state_tree::StateHashTreeDiff) -> Self { - Self { - block_id, - block_height, - diff, - } + pub fn load(version: Version, diff: StateHashTreeDiff) -> Self { + Self { version, diff } } } @@ -31,17 +28,15 @@ impl PendingStateTreeDiff { /// Returns all pending state tree diffs from the last committed block (exclusive) to the given block (inclusive). pub fn get_all_up_to_commit_block( tx: &TTx, - epoch: Epoch, - shard: Shard, block_id: &BlockId, - ) -> Result, StorageError> + ) -> Result>, StorageError> where TTx: StateStoreReadTransaction, { - tx.pending_state_tree_diffs_get_all_up_to_commit_block(epoch, shard, block_id) + tx.pending_state_tree_diffs_get_all_up_to_commit_block(block_id) } - pub fn remove_by_block(tx: &mut TTx, block_id: &BlockId) -> Result + pub fn remove_by_block(tx: &mut TTx, block_id: &BlockId) -> Result>, StorageError> where TTx: Deref + StateStoreWriteTransaction, TTx::Target: StateStoreReadTransaction, @@ -49,16 +44,34 @@ impl PendingStateTreeDiff { tx.pending_state_tree_diffs_remove_by_block(block_id) } - pub fn save(&self, tx: &mut TTx) -> Result + pub fn create( + tx: &mut TTx, + block_id: BlockId, + shard: Shard, + diff: VersionedStateHashTreeDiff, + ) -> Result<(), StorageError> where TTx: Deref + StateStoreWriteTransaction, TTx::Target: StateStoreReadTransaction, { - if tx.pending_state_tree_diffs_exists_for_block(&self.block_id)? { - Ok(false) - } else { - tx.pending_state_tree_diffs_insert(self)?; - Ok(true) - } + // if tx.pending_state_tree_diffs_exists_for_block(&block_id)? { + // Ok(false) + // } else { + tx.pending_state_tree_diffs_insert(block_id, shard, diff)?; + // Ok(true) + // } + Ok(()) + } +} + +#[derive(Debug, Clone)] +pub struct VersionedStateHashTreeDiff { + pub version: Version, + pub diff: StateHashTreeDiff, +} + +impl VersionedStateHashTreeDiff { + pub fn new(version: Version, diff: StateHashTreeDiff) -> Self { + Self { version, diff } } } diff --git a/dan_layer/storage/src/consensus_models/substate.rs b/dan_layer/storage/src/consensus_models/substate.rs index 4781077d7..1bc6bfd4c 100644 --- a/dan_layer/storage/src/consensus_models/substate.rs +++ b/dan_layer/storage/src/consensus_models/substate.rs @@ -327,14 +327,14 @@ impl SubstateRecord { #[derive(Debug, Clone)] pub struct SubstateCreatedProof { pub substate: SubstateData, - pub created_qc: QuorumCertificate, + // TODO: proof that data was created } #[derive(Debug, Clone)] pub struct SubstateDestroyedProof { pub substate_id: SubstateId, pub version: u32, - pub justify: QuorumCertificate, + // TODO: proof that data was destroyed pub destroyed_by_transaction: TransactionId, } diff --git a/dan_layer/storage/src/consensus_models/substate_change.rs b/dan_layer/storage/src/consensus_models/substate_change.rs index dd66d3c0b..f04de6b53 100644 --- a/dan_layer/storage/src/consensus_models/substate_change.rs +++ b/dan_layer/storage/src/consensus_models/substate_change.rs @@ -1,7 +1,7 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use tari_dan_common_types::SubstateAddress; +use tari_dan_common_types::{shard::Shard, SubstateAddress}; use tari_engine_types::substate::Substate; use tari_state_tree::SubstateTreeChange; use tari_transaction::{TransactionId, VersionedSubstateId}; @@ -12,11 +12,13 @@ use crate::consensus_models::SubstateRecord; pub enum SubstateChange { Up { id: VersionedSubstateId, + shard: Shard, transaction_id: TransactionId, substate: Substate, }, Down { id: VersionedSubstateId, + shard: Shard, transaction_id: TransactionId, }, } @@ -50,6 +52,13 @@ impl SubstateChange { } } + pub fn shard(&self) -> Shard { + match self { + SubstateChange::Up { shard, .. } => *shard, + SubstateChange::Down { shard, .. } => *shard, + } + } + pub fn is_down(&self) -> bool { matches!(self, SubstateChange::Down { .. }) } @@ -92,11 +101,13 @@ impl From for SubstateChange { if let Some(destroyed) = value.destroyed() { Self::Down { id: value.to_versioned_substate_id(), + shard: destroyed.by_shard, transaction_id: destroyed.by_transaction, } } else { Self::Up { id: value.to_versioned_substate_id(), + shard: value.created_by_shard, transaction_id: value.created_by_transaction, substate: value.into_substate(), } diff --git a/dan_layer/storage/src/global/backend_adapter.rs b/dan_layer/storage/src/global/backend_adapter.rs index 7f2f39084..53cbb8f8d 100644 --- a/dan_layer/storage/src/global/backend_adapter.rs +++ b/dan_layer/storage/src/global/backend_adapter.rs @@ -20,10 +20,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::{ - collections::{HashMap, HashSet}, - ops::RangeInclusive, -}; +use std::{collections::HashMap, ops::RangeInclusive}; use serde::{de::DeserializeOwned, Serialize}; use tari_common_types::types::{FixedHash, PublicKey}; @@ -33,6 +30,7 @@ use tari_dan_common_types::{ shard::Shard, Epoch, NodeAddressable, + ShardGroup, SubstateAddress, }; @@ -119,19 +117,19 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone { epoch: Epoch, sidechain_id: Option<&PublicKey>, ) -> Result; - fn validator_nodes_count_for_bucket( + fn validator_nodes_count_for_shard_group( &self, tx: &mut Self::DbTransaction<'_>, epoch: Epoch, sidechain_id: Option<&PublicKey>, - bucket: Shard, + shard_group: ShardGroup, ) -> Result; fn validator_nodes_set_committee_shard( &self, tx: &mut Self::DbTransaction<'_>, shard_key: SubstateAddress, - shard: Shard, + shard_group: ShardGroup, sidechain_id: Option<&PublicKey>, epoch: Epoch, ) -> Result<(), Self::Error>; @@ -144,11 +142,11 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone { substate_range: RangeInclusive, ) -> Result>, Self::Error>; - fn validator_nodes_get_for_shards( + fn validator_nodes_get_for_shard_group( &self, tx: &mut Self::DbTransaction<'_>, epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, Self::Error>; fn validator_nodes_get_committees_for_epoch( @@ -156,7 +154,7 @@ pub trait GlobalDbAdapter: AtomicDb + Send + Sync + Clone { tx: &mut Self::DbTransaction<'_>, epoch: Epoch, sidechain_id: Option<&PublicKey>, - ) -> Result>, Self::Error>; + ) -> Result>, Self::Error>; fn insert_epoch(&self, tx: &mut Self::DbTransaction<'_>, epoch: DbEpoch) -> Result<(), Self::Error>; fn get_epoch(&self, tx: &mut Self::DbTransaction<'_>, epoch: u64) -> Result, Self::Error>; diff --git a/dan_layer/storage/src/global/validator_node_db.rs b/dan_layer/storage/src/global/validator_node_db.rs index ec2b07345..00bcd3522 100644 --- a/dan_layer/storage/src/global/validator_node_db.rs +++ b/dan_layer/storage/src/global/validator_node_db.rs @@ -20,10 +20,10 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use tari_common_types::types::PublicKey; -use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, SubstateAddress}; +use tari_dan_common_types::{committee::Committee, shard::Shard, Epoch, ShardGroup, SubstateAddress}; use crate::global::{models::ValidatorNode, GlobalDbAdapter}; @@ -69,14 +69,14 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba .map_err(TGlobalDbAdapter::Error::into) } - pub fn count_in_bucket( + pub fn count_in_shard_group( &mut self, epoch: Epoch, sidechain_id: Option<&PublicKey>, - bucket: Shard, + shard_group: ShardGroup, ) -> Result { self.backend - .validator_nodes_count_for_bucket(self.tx, epoch, sidechain_id, bucket) + .validator_nodes_count_for_shard_group(self.tx, epoch, sidechain_id, shard_group) .map_err(TGlobalDbAdapter::Error::into) } @@ -112,32 +112,21 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba .map_err(TGlobalDbAdapter::Error::into) } - pub fn get_committees_for_shards( + pub fn get_committees_for_shard_group( &mut self, epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, TGlobalDbAdapter::Error> { self.backend - .validator_nodes_get_for_shards(self.tx, epoch, shards) + .validator_nodes_get_for_shard_group(self.tx, epoch, shard_group) .map_err(TGlobalDbAdapter::Error::into) } - pub fn get_committee_for_shard( - &mut self, - epoch: Epoch, - shard: Shard, - ) -> Result>, TGlobalDbAdapter::Error> { - let mut buckets = HashSet::new(); - buckets.insert(shard); - let res = self.get_committees_for_shards(epoch, buckets)?; - Ok(res.get(&shard).cloned()) - } - pub fn get_committees( &mut self, epoch: Epoch, sidechain_id: Option<&PublicKey>, - ) -> Result>, TGlobalDbAdapter::Error> { + ) -> Result>, TGlobalDbAdapter::Error> { self.backend .validator_nodes_get_committees_for_epoch(self.tx, epoch, sidechain_id) .map_err(TGlobalDbAdapter::Error::into) @@ -146,12 +135,12 @@ impl<'a, 'tx, TGlobalDbAdapter: GlobalDbAdapter> ValidatorNodeDb<'a, 'tx, TGloba pub fn set_committee_shard( &mut self, substate_address: SubstateAddress, - shard: Shard, + shard_group: ShardGroup, sidechain_id: Option<&PublicKey>, epoch: Epoch, ) -> Result<(), TGlobalDbAdapter::Error> { self.backend - .validator_nodes_set_committee_shard(self.tx, substate_address, shard, sidechain_id, epoch) + .validator_nodes_set_committee_shard(self.tx, substate_address, shard_group, sidechain_id, epoch) .map_err(TGlobalDbAdapter::Error::into) } } diff --git a/dan_layer/storage/src/state_store/mod.rs b/dan_layer/storage/src/state_store/mod.rs index d5ef26b0a..14a848002 100644 --- a/dan_layer/storage/src/state_store/mod.rs +++ b/dan_layer/storage/src/state_store/mod.rs @@ -3,14 +3,14 @@ use std::{ borrow::Borrow, - collections::HashSet, + collections::{HashMap, HashSet}, ops::{Deref, RangeInclusive}, }; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use tari_common_types::types::{FixedHash, PublicKey}; -use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, SubstateAddress}; +use tari_dan_common_types::{shard::Shard, Epoch, NodeAddressable, NodeHeight, ShardGroup, SubstateAddress}; use tari_engine_types::substate::SubstateId; use tari_state_tree::{Node, NodeKey, StaleTreeNode, Version}; use tari_transaction::{SubstateRequirement, TransactionId, VersionedSubstateId}; @@ -48,6 +48,7 @@ use crate::{ TransactionPoolStage, TransactionPoolStatusUpdate, TransactionRecord, + VersionedStateHashTreeDiff, Vote, }, StorageError, @@ -135,13 +136,17 @@ pub trait StateStoreReadTransaction: Sized { from_block_id: &BlockId, ) -> Result; fn blocks_get(&self, block_id: &BlockId) -> Result; - fn blocks_get_tip(&self, epoch: Epoch, shard: Shard) -> Result; - fn blocks_get_last_n_in_epoch(&self, n: usize, epoch: Epoch, shard: Shard) -> Result, StorageError>; + fn blocks_get_last_n_in_epoch( + &self, + n: usize, + epoch: Epoch, + shard_group: ShardGroup, + ) -> Result, StorageError>; /// Returns all blocks from and excluding the start block (lower height) to the end block (inclusive) fn blocks_get_all_between( &self, epoch: Epoch, - shard: Shard, + shard_group: ShardGroup, start_block_id_exclusive: &BlockId, end_block_id_inclusive: &BlockId, include_dummy_blocks: bool, @@ -275,10 +280,8 @@ pub trait StateStoreReadTransaction: Sized { fn pending_state_tree_diffs_exists_for_block(&self, block_id: &BlockId) -> Result; fn pending_state_tree_diffs_get_all_up_to_commit_block( &self, - epoch: Epoch, - shard: Shard, block_id: &BlockId, - ) -> Result, StorageError>; + ) -> Result>, StorageError>; fn state_transitions_get_n_after( &self, @@ -287,9 +290,10 @@ pub trait StateStoreReadTransaction: Sized { end_epoch: Epoch, ) -> Result, StorageError>; - fn state_transitions_get_last_id(&self) -> Result; + fn state_transitions_get_last_id(&self, shard: Shard) -> Result; - fn state_tree_nodes_get(&self, epoch: Epoch, shard: Shard, key: &NodeKey) -> Result, StorageError>; + fn state_tree_nodes_get(&self, shard: Shard, key: &NodeKey) -> Result, StorageError>; + fn state_tree_versions_get_latest(&self, shard: Shard) -> Result, StorageError>; } pub trait StateStoreWriteTransaction { @@ -426,27 +430,22 @@ pub trait StateStoreWriteTransaction { ) -> Result<(), StorageError>; // -------------------------------- Pending State Tree Diffs -------------------------------- // - fn pending_state_tree_diffs_insert(&mut self, diff: &PendingStateTreeDiff) -> Result<(), StorageError>; + fn pending_state_tree_diffs_insert( + &mut self, + block_id: BlockId, + shard: Shard, + diff: VersionedStateHashTreeDiff, + ) -> Result<(), StorageError>; fn pending_state_tree_diffs_remove_by_block( &mut self, block_id: &BlockId, - ) -> Result; + ) -> Result>, StorageError>; //---------------------------------- State tree --------------------------------------------// - fn state_tree_nodes_insert( - &mut self, - epoch: Epoch, - shard: Shard, - key: NodeKey, - node: Node, - ) -> Result<(), StorageError>; + fn state_tree_nodes_insert(&mut self, shard: Shard, key: NodeKey, node: Node) -> Result<(), StorageError>; - fn state_tree_nodes_mark_stale_tree_node( - &mut self, - epoch: Epoch, - shard: Shard, - node: StaleTreeNode, - ) -> Result<(), StorageError>; + fn state_tree_nodes_mark_stale_tree_node(&mut self, shard: Shard, node: StaleTreeNode) -> Result<(), StorageError>; + fn state_tree_shard_versions_set(&mut self, shard: Shard, version: Version) -> Result<(), StorageError>; } #[derive(Debug, Clone, Copy, Serialize, Deserialize)] diff --git a/dan_layer/storage_sqlite/migrations/2024-04-12-000000_create_committes/up.sql b/dan_layer/storage_sqlite/migrations/2024-04-12-000000_create_committes/up.sql index a6122185c..155281236 100644 --- a/dan_layer/storage_sqlite/migrations/2024-04-12-000000_create_committes/up.sql +++ b/dan_layer/storage_sqlite/migrations/2024-04-12-000000_create_committes/up.sql @@ -1,10 +1,11 @@ CREATE TABLE committees ( - id INTEGER PRIMARY KEY autoincrement NOT NULL, - validator_node_id INTEGER NOT NULL, - epoch BIGINT NOT NULL, - committee_bucket BIGINT NOT NULL, + id INTEGER PRIMARY KEY autoincrement NOT NULL, + validator_node_id INTEGER NOT NULL, + epoch BIGINT NOT NULL, + shard_start INTEGER NOT NULL, + shard_end INTEGER NOT NULL, FOREIGN KEY (validator_node_id) REFERENCES validator_nodes (id) ); -CREATE INDEX committees_epoch_index ON committees (epoch); +CREATE INDEX committees_validator_node_id_epoch_index ON committees (validator_node_id, epoch); diff --git a/dan_layer/storage_sqlite/src/global/backend_adapter.rs b/dan_layer/storage_sqlite/src/global/backend_adapter.rs index 4e89c381b..11c3e2bbb 100644 --- a/dan_layer/storage_sqlite/src/global/backend_adapter.rs +++ b/dan_layer/storage_sqlite/src/global/backend_adapter.rs @@ -47,6 +47,7 @@ use tari_dan_common_types::{ shard::Shard, Epoch, NodeAddressable, + ShardGroup, SubstateAddress, }; use tari_dan_storage::{ @@ -460,8 +461,9 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { sidechain_id: Option<&PublicKey>, ) -> Result { let db_sidechain_id = sidechain_id.map(|id| id.as_bytes()).unwrap_or(&[0u8; 32]); + let count = sql_query( - "SELECT COUNT(distinct public_key) as cnt FROM validator_nodes WHERE start_epoch <= ? AND end_epoch <= ? \ + "SELECT COUNT(distinct public_key) as cnt FROM validator_nodes WHERE start_epoch <= ? AND end_epoch >= ? \ AND sidechain_id = ?", ) .bind::(epoch.as_u64() as i64) @@ -476,12 +478,12 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { Ok(count.cnt as u64) } - fn validator_nodes_count_for_bucket( + fn validator_nodes_count_for_shard_group( &self, tx: &mut Self::DbTransaction<'_>, epoch: Epoch, sidechain_id: Option<&PublicKey>, - bucket: Shard, + shard_group: ShardGroup, ) -> Result { use crate::global::schema::{committees, validator_nodes}; @@ -489,7 +491,8 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { let count = committees::table .inner_join(validator_nodes::table.on(committees::validator_node_id.eq(validator_nodes::id))) .filter(committees::epoch.eq(epoch.as_u64() as i64)) - .filter(committees::committee_bucket.eq(i64::from(bucket.as_u32()))) + .filter(committees::shard_start.eq(shard_group.start().as_u32() as i32)) + .filter(committees::shard_end.eq(shard_group.end().as_u32() as i32)) .filter(validator_nodes::sidechain_id.eq(db_sidechain_id)) .count() .limit(1) @@ -507,32 +510,33 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { tx: &mut Self::DbTransaction<'_>, epoch: Epoch, sidechain_id: Option<&PublicKey>, - ) -> Result>, Self::Error> { + ) -> Result>, Self::Error> { use crate::global::schema::{committees, validator_nodes}; let db_sidechain_id = sidechain_id.map(|id| id.as_bytes()).unwrap_or(&[0u8; 32]); - let count = committees::table + let results = committees::table .inner_join(validator_nodes::table.on(committees::validator_node_id.eq(validator_nodes::id))) .select(( - committees::committee_bucket, + committees::shard_start, + committees::shard_end, validator_nodes::address, validator_nodes::public_key, )) .filter(committees::epoch.eq(epoch.as_u64() as i64)) .filter(validator_nodes::sidechain_id.eq(db_sidechain_id)) - .load::<(i64, String, Vec)>(tx.connection()) + .load::<(i32, i32, String, Vec)>(tx.connection()) .map_err(|source| SqliteStorageError::DieselError { source, operation: "validator_nodes_get_committees".to_string(), })?; let mut committees = HashMap::new(); - for (shard, address, public_key) in count { + for (shard_start, shard_end, address, public_key) in results { let addr = DbValidatorNode::try_parse_address(&address)?; let pk = PublicKey::from_canonical_bytes(&public_key) .map_err(|_| SqliteStorageError::MalformedDbData("Invalid public key".to_string()))?; committees - .entry(Shard::from(shard as u32)) + .entry(ShardGroup::new(shard_start as u32, shard_end as u32)) .or_insert_with(Committee::empty) .members .push((addr, pk)); @@ -545,7 +549,7 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { &self, tx: &mut Self::DbTransaction<'_>, shard_key: SubstateAddress, - shard: Shard, + shard_group: ShardGroup, sidechain_id: Option<&PublicKey>, epoch: Epoch, ) -> Result<(), Self::Error> { @@ -565,11 +569,13 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { source, operation: "validator_nodes_set_committee_bucket".to_string(), })?; + diesel::insert_into(committees::table) .values(( committees::validator_node_id.eq(validator_id), committees::epoch.eq(epoch.as_u64() as i64), - committees::committee_bucket.eq(i64::from(shard.as_u32())), + committees::shard_start.eq(shard_group.start().as_u32() as i32), + committees::shard_end.eq(shard_group.end().as_u32() as i32), )) .execute(tx.connection()) .map_err(|source| SqliteStorageError::DieselError { @@ -619,43 +625,63 @@ impl GlobalDbAdapter for SqliteGlobalDbAdapter { distinct_validators_sorted(validators) } - fn validator_nodes_get_for_shards( + fn validator_nodes_get_for_shard_group( &self, tx: &mut Self::DbTransaction<'_>, epoch: Epoch, - shards: HashSet, + shard_group: ShardGroup, ) -> Result>, Self::Error> { use crate::global::schema::{committees, validator_nodes}; - let mut shards = shards - .into_iter() - .map(|b| (b, Committee::empty())) - .collect::>(); + // let mut shards = shard_group + // .shard_iter() + // .map(|b| (b, Committee::empty())) + // .collect::>(); - for (shard, committee) in &mut shards { + let mut committees = HashMap::with_capacity(shard_group.len()); + for shard in shard_group.shard_iter() { let validators = validator_nodes::table .left_join(committees::table.on(committees::validator_node_id.eq(validator_nodes::id))) .select(validator_nodes::all_columns) .filter(committees::epoch.eq(epoch.as_u64() as i64)) - .filter(committees::committee_bucket.eq(i64::from(shard.as_u32()))) + .filter(committees::shard_start.le(shard.as_u32() as i32)) + .filter(committees::shard_end.ge(shard.as_u32() as i32)) .get_results::(tx.connection()) .map_err(|source| SqliteStorageError::DieselError { source, operation: "validator_nodes_get_by_buckets".to_string(), })?; - for validator in validators { - committee.members.push(( - DbValidatorNode::try_parse_address(&validator.address)?, - PublicKey::from_canonical_bytes(&validator.public_key).map_err(|_| { - SqliteStorageError::MalformedDbData(format!( - "Invalid public key in validator node record id={}", - validator.id + committees.insert( + shard, + validators + .into_iter() + .map(|validator| { + Ok::<_, SqliteStorageError>(( + DbValidatorNode::try_parse_address(&validator.address)?, + PublicKey::from_canonical_bytes(&validator.public_key).map_err(|_| { + SqliteStorageError::MalformedDbData(format!( + "Invalid public key in validator node record id={}", + validator.id + )) + })?, )) - })?, - )); - } + }) + .collect::>()?, + ); + + // for validator in validators { + // committee.members.push(( + // DbValidatorNode::try_parse_address(&validator.address)?, + // PublicKey::from_canonical_bytes(&validator.public_key).map_err(|_| { + // SqliteStorageError::MalformedDbData(format!( + // "Invalid public key in validator node record id={}", + // validator.id + // )) + // })?, + // )); + // } } - Ok(shards) + Ok(committees) } fn get_validator_nodes_within_epoch( diff --git a/dan_layer/storage_sqlite/src/global/schema.rs b/dan_layer/storage_sqlite/src/global/schema.rs index 807c9eef3..46544e178 100644 --- a/dan_layer/storage_sqlite/src/global/schema.rs +++ b/dan_layer/storage_sqlite/src/global/schema.rs @@ -19,7 +19,8 @@ diesel::table! { id -> Integer, validator_node_id -> Integer, epoch -> BigInt, - committee_bucket -> BigInt, + shard_start -> Integer, + shard_end -> Integer, } } @@ -69,6 +70,8 @@ diesel::table! { } } +diesel::joinable!(committees -> validator_nodes (validator_node_id)); + diesel::allow_tables_to_appear_in_same_query!( base_layer_block_info, bmt_cache, diff --git a/dan_layer/storage_sqlite/tests/global_db.rs b/dan_layer/storage_sqlite/tests/global_db.rs index 736dc2049..9aa27f5d1 100644 --- a/dan_layer/storage_sqlite/tests/global_db.rs +++ b/dan_layer/storage_sqlite/tests/global_db.rs @@ -5,7 +5,7 @@ use diesel::{Connection, SqliteConnection}; use rand::rngs::OsRng; use tari_common_types::types::PublicKey; use tari_crypto::keys::PublicKey as _; -use tari_dan_common_types::{shard::Shard, Epoch, PeerAddress, SubstateAddress}; +use tari_dan_common_types::{shard::Shard, Epoch, PeerAddress, ShardGroup, SubstateAddress}; use tari_dan_storage::global::{GlobalDb, ValidatorNodeDb}; use tari_dan_storage_sqlite::global::SqliteGlobalDbAdapter; use tari_utilities::ByteArray; @@ -65,14 +65,14 @@ fn insert_vn_with_public_key( .unwrap() } -fn update_committee_bucket( +fn set_committee_shard_group( validator_nodes: &mut ValidatorNodeDb<'_, '_, SqliteGlobalDbAdapter>, public_key: &PublicKey, - committee_bucket: Shard, + shard_group: ShardGroup, epoch: Epoch, ) { validator_nodes - .set_committee_shard(derived_substate_address(public_key), committee_bucket, None, epoch) + .set_committee_shard(derived_substate_address(public_key), shard_group, None, epoch) .unwrap(); } @@ -94,13 +94,13 @@ fn change_committee_bucket() { let mut validator_nodes = db.validator_nodes(&mut tx); let pk = new_public_key(); insert_vn_with_public_key(&mut validator_nodes, pk.clone(), Epoch(0), Epoch(4), None); - update_committee_bucket(&mut validator_nodes, &pk, Shard::from(1), Epoch(0)); - update_committee_bucket(&mut validator_nodes, &pk, Shard::from(3), Epoch(1)); - update_committee_bucket(&mut validator_nodes, &pk, Shard::from(7), Epoch(2)); - update_committee_bucket(&mut validator_nodes, &pk, Shard::from(4), Epoch(3)); + set_committee_shard_group(&mut validator_nodes, &pk, ShardGroup::new(1, 2), Epoch(0)); + set_committee_shard_group(&mut validator_nodes, &pk, ShardGroup::new(3, 4), Epoch(1)); + set_committee_shard_group(&mut validator_nodes, &pk, ShardGroup::new(7, 8), Epoch(2)); + set_committee_shard_group(&mut validator_nodes, &pk, ShardGroup::new(4, 5), Epoch(3)); + set_committee_shard_group(&mut validator_nodes, &pk, ShardGroup::new(4, 5), Epoch(3)); let vns = validator_nodes - .get_committee_for_shard(Epoch(3), Shard::from(4)) - .unwrap() + .get_committees_for_shard_group(Epoch(3), ShardGroup::new(4, 5)) .unwrap(); - assert_eq!(vns.len(), 1); + assert_eq!(vns.get(&Shard::from(4)).unwrap().len(), 2); } diff --git a/dan_layer/transaction/src/substate.rs b/dan_layer/transaction/src/substate.rs index 58ae6a553..d373462b4 100644 --- a/dan_layer/transaction/src/substate.rs +++ b/dan_layer/transaction/src/substate.rs @@ -4,7 +4,7 @@ use std::{borrow::Borrow, fmt::Display, str::FromStr}; use serde::{Deserialize, Serialize}; -use tari_dan_common_types::{shard::Shard, SubstateAddress}; +use tari_dan_common_types::{shard::Shard, NumPreshards, ShardGroup, SubstateAddress}; use tari_engine_types::{serde_with, substate::SubstateId}; #[derive(Debug, Clone, Deserialize, Serialize)] @@ -54,14 +54,20 @@ impl SubstateRequirement { } pub fn to_substate_address(&self) -> Option { - Some(SubstateAddress::from_substate_id(self.substate_id(), self.version()?)) + self.version() + .map(|v| SubstateAddress::from_substate_id(self.substate_id(), v)) } /// Calculates and returns the shard number that this SubstateAddress belongs. - /// A shard is a division of the 256-bit shard space. + /// A shard is a fixed division of the 256-bit shard space. /// If the substate version is not known, None is returned. - pub fn to_committee_shard(&self, num_committees: u32) -> Option { - Some(self.to_substate_address()?.to_shard(num_committees)) + pub fn to_shard(&self, num_shards: NumPreshards) -> Option { + self.to_substate_address().map(|a| a.to_shard(num_shards)) + } + + pub fn to_shard_group(&self, num_shards: NumPreshards, num_committees: u32) -> Option { + self.to_substate_address() + .map(|a| a.to_shard_group(num_shards, num_committees)) } pub fn to_versioned(&self) -> Option { @@ -179,8 +185,12 @@ impl VersionedSubstateId { /// Calculates and returns the shard number that this SubstateAddress belongs. /// A shard is an equal division of the 256-bit shard space. - pub fn to_committee_shard(&self, num_committees: u32) -> Shard { - self.to_substate_address().to_shard(num_committees) + pub fn to_shard(&self, num_shards: NumPreshards) -> Shard { + self.to_substate_address().to_shard(num_shards) + } + + pub fn to_shard_group(&self, num_shards: NumPreshards, num_committees: u32) -> ShardGroup { + self.to_substate_address().to_shard_group(num_shards, num_committees) } pub fn to_previous_version(&self) -> Option { diff --git a/dan_layer/transaction/src/transaction.rs b/dan_layer/transaction/src/transaction.rs index 2fb0be789..43bba1a01 100644 --- a/dan_layer/transaction/src/transaction.rs +++ b/dan_layer/transaction/src/transaction.rs @@ -114,10 +114,6 @@ impl Transaction { self.signatures().iter().all(|sig| sig.verify(&self.transaction)) } - pub fn involved_shards_iter(&self) -> impl Iterator + '_ { - self.versioned_input_addresses_iter() - } - pub fn inputs(&self) -> &IndexSet { &self.transaction.inputs } diff --git a/dan_layer/wallet/crypto/Cargo.toml b/dan_layer/wallet/crypto/Cargo.toml index ca6455913..e1f50a481 100644 --- a/dan_layer/wallet/crypto/Cargo.toml +++ b/dan_layer/wallet/crypto/Cargo.toml @@ -19,7 +19,6 @@ digest = { workspace = true } rand = { workspace = true } thiserror = { workspace = true } zeroize = { workspace = true } -log = { workspace = true } [dev-dependencies] tari_template_test_tooling = { workspace = true } diff --git a/dan_layer/wallet/crypto/src/api.rs b/dan_layer/wallet/crypto/src/api.rs index abd45e033..98f557e2a 100644 --- a/dan_layer/wallet/crypto/src/api.rs +++ b/dan_layer/wallet/crypto/src/api.rs @@ -159,11 +159,6 @@ fn generate_balance_proof( } let excess = RistrettoPublicKey::from_secret_key(&secret_excess); let (nonce, public_nonce) = RistrettoPublicKey::random_keypair(&mut OsRng); - const LOG_TARGET: &str = "tari::dan::wallet::confidential::withdraw"; - log::error!(target: LOG_TARGET, "๐ŸžW public_excess: {excess}"); - log::error!(target: LOG_TARGET, "๐ŸžW public_nonce: {}", public_nonce); - log::error!(target: LOG_TARGET, "๐ŸžW input_revealed_amount: {input_revealed_amount}"); - log::error!(target: LOG_TARGET, "๐ŸžW output_revealed_amount: {output_reveal_amount}"); let message = challenges::confidential_withdraw64(&excess, &public_nonce, input_revealed_amount, output_reveal_amount);