2023-02-08 03:12:43 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-10 17:32:59 +00:00
|
|
|
|
2023-02-14 01:02:37 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
|
2023-02-10 17:32:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
2023-02-08 03:12:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEthLogFromEvent(t *testing.T) {
|
|
|
|
// basic empty
|
|
|
|
data, topics, ok := ethLogFromEvent(nil)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Nil(t, data)
|
|
|
|
require.Nil(t, topics)
|
|
|
|
|
|
|
|
// basic topic
|
|
|
|
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t1",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}})
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Nil(t, data)
|
|
|
|
require.Len(t, topics, 1)
|
|
|
|
require.Equal(t, topics[0], ethtypes.EthHash{})
|
|
|
|
|
|
|
|
// basic topic with data
|
|
|
|
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t1",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}, {
|
|
|
|
Flags: 0,
|
|
|
|
Key: "d",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: []byte{0x0},
|
|
|
|
}})
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, data, []byte{0x0})
|
|
|
|
require.Len(t, topics, 1)
|
|
|
|
require.Equal(t, topics[0], ethtypes.EthHash{})
|
|
|
|
|
|
|
|
// skip topic
|
|
|
|
_, _, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t2",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}})
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// duplicate topic
|
|
|
|
_, _, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t1",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}, {
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t1",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}})
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// duplicate data
|
|
|
|
_, _, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "d",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}, {
|
|
|
|
Flags: 0,
|
|
|
|
Key: "d",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}})
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
// unknown key is fine
|
|
|
|
data, topics, ok = ethLogFromEvent([]types.EventEntry{{
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t5",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}, {
|
|
|
|
Flags: 0,
|
|
|
|
Key: "t1",
|
|
|
|
Codec: cid.Raw,
|
|
|
|
Value: make([]byte, 32),
|
|
|
|
}})
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Nil(t, data)
|
|
|
|
require.Len(t, topics, 1)
|
|
|
|
require.Equal(t, topics[0], ethtypes.EthHash{})
|
|
|
|
}
|
2023-02-14 01:02:37 +00:00
|
|
|
|
|
|
|
func TestReward(t *testing.T) {
|
|
|
|
baseFee := big.NewInt(100)
|
|
|
|
testcases := []struct {
|
|
|
|
maxFeePerGas, maxPriorityFeePerGas big.Int
|
|
|
|
answer big.Int
|
|
|
|
}{
|
|
|
|
{maxFeePerGas: big.NewInt(600), maxPriorityFeePerGas: big.NewInt(200), answer: big.NewInt(200)},
|
|
|
|
{maxFeePerGas: big.NewInt(600), maxPriorityFeePerGas: big.NewInt(300), answer: big.NewInt(300)},
|
|
|
|
{maxFeePerGas: big.NewInt(600), maxPriorityFeePerGas: big.NewInt(500), answer: big.NewInt(500)},
|
|
|
|
{maxFeePerGas: big.NewInt(600), maxPriorityFeePerGas: big.NewInt(600), answer: big.NewInt(500)},
|
|
|
|
{maxFeePerGas: big.NewInt(600), maxPriorityFeePerGas: big.NewInt(1000), answer: big.NewInt(500)},
|
|
|
|
{maxFeePerGas: big.NewInt(50), maxPriorityFeePerGas: big.NewInt(200), answer: big.NewInt(-50)},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2023-03-11 17:24:56 +00:00
|
|
|
msg := &types.Message{GasFeeCap: tc.maxFeePerGas, GasPremium: tc.maxPriorityFeePerGas}
|
|
|
|
reward := msg.EffectiveGasPremium(baseFee)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Equal(t, 0, reward.Int.Cmp(tc.answer.Int), reward, tc.answer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRewardPercentiles(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
percentiles []float64
|
|
|
|
txGasRewards gasRewardSorter
|
|
|
|
answer []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
percentiles: []float64{25, 50, 75},
|
|
|
|
txGasRewards: []gasRewardTuple{},
|
2023-03-09 00:28:17 +00:00
|
|
|
answer: []int64{MinGasPremium, MinGasPremium, MinGasPremium},
|
2023-02-14 01:02:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
percentiles: []float64{25, 50, 75, 100},
|
|
|
|
txGasRewards: []gasRewardTuple{
|
2023-03-11 17:24:56 +00:00
|
|
|
{gasUsed: int64(0), premium: big.NewInt(300)},
|
|
|
|
{gasUsed: int64(100), premium: big.NewInt(200)},
|
|
|
|
{gasUsed: int64(350), premium: big.NewInt(100)},
|
|
|
|
{gasUsed: int64(500), premium: big.NewInt(600)},
|
|
|
|
{gasUsed: int64(300), premium: big.NewInt(700)},
|
2023-02-14 01:02:37 +00:00
|
|
|
},
|
|
|
|
answer: []int64{200, 700, 700, 700},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
rewards, totalGasUsed := calculateRewardsAndGasUsed(tc.percentiles, tc.txGasRewards)
|
2023-03-11 17:24:56 +00:00
|
|
|
var gasUsed int64
|
2023-02-14 01:02:37 +00:00
|
|
|
for _, tx := range tc.txGasRewards {
|
2023-03-11 17:24:56 +00:00
|
|
|
gasUsed += tx.gasUsed
|
2023-02-14 01:02:37 +00:00
|
|
|
}
|
|
|
|
ans := []ethtypes.EthBigInt{}
|
|
|
|
for _, bi := range tc.answer {
|
|
|
|
ans = append(ans, ethtypes.EthBigInt(big.NewInt(bi)))
|
|
|
|
}
|
|
|
|
require.Equal(t, totalGasUsed, gasUsed)
|
|
|
|
require.Equal(t, len(ans), len(tc.percentiles))
|
|
|
|
require.Equal(t, ans, rewards)
|
|
|
|
}
|
|
|
|
}
|