diff --git a/pallets/market/src/lib.rs b/pallets/market/src/lib.rs index d36225f7..62fbb31a 100644 --- a/pallets/market/src/lib.rs +++ b/pallets/market/src/lib.rs @@ -362,13 +362,7 @@ pub mod pallet { who: T::AccountId, amount: BalanceOf, }, - /// 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, @@ -404,11 +398,35 @@ pub mod pallet { client: T::AccountId, provider: T::AccountId, }, + + /// Batch of published deals. + DealsPublished { + provider: T::AccountId, + deals: BoundedVec, T::MaxDeals>, + }, } /// Utility type to ensure that the bound for deal settlement is in sync. pub type MaxSettleDeals = ::MaxDeals; + #[derive(TypeInfo, Encode, Decode, Clone, PartialEq)] + pub struct PublishedDeal { + pub client: T::AccountId, + pub deal_id: DealId, + } + + impl core::fmt::Debug for PublishedDeal + 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 { @@ -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` @@ -913,9 +933,9 @@ pub mod pallet { Proposals::::insert(deal_id, deal.clone()); // Only deposit the event after storing everything - Self::deposit_event(Event::::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, }); } @@ -924,6 +944,11 @@ pub mod pallet { // PRE-COND: always succeeds, validated by `validate_deals` lock_funds::(&provider, total_provider_lockup)?; + Self::deposit_event(Event::::DealsPublished { + deals: published_deals, + provider, + }); + Ok(()) } } diff --git a/pallets/market/src/test.rs b/pallets/market/src/test.rs index 35fb235a..f6435007 100644 --- a/pallets/market/src/test.rs +++ b/pallets/market/src/test.rs @@ -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() { @@ -391,10 +391,12 @@ fn publish_storage_deals_fails_different_providers() { )); assert_eq!( events(), - [RuntimeEvent::Market(Event::::DealPublished { - deal_id: 0, - client: account::(ALICE), + [RuntimeEvent::Market(Event::::DealsPublished { provider: account::(PROVIDER), + deals: bounded_vec!(PublishedDeal { + deal_id: 0, + client: account::(ALICE), + }) })] ); }); @@ -421,10 +423,12 @@ fn publish_storage_deals_fails_client_not_enough_funds_for_second_deal() { )); assert_eq!( events(), - [RuntimeEvent::Market(Event::::DealPublished { - deal_id: 0, - client: account::(ALICE), + [RuntimeEvent::Market(Event::::DealsPublished { provider: account::(PROVIDER), + deals: bounded_vec!(PublishedDeal { + deal_id: 0, + client: account::(ALICE), + }) })] ); }); @@ -453,10 +457,12 @@ fn publish_storage_deals_fails_provider_not_enough_funds_for_second_deal() { )); assert_eq!( events(), - [RuntimeEvent::Market(Event::::DealPublished { - deal_id: 0, - client: account::(ALICE), + [RuntimeEvent::Market(Event::::DealsPublished { provider: account::(PROVIDER), + deals: bounded_vec!(PublishedDeal { + deal_id: 0, + client: account::(ALICE), + }) })] ); }); @@ -483,10 +489,12 @@ fn publish_storage_deals_fails_duplicate_deal_in_message() { )); assert_eq!( events(), - [RuntimeEvent::Market(Event::::DealPublished { - deal_id: 0, - client: account::(ALICE), + [RuntimeEvent::Market(Event::::DealsPublished { provider: account::(PROVIDER), + deals: bounded_vec!(PublishedDeal { + deal_id: 0, + client: account::(ALICE), + }) })] ); }); @@ -508,10 +516,12 @@ fn publish_storage_deals_fails_duplicate_deal_in_state() { )); assert_eq!( events(), - [RuntimeEvent::Market(Event::::DealPublished { - deal_id: 0, - client: account::(ALICE), + [RuntimeEvent::Market(Event::::DealsPublished { provider: account::(PROVIDER), + deals: bounded_vec!(PublishedDeal { + deal_id: 0, + client: account::(ALICE), + }) })] ); assert_noop!( @@ -583,18 +593,19 @@ fn publish_storage_deals() { assert_eq!( events(), - [ - RuntimeEvent::Market(Event::::DealPublished { - deal_id: alice_deal_id, - client: account::(ALICE), - provider: account::(PROVIDER), - }), - RuntimeEvent::Market(Event::::DealPublished { - deal_id: bob_deal_id, - client: account::(BOB), - provider: account::(PROVIDER), - }), - ] + [RuntimeEvent::Market(Event::::DealsPublished { + provider: account::(PROVIDER), + deals: bounded_vec!( + PublishedDeal { + deal_id: alice_deal_id, + client: account::(ALICE), + }, + PublishedDeal { + deal_id: bob_deal_id, + client: account::(BOB), + } + ) + }),] ); assert!(PendingProposals::::get().contains(&alice_hash)); assert!(PendingProposals::::get().contains(&bob_hash));