Skip to content

Commit

Permalink
Move exchange comparisons and etc to common/exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
hbandura committed Feb 15, 2024
1 parent e39a580 commit a1ef7b5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 48 deletions.
33 changes: 29 additions & 4 deletions core/txpool/celo_rates.go → common/exchange/rates.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
package txpool
package exchange

import (
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/log"
)

var (
unitRate = big.NewRat(1, 1)
// ErrNonWhitelistedFeeCurrency is returned if the currency specified to use for the fees
// isn't one of the currencies whitelisted for that purpose.
ErrNonWhitelistedFeeCurrency = errors.New("non-whitelisted fee currency address")
)

// ConvertCurrency does an exchange conversion from currencyFrom to currencyTo of the value given.
func ConvertCurrency(exchangeRates common.ExchangeRates, val1 *big.Int, currencyFrom *common.Address, currencyTo *common.Address) *big.Int {
goldAmount, err := contracts.ConvertCurrencyToGold(exchangeRates, val1, currencyFrom)
goldAmount, err := ConvertCurrencyToGold(exchangeRates, val1, currencyFrom)
if err != nil {
log.Error("Error trying to convert from currency to gold.", "value", val1, "fromCurrency", currencyFrom.Hex())
}
toAmount, err := contracts.ConvertGoldToCurrency(exchangeRates, currencyTo, goldAmount)
toAmount, err := ConvertGoldToCurrency(exchangeRates, currencyTo, goldAmount)
if err != nil {
log.Error("Error trying to convert from gold to currency.", "value", goldAmount, "toCurrency", currencyTo.Hex())
}
return toAmount
}

func ConvertCurrencyToGold(exchangeRates common.ExchangeRates, currencyAmount *big.Int, feeCurrency *common.Address) (*big.Int, error) {
if feeCurrency == nil {
return currencyAmount, nil
}
exchangeRate, ok := exchangeRates[*feeCurrency]
if !ok {
return nil, ErrNonWhitelistedFeeCurrency
}
return new(big.Int).Div(new(big.Int).Mul(currencyAmount, exchangeRate.Denom()), exchangeRate.Num()), nil
}

func ConvertGoldToCurrency(exchangeRates common.ExchangeRates, feeCurrency *common.Address, goldAmount *big.Int) (*big.Int, error) {
if feeCurrency == nil {
return goldAmount, nil
}
exchangeRate, ok := exchangeRates[*feeCurrency]
if !ok {
return nil, ErrNonWhitelistedFeeCurrency
}
return new(big.Int).Div(new(big.Int).Mul(goldAmount, exchangeRate.Num()), exchangeRate.Denom()), nil
}

func getRate(exchangeRates common.ExchangeRates, feeCurrency *common.Address) (*big.Rat, error) {
if feeCurrency == nil {
return unitRate, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package txpool
package exchange

import (
"math/big"
Expand Down
26 changes: 0 additions & 26 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,8 @@ const (

var (
tmpAddress = common.HexToAddress("0xce106a5")

// ErrNonWhitelistedFeeCurrency is returned if the currency specified to use for the fees
// isn't one of the currencies whitelisted for that purpose.
ErrNonWhitelistedFeeCurrency = errors.New("non-whitelisted fee currency address")
)

func ConvertCurrencyToGold(exchangeRates common.ExchangeRates, currencyAmount *big.Int, feeCurrency *common.Address) (*big.Int, error) {
if feeCurrency == nil {
return currencyAmount, nil
}
exchangeRate, ok := exchangeRates[*feeCurrency]
if !ok {
return nil, ErrNonWhitelistedFeeCurrency
}
return new(big.Int).Div(new(big.Int).Mul(currencyAmount, exchangeRate.Denom()), exchangeRate.Num()), nil
}

func ConvertGoldToCurrency(exchangeRates common.ExchangeRates, feeCurrency *common.Address, goldAmount *big.Int) (*big.Int, error) {
if feeCurrency == nil {
return goldAmount, nil
}
exchangeRate, ok := exchangeRates[*feeCurrency]
if !ok {
return nil, ErrNonWhitelistedFeeCurrency
}
return new(big.Int).Div(new(big.Int).Mul(goldAmount, exchangeRate.Num()), exchangeRate.Denom()), nil
}

// Debits transaction fees from the transaction sender and stores them in the temporary address
func DebitFees(evm *vm.EVM, feeCurrency *common.Address, address common.Address, amount *big.Int) error {
if amount.Cmp(big.NewInt(0)) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_celo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/exchange"
"github.com/ethereum/go-ethereum/consensus/ethash"
fee_currencies "github.com/ethereum/go-ethereum/contracts"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -108,7 +108,7 @@ func testNativeTransferWithFeeCurrency(t *testing.T, scheme string) {
if err != nil {
t.Fatal("could not get exchange rates")
}
baseFeeInFeeCurrency, _ := fee_currencies.ConvertGoldToCurrency(exchangeRates, &FeeCurrencyAddr, block.BaseFee())
baseFeeInFeeCurrency, _ := exchange.ConvertGoldToCurrency(exchangeRates, &FeeCurrencyAddr, block.BaseFee())
actual, _ := backend.GetBalanceERC20(block.Coinbase(), FeeCurrencyAddr)

// 3: Ensure that miner received only the tx's tip.
Expand Down
4 changes: 2 additions & 2 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/exchange"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
fee_currencies "github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -156,7 +156,7 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
if tx.Type() == types.CeloDynamicFeeTxType {
alternativeBaseFee := evm.Context.BaseFee
if msg.FeeCurrency != nil {
alternativeBaseFee, err = fee_currencies.ConvertGoldToCurrency(evm.Context.ExchangeRates, msg.FeeCurrency, evm.Context.BaseFee)
alternativeBaseFee, err = exchange.ConvertGoldToCurrency(evm.Context.ExchangeRates, msg.FeeCurrency, evm.Context.BaseFee)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/exchange"
cmath "github.com/ethereum/go-ethereum/common/math"
fee_currencies "github.com/ethereum/go-ethereum/contracts"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
Expand Down Expand Up @@ -209,7 +210,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
if baseFee != nil {
if msg.FeeCurrency != nil {
var err error
baseFee, err = fee_currencies.ConvertGoldToCurrency(exchangeRates, msg.FeeCurrency, baseFee)
baseFee, err = exchange.ConvertGoldToCurrency(exchangeRates, msg.FeeCurrency, baseFee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,7 +294,7 @@ func (st *StateTransition) buyGas() error {
// L1 data fee needs to be converted in fee currency
if st.msg.FeeCurrency != nil && l1Cost != nil {
// Existence of the fee currency has been checked in `preCheck`
l1Cost, _ = fee_currencies.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, st.msg.FeeCurrency, l1Cost)
l1Cost, _ = exchange.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, st.msg.FeeCurrency, l1Cost)
}
}
if l1Cost != nil {
Expand Down Expand Up @@ -443,7 +444,7 @@ func (st *StateTransition) preCheck() error {
isWhiteListed := common.IsCurrencyWhitelisted(st.evm.Context.ExchangeRates, msg.FeeCurrency)
if !isWhiteListed {
log.Trace("fee currency not whitelisted", "fee currency address", msg.FeeCurrency)
return fee_currencies.ErrNonWhitelistedFeeCurrency
return exchange.ErrNonWhitelistedFeeCurrency
}
}
}
Expand Down Expand Up @@ -703,7 +704,7 @@ func (st *StateTransition) distributeTxFees() error {
}
} else {
if l1Cost != nil {
l1Cost, _ = fee_currencies.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, feeCurrency, l1Cost)
l1Cost, _ = exchange.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, feeCurrency, l1Cost)
}
if err := fee_currencies.CreditFees(st.evm, feeCurrency, from, st.evm.Context.Coinbase, feeHandlerAddress, params.OptimismL1FeeRecipient, refund, tipTxFee, baseTxFee, l1Cost); err != nil {
log.Error("Error crediting", "from", from, "coinbase", st.evm.Context.Coinbase, "feeHandler", feeHandlerAddress)
Expand All @@ -725,7 +726,7 @@ func (st *StateTransition) calculateBaseFee() *big.Int {

if st.msg.FeeCurrency != nil {
// Existence of the fee currency has been checked in `preCheck`
baseFee, _ = fee_currencies.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, st.msg.FeeCurrency, baseFee)
baseFee, _ = exchange.ConvertGoldToCurrency(st.evm.Context.ExchangeRates, st.msg.FeeCurrency, baseFee)
}

return baseFee
Expand Down
11 changes: 5 additions & 6 deletions core/txpool/legacypool/celo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/common/exchange"
"github.com/ethereum/go-ethereum/core/types"
)

Expand Down Expand Up @@ -124,29 +123,29 @@ func (p *pricedList) compareWithRates(a, b *types.Transaction, goldBaseFee *big.
if goldBaseFee != nil {
tipA := effectiveTip(p.rates, goldBaseFee, a)
tipB := effectiveTip(p.rates, goldBaseFee, b)
result, _ := txpool.CompareValue(p.rates, tipA, a.FeeCurrency(), tipB, b.FeeCurrency())
result, _ := exchange.CompareValue(p.rates, tipA, a.FeeCurrency(), tipB, b.FeeCurrency())
return result
}

// Compare fee caps if baseFee is not specified or effective tips are equal
feeA := a.GasFeeCap()
feeB := b.GasFeeCap()
c, _ := txpool.CompareValue(p.rates, feeA, a.FeeCurrency(), feeB, b.FeeCurrency())
c, _ := exchange.CompareValue(p.rates, feeA, a.FeeCurrency(), feeB, b.FeeCurrency())
if c != 0 {
return c
}

// Compare tips if effective tips and fee caps are equal
tipCapA := a.GasTipCap()
tipCapB := b.GasTipCap()
result, _ := txpool.CompareValue(p.rates, tipCapA, a.FeeCurrency(), tipCapB, b.FeeCurrency())
result, _ := exchange.CompareValue(p.rates, tipCapA, a.FeeCurrency(), tipCapB, b.FeeCurrency())
return result
}

func baseFeeInCurrency(rates common.ExchangeRates, goldBaseFee *big.Int, feeCurrency *common.Address) *big.Int {
// can ignore the whitelist error since txs with non whitelisted currencies
// are pruned
baseFee, _ := contracts.ConvertGoldToCurrency(rates, feeCurrency, goldBaseFee)
baseFee, _ := exchange.ConvertGoldToCurrency(rates, feeCurrency, goldBaseFee)
return baseFee
}

Expand Down
5 changes: 3 additions & 2 deletions core/txpool/legacypool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/exchange"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -330,8 +331,8 @@ func (l *list) Add(tx *types.Transaction, priceBump uint64, _ txpool.L1CostFunc,
var thresholdFeeCapInCurrency = thresholdFeeCap
var thresholdTipInCurrency = thresholdTip
if tx.FeeCurrency() != old.FeeCurrency() {
thresholdFeeCapInCurrency = txpool.ConvertCurrency(rates, thresholdFeeCap, old.FeeCurrency(), tx.FeeCurrency())
thresholdTipInCurrency = txpool.ConvertCurrency(rates, thresholdTip, old.FeeCurrency(), tx.FeeCurrency())
thresholdFeeCapInCurrency = exchange.ConvertCurrency(rates, thresholdFeeCap, old.FeeCurrency(), tx.FeeCurrency())
thresholdTipInCurrency = exchange.ConvertCurrency(rates, thresholdTip, old.FeeCurrency(), tx.FeeCurrency())
}
// We have to ensure that both the new fee cap and tip are higher than the
// old ones as well as checking the percentage threshold to ensure that
Expand Down

0 comments on commit a1ef7b5

Please sign in to comment.