mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
fix: improve "insufficient funds for l1fee" error msg (#237)
fix(l2geth): improve "insufficient funds for l1fee" error msg
This commit is contained in:
parent
4588569b63
commit
cfbb26922f
1 changed files with 18 additions and 10 deletions
|
|
@ -162,11 +162,11 @@ func copyTransaction(tx *types.Transaction) *types.Transaction {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateTotalFee(tx *types.Transaction, state StateDB) (*big.Int, error) {
|
func CalculateFees(tx *types.Transaction, state StateDB) (*big.Int, *big.Int, *big.Int, error) {
|
||||||
unsigned := copyTransaction(tx)
|
unsigned := copyTransaction(tx)
|
||||||
raw, err := rlpEncode(unsigned)
|
raw, err := rlpEncode(unsigned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
l1BaseFee, overhead, scalar := readGPOStorageSlots(rcfg.L1GasPriceOracleAddress, state)
|
l1BaseFee, overhead, scalar := readGPOStorageSlots(rcfg.L1GasPriceOracleAddress, state)
|
||||||
|
|
@ -175,25 +175,33 @@ func CalculateTotalFee(tx *types.Transaction, state StateDB) (*big.Int, error) {
|
||||||
l2GasLimit := new(big.Int).SetUint64(tx.Gas())
|
l2GasLimit := new(big.Int).SetUint64(tx.Gas())
|
||||||
l2Fee := new(big.Int).Mul(tx.GasPrice(), l2GasLimit)
|
l2Fee := new(big.Int).Mul(tx.GasPrice(), l2GasLimit)
|
||||||
fee := new(big.Int).Add(l1Fee, l2Fee)
|
fee := new(big.Int).Add(l1Fee, l2Fee)
|
||||||
return fee, nil
|
return l1Fee, l2Fee, fee, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyFee(signer types.Signer, tx *types.Transaction, state StateDB) error {
|
func VerifyFee(signer types.Signer, tx *types.Transaction, state StateDB) error {
|
||||||
fee, err := CalculateTotalFee(tx, state)
|
from, err := types.Sender(signer, tx)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("invalid transaction: invalid sender")
|
||||||
|
}
|
||||||
|
|
||||||
|
balance := state.GetBalance(from)
|
||||||
|
|
||||||
|
l1Fee, l2Fee, _, err := CalculateFees(tx, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid transaction: %w", err)
|
return fmt.Errorf("invalid transaction: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cost := tx.Value()
|
cost := tx.Value()
|
||||||
cost = cost.Add(cost, fee)
|
cost = cost.Add(cost, l2Fee)
|
||||||
from, err := types.Sender(signer, tx)
|
if balance.Cmp(cost) < 0 {
|
||||||
if err != nil {
|
|
||||||
return errors.New("invalid transaction: invalid sender")
|
|
||||||
}
|
|
||||||
if state.GetBalance(from).Cmp(cost) < 0 {
|
|
||||||
return errors.New("invalid transaction: insufficient funds for gas * price + value")
|
return errors.New("invalid transaction: insufficient funds for gas * price + value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cost = cost.Add(cost, l1Fee)
|
||||||
|
if balance.Cmp(cost) < 0 {
|
||||||
|
return errors.New("invalid transaction: insufficient funds for l1fee + gas * price + value")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: check GasPrice is in an expected range
|
// TODO: check GasPrice is in an expected range
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue