Skip to content

Commit c52a7d6

Browse files
committed
refactor: Make domain structure shallow
Signed-off-by: Sam H. Smith <[email protected]>
1 parent 0424c39 commit c52a7d6

File tree

21 files changed

+234
-206
lines changed

21 files changed

+234
-206
lines changed

cli/src/lib.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ impl Iroha {
259259
let (events_sender, _) = broadcast::channel(10000);
260260
let world = World::with(
261261
[genesis_domain(config.genesis.public_key.clone())],
262+
[genesis_account(config.genesis.public_key.clone())],
262263
config
263264
.sumeragi
264265
.trusted_peers
@@ -554,14 +555,7 @@ fn genesis_account(public_key: PublicKey) -> Account {
554555

555556
fn genesis_domain(public_key: PublicKey) -> Domain {
556557
let genesis_account = genesis_account(public_key);
557-
let mut domain =
558-
Domain::new(iroha_genesis::GENESIS_DOMAIN_ID.clone()).build(&genesis_account.id);
559-
560-
domain
561-
.accounts
562-
.insert(genesis_account.id.clone(), genesis_account);
563-
564-
domain
558+
Domain::new(iroha_genesis::GENESIS_DOMAIN_ID.clone()).build(&genesis_account.id)
565559
}
566560

567561
/// Error of [`read_config_and_genesis`]

configs/swarm/executor.wasm

-4.79 KB
Binary file not shown.

core/benches/blocks/common.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ pub fn build_state(rt: &tokio::runtime::Handle, account_id: &AccountId) -> State
172172
let _guard = rt.enter();
173173
LiveQueryStore::test().start()
174174
};
175-
let mut domain = Domain::new(account_id.domain().clone()).build(account_id);
176-
domain.accounts.insert(
177-
account_id.clone(),
178-
Account::new(account_id.clone()).build(account_id),
175+
let domain = Domain::new(account_id.domain().clone()).build(account_id);
176+
let state = State::new(
177+
World::with(
178+
[domain],
179+
[Account::new(account_id.clone()).build(account_id)],
180+
UniqueVec::new(),
181+
),
182+
kura,
183+
query_handle,
179184
);
180-
let state = State::new(World::with([domain], UniqueVec::new()), kura, query_handle);
181185

182186
{
183187
let mut state_block = state.block();

core/benches/validation.rs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ fn build_test_and_transient_state() -> State {
5151
let (account_id, _account_keypair) = gen_account_in(&*STARTER_DOMAIN);
5252
let mut domain = Domain::new(STARTER_DOMAIN.clone()).build(&account_id);
5353
let account = Account::new(account_id.clone()).build(&account_id);
54-
assert!(domain.add_account(account).is_none());
5554
World::with([domain], UniqueVec::new())
5655
},
5756
kura,

core/src/block.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,8 @@ mod tests {
10041004
let (alice_id, alice_keypair) = gen_account_in("wonderland");
10051005
let account = Account::new(alice_id.clone()).build(&alice_id);
10061006
let domain_id = DomainId::from_str("wonderland").expect("Valid");
1007-
let mut domain = Domain::new(domain_id).build(&alice_id);
1008-
assert!(domain.add_account(account).is_none());
1009-
let world = World::with([domain], UniqueVec::new());
1007+
let domain = Domain::new(domain_id).build(&alice_id);
1008+
let world = World::with([domain], [account], UniqueVec::new());
10101009
let kura = Kura::blank_kura_for_testing();
10111010
let query_handle = LiveQueryStore::test().start();
10121011
let state = State::new(world, kura, query_handle);
@@ -1061,9 +1060,8 @@ mod tests {
10611060
let (alice_id, alice_keypair) = gen_account_in("wonderland");
10621061
let account = Account::new(alice_id.clone()).build(&alice_id);
10631062
let domain_id = DomainId::from_str("wonderland").expect("Valid");
1064-
let mut domain = Domain::new(domain_id).build(&alice_id);
1065-
assert!(domain.add_account(account).is_none());
1066-
let world = World::with([domain], UniqueVec::new());
1063+
let domain = Domain::new(domain_id).build(&alice_id);
1064+
let world = World::with([domain], [account], UniqueVec::new());
10671065
let kura = Kura::blank_kura_for_testing();
10681066
let query_handle = LiveQueryStore::test().start();
10691067
let state = State::new(world, kura, query_handle);
@@ -1136,12 +1134,8 @@ mod tests {
11361134
let (alice_id, alice_keypair) = gen_account_in("wonderland");
11371135
let account = Account::new(alice_id.clone()).build(&alice_id);
11381136
let domain_id = DomainId::from_str("wonderland").expect("Valid");
1139-
let mut domain = Domain::new(domain_id).build(&alice_id);
1140-
assert!(
1141-
domain.add_account(account).is_none(),
1142-
"{alice_id} already exist in the blockchain"
1143-
);
1144-
let world = World::with([domain], UniqueVec::new());
1137+
let domain = Domain::new(domain_id).build(&alice_id);
1138+
let world = World::with([domain], [account], UniqueVec::new());
11451139
let kura = Kura::blank_kura_for_testing();
11461140
let query_handle = LiveQueryStore::test().start();
11471141
let state = State::new(world, kura, query_handle);
@@ -1219,12 +1213,11 @@ mod tests {
12191213
GENESIS_DOMAIN_ID.clone(),
12201214
genesis_wrong_key.public_key().clone(),
12211215
);
1222-
let mut genesis_domain =
1216+
let genesis_domain =
12231217
Domain::new(GENESIS_DOMAIN_ID.clone()).build(&genesis_correct_account_id);
12241218
let genesis_wrong_account =
12251219
Account::new(genesis_wrong_account_id.clone()).build(&genesis_wrong_account_id);
1226-
assert!(genesis_domain.add_account(genesis_wrong_account).is_none(),);
1227-
let world = World::with([genesis_domain], UniqueVec::new());
1220+
let world = World::with([genesis_domain], [genesis_wrong_account], UniqueVec::new());
12281221
let kura = Kura::blank_kura_for_testing();
12291222
let query_handle = LiveQueryStore::test().start();
12301223
let state = State::new(world, kura, query_handle);

core/src/metrics.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ impl MetricsReporter {
137137
.accounts
138138
.get_metric_with_label_values(&[domain.id.name.as_ref()])
139139
.wrap_err("Failed to compose domains")?
140-
.set(domain.accounts.len() as u64);
140+
.set(
141+
state_view
142+
.world()
143+
.accounts_in_domain_iter(&domain.id)
144+
.count() as u64,
145+
);
141146
}
142147

143148
self.metrics.queue_size.set(self.queue.tx_len() as u64);

core/src/queue.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,9 @@ pub mod tests {
437437
pub fn world_with_test_domains() -> World {
438438
let domain_id = DomainId::from_str("wonderland").expect("Valid");
439439
let (account_id, _account_keypair) = gen_account_in("wonderland");
440-
let mut domain = Domain::new(domain_id).build(&account_id);
440+
let domain = Domain::new(domain_id).build(&account_id);
441441
let account = Account::new(account_id.clone()).build(&account_id);
442-
assert!(domain.add_account(account).is_none());
443-
World::with([domain], PeersIds::new())
442+
World::with([domain], [account], PeersIds::new())
444443
}
445444

446445
fn config_factory() -> Config {
@@ -833,12 +832,10 @@ pub mod tests {
833832
let (bob_id, bob_keypair) = gen_account_in("wonderland");
834833
let world = {
835834
let domain_id = DomainId::from_str("wonderland").expect("Valid");
836-
let mut domain = Domain::new(domain_id).build(&alice_id);
835+
let domain = Domain::new(domain_id).build(&alice_id);
837836
let alice_account = Account::new(alice_id.clone()).build(&alice_id);
838837
let bob_account = Account::new(bob_id.clone()).build(&bob_id);
839-
assert!(domain.add_account(alice_account).is_none());
840-
assert!(domain.add_account(bob_account).is_none());
841-
World::with([domain], PeersIds::new())
838+
World::with([domain], [alice_account, bob_account], PeersIds::new())
842839
};
843840
let query_handle = LiveQueryStore::test().start();
844841
let state = State::new(world, kura, query_handle);

core/src/smartcontracts/isi/account.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,7 @@ pub mod query {
546546
&self,
547547
state_ro: &'state impl StateReadOnly,
548548
) -> Result<Box<dyn Iterator<Item = Account> + 'state>, Error> {
549-
Ok(Box::new(
550-
state_ro
551-
.world()
552-
.domains_iter()
553-
.flat_map(|domain| domain.accounts.values())
554-
.cloned(),
555-
))
549+
Ok(Box::new(state_ro.world().accounts_iter().cloned()))
556550
}
557551
}
558552

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

579577
iroha_logger::trace!(%id);
580578
Ok(Box::new(
581-
state_ro.world().domain(id)?.accounts.values().cloned(),
579+
state_ro.world().accounts_in_domain_iter(id).cloned(),
582580
))
583581
}
584582
}
@@ -609,11 +607,8 @@ pub mod query {
609607
Ok(Box::new(
610608
state_ro
611609
.world()
612-
.map_domain(&asset_definition_id.domain.clone(), move |domain| {
613-
domain.accounts.values().filter(move |account| {
614-
account.assets.contains_key(&asset_definition_id)
615-
})
616-
})?
610+
.accounts_iter()
611+
.filter(move |account| account.assets.contains_key(&asset_definition_id))
617612
.cloned(),
618613
))
619614
}

core/src/smartcontracts/isi/asset.rs

+18-33
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,8 @@ pub mod query {
442442
Ok(Box::new(
443443
state_ro
444444
.world()
445-
.domains_iter()
446-
.flat_map(|domain| {
447-
domain
448-
.accounts
449-
.values()
450-
.flat_map(|account| account.assets.values())
451-
})
445+
.accounts_iter()
446+
.flat_map(|account| account.assets.values())
452447
.cloned(),
453448
))
454449
}
@@ -507,18 +502,14 @@ pub mod query {
507502
Ok(Box::new(
508503
state_ro
509504
.world()
510-
.domains_iter()
511-
.flat_map(move |domain| {
505+
.accounts_iter()
506+
.flat_map(move |account| {
512507
let name = name.clone();
513508

514-
domain.accounts.values().flat_map(move |account| {
515-
let name = name.clone();
516-
517-
account
518-
.assets
519-
.values()
520-
.filter(move |asset| asset.id().definition.name == name)
521-
})
509+
account
510+
.assets
511+
.values()
512+
.filter(move |asset| asset.id().definition.name == name)
522513
})
523514
.cloned(),
524515
))
@@ -548,18 +539,14 @@ pub mod query {
548539
Ok(Box::new(
549540
state_ro
550541
.world()
551-
.domains_iter()
552-
.flat_map(move |domain| {
542+
.accounts_iter()
543+
.flat_map(move |account| {
553544
let id = id.clone();
554545

555-
domain.accounts.values().flat_map(move |account| {
556-
let id = id.clone();
557-
558-
account
559-
.assets
560-
.values()
561-
.filter(move |asset| asset.id().definition == id)
562-
})
546+
account
547+
.assets
548+
.values()
549+
.filter(move |asset| asset.id().definition == id)
563550
})
564551
.cloned(),
565552
))
@@ -577,9 +564,7 @@ pub mod query {
577564
Ok(Box::new(
578565
state_ro
579566
.world()
580-
.domain(id)?
581-
.accounts
582-
.values()
567+
.accounts_in_domain_iter(id)
583568
.flat_map(|account| account.assets.values())
584569
.cloned(),
585570
))
@@ -601,9 +586,9 @@ pub mod query {
601586
.ok_or_else(|| FindError::AssetDefinition(asset_definition_id.clone()))?;
602587
iroha_logger::trace!(%domain_id, %asset_definition_id);
603588
Ok(Box::new(
604-
domain
605-
.accounts
606-
.values()
589+
state_ro
590+
.world()
591+
.accounts_in_domain_iter(&domain_id)
607592
.flat_map(move |account| {
608593
let domain_id = domain_id.clone();
609594
let asset_definition_id = asset_definition_id.clone();

core/src/smartcontracts/isi/domain.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use eyre::Result;
44
use iroha_data_model::{
5-
account::AccountsMap,
65
asset::{AssetDefinitionsMap, AssetTotalQuantityMap},
76
prelude::*,
87
query::error::FindError,
@@ -19,7 +18,6 @@ impl Registrable for iroha_data_model::domain::NewDomain {
1918
fn build(self, authority: &AccountId) -> Self::Target {
2019
Self::Target {
2120
id: self.id,
22-
accounts: AccountsMap::default(),
2321
asset_definitions: AssetDefinitionsMap::default(),
2422
asset_total_quantities: AssetTotalQuantityMap::default(),
2523
metadata: self.metadata,
@@ -56,15 +54,18 @@ pub mod isi {
5654
));
5755
}
5856

59-
let domain = state_transaction.world.domain_mut(&account_id.domain)?;
60-
if domain.accounts.contains_key(&account_id) {
57+
let _domain = state_transaction.world.domain_mut(&account_id.domain)?;
58+
if state_transaction.world.account(&account_id).is_ok() {
6159
return Err(RepetitionError {
6260
instruction_type: InstructionType::Register,
6361
id: IdBox::AccountId(account_id),
6462
}
6563
.into());
6664
}
67-
domain.add_account(account.clone());
65+
state_transaction
66+
.world
67+
.accounts
68+
.insert(account_id, account.clone());
6869

6970
state_transaction
7071
.world
@@ -103,8 +104,8 @@ pub mod isi {
103104

104105
if state_transaction
105106
.world
106-
.domain_mut(&account_id.domain)?
107-
.remove_account(&account_id)
107+
.accounts
108+
.remove(account_id.clone())
108109
.is_none()
109110
{
110111
return Err(FindError::Account(account_id).into());
@@ -168,22 +169,20 @@ pub mod isi {
168169
let asset_definition_id = self.object;
169170

170171
let mut assets_to_remove = Vec::new();
171-
for domain in state_transaction.world.domains_iter() {
172-
for account in domain.accounts.values() {
173-
assets_to_remove.extend(
174-
account
175-
.assets
176-
.values()
177-
.filter_map(|asset| {
178-
if asset.id().definition == asset_definition_id {
179-
return Some(asset.id());
180-
}
181-
182-
None
183-
})
184-
.cloned(),
185-
)
186-
}
172+
for (_, account) in state_transaction.world.accounts.iter() {
173+
assets_to_remove.extend(
174+
account
175+
.assets
176+
.values()
177+
.filter_map(|asset| {
178+
if asset.id().definition == asset_definition_id {
179+
return Some(asset.id());
180+
}
181+
182+
None
183+
})
184+
.cloned(),
185+
)
187186
}
188187

189188
let mut events = Vec::with_capacity(assets_to_remove.len() + 1);

core/src/smartcontracts/isi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ mod tests {
259259
};
260260

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

0 commit comments

Comments
 (0)