-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(power15): FIP0081 migration for power actor
Closes: #313
- Loading branch information
Showing
6 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package migration | ||
|
||
import ( | ||
"context" | ||
|
||
power14 "github.com/filecoin-project/go-state-types/builtin/v14/power" | ||
power15 "github.com/filecoin-project/go-state-types/builtin/v15/power" | ||
smoothing15 "github.com/filecoin-project/go-state-types/builtin/v15/util/smoothing" | ||
"github.com/filecoin-project/go-state-types/migration" | ||
"github.com/ipfs/go-cid" | ||
cbor "github.com/ipfs/go-ipld-cbor" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
type powerMigrator struct { | ||
rampStartEpoch int64 | ||
rampDurationEpochs uint64 | ||
outCodeCID cid.Cid | ||
} | ||
|
||
func newPowerMigrator(rampStartEpoch int64, rampDurationEpochs uint64, outCode cid.Cid) (*powerMigrator, error) { | ||
return &powerMigrator{ | ||
rampStartEpoch: rampStartEpoch, | ||
rampDurationEpochs: rampDurationEpochs, | ||
outCodeCID: outCode, | ||
}, nil | ||
} | ||
|
||
func (p powerMigrator) MigratedCodeCID() cid.Cid { return p.outCodeCID } | ||
|
||
func (p powerMigrator) Deferred() bool { return false } | ||
|
||
func (p powerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, in migration.ActorMigrationInput) (*migration.ActorMigrationResult, error) { | ||
var inState power14.State | ||
if err := store.Get(ctx, in.Head, &inState); err != nil { | ||
return nil, xerrors.Errorf("failed to load miner state for %s: %w", in.Address, err) | ||
} | ||
|
||
outState := power15.State{ | ||
TotalRawBytePower: inState.TotalRawBytePower, | ||
TotalBytesCommitted: inState.TotalBytesCommitted, | ||
TotalQualityAdjPower: inState.TotalQualityAdjPower, | ||
TotalQABytesCommitted: inState.TotalQABytesCommitted, | ||
TotalPledgeCollateral: inState.TotalPledgeCollateral, | ||
ThisEpochRawBytePower: inState.ThisEpochRawBytePower, | ||
ThisEpochQualityAdjPower: inState.ThisEpochQualityAdjPower, | ||
ThisEpochPledgeCollateral: inState.ThisEpochPledgeCollateral, | ||
ThisEpochQAPowerSmoothed: smoothing15.FilterEstimate{ | ||
PositionEstimate: inState.ThisEpochQAPowerSmoothed.PositionEstimate, | ||
VelocityEstimate: inState.ThisEpochQAPowerSmoothed.VelocityEstimate, | ||
}, | ||
MinerCount: inState.MinerCount, | ||
MinerAboveMinPowerCount: inState.MinerAboveMinPowerCount, | ||
RampStartEpoch: p.rampStartEpoch, | ||
RampDurationEpochs: p.rampDurationEpochs, | ||
CronEventQueue: inState.CronEventQueue, | ||
FirstCronEpoch: inState.FirstCronEpoch, | ||
Claims: inState.Claims, | ||
ProofValidationBatch: inState.ProofValidationBatch, | ||
} | ||
|
||
newHead, err := store.Put(ctx, &outState) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to put new state: %w", err) | ||
} | ||
|
||
return &migration.ActorMigrationResult{ | ||
NewCodeCID: p.MigratedCodeCID(), | ||
NewHead: newHead, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package migration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
"github.com/filecoin-project/go-state-types/big" | ||
power14 "github.com/filecoin-project/go-state-types/builtin/v14/power" | ||
smoothing14 "github.com/filecoin-project/go-state-types/builtin/v14/util/smoothing" | ||
power15 "github.com/filecoin-project/go-state-types/builtin/v15/power" | ||
"github.com/filecoin-project/go-state-types/migration" | ||
"github.com/ipfs/go-cid" | ||
cbor "github.com/ipfs/go-ipld-cbor" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPowerMigration(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
req := require.New(t) | ||
|
||
cst := cbor.NewMemCborStore() | ||
|
||
pvb := cid.MustParse("bafy2bzacaf2a") | ||
state14 := power14.State{ | ||
TotalRawBytePower: abi.NewStoragePower(101), | ||
TotalBytesCommitted: abi.NewStoragePower(102), | ||
TotalQualityAdjPower: abi.NewStoragePower(103), | ||
TotalQABytesCommitted: abi.NewStoragePower(104), | ||
TotalPledgeCollateral: abi.NewTokenAmount(105), | ||
ThisEpochRawBytePower: abi.NewStoragePower(106), | ||
ThisEpochQualityAdjPower: abi.NewStoragePower(107), | ||
ThisEpochPledgeCollateral: abi.NewTokenAmount(108), | ||
ThisEpochQAPowerSmoothed: smoothing14.NewEstimate(big.NewInt(109), big.NewInt(110)), | ||
MinerCount: 111, | ||
MinerAboveMinPowerCount: 112, | ||
CronEventQueue: cid.MustParse("bafy2bzacafza"), | ||
FirstCronEpoch: 113, | ||
Claims: cid.MustParse("bafy2bzacafzq"), | ||
ProofValidationBatch: &pvb, | ||
} | ||
|
||
state14Cid, err := cst.Put(ctx, &state14) | ||
req.NoError(err) | ||
|
||
var rampStartEpoch int64 = 101 | ||
var rampDurationEpochs uint64 = 202 | ||
|
||
migrator, err := newPowerMigrator(rampStartEpoch, rampDurationEpochs, cid.MustParse("bafy2bzaca4aaaaaaaaaqk")) | ||
req.NoError(err) | ||
|
||
result, err := migrator.MigrateState(ctx, cst, migration.ActorMigrationInput{ | ||
Address: address.TestAddress, | ||
Head: state14Cid, | ||
Cache: nil, | ||
}) | ||
req.NoError(err) | ||
req.Equal(cid.MustParse("bafy2bzaca4aaaaaaaaaqk"), result.NewCodeCID) | ||
req.NotEqual(cid.Undef, result.NewHead) | ||
|
||
newState := power15.State{} | ||
req.NoError(cst.Get(ctx, result.NewHead, &newState)) | ||
|
||
req.Equal(state14.TotalRawBytePower, newState.TotalRawBytePower) | ||
req.Equal(state14.TotalBytesCommitted, newState.TotalBytesCommitted) | ||
req.Equal(state14.TotalQualityAdjPower, newState.TotalQualityAdjPower) | ||
req.Equal(state14.TotalQABytesCommitted, newState.TotalQABytesCommitted) | ||
req.Equal(state14.TotalPledgeCollateral, newState.TotalPledgeCollateral) | ||
req.Equal(state14.ThisEpochRawBytePower, newState.ThisEpochRawBytePower) | ||
req.Equal(state14.ThisEpochQualityAdjPower, newState.ThisEpochQualityAdjPower) | ||
req.Equal(state14.ThisEpochPledgeCollateral, newState.ThisEpochPledgeCollateral) | ||
req.Equal(state14.ThisEpochQAPowerSmoothed.PositionEstimate, newState.ThisEpochQAPowerSmoothed.PositionEstimate) | ||
req.Equal(state14.ThisEpochQAPowerSmoothed.VelocityEstimate, newState.ThisEpochQAPowerSmoothed.VelocityEstimate) | ||
req.Equal(state14.MinerCount, newState.MinerCount) | ||
req.Equal(state14.MinerAboveMinPowerCount, newState.MinerAboveMinPowerCount) | ||
req.Equal(state14.CronEventQueue, newState.CronEventQueue) | ||
req.Equal(state14.FirstCronEpoch, newState.FirstCronEpoch) | ||
req.Equal(state14.Claims, newState.Claims) | ||
req.Equal(state14.ProofValidationBatch, newState.ProofValidationBatch) | ||
|
||
// Ramp parameters | ||
req.Equal(rampStartEpoch, newState.RampStartEpoch) | ||
req.Equal(rampDurationEpochs, newState.RampDurationEpochs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters