Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check datacaps for v8 verifreg match v9 datacap actor #9446

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions cmd/lotus-shed/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import (

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

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"

"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/datacap"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
Expand Down Expand Up @@ -124,6 +132,97 @@ var migrationsCmd = &cli.Command{

fmt.Println("new cid", newCid2)

err = checkStateInvariants(ctx, blk.ParentStateRoot, newCid2, bs)
if err != nil {
return err
}

return nil
},
}

func checkStateInvariants(ctx context.Context, oldStateRoot cid.Cid, newStateRoot cid.Cid, bs blockstore.Blockstore) error {
actorStore := store.ActorStore(ctx, blockstore.NewTieredBstore(bs, blockstore.NewMemorySync()))

verifregDatacaps, err := getVerifreg8Datacaps(ctx, oldStateRoot, actorStore)
if err != nil {
return err
}

newDatacaps, err := getDatacap9Datacaps(ctx, newStateRoot, actorStore)
if err != nil {
return err
}

if len(verifregDatacaps) != len(newDatacaps) {
return xerrors.Errorf("size of datacap maps do not match. verifreg: %d, datacap: %d", len(verifregDatacaps), len(newDatacaps))
}

for addr, oldDcap := range verifregDatacaps {
dcap, ok := newDatacaps[addr]
if !ok {
return xerrors.Errorf("datacap for address: %s not found in datacap state", addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("datacap for address: %s not found in datacap state", addr)
return xerrors.Errorf("datacap for address %s not found in datacap state", addr)

}
if dcap != oldDcap {
return xerrors.Errorf("datacap for address: %s do not match. verifreg: %d, datacap: %d", addr, oldDcap, dcap)
}
}

return nil
}

func getVerifreg8Datacaps(ctx context.Context, v8StateRoot cid.Cid, actorStore adt.Store) (map[address.Address]abi.StoragePower, error) {
stateTreeV8, err := state.LoadStateTree(actorStore, v8StateRoot)
if err != nil {
return nil, err
}

verifregV8, err := stateTreeV8.GetActor(verifreg.Address)
if err != nil {
return nil, err
}

verifregV8State, err := verifreg.Load(actorStore, verifregV8)
if err = actorStore.Get(ctx, verifregV8.Head, &verifregV8State); err != nil {
return nil, xerrors.Errorf("failed to get verifreg actor state: %w", err)
}

var verifregDatacaps = make(map[address.Address]abi.StoragePower)
err = verifregV8State.ForEachClient(func(addr address.Address, dcap abi.StoragePower) error {
verifregDatacaps[addr] = dcap
return nil
})
if err != nil {
return nil, err
}

return verifregDatacaps, nil
}

func getDatacap9Datacaps(ctx context.Context, v9StateRoot cid.Cid, actorStore adt.Store) (map[address.Address]abi.StoragePower, error) {
stateTreeV9, err := state.LoadStateTree(actorStore, v9StateRoot)
if err != nil {
return nil, err
}

datacapV9, err := stateTreeV9.GetActor(datacap.Address)
if err != nil {
return nil, err
}

datacapV9State, err := datacap.Load(actorStore, datacapV9)
if err = actorStore.Get(ctx, datacapV9.Head, &datacapV9State); err != nil {
return nil, xerrors.Errorf("failed to get verifreg actor state: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copypasta

}

var datacaps = make(map[address.Address]abi.StoragePower)
err = datacapV9State.ForEachClient(func(addr address.Address, dcap abi.StoragePower) error {
datacaps[addr] = dcap
return nil
})
if err != nil {
return nil, err
}

return datacaps, nil
}