add GasPrice for the DynamicFeeTransaction. (#2261)

This commit is contained in:
Zhengyan Gao 2021-07-01 12:29:32 +08:00 committed by GitHub
parent 17adaf5f87
commit f4ef314e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -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.

View File

@ -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.