From c681a5f0ef43b5b9e63883621c70d368869fc551 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 21 Feb 2022 15:17:06 +0100 Subject: [PATCH 1/7] graphql: add nextBaseFee and effectiveTip fields --- graphql/graphql.go | 41 +++++++++++++++++++++++++++++++++++++++++ graphql/schema.go | 6 +++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/graphql/graphql.go b/graphql/graphql.go index 16e0eb654d97..727696baca41 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" + "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth/filters" @@ -285,6 +286,33 @@ func (t *Transaction) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, e } } +func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) { + tx, err := t.resolve(ctx) + if err != nil || tx == nil { + return nil, err + } + header, err := t.block.resolveHeader(ctx) + if err != nil || header == nil { + return nil, err + } + if header.BaseFee == nil { + return (*hexutil.Big)(tx.GasPrice()), nil + } + + switch tx.Type() { + case types.AccessListTxType: + return nil, nil + case types.DynamicFeeTxType: + tip, err := tx.EffectiveGasTip(header.BaseFee) + if err != nil { + return nil, err + } + return (*hexutil.Big)(tip), nil + default: + return nil, nil + } +} + func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) { tx, err := t.resolve(ctx) if err != nil || tx == nil { @@ -598,6 +626,19 @@ func (b *Block) BaseFeePerGas(ctx context.Context) (*hexutil.Big, error) { return (*hexutil.Big)(header.BaseFee), nil } +func (b *Block) NextBaseFeePerGas(ctx context.Context) (*hexutil.Big, error) { + header, err := b.resolveHeader(ctx) + if err != nil { + return nil, err + } + if header.BaseFee == nil { + return nil, nil + } + chaincfg := b.backend.ChainConfig() + nextBaseFee := misc.CalcBaseFee(chaincfg, header) + return (*hexutil.Big)(nextBaseFee), nil +} + func (b *Block) Parent(ctx context.Context) (*Block, error) { if _, err := b.resolveHeader(ctx); err != nil { return nil, err diff --git a/graphql/schema.go b/graphql/schema.go index 86060cd2388c..b7b456248de6 100644 --- a/graphql/schema.go +++ b/graphql/schema.go @@ -98,6 +98,8 @@ const schema string = ` maxFeePerGas: BigInt # MaxPriorityFeePerGas is the maximum miner tip per gas offered to include a transaction, in wei. maxPriorityFeePerGas: BigInt + # EffectiveTip is the actual amount of reward going to miner after considering the max fee cap. + effectiveTip: BigInt # Gas is the maximum amount of gas this transaction can consume. gas: Long! # InputData is the data supplied to the target of the transaction. @@ -187,8 +189,10 @@ const schema string = ` gasLimit: Long! # GasUsed is the amount of gas that was used executing transactions in this block. gasUsed: Long! - # BaseFeePerGas is the fee perunit of gas burned by the protocol in this block. + # BaseFeePerGas is the fee per unit of gas burned by the protocol in this block. baseFeePerGas: BigInt + # NextBaseFeePerGas is the fee per unit of gas which needs to be burned in the next block. + nextBaseFeePerGas: BigInt # Timestamp is the unix timestamp at which this block was mined. timestamp: Long! # LogsBloom is a bloom filter that can be used to check if a block may From 41c26b6d61fafd81990af9ad01e650119790863d Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 22 Feb 2022 09:46:24 +0100 Subject: [PATCH 2/7] eth/gasprice: use stable sort for fee history rewards calc --- eth/gasprice/feehistory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth/gasprice/feehistory.go b/eth/gasprice/feehistory.go index 970dfd4467a5..4113089afb1e 100644 --- a/eth/gasprice/feehistory.go +++ b/eth/gasprice/feehistory.go @@ -117,7 +117,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) { reward, _ := tx.EffectiveGasTip(bf.block.BaseFee()) sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward} } - sort.Sort(sorter) + sort.Stable(sorter) var txIndex int sumGasUsed := sorter[0].gasUsed From 71528d514870a92aa9f84e8a3782cf36f94d74ed Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 22 Feb 2022 09:46:38 +0100 Subject: [PATCH 3/7] graphql: minor comment --- graphql/graphql.go | 1 + 1 file changed, 1 insertion(+) diff --git a/graphql/graphql.go b/graphql/graphql.go index 727696baca41..fa466e01aeef 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -299,6 +299,7 @@ func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) { return (*hexutil.Big)(tx.GasPrice()), nil } + // TODO: What to return for non-dynamicfee tx types? switch tx.Type() { case types.AccessListTxType: return nil, nil From 63b7c3c6206d445807243507f096a7f11a5734ec Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 28 Feb 2022 18:28:33 +0100 Subject: [PATCH 4/7] Simpler effectiveTip calc --- graphql/graphql.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/graphql/graphql.go b/graphql/graphql.go index fa466e01aeef..f6fd0227d326 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -299,19 +299,11 @@ func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) { return (*hexutil.Big)(tx.GasPrice()), nil } - // TODO: What to return for non-dynamicfee tx types? - switch tx.Type() { - case types.AccessListTxType: - return nil, nil - case types.DynamicFeeTxType: - tip, err := tx.EffectiveGasTip(header.BaseFee) - if err != nil { - return nil, err - } - return (*hexutil.Big)(tip), nil - default: - return nil, nil + tip, err := tx.EffectiveGasTip(header.BaseFee) + if err != nil { + return nil, err } + return (*hexutil.Big)(tip), nil } func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) { From 5ce5aecc577ebdf3af90a3c63c4a5e227f0a4469 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 1 Mar 2022 18:28:34 +0100 Subject: [PATCH 5/7] fix schema whitespaces --- graphql/schema.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/graphql/schema.go b/graphql/schema.go index b7b456248de6..0013e7bae75c 100644 --- a/graphql/schema.go +++ b/graphql/schema.go @@ -69,7 +69,7 @@ const schema string = ` transaction: Transaction! } - #EIP-2718 + #EIP-2718 type AccessTuple{ address: Address! storageKeys : [Bytes32!]! @@ -94,10 +94,10 @@ const schema string = ` value: BigInt! # GasPrice is the price offered to miners for gas, in wei per unit. gasPrice: BigInt! - # MaxFeePerGas is the maximum fee per gas offered to include a transaction, in wei. - maxFeePerGas: BigInt - # MaxPriorityFeePerGas is the maximum miner tip per gas offered to include a transaction, in wei. - maxPriorityFeePerGas: BigInt + # MaxFeePerGas is the maximum fee per gas offered to include a transaction, in wei. + maxFeePerGas: BigInt + # MaxPriorityFeePerGas is the maximum miner tip per gas offered to include a transaction, in wei. + maxPriorityFeePerGas: BigInt # EffectiveTip is the actual amount of reward going to miner after considering the max fee cap. effectiveTip: BigInt # Gas is the maximum amount of gas this transaction can consume. @@ -190,9 +190,9 @@ const schema string = ` # GasUsed is the amount of gas that was used executing transactions in this block. gasUsed: Long! # BaseFeePerGas is the fee per unit of gas burned by the protocol in this block. - baseFeePerGas: BigInt - # NextBaseFeePerGas is the fee per unit of gas which needs to be burned in the next block. - nextBaseFeePerGas: BigInt + baseFeePerGas: BigInt + # NextBaseFeePerGas is the fee per unit of gas which needs to be burned in the next block. + nextBaseFeePerGas: BigInt # Timestamp is the unix timestamp at which this block was mined. timestamp: Long! # LogsBloom is a bloom filter that can be used to check if a block may @@ -248,10 +248,10 @@ const schema string = ` gas: Long # GasPrice is the price, in wei, offered for each unit of gas. gasPrice: BigInt - # MaxFeePerGas is the maximum fee per gas offered, in wei. - maxFeePerGas: BigInt - # MaxPriorityFeePerGas is the maximum miner tip per gas offered, in wei. - maxPriorityFeePerGas: BigInt + # MaxFeePerGas is the maximum fee per gas offered, in wei. + maxFeePerGas: BigInt + # MaxPriorityFeePerGas is the maximum miner tip per gas offered, in wei. + maxPriorityFeePerGas: BigInt # Value is the value, in wei, sent along with the call. value: BigInt # Data is the data sent to the callee. From 9799f30f3e9b680a245706d199d636b9510b4e3f Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 1 Mar 2022 19:40:58 +0100 Subject: [PATCH 6/7] Fix nextbasefee for london transition block --- graphql/graphql.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/graphql/graphql.go b/graphql/graphql.go index f6fd0227d326..05edb2ebe126 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -624,10 +624,13 @@ func (b *Block) NextBaseFeePerGas(ctx context.Context) (*hexutil.Big, error) { if err != nil { return nil, err } + chaincfg := b.backend.ChainConfig() if header.BaseFee == nil { - return nil, nil + // Make sure next block doesn't enable EIP-1559 + if !chaincfg.IsLondon(new(big.Int).Add(header.Number, common.Big1)) { + return nil, nil + } } - chaincfg := b.backend.ChainConfig() nextBaseFee := misc.CalcBaseFee(chaincfg, header) return (*hexutil.Big)(nextBaseFee), nil } From 12df0c7434db324b0cb2044de90ce10de5f915c2 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 1 Mar 2022 21:27:11 +0100 Subject: [PATCH 7/7] Fix effective gas for pending txes --- graphql/graphql.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/graphql/graphql.go b/graphql/graphql.go index 05edb2ebe126..cb4b226ac57c 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -246,6 +246,10 @@ func (t *Transaction) EffectiveGasPrice(ctx context.Context) (*hexutil.Big, erro if err != nil || tx == nil { return nil, err } + // Pending tx + if t.block == nil { + return nil, nil + } header, err := t.block.resolveHeader(ctx) if err != nil || header == nil { return nil, err @@ -291,6 +295,10 @@ func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) { if err != nil || tx == nil { return nil, err } + // Pending tx + if t.block == nil { + return nil, nil + } header, err := t.block.resolveHeader(ctx) if err != nil || header == nil { return nil, err