Skip to content

Commit

Permalink
v13: Diff migration fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Feb 6, 2024
1 parent c34fd9b commit a6102ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
66 changes: 46 additions & 20 deletions builtin/v13/migration/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesWithDiff(ctx context.Con
providerSectorsMem := map[abi.ActorID]map[abi.SectorNumber][]abi.DealID{} // added
providerSectorsMemRemoved := map[abi.ActorID]map[abi.SectorNumber][]abi.DealID{} // removed

addProviderSectorEntry := func(deal abi.DealID, newState *market13.DealState) error {
addProviderSectorEntry := func(deal abi.DealID, newState *market13.DealState) (abi.SectorNumber, error) {
sid, ok := m.providerSectors.dealToSector[deal]
if !ok {
return xerrors.Errorf("deal %d not found in providerSectors", deal) // todo is this normal and possible??
return 0, xerrors.Errorf("deal %d not found in providerSectors", deal) // todo is this normal and possible??
}

newState.SectorNumber = sid.Number
Expand All @@ -156,13 +156,23 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesWithDiff(ctx context.Con
}
providerSectorsMem[sid.Miner][sid.Number] = append(providerSectorsMem[sid.Miner][sid.Number], deal)

return nil
return sid.Number, nil
}

removeProviderSectorEntry := func(deal abi.DealID, newState *market13.DealState) error {
sid, ok := m.providerSectors.removedDealToSector[deal]
if !ok {
return xerrors.Errorf("deal %d not found in providerSectors", deal) // todo is this normal and possible??
if newState.SectorNumber == 0 {
// already removed, sector was likely terminated/slashed, but not
// yet picked up by the market
return nil
}

// this can happen if the deal was terminated between premigration and migration
// if it's not here it wasn't in premigration ProviderSectors either
fmt.Printf("notice: deal %d not found in removedDealToSector\n", deal)
newState.SectorNumber = 0
return nil
}

newState.SectorNumber = 0
Expand All @@ -189,25 +199,27 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesWithDiff(ctx context.Con
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to unmarshal old state: %w", err)
}

//fmt.Printf("add deal %d\n", deal)

newState.SlashEpoch = oldState.SlashEpoch
newState.LastUpdatedEpoch = oldState.LastUpdatedEpoch
newState.SectorStartEpoch = oldState.SectorStartEpoch
newState.SectorNumber = 0 // terminated / not found (?)

if oldState.SectorStartEpoch != -1 { // todo slashEpoch != -1? or is this correct??
if err := addProviderSectorEntry(deal, &newState); err != nil {
if oldState.SlashEpoch == -1 {
si, err := addProviderSectorEntry(deal, &newState)
if err != nil {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to add provider sector entry: %w", err)
}
newState.SectorNumber = si
}

fmt.Printf("add deal %d to sector %d\n", deal, newState.SectorNumber)

if err := prevOutStates.Set(uint64(deal), &newState); err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to set new state: %w", err)
}

case amt.Remove:
//fmt.Printf("remove deal %d\n", deal)
fmt.Printf("remove deal %d\n", deal)

ok, err := prevOutStates.Get(uint64(deal), &newState)
if err != nil {
Expand Down Expand Up @@ -252,29 +264,43 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesWithDiff(ctx context.Con
newState.LastUpdatedEpoch = oldState.LastUpdatedEpoch
newState.SectorStartEpoch = oldState.SectorStartEpoch

//if oldState.SectorStartEpoch == -1 && prevOldState.SectorStartEpoch != -1 {
//fmt.Printf("deal %d start -1 both\n", deal)
// neither was in a sector, unclear if this can happen, but we handle this case anyway
//}
// if nowOld.Slash == -1, then 'now' is not slashed, so we should try to find the sector
// we probably don't care about prevOldSlash?? beyond it changing from newSlash?

fmt.Printf("deal %d slash %d -> %d, update %d -> %d (prev sec: %d)\n", deal, prevOldState.SlashEpoch, oldState.SlashEpoch, prevOldState.LastUpdatedEpoch, oldState.LastUpdatedEpoch, newState.SectorNumber)

if oldState.SlashEpoch != -1 && prevOldState.SlashEpoch == -1 {
// not slashed -> slashed
fmt.Printf("deal %d slash -1 -> %d\n", deal, oldState.SlashEpoch)

if err := removeProviderSectorEntry(deal, &newState); err != nil {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to remove provider sector entry: %w", err)
}
}

/*if oldState.SectorStartEpoch == -1 && prevOldState.SectorStartEpoch != -1 {
fmt.Printf("deal %d start -1 both\n", deal)
// neither was in a sector, unclear if this can happen, but we handle this case anyway
}
if (oldState.SectorStartEpoch != -1 && prevOldState.SectorStartEpoch == -1) && oldState.SlashEpoch == -1 {
//fmt.Printf("deal %d start -1 -> %d\n", deal, oldState.SectorStartEpoch)
fmt.Printf("deal %d start -1 -> %d\n", deal, oldState.SectorStartEpoch)
// wasn't in a sector, now is
if err := addProviderSectorEntry(deal, &newState); err != nil {
if _, err := addProviderSectorEntry(deal, &newState); err != nil {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to add provider sector entry: %w", err)
}
}
if (oldState.SectorStartEpoch == -1 && prevOldState.SectorStartEpoch != -1) && prevOldState.SlashEpoch != -1 {
//fmt.Printf("deal %d start %d -> -1\n", deal, prevOldState.SectorStartEpoch)
fmt.Printf("deal %d start %d -> -1\n", deal, prevOldState.SectorStartEpoch)
// was in a sector, now isn't
if err := removeProviderSectorEntry(deal, &newState); err != nil {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to remove provider sector entry: %w", err)
}
}
if (oldState.SectorStartEpoch != -1 && prevOldState.SectorStartEpoch != -1) && oldState.SlashEpoch == -1 {
//fmt.Printf("deal %d start %d -> %d\n", deal, prevOldState.SectorStartEpoch, oldState.SectorStartEpoch)
fmt.Printf("deal %d start %d -> %d\n", deal, prevOldState.SectorStartEpoch, oldState.SectorStartEpoch)
// both in a sector, check if the same
_, rm := m.providerSectors.removedDealToSector[deal]
if rm {
Expand All @@ -285,13 +311,13 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesWithDiff(ctx context.Con
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to remove provider sector entry: %w", err)
}
if err := addProviderSectorEntry(deal, &newState); err != nil {
if _, err := addProviderSectorEntry(deal, &newState); err != nil {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("failed to add provider sector entry: %w", err)
}
} else if _, added := m.providerSectors.dealToSector[deal]; added {
return cid.Cid{}, cid.Cid{}, xerrors.Errorf("deal %d with modified state was added to providerSectors, but was not in removedDealToSector", deal)
}
}
}*/

if err := prevOutStates.Set(uint64(deal), &newState); err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to set new state: %w", err)
Expand Down Expand Up @@ -434,7 +460,7 @@ func (m *marketMigrator) migrateProviderSectorsAndStatesFromScratch(ctx context.
newState.SlashEpoch = oldState.SlashEpoch
newState.LastUpdatedEpoch = oldState.LastUpdatedEpoch
newState.SectorStartEpoch = oldState.SectorStartEpoch
newState.SectorNumber = 0 // non- -1 slashed eport
newState.SectorNumber = 0 // non- -1 slashed epoch

if oldState.SlashEpoch == -1 {
// FIP: find the corresponding deal proposal object and extract the provider's actor ID;
Expand Down
15 changes: 12 additions & 3 deletions builtin/v13/migration/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ func (m *minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore,
continue
}

fmt.Printf("prov ds ADD %d: %v\n", sectorNo, sector.DealIDs)

m.providerSectors.lk.Lock()
for _, dealID := range sector.DealIDs {
m.providerSectors.dealToSector[dealID] = abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(i),
Number: sectorNo,
}
}
m.providerSectors.lk.Unlock()
Expand Down Expand Up @@ -161,14 +163,19 @@ func (m *minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore,
}
// snap

fmt.Printf("prov ds MOD %d: %v -> %v\n", sectorNo, sectorBefore.DealIDs, sectorAfter.DealIDs)

m.providerSectors.lk.Lock()
for _, dealID := range sectorAfter.DealIDs {
m.providerSectors.dealToSector[dealID] = abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(i),
Number: sectorNo,
}
}
m.providerSectors.lk.Unlock()
} else {
fmt.Println("sector2 before: ", string(pjsonb))
fmt.Println("sector2 after: ", string(pjson))
}

// extensions, etc. here; we don't care about those
Expand All @@ -188,11 +195,13 @@ func (m *minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore,
continue
}

fmt.Printf("prov ds REM %d: %v\n", sectorNo, sector.DealIDs)

m.providerSectors.lk.Lock()
for _, dealID := range sector.DealIDs {
m.providerSectors.removedDealToSector[dealID] = abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(i),
Number: sectorNo,
}
}
m.providerSectors.lk.Unlock()
Expand Down

0 comments on commit a6102ce

Please sign in to comment.