Skip to content

Commit

Permalink
Implement the calculate spower offchain feature (#944)
Browse files Browse the repository at this point in the history
Implement the feature to support spower calculation offchain, this needs to work together with the crust-spower service https://github.com/crustio/crust-spower.

Main changes:

swork::report_works doesn't invoke market::upsert_replicas and market::delete_replicas anymore.
The crust-spower service will index the work reports from chain, and aggregate multiple work reports, then call the newly added market::update_replicas extrinsic to update the replicas data in batch.
Update the market::calculate_rewards extrinsic to only liquidate, renew, or close file, but do not update replicas and spower anymore.
The crust-spower service will index the market::FilesV2 data from chain, and perform spower calculation for changed files in batch, then call the newly added swork::update_spower extrinsic to update the sworker spower and file spower.
The newly added market::update_replicas extrinsic and swork::update_spower extrinsic can only be called by specific register spower superior account, the account need to be set
Unit tests have been updated per these changes.
  • Loading branch information
wuhaixian1984 authored Jun 29, 2024
1 parent 40ff264 commit b5b7cd8
Show file tree
Hide file tree
Showing 14 changed files with 1,196 additions and 683 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

## Integration test
db/

# logs
*.log
583 changes: 364 additions & 219 deletions cstrml/market/src/lib.rs

Large diffs are not rendered by default.

39 changes: 37 additions & 2 deletions cstrml/market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const MERCHANT: AccountId32 = AccountId32::new([5u8; 32]);
pub const DAVE: AccountId32 = AccountId32::new([6u8; 32]);
pub const FERDIE: AccountId32 = AccountId32::new([7u8; 32]);
pub const ZIKUN: AccountId32 = AccountId32::new([8u8; 32]);
pub const SPOWER: AccountId32 = AccountId32::new([9u8; 32]);

#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode, Default)]
pub struct MockMerchantLedger {
Expand Down Expand Up @@ -301,8 +302,42 @@ pub fn init_swork_setup() {
}

// fake for report_works
pub fn add_who_into_replica(cid: &MerkleRoot, reported_size: u64, who: AccountId, owner: AccountId, anchor: SworkerAnchor, created_at: Option<u32>, _maybe_members: Option<BTreeSet<AccountId>>) -> u64 {
Market::upsert_replica(&who, owner, cid, reported_size, &anchor, created_at.unwrap_or(TryInto::<u32>::try_into(System::block_number()).ok().unwrap())).0
pub fn add_who_into_replica(
cid: &MerkleRoot,
reported_size: u64,
who: AccountId,
owner: AccountId,
anchor: SworkerAnchor,
report_slot: ReportSlot,
report_block: BlockNumber,
valid_at: BlockNumber) {

assert_ok!(Market::update_replicas(
Origin::signed(SPOWER.clone()),
vec![(cid.clone(),
reported_size,
vec![(who.clone(), owner.clone(), anchor.clone(), report_slot, report_block, valid_at, true)]
)],
400));
}

pub fn delete_replica(
cid: &MerkleRoot,
reported_size: u64,
who: AccountId,
owner: AccountId,
anchor: SworkerAnchor,
report_slot: ReportSlot,
report_block: BlockNumber,
valid_at: BlockNumber) {

assert_ok!(Market::update_replicas(
Origin::signed(SPOWER.clone()),
vec![(cid.clone(),
reported_size,
vec![(who.clone(), owner.clone(), anchor.clone(), report_slot, report_block, valid_at, false)]
)],
400));
}

pub fn legal_work_report_with_added_files() -> ReportWorksInfo {
Expand Down
Loading

0 comments on commit b5b7cd8

Please sign in to comment.