Make virtual gas real

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-07-10 15:12:27 +02:00
parent c0eb4b39ac
commit 4dd6f6400c
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 65 additions and 72 deletions

View File

@ -84,30 +84,35 @@ type Pricelist interface {
var prices = map[abi.ChainEpoch]Pricelist{ var prices = map[abi.ChainEpoch]Pricelist{
abi.ChainEpoch(0): &pricelistV0{ abi.ChainEpoch(0): &pricelistV0{
onChainMessageBase: 0, onChainMessageComputeBase: 137137,
onChainMessagePerByte: 2, onChainMessageStorageBase: 0, // TODO gas
onChainReturnValuePerByte: 8, onChainMessageStoragePerByte: 2, // TODO gas
sendBase: 5,
sendTransferFunds: 5, onChainReturnValuePerByte: 8, // TODO gas
sendInvokeMethod: 10,
ipldGetBase: 10, sendBase: 97236,
ipldGetPerByte: 1, sendTransferFunds: 96812,
ipldPutBase: 20, sendTransferOnlyPremium: 347806,
ipldPutPerByte: 2, sendInvokeMethod: -3110,
createActorBase: 40, // IPLD put + 20
createActorExtra: 500, ipldGetBase: 417230,
deleteActor: -500, // -createActorExtra ipldPutBase: 396100,
// Dragons: this cost is not persistable, create a LinearCost{a,b} struct that has a `.Cost(x) -> ax + b` ipldPutPerByte: 2, // TODO gas
verifySignature: map[crypto.SigType]func(int64) int64{
crypto.SigTypeBLS: func(x int64) int64 { return 3*x + 2 }, createActorCompute: 750011,
crypto.SigTypeSecp256k1: func(x int64) int64 { return 3*x + 2 }, createActorStorage: 500, // TODO gas
deleteActor: -500, // -createActorStorage
verifySignature: map[crypto.SigType]int64{
crypto.SigTypeBLS: 219946580,
crypto.SigTypeSecp256k1: 6726720,
}, },
hashingBase: 5,
hashingPerByte: 2, hashingBase: 110685,
computeUnsealedSectorCidBase: 100, computeUnsealedSectorCidBase: 431890,
verifySealBase: 2000, verifySealBase: 2000, // TODO gas , it VerifySeal syscall is not used
verifyPostBase: 700, verifyPostBase: 2621447835,
verifyConsensusFault: 10, verifyConsensusFault: 495422,
}, },
} }
@ -198,14 +203,14 @@ func (ps pricedSyscalls) VerifyConsensusFault(h1 []byte, h2 []byte, extra []byte
} }
func (ps pricedSyscalls) BatchVerifySeals(inp map[address.Address][]abi.SealVerifyInfo) (map[address.Address][]bool, error) { func (ps pricedSyscalls) BatchVerifySeals(inp map[address.Address][]abi.SealVerifyInfo) (map[address.Address][]bool, error) {
var gasChargeSum GasCharge
gasChargeSum.Name = "BatchVerifySeals"
count := int64(0) count := int64(0)
for _, svis := range inp { for _, svis := range inp {
count += int64(len(svis)) count += int64(len(svis))
} }
gasChargeSum = gasChargeSum.WithExtra(count).WithVirtual(129778623*count+716683250, 0)
ps.chargeGas(gasChargeSum) // TODO: this is only called by the cron actor. Should we even charge gas? gasChargeSum := newGasCharge("BatchVerifySeals", 0, 0)
gasChargeSum = gasChargeSum.WithExtra(count).WithVirtual(15075005*count+899741502, 0)
ps.chargeGas(gasChargeSum) // real gas charged by actors
defer ps.chargeGas(gasOnActorExec) defer ps.chargeGas(gasOnActorExec)
return ps.under.BatchVerifySeals(inp) return ps.under.BatchVerifySeals(inp)

View File

@ -20,8 +20,9 @@ type pricelistV0 struct {
// Together, these account for the cost of message propagation and validation, // Together, these account for the cost of message propagation and validation,
// up to but excluding any actual processing by the VM. // up to but excluding any actual processing by the VM.
// This is the cost a block producer burns when including an invalid message. // This is the cost a block producer burns when including an invalid message.
onChainMessageBase int64 onChainMessageComputeBase int64
onChainMessagePerByte int64 onChainMessageStorageBase int64
onChainMessageStoragePerByte int64
// Gas cost charged to the originator of a non-nil return value produced // Gas cost charged to the originator of a non-nil return value produced
// by an on-chain message is given by: // by an on-chain message is given by:
@ -41,15 +42,17 @@ type pricelistV0 struct {
// already accounted for). // already accounted for).
sendTransferFunds int64 sendTransferFunds int64
// Gsa cost charged, in addition to SendBase, if message only transfers funds.
sendTransferOnlyPremium int64
// Gas cost charged, in addition to SendBase, if a message invokes // Gas cost charged, in addition to SendBase, if a message invokes
// a method on the receiver. // a method on the receiver.
// Accounts for the cost of loading receiver code and method dispatch. // Accounts for the cost of loading receiver code and method dispatch.
sendInvokeMethod int64 sendInvokeMethod int64
// Gas cost (Base + len*PerByte) for any Get operation to the IPLD store // Gas cost for any Get operation to the IPLD store
// in the runtime VM context. // in the runtime VM context.
ipldGetBase int64 ipldGetBase int64
ipldGetPerByte int64
// Gas cost (Base + len*PerByte) for any Put operation to the IPLD store // Gas cost (Base + len*PerByte) for any Put operation to the IPLD store
// in the runtime VM context. // in the runtime VM context.
@ -64,18 +67,17 @@ type pricelistV0 struct {
// //
// Note: this costs assume that the extra will be partially or totally refunded while // Note: this costs assume that the extra will be partially or totally refunded while
// the base is covering for the put. // the base is covering for the put.
createActorBase int64 createActorCompute int64
createActorExtra int64 createActorStorage int64
// Gas cost for deleting an actor. // Gas cost for deleting an actor.
// //
// Note: this partially refunds the create cost to incentivise the deletion of the actors. // Note: this partially refunds the create cost to incentivise the deletion of the actors.
deleteActor int64 deleteActor int64
verifySignature map[crypto.SigType]func(len int64) int64 verifySignature map[crypto.SigType]int64
hashingBase int64 hashingBase int64
hashingPerByte int64
computeUnsealedSectorCidBase int64 computeUnsealedSectorCidBase int64
verifySealBase int64 verifySealBase int64
@ -87,57 +89,51 @@ var _ Pricelist = (*pricelistV0)(nil)
// OnChainMessage returns the gas used for storing a message of a given size in the chain. // OnChainMessage returns the gas used for storing a message of a given size in the chain.
func (pl *pricelistV0) OnChainMessage(msgSize int) GasCharge { func (pl *pricelistV0) OnChainMessage(msgSize int) GasCharge {
return newGasCharge("OnChainMessage", 0, pl.onChainMessageBase+pl.onChainMessagePerByte*int64(msgSize)).WithVirtual(77302, 0) return newGasCharge("OnChainMessage", pl.onChainMessageComputeBase,
pl.onChainMessageStorageBase+pl.onChainMessageStoragePerByte*int64(msgSize))
} }
// OnChainReturnValue returns the gas used for storing the response of a message in the chain. // OnChainReturnValue returns the gas used for storing the response of a message in the chain.
func (pl *pricelistV0) OnChainReturnValue(dataSize int) GasCharge { func (pl *pricelistV0) OnChainReturnValue(dataSize int) GasCharge {
return newGasCharge("OnChainReturnValue", 0, int64(dataSize)*pl.onChainReturnValuePerByte).WithVirtual(107294, 0) return newGasCharge("OnChainReturnValue", 0, int64(dataSize)*pl.onChainReturnValuePerByte)
} }
// OnMethodInvocation returns the gas used when invoking a method. // OnMethodInvocation returns the gas used when invoking a method.
func (pl *pricelistV0) OnMethodInvocation(value abi.TokenAmount, methodNum abi.MethodNum) GasCharge { func (pl *pricelistV0) OnMethodInvocation(value abi.TokenAmount, methodNum abi.MethodNum) GasCharge {
ret := pl.sendBase ret := pl.sendBase
extra := "" extra := ""
virtGas := int64(1072944)
if value != abi.NewTokenAmount(0) {
// TODO: fix this, it is comparing pointers instead of values
// see vv
ret += pl.sendTransferFunds
}
if big.Cmp(value, abi.NewTokenAmount(0)) != 0 { if big.Cmp(value, abi.NewTokenAmount(0)) != 0 {
virtGas += 497495 ret += pl.sendTransferFunds
if methodNum == builtin.MethodSend { if methodNum == builtin.MethodSend {
// transfer only // transfer only
virtGas += 973940 ret += pl.sendTransferOnlyPremium
} }
extra += "t" extra += "t"
} }
if methodNum != builtin.MethodSend { if methodNum != builtin.MethodSend {
ret += pl.sendInvokeMethod
extra += "i" extra += "i"
// running actors is cheaper becase we hand over to actors // running actors is cheaper becase we hand over to actors
virtGas += -295779 ret += pl.sendInvokeMethod
} }
return newGasCharge("OnMethodInvocation", ret, 0).WithVirtual(virtGas, 0).WithExtra(extra) return newGasCharge("OnMethodInvocation", ret, 0).WithExtra(extra)
} }
// OnIpldGet returns the gas used for storing an object // OnIpldGet returns the gas used for storing an object
func (pl *pricelistV0) OnIpldGet(dataSize int) GasCharge { func (pl *pricelistV0) OnIpldGet(dataSize int) GasCharge {
return newGasCharge("OnIpldGet", pl.ipldGetBase+int64(dataSize)*pl.ipldGetPerByte, 0). return newGasCharge("OnIpldGet", pl.ipldGetBase, 0)
WithExtra(dataSize).WithVirtual(433685, 0)
} }
// OnIpldPut returns the gas used for storing an object // OnIpldPut returns the gas used for storing an object
func (pl *pricelistV0) OnIpldPut(dataSize int) GasCharge { func (pl *pricelistV0) OnIpldPut(dataSize int) GasCharge {
return newGasCharge("OnIpldPut", pl.ipldPutBase, int64(dataSize)*pl.ipldPutPerByte). return newGasCharge("OnIpldPut", pl.ipldPutBase, int64(dataSize)*pl.ipldPutPerByte).
WithExtra(dataSize).WithVirtual(88970, 0) WithExtra(dataSize)
} }
// OnCreateActor returns the gas used for creating an actor // OnCreateActor returns the gas used for creating an actor
func (pl *pricelistV0) OnCreateActor() GasCharge { func (pl *pricelistV0) OnCreateActor() GasCharge {
return newGasCharge("OnCreateActor", pl.createActorBase, pl.createActorExtra).WithVirtual(65636, 0) return newGasCharge("OnCreateActor", pl.createActorCompute, pl.createActorStorage)
} }
// OnDeleteActor returns the gas used for deleting an actor // OnDeleteActor returns the gas used for deleting an actor
@ -148,50 +144,42 @@ func (pl *pricelistV0) OnDeleteActor() GasCharge {
// OnVerifySignature // OnVerifySignature
func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) (GasCharge, error) { func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) (GasCharge, error) {
costFn, ok := pl.verifySignature[sigType] cost, ok := pl.verifySignature[sigType]
if !ok { if !ok {
return GasCharge{}, fmt.Errorf("cost function for signature type %d not supported", sigType) return GasCharge{}, fmt.Errorf("cost function for signature type %d not supported", sigType)
} }
sigName, _ := sigType.Name()
virtGas := int64(0)
switch sigType {
case crypto.SigTypeBLS:
virtGas = 220138570
case crypto.SigTypeSecp256k1:
virtGas = 7053730
}
return newGasCharge("OnVerifySignature", costFn(int64(planTextSize)), 0). sigName, _ := sigType.Name()
return newGasCharge("OnVerifySignature", cost, 0).
WithExtra(map[string]interface{}{ WithExtra(map[string]interface{}{
"type": sigName, "type": sigName,
"size": planTextSize, "size": planTextSize,
}).WithVirtual(virtGas, 0), nil }), nil
} }
// OnHashing // OnHashing
func (pl *pricelistV0) OnHashing(dataSize int) GasCharge { func (pl *pricelistV0) OnHashing(dataSize int) GasCharge {
return newGasCharge("OnHashing", pl.hashingBase+int64(dataSize)*pl.hashingPerByte, 0).WithExtra(dataSize).WithVirtual(77300, 0) return newGasCharge("OnHashing", pl.hashingBase, 0).WithExtra(dataSize)
} }
// OnComputeUnsealedSectorCid // OnComputeUnsealedSectorCid
func (pl *pricelistV0) OnComputeUnsealedSectorCid(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) GasCharge { func (pl *pricelistV0) OnComputeUnsealedSectorCid(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) GasCharge {
// TODO: this needs more cost tunning, check with @lotus return newGasCharge("OnComputeUnsealedSectorCid", pl.computeUnsealedSectorCidBase, 0)
return newGasCharge("OnComputeUnsealedSectorCid", pl.computeUnsealedSectorCidBase, 0).WithVirtual(382370, 0)
} }
// OnVerifySeal // OnVerifySeal
func (pl *pricelistV0) OnVerifySeal(info abi.SealVerifyInfo) GasCharge { func (pl *pricelistV0) OnVerifySeal(info abi.SealVerifyInfo) GasCharge {
// TODO: this needs more cost tunning, check with @lotus // TODO: this needs more cost tunning, check with @lotus
return newGasCharge("OnVerifySeal", pl.verifySealBase, 0).WithVirtual(199954003, 0) // this is not used
return newGasCharge("OnVerifySeal", pl.verifySealBase, 0)
} }
// OnVerifyPost // OnVerifyPost
func (pl *pricelistV0) OnVerifyPost(info abi.WindowPoStVerifyInfo) GasCharge { func (pl *pricelistV0) OnVerifyPost(info abi.WindowPoStVerifyInfo) GasCharge {
// TODO: this needs more cost tunning, check with @lotus return newGasCharge("OnVerifyPost", pl.verifyPostBase, 0).WithExtra(len(info.ChallengedSectors))
return newGasCharge("OnVerifyPost", pl.verifyPostBase, 0).WithVirtual(2629471704, 0).WithExtra(len(info.ChallengedSectors))
} }
// OnVerifyConsensusFault // OnVerifyConsensusFault
func (pl *pricelistV0) OnVerifyConsensusFault() GasCharge { func (pl *pricelistV0) OnVerifyConsensusFault() GasCharge {
return newGasCharge("OnVerifyConsensusFault", pl.verifyConsensusFault, 0).WithVirtual(551935, 0) return newGasCharge("OnVerifyConsensusFault", pl.verifyConsensusFault, 0)
} }