Skip to content

Commit

Permalink
fix: flatten asset definitions and assets in data model
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <[email protected]>
  • Loading branch information
SamHSmith committed Jul 2, 2024
1 parent 77f68c5 commit 2381938
Show file tree
Hide file tree
Showing 19 changed files with 457 additions and 372 deletions.
1 change: 1 addition & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl Iroha {
let world = World::with(
[genesis_domain(config.genesis.public_key.clone())],
[genesis_account(config.genesis.public_key.clone())],
[],
config
.sumeragi
.trusted_peers
Expand Down
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions core/benches/blocks/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub fn build_state(rt: &tokio::runtime::Handle, account_id: &AccountId) -> State
World::with(
[domain],
[Account::new(account_id.clone()).build(account_id)],
[],
UniqueVec::new(),
),
kura,
Expand Down
13 changes: 9 additions & 4 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ mod tests {
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
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 @@ -1053,7 +1053,7 @@ mod tests {
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
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 @@ -1127,7 +1127,7 @@ mod tests {
let account = Account::new(alice_id.clone()).build(&alice_id);
let domain_id = DomainId::from_str("wonderland").expect("Valid");
let domain = Domain::new(domain_id).build(&alice_id);
let world = World::with([domain], [account], UniqueVec::new());
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 @@ -1208,7 +1208,12 @@ mod tests {
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);
let world = World::with([genesis_domain], [genesis_wrong_account], 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
4 changes: 2 additions & 2 deletions core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ pub mod tests {
let (account_id, _account_keypair) = gen_account_in("wonderland");
let domain = Domain::new(domain_id).build(&account_id);
let account = Account::new(account_id.clone()).build(&account_id);
World::with([domain], [account], PeersIds::new())
World::with([domain], [account], [], PeersIds::new())
}

fn config_factory() -> Config {
Expand Down Expand Up @@ -838,7 +838,7 @@ pub mod tests {
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);
World::with([domain], [alice_account, bob_account], 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
20 changes: 13 additions & 7 deletions core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,9 @@ pub mod isi {

let asset = state_transaction
.world
.account_mut(&asset_id.account)
.and_then(|account| {
account
.remove_asset(&asset_id.definition)
.ok_or_else(|| FindError::Asset(asset_id))
})?;
.assets
.remove(asset_id.clone())
.ok_or_else(|| FindError::Asset(asset_id))?;

match asset.value {
AssetValue::Numeric(increment) => {
Expand Down Expand Up @@ -586,7 +583,16 @@ pub mod query {
state_ro
.world()
.accounts_iter()
.filter(move |account| account.assets.contains_key(&asset_definition_id))
.filter(move |account| {
state_ro
.world()
.assets()
.get(&AssetId::new(
asset_definition_id.clone(),
account.id().clone(),
))
.is_some()
})
.cloned(),
))
}
Expand Down
112 changes: 46 additions & 66 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,18 @@ pub mod isi {

let asset = state_transaction
.world
.account_mut(&asset_id.account)
.and_then(|account| {
account
.remove_asset(&asset_id.definition)
.ok_or_else(|| FindError::Asset(asset_id.clone()))
})?;
.assets
.get_mut(&asset_id)
.ok_or_else(|| FindError::Asset(asset_id.clone()))?;

let destination_store = {
let value = asset.value.clone();

let destination_id =
AssetId::new(asset_id.definition.clone(), self.destination.clone());
let destination_store_asset = state_transaction
.world
.asset_or_insert(destination_id.clone(), asset.value)?;
.asset_or_insert(destination_id.clone(), value)?;

destination_store_asset.clone()
};
Expand Down Expand Up @@ -230,10 +229,10 @@ pub mod isi {
)?;
assert_numeric_spec(&self.object, &asset_definition)?;

let account = state_transaction.world.account_mut(&asset_id.account)?;
let asset = account
let asset = state_transaction
.world
.assets
.get_mut(&asset_id.definition)
.get_mut(&asset_id)
.ok_or_else(|| FindError::Asset(asset_id.clone()))?;
let AssetValue::Numeric(quantity) = &mut asset.value else {
return Err(Error::Conversion("Expected numeric asset type".to_owned()));
Expand All @@ -243,7 +242,11 @@ pub mod isi {
.ok_or(MathError::NotEnoughQuantity)?;

if asset.value.is_zero_value() {
assert!(account.remove_asset(&asset_id.definition).is_some());
assert!(state_transaction
.world
.assets
.remove(asset_id.clone())
.is_some());
}

#[allow(clippy::float_arithmetic)]
Expand Down Expand Up @@ -286,10 +289,10 @@ pub mod isi {
assert_numeric_spec(&self.object, &asset_definition)?;

{
let account = state_transaction.world.account_mut(&source_id.account)?;
let asset = account
let asset = state_transaction
.world
.assets
.get_mut(&source_id.definition)
.get_mut(&source_id)
.ok_or_else(|| FindError::Asset(source_id.clone()))?;
let AssetValue::Numeric(quantity) = &mut asset.value else {
return Err(Error::Conversion("Expected numeric asset type".to_owned()));
Expand All @@ -298,7 +301,11 @@ pub mod isi {
.checked_sub(self.object)
.ok_or(MathError::NotEnoughQuantity)?;
if asset.value.is_zero_value() {
assert!(account.remove_asset(&source_id.definition).is_some());
assert!(state_transaction
.world
.assets
.remove(source_id.clone())
.is_some());
}
}

Expand Down Expand Up @@ -430,13 +437,7 @@ pub mod query {
&self,
state_ro: &'state impl StateReadOnly,
) -> Result<Box<dyn Iterator<Item = Asset> + 'state>, Error> {
Ok(Box::new(
state_ro
.world()
.accounts_iter()
.flat_map(|account| account.assets.values())
.cloned(),
))
Ok(Box::new(state_ro.world().assets_iter().cloned()))
}
}

Expand All @@ -446,13 +447,7 @@ pub mod query {
&self,
state_ro: &'state impl StateReadOnly,
) -> Result<Box<dyn Iterator<Item = AssetDefinition> + 'state>, Error> {
Ok(Box::new(
state_ro
.world()
.domains_iter()
.flat_map(|domain| domain.asset_definitions.values())
.cloned(),
))
Ok(Box::new(state_ro.world().asset_definitions_iter().cloned()))
}
}

Expand Down Expand Up @@ -493,15 +488,8 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.accounts_iter()
.flat_map(move |account| {
let name = name.clone();

account
.assets
.values()
.filter(move |asset| asset.id().definition.name == name)
})
.assets_iter()
.filter(move |asset| asset.id().definition.name == name)
.cloned(),
))
}
Expand All @@ -513,9 +501,16 @@ pub mod query {
&self,
state_ro: &'state impl StateReadOnly,
) -> Result<Box<dyn Iterator<Item = Asset> + 'state>, Error> {
let id = &self.account;
let id = self.account.clone();
iroha_logger::trace!(%id);
Ok(Box::new(state_ro.world().account_assets(id)?.cloned()))
let _ = state_ro.world().map_account(&id, |_| ())?;
Ok(Box::new(
state_ro
.world()
.assets_iter()
.filter(move |asset| asset.id().account == id)
.cloned(),
))
}
}

Expand All @@ -530,15 +525,8 @@ pub mod query {
Ok(Box::new(
state_ro
.world()
.accounts_iter()
.flat_map(move |account| {
let id = id.clone();

account
.assets
.values()
.filter(move |asset| asset.id().definition == id)
})
.assets_iter()
.filter(move |asset| asset.id().definition == id)
.cloned(),
))
}
Expand All @@ -550,13 +538,13 @@ pub mod query {
&self,
state_ro: &'state impl StateReadOnly,
) -> Result<Box<dyn Iterator<Item = Asset> + 'state>, Error> {
let id = &self.domain;
let id = self.domain.clone();
iroha_logger::trace!(%id);
Ok(Box::new(
state_ro
.world()
.accounts_in_domain_iter(id)
.flat_map(|account| account.assets.values())
.assets_iter()
.filter(move |asset| asset.id().account.domain() == &id)
.cloned(),
))
}
Expand All @@ -570,24 +558,16 @@ pub mod query {
) -> Result<Box<dyn Iterator<Item = Asset> + 'state>, Error> {
let domain_id = self.domain.clone();
let asset_definition_id = self.asset_definition.clone();
let domain = state_ro.world().domain(&domain_id)?;
let _definition = domain
.asset_definitions
.get(&asset_definition_id)
.ok_or_else(|| FindError::AssetDefinition(asset_definition_id.clone()))?;
let _ = state_ro.world().domain(&domain_id)?;
let _ = state_ro.world().asset_definition(&asset_definition_id)?;
iroha_logger::trace!(%domain_id, %asset_definition_id);
Ok(Box::new(
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();

account.assets.values().filter(move |asset| {
asset.id().account.domain == domain_id
&& asset.id().definition == asset_definition_id
})
.assets_iter()
.filter(move |asset| {
asset.id().definition == asset_definition_id
&& asset.id().account.domain() == &domain_id
})
.cloned(),
))
Expand Down
Loading

0 comments on commit 2381938

Please sign in to comment.