Skip to content

Commit 8156c73

Browse files
authored
Merge pull request #935 from multiversx/update-gas-for-hooks
update gas for hooks
2 parents 0657ccf + 8a9e3b7 commit 8156c73

File tree

12 files changed

+337
-53
lines changed

12 files changed

+337
-53
lines changed

config/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
BigFloatAbs = 10
154154
BigFloatSqrt = 10
155155
BigFloatPow = 10
156+
BigFloatPowPerIteration = 10
156157
BigFloatFloor = 10
157158
BigFloatCeil = 10
158159
BigFloatIsInt = 10

config/gasCost.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,25 @@ type BigIntAPICost struct {
157157

158158
// BigFloatAPICost defines the big float operations gas cost config structure
159159
type BigFloatAPICost struct {
160-
BigFloatNewFromParts uint64
161-
BigFloatAdd uint64
162-
BigFloatSub uint64
163-
BigFloatMul uint64
164-
BigFloatDiv uint64
165-
BigFloatTruncate uint64
166-
BigFloatNeg uint64
167-
BigFloatClone uint64
168-
BigFloatCmp uint64
169-
BigFloatAbs uint64
170-
BigFloatSqrt uint64
171-
BigFloatPow uint64
172-
BigFloatFloor uint64
173-
BigFloatCeil uint64
174-
BigFloatIsInt uint64
175-
BigFloatSetBigInt uint64
176-
BigFloatSetInt64 uint64
177-
BigFloatGetConst uint64
160+
BigFloatNewFromParts uint64
161+
BigFloatAdd uint64
162+
BigFloatSub uint64
163+
BigFloatMul uint64
164+
BigFloatDiv uint64
165+
BigFloatTruncate uint64
166+
BigFloatNeg uint64
167+
BigFloatClone uint64
168+
BigFloatCmp uint64
169+
BigFloatAbs uint64
170+
BigFloatSqrt uint64
171+
BigFloatPow uint64
172+
BigFloatPowPerIteration uint64
173+
BigFloatFloor uint64
174+
BigFloatCeil uint64
175+
BigFloatIsInt uint64
176+
BigFloatSetBigInt uint64
177+
BigFloatSetInt64 uint64
178+
BigFloatGetConst uint64
178179
}
179180

180181
// CryptoAPICost defines the crypto operations gas cost config structure

config/gasSchedule.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ func FillGasMapBigFloatAPICosts(value uint64) map[string]uint64 {
431431
gasMap["BigFloatAbs"] = value
432432
gasMap["BigFloatSqrt"] = value
433433
gasMap["BigFloatPow"] = value
434+
gasMap["BigFloatPowPerIteration"] = value
434435
gasMap["BigFloatFloor"] = value
435436
gasMap["BigFloatCeil"] = value
436437
gasMap["BigFloatIsInt"] = value

integrationTests/json/scenariosAdderLog_test.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,26 @@ VM hook begin: MBufferToBigIntUnsigned(-202, -203)
9292
GetPointsUsed: 21611
9393
GetPointsUsed: 21611
9494
SetPointsUsed: 25611
95+
GetPointsUsed: 25611
96+
GetPointsUsed: 25611
97+
SetPointsUsed: 26611
9598
VM hook end: MBufferToBigIntUnsigned(-202, -203)
9699
VM hook begin: BigIntFinishUnsigned(-203)
97-
GetPointsUsed: 25622
98-
GetPointsUsed: 25622
99-
SetPointsUsed: 26622
100100
GetPointsUsed: 26622
101101
GetPointsUsed: 26622
102-
SetPointsUsed: 36622
102+
SetPointsUsed: 27622
103+
GetPointsUsed: 27622
104+
GetPointsUsed: 27622
105+
SetPointsUsed: 37622
103106
VM hook end: BigIntFinishUnsigned(-203)
104-
GetPointsUsed: 36624
105-
GetPointsUsed: 36624
106-
GetPointsUsed: 36624
107-
GetPointsUsed: 36624
108-
GetPointsUsed: 36624
109-
GetPointsUsed: 36624
110-
GetPointsUsed: 36624
111-
GetPointsUsed: 36624
107+
GetPointsUsed: 37624
108+
GetPointsUsed: 37624
109+
GetPointsUsed: 37624
110+
GetPointsUsed: 37624
111+
GetPointsUsed: 37624
112+
GetPointsUsed: 37624
113+
GetPointsUsed: 37624
114+
GetPointsUsed: 37624
112115
Reset: true
113116
SetPointsUsed: 0
114117
SetGasLimit: 3791200
@@ -150,36 +153,39 @@ VM hook begin: MBufferToBigIntUnsigned(-203, -204)
150153
GetPointsUsed: 22653
151154
GetPointsUsed: 22653
152155
SetPointsUsed: 26653
156+
GetPointsUsed: 26653
157+
GetPointsUsed: 26653
158+
SetPointsUsed: 27653
153159
VM hook end: MBufferToBigIntUnsigned(-203, -204)
154160
VM hook begin: BigIntAdd(-204, -204, -201)
155-
GetPointsUsed: 26673
156-
GetPointsUsed: 26673
157-
SetPointsUsed: 28673
161+
GetPointsUsed: 27673
162+
GetPointsUsed: 27673
163+
SetPointsUsed: 29673
158164
VM hook end: BigIntAdd(-204, -204, -201)
159165
VM hook begin: MBufferFromBigIntUnsigned(-205, -204)
160-
GetPointsUsed: 28712
161-
GetPointsUsed: 28712
162-
SetPointsUsed: 32712
166+
GetPointsUsed: 29712
167+
GetPointsUsed: 29712
168+
SetPointsUsed: 33712
163169
VM hook end: MBufferFromBigIntUnsigned(-205, -204)
164170
VM hook begin: MBufferStorageStore(-202, -205)
165-
GetPointsUsed: 32724
166-
GetPointsUsed: 32724
167-
SetPointsUsed: 107724
168-
GetPointsUsed: 107724
169-
GetPointsUsed: 107724
170-
SetPointsUsed: 107724
171-
GetPointsUsed: 107724
172-
GetPointsUsed: 107724
173-
SetPointsUsed: 107724
171+
GetPointsUsed: 33724
172+
GetPointsUsed: 33724
173+
SetPointsUsed: 108724
174+
GetPointsUsed: 108724
175+
GetPointsUsed: 108724
176+
SetPointsUsed: 108724
177+
GetPointsUsed: 108724
178+
GetPointsUsed: 108724
179+
SetPointsUsed: 108724
174180
VM hook end: MBufferStorageStore(-202, -205)
175-
GetPointsUsed: 107731
176-
GetPointsUsed: 107731
177-
GetPointsUsed: 107731
178-
GetPointsUsed: 107731
179-
GetPointsUsed: 107731
180-
GetPointsUsed: 107731
181-
GetPointsUsed: 107731
182-
GetPointsUsed: 107731
181+
GetPointsUsed: 108731
182+
GetPointsUsed: 108731
183+
GetPointsUsed: 108731
184+
GetPointsUsed: 108731
185+
GetPointsUsed: 108731
186+
GetPointsUsed: 108731
187+
GetPointsUsed: 108731
188+
GetPointsUsed: 108731
183189
Clean: true
184190
`
185191

scenario/gasSchedules/gasScheduleEmbedGenerated.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scenario/gasSchedules/gasScheduleV3.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
BigFloatAbs = 5000
210210
BigFloatSqrt = 7000
211211
BigFloatPow = 10000
212+
BigFloatPowPerIteration = 10000
212213
BigFloatFloor = 5000
213214
BigFloatCeil = 5000
214215
BigFloatIsInt = 3000

scenario/gasSchedules/gasScheduleV4.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
BigFloatAbs = 5000
212212
BigFloatSqrt = 7000
213213
BigFloatPow = 10000
214+
BigFloatPowPerIteration = 10000
214215
BigFloatFloor = 5000
215216
BigFloatCeil = 5000
216217
BigFloatIsInt = 3000

vmhost/vmhooks/bigFloatOps.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,13 @@ func (context *VMHooksImpl) BigFloatPow(destinationHandle, opHandle, exponent in
481481
managedType := context.GetManagedTypesContext()
482482
metering := context.GetMeteringContext()
483483
metering.StartGasTracing(bigFloatPowName)
484+
enableEpochsHandler := context.GetEnableEpochsHandler()
484485

485486
gasToUse := metering.GasSchedule().BigFloatAPICost.BigFloatPow
487+
if enableEpochsHandler.IsFlagEnabled(vmhost.BarnardOpcodesFlag) {
488+
gasToUse += vmMath.MulUint64(metering.GasSchedule().BigFloatAPICost.BigFloatPowPerIteration, uint64(exponent))
489+
}
490+
486491
err := metering.UseGasBounded(gasToUse)
487492
if err != nil {
488493
context.FailExecution(err)

vmhost/vmhooks/bigIntOps.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (context *VMHooksImpl) BigIntGetSignedArgument(id int32, destinationHandle
8484
managedType := context.GetManagedTypesContext()
8585
runtime := context.GetRuntimeContext()
8686
metering := context.GetMeteringContext()
87+
enableEpochsHandler := context.GetEnableEpochsHandler()
8788

8889
gasToUse := metering.GasSchedule().BigIntAPICost.BigIntGetSignedArgument
8990
err := metering.UseGasBoundedAndAddTracedGas(bigIntGetSignedArgumentName, gasToUse)
@@ -97,6 +98,15 @@ func (context *VMHooksImpl) BigIntGetSignedArgument(id int32, destinationHandle
9798
return
9899
}
99100

101+
if enableEpochsHandler.IsFlagEnabled(vmhost.BarnardOpcodesFlag) {
102+
gasToUse = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(len(args[id])))
103+
err = metering.UseGasBounded(gasToUse)
104+
if err != nil {
105+
context.FailExecution(err)
106+
return
107+
}
108+
}
109+
100110
value := managedType.GetBigIntOrCreate(destinationHandle)
101111

102112
twos.SetBytes(value, args[id])

vmhost/vmhooks/manBufOps.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ func (context *VMHooksImpl) MBufferAppendBytes(accumulatorHandle int32, dataOffs
450450
func (context *VMHooksImpl) MBufferToBigIntUnsigned(mBufferHandle int32, bigIntHandle int32) int32 {
451451
managedType := context.GetManagedTypesContext()
452452
metering := context.GetMeteringContext()
453+
enableEpochsHandler := context.GetEnableEpochsHandler()
453454

454455
gasToUse := metering.GasSchedule().ManagedBufferAPICost.MBufferToBigIntUnsigned
455456
err := metering.UseGasBoundedAndAddTracedGas(mBufferToBigIntUnsignedName, gasToUse)
@@ -464,6 +465,15 @@ func (context *VMHooksImpl) MBufferToBigIntUnsigned(mBufferHandle int32, bigIntH
464465
return 1
465466
}
466467

468+
if enableEpochsHandler.IsFlagEnabled(vmhost.BarnardOpcodesFlag) {
469+
gasToUse = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(len(managedBuffer)))
470+
err = metering.UseGasBounded(gasToUse)
471+
if err != nil {
472+
context.FailExecution(err)
473+
return 1
474+
}
475+
}
476+
467477
bigInt := managedType.GetBigIntOrCreate(bigIntHandle)
468478
bigInt.SetBytes(managedBuffer)
469479

@@ -475,6 +485,7 @@ func (context *VMHooksImpl) MBufferToBigIntUnsigned(mBufferHandle int32, bigIntH
475485
func (context *VMHooksImpl) MBufferToBigIntSigned(mBufferHandle int32, bigIntHandle int32) int32 {
476486
managedType := context.GetManagedTypesContext()
477487
metering := context.GetMeteringContext()
488+
enableEpochsHandler := context.GetEnableEpochsHandler()
478489

479490
gasToUse := metering.GasSchedule().ManagedBufferAPICost.MBufferToBigIntSigned
480491
err := metering.UseGasBoundedAndAddTracedGas(mBufferToBigIntSignedName, gasToUse)
@@ -489,6 +500,15 @@ func (context *VMHooksImpl) MBufferToBigIntSigned(mBufferHandle int32, bigIntHan
489500
return 1
490501
}
491502

503+
if enableEpochsHandler.IsFlagEnabled(vmhost.BarnardOpcodesFlag) {
504+
gasToUse = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(len(managedBuffer)))
505+
err = metering.UseGasBounded(gasToUse)
506+
if err != nil {
507+
context.FailExecution(err)
508+
return 1
509+
}
510+
}
511+
492512
bigInt := managedType.GetBigIntOrCreate(bigIntHandle)
493513
twos.SetBytes(bigInt, managedBuffer)
494514

@@ -560,6 +580,14 @@ func (context *VMHooksImpl) MBufferToSmallIntUnsigned(mBufferHandle int32) int64
560580
context.FailExecution(err)
561581
return 1
562582
}
583+
584+
gasToUse = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(len(data)))
585+
err = metering.UseGasBounded(gasToUse)
586+
if err != nil {
587+
context.FailExecution(err)
588+
return 1
589+
}
590+
563591
bigInt := big.NewInt(0).SetBytes(data)
564592
if !bigInt.IsUint64() {
565593
context.FailExecution(vmhost.ErrBytesExceedUint64)
@@ -586,6 +614,14 @@ func (context *VMHooksImpl) MBufferToSmallIntSigned(mBufferHandle int32) int64 {
586614
context.FailExecution(err)
587615
return 1
588616
}
617+
618+
gasToUse = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(len(data)))
619+
err = metering.UseGasBounded(gasToUse)
620+
if err != nil {
621+
context.FailExecution(err)
622+
return 1
623+
}
624+
589625
bigInt := twos.SetBytes(big.NewInt(0), data)
590626
if !bigInt.IsInt64() {
591627
context.FailExecution(vmhost.ErrBytesExceedInt64)

0 commit comments

Comments
 (0)