Skip to content

Commit

Permalink
Merge pull request filecoin-project#4873 from filecoin-project/feat/s…
Browse files Browse the repository at this point in the history
…dr-upgrade

Support seal proof type switching
  • Loading branch information
magik6k authored and bibibong committed Jan 7, 2021
1 parent 28effc8 commit b59a49e
Show file tree
Hide file tree
Showing 21 changed files with 330 additions and 182 deletions.
2 changes: 1 addition & 1 deletion api/test/ccupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCCUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration) {
func testCCUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration, upgradeHeight abi.ChainEpoch) {
ctx := context.Background()
n, sn := b(t, []FullNodeOpts{FullNodeWithUpgradeAt(upgradeHeight)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV2At(upgradeHeight)}, OneMiner)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
Expand Down
21 changes: 20 additions & 1 deletion api/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var OneMiner = []StorageMiner{{Full: 0, Preseal: PresealGenesis}}
var OneFull = DefaultFullOpts(1)
var TwoFull = DefaultFullOpts(2)

var FullNodeWithUpgradeAt = func(upgradeHeight abi.ChainEpoch) FullNodeOpts {
var FullNodeWithActorsV2At = func(upgradeHeight abi.ChainEpoch) FullNodeOpts {
return FullNodeOpts{
Opts: func(nodes []TestNode) node.Option {
return node.Override(new(stmgr.UpgradeSchedule), stmgr.UpgradeSchedule{ /* {
Expand All @@ -121,6 +121,25 @@ var FullNodeWithUpgradeAt = func(upgradeHeight abi.ChainEpoch) FullNodeOpts {
}
}

var FullNodeWithSDRAt = func(calico, persian abi.ChainEpoch) FullNodeOpts {
return FullNodeOpts{
Opts: func(nodes []TestNode) node.Option {
return node.Override(new(stmgr.UpgradeSchedule), stmgr.UpgradeSchedule{{
Network: network.Version6,
Height: 1,
Migration: stmgr.UpgradeActorsV2,
}, {
Network: network.Version7,
Height: calico,
Migration: stmgr.UpgradeCalico,
}, {
Network: network.Version8,
Height: persian,
}})
},
}
}

var MineNext = miner.MineReq{
InjectNulls: 0,
Done: func(bool, abi.ChainEpoch, error) {},
Expand Down
121 changes: 107 additions & 14 deletions api/test/window_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,113 @@ package test
import (
"context"
"fmt"
"sort"
"sync/atomic"

"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/EpiK-Protocol/go-epik/extern/sector-storage/mock"
sealing "github.com/EpiK-Protocol/go-epik/extern/storage-sealing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/specs-storage/storage"

"github.com/EpiK-Protocol/go-epik/api"
"github.com/EpiK-Protocol/go-epik/build"
"github.com/EpiK-Protocol/go-epik/chain/types"
bminer "github.com/EpiK-Protocol/go-epik/miner"
"github.com/EpiK-Protocol/go-epik/node/impl"
"github.com/filecoin-project/lotus/node/impl"
)

func TestSDRUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n, sn := b(t, []FullNodeOpts{FullNodeWithSDRAt(500, 1000)}, OneMiner)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]

addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}

if err := miner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
build.Clock.Sleep(time.Second)

pledge := make(chan struct{})
mine := int64(1)
done := make(chan struct{})
go func() {
defer close(done)
round := 0
for atomic.LoadInt64(&mine) != 0 {
build.Clock.Sleep(blocktime)
if err := sn[0].MineOne(ctx, bminer.MineReq{Done: func(bool, abi.ChainEpoch, error) {

}}); err != nil {
t.Error(err)
}

// 3 sealing rounds: before, during after.
if round >= 3 {
continue
}

head, err := client.ChainHead(ctx)
assert.NoError(t, err)

// rounds happen every 100 blocks, with a 50 block offset.
if head.Height() >= abi.ChainEpoch(round*500+50) {
round++
pledge <- struct{}{}

ver, err := client.StateNetworkVersion(ctx, head.Key())
assert.NoError(t, err)
switch round {
case 1:
assert.Equal(t, network.Version6, ver)
case 2:
assert.Equal(t, network.Version7, ver)
case 3:
assert.Equal(t, network.Version8, ver)
}
}

}
}()

// before.
pledgeSectors(t, ctx, miner, 9, 0, pledge)

s, err := miner.SectorsList(ctx)
require.NoError(t, err)
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})

for i, id := range s {
info, err := miner.SectorsStatus(ctx, id, true)
require.NoError(t, err)
expectProof := abi.RegisteredSealProof_StackedDrg2KiBV1
if i >= 3 {
// after
expectProof = abi.RegisteredSealProof_StackedDrg2KiBV1_1
}
assert.Equal(t, expectProof, info.SealProof, "sector %d, id %d", i, id)
}

atomic.StoreInt64(&mine, 0)
<-done
}

func TestPledgeSector(t *testing.T, b APIBuilder, blocktime time.Duration, nSectors int) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -63,11 +150,13 @@ func TestPledgeSector(t *testing.T, b APIBuilder, blocktime time.Duration, nSect

func pledgeSectors(t *testing.T, ctx context.Context, miner TestStorageNode, n, existing int, blockNotif <-chan struct{}) {
for i := 0; i < n; i++ {
err := miner.PledgeSector(ctx)
require.NoError(t, err)
if i%3 == 0 && blockNotif != nil {
<-blockNotif
log.Errorf("WAIT")
}
log.Errorf("PLEDGING %d", i)
err := miner.PledgeSector(ctx)
require.NoError(t, err)
}

for {
Expand Down Expand Up @@ -126,7 +215,7 @@ func testWindowPostUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration,
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n, sn := b(t, []FullNodeOpts{FullNodeWithUpgradeAt(upgradeHeight)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV2At(upgradeHeight)}, OneMiner)

client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
Expand Down Expand Up @@ -209,15 +298,17 @@ func testWindowPostUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration,

// Drop the partition
err = secs.ForEach(func(sid uint64) error {
return miner.StorageMiner.(*impl.StorageMinerAPI).IStorageMgr.(*mock.SectorMgr).MarkCorrupted(abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(sid),
return miner.StorageMiner.(*impl.StorageMinerAPI).IStorageMgr.(*mock.SectorMgr).MarkCorrupted(storage.SectorRef{
ID: abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(sid),
},
}, true)
})
require.NoError(t, err)
}

var s abi.SectorID
var s storage.SectorRef

// Drop 1 sectors from deadline 3 partition 0
{
Expand All @@ -238,9 +329,11 @@ func testWindowPostUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration,
require.NoError(t, err)
fmt.Println("the sectors", all)

s = abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(sn),
s = storage.SectorRef{
ID: abi.SectorID{
Miner: abi.ActorID(mid),
Number: abi.SectorNumber(sn),
},
}

err = miner.StorageMiner.(*impl.StorageMinerAPI).IStorageMgr.(*mock.SectorMgr).MarkFailed(s, true)
Expand Down
2 changes: 2 additions & 0 deletions build/params_2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
// var UpgradeLiftoffHeight = abi.ChainEpoch(-5)

// const UpgradeKumquatHeight = 15
// const UpgradeCalicoHeight = 20
// const UpgradePersianHeight = 25

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
Expand Down
4 changes: 4 additions & 0 deletions build/params_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var DrandSchedule = map[abi.ChainEpoch]DrandEnum{

// const UpgradeKumquatHeight = 170000

// // TODO: Height??
// const UpgradeCalicoHeight = 999999
// const UpgradePersianHeight = UpgradeCalicoHeight + (builtin2.EpochsInDay * 2)

func init() {
policy.SetConsensusMinerMinPower(abi.NewStoragePower(1))

Expand Down
4 changes: 3 additions & 1 deletion build/params_testground.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ var (
// UpgradeActorsV2Height abi.ChainEpoch = 10
// UpgradeLiftoffHeight abi.ChainEpoch = -5
// UpgradeKumquatHeight abi.ChainEpoch = -6
// UpgradeCalicoHeight abi.ChainEpoch = -7
// UpgradePersianHeight abi.ChainEpoch = -8

DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
}

NewestNetworkVersion = network.Version5
NewestNetworkVersion = network.Version8
// ActorUpgradeNetworkVersion = network.Version4

Devnet = true
Expand Down
42 changes: 42 additions & 0 deletions chain/stmgr/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,48 @@ func UpgradeLiftoff(ctx context.Context, sm *StateManager, cb ExecCallback, root
return tree.Flush(ctx)
}
func UpgradeCalico(ctx context.Context, sm *StateManager, cb ExecCallback, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
store := sm.cs.Store(ctx)
var stateRoot types.StateRoot
if err := store.Get(ctx, root, &stateRoot); err != nil {
return cid.Undef, xerrors.Errorf("failed to decode state root: %w", err)
}
if stateRoot.Version != types.StateTreeVersion1 {
return cid.Undef, xerrors.Errorf(
"expected state root version 1 for calico upgrade, got %d",
stateRoot.Version,
)
}
newHamtRoot, err := nv7.MigrateStateTree(ctx, store, stateRoot.Actors, epoch, nv7.DefaultConfig())
if err != nil {
return cid.Undef, xerrors.Errorf("running nv7 migration: %w", err)
}
newRoot, err := store.Put(ctx, &types.StateRoot{
Version: stateRoot.Version,
Actors: newHamtRoot,
Info: stateRoot.Info,
})
if err != nil {
return cid.Undef, xerrors.Errorf("failed to persist new state root: %w", err)
}
// perform some basic sanity checks to make sure everything still works.
if newSm, err := state.LoadStateTree(store, newRoot); err != nil {
return cid.Undef, xerrors.Errorf("state tree sanity load failed: %w", err)
} else if newRoot2, err := newSm.Flush(ctx); err != nil {
return cid.Undef, xerrors.Errorf("state tree sanity flush failed: %w", err)
} else if newRoot2 != newRoot {
return cid.Undef, xerrors.Errorf("state-root mismatch: %s != %s", newRoot, newRoot2)
} else if _, err := newSm.GetActor(builtin0.InitActorAddr); err != nil {
return cid.Undef, xerrors.Errorf("failed to load init actor after upgrade: %w", err)
}
return newRoot, nil
}
func setNetworkName(ctx context.Context, store adt.Store, tree *state.StateTree, name string) error {
ia, err := tree.GetActor(builtin0.InitActorAddr)
if err != nil {
Expand Down
Loading

0 comments on commit b59a49e

Please sign in to comment.