2020-01-31 23:51:15 +00:00
|
|
|
package stmgr_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-08 23:33:50 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
ipldcbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-01-31 23:51:15 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-28 19:48:08 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/cbor"
|
2020-10-08 01:09:33 +00:00
|
|
|
|
2020-10-15 00:29:25 +00:00
|
|
|
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-10-08 01:09:33 +00:00
|
|
|
init2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/init"
|
|
|
|
rt2 "github.com/filecoin-project/specs-actors/v2/actors/runtime"
|
2020-02-10 19:16:36 +00:00
|
|
|
|
2020-10-08 23:33:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-01-31 23:51:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
2020-10-08 20:32:54 +00:00
|
|
|
_init "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
2020-09-23 19:24:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
2020-01-31 23:51:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
|
|
|
. "github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-02-07 07:01:37 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
|
2020-01-31 23:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-23 19:24:51 +00:00
|
|
|
policy.SetSupportedProofTypes(abi.RegisteredSealProof_StackedDrg2KiBV1)
|
|
|
|
policy.SetConsensusMinerMinPower(abi.NewStoragePower(2048))
|
|
|
|
policy.SetMinVerifiedDealSize(abi.NewStoragePower(256))
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const testForkHeight = 40
|
|
|
|
|
|
|
|
type testActor struct {
|
|
|
|
}
|
|
|
|
|
2020-09-28 21:43:55 +00:00
|
|
|
// must use existing actor that an account is allowed to exec.
|
2020-10-15 00:29:25 +00:00
|
|
|
func (testActor) Code() cid.Cid { return builtin0.PaymentChannelActorCodeID }
|
2020-09-28 19:48:08 +00:00
|
|
|
func (testActor) State() cbor.Er { return new(testActorState) }
|
|
|
|
|
2020-01-31 23:51:15 +00:00
|
|
|
type testActorState struct {
|
|
|
|
HasUpgraded uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tas *testActorState) MarshalCBOR(w io.Writer) error {
|
|
|
|
return cbg.CborWriteHeader(w, cbg.MajUnsignedInt, tas.HasUpgraded)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tas *testActorState) UnmarshalCBOR(r io.Reader) error {
|
|
|
|
t, v, err := cbg.CborReadHeader(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if t != cbg.MajUnsignedInt {
|
2020-02-29 01:12:47 +00:00
|
|
|
return fmt.Errorf("wrong type in test actor state (got %d)", t)
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
tas.HasUpgraded = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-28 19:48:08 +00:00
|
|
|
func (ta testActor) Exports() []interface{} {
|
2020-01-31 23:51:15 +00:00
|
|
|
return []interface{}{
|
|
|
|
1: ta.Constructor,
|
|
|
|
2: ta.TestMethod,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
func (ta *testActor) Constructor(rt rt2.Runtime, params *abi.EmptyValue) *abi.EmptyValue {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.ValidateImmediateCallerAcceptAny()
|
2020-09-14 19:41:55 +00:00
|
|
|
rt.StateCreate(&testActorState{11})
|
2020-10-07 22:28:16 +00:00
|
|
|
//fmt.Println("NEW ACTOR ADDRESS IS: ", rt.Receiver())
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-09-10 06:30:47 +00:00
|
|
|
return abi.Empty
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
func (ta *testActor) TestMethod(rt rt2.Runtime, params *abi.EmptyValue) *abi.EmptyValue {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.ValidateImmediateCallerAcceptAny()
|
2020-01-31 23:51:15 +00:00
|
|
|
var st testActorState
|
2020-09-14 19:41:55 +00:00
|
|
|
rt.StateReadonly(&st)
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-02-29 01:12:47 +00:00
|
|
|
if rt.CurrEpoch() > testForkHeight {
|
2020-01-31 23:51:15 +00:00
|
|
|
if st.HasUpgraded != 55 {
|
2020-02-29 01:12:47 +00:00
|
|
|
panic(aerrors.Fatal("fork updating applied in wrong order"))
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if st.HasUpgraded != 11 {
|
2020-02-29 01:12:47 +00:00
|
|
|
panic(aerrors.Fatal("fork updating happened too early"))
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 06:30:47 +00:00
|
|
|
return abi.Empty
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestForkHeightTriggers(t *testing.T) {
|
|
|
|
logging.SetAllLoggers(logging.LevelInfo)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
cg, err := gen.NewGenerator()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// predicting the address here... may break if other assumptions change
|
2020-02-29 01:12:47 +00:00
|
|
|
taddr, err := address.NewIDAddress(1002)
|
2020-01-31 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:09:56 +00:00
|
|
|
sm, err := NewStateManagerWithUpgradeSchedule(
|
|
|
|
cg.ChainStore(), UpgradeSchedule{{
|
|
|
|
Network: 1,
|
|
|
|
Height: testForkHeight,
|
|
|
|
Migration: func(ctx context.Context, sm *StateManager, cb ExecCallback,
|
2020-10-07 20:56:38 +00:00
|
|
|
root cid.Cid, height abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
|
2020-10-01 22:02:40 +00:00
|
|
|
cst := ipldcbor.NewCborStore(sm.ChainStore().Blockstore())
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
st, err := sm.StateTree(root)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting state tree: %w", err)
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
act, err := st.GetActor(taddr)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
var tas testActorState
|
|
|
|
if err := cst.Get(ctx, act.Head, &tas); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("in fork handler, failed to run get: %w", err)
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
tas.HasUpgraded = 55
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
ns, err := cst.Put(ctx, &tas)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
act.Head = ns
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
if err := st.SetActor(taddr, act); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
return st.Flush(ctx)
|
2020-10-03 00:09:56 +00:00
|
|
|
}}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 22:02:40 +00:00
|
|
|
inv := vm.NewActorRegistry()
|
2020-09-28 19:48:08 +00:00
|
|
|
inv.Register(nil, testActor{})
|
2020-10-01 22:02:40 +00:00
|
|
|
|
2020-09-16 05:00:00 +00:00
|
|
|
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (*vm.VM, error) {
|
|
|
|
nvm, err := vm.NewVM(ctx, vmopt)
|
2020-01-31 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nvm.SetInvoker(inv)
|
|
|
|
return nvm, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cg.SetStateManager(sm)
|
|
|
|
|
|
|
|
var msgs []*types.SignedMessage
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
enc, err := actors.SerializeParams(&init2.ExecParams{CodeCID: (testActor{}).Code()})
|
2020-01-31 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &types.Message{
|
|
|
|
From: cg.Banker(),
|
2020-10-08 20:32:54 +00:00
|
|
|
To: _init.Address,
|
|
|
|
Method: _init.Methods.Exec,
|
2020-01-31 23:51:15 +00:00
|
|
|
Params: enc,
|
2020-07-20 17:57:06 +00:00
|
|
|
GasLimit: types.TestGasLimit,
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
2020-10-08 23:33:50 +00:00
|
|
|
sig, err := cg.Wallet().WalletSign(ctx, cg.Banker(), m.Cid().Bytes(), api.MsgMeta{})
|
2020-01-31 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
msgs = append(msgs, &types.SignedMessage{
|
|
|
|
Signature: *sig,
|
|
|
|
Message: *m,
|
|
|
|
})
|
|
|
|
|
|
|
|
nonce := uint64(1)
|
|
|
|
cg.GetMessages = func(cg *gen.ChainGen) ([]*types.SignedMessage, error) {
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
fmt.Println("added construct method")
|
|
|
|
m := msgs
|
|
|
|
msgs = nil
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &types.Message{
|
|
|
|
From: cg.Banker(),
|
|
|
|
To: taddr,
|
|
|
|
Method: 2,
|
|
|
|
Params: nil,
|
|
|
|
Nonce: nonce,
|
2020-07-20 17:57:06 +00:00
|
|
|
GasLimit: types.TestGasLimit,
|
2020-01-31 23:51:15 +00:00
|
|
|
}
|
|
|
|
nonce++
|
|
|
|
|
2020-10-08 23:33:50 +00:00
|
|
|
sig, err := cg.Wallet().WalletSign(ctx, cg.Banker(), m.Cid().Bytes(), api.MsgMeta{})
|
2020-01-31 23:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*types.SignedMessage{
|
2020-04-08 19:37:04 +00:00
|
|
|
{
|
2020-01-31 23:51:15 +00:00
|
|
|
Signature: *sig,
|
|
|
|
Message: *m,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
_, err = cg.NextTipSet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 22:28:16 +00:00
|
|
|
|
|
|
|
func TestForkRefuseCall(t *testing.T) {
|
|
|
|
logging.SetAllLoggers(logging.LevelInfo)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
cg, err := gen.NewGenerator()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := NewStateManagerWithUpgradeSchedule(
|
|
|
|
cg.ChainStore(), UpgradeSchedule{{
|
2020-10-07 23:14:11 +00:00
|
|
|
Network: 1,
|
|
|
|
Expensive: true,
|
|
|
|
Height: testForkHeight,
|
2020-10-07 22:28:16 +00:00
|
|
|
Migration: func(ctx context.Context, sm *StateManager, cb ExecCallback,
|
|
|
|
root cid.Cid, height abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
|
|
|
|
return root, nil
|
|
|
|
}}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inv := vm.NewActorRegistry()
|
|
|
|
inv.Register(nil, testActor{})
|
|
|
|
|
|
|
|
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (*vm.VM, error) {
|
|
|
|
nvm, err := vm.NewVM(ctx, vmopt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nvm.SetInvoker(inv)
|
|
|
|
return nvm, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cg.SetStateManager(sm)
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
enc, err := actors.SerializeParams(&init2.ExecParams{CodeCID: (testActor{}).Code()})
|
2020-10-07 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &types.Message{
|
|
|
|
From: cg.Banker(),
|
2020-10-08 20:32:54 +00:00
|
|
|
To: _init.Address,
|
|
|
|
Method: _init.Methods.Exec,
|
2020-10-07 22:28:16 +00:00
|
|
|
Params: enc,
|
|
|
|
GasLimit: types.TestGasLimit,
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasPremium: types.NewInt(0),
|
|
|
|
GasFeeCap: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
ts, err := cg.NextTipSet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := sm.CallWithGas(ctx, m, nil, ts.TipSet.TipSet())
|
|
|
|
switch ts.TipSet.TipSet().Height() {
|
|
|
|
case testForkHeight, testForkHeight + 1:
|
|
|
|
// If I had a fork, or I _will_ have a fork, it should fail.
|
2020-10-07 23:14:11 +00:00
|
|
|
require.Equal(t, ErrExpensiveFork, err)
|
2020-10-07 22:28:16 +00:00
|
|
|
default:
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
|
|
|
}
|
|
|
|
// Call just runs on the parent state for a tipset, so we only
|
|
|
|
// expect an error at the fork height.
|
|
|
|
ret, err = sm.Call(ctx, m, ts.TipSet.TipSet())
|
|
|
|
switch ts.TipSet.TipSet().Height() {
|
|
|
|
case testForkHeight + 1:
|
2020-10-07 23:14:11 +00:00
|
|
|
require.Equal(t, ErrExpensiveFork, err)
|
2020-10-07 22:28:16 +00:00
|
|
|
default:
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|