Skip to content

Commit

Permalink
fix(market): batch published deals event
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte committed Nov 15, 2024
1 parent 6f5db07 commit c95c422
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
43 changes: 34 additions & 9 deletions pallets/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,7 @@ pub mod pallet {
who: T::AccountId,
amount: BalanceOf<T>,
},
/// Deal has been successfully published between a client and a provider.
DealPublished {
deal_id: DealId,
client: T::AccountId,
provider: T::AccountId,
},
// Deal has been successfully activated.
/// Deal has been successfully activated.
DealActivated {
deal_id: DealId,
client: T::AccountId,
Expand Down Expand Up @@ -404,11 +398,35 @@ pub mod pallet {
client: T::AccountId,
provider: T::AccountId,
},

/// Batch of published deals.
DealsPublished {
provider: T::AccountId,
deals: BoundedVec<PublishedDeal<T>, T::MaxDeals>,
},
}

/// Utility type to ensure that the bound for deal settlement is in sync.
pub type MaxSettleDeals<T> = <T as Config>::MaxDeals;

#[derive(TypeInfo, Encode, Decode, Clone, PartialEq)]
pub struct PublishedDeal<T: Config> {
pub client: T::AccountId,
pub deal_id: DealId,
}

impl<T> core::fmt::Debug for PublishedDeal<T>
where
T: Config,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PublishedDeal")
.field("deal_id", &self.deal_id)
.field("client", &self.client)
.finish()
}
}

/// The data part of the event pushed when the deal is successfully settled.
#[derive(TypeInfo, Encode, Decode, Clone, PartialEq)]
pub struct SettledDealData<T: Config> {
Expand Down Expand Up @@ -890,6 +908,8 @@ pub mod pallet {
let (valid_deals, total_provider_lockup) =
Self::validate_deals(provider.clone(), deals, current_block)?;

let mut published_deals = BoundedVec::new();

// Lock up funds for the clients and emit events
for deal in valid_deals.into_iter() {
// PRE-COND: always succeeds, validated by `validate_deals`
Expand All @@ -913,9 +933,9 @@ pub mod pallet {
Proposals::<T>::insert(deal_id, deal.clone());

// Only deposit the event after storing everything
Self::deposit_event(Event::<T>::DealPublished {
// force_push is ok since the bound is the same as the input one
published_deals.force_push(PublishedDeal {
client: deal.client,
provider: provider.clone(),
deal_id,
});
}
Expand All @@ -924,6 +944,11 @@ pub mod pallet {
// PRE-COND: always succeeds, validated by `validate_deals`
lock_funds::<T>(&provider, total_provider_lockup)?;

Self::deposit_event(Event::<T>::DealsPublished {
deals: published_deals,
provider,
});

Ok(())
}
}
Expand Down
69 changes: 40 additions & 29 deletions pallets/market/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
mock::*,
pallet::{lock_funds, slash_and_burn, unlock_funds},
ActiveDealState, BalanceEntry, BalanceTable, Config, DealSettlementError, DealState,
DealsForBlock, Error, Event, PendingProposals, Proposals, SectorDeals, SectorTerminateError,
SettledDealData,
DealsForBlock, Error, Event, PendingProposals, Proposals, PublishedDeal, SectorDeals,
SectorTerminateError, SettledDealData,
};
#[test]
fn initial_state() {
Expand Down Expand Up @@ -391,10 +391,12 @@ fn publish_storage_deals_fails_different_providers() {
));
assert_eq!(
events(),
[RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: 0,
client: account::<Test>(ALICE),
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(PublishedDeal {
deal_id: 0,
client: account::<Test>(ALICE),
})
})]
);
});
Expand All @@ -421,10 +423,12 @@ fn publish_storage_deals_fails_client_not_enough_funds_for_second_deal() {
));
assert_eq!(
events(),
[RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: 0,
client: account::<Test>(ALICE),
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(PublishedDeal {
deal_id: 0,
client: account::<Test>(ALICE),
})
})]
);
});
Expand Down Expand Up @@ -453,10 +457,12 @@ fn publish_storage_deals_fails_provider_not_enough_funds_for_second_deal() {
));
assert_eq!(
events(),
[RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: 0,
client: account::<Test>(ALICE),
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(PublishedDeal {
deal_id: 0,
client: account::<Test>(ALICE),
})
})]
);
});
Expand All @@ -483,10 +489,12 @@ fn publish_storage_deals_fails_duplicate_deal_in_message() {
));
assert_eq!(
events(),
[RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: 0,
client: account::<Test>(ALICE),
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(PublishedDeal {
deal_id: 0,
client: account::<Test>(ALICE),
})
})]
);
});
Expand All @@ -508,10 +516,12 @@ fn publish_storage_deals_fails_duplicate_deal_in_state() {
));
assert_eq!(
events(),
[RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: 0,
client: account::<Test>(ALICE),
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(PublishedDeal {
deal_id: 0,
client: account::<Test>(ALICE),
})
})]
);
assert_noop!(
Expand Down Expand Up @@ -583,18 +593,19 @@ fn publish_storage_deals() {

assert_eq!(
events(),
[
RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: alice_deal_id,
client: account::<Test>(ALICE),
provider: account::<Test>(PROVIDER),
}),
RuntimeEvent::Market(Event::<Test>::DealPublished {
deal_id: bob_deal_id,
client: account::<Test>(BOB),
provider: account::<Test>(PROVIDER),
}),
]
[RuntimeEvent::Market(Event::<Test>::DealsPublished {
provider: account::<Test>(PROVIDER),
deals: bounded_vec!(
PublishedDeal {
deal_id: alice_deal_id,
client: account::<Test>(ALICE),
},
PublishedDeal {
deal_id: bob_deal_id,
client: account::<Test>(BOB),
}
)
}),]
);
assert!(PendingProposals::<Test>::get().contains(&alice_hash));
assert!(PendingProposals::<Test>::get().contains(&bob_hash));
Expand Down

0 comments on commit c95c422

Please sign in to comment.