Change to scaling overestimation burn algorithm

Change the treshold to 1.1

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-08-05 02:09:39 +02:00
parent 3058f280d9
commit d9ba8d3671
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
3 changed files with 44 additions and 13 deletions

View File

@ -1,18 +1,44 @@
package vm
import (
"math/big"
)
const (
gasOveruseNum = 3
gasOveruseNum = 11
gasOveruseDenom = 10
)
// ComputeGasOutputs computes amount of gas to be refunded and amount of gas to be burned
// Result is (refund, burn)
func ComputeGasOutputs(gasUsed, gasLimit int64) (int64, int64) {
allowedGasOverUsed := gasUsed + (gasUsed*gasOveruseNum)/gasOveruseDenom
gasToBurn := gasLimit - allowedGasOverUsed
if gasToBurn < 0 {
gasToBurn = 0
if gasUsed == 0 {
return 0, gasLimit
}
return gasLimit - gasUsed - gasToBurn, gasToBurn
// over = gasLimit/gasUsed - 1 - 0.3
// over = min(over, 1)
// gasToBurn = (gasLimit - gasUsed) * over
// so to factor out division from `over`
// over*gasUsed = min(gasLimit - (13*gasUsed)/10, gasUsed)
// gasToBurn = ((gasLimit - gasUsed)*over*gasUsed) / gasUsed
over := gasLimit - (gasOveruseNum*gasUsed)/gasOveruseDenom
if over < 0 {
return gasLimit - gasUsed, 0
}
// if we want sharper scaling it goes here:
// over *= 2
if over > gasUsed {
over = gasUsed
}
// needs bigint, as it overflows in pathological case gasLimit > 2^32 gasUsed = gasLimit / 2
gasToBurn := big.NewInt(gasLimit - gasUsed)
gasToBurn = gasToBurn.Mul(gasToBurn, big.NewInt(over))
gasToBurn = gasToBurn.Div(gasToBurn, big.NewInt(gasUsed))
return gasLimit - gasUsed - gasToBurn.Int64(), gasToBurn.Int64()
}

View File

@ -14,21 +14,26 @@ func TestGasBurn(t *testing.T) {
refund int64
burn int64
}{
{100, 200, 30, 70},
{1000, 1300, 300, 0},
{500, 700, 150, 50},
{100, 200, 10, 90},
{100, 150, 30, 20},
{1000, 1300, 240, 60},
{500, 700, 140, 60},
{200, 200, 0, 0},
{20000, 21000, 1000, 0},
{0, 2000, 0, 2000},
{500, 651, 150, 1},
{500, 651, 121, 30},
{500, 5000, 0, 4500},
{7499e6, 7500e6, 1000000, 0},
{7500e6 / 2, 7500e6, 375000000, 3375000000},
{1, 7500e6, 0, 7499999999},
}
for _, test := range tests {
test := test
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
refund, toBurn := ComputeGasOutputs(test.used, test.limit)
assert.Equal(t, test.burn, toBurn)
assert.Equal(t, test.refund, refund)
assert.Equal(t, test.refund, refund, "refund")
assert.Equal(t, test.burn, toBurn, "burned")
})
}
}

2
go.mod
View File

@ -17,7 +17,7 @@ require (
github.com/drand/drand v1.0.3-0.20200714175734-29705eaf09d4
github.com/drand/kyber v1.1.1
github.com/fatih/color v1.8.0
github.com/filecoin-project/chain-validation v0.0.6-0.20200804050537-d19ebfb80735
github.com/filecoin-project/chain-validation v0.0.6-0.20200805000849-cee42fef3885
github.com/filecoin-project/filecoin-ffi v0.30.4-0.20200716204036-cddc56607e1d
github.com/filecoin-project/go-address v0.0.2-0.20200504173055-8b6f2fb2b3ef
github.com/filecoin-project/go-amt-ipld/v2 v2.1.1-0.20200731171407-e559a0579161 // indirect