2023-02-10 18:33:59 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-03-09 00:28:17 +00:00
|
|
|
"sort"
|
2023-02-10 18:33:59 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2023-03-09 00:28:17 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2023-02-10 18:33:59 +00:00
|
|
|
|
2023-03-09 00:28:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2023-02-10 18:33:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
|
|
|
"github.com/filecoin-project/lotus/lib/result"
|
2023-03-09 00:28:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
2023-02-10 18:33:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-09 00:28:17 +00:00
|
|
|
// calculateExpectations calculates the expected number of items to be included in the response
|
|
|
|
// of eth_feeHistory. It takes care of null rounds by finding the closet tipset with height
|
|
|
|
// smaller than startHeight, and then looks back at requestAmount of items. It also considers
|
|
|
|
// scenarios where there are not enough items to look back.
|
|
|
|
func calculateExpectations(tsHeights []int, requestAmount, startHeight int) (count, oldestHeight int) {
|
|
|
|
latestIdx := sort.SearchInts(tsHeights, startHeight)
|
|
|
|
// SearchInts returns the index of the number that's larger than the target if the target
|
|
|
|
// doesn't exist. However, we're looking for the closet number that's smaller that the target
|
|
|
|
for tsHeights[latestIdx] > startHeight {
|
|
|
|
latestIdx--
|
|
|
|
}
|
|
|
|
cnt := requestAmount
|
|
|
|
oldestIdx := latestIdx - requestAmount + 1
|
|
|
|
if oldestIdx < 0 {
|
|
|
|
cnt = latestIdx + 1
|
|
|
|
oldestIdx = 0
|
|
|
|
}
|
|
|
|
return cnt, tsHeights[oldestIdx]
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:33:59 +00:00
|
|
|
func TestEthFeeHistory(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
kit.QuietAllLogsExcept()
|
|
|
|
|
|
|
|
blockTime := 100 * time.Millisecond
|
|
|
|
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-03-09 00:28:17 +00:00
|
|
|
heads, err := client.ChainNotify(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Save the full view of the tipsets to calculate the answer when there are null rounds
|
|
|
|
tsHeights := []int{1}
|
|
|
|
go func() {
|
|
|
|
for chg := range heads {
|
|
|
|
for _, c := range chg {
|
|
|
|
tsHeights = append(tsHeights, int(c.Val.Height()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
miner := ens.InterconnectAll().BeginMining(blockTime)
|
|
|
|
|
|
|
|
client.WaitTillChain(ctx, kit.HeightAtLeast(7))
|
|
|
|
miner[0].InjectNulls(abi.ChainEpoch(5))
|
|
|
|
|
|
|
|
// Wait for the network to create at least 20 tipsets
|
2023-02-14 02:42:44 +00:00
|
|
|
client.WaitTillChain(ctx, kit.HeightAtLeast(20))
|
2023-03-09 00:28:17 +00:00
|
|
|
for _, m := range miner {
|
|
|
|
m.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := client.ChainNotify(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Wait for 5 seconds of inactivity
|
|
|
|
func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
continue
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sort.Ints(tsHeights)
|
|
|
|
|
|
|
|
// because of the deferred execution, the last tipset is not executed yet,
|
|
|
|
// and the one before the last one is the last executed tipset,
|
|
|
|
// which corresponds to the "latest" tag in EthGetBlockByNumber
|
|
|
|
latestBlk := ethtypes.EthUint64(tsHeights[len(tsHeights)-2])
|
|
|
|
blk, err := client.EthGetBlockByNumber(ctx, "latest", false)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(blk.Number, latestBlk)
|
|
|
|
|
|
|
|
assertHistory := func(history *ethtypes.EthFeeHistory, requestAmount, startHeight int) {
|
|
|
|
amount, oldest := calculateExpectations(tsHeights, requestAmount, startHeight)
|
|
|
|
require.Equal(amount+1, len(history.BaseFeePerGas))
|
|
|
|
require.Equal(amount, len(history.GasUsedRatio))
|
|
|
|
require.Equal(ethtypes.EthUint64(oldest), history.OldestBlock)
|
|
|
|
}
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err := client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "0x10"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 5, 16)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Nil(history.Reward)
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{"5", "0x10"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 5, 16)
|
|
|
|
require.Nil(history.Reward)
|
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "latest"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
|
|
|
assertHistory(&history, 5, int(latestBlk))
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Nil(history.Reward)
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{"0x10", "0x12"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 16, 18)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Nil(history.Reward)
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "0x10"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 5, 16)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Nil(history.Reward)
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "10"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 5, 10)
|
|
|
|
require.Nil(history.Reward)
|
|
|
|
|
|
|
|
// test when the requested number of blocks is longer than chain length
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{"0x30", "latest"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
|
|
|
assertHistory(&history, 48, int(latestBlk))
|
|
|
|
require.Nil(history.Reward)
|
|
|
|
|
|
|
|
// test when the requested number of blocks is longer than chain length
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{"0x30", "10"}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
|
|
|
assertHistory(&history, 48, 10)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Nil(history.Reward)
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
2023-02-14 01:02:37 +00:00
|
|
|
json.Marshal([]interface{}{5, "10", &[]float64{25, 50, 75}}),
|
2023-02-10 18:33:59 +00:00
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-03-09 00:28:17 +00:00
|
|
|
assertHistory(&history, 5, 10)
|
2023-02-10 18:33:59 +00:00
|
|
|
require.NotNil(history.Reward)
|
2023-02-14 01:02:37 +00:00
|
|
|
require.Equal(5, len(*history.Reward))
|
|
|
|
for _, arr := range *history.Reward {
|
|
|
|
require.Equal(3, len(arr))
|
2023-03-09 00:28:17 +00:00
|
|
|
for _, item := range arr {
|
|
|
|
require.Equal(ethtypes.EthBigInt(types.NewInt(full.MinGasPremium)), item)
|
|
|
|
}
|
2023-02-14 01:02:37 +00:00
|
|
|
}
|
2023-02-10 18:33:59 +00:00
|
|
|
|
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
2023-02-14 01:02:37 +00:00
|
|
|
json.Marshal([]interface{}{1025, "10", &[]float64{25, 50, 75}}),
|
2023-02-10 18:33:59 +00:00
|
|
|
).Assert(require.NoError))
|
|
|
|
require.Error(err)
|
2023-02-14 01:02:37 +00:00
|
|
|
|
2023-03-09 00:28:17 +00:00
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "10", &[]float64{75, 50}}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.Error(err)
|
|
|
|
|
2023-02-14 01:02:37 +00:00
|
|
|
history, err = client.EthFeeHistory(ctx, result.Wrap[jsonrpc.RawParams](
|
|
|
|
json.Marshal([]interface{}{5, "10", &[]float64{}}),
|
|
|
|
).Assert(require.NoError))
|
|
|
|
require.NoError(err)
|
2023-02-10 18:33:59 +00:00
|
|
|
}
|