Skip to content

Commit

Permalink
refactor: Make domain structure shallow
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <sam.henning.smith@protonmail.com>
  • Loading branch information
SamHSmith committed Jun 21, 2024
1 parent 0424c39 commit 0c07613
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 209 deletions.
10 changes: 2 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ impl Iroha {
let (events_sender, _) = broadcast::channel(10000);
let world = World::with(
[genesis_domain(config.genesis.public_key.clone())],
[genesis_account(config.genesis.public_key.clone())],
config
.sumeragi
.trusted_peers
Expand Down Expand Up @@ -554,14 +555,7 @@ fn genesis_account(public_key: PublicKey) -> Account {

fn genesis_domain(public_key: PublicKey) -> Domain {
let genesis_account = genesis_account(public_key);
let mut domain =
Domain::new(iroha_genesis::GENESIS_DOMAIN_ID.clone()).build(&genesis_account.id);

domain
.accounts
.insert(genesis_account.id.clone(), genesis_account);

domain
Domain::new(iroha_genesis::GENESIS_DOMAIN_ID.clone()).build(&genesis_account.id)
}

/// Error of [`read_config_and_genesis`]
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
14 changes: 9 additions & 5 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ pub fn build_state(rt: &tokio::runtime::Handle, account_id: &AccountId) -> State
let _guard = rt.enter();
LiveQueryStore::test().start()
};
let mut domain = Domain::new(account_id.domain().clone()).build(account_id);
domain.accounts.insert(
account_id.clone(),
Account::new(account_id.clone()).build(account_id),
let domain = Domain::new(account_id.domain().clone()).build(account_id);
let state = State::new(
World::with(
[domain],
[Account::new(account_id.clone()).build(account_id)],
UniqueVec::new(),
),
kura,
query_handle,
);
let state = State::new(World::with([domain], UniqueVec::new()), kura, query_handle);

{
let mut state_block = state.block();
Expand Down
1 change: 0 additions & 1 deletion core/benches/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ fn build_test_and_transient_state() -> State {
let (account_id, _account_keypair) = gen_account_in(&*STARTER_DOMAIN);
let mut domain = Domain::new(STARTER_DOMAIN.clone()).build(&account_id);
let account = Account::new(account_id.clone()).build(&account_id);
assert!(domain.add_account(account).is_none());
World::with([domain], UniqueVec::new())
},
kura,
Expand Down
23 changes: 8 additions & 15 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,8 @@ mod tests {
let (alice_id, alice_keypair) = gen_account_in("wonderland");
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let mut domain = Domain::new(domain_id).build(&alice_id);
assert!(domain.add_account(account).is_none());
let world = World::with([domain], UniqueVec::new());
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
Expand Down Expand Up @@ -1061,9 +1060,8 @@ mod tests {
let (alice_id, alice_keypair) = gen_account_in("wonderland");
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let mut domain = Domain::new(domain_id).build(&alice_id);
assert!(domain.add_account(account).is_none());
let world = World::with([domain], UniqueVec::new());
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
Expand Down Expand Up @@ -1136,12 +1134,8 @@ mod tests {
let (alice_id, alice_keypair) = gen_account_in("wonderland");
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let mut domain = Domain::new(domain_id).build(&alice_id);
assert!(
domain.add_account(account).is_none(),
"{alice_id} already exist in the blockchain"
);
let world = World::with([domain], UniqueVec::new());
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
Expand Down Expand Up @@ -1219,12 +1213,11 @@ mod tests {
GENESIS_DOMAIN_ID.clone(),
genesis_wrong_key.public_key().clone(),
);
let mut genesis_domain =
let genesis_domain =
Domain::new(GENESIS_DOMAIN_ID.clone()).build(&genesis_correct_account_id);
let genesis_wrong_account =
Account::new(genesis_wrong_account_id.clone()).build(&genesis_wrong_account_id);
assert!(genesis_domain.add_account(genesis_wrong_account).is_none(),);
let world = World::with([genesis_domain], UniqueVec::new());
let world = World::with([genesis_domain], [genesis_wrong_account], UniqueVec::new());
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
Expand Down
7 changes: 6 additions & 1 deletion core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ impl MetricsReporter {
.accounts
.get_metric_with_label_values(&[domain.id.name.as_ref()])
.wrap_err("Failed to compose domains")?
.set(domain.accounts.len() as u64);
.set(
state_view
.world()
.accounts_in_domain_iter(&domain.id)
.count() as u64,
);
}

self.metrics.queue_size.set(self.queue.tx_len() as u64);
Expand Down
11 changes: 4 additions & 7 deletions core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,9 @@ pub mod tests {
pub fn world_with_test_domains() -> World {
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let (account_id, _account_keypair) = gen_account_in("wonderland");
let mut domain = Domain::new(domain_id).build(&account_id);
let domain = Domain::new(domain_id).build(&account_id);
let account = Account::new(account_id.clone()).build(&account_id);
assert!(domain.add_account(account).is_none());
World::with([domain], PeersIds::new())
World::with([domain], [account], PeersIds::new())
}

fn config_factory() -> Config {
Expand Down Expand Up @@ -833,12 +832,10 @@ pub mod tests {
let (bob_id, bob_keypair) = gen_account_in("wonderland");
let world = {
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let mut domain = Domain::new(domain_id).build(&alice_id);
let domain = Domain::new(domain_id).build(&alice_id);
let alice_account = Account::new(alice_id.clone()).build(&alice_id);
let bob_account = Account::new(bob_id.clone()).build(&bob_id);
assert!(domain.add_account(alice_account).is_none());
assert!(domain.add_account(bob_account).is_none());
World::with([domain], PeersIds::new())
World::with([domain], [alice_account, bob_account], PeersIds::new())
};
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura, query_handle);
Expand Down
21 changes: 8 additions & 13 deletions core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,7 @@ pub mod query {
&self,
state_ro: &'state impl StateReadOnly,
) -> Result<Box<dyn Iterator<Item = Account> + 'state>, Error> {
Ok(Box::new(
state_ro
.world()
.domains_iter()
.flat_map(|domain| domain.accounts.values())
.cloned(),
))
Ok(Box::new(state_ro.world().accounts_iter().cloned()))
}
}

Expand All @@ -561,6 +555,10 @@ pub mod query {
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Account, Error> {
let id = &self.id;
iroha_logger::trace!(%id);
state_ro
.world()
.domain(id.domain())
.map_err(|_| FindError::Domain(id.domain().clone()))?;
state_ro
.world()
.map_account(id, Clone::clone)
Expand All @@ -578,7 +576,7 @@ pub mod query {

iroha_logger::trace!(%id);
Ok(Box::new(
state_ro.world().domain(id)?.accounts.values().cloned(),
state_ro.world().accounts_in_domain_iter(id).cloned(),
))
}
}
Expand Down Expand Up @@ -609,11 +607,8 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.map_domain(&asset_definition_id.domain.clone(), move |domain| {
domain.accounts.values().filter(move |account| {
account.assets.contains_key(&asset_definition_id)
})
})?
.accounts_iter()
.filter(move |account| account.assets.contains_key(&asset_definition_id))
.cloned(),
))
}
Expand Down
51 changes: 18 additions & 33 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,8 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.domains_iter()
.flat_map(|domain| {
domain
.accounts
.values()
.flat_map(|account| account.assets.values())
})
.accounts_iter()
.flat_map(|account| account.assets.values())
.cloned(),
))
}
Expand Down Expand Up @@ -507,18 +502,14 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.domains_iter()
.flat_map(move |domain| {
.accounts_iter()
.flat_map(move |account| {
let name = name.clone();

domain.accounts.values().flat_map(move |account| {
let name = name.clone();

account
.assets
.values()
.filter(move |asset| asset.id().definition.name == name)
})
account
.assets
.values()
.filter(move |asset| asset.id().definition.name == name)
})
.cloned(),
))
Expand Down Expand Up @@ -548,18 +539,14 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.domains_iter()
.flat_map(move |domain| {
.accounts_iter()
.flat_map(move |account| {
let id = id.clone();

domain.accounts.values().flat_map(move |account| {
let id = id.clone();

account
.assets
.values()
.filter(move |asset| asset.id().definition == id)
})
account
.assets
.values()
.filter(move |asset| asset.id().definition == id)
})
.cloned(),
))
Expand All @@ -577,9 +564,7 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.domain(id)?
.accounts
.values()
.accounts_in_domain_iter(id)
.flat_map(|account| account.assets.values())
.cloned(),
))
Expand All @@ -601,9 +586,9 @@ pub mod query {
.ok_or_else(|| FindError::AssetDefinition(asset_definition_id.clone()))?;
iroha_logger::trace!(%domain_id, %asset_definition_id);
Ok(Box::new(
domain
.accounts
.values()
state_ro
.world()
.accounts_in_domain_iter(&domain_id)
.flat_map(move |account| {
let domain_id = domain_id.clone();
let asset_definition_id = asset_definition_id.clone();
Expand Down
45 changes: 22 additions & 23 deletions core/src/smartcontracts/isi/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use eyre::Result;
use iroha_data_model::{
account::AccountsMap,
asset::{AssetDefinitionsMap, AssetTotalQuantityMap},
prelude::*,
query::error::FindError,
Expand All @@ -19,7 +18,6 @@ impl Registrable for iroha_data_model::domain::NewDomain {
fn build(self, authority: &AccountId) -> Self::Target {
Self::Target {
id: self.id,
accounts: AccountsMap::default(),
asset_definitions: AssetDefinitionsMap::default(),
asset_total_quantities: AssetTotalQuantityMap::default(),
metadata: self.metadata,
Expand Down Expand Up @@ -56,15 +54,18 @@ pub mod isi {
));
}

let domain = state_transaction.world.domain_mut(&account_id.domain)?;
if domain.accounts.contains_key(&account_id) {
let _domain = state_transaction.world.domain_mut(&account_id.domain)?;
if state_transaction.world.account(&account_id).is_ok() {
return Err(RepetitionError {
instruction_type: InstructionType::Register,
id: IdBox::AccountId(account_id),
}
.into());
}
domain.add_account(account.clone());
state_transaction
.world
.accounts
.insert(account_id, account.clone());

state_transaction
.world
Expand Down Expand Up @@ -103,8 +104,8 @@ pub mod isi {

if state_transaction
.world
.domain_mut(&account_id.domain)?
.remove_account(&account_id)
.accounts
.remove(account_id.clone())
.is_none()
{
return Err(FindError::Account(account_id).into());
Expand Down Expand Up @@ -168,22 +169,20 @@ pub mod isi {
let asset_definition_id = self.object;

let mut assets_to_remove = Vec::new();
for domain in state_transaction.world.domains_iter() {
for account in domain.accounts.values() {
assets_to_remove.extend(
account
.assets
.values()
.filter_map(|asset| {
if asset.id().definition == asset_definition_id {
return Some(asset.id());
}

None
})
.cloned(),
)
}
for (_, account) in state_transaction.world.accounts.iter() {
assets_to_remove.extend(
account
.assets
.values()
.filter_map(|asset| {
if asset.id().definition == asset_definition_id {
return Some(asset.id());
}

None
})
.cloned(),
)
}

let mut events = Vec::with_capacity(assets_to_remove.len() + 1);
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ mod tests {
};

fn state_with_test_domains(kura: &Arc<Kura>) -> Result<State> {
let world = World::with([], PeersIds::new());
let world = World::with([], [], PeersIds::new());
let query_handle = LiveQueryStore::test().start();
let state = State::new(world, kura.clone(), query_handle);
let asset_definition_id = AssetDefinitionId::from_str("rose#wonderland")?;
Expand Down
Loading

0 comments on commit 0c07613

Please sign in to comment.