Skip to content

Commit

Permalink
empty activity for all nodes support (#382)
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
- [x] 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 -->
- [ ] 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
aie0 authored Jul 2, 2024
1 parent bcbee27 commit aac18ae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pallets/ddc-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,10 @@ pub mod pallet {
pub(crate) fn create_merkle_root(
leaves: &[ActivityHash],
) -> Result<ActivityHash, OCWError> {
if leaves.is_empty() {
return Ok(ActivityHash::default());
}

let store = MemStore::default();
let mut mmr: MMR<ActivityHash, MergeActivityHash, &MemStore<ActivityHash>> =
MemMMR::<_, MergeActivityHash>::new(0, &store);
Expand Down Expand Up @@ -1924,10 +1928,17 @@ pub mod pallet {
// threshold
era_validation.payers_merkle_root_hash = payers_merkle_root_hash;
era_validation.payees_merkle_root_hash = payees_merkle_root_hash;
era_validation.status = EraValidationStatus::ReadyForPayout;
era_validation.start_era = era_activity.start;
era_validation.end_era = era_activity.end;

if payers_merkle_root_hash == ActivityHash::default() &&
payees_merkle_root_hash == payers_merkle_root_hash
{
era_validation.status = EraValidationStatus::PayoutSuccess;
} else {
era_validation.status = EraValidationStatus::ReadyForPayout;
}

should_deposit_ready_event = true;
}

Expand Down
48 changes: 48 additions & 0 deletions pallets/ddc-verification/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,35 @@ fn test_get_consensus_nodes_activity_success() {
assert_eq!(consensus_activities[0].stored_bytes, 100);
}

#[test]
fn test_get_consensus_customers_activity_empty() {
let cluster_id = ClusterId::from([1; 20]);
let era_id = 1;
let min_nodes = 3;
let threshold = Percent::from_percent(67);

let node_pubkey_0 = NodePubKey::StoragePubKey(AccountId32::new([0; 32]));
let node_pubkey_1 = NodePubKey::StoragePubKey(AccountId32::new([1; 32]));
let node_pubkey_2 = NodePubKey::StoragePubKey(AccountId32::new([2; 32]));

let customers_activity = vec![
(node_pubkey_0.clone(), Vec::<CustomerActivity>::new()),
(node_pubkey_1.clone(), Vec::<CustomerActivity>::new()),
(node_pubkey_2.clone(), Vec::<CustomerActivity>::new()),
];

let result = DdcVerification::get_consensus_for_activities(
&cluster_id,
era_id,
&customers_activity,
min_nodes,
threshold,
);
assert!(result.is_ok());
let consensus_activities = result.unwrap();
assert_eq!(consensus_activities.len(), 0);
}

#[test]
fn test_get_consensus_customers_activity_not_enough_nodes() {
let cluster_id1 = ClusterId::from([1; 20]);
Expand Down Expand Up @@ -1055,6 +1084,15 @@ fn test_convert_to_batch_merkle_roots() {
assert_eq!(result_roots, expected_roots);
}

#[test]
fn test_convert_to_batch_merkle_roots_empty() {
let result_roots =
DdcVerification::convert_to_batch_merkle_roots(Vec::<Vec<NodeActivity>>::new()).unwrap();
let expected_roots: Vec<ActivityHash> = Vec::<ActivityHash>::new();

assert_eq!(result_roots, expected_roots);
}

#[test]
fn test_split_to_batches_empty_activities() {
let activities: Vec<NodeActivity> = vec![];
Expand Down Expand Up @@ -1655,6 +1693,16 @@ fn create_merkle_root_works() {
});
}

#[test]
fn create_merkle_root_empty() {
new_test_ext().execute_with(|| {
let leaves = Vec::<ActivityHash>::new();
let root = DdcVerification::create_merkle_root(&leaves).unwrap();

assert_eq!(root, ActivityHash::default());
});
}

#[test]
fn proof_merkle_leaf_works() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit aac18ae

Please sign in to comment.