Charge per byte of storage

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-08-29 20:20:59 +02:00
parent 55abcaa7d0
commit 5c31b81701
9 changed files with 91 additions and 236 deletions

View File

@ -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

View File

@ -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,

View File

@ -20,11 +20,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
{

View File

@ -81,7 +81,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),
}
@ -125,7 +125,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),
}

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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,11 +27,17 @@ var log = logging.Logger("vm")
const (
gasFundTransfer = 10
gasGetObj = 10
gasPutObj = 20
gasPutPerByte = 2
gasCommit = 50
)
const (
outOfGasErrCode = 200
)
type VMContext struct {
ctx context.Context
@ -58,21 +65,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(vmc.cst.Get(context.TODO(), c, out), "getting cid")
return aerrors.Escalate(err, "getting cid")
}
return nil
}
func (vmc *VMContext) GetHead() cid.Cid {
@ -144,7 +155,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 +212,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 +247,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 {
@ -357,8 +398,13 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
ret, actorErr, vmctx := vm.send(ctx, msg, nil)
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

1
go.mod
View File

@ -65,7 +65,6 @@ require (
github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a
github.com/smartystreets/assertions v1.0.1 // 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/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7

6
go.sum
View File

@ -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/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/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/go.mod h1:muo8IeR187EUiX5AcMmIb0XSgpSU3qrszgh+pGWf3rY=
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/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-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/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=