Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

refactor(txpool): Use Geth TxPool #1041

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
11fcbbe
initial
calbera Jul 18, 2023
b8aa222
hooked up and compiling
calbera Jul 18, 2023
8096f2c
setup tests correctly
calbera Jul 18, 2023
e23a001
format
calbera Jul 18, 2023
8e42da7
nil ptr error in Selecting for building block
calbera Jul 19, 2023
318a12f
newIterator
calbera Jul 19, 2023
e90cd51
cleanup
calbera Jul 19, 2023
7cb0703
it fuckin works
calbera Jul 19, 2023
72580f8
Merge branch 'main' into calbera/plr-107-geth-txpool
calbera Jul 20, 2023
30613e8
remove stateAtHeader
calbera Jul 20, 2023
d333160
Merge branch 'main' into calbera/plr-107-geth-txpool
calbera Jul 25, 2023
2911a72
merge main
itsdevbear Aug 29, 2023
d137a25
fix gas issue
itsdevbear Aug 29, 2023
052ea7d
fix gas issue
itsdevbear Aug 29, 2023
c8f7ca0
fix gas issue
itsdevbear Aug 29, 2023
81c3b4e
fix tests
itsdevbear Aug 29, 2023
6d73386
deprecation warning
itsdevbear Aug 29, 2023
a99d15f
bing bong
itsdevbear Aug 30, 2023
18fcaa5
deprecate
itsdevbear Aug 30, 2023
4c2a7d4
checkout ree
itsdevbear Aug 30, 2023
65b6a69
Merge branch 'reeee-fuck' into calbera/plr-107-geth-txpool
itsdevbear Aug 30, 2023
54cd8a5
bing bong
itsdevbear Aug 30, 2023
ef0dc38
big runners
itsdevbear Aug 30, 2023
6e198e1
RemoveTx for now
itsdevbear Aug 30, 2023
2adcd6a
RemoveTx for now
itsdevbear Aug 30, 2023
1fb17ee
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Aug 30, 2023
d64c17d
add sync stuff for brick
itsdevbear Aug 30, 2023
15b60fe
1/2 cache size
itsdevbear Aug 30, 2023
6c51f78
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Aug 30, 2023
cc90409
Update backend.go
itsdevbear Aug 30, 2023
ff221c0
Merge branch 'main' into calbera/plr-107-geth-txpool
calbera Aug 31, 2023
bfc069a
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 1, 2023
36a689e
fix merge
itsdevbear Sep 1, 2023
7a80338
merge
itsdevbear Sep 1, 2023
d97a341
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 1, 2023
5413a3b
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 1, 2023
50535db
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 5, 2023
7b5a239
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 6, 2023
c962c86
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 7, 2023
9bd2d1b
tidy and sync latest main
calbera Sep 8, 2023
e1a6565
Merge branch 'main' into calbera/plr-107-geth-txpool
calbera Sep 8, 2023
0391785
Merge branch 'main' into calbera/plr-107-geth-txpool
ocnc Sep 11, 2023
717a498
Merge branch 'main' of github.com:berachain/polaris into calbera/plr-…
calbera Sep 13, 2023
e9cb5fa
fix the merge
calbera Sep 13, 2023
1f1c8f0
Remove focus from unit test
calbera Sep 13, 2023
e1f51c6
Merge branch 'main' into calbera/plr-107-geth-txpool
itsdevbear Sep 19, 2023
0182721
merge mai
itsdevbear Sep 19, 2023
bf268c8
merge
itsdevbear Sep 19, 2023
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: 4 additions & 5 deletions cosmos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/grpc v1.58.1
google.golang.org/protobuf v1.31.0
pkg.berachain.dev/polaris/contracts v0.0.0-20230830221145-a6c6454d3697
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230830221145-a6c6454d3697
pkg.berachain.dev/polaris/eth v0.0.0-20230830221145-a6c6454d3697
pkg.berachain.dev/polaris/lib v0.0.0-20230830221145-a6c6454d3697
pkg.berachain.dev/polaris/contracts v0.0.0-20230907215015-1928f4a21413
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230907215015-1928f4a21413
pkg.berachain.dev/polaris/eth v0.0.0-20230907215015-1928f4a21413
pkg.berachain.dev/polaris/lib v0.0.0-20230907215015-1928f4a21413
)

require (
Expand Down Expand Up @@ -185,7 +185,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/pointerstructure v1.2.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mtibben/percent v0.2.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions cosmos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1505,14 +1505,14 @@ nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
pkg.berachain.dev/polaris/contracts v0.0.0-20230830221145-a6c6454d3697 h1:D51V7Xugl2EOq7c966pjqTYXT7WodBZMiIsqcze/qgo=
pkg.berachain.dev/polaris/contracts v0.0.0-20230830221145-a6c6454d3697/go.mod h1:ceCUUKQ1GkDJqXjAmeOrjJ/8AEqdUip466n8G4UCgjM=
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230830221145-a6c6454d3697 h1:8obXF9sgfUyzCptiV0U/8XzTITUXcnwsnxANsbQcjCQ=
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230830221145-a6c6454d3697/go.mod h1:SRAJJuk4J4tNbIupmEm0+72xdriL2via2o+dN6k4gW4=
pkg.berachain.dev/polaris/eth v0.0.0-20230830221145-a6c6454d3697 h1:d+w0G+qOE9s1tYNOdMDaOmNbUCcP0EjUeXXMFT5jg2M=
pkg.berachain.dev/polaris/eth v0.0.0-20230830221145-a6c6454d3697/go.mod h1:Ry+Ldr32oNPMJVVfDCMYymqA2BlXsL5oyrZTVLxIT1A=
pkg.berachain.dev/polaris/lib v0.0.0-20230830221145-a6c6454d3697 h1:wgvK5vI7UlQYBs3kB2wZBgsxUjYK1lCV0LFJ4YF2u/4=
pkg.berachain.dev/polaris/lib v0.0.0-20230830221145-a6c6454d3697/go.mod h1:+3IkGuGJ/kV2d90ImTqPSrI9g+M6nV0eX1MAmtIfFZA=
pkg.berachain.dev/polaris/contracts v0.0.0-20230907215015-1928f4a21413 h1:OeMaIkzuqq8DsnDLpmngvsytIH+Zaj03u+tuUbQ1U68=
pkg.berachain.dev/polaris/contracts v0.0.0-20230907215015-1928f4a21413/go.mod h1:7egdVXsvyRMs9LspooyQCSDHtpPORRypbzI7OT0lKys=
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230907215015-1928f4a21413 h1:S+zX0WhdpZ965bM9pqG3aEpWET9MWKZrc2kgyrGitvs=
pkg.berachain.dev/polaris/e2e/localnet v0.0.0-20230907215015-1928f4a21413/go.mod h1:L3B+coflSwqvRaYph1DwzlP5F8C0/RJN1uy5rw8M5F4=
pkg.berachain.dev/polaris/eth v0.0.0-20230907215015-1928f4a21413 h1:6HRw5wdbWbil/bQiTNgcBmk/4kyYHEZaxuzXJqy9x44=
pkg.berachain.dev/polaris/eth v0.0.0-20230907215015-1928f4a21413/go.mod h1:UXClihDgn4IwQN2uDOJ2EYkvefSaHKoV5ISPZargjwQ=
pkg.berachain.dev/polaris/lib v0.0.0-20230907215015-1928f4a21413 h1:SUZhApl6lsnevApQFzjEu8QIhaWwfWbXBeYskyjeO8I=
pkg.berachain.dev/polaris/lib v0.0.0-20230907215015-1928f4a21413/go.mod h1:vXAda7BYoe3BYvv3t8yOKGQVosW2wsf7yJwULBVE6oM=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
9 changes: 6 additions & 3 deletions cosmos/testing/types/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ func NewMultiStore() types.MultiStore {
kvstore: map[string]interfaces.KVStore{},
MultiStoreMock: &mock.MultiStoreMock{},
}
ms.GetKVStoreFunc = func(storeKey types.StoreKey) types.KVStore {
ms.MultiStoreMock.GetKVStoreFunc = func(storeKey types.StoreKey) types.KVStore {
if store, ok := ms.kvstore[storeKey.String()]; ok {
return store
}
store := newTestKVStore()
ms.kvstore[storeKey.String()] = store
return store
}

ms.CacheMultiStoreFunc = func() types.CacheMultiStore {
ms.MultiStoreMock.CacheMultiStoreFunc = func() types.CacheMultiStore {
return NewCachedMultiStore(ms)
}

Expand Down Expand Up @@ -102,6 +101,10 @@ func NewCachedMultiStore(ms MultiStore) types.CacheMultiStore {
utils.MustGetAs[*TestKVStore](store).Write()
}
}

cached.CacheMultiStoreFunc = func() types.CacheMultiStore {
return NewCachedMultiStore(ms)
}
return cached
}

Expand Down
21 changes: 20 additions & 1 deletion cosmos/testing/utils/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package utils

import (
"errors"
"testing"

"cosmossdk.io/log"
Expand Down Expand Up @@ -55,6 +56,8 @@ import (
"pkg.berachain.dev/polaris/cosmos/types"
evmtypes "pkg.berachain.dev/polaris/cosmos/x/evm/types"
"pkg.berachain.dev/polaris/eth/common"
coremock "pkg.berachain.dev/polaris/eth/core/mock"
coretypes "pkg.berachain.dev/polaris/eth/core/types"
)

var (
Expand All @@ -68,7 +71,8 @@ var (

// NewContext creates a SDK context and mounts a mock multistore.
func NewContext() sdk.Context {
return sdk.NewContext(mock.NewMultiStore(), cometproto.Header{}, false, log.NewTestLogger(&testing.T{}))
ms := mock.NewMultiStore()
return sdk.NewContext(ms, cometproto.Header{}, false, log.NewTestLogger(&testing.T{}))
}

func NewContextWithMultiStore(ms storetypes.MultiStore) sdk.Context {
Expand Down Expand Up @@ -181,3 +185,18 @@ func GetEncodingConfig() testutil.TestEncodingConfig {
authz.AppModuleBasic{},
)
}

func MockQueryContext(height int64, _ bool) (sdk.Context, error) {
if height <= 0 {
return sdk.Context{}, errors.New("cannot query context at this height")
}
ctx := NewContext().WithBlockHeight(height)
header := coremock.GenerateHeaderAtHeight(height)
headerBz, err := coretypes.MarshalHeader(header)
if err != nil {
return sdk.Context{}, err
}
ctx.KVStore(EvmKey).Set([]byte{evmtypes.HeaderKey}, headerBz)
ctx.KVStore(EvmKey).Set(header.Hash().Bytes(), header.Number.Bytes())
return ctx, nil
}
2 changes: 1 addition & 1 deletion cosmos/x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("", func() {
k = keeper.NewKeeper(
ak, sk,
storetypes.NewKVStoreKey("evm"),
evmmempool.NewPolarisEthereumTxPool(),
evmmempool.NewWrappedGethTxPool(),
func() *ethprecompile.Injector {
return ethprecompile.NewPrecompiles([]ethprecompile.Registrable{sc}...)
},
Expand Down
27 changes: 23 additions & 4 deletions cosmos/x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,36 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/consensus/misc"

coretypes "pkg.berachain.dev/polaris/eth/core/types"
)

func (k *Keeper) BeginBlocker(ctx context.Context) error {
sCtx := sdk.UnwrapSDKContext(ctx)
if k.lock {
// unlock the keeper so that the Polaris services (EVM, JSON-RPC, TxPool, etc.) can start
k.lock = false
}

// Prepare the Polaris Ethereum block.
k.lock = false
k.polaris.Prepare(ctx, uint64(sCtx.BlockHeight()))
k.polaris.Prepare(ctx, uint64(sdk.UnwrapSDKContext(ctx).BlockHeight()))
return nil
}

func (k *Keeper) EndBlock(ctx context.Context) error {
// Finalize the Polaris Ethereum block.
return k.polaris.Finalize(ctx)
if err := k.polaris.Finalize(ctx); err != nil {
return err
}

// Prepare the txpool for the next pending block.
// TODO: move this.
chainConfig := k.host.GetConfigurationPlugin().ChainConfig()
k.GetHost().GetTxPoolPlugin().Prepare(
misc.CalcBaseFee(chainConfig, k.polaris.Blockchain().CurrentBlock()),
coretypes.LatestSignerForChainID(chainConfig.ChainID),
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
)

return nil
}
1 change: 1 addition & 0 deletions cosmos/x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, genState *core.Genesis) error {
plugin.InitGenesis(ctx, genState)
}
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions cosmos/x/evm/keeper/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var _ core.PolarisHostChain = (*host)(nil)
// It includes core.PolarisHostChain and functions that are called in other packages.
type Host interface {
core.PolarisHostChain
GetTxPoolPlugin() txpool.Plugin
GetAllPlugins() []plugins.Base
Setup(
storetypes.StoreKey,
Expand Down Expand Up @@ -87,7 +88,7 @@ func NewHost(
h.cp = configuration.NewPlugin(storeKey)
h.ep = engine.NewPlugin()
h.gp = gas.NewPlugin()
h.txp = txpool.NewPlugin(utils.MustGetAs[*mempool.EthTxPool](ethTxMempool))
h.txp = txpool.NewPlugin(utils.MustGetAs[*mempool.WrappedGethTxPool](ethTxMempool))
h.pcs = precompiles

return h
Expand All @@ -106,7 +107,6 @@ func (h *host) Setup(
h.pp = precompile.NewPlugin(h.pcs().GetPrecompiles(), h.sp)
// TODO: re-enable historical plugin using ABCI listener.
h.hp = historical.NewPlugin(h.cp, h.bp, nil, storeKey)
h.txp.SetNonceRetriever(h.sp)

// Set the query context function for the block and state plugins
h.sp.SetQueryContextFn(qc)
Expand Down Expand Up @@ -148,11 +148,11 @@ func (h *host) GetStatePlugin() core.StatePlugin {
}

// GetTxPoolPlugin returns the txpool plugin.
func (h *host) GetTxPoolPlugin() core.TxPoolPlugin {
func (h *host) GetTxPoolPlugin() txpool.Plugin {
return h.txp
}

// GetAllPlugins returns all the plugins.
func (h *host) GetAllPlugins() []plugins.Base {
return []plugins.Base{h.bp, h.cp, h.gp, h.hp, h.pp, h.sp, h.txp}
return []plugins.Base{h.bp, h.cp, h.ep, h.gp, h.hp, h.pp, h.sp, h.txp}
}
54 changes: 35 additions & 19 deletions cosmos/x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import (
"pkg.berachain.dev/polaris/cosmos/x/evm/plugins/block"
"pkg.berachain.dev/polaris/cosmos/x/evm/plugins/engine"
"pkg.berachain.dev/polaris/cosmos/x/evm/plugins/state"
"pkg.berachain.dev/polaris/cosmos/x/evm/plugins/txpool"
"pkg.berachain.dev/polaris/cosmos/x/evm/types"
"pkg.berachain.dev/polaris/eth/common"
"pkg.berachain.dev/polaris/eth/core"
ethprecompile "pkg.berachain.dev/polaris/eth/core/precompile"
"pkg.berachain.dev/polaris/eth/core/txpool"
ethlog "pkg.berachain.dev/polaris/eth/log"
"pkg.berachain.dev/polaris/eth/polar"
"pkg.berachain.dev/polaris/lib/utils"
)

type Keeper struct {
Expand Down Expand Up @@ -108,7 +110,7 @@ func (k *Keeper) Setup(
panic(err)
}

k.polaris = polar.NewWithNetworkingStack(cfg, k.host, node, ethlog.FuncHandler(
k.polaris = polar.NewWithNetworkingStack(cfg, node, ethlog.FuncHandler(
func(r *ethlog.Record) error {
polarisGethLogger := logger.With("module", "polaris-geth")
switch r.Lvl { //nolint:nolintlint,exhaustive // linter is bugged.
Expand All @@ -134,28 +136,42 @@ func (k *Keeper) GetHost() Host {
return k.host
}

// StartServices waits until the first block is being processed for the lock to unlock before
// starting the networking stack and txpool service.
func (k *Keeper) StartServices(clientContext client.Context) {
// Set the Polaris blockchain.
k.SetupBlockchain()

// spin lock until the first block is being processed
for ; k.lock; time.Sleep(1000 * time.Millisecond) { //nolint:gomnd // 100ms is fine.
continue
}

// start the txpool service
txpool := txpool.NewTxPool(
txpool.DefaultConfig, k.host.GetConfigurationPlugin().ChainConfig(), k.polaris.Blockchain(),
)
k.polaris.SetTxPool(txpool)
k.polaris.SetEngine(k.host.GetEnginePlugin())
k.host.GetTxPoolPlugin().Start(txpool, clientContext)
utils.MustGetAs[engine.Plugin](k.host.GetEnginePlugin()).Start(clientContext)

// start the networking stack (json-rpc, graphql, etc.)
if err := k.polaris.StartServices(); err != nil {
panic(err)
}
}

func (k *Keeper) SetupBlockchain() {
// Set the Polaris blockchain.
k.polaris.SetBlockchain(core.NewChain(k.host))
}

// GetPolaris returns the Polaris instance.
func (k *Keeper) GetPolaris() *polar.Polaris {
return k.polaris
}

func (k *Keeper) SetClientCtx(clientContext client.Context) {
k.host.GetTxPoolPlugin().(txpool.Plugin).SetClientContext(clientContext)
k.host.GetEnginePlugin().(engine.Plugin).Start(clientContext)

// TODO: move this
go func() {
// spin lock for a bit
for ; k.lock; time.Sleep(1 * time.Second) {
continue
}

if err := k.polaris.StartServices(); err != nil {
panic(err)
}
}()
}

// TODO: Remove these, because they're hacky af.
// Required temporarily for BGT plugin.
func (k *Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress) *big.Int {
Expand Down
4 changes: 3 additions & 1 deletion cosmos/x/evm/keeper/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var _ = Describe("Processor", func() {
k = keeper.NewKeeper(
ak, sk,
storetypes.NewKVStoreKey("evm"),
evmmempool.NewPolarisEthereumTxPool(),
evmmempool.NewWrappedGethTxPool(),
func() *ethprecompile.Injector {
return ethprecompile.NewPrecompiles([]ethprecompile.Registrable{sc}...)
},
Expand Down Expand Up @@ -124,6 +124,8 @@ var _ = Describe("Processor", func() {
ctx = ctx.WithBlockGasMeter(storetypes.NewGasMeter(100000000000000)).
WithKVGasConfig(storetypes.GasConfig{}).
WithBlockHeight(1)

k.SetupBlockchain()
err = k.BeginBlocker(ctx)
Expect(err).ToNot(HaveOccurred())
})
Expand Down
12 changes: 4 additions & 8 deletions cosmos/x/evm/plugins/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ import (
"pkg.berachain.dev/polaris/eth/core"
)

// Base is the base interface which all x/evm Polaris plugins must implement

type Base interface {
IsPlugin()
}

// HasGenesis represents the base class that all x/evm Polaris plugins which have
// InitGenesis or ExportGenesis methods must implement
// Base is the base interface which all x/evm Polaris plugins must implement.
type Base interface{}

// HasGenesis represents the base class that all x/evm Polaris plugins which have InitGenesis or
// ExportGenesis methods must implement.
type HasGenesis interface {
InitGenesis(sdk.Context, *core.Genesis)
ExportGenesis(sdk.Context, *core.Genesis)
Expand Down
Loading
Loading