Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/celo11' into Kourin1996/add-celo…
Browse files Browse the repository at this point in the history
…-tx-encoding-decoding
  • Loading branch information
Kourin1996 committed Jan 8, 2025
2 parents 95797ff + e806411 commit 15f2f68
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 26 deletions.
2 changes: 1 addition & 1 deletion common/exchange/rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ConvertCurrencyToCelo(exchangeRates common.ExchangeRates, feeCurrency *comm
return currencyAmount, nil
}
if currencyAmount == nil {
return nil, fmt.Errorf("Can't convert nil amount to CELO.")
return nil, fmt.Errorf("could not convert nil amount to CELO")
}
exchangeRate, ok := exchangeRates[*feeCurrency]
if !ok {
Expand Down
20 changes: 16 additions & 4 deletions contracts/addresses/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,31 @@ var (
}
)

// GetAddresses returns the addresses for the given chainID.
// GetAddresses returns the addresses for the given chainID or
// nil if not found.
func GetAddresses(chainID *big.Int) *CeloAddresses {
// ChainID can be uninitialized in some tests
if chainID == nil {
return MainnetAddresses
return nil
}

switch chainID.Uint64() {
case params.CeloAlfajoresChainID:
return AlfajoresAddresses
case params.CeloBaklavaChainID:
return BaklavaAddresses
default:
case params.CeloMainnetChainID:
return MainnetAddresses
default:
return nil
}
}

// GetAddressesOrDefault returns the addresses for the given chainID or
// the Mainnet addresses if none are found.
func GetAddressesOrDefault(chainID *big.Int, defaultValue *CeloAddresses) *CeloAddresses {
addresses := GetAddresses(chainID)
if addresses == nil {
return defaultValue
}
return addresses
}
10 changes: 8 additions & 2 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ func GetRegisteredCurrencies(caller *abigen.FeeCurrencyDirectoryCaller) ([]commo

// GetExchangeRates returns the exchange rates for the provided gas currencies
func GetExchangeRates(caller *CeloBackend) (common.ExchangeRates, error) {
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.GetAddresses(caller.ChainConfig.ChainID).FeeCurrencyDirectory, caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(
addresses.GetAddressesOrDefault(caller.ChainConfig.ChainID, addresses.MainnetAddresses).FeeCurrencyDirectory,
caller,
)
if err != nil {
return common.ExchangeRates{}, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
Expand All @@ -203,7 +206,10 @@ func GetExchangeRates(caller *CeloBackend) (common.ExchangeRates, error) {
// GetFeeCurrencyContext returns the fee currency block context for all registered gas currencies from CELO
func GetFeeCurrencyContext(caller *CeloBackend) (common.FeeCurrencyContext, error) {
var feeContext common.FeeCurrencyContext
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.GetAddresses(caller.ChainConfig.ChainID).FeeCurrencyDirectory, caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(
addresses.GetAddressesOrDefault(caller.ChainConfig.ChainID, addresses.MainnetAddresses).FeeCurrencyDirectory,
caller,
)
if err != nil {
return feeContext, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_celo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func testNativeTransferWithFeeCurrency(t *testing.T, scheme string, feeCurrencyA
funds = DevBalance
gspec = &Genesis{
Config: &config,
Alloc: celoGenesisAccounts(addr1),
Alloc: CeloGenesisAccounts(addr1),
}
)
gspec.Config.Cel2Time = uint64ptr(0)
Expand Down
32 changes: 18 additions & 14 deletions core/celo_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ var (
DevAddr = common.BytesToAddress(DevAddr32.Bytes())
DevAddr32 = common.HexToHash("0x42cf1bbc38BaAA3c4898ce8790e21eD2738c6A4a")

DevFeeCurrencyAddr = common.HexToAddress("0x000000000000000000000000000000000000ce16") // worth half as much as native CELO
DevFeeCurrencyAddr2 = common.HexToAddress("0x000000000000000000000000000000000000ce17") // worth twice as much as native CELO
DevBalance, _ = new(big.Int).SetString("100000000000000000000", 10)
rateNumerator, _ = new(big.Int).SetString("2000000000000000000000000", 10)
rateNumerator2, _ = new(big.Int).SetString("500000000000000000000000", 10)
rateDenominator, _ = new(big.Int).SetString("1000000000000000000000000", 10)
mockOracleAddr = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0001")
mockOracleAddr2 = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0002")
mockOracleAddr3 = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0003")
FaucetAddr = common.HexToAddress("0xfcf982bb4015852e706100b14e21f947a5bb718e")
DevFeeCurrencyAddr = common.HexToAddress("0x000000000000000000000000000000000000ce16") // worth half as much as native CELO
DevFeeCurrencyAddr2 = common.HexToAddress("0x000000000000000000000000000000000000ce17") // worth twice as much as native CELO
DevBalance, _ = new(big.Int).SetString("100000000000000000000", 10)
rateNumerator, _ = new(big.Int).SetString("2000000000000000000000000", 10)
rateNumerator2, _ = new(big.Int).SetString("500000000000000000000000", 10)
rateDenominator, _ = new(big.Int).SetString("1000000000000000000000000", 10)
mockOracleAddr = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0001")
mockOracleAddr2 = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0002")
mockOracleAddr3 = common.HexToAddress("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb0003")
FaucetAddr = common.HexToAddress("0xfcf982bb4015852e706100b14e21f947a5bb718e")
FeeCurrencyIntrinsicGas = uint64(50000)
)

func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
func CeloGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
// Initialize Bytecodes
celoTokenBytecode, err := DecodeHex(celo.CeloTokenBytecodeRaw)
if err != nil {
Expand All @@ -79,7 +80,7 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {

faucetBalance, ok := new(big.Int).SetString("500000000000000000000000000", 10) // 500M
if !ok {
panic("Couldn not set faucet balance!")
panic("Could not set faucet balance!")
}
genesisAccounts := map[common.Address]GenesisAccount{
addresses.MainnetAddresses.CeloToken: {
Expand Down Expand Up @@ -133,6 +134,9 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
FaucetAddr: {
Balance: faucetBalance,
},
fundedAddr: {
Balance: DevBalance,
},
}

// FeeCurrencyDirectory
Expand Down Expand Up @@ -160,6 +164,6 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {

func addFeeCurrencyToStorage(feeCurrencyAddr common.Address, oracleAddr common.Address, storage map[common.Hash]common.Hash) {
structStart := CalcMapAddr(common.HexToHash("0x1"), common.BytesToHash(feeCurrencyAddr.Bytes()))
storage[structStart] = common.BytesToHash(oracleAddr.Bytes()) // oracle
storage[incHash(structStart, 1)] = common.BigToHash(big.NewInt(50000)) // intrinsicGas
storage[structStart] = common.BytesToHash(oracleAddr.Bytes()) // oracle
storage[incHash(structStart, 1)] = common.BigToHash(big.NewInt(int64(FeeCurrencyIntrinsicGas))) // intrinsicGas
}
2 changes: 1 addition & 1 deletion core/celo_state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (st *StateTransition) distributeTxFees() error {
tipTxFee := new(big.Int).Sub(totalTxFee, baseTxFee)

feeCurrency := st.msg.FeeCurrency
feeHandlerAddress := addresses.GetAddresses(st.evm.ChainConfig().ChainID).FeeHandler
feeHandlerAddress := addresses.GetAddressesOrDefault(st.evm.ChainConfig().ChainID, addresses.MainnetAddresses).FeeHandler

log.Trace("distributeTxFees", "from", from, "refund", refund, "feeCurrency", feeCurrency,
"coinbaseFeeRecipient", st.evm.Context.Coinbase, "coinbaseFee", tipTxFee,
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
}

// Add state from celoGenesisAccounts
for addr, data := range celoGenesisAccounts(common.HexToAddress("0x2")) {
for addr, data := range CeloGenesisAccounts(common.HexToAddress("0x2")) {
genesis.Alloc[addr] = data
}

Expand Down
42 changes: 42 additions & 0 deletions core/txpool/celo_validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package txpool

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -9,6 +10,14 @@ import (
"github.com/ethereum/go-ethereum/params"
)

var (

// ErrGasPriceDoesNotExceedBaseFeeFloor is returned if the gas price specified is
// lower than the configured base-fee-floor
ErrGasPriceDoesNotExceedBaseFeeFloor = errors.New("gas-price is less than the base-fee-floor")
ErrMinimumEffectiveGasTipBelowMinTip = errors.New("effective gas tip at base-fee-floor is below threshold")
)

// AcceptSet is a set of accepted transaction types for a transaction subpool.
type AcceptSet = map[uint8]struct{}

Expand Down Expand Up @@ -52,9 +61,42 @@ func CeloValidateTransaction(tx *types.Transaction, head *types.Header,
if err := ValidateTransaction(tx, head, signer, opts, currencyCtx); err != nil {
return err
}

if !common.IsCurrencyAllowed(currencyCtx.ExchangeRates, tx.FeeCurrency()) {
return exchange.ErrUnregisteredFeeCurrency
}

if opts.Config.Celo != nil {
// Make sure that the effective gas tip at the base fee floor is at least the
// requested min-tip.
// The min-tip for local transactions is set to 0, we can skip checking here.
if opts.MinTip != nil && opts.MinTip.Cmp(new(big.Int)) > 0 {
// If not, this would never be included, so we can reject early.
minTip, err := exchange.ConvertCeloToCurrency(currencyCtx.ExchangeRates, tx.FeeCurrency(), opts.MinTip)
if err != nil {
return err
}
baseFeeFloor, err := exchange.ConvertCeloToCurrency(currencyCtx.ExchangeRates, tx.FeeCurrency(), new(big.Int).SetUint64(opts.Config.Celo.EIP1559BaseFeeFloor))
if err != nil {
return err
}
if tx.EffectiveGasTipIntCmp(minTip, baseFeeFloor) < 0 {
return ErrUnderpriced
}
}

celoGasPrice, err := exchange.ConvertCurrencyToCelo(
currencyCtx.ExchangeRates,
tx.FeeCurrency(),
tx.GasFeeCap(),
)
if err != nil {
return err
}

if new(big.Int).SetUint64(opts.Config.Celo.EIP1559BaseFeeFloor).Cmp(celoGasPrice) == 1 {
return ErrGasPriceDoesNotExceedBaseFeeFloor
}
}
return nil
}
Loading

0 comments on commit 15f2f68

Please sign in to comment.