Skip to content

Commit

Permalink
Added logs for activity (#417)
Browse files Browse the repository at this point in the history
## Description
<!-- Describe what change this PR is implementing -->

## Types of Changes
Please select the branch type you are merging and fill in the relevant
template.
<!--- Check the following box with an x if the following applies: -->
- [ ] Hotfix
- [ ] Release
- [ ] Fix or Feature

## Fix or Feature
<!--- Check the following box with an x if the following applies: -->

### Types of Changes
<!--- What types of changes does your code introduce? -->
- [ ] Tech Debt (Code improvements)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Dependency upgrade (A change in substrate or any 3rd party crate
version)

### Migrations and Hooks
<!--- Check the following box with an x if the following applies: -->
- [ ] This change requires a runtime migration.
- [ ] Modifies `on_initialize`
- [ ] Modifies `on_finalize`

### Checklist for Fix or Feature
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] Change has been tested locally.
- [ ] Change adds / updates tests if applicable.
- [ ] Changelog doc updated.
- [ ] `spec_version` has been incremented.
- [ ] `network-relayer`'s
[events](https://github.com/Cerebellum-Network/network-relayer/blob/dev-cere/shared/substrate/events.go)
have been updated according to the blockchain events if applicable.
- [ ] All CI checks have been passed successfully

## Checklist for Hotfix
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] Change has been deployed to Testnet.
- [ ] Change has been tested in Testnet.
- [ ] Changelog has been updated.
- [ ] Crate version has been updated.
- [ ] `spec_version` has been incremented.
- [ ] Transaction version has been updated if required.
- [ ] Pull Request to `dev` has been created.
- [ ] Pull Request to `staging` has been created.
- [ ] `network-relayer`'s
[events](https://github.com/Cerebellum-Network/network-relayer/blob/dev-cere/shared/substrate/events.go)
have been updated according to the blockchain events if applicable.
- [ ] All CI checks have been passed successfully

## Checklist for Release
<!--- All boxes need to be checked. Follow this checklist before
requiring PR review -->
- [ ] Change has been deployed to Devnet.
- [ ] Change has been tested in Devnet.
- [ ] Change has been deployed to Qanet.
- [ ] Change has been tested in Qanet.
- [ ] Change has been deployed to Testnet.
- [ ] Change has been tested in Testnet.
- [ ] Changelog has been updated.
- [ ] Crate version has been updated.
- [ ] Spec version has been updated.
- [ ] Transaction version has been updated if required.
- [ ] All CI checks have been passed successfully
  • Loading branch information
ayushmishra2005 authored Aug 13, 2024
1 parent f164ec0 commit a1b604a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
67 changes: 58 additions & 9 deletions pallets/ddc-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ pub mod pallet {
NotEnoughNodesForConsensus {
cluster_id: ClusterId,
era_id: DdcEra,
id: ActivityHash,
node_id: String,
validator: T::AccountId,
},
/// Not enough buckets for consensus.
NotEnoughBucketsForConsensus {
cluster_id: ClusterId,
era_id: DdcEra,
customer_id: String,
bucket_id: BucketId,
validator: T::AccountId,
},
/// No activity in consensus.
Expand Down Expand Up @@ -292,7 +300,13 @@ pub mod pallet {
NotEnoughNodesForConsensus {
cluster_id: ClusterId,
era_id: DdcEra,
id: ActivityHash,
node_id: String,
},
NotEnoughBucketsForConsensus {
cluster_id: ClusterId,
era_id: DdcEra,
customer_id: String,
bucket_id: BucketId,
},
/// No activity in consensus.
ActivityNotInConsensus {
Expand Down Expand Up @@ -552,6 +566,8 @@ pub mod pallet {
{
fn get_consensus_id<T: Config>(&self) -> ActivityHash;
fn hash<T: Config>(&self) -> ActivityHash;

fn get_consensus_error(&self, cluster_id: ClusterId, era_id: DdcEra) -> OCWError;
}

impl Activity for NodeActivity {
Expand All @@ -562,6 +578,12 @@ pub mod pallet {
fn hash<T: Config>(&self) -> ActivityHash {
T::ActivityHasher::hash(&self.encode()).into()
}

fn get_consensus_error(&self, cluster_id: ClusterId, era_id: DdcEra) -> OCWError {
let node_id = &self.node_id;

OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, node_id: node_id.clone() }
}
}
impl Activity for CustomerActivity {
fn get_consensus_id<T: Config>(&self) -> ActivityHash {
Expand All @@ -573,6 +595,18 @@ pub mod pallet {
fn hash<T: Config>(&self) -> ActivityHash {
T::ActivityHasher::hash(&self.encode()).into()
}

fn get_consensus_error(&self, cluster_id: ClusterId, era_id: DdcEra) -> OCWError {
let customer_id = &self.customer_id;
let bucket_id = &self.bucket_id;

OCWError::NotEnoughBucketsForConsensus {
cluster_id,
era_id,
customer_id: customer_id.clone(),
bucket_id: *bucket_id,
}
}
}

/// Unwrap or send an error log
Expand Down Expand Up @@ -2356,11 +2390,12 @@ pub mod pallet {
// Check if each customer/bucket appears in at least `min_nodes` nodes
for (id, activities) in customer_buckets {
if activities.len() < min_nodes.into() {
errors.push(OCWError::NotEnoughNodesForConsensus {
cluster_id: (*cluster_id),
era_id,
id,
});
let errs: Vec<OCWError> = activities
.into_iter()
.map(|a| a.get_consensus_error(*cluster_id, era_id))
.collect();

errors.extend(errs);
} else if let Some(activity) =
Self::reach_consensus(&activities, min_threshold.into())
{
Expand Down Expand Up @@ -2745,11 +2780,25 @@ pub mod pallet {

for error in errors {
match error {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, id } => {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, node_id } => {
Self::deposit_event(Event::NotEnoughNodesForConsensus {
cluster_id,
era_id,
id,
node_id,
validator: caller.clone(),
});
},
OCWError::NotEnoughBucketsForConsensus {
cluster_id,
era_id,
customer_id,
bucket_id,
} => {
Self::deposit_event(Event::NotEnoughBucketsForConsensus {
cluster_id,
era_id,
customer_id,
bucket_id,
validator: caller.clone(),
});
},
Expand Down
30 changes: 16 additions & 14 deletions pallets/ddc-verification/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,11 @@ fn test_get_consensus_customers_activity_not_enough_nodes() {
);
assert!(result.is_err());
let errors = result.err().unwrap();
assert_eq!(errors.len(), 1);
assert_eq!(errors.len(), 2);
match &errors[0] {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, customers_activity[0].1[0].get_consensus_id::<mock::Test>());
OCWError::NotEnoughBucketsForConsensus { cluster_id, era_id, customer_id, bucket_id } => {
assert_eq!(*customer_id, "0".to_string());
assert_eq!(*bucket_id, 1);
assert_eq!(*cluster_id, cluster_id1);
assert_eq!(*era_id, era_id1);
},
Expand Down Expand Up @@ -697,10 +698,10 @@ fn test_get_consensus_nodes_activity_not_enough_nodes() {
);
assert!(result.is_err());
let errors = result.err().unwrap();
assert_eq!(errors.len(), 1);
assert_eq!(errors.len(), 2);
match &errors[0] {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, nodes_activity[0].1[0].get_consensus_id::<mock::Test>());
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, node_id } => {
assert_eq!(*node_id, "0".to_string());
assert_eq!(*cluster_id, cluster_id1);
assert_eq!(*era_id, era_id1);
},
Expand Down Expand Up @@ -961,18 +962,19 @@ fn test_get_consensus_customers_activity_diff_errors() {
);
assert!(result.is_err());
let errors = result.err().unwrap();
assert_eq!(errors.len(), 2);
match &errors[1] {
assert_eq!(errors.len(), 3);
match &errors[2] {
OCWError::ActivityNotInConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, customers_activity[0].1[0].get_consensus_id::<mock::Test>());
assert_eq!(*cluster_id, cluster_id1);
assert_eq!(*era_id, era_id1);
},
_ => panic!("Expected CustomerActivityNotInConsensus error"),
}
match &errors[0] {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, customers_activity[3].1[0].get_consensus_id::<mock::Test>());
match &errors[1] {
OCWError::NotEnoughBucketsForConsensus { cluster_id, era_id, customer_id, bucket_id } => {
assert_eq!(*customer_id, "0".to_string());
assert_eq!(*bucket_id, 2);
assert_eq!(*cluster_id, cluster_id1);
assert_eq!(*era_id, era_id1);
},
Expand Down Expand Up @@ -1328,7 +1330,7 @@ fn test_get_consensus_nodes_activity_diff_errors() {
);
assert!(result.is_err());
let errors = result.err().unwrap();
assert_eq!(errors.len(), 2);
assert_eq!(errors.len(), 3);
match &errors[0] {
OCWError::ActivityNotInConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, nodes_activity[0].1[0].get_consensus_id::<mock::Test>());
Expand All @@ -1338,8 +1340,8 @@ fn test_get_consensus_nodes_activity_diff_errors() {
_ => panic!("Expected CustomerActivityNotInConsensus error"),
}
match &errors[1] {
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, id } => {
assert_eq!(*id, nodes_activity[3].1[0].get_consensus_id::<mock::Test>());
OCWError::NotEnoughNodesForConsensus { cluster_id, era_id, node_id } => {
assert_eq!(*node_id, "1".to_string());
assert_eq!(*cluster_id, cluster_id1);
assert_eq!(*era_id, era_id1);
},
Expand Down
2 changes: 1 addition & 1 deletion runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 54115,
spec_version: 54116,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 19,
Expand Down
2 changes: 1 addition & 1 deletion runtime/cere/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 54115,
spec_version: 54116,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 19,
Expand Down

0 comments on commit a1b604a

Please sign in to comment.