Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/better nft events #125

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 114 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = '2021'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/dscp-node/'
name = 'dscp-node'
version = '5.0.2'
version = '5.0.3'

[[bin]]
name = 'dscp-node'
Expand All @@ -15,7 +15,7 @@ name = 'dscp-node'
targets = ['x86_64-unknown-linux-gnu']

[dependencies]
clap = { version = "4.1.11", features = ["derive"] }
clap = { version = "4.2.1", features = ["derive"] }
futures = { version = "0.3.27", features = ["thread-pool"]}
bs58 = "0.4.0"

Expand Down
4 changes: 2 additions & 2 deletions pallets/doas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.158", optional = true }
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.159", optional = true }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false, features = ["derive"] }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
Expand Down
6 changes: 3 additions & 3 deletions pallets/process-validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ edition = '2021'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/dscp-node/'
name = 'pallet-process-validation'
version = "3.4.1"
version = "3.4.2"


[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']

[dependencies]
serde = { version = "1.0.158", optional = true }
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.159", optional = true }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false, features = ["derive"] }
dscp-pallet-traits = { default-features = false, path = '../traits' }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
Expand Down
6 changes: 3 additions & 3 deletions pallets/process-validation/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ benchmarks! {
metadata: BTreeMap::from_iter(vec![(Default::default(), Default::default())])
}; 10];
}: {
let _ = ProcessValidation::<T>::validate_process(process, &account_id, &inputs, &outputs);
let _ = ProcessValidation::<T>::validate_process(&process, &account_id, &inputs, &outputs);
}

validate_process_min {
Expand All @@ -124,7 +124,7 @@ benchmarks! {
metadata: BTreeMap::from_iter(vec![(Default::default(), Default::default())])
}; 10];
}: {
let _ = ProcessValidation::<T>::validate_process(process, &account_id, &inputs, &outputs);
let _ = ProcessValidation::<T>::validate_process(&process, &account_id, &inputs, &outputs);
}

validate_process_max {
Expand All @@ -143,7 +143,7 @@ benchmarks! {
metadata: BTreeMap::from_iter(vec![(Default::default(), Default::default())])
}; 10];
}: {
let _ = ProcessValidation::<T>::validate_process(process, &account_id, &inputs, &outputs);
let _ = ProcessValidation::<T>::validate_process(&process, &account_id, &inputs, &outputs);
}
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/process-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ impl<T: Config> ProcessValidator<T::TokenId, T::AccountId, T::RoleKey, T::TokenM
type Weights = T::WeightInfo;

fn validate_process(
id: ProcessFullyQualifiedId<Self::ProcessIdentifier, Self::ProcessVersion>,
id: &ProcessFullyQualifiedId<Self::ProcessIdentifier, Self::ProcessVersion>,
sender: &T::AccountId,
inputs: &Vec<ProcessIO<T::TokenId, T::AccountId, T::RoleKey, T::TokenMetadataKey, T::TokenMetadataValue>>,
outputs: &Vec<ProcessIO<T::TokenId, T::AccountId, T::RoleKey, T::TokenMetadataKey, T::TokenMetadataValue>>
) -> ValidationResult<u32> {
let maybe_process = <ProcessModel<T>>::try_get(id.id, id.version);
let maybe_process = <ProcessModel<T>>::try_get(id.id.clone(), id.version.clone());

match maybe_process {
Ok(process) => {
Expand Down
24 changes: 12 additions & 12 deletions pallets/process-validation/src/tests/validate_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn it_succeeds_when_process_exists() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -53,7 +53,7 @@ fn it_fails_when_process_id_doesnt_exist() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::B,
version: 1u32
},
Expand Down Expand Up @@ -85,7 +85,7 @@ fn it_fails_when_process_version_doesnt_exist() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 2u32
},
Expand Down Expand Up @@ -117,7 +117,7 @@ fn it_fails_when_process_disabled() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -156,7 +156,7 @@ fn it_succeeds_when_all_restrictions_succeed() {
token_roles.insert(Default::default(), 0u64);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -199,7 +199,7 @@ fn it_fails_when_one_restrictions_fails() {
token_roles.insert(Default::default(), 1u64);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -244,7 +244,7 @@ fn it_succeeds_with_complex_tree() {
token_roles.insert(Default::default(), 1u64);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -289,7 +289,7 @@ fn it_fails_with_complex_tree() {
token_roles.insert(Default::default(), 1u64);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -329,7 +329,7 @@ fn it_succeeds_with_handed_expressions_r() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -369,7 +369,7 @@ fn it_succeeds_with_handed_expressions_l() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -409,7 +409,7 @@ fn it_fails_with_handed_expressions_r() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down Expand Up @@ -449,7 +449,7 @@ fn it_fails_with_handed_expressions_l() {
);

let result = ProcessValidation::validate_process(
ProcessFullyQualifiedId {
&ProcessFullyQualifiedId {
id: ProcessIdentifier::A,
version: 1u32
},
Expand Down
6 changes: 3 additions & 3 deletions pallets/simple-nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ edition = '2021'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/dscp-node/'
name = 'pallet-simple-nft'
version = "5.0.0"
version = "5.0.1"


[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']

[dependencies]
scale-info = { version = "2.3.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false, features = ["derive"] }
dscp-pallet-traits = { default-features = false, path = '../traits' }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
Expand All @@ -24,7 +24,7 @@ sp-std = { default-features = false, version = "5.0.0", git = "https://github.co


[dev-dependencies]
serde = { version = "1.0.158" }
serde = { version = "1.0.159" }
sp-core = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }


Expand Down
49 changes: 32 additions & 17 deletions pallets/simple-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use frame_support::{
BoundedVec
};
pub use pallet::*;
use sp_runtime::traits::{AtLeast32Bit, One};
use sp_runtime::traits::{AtLeast32Bit, Hash, One};

/// A FRAME pallet for handling non-fungible tokens
use sp_std::prelude::*;
Expand Down Expand Up @@ -162,12 +162,14 @@ pub mod pallet {
>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A token was issued.
Minted(T::TokenId, T::AccountId, BoundedVec<T::TokenId, T::MaxInputCount>),
/// A token was burnt.
Burnt(T::TokenId, T::AccountId, BoundedVec<T::TokenId, T::MaxOutputCount>)
/// A process was successfully run
ProcessRan {
sender: T::AccountId,
process: ProcessId<T>,
inputs: BoundedVec<T::TokenId, T::MaxInputCount>,
outputs: BoundedVec<T::TokenId, T::MaxOutputCount>
}
}

#[pallet::error]
Expand Down Expand Up @@ -244,7 +246,7 @@ pub mod pallet {
}
);

let process_is_valid = T::ProcessValidator::validate_process(process, &sender, &io_inputs, &io_outputs);
let process_is_valid = T::ProcessValidator::validate_process(&process, &sender, &io_inputs, &io_outputs);
ensure!(process_is_valid.success, Error::<T>::ProcessInvalid);

// STORAGE MUTATIONS
Expand Down Expand Up @@ -280,21 +282,34 @@ pub mod pallet {
// Update last token
<LastToken<T>>::put(last);

// EVENTS

// Emit events
for token_id in children.iter() {
Self::deposit_event(Event::Minted(*token_id, sender.clone(), inputs.clone()));
}
for token_id in inputs.iter() {
Self::deposit_event(Event::Burnt(*token_id, sender.clone(), children.clone()));
}

let actual_weight = T::WeightInfo::run_process(inputs.len() as u32, outputs.len() as u32)
+ ProcessValidatorWeights::<T>::validate_process(process_is_valid.executed_len)
- ProcessValidatorWeights::<T>::validate_process_min();

// EVENTS
let process_id = &process.id;
let process_version = &process.version;
Self::deposit_event(
vec![
T::Hashing::hash_of(&b"simpleNFT.ProcessRan"),
T::Hashing::hash_of(&(b"simpleNFT.ProcessRan", process_id)),
T::Hashing::hash_of(&(b"simpleNFT.ProcessRan", process_id, process_version)),
],
Event::ProcessRan {
sender,
process,
inputs,
outputs: children
}
);

Ok(Some(actual_weight).into())
}
}
}

impl<T: Config> Pallet<T> {
fn deposit_event(topics: Vec<T::Hash>, event: Event<T>) {
<frame_system::Pallet<T>>::deposit_event_indexed(&topics, <T as Config>::RuntimeEvent::from(event).into())
}
}
9 changes: 7 additions & 2 deletions pallets/simple-nft/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ impl ProcessValidator<u64, u64, Role, u64, MetadataValue<u64>> for MockProcessVa
type Weights = ();

fn validate_process(
id: TestProcessId,
id: &TestProcessId,
_sender: &u64,
_inputs: &Vec<TestProcessIO>,
_outputs: &Vec<TestProcessIO>
) -> ValidationResult<u32> {
ValidationResult {
success: id.id == ProcessIdentifier::ShouldSucceed,
success: id.id.clone() == ProcessIdentifier::ShouldSucceed,
executed_len: 0u32
}
}
Expand Down Expand Up @@ -151,3 +151,8 @@ impl pallet_simple_nft::Config for Test {
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
pub fn run_to_block(n: u64) {
while System::block_number() < n {
System::set_block_number(System::block_number() + 1);
}
}
Loading