Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Add gas to test vm
Browse files Browse the repository at this point in the history
- introduce runtime.Pricelist - the price list interface
- copy current filecoin pricelist to testing package
- charge for gas
- return (and ignore) gas used from ApplyMessage
- setup large default gas limit
- handle out of gas aborts
  • Loading branch information
ZenGround0 committed May 18, 2021
1 parent 5114581 commit 82031c2
Show file tree
Hide file tree
Showing 13 changed files with 774 additions and 52 deletions.
27 changes: 27 additions & 0 deletions actors/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,30 @@ type StateHandle interface {
// ```
StateTransaction(obj cbor.Er, f func())
}

type Pricelist interface {
// OnChainMessage returns the gas used for storing a message of a given size in the chain.
OnChainMessage(msgSize int) GasCharge
// OnChainReturnValue returns the gas used for storing the response of a message in the chain.
OnChainReturnValue(dataSize int) GasCharge

// OnMethodInvocation returns the gas used when invoking a method.
OnMethodInvocation(value abi.TokenAmount, methodNum abi.MethodNum) GasCharge

// OnIpldGet returns the gas used for storing an object
OnIpldGet() GasCharge
// OnIpldPut returns the gas used for storing an object
OnIpldPut(dataSize int) GasCharge

// OnCreateActor returns the gas used for creating an actor
OnCreateActor() GasCharge
// OnDeleteActor returns the gas used for deleting an actor
OnDeleteActor() GasCharge

OnVerifySignature(sigType crypto.SigType, planTextSize int) (GasCharge, error)
OnHashing(dataSize int) GasCharge
OnComputeUnsealedSectorCid(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) GasCharge
OnVerifySeal(info proof.SealVerifyInfo) GasCharge
OnVerifyPost(info proof.WindowPoStVerifyInfo) GasCharge
OnVerifyConsensusFault() GasCharge
}
27 changes: 27 additions & 0 deletions actors/runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ const (
)

type VMActor = rt.VMActor

type GasCharge struct {
Name string
Extra interface{}

ComputeGas int64
StorageGas int64

VirtualCompute int64
VirtualStorage int64
}

func (g GasCharge) Total() int64 {
return g.ComputeGas + g.StorageGas
}
func (g GasCharge) WithVirtual(compute, storage int64) GasCharge {
out := g
out.VirtualCompute = compute
out.VirtualStorage = storage
return out
}

func (g GasCharge) WithExtra(extra interface{}) GasCharge {
out := g
out.Extra = extra
return out
}
2 changes: 1 addition & 1 deletion actors/test/commit_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestCommitPoStFlow(t *testing.T) {
ChainCommitRand: []byte("not really random"),
}
// PoSt is rejected for skipping all sectors.
_, code := tv.ApplyMessage(addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.SubmitWindowedPoSt, &submitParams)
_, code, _ := tv.ApplyMessage(addrs[0], minerAddrs.RobustAddress, big.Zero(), builtin.MethodsMiner.SubmitWindowedPoSt, &submitParams)
assert.Equal(t, exitcode.ErrIllegalArgument, code)

vm.ExpectInvocation{
Expand Down
8 changes: 5 additions & 3 deletions actors/test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func publishDeal(t *testing.T, v *vm.VM, provider, dealClient, minerID addr.Addr

publishDealParams := market.PublishStorageDealsParams{
Deals: []market.ClientDealProposal{{
Proposal: deal,
ClientSignature: crypto.Signature{},
Proposal: deal,
ClientSignature: crypto.Signature{
Type: crypto.SigTypeBLS,
},
}},
}
ret, code := v.ApplyMessage(provider, builtin.StorageMarketActorAddr, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, &publishDealParams)
ret, code, _ := v.ApplyMessage(provider, builtin.StorageMarketActorAddr, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, &publishDealParams)
require.Equal(t, exitcode.Ok, code)

expectedPublishSubinvocations := []vm.ExpectInvocation{
Expand Down
6 changes: 3 additions & 3 deletions actors/test/multisig_delete_self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestMultisigDeleteSelf2Of3RemovedIsProposer(t *testing.T) {
vm.ApplyOk(t, v, addrs[1], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)

// txnid not found when third approval gets processed indicating that the transaction has gone through successfully
_, code := v.ApplyMessage(addrs[2], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
_, code, _ := v.ApplyMessage(addrs[2], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
assert.Equal(t, exitcode.ErrNotFound, code)

}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestMultisigDeleteSelf2Of3RemovedIsApprover(t *testing.T) {
vm.ApplyOk(t, v, addrs[0], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)

// txnid not found when third approval gets processed indicating that the transaction has gone through successfully
_, code := v.ApplyMessage(addrs[2], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
_, code, _ := v.ApplyMessage(addrs[2], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
assert.Equal(t, exitcode.ErrNotFound, code)

}
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestMultisigDeleteSelf2Of2(t *testing.T) {
vm.ApplyOk(t, v, addrs[1], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)

// txnid not found when another approval gets processed indicating that the transaction has gone through successfully
_, code := v.ApplyMessage(addrs[1], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
_, code, _ := v.ApplyMessage(addrs[1], multisigAddr, big.Zero(), builtin.MethodsMultisig.Approve, &approveRemoveSignerParams)
assert.Equal(t, exitcode.ErrNotFound, code)
}

Expand Down
7 changes: 7 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/filecoin-project/specs-actors/v5/actors/builtin/system"
"github.com/filecoin-project/specs-actors/v5/actors/builtin/verifreg"
"github.com/filecoin-project/specs-actors/v5/actors/util/smoothing"
vm_test "github.com/filecoin-project/specs-actors/v5/support/vm"
)

func main() {
Expand Down Expand Up @@ -233,4 +234,10 @@ func main() {
panic(err)
}

if err := gen.WriteTupleEncodersToFile("./support/vm/cbor_gen.go", "vm_test",
vm_test.ChainMessage{},
); err != nil {
panic(err)
}

}
5 changes: 3 additions & 2 deletions support/agent/deal_client_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package agent

import (
"crypto/sha256"
mh "github.com/multiformats/go-multihash"
"math/bits"
"math/rand"
"strconv"

mh "github.com/multiformats/go-multihash"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
Expand Down Expand Up @@ -170,7 +171,7 @@ func (dca *DealClientAgent) createDeal(s SimState, provider DealProvider) error

provider.CreateDeal(market.ClientDealProposal{
Proposal: proposal,
ClientSignature: crypto.Signature{},
ClientSignature: crypto.Signature{Type: crypto.SigTypeBLS},
})
dca.DealCount++
return nil
Expand Down
9 changes: 4 additions & 5 deletions support/agent/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *Sim) Tick() error {

// run messages
for _, msg := range blockMessages {
ret, code := s.v.ApplyMessage(msg.From, msg.To, msg.Value, msg.Method, msg.Params)
ret, code, _ := s.v.ApplyMessage(msg.From, msg.To, msg.Value, msg.Method, msg.Params)

// for now, assume everything should work
if code != exitcode.Ok {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *Sim) Tick() error {
}

// run cron
_, code := s.v.ApplyMessage(builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
_, code, _ := s.v.ApplyMessage(builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)
if code != exitcode.Ok {
return errors.Errorf("exitcode %d: cron message failed:\n%s\n", code, strings.Join(s.v.GetLogs(), "\n"))
}
Expand Down Expand Up @@ -315,7 +315,7 @@ func (s *Sim) rewardMiner(addr address.Address, wins uint64) error {
GasReward: big.Zero(),
WinCount: int64(wins),
}
_, code := s.v.ApplyMessage(builtin.SystemActorAddr, builtin.RewardActorAddr, big.Zero(), builtin.MethodsReward.AwardBlockReward, &rewardParams)
_, code, _ := s.v.ApplyMessage(builtin.SystemActorAddr, builtin.RewardActorAddr, big.Zero(), builtin.MethodsReward.AwardBlockReward, &rewardParams)
if code != exitcode.Ok {
return errors.Errorf("exitcode %d: reward message failed:\n%s\n", code, strings.Join(s.v.GetLogs(), "\n"))
}
Expand Down Expand Up @@ -441,7 +441,7 @@ type PowerTable struct {

// VM interface allowing a simulation to operate over multiple VM versions
type SimVM interface {
ApplyMessage(from, to address.Address, value abi.TokenAmount, method abi.MethodNum, params interface{}) (cbor.Marshaler, exitcode.ExitCode)
ApplyMessage(from, to address.Address, value abi.TokenAmount, method abi.MethodNum, params interface{}) (cbor.Marshaler, exitcode.ExitCode, int64)
GetCirculatingSupply() abi.TokenAmount
GetLogs() []string
GetState(addr address.Address, out cbor.Unmarshaler) error
Expand All @@ -458,7 +458,6 @@ type SimVM interface {
}

var _ SimVM = (*vm.VM)(nil)
var _ SimVM = (*vm2.VM)(nil)

type SimMinerState interface {
HasSectorNo(adt.Store, abi.SectorNumber) (bool, error)
Expand Down
Loading

0 comments on commit 82031c2

Please sign in to comment.