Merge pull request #164 from filecoin-project/feat/gas4
Charge gas per storage byte and msg byte
This commit is contained in:
commit
1bfa0483fa
@ -15,13 +15,13 @@ import (
|
|||||||
func TestMultiSigCreate(t *testing.T) {
|
func TestMultiSigCreate(t *testing.T) {
|
||||||
var creatorAddr, sig1Addr, sig2Addr, outsideAddr address.Address
|
var creatorAddr, sig1Addr, sig2Addr, outsideAddr address.Address
|
||||||
opts := []HarnessOpt{
|
opts := []HarnessOpt{
|
||||||
HarnessAddr(&creatorAddr, 10000),
|
HarnessAddr(&creatorAddr, 100000),
|
||||||
HarnessAddr(&sig1Addr, 10000),
|
HarnessAddr(&sig1Addr, 100000),
|
||||||
HarnessAddr(&sig2Addr, 10000),
|
HarnessAddr(&sig2Addr, 100000),
|
||||||
HarnessAddr(&outsideAddr, 1000),
|
HarnessAddr(&outsideAddr, 100000),
|
||||||
}
|
}
|
||||||
|
|
||||||
h := NewHarness2(t, opts...)
|
h := NewHarness(t, opts...)
|
||||||
ret, _ := h.CreateActor(t, creatorAddr, actors.MultisigActorCodeCid,
|
ret, _ := h.CreateActor(t, creatorAddr, actors.MultisigActorCodeCid,
|
||||||
actors.MultiSigConstructorParams{
|
actors.MultiSigConstructorParams{
|
||||||
Signers: []address.Address{creatorAddr, sig1Addr, sig2Addr},
|
Signers: []address.Address{creatorAddr, sig1Addr, sig2Addr},
|
||||||
@ -47,7 +47,7 @@ func TestMultiSigOps(t *testing.T) {
|
|||||||
HarnessAddr(&creatorAddr, 100000),
|
HarnessAddr(&creatorAddr, 100000),
|
||||||
HarnessAddr(&sig1Addr, 100000),
|
HarnessAddr(&sig1Addr, 100000),
|
||||||
HarnessAddr(&sig2Addr, 100000),
|
HarnessAddr(&sig2Addr, 100000),
|
||||||
HarnessAddr(&outsideAddr, 1000),
|
HarnessAddr(&outsideAddr, 100000),
|
||||||
HarnessActor(&multSigAddr, &creatorAddr, actors.MultisigActorCodeCid,
|
HarnessActor(&multSigAddr, &creatorAddr, actors.MultisigActorCodeCid,
|
||||||
func() interface{} {
|
func() interface{} {
|
||||||
return actors.MultiSigConstructorParams{
|
return actors.MultiSigConstructorParams{
|
||||||
@ -57,7 +57,7 @@ func TestMultiSigOps(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
h := NewHarness2(t, opts...)
|
h := NewHarness(t, opts...)
|
||||||
{
|
{
|
||||||
const chargeVal = 2000
|
const chargeVal = 2000
|
||||||
// Send funds into the multisig
|
// Send funds into the multisig
|
||||||
|
@ -13,11 +13,11 @@ import (
|
|||||||
func TestPaychCreate(t *testing.T) {
|
func TestPaychCreate(t *testing.T) {
|
||||||
var creatorAddr, targetAddr address.Address
|
var creatorAddr, targetAddr address.Address
|
||||||
opts := []HarnessOpt{
|
opts := []HarnessOpt{
|
||||||
HarnessAddr(&creatorAddr, 10000),
|
HarnessAddr(&creatorAddr, 100000),
|
||||||
HarnessAddr(&targetAddr, 10000),
|
HarnessAddr(&targetAddr, 100000),
|
||||||
}
|
}
|
||||||
|
|
||||||
h := NewHarness2(t, opts...)
|
h := NewHarness(t, opts...)
|
||||||
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
||||||
actors.PCAConstructorParams{
|
actors.PCAConstructorParams{
|
||||||
To: targetAddr,
|
To: targetAddr,
|
||||||
@ -42,11 +42,11 @@ func signVoucher(t *testing.T, w *wallet.Wallet, addr address.Address, sv *types
|
|||||||
func TestPaychUpdate(t *testing.T) {
|
func TestPaychUpdate(t *testing.T) {
|
||||||
var creatorAddr, targetAddr address.Address
|
var creatorAddr, targetAddr address.Address
|
||||||
opts := []HarnessOpt{
|
opts := []HarnessOpt{
|
||||||
HarnessAddr(&creatorAddr, 10000),
|
HarnessAddr(&creatorAddr, 100000),
|
||||||
HarnessAddr(&targetAddr, 10000),
|
HarnessAddr(&targetAddr, 100000),
|
||||||
}
|
}
|
||||||
|
|
||||||
h := NewHarness2(t, opts...)
|
h := NewHarness(t, opts...)
|
||||||
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
ret, _ := h.CreateActor(t, creatorAddr, actors.PaymentChannelActorCodeCid,
|
||||||
actors.PCAConstructorParams{
|
actors.PCAConstructorParams{
|
||||||
To: targetAddr,
|
To: targetAddr,
|
||||||
|
@ -20,11 +20,11 @@ func TestStorageMarketCreateMiner(t *testing.T) {
|
|||||||
var ownerAddr, workerAddr address.Address
|
var ownerAddr, workerAddr address.Address
|
||||||
|
|
||||||
opts := []HarnessOpt{
|
opts := []HarnessOpt{
|
||||||
HarnessAddr(&ownerAddr, 10000),
|
HarnessAddr(&ownerAddr, 100000),
|
||||||
HarnessAddr(&workerAddr, 10000),
|
HarnessAddr(&workerAddr, 100000),
|
||||||
}
|
}
|
||||||
|
|
||||||
h := NewHarness2(t, opts...)
|
h := NewHarness(t, opts...)
|
||||||
|
|
||||||
var minerAddr address.Address
|
var minerAddr address.Address
|
||||||
{
|
{
|
||||||
|
@ -81,7 +81,7 @@ func TestVMInvokeMethod(t *testing.T) {
|
|||||||
Method: IAMethods.Exec,
|
Method: IAMethods.Exec,
|
||||||
Params: enc,
|
Params: enc,
|
||||||
GasPrice: types.NewInt(1),
|
GasPrice: types.NewInt(1),
|
||||||
GasLimit: types.NewInt(1000),
|
GasLimit: types.NewInt(10000),
|
||||||
Value: types.NewInt(0),
|
Value: types.NewInt(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ func TestStorageMarketActorCreateMiner(t *testing.T) {
|
|||||||
Method: SMAMethods.CreateStorageMiner,
|
Method: SMAMethods.CreateStorageMiner,
|
||||||
Params: enc,
|
Params: enc,
|
||||||
GasPrice: types.NewInt(1),
|
GasPrice: types.NewInt(1),
|
||||||
GasLimit: types.NewInt(1000),
|
GasLimit: types.NewInt(10000),
|
||||||
Value: types.NewInt(0),
|
Value: types.NewInt(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
"github.com/filecoin-project/go-lotus/chain/wallet"
|
"github.com/filecoin-project/go-lotus/chain/wallet"
|
||||||
)
|
)
|
||||||
|
|
||||||
const testGasLimit = 1000
|
const testGasLimit = 10000
|
||||||
|
|
||||||
type HarnessInit struct {
|
type HarnessInit struct {
|
||||||
NAddrs uint64
|
NAddrs uint64
|
||||||
@ -37,9 +37,9 @@ const (
|
|||||||
HarnessPostInit
|
HarnessPostInit
|
||||||
)
|
)
|
||||||
|
|
||||||
type HarnessOpt func(testing.TB, *Harness2) error
|
type HarnessOpt func(testing.TB, *Harness) error
|
||||||
|
|
||||||
type Harness2 struct {
|
type Harness struct {
|
||||||
HI HarnessInit
|
HI HarnessInit
|
||||||
Stage HarnessStage
|
Stage HarnessStage
|
||||||
Nonces map[address.Address]uint64
|
Nonces map[address.Address]uint64
|
||||||
@ -57,7 +57,7 @@ type Harness2 struct {
|
|||||||
var HarnessMinerFunds = types.NewInt(1000000)
|
var HarnessMinerFunds = types.NewInt(1000000)
|
||||||
|
|
||||||
func HarnessAddr(addr *address.Address, value uint64) HarnessOpt {
|
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 {
|
if h.Stage != HarnessPreInit {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ func HarnessAddr(addr *address.Address, value uint64) HarnessOpt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HarnessMiner(addr *address.Address) 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 {
|
if h.Stage != HarnessPreInit {
|
||||||
return nil
|
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 {
|
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 {
|
if h.Stage != HarnessPostInit {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -112,18 +112,18 @@ func HarnessActor(actor *address.Address, creator *address.Address, code cid.Cid
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HarnessCtx(ctx context.Context) HarnessOpt {
|
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
|
h.ctx = ctx
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHarness2(t *testing.T, options ...HarnessOpt) *Harness2 {
|
func NewHarness(t *testing.T, options ...HarnessOpt) *Harness {
|
||||||
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
h := &Harness2{
|
h := &Harness{
|
||||||
Stage: HarnessPreInit,
|
Stage: HarnessPreInit,
|
||||||
Nonces: make(map[address.Address]uint64),
|
Nonces: make(map[address.Address]uint64),
|
||||||
HI: HarnessInit{
|
HI: HarnessInit{
|
||||||
@ -172,7 +172,7 @@ func NewHarness2(t *testing.T, options ...HarnessOpt) *Harness2 {
|
|||||||
return h
|
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()
|
t.Helper()
|
||||||
if msg.Nonce == 0 {
|
if msg.Nonce == 0 {
|
||||||
msg.Nonce, _ = h.Nonces[msg.From]
|
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
|
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) {
|
code cid.Cid, params interface{}) (*vm.ApplyRet, *state.StateTree) {
|
||||||
t.Helper()
|
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) {
|
value types.BigInt) (*vm.ApplyRet, *state.StateTree) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return h.Apply(t, types.Message{
|
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) {
|
method uint64, params interface{}) (*vm.ApplyRet, *state.StateTree) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return h.Apply(t, types.Message{
|
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()
|
t.Helper()
|
||||||
|
|
||||||
b, err := h.vm.ActorBalance(addr)
|
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()
|
t.Helper()
|
||||||
lastBalance, ok := h.lastBalanceCheck[addr]
|
lastBalance, ok := h.lastBalanceCheck[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/filecoin-project/go-lotus/lib/bufbstore"
|
"github.com/filecoin-project/go-lotus/lib/bufbstore"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
|
|
||||||
|
block "github.com/ipfs/go-block-format"
|
||||||
bserv "github.com/ipfs/go-blockservice"
|
bserv "github.com/ipfs/go-blockservice"
|
||||||
cid "github.com/ipfs/go-cid"
|
cid "github.com/ipfs/go-cid"
|
||||||
hamt "github.com/ipfs/go-hamt-ipld"
|
hamt "github.com/ipfs/go-hamt-ipld"
|
||||||
@ -26,9 +27,17 @@ var log = logging.Logger("vm")
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
gasFundTransfer = 10
|
gasFundTransfer = 10
|
||||||
gasGetObj = 10
|
gasInvoke = 5
|
||||||
gasPutObj = 20
|
|
||||||
gasCommit = 50
|
gasGetObj = 10
|
||||||
|
gasPutObj = 20
|
||||||
|
gasPutPerByte = 2
|
||||||
|
gasCommit = 50
|
||||||
|
gasPerMessageByte = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
outOfGasErrCode = 200
|
||||||
)
|
)
|
||||||
|
|
||||||
type VMContext struct {
|
type VMContext struct {
|
||||||
@ -58,21 +67,25 @@ func (vmc *VMContext) Message() *types.Message {
|
|||||||
// Storage interface
|
// Storage interface
|
||||||
|
|
||||||
func (vmc *VMContext) Put(i interface{}) (cid.Cid, aerrors.ActorError) {
|
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)
|
c, err := vmc.cst.Put(context.TODO(), i)
|
||||||
if err != nil {
|
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 cid.Undef, aerrors.Escalate(err, "putting cid")
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vmc *VMContext) Get(c cid.Cid, out interface{}) aerrors.ActorError {
|
func (vmc *VMContext) Get(c cid.Cid, out interface{}) aerrors.ActorError {
|
||||||
if err := vmc.ChargeGas(gasGetObj); err != nil {
|
err := vmc.cst.Get(context.TODO(), c, out)
|
||||||
return aerrors.Wrap(err, "out of gas")
|
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 {
|
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,
|
GasLimit: vmc.gasAvailable,
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, err, _ := vmc.vm.send(ctx, msg, vmc)
|
ret, err, _ := vmc.vm.send(ctx, msg, vmc, 0)
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +157,7 @@ func (vmc *VMContext) ChargeGas(amount uint64) aerrors.ActorError {
|
|||||||
toUse := types.NewInt(amount)
|
toUse := types.NewInt(amount)
|
||||||
vmc.gasUsed = types.BigAdd(vmc.gasUsed, toUse)
|
vmc.gasUsed = types.BigAdd(vmc.gasUsed, toUse)
|
||||||
if types.BigCmp(vmc.gasUsed, vmc.gasAvailable) > 0 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@ -201,8 +214,34 @@ func ResolveToKeyAddr(state types.StateTree, cst *hamt.CborIpldStore, addr addre
|
|||||||
return aast.Address, nil
|
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 {
|
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,
|
ctx: ctx,
|
||||||
vm: vm,
|
vm: vm,
|
||||||
state: vm.cstate,
|
state: vm.cstate,
|
||||||
@ -210,11 +249,15 @@ func (vm *VM) makeVMContext(ctx context.Context, sroot cid.Cid, msg *types.Messa
|
|||||||
msg: msg,
|
msg: msg,
|
||||||
origin: origin,
|
origin: origin,
|
||||||
height: vm.blockHeight,
|
height: vm.blockHeight,
|
||||||
cst: vm.cst,
|
|
||||||
|
|
||||||
gasUsed: usedGas,
|
gasUsed: usedGas,
|
||||||
gasAvailable: msg.GasLimit,
|
gasAvailable: msg.GasLimit,
|
||||||
}
|
}
|
||||||
|
vmc.cst = &hamt.CborIpldStore{
|
||||||
|
Blocks: &gasChargingBlocks{vmc.ChargeGas, vm.cst.Blocks},
|
||||||
|
Atlas: vm.cst.Atlas,
|
||||||
|
}
|
||||||
|
return vmc
|
||||||
}
|
}
|
||||||
|
|
||||||
type VM struct {
|
type VM struct {
|
||||||
@ -253,7 +296,8 @@ type ApplyRet struct {
|
|||||||
ActorErr aerrors.ActorError
|
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
|
st := vm.cstate
|
||||||
fromActor, err := st.GetActor(msg.From)
|
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
|
origin := msg.From
|
||||||
if parent != nil {
|
if parent != nil {
|
||||||
gasUsed = parent.gasUsed
|
gasUsed = types.BigAdd(parent.gasUsed, gasUsed)
|
||||||
origin = parent.origin
|
origin = parent.origin
|
||||||
}
|
}
|
||||||
vmctx := vm.makeVMContext(ctx, toActor.Head, msg, origin, gasUsed)
|
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)
|
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)
|
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
|
||||||
totalCost := types.BigAdd(gascost, msg.Value)
|
totalCost := types.BigAdd(gascost, msg.Value)
|
||||||
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
|
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
|
||||||
@ -355,10 +405,15 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
|
|||||||
}
|
}
|
||||||
fromActor.Nonce++
|
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 errcode uint8
|
||||||
var gasUsed types.BigInt
|
var gasUsed types.BigInt
|
||||||
|
|
||||||
if errcode = aerrors.RetCode(actorErr); errcode != 0 {
|
if errcode = aerrors.RetCode(actorErr); errcode != 0 {
|
||||||
gasUsed = msg.GasLimit
|
gasUsed = msg.GasLimit
|
||||||
// revert all state changes since snapshot
|
// revert all state changes since snapshot
|
||||||
@ -488,6 +543,9 @@ func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params [
|
|||||||
defer func() {
|
defer func() {
|
||||||
vmctx.ctx = oldCtx
|
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)
|
ret, err := vm.inv.Invoke(act, vmctx, method, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
1
go.mod
1
go.mod
@ -65,7 +65,6 @@ require (
|
|||||||
github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a
|
github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a
|
||||||
github.com/smartystreets/assertions v1.0.1 // indirect
|
github.com/smartystreets/assertions v1.0.1 // indirect
|
||||||
github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect
|
github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect
|
||||||
github.com/stretchr/objx v0.1.1 // indirect
|
|
||||||
github.com/stretchr/testify v1.3.0
|
github.com/stretchr/testify v1.3.0
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a
|
||||||
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
|
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
|
||||||
|
6
go.sum
6
go.sum
@ -64,10 +64,6 @@ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m
|
|||||||
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
|
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
|
||||||
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
|
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
|
||||||
github.com/fd/go-nat v1.0.0/go.mod h1:BTBu/CKvMmOMUPkKVef1pngt2WFH/lg7E6yQnulfp6E=
|
github.com/fd/go-nat v1.0.0/go.mod h1:BTBu/CKvMmOMUPkKVef1pngt2WFH/lg7E6yQnulfp6E=
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828222406-5f4c30401bda h1:IZKqXB3VvlRCwroy8GbdHjyxJgueveGPXKiSL8nvI7o=
|
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828222406-5f4c30401bda/go.mod h1:muo8IeR187EUiX5AcMmIb0XSgpSU3qrszgh+pGWf3rY=
|
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828225523-8bbaf27f2782 h1:J0h4pFlx2ZSm1xWO7e2EesXz5xW/35AMqMzk4VGZ6B8=
|
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828225523-8bbaf27f2782/go.mod h1:muo8IeR187EUiX5AcMmIb0XSgpSU3qrszgh+pGWf3rY=
|
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828233303-a41bd0b6f985 h1:rpid5Xgp6GnDACqZvugxWvJ8L1HpyttUkzpUk/4BPXk=
|
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828233303-a41bd0b6f985 h1:rpid5Xgp6GnDACqZvugxWvJ8L1HpyttUkzpUk/4BPXk=
|
||||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828233303-a41bd0b6f985/go.mod h1:muo8IeR187EUiX5AcMmIb0XSgpSU3qrszgh+pGWf3rY=
|
github.com/filecoin-project/go-amt-ipld v0.0.0-20190828233303-a41bd0b6f985/go.mod h1:muo8IeR187EUiX5AcMmIb0XSgpSU3qrszgh+pGWf3rY=
|
||||||
github.com/filecoin-project/go-leb128 v0.0.0-20190212224330-8d79a5489543 h1:aMJGfgqe1QDhAVwxRg5fjCRF533xHidiKsugk7Vvzug=
|
github.com/filecoin-project/go-leb128 v0.0.0-20190212224330-8d79a5489543 h1:aMJGfgqe1QDhAVwxRg5fjCRF533xHidiKsugk7Vvzug=
|
||||||
@ -518,8 +514,6 @@ github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc h1:BCPnHtcboa
|
|||||||
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc/go.mod h1:r45hJU7yEoA81k6MWNhpMj/kms0n14dkzkxYHoB96UM=
|
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc/go.mod h1:r45hJU7yEoA81k6MWNhpMj/kms0n14dkzkxYHoB96UM=
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgPVpV1gWj68IIj1zQNyp7QzBvr5d5UOvMqRNw=
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgPVpV1gWj68IIj1zQNyp7QzBvr5d5UOvMqRNw=
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822012446-bb2210dd2804 h1:wK83hcnZgKf1AyH8804pc4zVZOB2ND+d2cagsuhQmZc=
|
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822012446-bb2210dd2804/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
|
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a h1:9oEQR9eq2H2JDmglMcrCa+TxUEYy3HKSiNoLIkPNy/U=
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a h1:9oEQR9eq2H2JDmglMcrCa+TxUEYy3HKSiNoLIkPNy/U=
|
||||||
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
|
||||||
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=
|
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=
|
||||||
|
Loading…
Reference in New Issue
Block a user