|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/filecoin-project/go-state-types/abi" |
| 7 | + miner15 "github.com/filecoin-project/go-state-types/builtin/v15/miner" |
| 8 | + miner16 "github.com/filecoin-project/go-state-types/builtin/v16/miner" |
| 9 | + "github.com/filecoin-project/go-state-types/builtin/v16/util/adt" |
| 10 | + "github.com/filecoin-project/go-state-types/migration" |
| 11 | + "github.com/ipfs/go-cid" |
| 12 | + cbor "github.com/ipfs/go-ipld-cbor" |
| 13 | + "golang.org/x/xerrors" |
| 14 | +) |
| 15 | + |
| 16 | +type minerMigrator struct { |
| 17 | + OutCodeCID cid.Cid |
| 18 | +} |
| 19 | + |
| 20 | +func newMinerMigrator(_ context.Context, _ cbor.IpldStore, outCode cid.Cid) (*minerMigrator, error) { |
| 21 | + return &minerMigrator{ |
| 22 | + OutCodeCID: outCode, |
| 23 | + }, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (m *minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, in migration.ActorMigrationInput) (result *migration.ActorMigrationResult, err error) { |
| 27 | + var inState miner15.State |
| 28 | + if err := store.Get(ctx, in.Head, &inState); err != nil { |
| 29 | + return nil, xerrors.Errorf("failed to load miner state for %s: %w", in.Address, err) |
| 30 | + } |
| 31 | + |
| 32 | + ctxStore := adt.WrapStore(ctx, store) |
| 33 | + |
| 34 | + inSectors, err := miner15.LoadSectors(ctxStore, inState.Sectors) |
| 35 | + if err != nil { |
| 36 | + return nil, xerrors.Errorf("failed to load sectors array: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + arr, err := adt.MakeEmptyArray(ctxStore, miner16.SectorsAmtBitwidth) |
| 40 | + if err != nil { |
| 41 | + return nil, xerrors.Errorf("failed to create sectors array: %w", err) |
| 42 | + } |
| 43 | + outSectors := miner16.Sectors{Array: arr, Store: ctxStore} |
| 44 | + |
| 45 | + inSectors.ForEach(func(sn abi.SectorNumber, soci *miner15.SectorOnChainInfo) error { |
| 46 | + return outSectors.Set(sn, &miner16.SectorOnChainInfo{ |
| 47 | + SectorNumber: soci.SectorNumber, |
| 48 | + SealProof: soci.SealProof, |
| 49 | + SealedCID: soci.SealedCID, |
| 50 | + DealIDs: soci.DealIDs, |
| 51 | + Activation: soci.Activation, |
| 52 | + Expiration: soci.Expiration, |
| 53 | + DealWeight: soci.DealWeight, |
| 54 | + VerifiedDealWeight: soci.VerifiedDealWeight, |
| 55 | + InitialPledge: soci.InitialPledge, |
| 56 | + ExpectedDayReward: soci.ExpectedDayReward, |
| 57 | + ExpectedStoragePledge: soci.ExpectedStoragePledge, |
| 58 | + PowerBaseEpoch: soci.PowerBaseEpoch, |
| 59 | + ReplacedDayReward: soci.ReplacedDayReward, |
| 60 | + SectorKeyCID: soci.SectorKeyCID, |
| 61 | + Flags: miner16.SectorOnChainInfoFlags(soci.Flags), |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + outSectorsRoot, err := outSectors.Root() |
| 66 | + if err != nil { |
| 67 | + return nil, xerrors.Errorf("failed to flush sectors: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: implement cached migrator with diff, see v13 for example |
| 71 | + |
| 72 | + outState := miner16.State{ |
| 73 | + Info: inState.Info, |
| 74 | + PreCommitDeposits: inState.PreCommitDeposits, |
| 75 | + LockedFunds: inState.LockedFunds, |
| 76 | + VestingFunds: inState.VestingFunds, |
| 77 | + FeeDebt: inState.FeeDebt, |
| 78 | + InitialPledge: inState.InitialPledge, |
| 79 | + PreCommittedSectors: inState.PreCommittedSectors, |
| 80 | + PreCommittedSectorsCleanUp: inState.PreCommittedSectorsCleanUp, |
| 81 | + AllocatedSectors: inState.AllocatedSectors, |
| 82 | + Sectors: outSectorsRoot, |
| 83 | + ProvingPeriodStart: inState.ProvingPeriodStart, |
| 84 | + CurrentDeadline: inState.CurrentDeadline, |
| 85 | + Deadlines: inState.Deadlines, |
| 86 | + EarlyTerminations: inState.EarlyTerminations, |
| 87 | + DeadlineCronActive: inState.DeadlineCronActive, |
| 88 | + } |
| 89 | + |
| 90 | + newHead, err := store.Put(ctx, &outState) |
| 91 | + if err != nil { |
| 92 | + return nil, xerrors.Errorf("failed to put new state: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + return &migration.ActorMigrationResult{ |
| 96 | + NewCodeCID: m.MigratedCodeCID(), |
| 97 | + NewHead: newHead, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (m *minerMigrator) MigratedCodeCID() cid.Cid { |
| 102 | + return m.OutCodeCID |
| 103 | +} |
| 104 | + |
| 105 | +func (m *minerMigrator) Deferred() bool { |
| 106 | + return false |
| 107 | +} |
| 108 | + |
| 109 | +var _ migration.ActorMigration = (*minerMigrator)(nil) |
0 commit comments