Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Implemented FeeHistory API #1130

Merged
merged 9 commits into from
Feb 11, 2022
50 changes: 46 additions & 4 deletions api/api_ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"encoding/binary"
"errors"
"math/big"
"strconv"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -305,7 +307,7 @@ func (api *EthereumAPI) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big,
return api.publicKlayAPI.GasPrice(ctx)
}

type feeHistoryResult struct {
type FeeHistoryResult struct {
jimni1222 marked this conversation as resolved.
Show resolved Hide resolved
OldestBlock *hexutil.Big `json:"oldestBlock"`
Reward [][]*hexutil.Big `json:"reward,omitempty"`
BaseFee []*hexutil.Big `json:"baseFeePerGas,omitempty"`
Expand All @@ -315,9 +317,49 @@ type feeHistoryResult struct {
// DecimalOrHex unmarshals a non-negative decimal or hex parameter into a uint64.
type DecimalOrHex uint64

func (api *EthereumAPI) FeeHistory(ctx context.Context, blockCount DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) {
// TODO-Klaytn: Not implemented yet.
return nil, nil
// UnmarshalJSON implements json.Unmarshaler.
func (dh *DecimalOrHex) UnmarshalJSON(data []byte) error {
input := strings.TrimSpace(string(data))
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}

value, err := strconv.ParseUint(input, 10, 64)
if err != nil {
value, err = hexutil.DecodeUint64(input)
}
if err != nil {
return err
}
*dh = DecimalOrHex(value)
return nil
}

func (api *EthereumAPI) FeeHistory(ctx context.Context, blockCount DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*FeeHistoryResult, error) {
oldest, reward, baseFee, gasUsed, err := api.publicKlayAPI.b.FeeHistory(ctx, int(blockCount), lastBlock, rewardPercentiles)
if err != nil {
return nil, err
}
results := &FeeHistoryResult{
OldestBlock: (*hexutil.Big)(oldest),
GasUsedRatio: gasUsed,
}
if reward != nil {
results.Reward = make([][]*hexutil.Big, len(reward))
for i, w := range reward {
results.Reward[i] = make([]*hexutil.Big, len(w))
for j, v := range w {
results.Reward[i][j] = (*hexutil.Big)(v)
}
}
}
if baseFee != nil {
results.BaseFee = make([]*hexutil.Big, len(baseFee))
for i, v := range baseFee {
results.BaseFee[i] = (*hexutil.Big)(v)
}
}
return results, nil
}

// Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not
Expand Down
1 change: 1 addition & 0 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Backend interface {
AccountManager() accounts.AccountManager
RPCGasCap() *big.Int // global gas cap for klay_call over rpc: DoS protection
Engine() consensus.Engine
FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)

// BlockChain API
SetHead(number uint64)
Expand Down
18 changes: 18 additions & 0 deletions api/mocks/backend_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions log/log_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const (
ChainDataFetcher
KAS
FORK
NodeCnGasPrice

// ModuleNameLen should be placed at the end of the list.
ModuleNameLen
Expand Down Expand Up @@ -199,4 +200,5 @@ var moduleNames = [ModuleNameLen]string{
"datasync/chaindatafetcher",
"kas",
"fork",
"node/cn/gasprice",
}
4 changes: 4 additions & 0 deletions node/cn/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,7 @@ func (b *CNAPIBackend) RPCGasCap() *big.Int {
func (b *CNAPIBackend) Engine() consensus.Engine {
return b.cn.engine
}

func (b *CNAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}
6 changes: 4 additions & 2 deletions node/cn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func GetDefaultConfig() *Config {

TxPool: blockchain.DefaultTxPoolConfig,
GPO: gasprice.Config{
Blocks: 20,
Percentile: 60,
Blocks: 20,
Percentile: 60,
MaxHeaderHistory: 1024,
MaxBlockHistory: 1024,
},
WsEndpoint: "localhost:8546",

Expand Down
Loading