Skip to content

Commit

Permalink
Merge pull request #5902 from filecoin-project/feat/FIP0061
Browse files Browse the repository at this point in the history
Feat/actor state migrate for nv19  / actor nv19 状态迁移
  • Loading branch information
simlecode authored Apr 13, 2023
2 parents 3ef790f + 35fd02c commit 642728e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 13 deletions.
6 changes: 3 additions & 3 deletions fixtures/networks/butterfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func ButterflySnapNet() *NetworkConf {
UpgradeOhSnapHeight: -18,
UpgradeSkyrHeight: -19,
UpgradeSharkHeight: -20,
UpgradeHyggeHeight: 600,
UpgradeHyggeHeight: -21,
// TODO: set upgrade height
UpgradeLightningHeight: 999999999999999,
UpgradeThunderHeight: 999999999999999 + 1,
UpgradeLightningHeight: 600,
UpgradeThunderHeight: 1440,
},
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 1},
AddressNetwork: address.Testnet,
Expand Down
120 changes: 118 additions & 2 deletions pkg/fork/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"go.opencensus.io/trace"

nv18 "github.com/filecoin-project/go-state-types/builtin/v10/migration"
nv19 "github.com/filecoin-project/go-state-types/builtin/v11/migration"
nv17 "github.com/filecoin-project/go-state-types/builtin/v9/migration"
"github.com/filecoin-project/go-state-types/migration"
"github.com/filecoin-project/specs-actors/actors/migration/nv3"
Expand Down Expand Up @@ -372,7 +373,19 @@ func DefaultUpgradeSchedule(cf *ChainFork, upgradeHeight *config.ForkUpgradeConf
}, {
Height: upgradeHeight.UpgradeLightningHeight,
Network: network.Version19,
Migration: nil,
Migration: cf.UpgradeActorsV11,
PreMigrations: []PreMigration{{
PreMigration: cf.PreUpgradeActorsV11,
StartWithin: 240,
DontStartWithin: 60,
StopWithin: 20,
}, {
PreMigration: cf.PreUpgradeActorsV11,
StartWithin: 15,
DontStartWithin: 10,
StopWithin: 5,
}},
Expensive: true,
}, {
Height: upgradeHeight.UpgradeThunderHeight,
Network: network.Version20,
Expand Down Expand Up @@ -2414,7 +2427,7 @@ func (c *ChainFork) upgradeActorsV10Common(

manifest, ok := actors.GetManifest(actorstypes.Version10)
if !ok {
return cid.Undef, fmt.Errorf("no manifest CID for v9 upgrade")
return cid.Undef, fmt.Errorf("no manifest CID for v10 upgrade")
}

// Perform the migration
Expand Down Expand Up @@ -2448,6 +2461,109 @@ func (c *ChainFork) upgradeActorsV10Common(
return newRoot, nil
}

func (c *ChainFork) PreUpgradeActorsV11(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
// Use half the CPUs for pre-migration, but leave at least 3.
workerCount := MigrationMaxWorkerCount
if workerCount <= 4 {
workerCount = 1
} else {
workerCount /= 2
}

nv := c.GetNetworkVersion(ctx, epoch)
lbts, lbRoot, err := c.cr.GetLookbackTipSetForRound(ctx, ts, epoch, nv)
if err != nil {
return fmt.Errorf("error getting lookback ts for premigration: %w", err)
}

config := migration.Config{
MaxWorkers: uint(workerCount),
ProgressLogPeriod: time.Minute * 5,
}

_, err = c.upgradeActorsV11Common(ctx, cache, lbRoot, epoch, lbts, config)
return err
}

func (c *ChainFork) UpgradeActorsV11(ctx context.Context, cache MigrationCache,
root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
// Use all the CPUs except 2.
workerCount := MigrationMaxWorkerCount - 3
if workerCount <= 0 {
workerCount = 1
}
config := migration.Config{
MaxWorkers: uint(workerCount),
JobQueueSize: 1000,
ResultQueueSize: 100,
ProgressLogPeriod: 10 * time.Second,
}
newRoot, err := c.upgradeActorsV11Common(ctx, cache, root, epoch, ts, config)
if err != nil {
return cid.Undef, fmt.Errorf("migrating actors v11 state: %w", err)
}
return newRoot, nil
}

func (c *ChainFork) upgradeActorsV11Common(
ctx context.Context, cache MigrationCache,
root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet,
config migration.Config,
) (cid.Cid, error) {
writeStore := blockstoreutil.NewAutobatch(ctx, c.bs, units.GiB/4)
store := adt.WrapStore(ctx, cbor.NewCborStore(writeStore))

// ensure that the manifest is loaded in the blockstore
if err := actors.LoadBundles(ctx, c.bs, actorstypes.Version11); err != nil {
return cid.Undef, fmt.Errorf("failed to load manifest bundle: %w", err)
}

// Load the state root.
var stateRoot vmstate.StateRoot
if err := store.Get(ctx, root, &stateRoot); err != nil {
return cid.Undef, fmt.Errorf("failed to decode state root: %w", err)
}

if stateRoot.Version != vmstate.StateTreeVersion5 {
return cid.Undef, fmt.Errorf(
"expected state root version 5 for actors v11 upgrade, got %d",
stateRoot.Version,
)
}

manifest, ok := actors.GetManifest(actorstypes.Version11)
if !ok {
return cid.Undef, fmt.Errorf("no manifest CID for v11 upgrade")
}

// Perform the migration
newHamtRoot, err := nv19.MigrateStateTree(ctx, store, manifest, stateRoot.Actors, epoch, config,
migrationLogger{}, cache)
if err != nil {
return cid.Undef, fmt.Errorf("upgrading to actors v11: %w", err)
}

// Persist the result.
newRoot, err := store.Put(ctx, &vmstate.StateRoot{
Version: vmstate.StateTreeVersion5,
Actors: newHamtRoot,
Info: stateRoot.Info,
})
if err != nil {
return cid.Undef, fmt.Errorf("failed to persist new state root: %w", err)
}

// Persist the new tree and shuts down the flush worker
if err := writeStore.Flush(ctx); err != nil {
return cid.Undef, fmt.Errorf("writeStore flush failed: %w", err)
}
if err := writeStore.Shutdown(ctx); err != nil {
return cid.Undef, fmt.Errorf("writeStore shutdown failed: %w", err)
}

return newRoot, nil
}

func (c *ChainFork) GetForkUpgrade() *config.ForkUpgradeConfig {
return c.forkUpgrade
}
Expand Down
35 changes: 28 additions & 7 deletions pkg/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ import (
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
"github.com/filecoin-project/venus/pkg/vmsupport"
"github.com/filecoin-project/venus/venus-shared/types"

power11 "github.com/filecoin-project/go-state-types/builtin/v11/power"
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
)

func MinerAddress(genesisIndex uint64) address.Address {
Expand Down Expand Up @@ -137,7 +140,7 @@ func SetupStorageMiners(ctx context.Context, cs *chain.Store, sroot cid.Cid, min
sectorWeight []abi.StoragePower
}, len(miners))

maxPeriods := policy.GetMaxSectorExpirationExtension() / minertypes.WPoStProvingPeriod
maxPeriods := policy.GetDefaultSectorExpirationExtension() / minertypes.WPoStProvingPeriod
rawPow, qaPow := big.NewInt(0), big.NewInt(0)
for i, m := range miners {
// Create miner through power actor
Expand All @@ -150,14 +153,32 @@ func SetupStorageMiners(ctx context.Context, cs *chain.Store, sroot cid.Cid, min
}

{
constructorParams := &power0.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerID),
SealProofType: spt,
var params []byte
if nv <= network.Version10 {
constructorParams := &power2.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerID),
SealProofType: spt,
}

params = mustEnc(constructorParams)
} else {
ppt, err := spt.RegisteredWindowPoStProofByNetworkVersion(nv)
if err != nil {
return cid.Undef, fmt.Errorf("failed to convert spt to wpt: %w", err)
}

constructorParams := &power11.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerID),
WindowPoStProofType: ppt,
}

params = mustEnc(constructorParams)
}

params := mustEnc(constructorParams)
rval, err := doExecValue(ctx, genesisVM, power.Address, m.Owner, m.PowerBalance, power.Methods.CreateMiner, params)
if err != nil {
return cid.Undef, fmt.Errorf("failed to create genesis miner: %w", err)
Expand Down
1 change: 1 addition & 0 deletions venus-shared/actors/builtin_actors_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func LoadBundles(ctx context.Context, bs blockstore.Blockstore, versions ...acto
// All manifests are registered on start, so this must succeed.
return fmt.Errorf("unknown actor version v%d", av)
}
fmt.Printf("manifest cid: %s\n", manifestCid)

if haveManifest, err := bs.Has(ctx, manifestCid); err != nil {
return fmt.Errorf("blockstore error when loading manifest %s: %w", manifestCid, err)
Expand Down

0 comments on commit 642728e

Please sign in to comment.