Skip to content

Commit

Permalink
Make virtual gas real
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Jakub Sztandera committed Jul 10, 2020
1 parent 9266611 commit 23f4534
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 72 deletions.
59 changes: 32 additions & 27 deletions chain/vm/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,35 @@ type Pricelist interface {

var prices = map[abi.ChainEpoch]Pricelist{
abi.ChainEpoch(0): &pricelistV0{
onChainMessageBase: 0,
onChainMessagePerByte: 2,
onChainReturnValuePerByte: 8,
sendBase: 5,
sendTransferFunds: 5,
sendInvokeMethod: 10,
ipldGetBase: 10,
ipldGetPerByte: 1,
ipldPutBase: 20,
ipldPutPerByte: 2,
createActorBase: 40, // IPLD put + 20
createActorExtra: 500,
deleteActor: -500, // -createActorExtra
// Dragons: this cost is not persistable, create a LinearCost{a,b} struct that has a `.Cost(x) -> ax + b`
verifySignature: map[crypto.SigType]func(int64) int64{
crypto.SigTypeBLS: func(x int64) int64 { return 3*x + 2 },
crypto.SigTypeSecp256k1: func(x int64) int64 { return 3*x + 2 },
onChainMessageComputeBase: 137137,
onChainMessageStorageBase: 0, // TODO gas
onChainMessageStoragePerByte: 2, // TODO gas

onChainReturnValuePerByte: 8, // TODO gas

sendBase: 97236,
sendTransferFunds: 96812,
sendTransferOnlyPremium: 347806,
sendInvokeMethod: -3110,

ipldGetBase: 417230,
ipldPutBase: 396100,
ipldPutPerByte: 2, // TODO gas

createActorCompute: 750011,
createActorStorage: 500, // TODO gas
deleteActor: -500, // -createActorStorage

verifySignature: map[crypto.SigType]int64{
crypto.SigTypeBLS: 219946580,
crypto.SigTypeSecp256k1: 6726720,
},
hashingBase: 5,
hashingPerByte: 2,
computeUnsealedSectorCidBase: 100,
verifySealBase: 2000,
verifyPostBase: 700,
verifyConsensusFault: 10,

hashingBase: 110685,
computeUnsealedSectorCidBase: 431890,
verifySealBase: 2000, // TODO gas , it VerifySeal syscall is not used
verifyPostBase: 2621447835,
verifyConsensusFault: 495422,
},
}

Expand Down Expand Up @@ -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) {
var gasChargeSum GasCharge
gasChargeSum.Name = "BatchVerifySeals"
count := int64(0)
for _, svis := range inp {
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)

return ps.under.BatchVerifySeals(inp)
Expand Down
78 changes: 33 additions & 45 deletions chain/vm/gas_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type pricelistV0 struct {
// Together, these account for the cost of message propagation and validation,
// up to but excluding any actual processing by the VM.
// This is the cost a block producer burns when including an invalid message.
onChainMessageBase int64
onChainMessagePerByte int64
onChainMessageComputeBase int64
onChainMessageStorageBase int64
onChainMessageStoragePerByte int64

// Gas cost charged to the originator of a non-nil return value produced
// by an on-chain message is given by:
Expand All @@ -41,15 +42,17 @@ type pricelistV0 struct {
// already accounted for).
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
// a method on the receiver.
// Accounts for the cost of loading receiver code and method dispatch.
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.
ipldGetBase int64
ipldGetPerByte int64
ipldGetBase int64

// Gas cost (Base + len*PerByte) for any Put operation to the IPLD store
// in the runtime VM context.
Expand All @@ -64,18 +67,17 @@ type pricelistV0 struct {
//
// Note: this costs assume that the extra will be partially or totally refunded while
// the base is covering for the put.
createActorBase int64
createActorExtra int64
createActorCompute int64
createActorStorage int64

// Gas cost for deleting an actor.
//
// Note: this partially refunds the create cost to incentivise the deletion of the actors.
deleteActor int64

verifySignature map[crypto.SigType]func(len int64) int64
verifySignature map[crypto.SigType]int64

hashingBase int64
hashingPerByte int64
hashingBase int64

computeUnsealedSectorCidBase int64
verifySealBase int64
Expand All @@ -87,57 +89,51 @@ var _ Pricelist = (*pricelistV0)(nil)

// OnChainMessage returns the gas used for storing a message of a given size in the chain.
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.
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.
func (pl *pricelistV0) OnMethodInvocation(value abi.TokenAmount, methodNum abi.MethodNum) GasCharge {
ret := pl.sendBase
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 {
virtGas += 497495
ret += pl.sendTransferFunds
if methodNum == builtin.MethodSend {
// transfer only
virtGas += 973940
ret += pl.sendTransferOnlyPremium
}
extra += "t"
}

if methodNum != builtin.MethodSend {
ret += pl.sendInvokeMethod
extra += "i"
// 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
func (pl *pricelistV0) OnIpldGet(dataSize int) GasCharge {
return newGasCharge("OnIpldGet", pl.ipldGetBase+int64(dataSize)*pl.ipldGetPerByte, 0).
WithExtra(dataSize).WithVirtual(433685, 0)
return newGasCharge("OnIpldGet", pl.ipldGetBase, 0)
}

// OnIpldPut returns the gas used for storing an object
func (pl *pricelistV0) OnIpldPut(dataSize int) GasCharge {
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
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
Expand All @@ -148,50 +144,42 @@ func (pl *pricelistV0) OnDeleteActor() GasCharge {
// OnVerifySignature

func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) (GasCharge, error) {
costFn, ok := pl.verifySignature[sigType]
cost, ok := pl.verifySignature[sigType]
if !ok {
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{}{
"type": sigName,
"size": planTextSize,
}).WithVirtual(virtGas, 0), nil
}), nil
}

// OnHashing
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
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).WithVirtual(382370, 0)
return newGasCharge("OnComputeUnsealedSectorCid", pl.computeUnsealedSectorCidBase, 0)
}

// OnVerifySeal
func (pl *pricelistV0) OnVerifySeal(info abi.SealVerifyInfo) GasCharge {
// 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
func (pl *pricelistV0) OnVerifyPost(info abi.WindowPoStVerifyInfo) GasCharge {
// TODO: this needs more cost tunning, check with @lotus
return newGasCharge("OnVerifyPost", pl.verifyPostBase, 0).WithVirtual(2629471704, 0).WithExtra(len(info.ChallengedSectors))
return newGasCharge("OnVerifyPost", pl.verifyPostBase, 0).WithExtra(len(info.ChallengedSectors))
}

// OnVerifyConsensusFault
func (pl *pricelistV0) OnVerifyConsensusFault() GasCharge {
return newGasCharge("OnVerifyConsensusFault", pl.verifyConsensusFault, 0).WithVirtual(551935, 0)
return newGasCharge("OnVerifyConsensusFault", pl.verifyConsensusFault, 0)
}

0 comments on commit 23f4534

Please sign in to comment.