Skip to content

Commit

Permalink
move TxMaxFee into fee package
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitprincess committed Oct 26, 2023
1 parent 7208b6c commit ee94534
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions chain/chainhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,12 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
}
case types.TxType_FEEDELEGATION:
balance := receiver.Balance()
var fee *big.Int
fee, err = tx.GetMaxFee(balance, bs.GasPrice, bi.ForkVersion)
var maxFee *big.Int
maxFee, err = fee.TxMaxFee(bi.ForkVersion, len(tx.GetBody().GetPayload()), tx.GetBody().GetGasLimit(), balance, bs.GasPrice)
if err != nil {
return err
}
if fee.Cmp(balance) > 0 {
if maxFee.Cmp(balance) > 0 {
return types.ErrInsufficientBalance
}
var contractState *state.ContractState
Expand Down
6 changes: 3 additions & 3 deletions contract/vm_direct/vm_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,12 @@ func executeTx(
}
case types.TxType_FEEDELEGATION:
balance := receiver.Balance()
var fee *big.Int
fee, err = tx.GetMaxFee(balance, bs.GasPrice, bi.ForkVersion)
var maxFee *big.Int
maxFee, err = fee.TxMaxFee(bi.ForkVersion, len(tx.GetBody().GetPayload()), tx.GetBody().GetGasLimit(), balance, bs.GasPrice)
if err != nil {
return err
}
if fee.Cmp(balance) > 0 {
if maxFee.Cmp(balance) > 0 {
return types.ErrInsufficientBalance
}
var contractState *state.ContractState
Expand Down
22 changes: 21 additions & 1 deletion fee/fee.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fee

import "math/big"
import (
"fmt"
"math/big"
)

const (
baseTxFee = "2000000000000000" // 0.002 AERGO
Expand Down Expand Up @@ -76,3 +79,20 @@ func TxExecuteFee(version int32, isQuery bool, gasPrice *big.Int, usedGas uint64
}
return PaymentDataFee(dbUpdateTotalSize)
}

func TxMaxFee(version int32, lenPayload int, gasLimit uint64, balance, gasPrice *big.Int) (*big.Int, error) {
if IsZeroFee() {
return NewZeroFee(), nil
}
if IsUseTxGas(version) {
minGasLimit := TxGas(lenPayload)
if gasLimit == 0 {
gasLimit = MaxGasLimit(balance, gasPrice)
}
if minGasLimit > gasLimit {
return nil, fmt.Errorf("the minimum required amount of gas: %d", minGasLimit)
}
return CalcFee(gasPrice, gasLimit), nil
}
return MaxPayloadTxFee(lenPayload), nil
}
4 changes: 2 additions & 2 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,11 @@ func (mp *MemPool) validateTx(tx types.Transaction, account types.Address) error
return err
}
bal := aergoState.GetBalanceBigInt()
fee, err := tx.GetMaxFee(bal, system.GetGasPrice(), mp.nextBlockVersion())
maxFee, err := fee.TxMaxFee(mp.nextBlockVersion(), len(tx.GetBody().GetPayload()), tx.GetBody().GetGasLimit(), bal, system.GetGasPrice())
if err != nil {
return err
}
if fee.Cmp(bal) > 0 {
if maxFee.Cmp(bal) > 0 {
return types.ErrInsufficientBalance
}
txBody := tx.GetBody()
Expand Down
23 changes: 2 additions & 21 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type Transaction interface {
GetVerifedAccount() Address
SetVerifedAccount(account Address) bool
RemoveVerifedAccount() bool
GetMaxFee(balance, gasPrice *big.Int, version int32) (*big.Int, error)
}

type transaction struct {
Expand Down Expand Up @@ -308,11 +307,11 @@ func (tx *transaction) ValidateWithSenderState(senderState *State, gasPrice *big
if b.Sign() < 0 {
return ErrInsufficientBalance
}
fee, err := tx.GetMaxFee(b, gasPrice, version)
maxFee, err := fee.TxMaxFee(version, len(tx.GetBody().GetPayload()), tx.GetBody().GetGasLimit(), balance, gasPrice)
if err != nil {
return err
}
if fee.Cmp(b) > 0 {
if maxFee.Cmp(b) > 0 {
return ErrInsufficientBalance
}
case TxType_GOVERNANCE:
Expand Down Expand Up @@ -391,24 +390,6 @@ func (tx *transaction) Clone() *transaction {
return res
}

func (tx *transaction) GetMaxFee(balance, gasPrice *big.Int, version int32) (*big.Int, error) {
if fee.IsZeroFee() {
return fee.NewZeroFee(), nil
}
if version >= 2 {
minGasLimit := fee.TxGas(len(tx.GetBody().GetPayload()))
gasLimit := tx.GetBody().GasLimit
if gasLimit == 0 {
gasLimit = fee.MaxGasLimit(balance, gasPrice)
}
if minGasLimit > gasLimit {
return nil, fmt.Errorf("the minimum required amount of gas: %d", minGasLimit)
}
return new(big.Int).Mul(new(big.Int).SetUint64(gasLimit), gasPrice), nil
}
return fee.MaxPayloadTxFee(len(tx.GetBody().GetPayload())), nil
}

const allowedNameChar = "abcdefghijklmnopqrstuvwxyz1234567890"

func validateAllowedChar(param []byte) error {
Expand Down

0 comments on commit ee94534

Please sign in to comment.