From f4ef314e5016daa2e005bc0418bef260e4b5f841 Mon Sep 17 00:00:00 2001 From: Zhengyan Gao <1135326346@qq.com> Date: Thu, 1 Jul 2021 12:29:32 +0800 Subject: [PATCH] add GasPrice for the DynamicFeeTransaction. (#2261) --- cmd/rpcdaemon/commands/eth_api.go | 15 ++++++++++++--- cmd/rpcdaemon/commands/eth_txs.go | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index aee6c9e67..f24cc2c20 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -3,6 +3,7 @@ package commands import ( "bytes" "context" + "github.com/holiman/uint256" "math/big" "sync" @@ -109,7 +110,7 @@ func (api *BaseAPI) chainConfig(tx ethdb.Tx) (*params.ChainConfig, error) { return cfg, err } -//nolint:unused +// nolint:unused func (api *BaseAPI) genesis(tx ethdb.Tx) (*types.Block, error) { _, genesis, err := api.chainConfigWithGenesis(tx) return genesis, err @@ -206,7 +207,7 @@ type RPCTransaction struct { // newRPCTransaction returns a transaction that will serialize to the RPC // representation, with the given location metadata set (if available). -func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction { +func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int) *RPCTransaction { // Determine the signer. For replay-protected transactions, use the most permissive // signer, because we assume that signers are backwards-compatible with old // transactions. For non-protected transactions, the homestead signer signer is used @@ -245,6 +246,14 @@ func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber result.R = (*hexutil.Big)(t.R.ToBig()) result.S = (*hexutil.Big)(t.S.ToBig()) result.Accesses = &t.AccessList + baseFee, overflow := uint256.FromBig(baseFee) + if baseFee != nil && !overflow && blockHash != (common.Hash{}) { + // price = min(tip + baseFee, gasFeeCap) + price := math.Min256(new(uint256.Int).Add(tx.GetTip(), baseFee), tx.GetFeeCap()) + result.GasPrice = (*hexutil.Big)(price.ToBig()) + } else { + result.GasPrice = nil + } } signer := types.LatestSignerForChainID(chainId) result.From, _ = tx.Sender(*signer) @@ -258,7 +267,7 @@ func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber // newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation func newRPCPendingTransaction(tx types.Transaction) *RPCTransaction { - return newRPCTransaction(tx, common.Hash{}, 0, 0) + return newRPCTransaction(tx, common.Hash{}, 0, 0, nil) } // newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index. diff --git a/cmd/rpcdaemon/commands/eth_txs.go b/cmd/rpcdaemon/commands/eth_txs.go index e4afca42c..2db4478a6 100644 --- a/cmd/rpcdaemon/commands/eth_txs.go +++ b/cmd/rpcdaemon/commands/eth_txs.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "math/big" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common/hexutil" @@ -25,8 +26,23 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash) // https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash txn, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(tx, hash) + + // Add GasPrice for the DynamicFeeTransaction + var baseFee *big.Int + chainConfig, err := api.chainConfig(tx) + if err != nil { + return nil, err + } + if chainConfig.IsLondon(blockNumber) && blockHash != (common.Hash{}) { + block, err := rawdb.ReadBlockByHash(tx, blockHash) + if err != nil { + return nil, err + } + baseFee = block.BaseFee() + } + if txn != nil { - return newRPCTransaction(txn, blockHash, blockNumber, txIndex), nil + return newRPCTransaction(txn, blockHash, blockNumber, txIndex, baseFee), nil } // No finalized transaction, try to retrieve it from the pool @@ -95,7 +111,7 @@ func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, block return nil, fmt.Errorf("txIndex (%d) out of range (nTxs: %d)", uint64(txIndex), uint64(len(txs))) } - return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil + return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex), block.BaseFee()), nil } // GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index. @@ -145,7 +161,7 @@ func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blo return nil, fmt.Errorf("txIndex (%d) out of range (nTxs: %d)", uint64(txIndex), uint64(len(txs))) } - return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil + return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex), block.BaseFee()), nil } // GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.