Skip to content

Commit

Permalink
Fast migration for v15
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Jan 10, 2022
1 parent 3fb71cd commit ebeb7b0
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 30 deletions.
4 changes: 4 additions & 0 deletions chain/actors/builtin/paych/message.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (m message{{.v}}) Create(to address.Address, initialAmount abi.TokenAmount)

func (m message{{.v}}) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych{{.v}}.UpdateChannelStateParams{
{{if (ge .v 7)}}
Sv: toV{{.v}}SignedVoucher(*sv),
{{else}}
Sv: *sv,
{{end}}
Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message0.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message0) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message0) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych0.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message2) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message2) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych2.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message3.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message3) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message3) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych3.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message4.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message4) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message4) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych4.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message5.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message5) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message5) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych5.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message6.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message6) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message6) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych6.UpdateChannelStateParams{
Sv: *sv,

Sv: *sv,

Secret: secret,
})
if aerr != nil {
Expand Down
4 changes: 3 additions & 1 deletion chain/actors/builtin/paych/message7.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (m message7) Create(to address.Address, initialAmount abi.TokenAmount) (*ty

func (m message7) Update(paych address.Address, sv *SignedVoucher, secret []byte) (*types.Message, error) {
params, aerr := actors.SerializeParams(&paych7.UpdateChannelStateParams{
Sv: *sv,

Sv: toV7SignedVoucher(*sv),

Secret: secret,
})
if aerr != nil {
Expand Down
18 changes: 18 additions & 0 deletions chain/actors/builtin/paych/state.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,21 @@ func (ls *laneState{{.v}}) Redeemed() (big.Int, error) {
func (ls *laneState{{.v}}) Nonce() (uint64, error) {
return ls.LaneState.Nonce, nil
}

{{if (ge .v 7)}}
func toV{{.v}}SignedVoucher(sv SignedVoucher) paych{{.v}}.SignedVoucher {
return paych{{.v}}.SignedVoucher{
ChannelAddr: sv.ChannelAddr,
TimeLockMin: sv.TimeLockMin,
TimeLockMax: sv.TimeLockMax,
SecretHash: sv.SecretPreimage,
Extra: sv.Extra,
Lane: sv.Lane,
Nonce: sv.Nonce,
Amount: sv.Amount,
MinSettleHeight: sv.MinSettleHeight,
Merges: sv.Merges,
Signature: sv.Signature,
}
}
{{end}}
16 changes: 16 additions & 0 deletions chain/actors/builtin/paych/v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,19 @@ func (ls *laneState7) Redeemed() (big.Int, error) {
func (ls *laneState7) Nonce() (uint64, error) {
return ls.LaneState.Nonce, nil
}

func toV7SignedVoucher(sv SignedVoucher) paych7.SignedVoucher {
return paych7.SignedVoucher{
ChannelAddr: sv.ChannelAddr,
TimeLockMin: sv.TimeLockMin,
TimeLockMax: sv.TimeLockMax,
SecretHash: sv.SecretPreimage,
Extra: sv.Extra,
Lane: sv.Lane,
Nonce: sv.Nonce,
Amount: sv.Amount,
MinSettleHeight: sv.MinSettleHeight,
Merges: sv.Merges,
Signature: sv.Signature,
}
}
20 changes: 17 additions & 3 deletions chain/consensus/filcns/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"runtime"
"time"

autobatch "github.com/application-research/go-bs-autobatch"

"github.com/filecoin-project/specs-actors/v6/actors/migration/nv14"
"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"

Expand Down Expand Up @@ -1245,8 +1247,15 @@ func PreUpgradeActorsV7(ctx context.Context, sm *stmgr.StateManager, cache stmgr
workerCount /= 2
}

config := nv15.Config{MaxWorkers: uint(workerCount)}
_, err := upgradeActorsV7Common(ctx, sm, cache, root, epoch, ts, config)
lbts, lbRoot, err := stmgr.GetLookbackTipSetForRound(ctx, sm, ts, epoch)
if err != nil {
return xerrors.Errorf("error getting lookback ts for premigration: %w", err)
}

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

_, err = upgradeActorsV7Common(ctx, sm, cache, lbRoot, epoch, lbts, config)
return err
}

Expand All @@ -1255,7 +1264,12 @@ func upgradeActorsV7Common(
root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet,
config nv15.Config,
) (cid.Cid, error) {
buf := blockstore.NewTieredBstore(sm.ChainStore().StateBlockstore(), blockstore.NewMemorySync())
writeStore, err := autobatch.NewBlockstore(sm.ChainStore().StateBlockstore(), blockstore.NewMemorySync(), 100_000, 100, true)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create writeStore: %w", err)
}

buf := blockstore.NewTieredBstore(sm.ChainStore().StateBlockstore(), writeStore)
store := store.ActorStore(ctx, buf)

// Load the state root.
Expand Down
6 changes: 3 additions & 3 deletions chain/stmgr/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"sync"
"time"

"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"

"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/specs-actors/v3/actors/migration/nv10"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
init_ "github.com/filecoin-project/lotus/chain/actors/builtin/init"
Expand Down Expand Up @@ -211,7 +211,7 @@ func (sm *StateManager) hasExpensiveFork(height abi.ChainEpoch) bool {
return ok
}

func runPreMigration(ctx context.Context, sm *StateManager, fn PreMigrationFunc, cache *nv10.MemMigrationCache, ts *types.TipSet) {
func runPreMigration(ctx context.Context, sm *StateManager, fn PreMigrationFunc, cache *nv15.MemMigrationCache, ts *types.TipSet) {
height := ts.Height()
parent := ts.ParentState()

Expand Down
13 changes: 7 additions & 6 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"sync"

"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"

"github.com/filecoin-project/lotus/chain/rand"

"github.com/filecoin-project/lotus/chain/beacon"
Expand All @@ -18,10 +20,6 @@ import (
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"

// Used for genesis.
msig0 "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
"github.com/filecoin-project/specs-actors/v3/actors/migration/nv10"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
Expand All @@ -30,6 +28,9 @@ import (
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"

// Used for genesis.
msig0 "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
)

const LookbackNoLimit = api.LookbackNoLimit
Expand All @@ -53,7 +54,7 @@ type versionSpec struct {
type migration struct {
upgrade MigrationFunc
preMigrations []PreMigration
cache *nv10.MemMigrationCache
cache *nv15.MemMigrationCache
}

type Executor interface {
Expand Down Expand Up @@ -121,7 +122,7 @@ func NewStateManager(cs *store.ChainStore, exec Executor, sys vm.SyscallBuilder,
migration := &migration{
upgrade: upgrade.Migration,
preMigrations: upgrade.PreMigrations,
cache: nv10.NewMemMigrationCache(),
cache: nv15.NewMemMigrationCache(),
}
stateMigrations[upgrade.Height] = migration
}
Expand Down
1 change: 1 addition & 0 deletions cmd/lotus-shed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {
balancerCmd,
sendCsvCmd,
terminationsCmd,
migrationsCmd,
}

app := &cli.App{
Expand Down
127 changes: 127 additions & 0 deletions cmd/lotus-shed/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"context"
"fmt"
"io"
"time"

"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"

"github.com/filecoin-project/lotus/chain/types"

"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/node/repo"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
)

var migrationsCmd = &cli.Command{
Name: "migrate-nv15",
Description: "Run the specified migration",
ArgsUsage: "[block to look back from]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
},
Action: func(cctx *cli.Context) error {
ctx := context.TODO()

if cctx.NArg() != 1 {
return fmt.Errorf("must pass block cid")
}

blkCid, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse input: %w", err)
}

fsrepo, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return err
}

lkrepo, err := fsrepo.Lock(repo.FullNode)
if err != nil {
return err
}

defer lkrepo.Close() //nolint:errcheck

bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}

defer func() {
if c, ok := bs.(io.Closer); ok {
if err := c.Close(); err != nil {
log.Warnf("failed to close blockstore: %s", err)
}
}
}()

mds, err := lkrepo.Datastore(context.Background(), "/metadata")
if err != nil {
return err
}

cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
defer cs.Close() //nolint:errcheck

sm, err := stmgr.NewStateManager(cs, filcns.NewTipSetExecutor(), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil)
if err != nil {
return err
}

cache := nv15.NewMemMigrationCache()

blk, err := cs.GetBlock(ctx, blkCid)
if err != nil {
return err
}

migrationTs, err := cs.LoadTipSet(ctx, types.NewTipSetKey(blk.Parents...))
if err != nil {
return err
}

ts1, err := cs.GetTipsetByHeight(ctx, blk.Height-240, migrationTs, false)
if err != nil {
return err
}

startTime := time.Now()

err = filcns.PreUpgradeActorsV7(ctx, sm, cache, ts1.ParentState(), ts1.Height()-1, ts1)
if err != nil {
return err
}

fmt.Println("completed round 1, took ", time.Since(startTime))
startTime = time.Now()

newCid1, err := filcns.UpgradeActorsV7(ctx, sm, cache, nil, blk.ParentStateRoot, blk.Height-1, migrationTs)
if err != nil {
return err
}
fmt.Println("completed round actual (with cache), took ", time.Since(startTime))

fmt.Println("new cid", newCid1)

newCid2, err := filcns.UpgradeActorsV7(ctx, sm, nv15.NewMemMigrationCache(), nil, blk.ParentStateRoot, blk.Height-1, migrationTs)
if err != nil {
return err
}
fmt.Println("completed round actual (without cache), took ", time.Since(startTime))

fmt.Println("new cid", newCid2)
return nil
},
}
Loading

0 comments on commit ebeb7b0

Please sign in to comment.