Skip to content

Commit

Permalink
[feature] hyperledger-iroha#3082: Provide full data in Created event
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
  • Loading branch information
Erigara committed Jan 27, 2023
1 parent c8e84a7 commit 10c2a94
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 47 deletions.
7 changes: 4 additions & 3 deletions client/tests/integration/events/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ fn transaction_execution_should_produce_events(executable: Executable, port: u16
// assertion
for i in 0..4_usize {
let domain_id = DomainId::new(i.to_string().parse().expect("Valid"));
let expected_event = DomainEvent::Created(domain_id).into();
let domain = Domain::new(domain_id);
let expected_event = DomainEvent::Created(domain).into();
let event: DataEvent = event_receiver.recv()??.try_into()?;
assert_eq!(event, expected_event);
}
Expand Down Expand Up @@ -168,7 +169,7 @@ fn produce_multiple_events() -> Result<()> {
let instructions = [
RegisterBox::new(permission_token_definition_1.clone()).into(),
RegisterBox::new(permission_token_definition_2.clone()).into(),
RegisterBox::new(role).into(),
RegisterBox::new(role.clone()).into(),
];
client.submit_all_blocking(instructions)?;

Expand All @@ -189,7 +190,7 @@ fn produce_multiple_events() -> Result<()> {
WorldEvent::PermissionToken(PermissionTokenEvent::DefinitionCreated(
permission_token_definition_2,
)),
WorldEvent::Role(RoleEvent::Created(role_id.clone())),
WorldEvent::Role(RoleEvent::Created(role)),
WorldEvent::Domain(DomainEvent::Account(AccountEvent::PermissionAdded(
AccountPermissionChanged {
account_id: alice_id.clone(),
Expand Down
8 changes: 4 additions & 4 deletions core/src/smartcontracts/isi/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod isi {
_authority: <Account as Identifiable>::Id,
wsv: &WorldStateView,
) -> Result<(), Self::Error> {
let account: Account = self.object.build();
let account: Account = self.object.clone().build();
let account_id = account.id().clone();

account_id
Expand All @@ -42,7 +42,7 @@ pub mod isi {
}

domain.add_account(account);
Ok(DomainEvent::Account(AccountEvent::Created(account_id)))
Ok(DomainEvent::Account(AccountEvent::Created(self.object)))
})
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ pub mod isi {
authority: <Account as Identifiable>::Id,
wsv: &WorldStateView,
) -> Result<(), Self::Error> {
let asset_definition = self.object.build();
let asset_definition = self.object.clone().build();
asset_definition
.id()
.name
Expand Down Expand Up @@ -111,7 +111,7 @@ pub mod isi {

domain.add_asset_definition(asset_definition, authority);
Ok(DomainEvent::AssetDefinition(AssetDefinitionEvent::Created(
asset_definition_id,
self.object,
)))
})
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/smartcontracts/isi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub mod isi {
_authority: <Account as Identifiable>::Id,
wsv: &WorldStateView,
) -> Result<(), Self::Error> {
let domain: Domain = self.object.build();
let domain: Domain = self.object.clone().build();
let domain_id = domain.id().clone();

domain_id
Expand All @@ -82,7 +82,7 @@ pub mod isi {
}

world.domains.insert(domain_id.clone(), domain);
Ok(DomainEvent::Created(domain_id).into())
Ok(DomainEvent::Created(self.object).into())
})?;

Ok(())
Expand Down Expand Up @@ -121,7 +121,7 @@ pub mod isi {
_authority: <Account as Identifiable>::Id,
wsv: &WorldStateView,
) -> Result<(), Self::Error> {
let role = self.object.build();
let role = self.object.clone().build();

for permission in role.permissions() {
let definition = wsv
Expand All @@ -144,7 +144,7 @@ pub mod isi {
let role_id = role.id().clone();
wsv.modify_world(|world| {
world.roles.insert(role_id.clone(), role);
Ok(RoleEvent::Created(role_id).into())
Ok(RoleEvent::Created(self.object).into())
})
}
}
Expand Down
7 changes: 3 additions & 4 deletions core/src/wsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,10 @@ impl WorldStateView {

// This function is strictly infallible.
self.modify_account(&id.account_id, |account| {
assert!(account
.add_asset(Asset::new(id.clone(), default_asset_value.into()))
.is_none());
let asset = Asset::new(id.clone(), default_asset_value.into());
assert!(account.add_asset(asset.clone()).is_none());

Ok(AccountEvent::Asset(AssetEvent::Created(id.clone())))
Ok(AccountEvent::Asset(AssetEvent::Created(asset)))
})
.map_err(|err| {
iroha_logger::warn!(?err);
Expand Down
6 changes: 3 additions & 3 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ declare_item! {
#[display(fmt = "[{id}]")]
pub struct NewAccount {
/// Identification
id: <Account as Identifiable>::Id,
pub id: <Account as Identifiable>::Id,
/// Signatories, i.e. signatures attached to this message.
signatories: Signatories,
pub signatories: Signatories,
/// Metadata that should be submitted with the builder
metadata: Metadata,
pub metadata: Metadata,
}
}

Expand Down
8 changes: 6 additions & 2 deletions data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ declare_item! {
pub struct Asset {
/// Component Identification.
#[getset(skip)]
id: Id,
pub id: Id,
/// Asset's Quantity.
value: AssetValue,
pub value: AssetValue,
}
}

Expand Down Expand Up @@ -501,9 +501,13 @@ declare_item! {
)]
#[display(fmt = "{id} {mintable}{value_type}")]
pub struct NewAssetDefinition {
/// The identification associated with the asset definition builder.
id: <AssetDefinition as Identifiable>::Id,
/// The type value associated with the asset definition builder.
value_type: AssetValueType,
/// The mintablility associated with the asset definition builder.
mintable: Mintable,
/// Metadata associated with the asset definition builder.
metadata: Metadata,
}
}
Expand Down
6 changes: 3 additions & 3 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ declare_item! {
#[display(fmt = "[{id}]")]
pub struct NewDomain {
/// The identification associated with the domain builder.
id: <Domain as Identifiable>::Id,
pub id: <Domain as Identifiable>::Id,
/// The (IPFS) link to the logo of this domain.
logo: Option<IpfsPath>,
pub logo: Option<IpfsPath>,
/// Metadata associated with the domain builder.
metadata: Metadata,
pub metadata: Metadata,
}
}

Expand Down
33 changes: 18 additions & 15 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod asset {
//! This module contains `AssetEvent`, `AssetDefinitionEvent` and its impls

use super::*;
use crate::asset::NewAssetDefinition;

// type alias required by `Filter` macro
type AssetMetadataChanged = MetadataChanged<AssetId>;
Expand All @@ -55,7 +56,7 @@ mod asset {
#[non_exhaustive]
#[allow(missing_docs)]
pub enum AssetEvent {
Created(AssetId),
Created(Asset),
Deleted(AssetId),
Added(AssetChanged),
Removed(AssetChanged),
Expand All @@ -68,8 +69,8 @@ mod asset {

fn origin_id(&self) -> &<Asset as Identifiable>::Id {
match self {
Self::Created(id)
| Self::Deleted(id)
Self::Created(asset) => asset.id(),
Self::Deleted(id)
| Self::Added(AssetChanged { asset_id: id, .. })
| Self::Removed(AssetChanged { asset_id: id, .. })
| Self::MetadataInserted(MetadataChanged { target_id: id, .. })
Expand All @@ -96,7 +97,7 @@ mod asset {
#[non_exhaustive]
#[allow(missing_docs)]
pub enum AssetDefinitionEvent {
Created(AssetDefinitionId),
Created(NewAssetDefinition),
MintabilityChanged(AssetDefinitionId),
Deleted(AssetDefinitionId),
MetadataInserted(AssetDefinitionMetadataChanged),
Expand All @@ -109,8 +110,8 @@ mod asset {

fn origin_id(&self) -> &<AssetDefinition as Identifiable>::Id {
match self {
Self::Created(id)
| Self::Deleted(id)
Self::Created(asset_definition) => asset_definition.id(),
Self::Deleted(id)
| Self::MintabilityChanged(id)
| Self::MetadataInserted(MetadataChanged { target_id: id, .. })
| Self::MetadataRemoved(MetadataChanged { target_id: id, .. })
Expand Down Expand Up @@ -226,7 +227,7 @@ mod role {
#[non_exhaustive]
#[allow(missing_docs)]
pub enum RoleEvent {
Created(RoleId),
Created(NewRole),
Deleted(RoleId),
/// [`PermissionToken`]s with particular [`Id`](crate::permission::token::Id) were
/// removed from the role.
Expand Down Expand Up @@ -260,8 +261,8 @@ mod role {

fn origin_id(&self) -> &<Role as Identifiable>::Id {
match self {
Self::Created(role_id)
| Self::Deleted(role_id)
Self::Created(role) => role.id(),
Self::Deleted(role_id)
| Self::PermissionRemoved(PermissionRemoved { role_id, .. }) => role_id,
}
}
Expand Down Expand Up @@ -344,6 +345,7 @@ mod account {
//! This module contains `AccountEvent` and its impls

use super::*;
use crate::account::NewAccount;

// type alias required by `Filter` macro
type AccountMetadataChanged = MetadataChanged<AccountId>;
Expand All @@ -368,7 +370,7 @@ mod account {
#[allow(missing_docs)]
pub enum AccountEvent {
Asset(AssetEvent),
Created(AccountId),
Created(NewAccount),
Deleted(AccountId),
AuthenticationAdded(AccountId),
AuthenticationRemoved(AccountId),
Expand All @@ -386,8 +388,8 @@ mod account {
fn origin_id(&self) -> &<Account as Identifiable>::Id {
match self {
Self::Asset(asset) => &asset.origin_id().account_id,
Self::Created(id)
| Self::Deleted(id)
Self::Created(account) => account.id(),
Self::Deleted(id)
| Self::AuthenticationAdded(id)
| Self::AuthenticationRemoved(id)
| Self::PermissionAdded(AccountPermissionChanged { account_id: id, .. })
Expand Down Expand Up @@ -447,6 +449,7 @@ mod domain {
//! This module contains `DomainEvent` and its impls

use super::*;
use crate::domain::NewDomain;

// type alias required by `Filter` macro
type DomainMetadataChanged = MetadataChanged<DomainId>;
Expand All @@ -472,7 +475,7 @@ mod domain {
pub enum DomainEvent {
Account(AccountEvent),
AssetDefinition(AssetDefinitionEvent),
Created(DomainId),
Created(NewDomain),
Deleted(DomainId),
MetadataInserted(DomainMetadataChanged),
MetadataRemoved(DomainMetadataChanged),
Expand All @@ -485,8 +488,8 @@ mod domain {
match self {
Self::Account(account) => &account.origin_id().domain_id,
Self::AssetDefinition(asset_definition) => &asset_definition.origin_id().domain_id,
Self::Created(id)
| Self::Deleted(id)
Self::Created(domain) => domain.id(),
Self::Deleted(id)
| Self::MetadataInserted(MetadataChanged { target_id: id, .. })
| Self::MetadataRemoved(MetadataChanged { target_id: id, .. }) => id,
}
Expand Down
11 changes: 7 additions & 4 deletions data_model/src/events/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,18 @@ mod tests {
let asset_name = "rose".parse().expect("Valid");

let domain_id = DomainId::new(domain_name);
let domain = Domain::new(domain_id.clone());
let account_id = AccountId::new(account_name, domain_id.clone());
let account = Account::new(account_id.clone(), []);
let asset_id = AssetId::new(
AssetDefinitionId::new(asset_name, domain_id.clone()),
AssetDefinitionId::new(asset_name, domain_id),
account_id.clone(),
);
let asset = Asset::new(asset_id, 0u32);

let domain_created = DomainEvent::Created(domain_id);
let account_created = AccountEvent::Created(account_id.clone());
let asset_created = AssetEvent::Created(asset_id);
let domain_created = DomainEvent::Created(domain);
let account_created = AccountEvent::Created(account);
let asset_created = AssetEvent::Created(asset);
let account_asset_created = AccountEvent::Asset(asset_created.clone());
let account_filter = BySome(EntityFilter::ByAccount(BySome(AccountFilter::new(
BySome(OriginFilter(account_id)),
Expand Down
10 changes: 5 additions & 5 deletions docs/source/references/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@
{
"name": "Created",
"discriminant": 1,
"ty": "iroha_data_model::account::Id"
"ty": "iroha_data_model::account::NewAccount"
},
{
"name": "Deleted",
Expand Down Expand Up @@ -1849,7 +1849,7 @@
{
"name": "Created",
"discriminant": 0,
"ty": "iroha_data_model::asset::DefinitionId"
"ty": "iroha_data_model::asset::NewAssetDefinition"
},
{
"name": "MintabilityChanged",
Expand Down Expand Up @@ -1949,7 +1949,7 @@
{
"name": "Created",
"discriminant": 0,
"ty": "iroha_data_model::asset::Id"
"ty": "iroha_data_model::asset::Asset"
},
{
"name": "Deleted",
Expand Down Expand Up @@ -2045,7 +2045,7 @@
{
"name": "Created",
"discriminant": 2,
"ty": "iroha_data_model::domain::Id"
"ty": "iroha_data_model::domain::NewDomain"
},
{
"name": "Deleted",
Expand Down Expand Up @@ -2213,7 +2213,7 @@
{
"name": "Created",
"discriminant": 0,
"ty": "iroha_data_model::role::Id"
"ty": "iroha_data_model::role::NewRole"
},
{
"name": "Deleted",
Expand Down

0 comments on commit 10c2a94

Please sign in to comment.