2020-03-19 16:27:42 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2020-04-23 16:45:52 +00:00
|
|
|
"fmt"
|
2020-09-07 06:08:53 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2022-06-14 15:00:51 +00:00
|
|
|
proof7 "github.com/filecoin-project/specs-actors/v7/actors/runtime/proof"
|
2020-10-08 20:32:54 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-03-19 16:27:42 +00:00
|
|
|
)
|
|
|
|
|
2020-07-28 20:30:59 +00:00
|
|
|
type scalingCost struct {
|
|
|
|
flat int64
|
|
|
|
scale int64
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:44:41 +00:00
|
|
|
type stepCost []step
|
|
|
|
|
|
|
|
type step struct {
|
|
|
|
start int64
|
|
|
|
cost int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc stepCost) Lookup(x int64) int64 {
|
|
|
|
i := 0
|
|
|
|
for ; i < len(sc); i++ {
|
|
|
|
if sc[i].start > x {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i-- // look at previous item
|
|
|
|
if i < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc[i].cost
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
type pricelistV0 struct {
|
2020-11-04 19:08:42 +00:00
|
|
|
computeGasMulti int64
|
|
|
|
storageGasMulti int64
|
2020-03-19 16:27:42 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// System operations
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Gas cost charged to the originator of an on-chain message (regardless of
|
|
|
|
// whether it succeeds or fails in application) is given by:
|
|
|
|
// OnChainMessageBase + len(serialized message)*OnChainMessagePerByte
|
|
|
|
// Together, these account for the cost of message propagation and validation,
|
2022-03-15 23:40:17 +00:00
|
|
|
// up to but excluding any actual processing by the LegacyVM.
|
2020-03-19 16:27:42 +00:00
|
|
|
// This is the cost a block producer burns when including an invalid message.
|
2020-07-10 13:12:27 +00:00
|
|
|
onChainMessageComputeBase int64
|
|
|
|
onChainMessageStorageBase int64
|
|
|
|
onChainMessageStoragePerByte int64
|
2020-03-19 16:27:42 +00:00
|
|
|
|
|
|
|
// Gas cost charged to the originator of a non-nil return value produced
|
|
|
|
// by an on-chain message is given by:
|
|
|
|
// len(return value)*OnChainReturnValuePerByte
|
|
|
|
onChainReturnValuePerByte int64
|
|
|
|
|
|
|
|
// Gas cost for any message send execution(including the top-level one
|
|
|
|
// initiated by an on-chain message).
|
|
|
|
// This accounts for the cost of loading sender and receiver actors and
|
|
|
|
// (for top-level messages) incrementing the sender's sequence number.
|
|
|
|
// Load and store of actor sub-state is charged separately.
|
|
|
|
sendBase int64
|
|
|
|
|
|
|
|
// Gas cost charged, in addition to SendBase, if a message send
|
|
|
|
// is accompanied by any nonzero currency amount.
|
|
|
|
// Accounts for writing receiver's new balance (the sender's state is
|
|
|
|
// already accounted for).
|
|
|
|
sendTransferFunds int64
|
|
|
|
|
2020-07-10 13:12:27 +00:00
|
|
|
// Gsa cost charged, in addition to SendBase, if message only transfers funds.
|
|
|
|
sendTransferOnlyPremium int64
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
// Gas cost charged, in addition to SendBase, if a message invokes
|
|
|
|
// a method on the receiver.
|
|
|
|
// Accounts for the cost of loading receiver code and method dispatch.
|
|
|
|
sendInvokeMethod int64
|
|
|
|
|
2020-07-10 13:12:27 +00:00
|
|
|
// Gas cost for any Get operation to the IPLD store
|
2022-03-15 23:40:17 +00:00
|
|
|
// in the runtime LegacyVM context.
|
2020-07-10 13:12:27 +00:00
|
|
|
ipldGetBase int64
|
2020-03-19 16:27:42 +00:00
|
|
|
|
|
|
|
// Gas cost (Base + len*PerByte) for any Put operation to the IPLD store
|
2022-03-15 23:40:17 +00:00
|
|
|
// in the runtime LegacyVM context.
|
2020-03-19 16:27:42 +00:00
|
|
|
//
|
|
|
|
// Note: these costs should be significantly higher than the costs for Get
|
|
|
|
// operations, since they reflect not only serialization/deserialization
|
|
|
|
// but also persistent storage of chain data.
|
|
|
|
ipldPutBase int64
|
|
|
|
ipldPutPerByte int64
|
|
|
|
|
|
|
|
// Gas cost for creating a new actor (via InitActor's Exec method).
|
|
|
|
//
|
|
|
|
// Note: this costs assume that the extra will be partially or totally refunded while
|
|
|
|
// the base is covering for the put.
|
2020-07-10 13:12:27 +00:00
|
|
|
createActorCompute int64
|
|
|
|
createActorStorage int64
|
2020-03-19 16:27:42 +00:00
|
|
|
|
|
|
|
// Gas cost for deleting an actor.
|
|
|
|
//
|
|
|
|
// Note: this partially refunds the create cost to incentivise the deletion of the actors.
|
|
|
|
deleteActor int64
|
|
|
|
|
2020-07-10 13:12:27 +00:00
|
|
|
verifySignature map[crypto.SigType]int64
|
2020-03-19 16:27:42 +00:00
|
|
|
|
2020-07-10 13:12:27 +00:00
|
|
|
hashingBase int64
|
2020-03-19 16:27:42 +00:00
|
|
|
|
|
|
|
computeUnsealedSectorCidBase int64
|
|
|
|
verifySealBase int64
|
2021-03-10 15:16:44 +00:00
|
|
|
verifyAggregateSealBase int64
|
2021-05-27 15:44:41 +00:00
|
|
|
verifyAggregateSealPer map[abi.RegisteredSealProof]int64
|
|
|
|
verifyAggregateSealSteps map[abi.RegisteredSealProof]stepCost
|
|
|
|
|
|
|
|
verifyPostLookup map[abi.RegisteredPoStProof]scalingCost
|
|
|
|
verifyPostDiscount bool
|
|
|
|
verifyConsensusFault int64
|
2022-01-06 14:35:39 +00:00
|
|
|
|
|
|
|
verifyReplicaUpdate int64
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Pricelist = (*pricelistV0)(nil)
|
|
|
|
|
|
|
|
// OnChainMessage returns the gas used for storing a message of a given size in the chain.
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnChainMessage(msgSize int) GasCharge {
|
2020-07-10 13:12:27 +00:00
|
|
|
return newGasCharge("OnChainMessage", pl.onChainMessageComputeBase,
|
2020-11-04 19:08:42 +00:00
|
|
|
(pl.onChainMessageStorageBase+pl.onChainMessageStoragePerByte*int64(msgSize))*pl.storageGasMulti)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnChainReturnValue returns the gas used for storing the response of a message in the chain.
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnChainReturnValue(dataSize int) GasCharge {
|
2020-11-04 19:08:42 +00:00
|
|
|
return newGasCharge("OnChainReturnValue", 0, int64(dataSize)*pl.onChainReturnValuePerByte*pl.storageGasMulti)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnMethodInvocation returns the gas used when invoking a method.
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnMethodInvocation(value abi.TokenAmount, methodNum abi.MethodNum) GasCharge {
|
2020-03-19 16:27:42 +00:00
|
|
|
ret := pl.sendBase
|
2020-06-29 17:19:02 +00:00
|
|
|
extra := ""
|
2020-06-30 00:31:39 +00:00
|
|
|
|
2020-06-30 12:09:03 +00:00
|
|
|
if big.Cmp(value, abi.NewTokenAmount(0)) != 0 {
|
2020-07-10 13:12:27 +00:00
|
|
|
ret += pl.sendTransferFunds
|
2020-10-08 20:32:54 +00:00
|
|
|
if methodNum == builtin.MethodSend {
|
2020-06-30 12:09:03 +00:00
|
|
|
// transfer only
|
2020-07-10 13:12:27 +00:00
|
|
|
ret += pl.sendTransferOnlyPremium
|
2020-06-30 12:09:03 +00:00
|
|
|
}
|
2020-06-29 17:19:02 +00:00
|
|
|
extra += "t"
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
2020-07-10 13:12:27 +00:00
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
if methodNum != builtin.MethodSend {
|
2020-06-29 17:19:02 +00:00
|
|
|
extra += "i"
|
2020-06-30 12:09:03 +00:00
|
|
|
// running actors is cheaper becase we hand over to actors
|
2020-07-10 13:12:27 +00:00
|
|
|
ret += pl.sendInvokeMethod
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
2020-07-10 13:12:27 +00:00
|
|
|
return newGasCharge("OnMethodInvocation", ret, 0).WithExtra(extra)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnIpldGet returns the gas used for storing an object
|
2020-07-15 19:40:49 +00:00
|
|
|
func (pl *pricelistV0) OnIpldGet() GasCharge {
|
2020-11-11 17:59:48 +00:00
|
|
|
return newGasCharge("OnIpldGet", pl.ipldGetBase, 0).WithVirtual(114617, 0)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnIpldPut returns the gas used for storing an object
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnIpldPut(dataSize int) GasCharge {
|
2020-11-04 19:08:42 +00:00
|
|
|
return newGasCharge("OnIpldPut", pl.ipldPutBase, int64(dataSize)*pl.ipldPutPerByte*pl.storageGasMulti).
|
2020-11-11 17:59:48 +00:00
|
|
|
WithExtra(dataSize).WithVirtual(400000, int64(dataSize)*1300)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnCreateActor returns the gas used for creating an actor
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnCreateActor() GasCharge {
|
2020-11-04 19:08:42 +00:00
|
|
|
return newGasCharge("OnCreateActor", pl.createActorCompute, pl.createActorStorage*pl.storageGasMulti)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnDeleteActor returns the gas used for deleting an actor
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnDeleteActor() GasCharge {
|
2020-11-04 19:08:42 +00:00
|
|
|
return newGasCharge("OnDeleteActor", 0, pl.deleteActor*pl.storageGasMulti)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnVerifySignature
|
2020-06-29 16:46:50 +00:00
|
|
|
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) (GasCharge, error) {
|
2020-07-10 13:12:27 +00:00
|
|
|
cost, ok := pl.verifySignature[sigType]
|
2020-03-19 16:27:42 +00:00
|
|
|
if !ok {
|
2020-06-11 18:37:14 +00:00
|
|
|
return GasCharge{}, fmt.Errorf("cost function for signature type %d not supported", sigType)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
2020-06-30 12:09:03 +00:00
|
|
|
|
2020-07-10 13:12:27 +00:00
|
|
|
sigName, _ := sigType.Name()
|
|
|
|
return newGasCharge("OnVerifySignature", cost, 0).
|
2020-06-30 00:31:39 +00:00
|
|
|
WithExtra(map[string]interface{}{
|
|
|
|
"type": sigName,
|
|
|
|
"size": planTextSize,
|
2020-07-10 13:12:27 +00:00
|
|
|
}), nil
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnHashing
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnHashing(dataSize int) GasCharge {
|
2020-07-10 13:12:27 +00:00
|
|
|
return newGasCharge("OnHashing", pl.hashingBase, 0).WithExtra(dataSize)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnComputeUnsealedSectorCid
|
2020-06-15 16:30:49 +00:00
|
|
|
func (pl *pricelistV0) OnComputeUnsealedSectorCid(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) GasCharge {
|
2020-07-10 13:12:27 +00:00
|
|
|
return newGasCharge("OnComputeUnsealedSectorCid", pl.computeUnsealedSectorCidBase, 0)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnVerifySeal
|
2021-11-04 15:59:29 +00:00
|
|
|
func (pl *pricelistV0) OnVerifySeal(info proof7.SealVerifyInfo) GasCharge {
|
2020-03-19 16:27:42 +00:00
|
|
|
// TODO: this needs more cost tunning, check with @lotus
|
2020-07-10 13:12:27 +00:00
|
|
|
// this is not used
|
|
|
|
return newGasCharge("OnVerifySeal", pl.verifySealBase, 0)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:16:44 +00:00
|
|
|
// OnVerifyAggregateSeals
|
2021-11-04 15:59:29 +00:00
|
|
|
func (pl *pricelistV0) OnVerifyAggregateSeals(aggregate proof7.AggregateSealVerifyProofAndInfos) GasCharge {
|
2021-05-27 15:44:41 +00:00
|
|
|
proofType := aggregate.SealProof
|
|
|
|
perProof, ok := pl.verifyAggregateSealPer[proofType]
|
|
|
|
if !ok {
|
|
|
|
perProof = pl.verifyAggregateSealPer[abi.RegisteredSealProof_StackedDrg32GiBV1_1]
|
|
|
|
}
|
|
|
|
|
|
|
|
step, ok := pl.verifyAggregateSealSteps[proofType]
|
|
|
|
if !ok {
|
|
|
|
step = pl.verifyAggregateSealSteps[abi.RegisteredSealProof_StackedDrg32GiBV1_1]
|
|
|
|
}
|
|
|
|
num := int64(len(aggregate.Infos))
|
|
|
|
return newGasCharge("OnVerifyAggregateSeals", perProof*num+step.Lookup(num), 0)
|
2021-03-10 15:16:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
// OnVerifyReplicaUpdate
|
|
|
|
func (pl *pricelistV0) OnVerifyReplicaUpdate(update proof7.ReplicaUpdateInfo) GasCharge {
|
2022-01-06 14:35:39 +00:00
|
|
|
return newGasCharge("OnVerifyReplicaUpdate", pl.verifyReplicaUpdate, 0)
|
2021-11-04 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
// OnVerifyPost
|
2021-11-04 15:59:29 +00:00
|
|
|
func (pl *pricelistV0) OnVerifyPost(info proof7.WindowPoStVerifyInfo) GasCharge {
|
2020-07-28 13:11:41 +00:00
|
|
|
sectorSize := "unknown"
|
2020-07-28 20:30:59 +00:00
|
|
|
var proofType abi.RegisteredPoStProof
|
|
|
|
|
|
|
|
if len(info.Proofs) != 0 {
|
|
|
|
proofType = info.Proofs[0].PoStProof
|
|
|
|
ss, err := info.Proofs[0].PoStProof.SectorSize()
|
2020-07-28 13:11:41 +00:00
|
|
|
if err == nil {
|
|
|
|
sectorSize = ss.ShortString()
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 20:30:59 +00:00
|
|
|
|
|
|
|
cost, ok := pl.verifyPostLookup[proofType]
|
|
|
|
if !ok {
|
|
|
|
cost = pl.verifyPostLookup[abi.RegisteredPoStProof_StackedDrgWindow512MiBV1]
|
|
|
|
}
|
|
|
|
|
2020-07-31 19:38:23 +00:00
|
|
|
gasUsed := cost.flat + int64(len(info.ChallengedSectors))*cost.scale
|
2020-10-29 19:34:48 +00:00
|
|
|
if pl.verifyPostDiscount {
|
|
|
|
gasUsed /= 2 // XXX: this is an artificial discount
|
|
|
|
}
|
2020-07-31 19:38:23 +00:00
|
|
|
|
2020-11-11 18:07:39 +00:00
|
|
|
return newGasCharge("OnVerifyPost", gasUsed, 0).
|
|
|
|
WithVirtual(117680921+43780*int64(len(info.ChallengedSectors)), 0).
|
2020-07-28 13:11:41 +00:00
|
|
|
WithExtra(map[string]interface{}{
|
|
|
|
"type": sectorSize,
|
|
|
|
"size": len(info.ChallengedSectors),
|
|
|
|
})
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnVerifyConsensusFault
|
2020-06-11 18:37:14 +00:00
|
|
|
func (pl *pricelistV0) OnVerifyConsensusFault() GasCharge {
|
2020-07-10 13:12:27 +00:00
|
|
|
return newGasCharge("OnVerifyConsensusFault", pl.verifyConsensusFault, 0)
|
2020-03-19 16:27:42 +00:00
|
|
|
}
|