diff --git a/chain/actors/actor_multisig_test.go b/chain/actors/actor_multisig_test.go index b291039cd..b66a660a5 100644 --- a/chain/actors/actor_multisig_test.go +++ b/chain/actors/actor_multisig_test.go @@ -15,13 +15,13 @@ import ( func TestMultiSigCreate(t *testing.T) { var creatorAddr, sig1Addr, sig2Addr, outsideAddr address.Address opts := []HarnessOpt{ - HarnessAddr(&creatorAddr, 10000), - HarnessAddr(&sig1Addr, 10000), - HarnessAddr(&sig2Addr, 10000), - HarnessAddr(&outsideAddr, 1000), + HarnessAddr(&creatorAddr, 100000), + HarnessAddr(&sig1Addr, 100000), + HarnessAddr(&sig2Addr, 100000), + HarnessAddr(&outsideAddr, 100000), } - h := NewHarness2(t, opts...) + h := NewHarness(t, opts...) ret, _ := h.CreateActor(t, creatorAddr, actors.MultisigActorCodeCid, actors.MultiSigConstructorParams{ Signers: []address.Address{creatorAddr, sig1Addr, sig2Addr}, @@ -47,7 +47,7 @@ func TestMultiSigOps(t *testing.T) { HarnessAddr(&creatorAddr, 100000), HarnessAddr(&sig1Addr, 100000), HarnessAddr(&sig2Addr, 100000), - HarnessAddr(&outsideAddr, 1000), + HarnessAddr(&outsideAddr, 100000), HarnessActor(&multSigAddr, &creatorAddr, actors.MultisigActorCodeCid, func() interface{} { return actors.MultiSigConstructorParams{ @@ -57,7 +57,7 @@ func TestMultiSigOps(t *testing.T) { }), } - h := NewHarness2(t, opts...) + h := NewHarness(t, opts...) { const chargeVal = 2000 // Send funds into the multisig diff --git a/chain/actors/actor_paych_test.go b/chain/actors/actor_paych_test.go index 6867c6d51..c19d44792 100644 --- a/chain/actors/actor_paych_test.go +++ b/chain/actors/actor_paych_test.go @@ -13,11 +13,11 @@ import ( func TestPaychCreate(t *testing.T) { var creatorAddr, targetAddr address.Address opts := []HarnessOpt{ - HarnessAddr(&creatorAddr, 10000), - HarnessAddr(&targetAddr, 10000), + HarnessAddr(&creatorAddr, 100000), + HarnessAddr(&targetAddr, 100000), } - h := NewHarness2(t, opts...) + h := NewHarness(t, opts...) ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid, actors.PCAConstructorParams{ To: targetAddr, @@ -42,11 +42,11 @@ func signVoucher(t *testing.T, w *wallet.Wallet, addr address.Address, sv *types func TestPaychUpdate(t *testing.T) { var creatorAddr, targetAddr address.Address opts := []HarnessOpt{ - HarnessAddr(&creatorAddr, 10000), - HarnessAddr(&targetAddr, 10000), + HarnessAddr(&creatorAddr, 100000), + HarnessAddr(&targetAddr, 100000), } - h := NewHarness2(t, opts...) + h := NewHarness(t, opts...) ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid, actors.PCAConstructorParams{ To: targetAddr, diff --git a/chain/actors/actor_storagemarket_test.go b/chain/actors/actor_storagemarket_test.go index f368592ea..8ba9897df 100644 --- a/chain/actors/actor_storagemarket_test.go +++ b/chain/actors/actor_storagemarket_test.go @@ -21,11 +21,11 @@ func TestStorageMarketCreateMiner(t *testing.T) { var ownerAddr, workerAddr address.Address opts := []HarnessOpt{ - HarnessAddr(&ownerAddr, 10000), - HarnessAddr(&workerAddr, 10000), + HarnessAddr(&ownerAddr, 100000), + HarnessAddr(&workerAddr, 100000), } - h := NewHarness2(t, opts...) + h := NewHarness(t, opts...) var minerAddr address.Address { diff --git a/chain/actors/actors_test.go b/chain/actors/actors_test.go index 9d3419355..7fce5eef2 100644 --- a/chain/actors/actors_test.go +++ b/chain/actors/actors_test.go @@ -82,7 +82,7 @@ func TestVMInvokeMethod(t *testing.T) { Method: IAMethods.Exec, Params: enc, GasPrice: types.NewInt(1), - GasLimit: types.NewInt(1000), + GasLimit: types.NewInt(10000), Value: types.NewInt(0), } @@ -126,7 +126,7 @@ func TestStorageMarketActorCreateMiner(t *testing.T) { Method: SMAMethods.CreateStorageMiner, Params: enc, GasPrice: types.NewInt(1), - GasLimit: types.NewInt(1000), + GasLimit: types.NewInt(10000), Value: types.NewInt(0), } diff --git a/chain/actors/harness2_test.go b/chain/actors/harness2_test.go index daf741e98..3c46000bc 100644 --- a/chain/actors/harness2_test.go +++ b/chain/actors/harness2_test.go @@ -22,7 +22,7 @@ import ( "github.com/filecoin-project/go-lotus/chain/wallet" ) -const testGasLimit = 1000 +const testGasLimit = 10000 type HarnessInit struct { NAddrs uint64 @@ -37,9 +37,9 @@ const ( HarnessPostInit ) -type HarnessOpt func(testing.TB, *Harness2) error +type HarnessOpt func(testing.TB, *Harness) error -type Harness2 struct { +type Harness struct { HI HarnessInit Stage HarnessStage Nonces map[address.Address]uint64 @@ -57,7 +57,7 @@ type Harness2 struct { var HarnessMinerFunds = types.NewInt(1000000) func HarnessAddr(addr *address.Address, value uint64) HarnessOpt { - return func(t testing.TB, h *Harness2) error { + return func(t testing.TB, h *Harness) error { if h.Stage != HarnessPreInit { return nil } @@ -76,7 +76,7 @@ func HarnessAddr(addr *address.Address, value uint64) HarnessOpt { } func HarnessMiner(addr *address.Address) HarnessOpt { - return func(_ testing.TB, h *Harness2) error { + return func(_ testing.TB, h *Harness) error { if h.Stage != HarnessPreInit { return nil } @@ -92,7 +92,7 @@ func HarnessMiner(addr *address.Address) HarnessOpt { } func HarnessActor(actor *address.Address, creator *address.Address, code cid.Cid, params func() interface{}) HarnessOpt { - return func(t testing.TB, h *Harness2) error { + return func(t testing.TB, h *Harness) error { if h.Stage != HarnessPostInit { return nil } @@ -112,18 +112,18 @@ func HarnessActor(actor *address.Address, creator *address.Address, code cid.Cid } func HarnessCtx(ctx context.Context) HarnessOpt { - return func(t testing.TB, h *Harness2) error { + return func(t testing.TB, h *Harness) error { h.ctx = ctx return nil } } -func NewHarness2(t *testing.T, options ...HarnessOpt) *Harness2 { +func NewHarness(t *testing.T, options ...HarnessOpt) *Harness { w, err := wallet.NewWallet(wallet.NewMemKeyStore()) if err != nil { t.Fatal(err) } - h := &Harness2{ + h := &Harness{ Stage: HarnessPreInit, Nonces: make(map[address.Address]uint64), HI: HarnessInit{ @@ -172,7 +172,7 @@ func NewHarness2(t *testing.T, options ...HarnessOpt) *Harness2 { return h } -func (h *Harness2) Apply(t testing.TB, msg types.Message) (*vm.ApplyRet, *state.StateTree) { +func (h *Harness) Apply(t testing.TB, msg types.Message) (*vm.ApplyRet, *state.StateTree) { t.Helper() if msg.Nonce == 0 { msg.Nonce, _ = h.Nonces[msg.From] @@ -204,7 +204,7 @@ func (h *Harness2) Apply(t testing.TB, msg types.Message) (*vm.ApplyRet, *state. return ret, state } -func (h *Harness2) CreateActor(t testing.TB, from address.Address, +func (h *Harness) CreateActor(t testing.TB, from address.Address, code cid.Cid, params interface{}) (*vm.ApplyRet, *state.StateTree) { t.Helper() @@ -223,7 +223,7 @@ func (h *Harness2) CreateActor(t testing.TB, from address.Address, }) } -func (h *Harness2) SendFunds(t testing.TB, from address.Address, to address.Address, +func (h *Harness) SendFunds(t testing.TB, from address.Address, to address.Address, value types.BigInt) (*vm.ApplyRet, *state.StateTree) { t.Helper() return h.Apply(t, types.Message{ @@ -236,7 +236,7 @@ func (h *Harness2) SendFunds(t testing.TB, from address.Address, to address.Addr }) } -func (h *Harness2) Invoke(t testing.TB, from address.Address, to address.Address, +func (h *Harness) Invoke(t testing.TB, from address.Address, to address.Address, method uint64, params interface{}) (*vm.ApplyRet, *state.StateTree) { t.Helper() return h.Apply(t, types.Message{ @@ -250,7 +250,7 @@ func (h *Harness2) Invoke(t testing.TB, from address.Address, to address.Address }) } -func (h *Harness2) AssertBalance(t testing.TB, addr address.Address, amt uint64) { +func (h *Harness) AssertBalance(t testing.TB, addr address.Address, amt uint64) { t.Helper() b, err := h.vm.ActorBalance(addr) @@ -263,7 +263,7 @@ func (h *Harness2) AssertBalance(t testing.TB, addr address.Address, amt uint64) } } -func (h *Harness2) AssertBalanceChange(t testing.TB, addr address.Address, amt int64) { +func (h *Harness) AssertBalanceChange(t testing.TB, addr address.Address, amt int64) { t.Helper() lastBalance, ok := h.lastBalanceCheck[addr] if !ok { diff --git a/chain/actors/harness_test.go b/chain/actors/harness_test.go deleted file mode 100644 index cd913453e..000000000 --- a/chain/actors/harness_test.go +++ /dev/null @@ -1,184 +0,0 @@ -package actors_test - -import ( - "context" - "testing" - - . "github.com/filecoin-project/go-lotus/chain/actors" - "github.com/filecoin-project/go-lotus/chain/gen" - "github.com/filecoin-project/go-lotus/chain/store" - "github.com/filecoin-project/go-lotus/chain/vm" - - "github.com/filecoin-project/go-lotus/chain/address" - "github.com/filecoin-project/go-lotus/chain/state" - "github.com/filecoin-project/go-lotus/chain/types" - - dstore "github.com/ipfs/go-datastore" - hamt "github.com/ipfs/go-hamt-ipld" - bstore "github.com/ipfs/go-ipfs-blockstore" - cbor "github.com/ipfs/go-ipld-cbor" -) - -type Harness struct { - Steps []Step - From address.Address - Third address.Address - - currStep int - - t *testing.T - actors []address.Address - vm *vm.VM - bs bstore.Blockstore - cs *store.ChainStore -} - -type Step struct { - M types.Message - Ret func(*testing.T, *types.MessageReceipt) - Err func(*testing.T, error) -} - -func NewHarness(t *testing.T) *Harness { - h := &Harness{t: t} - h.bs = bstore.NewBlockstore(dstore.NewMapDatastore()) - - from := blsaddr(0) - maddr := blsaddr(1) - third := blsaddr(2) - - actors := map[address.Address]types.BigInt{ - from: types.NewInt(1000000), - maddr: types.NewInt(0), - third: types.NewInt(1000), - } - st, err := gen.MakeInitialStateTree(h.bs, actors) - if err != nil { - t.Fatal(err) - } - - stateroot, err := st.Flush() - if err != nil { - t.Fatal(err) - } - - h.cs = store.NewChainStore(h.bs, nil) - - h.vm, err = vm.NewVM(stateroot, 1, maddr, h.cs) - if err != nil { - t.Fatal(err) - } - h.actors = []address.Address{from, maddr, third} - h.From = from - h.Third = third - return h -} - -func (h *Harness) Execute() *state.StateTree { - for i, step := range h.Steps { - h.currStep = i - ret, err := h.vm.ApplyMessage(context.TODO(), &step.M) - if step.Err != nil { - step.Err(h.t, err) - } else { - h.NoError(h.t, err) - } - step.Ret(h.t, &ret.MessageReceipt) - } - stateroot, err := h.vm.Flush(context.TODO()) - if err != nil { - h.t.Fatalf("%+v", err) - } - cst := hamt.CSTFromBstore(h.bs) - state, err := state.LoadStateTree(cst, stateroot) - if err != nil { - h.t.Fatal(err) - } - return state -} - -func (h *Harness) DumpObject(obj interface{}) []byte { - enc, err := cbor.DumpObject(obj) - if err != nil { - h.t.Fatal(err) - } - return enc -} -func (h *Harness) NoError(t *testing.T, err error) { - if err != nil { - t.Fatalf("Error in step %d: %+v", h.currStep, err) - } -} - -func TestVMInvokeHarness(t *testing.T) { - h := NewHarness(t) - var outaddr address.Address - h.Steps = []Step{ - { - M: types.Message{ - To: InitActorAddress, - From: h.From, - Method: IAMethods.Exec, - Params: h.DumpObject( - &ExecParams{ - Code: StorageMinerCodeCid, - Params: h.DumpObject(&StorageMinerConstructorParams{ - Owner: h.From, - }), - }), - GasPrice: types.NewInt(1), - GasLimit: types.NewInt(1000), - Value: types.NewInt(0), - }, - Ret: func(t *testing.T, ret *types.MessageReceipt) { - if ret.ExitCode != 0 { - t.Fatal("invocation failed: ", ret.ExitCode) - } - - var err error - outaddr, err = address.NewFromBytes(ret.Return) - if err != nil { - t.Fatal(err) - } - - if outaddr.String() != "t0103" { - t.Fatal("hold up") - } - }, - Err: h.NoError, - }, - } - - state := h.Execute() - act, err := state.GetActor(outaddr) - if err != nil { - t.Fatal(err) - } - if act.Code != StorageMinerCodeCid { - t.Fatalf("Expected correct code, got %s, instead of %s", act.Code, StorageMinerCodeCid) - } - hblock, err := h.bs.Get(act.Head) - if err != nil { - t.Fatal(err) - } - - smas := &StorageMinerActorState{} - err = cbor.DecodeInto(hblock.RawData(), smas) - if err != nil { - t.Fatal(err) - } - - iblock, err := h.bs.Get(smas.Info) - if err != nil { - t.Fatal(err) - } - - var minfo MinerInfo - if err := cbor.DecodeInto(iblock.RawData(), &minfo); err != nil { - t.Fatal(err) - } - - if minfo.Owner != h.From { - t.Fatalf("Owner should be %s, but is %s", h.From, minfo.Owner) - } -} diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 874135bf9..5f339f39e 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -13,6 +13,7 @@ import ( "github.com/filecoin-project/go-lotus/lib/bufbstore" "go.opencensus.io/trace" + block "github.com/ipfs/go-block-format" bserv "github.com/ipfs/go-blockservice" cid "github.com/ipfs/go-cid" hamt "github.com/ipfs/go-hamt-ipld" @@ -26,9 +27,17 @@ var log = logging.Logger("vm") const ( gasFundTransfer = 10 - gasGetObj = 10 - gasPutObj = 20 - gasCommit = 50 + gasInvoke = 5 + + gasGetObj = 10 + gasPutObj = 20 + gasPutPerByte = 2 + gasCommit = 50 + gasPerMessageByte = 2 +) + +const ( + outOfGasErrCode = 200 ) type VMContext struct { @@ -58,21 +67,25 @@ func (vmc *VMContext) Message() *types.Message { // Storage interface func (vmc *VMContext) Put(i interface{}) (cid.Cid, aerrors.ActorError) { - if err := vmc.ChargeGas(gasPutObj); err != nil { - return cid.Undef, aerrors.Wrap(err, "out of gas") - } c, err := vmc.cst.Put(context.TODO(), i) if err != nil { + if aerr := vmc.ChargeGas(0); aerr != nil { + return cid.Undef, aerrors.Absorb(err, outOfGasErrCode, "Put out of gas") + } return cid.Undef, aerrors.Escalate(err, "putting cid") } return c, nil } func (vmc *VMContext) Get(c cid.Cid, out interface{}) aerrors.ActorError { - if err := vmc.ChargeGas(gasGetObj); err != nil { - return aerrors.Wrap(err, "out of gas") + err := vmc.cst.Get(context.TODO(), c, out) + if err != nil { + if aerr := vmc.ChargeGas(0); aerr != nil { + return aerrors.Absorb(err, outOfGasErrCode, "Get out of gas") + } + return aerrors.Escalate(err, "getting cid") } - return aerrors.Escalate(vmc.cst.Get(context.TODO(), c, out), "getting cid") + return nil } func (vmc *VMContext) GetHead() cid.Cid { @@ -127,7 +140,7 @@ func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt GasLimit: vmc.gasAvailable, } - ret, err, _ := vmc.vm.send(ctx, msg, vmc) + ret, err, _ := vmc.vm.send(ctx, msg, vmc, 0) return ret, err } @@ -144,7 +157,7 @@ func (vmc *VMContext) ChargeGas(amount uint64) aerrors.ActorError { toUse := types.NewInt(amount) vmc.gasUsed = types.BigAdd(vmc.gasUsed, toUse) if types.BigCmp(vmc.gasUsed, vmc.gasAvailable) > 0 { - return aerrors.Newf(254, "not enough gas: used=%s, available=%s", vmc.gasUsed, vmc.gasAvailable) + return aerrors.Newf(outOfGasErrCode, "not enough gas: used=%s, available=%s", vmc.gasUsed, vmc.gasAvailable) } return nil } @@ -201,8 +214,34 @@ func ResolveToKeyAddr(state types.StateTree, cst *hamt.CborIpldStore, addr addre return aast.Address, nil } +type hBlocks interface { + GetBlock(context.Context, cid.Cid) (block.Block, error) + AddBlock(block.Block) error +} + +var _ hBlocks = (*gasChargingBlocks)(nil) + +type gasChargingBlocks struct { + chargeGas func(uint64) aerrors.ActorError + under hBlocks +} + +func (bs *gasChargingBlocks) GetBlock(ctx context.Context, c cid.Cid) (block.Block, error) { + if err := bs.chargeGas(gasGetObj); err != nil { + return nil, err + } + return bs.under.GetBlock(ctx, c) +} + +func (bs *gasChargingBlocks) AddBlock(blk block.Block) error { + if err := bs.chargeGas(gasPutObj + uint64(len(blk.RawData()))*gasPutPerByte); err != nil { + return err + } + return bs.under.AddBlock(blk) +} + func (vm *VM) makeVMContext(ctx context.Context, sroot cid.Cid, msg *types.Message, origin address.Address, usedGas types.BigInt) *VMContext { - return &VMContext{ + vmc := &VMContext{ ctx: ctx, vm: vm, state: vm.cstate, @@ -210,11 +249,15 @@ func (vm *VM) makeVMContext(ctx context.Context, sroot cid.Cid, msg *types.Messa msg: msg, origin: origin, height: vm.blockHeight, - cst: vm.cst, gasUsed: usedGas, gasAvailable: msg.GasLimit, } + vmc.cst = &hamt.CborIpldStore{ + Blocks: &gasChargingBlocks{vmc.ChargeGas, vm.cst.Blocks}, + Atlas: vm.cst.Atlas, + } + return vmc } type VM struct { @@ -253,7 +296,8 @@ type ApplyRet struct { ActorErr aerrors.ActorError } -func (vm *VM) send(ctx context.Context, msg *types.Message, parent *VMContext) ([]byte, aerrors.ActorError, *VMContext) { +func (vm *VM) send(ctx context.Context, msg *types.Message, parent *VMContext, + gasCharge uint64) ([]byte, aerrors.ActorError, *VMContext) { st := vm.cstate fromActor, err := st.GetActor(msg.From) @@ -274,10 +318,10 @@ func (vm *VM) send(ctx context.Context, msg *types.Message, parent *VMContext) ( } } - gasUsed := types.NewInt(0) + gasUsed := types.NewInt(gasCharge) origin := msg.From if parent != nil { - gasUsed = parent.gasUsed + gasUsed = types.BigAdd(parent.gasUsed, gasUsed) origin = parent.origin } vmctx := vm.makeVMContext(ctx, toActor.Head, msg, origin, gasUsed) @@ -341,6 +385,12 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet, return nil, xerrors.Errorf("from actor not found: %w", err) } + serMsg, err := msg.Serialize() + if err != nil { + return nil, xerrors.Errorf("could not serialize message: %w", err) + } + msgGasCost := uint64(len(serMsg)) * gasPerMessageByte + gascost := types.BigMul(msg.GasLimit, msg.GasPrice) totalCost := types.BigAdd(gascost, msg.Value) if types.BigCmp(fromActor.Balance, totalCost) < 0 { @@ -355,10 +405,15 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet, } fromActor.Nonce++ - ret, actorErr, vmctx := vm.send(ctx, msg, nil) + ret, actorErr, vmctx := vm.send(ctx, msg, nil, msgGasCost) + + if aerrors.IsFatal(actorErr) { + return nil, xerrors.Errorf("fatal error: %w", actorErr) + } var errcode uint8 var gasUsed types.BigInt + if errcode = aerrors.RetCode(actorErr); errcode != 0 { gasUsed = msg.GasLimit // revert all state changes since snapshot @@ -488,6 +543,9 @@ func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params [ defer func() { vmctx.ctx = oldCtx }() + if err := vmctx.ChargeGas(gasInvoke); err != nil { + return nil, aerrors.Wrap(err, "invokeing") + } ret, err := vm.inv.Invoke(act, vmctx, method, params) if err != nil { return nil, err