2020-08-01 22:09:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-06 23:56:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-01 22:09:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGasBurn(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
used int64
|
|
|
|
limit int64
|
|
|
|
refund int64
|
|
|
|
burn int64
|
|
|
|
}{
|
2020-08-05 00:09:39 +00:00
|
|
|
{100, 200, 10, 90},
|
|
|
|
{100, 150, 30, 20},
|
|
|
|
{1000, 1300, 240, 60},
|
|
|
|
{500, 700, 140, 60},
|
2020-08-01 22:09:06 +00:00
|
|
|
{200, 200, 0, 0},
|
|
|
|
{20000, 21000, 1000, 0},
|
|
|
|
{0, 2000, 0, 2000},
|
2020-08-05 00:09:39 +00:00
|
|
|
{500, 651, 121, 30},
|
|
|
|
{500, 5000, 0, 4500},
|
|
|
|
{7499e6, 7500e6, 1000000, 0},
|
|
|
|
{7500e6 / 2, 7500e6, 375000000, 3375000000},
|
|
|
|
{1, 7500e6, 0, 7499999999},
|
2020-08-01 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
|
2020-08-06 19:05:16 +00:00
|
|
|
refund, toBurn := ComputeGasOverestimationBurn(test.used, test.limit)
|
2020-08-05 00:09:39 +00:00
|
|
|
assert.Equal(t, test.refund, refund, "refund")
|
|
|
|
assert.Equal(t, test.burn, toBurn, "burned")
|
2020-08-01 22:09:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 23:56:53 +00:00
|
|
|
|
|
|
|
func TestGasOutputs(t *testing.T) {
|
|
|
|
baseFee := types.NewInt(10)
|
|
|
|
tests := []struct {
|
|
|
|
used int64
|
|
|
|
limit int64
|
|
|
|
|
|
|
|
feeCap uint64
|
|
|
|
premium uint64
|
|
|
|
|
|
|
|
BaseFeeBurn uint64
|
|
|
|
OverEstimationBurn uint64
|
|
|
|
MinerPenalty uint64
|
|
|
|
MinerTip uint64
|
|
|
|
Refund uint64
|
|
|
|
}{
|
|
|
|
{100, 110, 11, 1, 1000, 0, 0, 110, 100},
|
|
|
|
{100, 130, 11, 1, 1000, 60, 0, 130, 240},
|
|
|
|
{100, 110, 10, 1, 1000, 0, 0, 0, 100},
|
|
|
|
{100, 110, 6, 1, 600, 0, 400, 0, 60},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
|
|
|
|
output := ComputeGasOutputs(test.used, test.limit, baseFee, types.NewInt(test.feeCap), types.NewInt(test.premium))
|
|
|
|
i2s := func(i uint64) string {
|
|
|
|
return fmt.Sprintf("%d", i)
|
|
|
|
}
|
|
|
|
assert.Equal(t, i2s(test.BaseFeeBurn), output.BaseFeeBurn.String(), "BaseFeeBurn")
|
|
|
|
assert.Equal(t, i2s(test.OverEstimationBurn), output.OverEstimationBurn.String(), "OverEstimationBurn")
|
|
|
|
assert.Equal(t, i2s(test.MinerPenalty), output.MinerPenalty.String(), "MinerPenalty")
|
|
|
|
assert.Equal(t, i2s(test.MinerTip), output.MinerTip.String(), "MinerTip")
|
|
|
|
assert.Equal(t, i2s(test.Refund), output.Refund.String(), "Refund")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|