fix: improve "insufficient funds for l1fee" error msg (#237)

fix(l2geth): improve "insufficient funds for l1fee" error msg
This commit is contained in:
HAOYUatHZ 2023-03-02 20:03:37 +08:00 committed by GitHub
parent 4588569b63
commit cfbb26922f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)
raw, err := rlpEncode(unsigned)
if err != nil {
return nil, err
return nil, nil, nil, err
}
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())
l2Fee := new(big.Int).Mul(tx.GasPrice(), l2GasLimit)
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 {
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 {
return fmt.Errorf("invalid transaction: %w", err)
}
cost := tx.Value()
cost = cost.Add(cost, fee)
from, err := types.Sender(signer, tx)
if err != nil {
return errors.New("invalid transaction: invalid sender")
}
if state.GetBalance(from).Cmp(cost) < 0 {
cost = cost.Add(cost, l2Fee)
if balance.Cmp(cost) < 0 {
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
return nil