Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] more minimal version of coreth (to abstract miner, chain & txpool) #572

Draft
wants to merge 7 commits into
base: coreth-001
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,20 @@ func New(

eth.bloomIndexer.Start(eth.blockchain)

blockchain := BlockChain(eth.blockchain) // used in initializing the txpool and miner
// Uncomment the following to enable the new blobpool

// config.BlobPool.Datadir = ""
// blobPool := blobpool.New(config.BlobPool, &chainWithFinalBlock{eth.blockchain})
// blobPool := blobpool.New(config.BlobPool, &chainWithFinalBlock{blockchain})

legacyPool := legacypool.New(config.TxPool, eth.blockchain)
legacyPool := legacypool.New(config.TxPool, blockchain)

eth.txPool, err = txpool.New(new(big.Int).SetUint64(config.TxPool.PriceLimit), eth.blockchain, []txpool.SubPool{legacyPool}) //, blobPool})
eth.txPool, err = txpool.New(new(big.Int).SetUint64(config.TxPool.PriceLimit), blockchain, []txpool.SubPool{legacyPool}) //, blobPool})
if err != nil {
return nil, err
}

eth.miner = miner.New(eth, &config.Miner, eth.blockchain.Config(), eth.EventMux(), eth.engine, clock)
eth.miner = miner.New(blockchain, eth.txPool, &config.Miner, blockchain.Config(), eth.EventMux(), eth.engine, clock)

allowUnprotectedTxHashes := make(map[common.Hash]struct{})
for _, txHash := range config.AllowUnprotectedTxHashes {
Expand Down
3 changes: 1 addition & 2 deletions eth/chain_with_final_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
package eth

import (
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/types"
)

const blocksToKeep = 604_800 // Approx. 2 weeks worth of blocks assuming 2s block time

type chainWithFinalBlock struct {
*core.BlockChain
BlockChain
}

// CurrentFinalBlock returns the current block below which blobs should not
Expand Down
26 changes: 26 additions & 0 deletions eth/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eth

import (
"github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/txpool"
"github.com/ava-labs/coreth/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

type BlockChain interface {
HasBlock(common.Hash, uint64) bool
GetBlock(common.Hash, uint64) *types.Block
LastAcceptedBlock() *types.Block

consensus.ChainHeaderReader
Engine() consensus.Engine
CacheConfig() *core.CacheConfig
GetVMConfig() *vm.Config
StateAt(common.Hash) (*state.StateDB, error)

txpool.BlockChain
SenderCacher() *core.TxSenderCacher
}
29 changes: 29 additions & 0 deletions miner/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package miner

import (
"math/big"

"github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/txpool"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

type TxPool interface {
Locals() []common.Address
PendingWithBaseFee(enforceTips bool, baseFee *big.Int) map[common.Address][]*txpool.LazyTransaction
}

type BlockChain interface {
consensus.ChainHeaderReader
Engine() consensus.Engine
HasBlock(common.Hash, uint64) bool
CacheConfig() *core.CacheConfig
GetVMConfig() *vm.Config
StateAt(common.Hash) (*state.StateDB, error)
}
12 changes: 2 additions & 10 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@ package miner
import (
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/txpool"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
)

// Backend wraps all methods required for mining.
type Backend interface {
BlockChain() *core.BlockChain
TxPool() *txpool.TxPool
}

// Config is the configuration parameters of mining.
type Config struct {
Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards
Expand All @@ -54,9 +46,9 @@ type Miner struct {
worker *worker
}

func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, clock *mockable.Clock) *Miner {
func New(chain BlockChain, txPool TxPool, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, clock *mockable.Clock) *Miner {
return &Miner{
worker: newWorker(config, chainConfig, engine, eth, mux, clock),
worker: newWorker(config, chainConfig, engine, chain, txPool, mux, clock),
}
}

Expand Down
18 changes: 9 additions & 9 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ type worker struct {
config *Config
chainConfig *params.ChainConfig
engine consensus.Engine
eth Backend
chain *core.BlockChain
txPool TxPool
chain BlockChain

// Feeds
// TODO remove since this will never be written to
Expand All @@ -107,13 +107,13 @@ type worker struct {
beaconRoot *common.Hash // TODO: set to empty hash, retained for upstream compatibility and future use
}

func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, clock *mockable.Clock) *worker {
func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, chain BlockChain, txPool TxPool, mux *event.TypeMux, clock *mockable.Clock) *worker {
worker := &worker{
config: config,
chainConfig: chainConfig,
engine: engine,
eth: eth,
chain: eth.BlockChain(),
txPool: txPool,
chain: chain,
mux: mux,
coinbase: config.Etherbase,
clock: clock,
Expand All @@ -137,7 +137,7 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte

tstart := w.clock.Time()
timestamp := uint64(tstart.Unix())
parent := w.chain.CurrentBlock()
parent := w.chain.CurrentHeader()
// Note: in order to support asynchronous block production, blocks are allowed to have
// the same timestamp as their parent. This allows more than one block to be produced
// per second.
Expand Down Expand Up @@ -216,11 +216,11 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
return nil, err
}

pending := w.eth.TxPool().PendingWithBaseFee(true, header.BaseFee)
pending := w.txPool.PendingWithBaseFee(true, header.BaseFee)

// Split the pending transactions into locals and remotes.
localTxs, remoteTxs := make(map[common.Address][]*txpool.LazyTransaction), pending
for _, account := range w.eth.TxPool().Locals() {
for _, account := range w.txPool.Locals() {
if txs := remoteTxs[account]; len(txs) > 0 {
delete(remoteTxs, account)
localTxs[account] = txs
Expand All @@ -245,7 +245,7 @@ func (w *worker) createCurrentEnvironment(predicateContext *precompileconfig.Pre
if err != nil {
return nil, err
}
state.StartPrefetcher("miner", w.eth.BlockChain().CacheConfig().TriePrefetcherParallelism)
state.StartPrefetcher("miner", w.chain.CacheConfig().TriePrefetcherParallelism)
return &environment{
signer: types.MakeSigner(w.chainConfig, header.Number, header.Time),
state: state,
Expand Down
3 changes: 1 addition & 2 deletions plugin/evm/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/txpool"
"github.com/ava-labs/coreth/params"

"github.com/ava-labs/avalanchego/snow"
Expand All @@ -27,7 +26,7 @@ type blockBuilder struct {
ctx *snow.Context
chainConfig *params.ChainConfig

txPool *txpool.TxPool
txPool TxPool
mempool *Mempool

shutdownChan <-chan struct{}
Expand Down
3 changes: 1 addition & 2 deletions plugin/evm/export_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"math/big"

"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/params"

"github.com/ava-labs/avalanchego/chains/atomic"
Expand Down Expand Up @@ -369,7 +368,7 @@ func (vm *VM) newExportTx(
}

// EVMStateTransfer executes the state update from the atomic export transaction
func (utx *UnsignedExportTx) EVMStateTransfer(ctx *snow.Context, state *state.StateDB) error {
func (utx *UnsignedExportTx) EVMStateTransfer(ctx *snow.Context, state StateDB) error {
addrs := map[[20]byte]uint64{}
for _, from := range utx.Ins {
if from.AssetID == ctx.AVAXAssetID {
Expand Down
5 changes: 2 additions & 3 deletions plugin/evm/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/ava-labs/avalanchego/network/p2p/gossip"

"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/txpool"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
)
Expand Down Expand Up @@ -114,7 +113,7 @@ func (tx *GossipAtomicTx) GossipID() ids.ID {
return tx.Tx.ID()
}

func NewGossipEthTxPool(mempool *txpool.TxPool, registerer prometheus.Registerer) (*GossipEthTxPool, error) {
func NewGossipEthTxPool(mempool TxPool, registerer prometheus.Registerer) (*GossipEthTxPool, error) {
bloom, err := gossip.NewBloomFilter(registerer, "eth_tx_bloom_filter", txGossipBloomMinTargetElements, txGossipBloomTargetFalsePositiveRate, txGossipBloomResetFalsePositiveRate)
if err != nil {
return nil, fmt.Errorf("failed to initialize bloom filter: %w", err)
Expand All @@ -128,7 +127,7 @@ func NewGossipEthTxPool(mempool *txpool.TxPool, registerer prometheus.Registerer
}

type GossipEthTxPool struct {
mempool *txpool.TxPool
mempool TxPool
pendingTxs chan core.NewTxsEvent

bloom *gossip.BloomFilter
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type GossipHandler struct {
vm *VM
atomicMempool *Mempool
txPool *txpool.TxPool
txPool TxPool
stats GossipStats
}

Expand Down
3 changes: 1 addition & 2 deletions plugin/evm/import_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math/big"
"slices"

"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/params"

"github.com/ava-labs/avalanchego/chains/atomic"
Expand Down Expand Up @@ -428,7 +427,7 @@ func (vm *VM) newImportTxWithUTXOs(

// EVMStateTransfer performs the state transfer to increase the balances of
// accounts accordingly with the imported EVMOutputs
func (utx *UnsignedImportTx) EVMStateTransfer(ctx *snow.Context, state *state.StateDB) error {
func (utx *UnsignedImportTx) EVMStateTransfer(ctx *snow.Context, state StateDB) error {
for _, to := range utx.Outs {
if to.AssetID == ctx.AVAXAssetID {
log.Debug("crosschain", "src", utx.SourceChain, "addr", to.Address, "amount", to.Amount, "assetID", "AVAX")
Expand Down
Loading
Loading