Skip to content

Commit

Permalink
Move CeloBackend from core to contracts
Browse files Browse the repository at this point in the history
...and other import cleanups.

Goals:
* Prevent circular dependencies (being in core is really problematic in
  this regard)
* Avoid package aliases in imports
  • Loading branch information
karlb committed Apr 3, 2024
1 parent 7f4450f commit 147a24e
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 34 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion contracts/celo/celo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package contracts
package celo

import _ "embed"

Expand Down
7 changes: 3 additions & 4 deletions core/celo_backend.go → contracts/celo_backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package contracts

import (
"context"
Expand All @@ -8,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/contracts/celo/abigen"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -86,11 +85,11 @@ func (b *CeloBackend) GetFeeBalance(account common.Address, feeCurrency *common.
// GetExchangeRates returns the exchange rates for all gas currencies from CELO
func (b *CeloBackend) GetExchangeRates() (common.ExchangeRates, error) {
exchangeRates := map[common.Address]*big.Rat{}
whitelist, err := abigen.NewFeeCurrencyWhitelistCaller(contracts.FeeCurrencyWhitelistAddress, b)
whitelist, err := abigen.NewFeeCurrencyWhitelistCaller(FeeCurrencyWhitelistAddress, b)
if err != nil {
return exchangeRates, fmt.Errorf("Failed to access FeeCurrencyWhitelist: %w", err)
}
oracle, err := abigen.NewSortedOraclesCaller(contracts.SortedOraclesAddress, b)
oracle, err := abigen.NewSortedOraclesCaller(SortedOraclesAddress, b)
if err != nil {
return exchangeRates, fmt.Errorf("Failed to access SortedOracle: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package contracts

import (
"math/big"
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 @@ -23,7 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/exchange"
"github.com/ethereum/go-ethereum/consensus/ethash"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -100,7 +100,7 @@ func testNativeTransferWithFeeCurrency(t *testing.T, scheme string) {

state, _ := chain.State()

backend := CeloBackend{
backend := contracts.CeloBackend{
ChainConfig: chain.chainConfig,
State: state,
}
Expand Down
4 changes: 2 additions & 2 deletions core/celo_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/contracts/celo/abigen"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -51,7 +51,7 @@ func setCeloFieldsInBlockContext(blockContext *vm.BlockContext, header *types.He
return
}

caller := &CeloBackend{config, statedb}
caller := &contracts.CeloBackend{ChainConfig: config, State: statedb}

// Add fee currency exchange rates
var err error
Expand Down
19 changes: 10 additions & 9 deletions core/celo_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down Expand Up @@ -48,32 +49,32 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
)

// Initialize Bytecodes
registryBytecode, err := DecodeHex(contracts.RegistryBytecodeRaw)
registryBytecode, err := DecodeHex(celo.RegistryBytecodeRaw)
if err != nil {
panic(err)
}
goldTokenBytecode, err := DecodeHex(contracts.GoldTokenBytecodeRaw)
goldTokenBytecode, err := DecodeHex(celo.GoldTokenBytecodeRaw)
if err != nil {
panic(err)
}
proxyBytecode, err := DecodeHex(contracts.ProxyBytecodeRaw)
proxyBytecode, err := DecodeHex(celo.ProxyBytecodeRaw)
if err != nil {
panic(err)
}
// sortedOraclesBytecodeLinked := bytes.Replace(contracts.SortedOraclesBytecodeRaw, []byte("__$c0b499b413513d0c67e2a6a17d90846cb3$__"), []byte("000000000000000000000000000000000000ce17"), -1)
sortedOraclesBytecode, err := DecodeHex(contracts.MockSortedOraclesBytecodeRaw)
// sortedOraclesBytecodeLinked := bytes.Replace(celo.SortedOraclesBytecodeRaw, []byte("__$c0b499b413513d0c67e2a6a17d90846cb3$__"), []byte("000000000000000000000000000000000000ce17"), -1)
sortedOraclesBytecode, err := DecodeHex(celo.MockSortedOraclesBytecodeRaw)
if err != nil {
panic(err)
}
feeCurrencyWhitelistBytecode, err := DecodeHex(contracts.FeeCurrencyWhitelistBytecodeRaw)
feeCurrencyWhitelistBytecode, err := DecodeHex(celo.FeeCurrencyWhitelistBytecodeRaw)
if err != nil {
panic(err)
}
feeCurrencyBytecode, err := DecodeHex(contracts.FeeCurrencyBytecodeRaw)
feeCurrencyBytecode, err := DecodeHex(celo.FeeCurrencyBytecodeRaw)
if err != nil {
panic(err)
}
addressSortedLinkedListWithMedian, err := DecodeHex(contracts.AddressSortedLinkedListWithMedianBytecodeRaw)
addressSortedLinkedListWithMedian, err := DecodeHex(celo.AddressSortedLinkedListWithMedianBytecodeRaw)
if err != nil {
panic(err)
}
Expand Down
13 changes: 6 additions & 7 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (
"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"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -127,10 +126,10 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b
// In this case, however, the user always ends up paying `maxGasForDebitAndCreditTransactions`
// keeping it consistent.
if feeCurrency != nil {
if (math.MaxUint64 - gas) < fee_currencies.IntrinsicGasForAlternativeFeeCurrency {
if (math.MaxUint64 - gas) < contracts.IntrinsicGasForAlternativeFeeCurrency {
return 0, ErrGasUintOverflow
}
gas += fee_currencies.IntrinsicGasForAlternativeFeeCurrency
gas += contracts.IntrinsicGasForAlternativeFeeCurrency
}

if accessList != nil {
Expand Down Expand Up @@ -345,7 +344,7 @@ func (st *StateTransition) canPayFee(checkAmount *big.Int) error {
return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), balance, checkAmount)
}
} else {
backend := &CeloBackend{
backend := &contracts.CeloBackend{
ChainConfig: st.evm.ChainConfig(),
State: st.state,
}
Expand All @@ -369,7 +368,7 @@ func (st *StateTransition) subFees(effectiveFee *big.Int) (err error) {
st.state.SubBalance(st.msg.From, effectiveFee)
return nil
} else {
return fee_currencies.DebitFees(st.evm, st.msg.FeeCurrency, st.msg.From, effectiveFee)
return contracts.DebitFees(st.evm, st.msg.FeeCurrency, st.msg.From, effectiveFee)
}
}

Expand Down Expand Up @@ -721,7 +720,7 @@ func (st *StateTransition) distributeTxFees() error {
if l1Cost != nil {
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 {
if err := contracts.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)
return err
}
Expand Down
5 changes: 3 additions & 2 deletions core/txpool/blobpool/blobpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/txpool"
Expand Down Expand Up @@ -317,8 +318,8 @@ type BlobPool struct {
lock sync.RWMutex // Mutex protecting the pool during reorg handling

// Celo specific
celoBackend *core.CeloBackend // For fee currency balances & exchange rate calculation
currentRates common.ExchangeRates // current exchange rates for fee currencies
celoBackend *contracts.CeloBackend // For fee currency balances & exchange rate calculation
currentRates common.ExchangeRates // current exchange rates for fee currencies
}

// New creates a new blob transaction pool to gather, sort and filter inbound
Expand Down
4 changes: 2 additions & 2 deletions core/txpool/blobpool/celo_blobpool.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package blobpool

import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/log"
)

func (pool *BlobPool) recreateCeloProperties() {
pool.celoBackend = &core.CeloBackend{
pool.celoBackend = &contracts.CeloBackend{
ChainConfig: pool.chain.Config(),
State: pool.state,
}
Expand Down
4 changes: 2 additions & 2 deletions core/txpool/legacypool/celo_legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)
Expand All @@ -31,7 +31,7 @@ func (pool *LegacyPool) getBalances(address common.Address, currencies []common.
}

func (pool *LegacyPool) recreateCeloProperties() {
pool.celoBackend = &core.CeloBackend{
pool.celoBackend = &contracts.CeloBackend{
ChainConfig: pool.chainconfig,
State: pool.currentState,
}
Expand Down
5 changes: 3 additions & 2 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/prque"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/txpool"
Expand Down Expand Up @@ -238,8 +239,8 @@ type LegacyPool struct {
l1CostFn txpool.L1CostFunc // To apply L1 costs as rollup, optional field, may be nil.

// Celo specific
celoBackend *core.CeloBackend // For fee currency balances & exchange rate calculation
currentRates common.ExchangeRates // current exchange rates for fee currencies
celoBackend *contracts.CeloBackend // For fee currency balances & exchange rate calculation
currentRates common.ExchangeRates // current exchange rates for fee currencies
}

type txpoolResetRequest struct {
Expand Down

0 comments on commit 147a24e

Please sign in to comment.