feat: allow nil deal state change (deal removed)

This commit is contained in:
Dirk McCormick 2020-06-29 18:34:24 -04:00 committed by Łukasz Magiera
parent 131928ee53
commit 1fd1c2edbd
2 changed files with 32 additions and 17 deletions

View File

@ -2,7 +2,6 @@ package state
import (
"context"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-amt-ipld/v2"
"github.com/filecoin-project/lotus/api/apibstore"
@ -107,8 +106,8 @@ type ChangedDeals map[abi.DealID]DealStateChange
// DealStateChange is a change in deal state from -> to
type DealStateChange struct {
From market.DealState
To market.DealState
From *market.DealState
To *market.DealState
}
// DealStateChangedForIDs detects changes in the deal state AMT for the given deal IDs
@ -116,17 +115,22 @@ func (sp *StatePredicates) DealStateChangedForIDs(dealIds []abi.DealID) DiffDeal
return func(ctx context.Context, oldDealStateRoot *amt.Root, newDealStateRoot *amt.Root) (changed bool, user UserData, err error) {
changedDeals := make(ChangedDeals)
for _, dealID := range dealIds {
var oldDealPtr, newDealPtr *market.DealState
var oldDeal, newDeal market.DealState
err := oldDealStateRoot.Get(ctx, uint64(dealID), &oldDeal)
if err != nil {
if err == nil {
oldDealPtr = &oldDeal
} else if _, ok := err.(*amt.ErrNotFound); !ok {
return false, nil, err
}
err = newDealStateRoot.Get(ctx, uint64(dealID), &newDeal)
if err != nil {
if err == nil {
newDealPtr = &newDeal
} else if _, ok := err.(*amt.ErrNotFound); !ok {
return false, nil, err
}
if oldDeal != newDeal {
changedDeals[dealID] = DealStateChange{oldDeal, newDeal}
if oldDealPtr == nil || newDealPtr == nil || oldDeal != newDeal {
changedDeals[dealID] = DealStateChange{oldDealPtr, newDealPtr}
}
}
if len(changedDeals) > 0 {

View File

@ -71,12 +71,10 @@ func TestPredicates(t *testing.T) {
abi.DealID(1): {
SectorStartEpoch: 1,
LastUpdatedEpoch: 2,
SlashEpoch: 0,
},
abi.DealID(2): {
SectorStartEpoch: 4,
LastUpdatedEpoch: 5,
SlashEpoch: 0,
},
}
oldStateC := createMarketState(ctx, t, store, oldDeals)
@ -85,12 +83,6 @@ func TestPredicates(t *testing.T) {
abi.DealID(1): {
SectorStartEpoch: 1,
LastUpdatedEpoch: 3,
SlashEpoch: 0,
},
abi.DealID(2): {
SectorStartEpoch: 4,
LastUpdatedEpoch: 6,
SlashEpoch: 6,
},
}
newStateC := createMarketState(ctx, t, store, newDeals)
@ -131,8 +123,27 @@ func TestPredicates(t *testing.T) {
t.Fatal("Unexpected change to LastUpdatedEpoch")
}
deal2 := changedDeals[abi.DealID(2)]
if deal2.From.SlashEpoch != 0 || deal2.To.SlashEpoch != 6 {
t.Fatal("Unexpected change to SlashEpoch")
if deal2.From.LastUpdatedEpoch != 5 || deal2.To != nil {
t.Fatal("Expected To to be nil")
}
// Diff with non-existent deal.
// Note that for non existent deals we expect a change with
// From and To set to nil. This is to ensure that if a deal expires and is
// removed from state we will still find out about the change.
noDeal := []abi.DealID{3}
diffNoDealFn := preds.OnStorageMarketActorChanged(preds.OnDealStateChanged(preds.DealStateChangedForIDs(noDeal)))
changed, val, err = diffNoDealFn(ctx, oldState, newState)
require.NoError(t, err)
require.True(t, changed)
changedDeals, ok = val.(ChangedDeals)
require.True(t, ok)
require.Len(t, changedDeals, 1)
require.Contains(t, changedDeals, noDeal[0])
deal1 = changedDeals[abi.DealID(1)]
if deal1.From != nil || deal1.To != nil {
t.Fatal("Expected both from and to to be nil")
}
// Test that OnActorStateChanged does not call the callback if the state has not changed