From d2115b72a0e15535a47c0cddf7be09cf78ffa4bc Mon Sep 17 00:00:00 2001 From: tanlang Date: Wed, 21 Sep 2022 09:02:45 +0000 Subject: [PATCH] feat: rm GetDealsByPieceStatus from StorageDealRepo --- models/badger/storage_deal_test.go | 16 ---------------- models/mysql/storage_deal.go | 14 -------------- models/mysql/storage_deal_test.go | 23 ----------------------- models/repo/repo.go | 3 +-- models/storage_deal_test.go | 4 ++-- storageprovider/deal_assigner.go | 3 ++- 6 files changed, 5 insertions(+), 58 deletions(-) diff --git a/models/badger/storage_deal_test.go b/models/badger/storage_deal_test.go index d3b7be6f..d38b611e 100644 --- a/models/badger/storage_deal_test.go +++ b/models/badger/storage_deal_test.go @@ -88,22 +88,6 @@ func TestStorageDeal(t *testing.T) { assert.Equal(t, dealCases[0], *res[0]) }) - t.Run("GetDealsByPieceStatus", func(t *testing.T) { - t.Run("With Provider", func(t *testing.T) { - res, err := r.GetDealsByPieceStatus(ctx, dealCases[0].Proposal.Provider, dealCases[0].PieceStatus) - assert.NoError(t, err) - assert.Equal(t, 1, len(res)) - assert.Equal(t, dealCases[0], *res[0]) - }) - - t.Run("Without Provider", func(t *testing.T) { - res, err := r.GetDealsByPieceStatus(ctx, address.Undef, dealCases[0].PieceStatus) - assert.NoError(t, err) - assert.Equal(t, 1, len(res)) - assert.Equal(t, dealCases[0], *res[0]) - }) - }) - t.Run("GetDealsByPieceStatusAndDealStatus", func(t *testing.T) { t.Run("With DealStatus", func(t *testing.T) { res, err := r.GetDealsByPieceStatusAndDealStatus(ctx, dealCases[0].Proposal.Provider, dealCases[0].PieceStatus, dealCases[0].State) diff --git a/models/mysql/storage_deal.go b/models/mysql/storage_deal.go index c438ef39..fa182164 100644 --- a/models/mysql/storage_deal.go +++ b/models/mysql/storage_deal.go @@ -516,20 +516,6 @@ func (sdr *storageDealRepo) GetDealByDealID(ctx context.Context, mAddr address.A return toStorageDeal(dbDeal) } -func (sdr *storageDealRepo) GetDealsByPieceStatus(ctx context.Context, mAddr address.Address, pieceStatus types.PieceStatus) ([]*types.MinerDeal, error) { - query := sdr.WithContext(ctx).Table(storageDealTableName).Where("piece_status = ?", pieceStatus) - if mAddr != address.Undef { - query.Where("cdp_provider=?", DBAddress(mAddr).String()) - } - - var dbDeals []*storageDeal - if err := query.Find(&dbDeals).Error; err != nil { - return nil, err - } - - return fromDbDeals(dbDeals) -} - func (sdr *storageDealRepo) GetDealsByPieceStatusAndDealStatus(ctx context.Context, mAddr address.Address, pieceStatus types.PieceStatus, dealStatus ...storagemarket.StorageDealStatus) ([]*types.MinerDeal, error) { query := sdr.WithContext(ctx).Table(storageDealTableName).Where("piece_status = ?", pieceStatus) if len(dealStatus) > 0 { diff --git a/models/mysql/storage_deal_test.go b/models/mysql/storage_deal_test.go index c80992d8..3b600796 100644 --- a/models/mysql/storage_deal_test.go +++ b/models/mysql/storage_deal_test.go @@ -217,28 +217,6 @@ func testGetGetDealByDealID(t *testing.T, r repo.Repo, mock sqlmock.Sqlmock) { assert.Equal(t, deal, res) } -func testGetDealsByPieceStatus(t *testing.T, r repo.Repo, mock sqlmock.Sqlmock) { - deal := storageDealCases[0] - dbDeal := dbStorageDealCases[0] - - db, err := getMysqlDryrunDB() - assert.NoError(t, err) - - rows, err := getFullRows(dbDeal) - assert.NoError(t, err) - - var md []storageDeal - sql, vars, err := getSQL(db.Table((&storageDeal{}).TableName()).Where("piece_status = ?", deal.PieceStatus).Where("cdp_provider=?", DBAddress(deal.Proposal.Provider).String()).Find(&md)) - assert.NoError(t, err) - - mock.ExpectQuery(regexp.QuoteMeta(sql)).WithArgs(vars...).WillReturnRows(rows) - - res, err := r.StorageDealRepo().GetDealsByPieceStatus(context.Background(), deal.Proposal.Provider, deal.PieceStatus) - assert.NoError(t, err) - assert.Equal(t, 1, len(res)) - assert.Equal(t, deal, res[0]) -} - func testGetDealsByPieceStatusAndDealStatus(t *testing.T, r repo.Repo, mock sqlmock.Sqlmock) { deal := storageDealCases[0] dbDeal := dbStorageDealCases[0] @@ -469,7 +447,6 @@ func TestStorageDealRepo(t *testing.T) { t.Run("mysql test GetDealByAddrAndStatus", wrapper(testGetDealByAddrAndStatus, r, mock)) t.Run("mysql test GetDealByDealID", wrapper(testGetGetDealByDealID, r, mock)) - t.Run("mysql test GetDealsByPieceStatus", wrapper(testGetDealsByPieceStatus, r, mock)) t.Run("mysql test GetDealsByPieceStatus", wrapper(testGetDealsByPieceStatusAndDealStatus, r, mock)) t.Run("mysql test UpdateDealStatus", wrapper(testUpdateDealStatus, r, mock)) diff --git a/models/repo/repo.go b/models/repo/repo.go index 0d7476bf..cb9cac83 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -35,8 +35,7 @@ type StorageDealRepo interface { //todo rename Getxxx to Listxxx if return deals list GetDeals(ctx context.Context, mAddr address.Address, pageIndex, pageSize int) ([]*types.MinerDeal, error) - //GetDealsByPieceStatus list deals by providor and piece status, but if addr is Undef, only filter by piece status - GetDealsByPieceStatus(ctx context.Context, mAddr address.Address, pieceStatus types.PieceStatus) ([]*types.MinerDeal, error) + //GetDealsByPieceStatusAndDealStatus list deals by providor, piece status and deal status, but if addr is Undef, only filter by piece status GetDealsByPieceStatusAndDealStatus(ctx context.Context, mAddr address.Address, pieceStatus types.PieceStatus, dealStatus ...storagemarket.StorageDealStatus) ([]*types.MinerDeal, error) //GetDealsByDataCidAndDealStatus query deals from address data cid and deal status, if mAddr equal undef wont filter by address GetDealsByDataCidAndDealStatus(ctx context.Context, mAddr address.Address, dataCid cid.Cid, pieceStatuss []types.PieceStatus) ([]*types.MinerDeal, error) diff --git a/models/storage_deal_test.go b/models/storage_deal_test.go index e6e4190e..92d4a14d 100644 --- a/models/storage_deal_test.go +++ b/models/storage_deal_test.go @@ -179,12 +179,12 @@ func testStorageDeal(t *testing.T, dealRepo repo.StorageDealRepo) { assert.Len(t, deals, 1) compareDeal(t, deals[0], deal2) - deals, err = dealRepo.GetDealsByPieceStatus(ctx, deal2.ClientDealProposal.Proposal.Provider, types.Proving) + deals, err = dealRepo.GetDealsByPieceStatusAndDealStatus(ctx, deal2.ClientDealProposal.Proposal.Provider, types.Proving) assert.Nil(t, err) assert.Len(t, deals, 1) compareDeal(t, deals[0], deal2) - deals, err = dealRepo.GetDealsByPieceStatus(ctx, address.Undef, types.Proving) + deals, err = dealRepo.GetDealsByPieceStatusAndDealStatus(ctx, address.Undef, types.Proving) assert.Nil(t, err) assert.Len(t, deals, 2) diff --git a/storageprovider/deal_assigner.go b/storageprovider/deal_assigner.go index a31d3c97..80e2e7e0 100644 --- a/storageprovider/deal_assigner.go +++ b/storageprovider/deal_assigner.go @@ -146,7 +146,7 @@ func (ps *dealAssigner) GetUnPackedDeals(ctx context.Context, miner address.Addr spec.MaxPiece = defaultMaxPiece } - mds, err := ps.repo.StorageDealRepo().GetDealsByPieceStatus(ctx, miner, types.Undefine) + mds, err := ps.repo.StorageDealRepo().GetDealsByPieceStatusAndDealStatus(ctx, miner, types.Undefine, storagemarket.StorageDealAwaitingPreCommit) if err != nil { return nil, err } @@ -206,6 +206,7 @@ func (ps *dealAssigner) AssignUnPackedDeals(ctx context.Context, sid abi.SectorI var deals []*types.DealInfoIncludePath for _, md := range mds { + // 订单筛选和组合的逻辑完全由 pickAndAlign 完成 deals = append(deals, &types.DealInfoIncludePath{ DealProposal: md.Proposal,