lotus/cmd/lotus-storage-miner/actor_test.go

104 lines
2.7 KiB
Go
Raw Normal View History

2020-10-21 03:35:18 +00:00
package main
import (
"bytes"
"context"
"flag"
"fmt"
"regexp"
"strconv"
"testing"
"time"
2021-06-21 10:56:10 +00:00
"github.com/filecoin-project/go-state-types/network"
2020-10-21 03:35:18 +00:00
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api"
2020-10-21 03:35:18 +00:00
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
2021-05-21 13:33:48 +00:00
"github.com/filecoin-project/lotus/itests/kit"
2020-10-21 03:35:18 +00:00
"github.com/filecoin-project/lotus/node/repo"
)
func TestWorkerKeyChange(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2021-06-07 22:56:59 +00:00
kit.QuietMiningLogs()
2020-10-21 03:35:18 +00:00
blocktime := 1 * time.Millisecond
2021-06-21 10:56:10 +00:00
client1, client2, miner, ens := kit.EnsembleTwoOne(t, kit.MockProofs(),
kit.ConstructorOpts(kit.InstantaneousNetworkVersion(network.Version13)),
)
ens.InterconnectAll().BeginMining(blocktime)
2020-10-21 03:35:18 +00:00
output := bytes.NewBuffer(nil)
run := func(cmd *cli.Command, args ...string) error {
app := cli.NewApp()
app.Metadata = map[string]interface{}{
"repoType": repo.StorageMiner,
2021-06-21 10:56:10 +00:00
"testnode-full": client1,
"testnode-storage": miner,
2020-10-21 03:35:18 +00:00
}
app.Writer = output
api.RunningNodeType = api.NodeMiner
2020-10-21 03:35:18 +00:00
fs := flag.NewFlagSet("", flag.ContinueOnError)
for _, f := range cmd.Flags {
if err := f.Apply(fs); err != nil {
return err
}
}
require.NoError(t, fs.Parse(args))
cctx := cli.NewContext(app, fs, nil)
return cmd.Action(cctx)
}
newKey, err := client1.WalletNew(ctx, types.KTBLS)
2020-10-21 03:35:18 +00:00
require.NoError(t, err)
// Initialize wallet.
2021-05-21 13:33:48 +00:00
kit.SendFunds(ctx, t, client1, newKey, abi.NewTokenAmount(0))
2020-10-21 03:35:18 +00:00
require.NoError(t, run(actorProposeChangeWorker, "--really-do-it", newKey.String()))
result := output.String()
output.Reset()
require.Contains(t, result, fmt.Sprintf("Worker key change to %s successfully proposed.", newKey))
epochRe := regexp.MustCompile("at or after height (?P<epoch>[0-9]+) to complete")
matches := epochRe.FindStringSubmatch(result)
require.NotNil(t, matches)
targetEpoch, err := strconv.Atoi(matches[1])
require.NoError(t, err)
require.NotZero(t, targetEpoch)
// Too early.
require.Error(t, run(actorConfirmChangeWorker, "--really-do-it", newKey.String()))
output.Reset()
2021-06-21 10:56:10 +00:00
client1.WaitTillChain(ctx, kit.HeightAtLeast(abi.ChainEpoch(targetEpoch)))
2020-10-21 03:35:18 +00:00
require.NoError(t, run(actorConfirmChangeWorker, "--really-do-it", newKey.String()))
output.Reset()
head, err := client1.ChainHead(ctx)
require.NoError(t, err)
// Wait for finality (worker key switch).
targetHeight := head.Height() + policy.ChainFinality
2021-06-21 10:56:10 +00:00
client1.WaitTillChain(ctx, kit.HeightAtLeast(targetHeight))
// Make sure the other node can catch up.
2021-06-21 10:56:10 +00:00
client2.WaitTillChain(ctx, kit.HeightAtLeast(targetHeight))
2020-10-21 03:35:18 +00:00
}