core/types: add IntrinsicGas() function to compute tx intrinsic gas

This commit is contained in:
Cedrick AHOUANGANSI 2025-02-08 20:25:45 +01:00 committed by lightclient
parent 51753b5648
commit 45c2c0bd3d
No known key found for this signature in database
GPG key ID: 657913021EF45A6A
8 changed files with 32 additions and 30 deletions

View file

@ -96,23 +96,17 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
gasPrice = gen.header.BaseFee
}
rules := params.TestChainConfig.Rules(common.Big0, true, 0)
tx0 := types.NewTx(&types.LegacyTx{
txdata := &types.LegacyTx{
Nonce: gen.TxNonce(benchRootAddr),
To: &toaddr,
Value: big.NewInt(1),
Gas: params.TxGas,
Data: data,
GasPrice: gasPrice,
})
gas, _ := tx0.IntrinsicGas(&rules)
tx, _ := types.SignNewTx(benchRootKey, signer, &types.LegacyTx{
Nonce: gen.TxNonce(benchRootAddr),
To: &toaddr,
Value: big.NewInt(1),
Gas: gas,
Data: data,
GasPrice: gasPrice,
})
}
gas, _ := types.IntrinsicGas(txdata, &rules)
txdata.Gas = gas
tx, _ := types.SignNewTx(benchRootKey, signer, txdata)
gen.AddTx(tx)
}
}

View file

@ -82,15 +82,6 @@ func FloorDataGas(data []byte) (uint64, error) {
return params.TxGas + tokens*params.TxCostFloorPerToken, nil
}
// toWordSize returns the ceiled word size required for init code payment calculation.
func toWordSize(size uint64) uint64 {
if size > math.MaxUint64-31 {
return math.MaxUint64/32 + 1
}
return (size + 31) / 32
}
// A Message contains the data derived from a single transaction that is relevant to state
// processing.
type Message struct {

View file

@ -89,6 +89,7 @@ type TxData interface {
value() *big.Int
nonce() uint64
to() *common.Address
setCodeAuthorizations() []SetCodeAuthorization
rawSignatureValues() (v, r, s *big.Int)
setSignatureValues(chainID, v, r, s *big.Int)
@ -482,11 +483,7 @@ func (tx *Transaction) WithBlobTxSidecar(sideCar *BlobTxSidecar) *Transaction {
// SetCodeAuthorizations returns the authorizations list of the transaction.
func (tx *Transaction) SetCodeAuthorizations() []SetCodeAuthorization {
setcodetx, ok := tx.inner.(*SetCodeTx)
if !ok {
return nil
}
return setcodetx.AuthList
return tx.inner.setCodeAuthorizations()
}
// SetCodeAuthorities returns a list of unique authorities from the
@ -584,13 +581,18 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e
return &Transaction{inner: cpy, time: tx.time}, nil
}
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
// IntrinsicGas returns the 'intrinsic gas' computed for a message with the given data.
func (tx *Transaction) IntrinsicGas(rules *params.Rules) (uint64, error) {
return IntrinsicGas(tx.inner, rules)
}
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
func IntrinsicGas(txdata TxData, rules *params.Rules) (uint64, error) {
var (
data = tx.Data()
accessList = tx.AccessList()
authList = tx.SetCodeAuthorizations()
isContractCreation = tx.To() == nil
data = txdata.data()
accessList = txdata.accessList()
authList = txdata.setCodeAuthorizations()
isContractCreation = txdata.to() == nil
)
// Set the starting gas for the raw transaction

View file

@ -107,6 +107,9 @@ func (tx *AccessListTx) gasFeeCap() *big.Int { return tx.GasPrice }
func (tx *AccessListTx) value() *big.Int { return tx.Value }
func (tx *AccessListTx) nonce() uint64 { return tx.Nonce }
func (tx *AccessListTx) to() *common.Address { return tx.To }
func (tx *AccessListTx) setCodeAuthorizations() []SetCodeAuthorization {
return nil
}
func (tx *AccessListTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice)

View file

@ -179,6 +179,9 @@ func (tx *BlobTx) value() *big.Int { return tx.Value.ToBig() }
func (tx *BlobTx) nonce() uint64 { return tx.Nonce }
func (tx *BlobTx) to() *common.Address { tmp := tx.To; return &tmp }
func (tx *BlobTx) blobGas() uint64 { return params.BlobTxBlobGasPerBlob * uint64(len(tx.BlobHashes)) }
func (tx *BlobTx) setCodeAuthorizations() []SetCodeAuthorization {
return nil
}
func (tx *BlobTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {

View file

@ -96,6 +96,9 @@ func (tx *DynamicFeeTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *DynamicFeeTx) value() *big.Int { return tx.Value }
func (tx *DynamicFeeTx) nonce() uint64 { return tx.Nonce }
func (tx *DynamicFeeTx) to() *common.Address { return tx.To }
func (tx *DynamicFeeTx) setCodeAuthorizations() []SetCodeAuthorization {
return nil
}
func (tx *DynamicFeeTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {

View file

@ -103,6 +103,9 @@ func (tx *LegacyTx) gasFeeCap() *big.Int { return tx.GasPrice }
func (tx *LegacyTx) value() *big.Int { return tx.Value }
func (tx *LegacyTx) nonce() uint64 { return tx.Nonce }
func (tx *LegacyTx) to() *common.Address { return tx.To }
func (tx *LegacyTx) setCodeAuthorizations() []SetCodeAuthorization {
return nil
}
func (tx *LegacyTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice)

View file

@ -193,6 +193,9 @@ func (tx *SetCodeTx) gasPrice() *big.Int { return tx.GasFeeCap.ToBig() }
func (tx *SetCodeTx) value() *big.Int { return tx.Value.ToBig() }
func (tx *SetCodeTx) nonce() uint64 { return tx.Nonce }
func (tx *SetCodeTx) to() *common.Address { tmp := tx.To; return &tmp }
func (tx *SetCodeTx) setCodeAuthorizations() []SetCodeAuthorization {
return tx.AuthList
}
func (tx *SetCodeTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {