Skip to content

Commit

Permalink
mdbx: use lable to defautl table cfg detection (#12338)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Nov 22, 2024
1 parent eb89d4d commit a7c728f
Show file tree
Hide file tree
Showing 74 changed files with 251 additions and 417 deletions.
3 changes: 2 additions & 1 deletion cl/antiquary/state_antiquary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "embed"
"testing"

"github.com/erigontech/erigon-lib/kv"
"github.com/stretchr/testify/require"

"github.com/erigontech/erigon-lib/common/datadir"
Expand All @@ -36,7 +37,7 @@ import (
)

func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postState *state.CachingBeaconState) {
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
reader := tests.LoadChain(blocks, postState, db, t)

ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions cl/beacon/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
blocks, preState, postState = tests.GetCapellaRandom()
}
fcu = mock_services2.NewForkChoiceStorageMock(t)
db = memdb.NewTestDB(t)
blobDb := memdb.NewTestDB(t)
db = memdb.NewTestDB(t, kv.ChainDB)
blobDb := memdb.NewTestDB(t, kv.ChainDB)
var reader *tests.MockBlockReader
reader = tests.LoadChain(blocks, postState, db, t)
firstBlockRoot, _ := blocks[0].Block.HashSSZ()
Expand Down
2 changes: 1 addition & 1 deletion cl/persistence/beacon_indicies/indicies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func setupTestDB(t *testing.T) kv.RwDB {
// Create an in-memory SQLite DB for testing purposes
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
return db
}

Expand Down
2 changes: 1 addition & 1 deletion cl/persistence/blob_storage/blob_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func setupTestDB(t *testing.T) kv.RwDB {
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
return db
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

"github.com/erigontech/erigon-lib/kv"
"github.com/stretchr/testify/require"

libcommon "github.com/erigontech/erigon-lib/common"
Expand All @@ -36,7 +37,7 @@ import (
)

func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postState *state.CachingBeaconState) {
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
reader := tests.LoadChain(blocks, postState, db, t)

ctx := context.Background()
Expand Down
3 changes: 2 additions & 1 deletion cl/phase1/network/services/block_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

"github.com/erigontech/erigon-lib/kv"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

Expand All @@ -34,7 +35,7 @@ import (
)

func setupBlockService(t *testing.T, ctrl *gomock.Controller) (BlockService, *synced_data.SyncedDataManager, *eth_clock.MockEthereumClock, *mock_services.ForkChoiceStorageMock) {
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
cfg := &clparams.MainnetBeaconConfig
syncedDataManager := synced_data.NewSyncedDataManager(cfg, true)
ethClock := eth_clock.NewMockEthereumClock(ctrl)
Expand Down
9 changes: 0 additions & 9 deletions cl/phase1/stages/stage_history_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/memdb"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/cl/antiquary"
"github.com/erigontech/erigon/cl/persistence/beacon_indicies"
Expand Down Expand Up @@ -267,14 +266,6 @@ func SpawnStageHistoryDownload(cfg StageHistoryReconstructionCfg, ctx context.Co
}
cfg.downloader.SetThrottle(cfg.backfillingThrottling) // throttle to 0.6 second for backfilling
cfg.downloader.SetNeverSkip(false)
// If i do not give it a database, erigon lib starts to cry uncontrollably
db2 := memdb.New(cfg.tmpdir)
defer db2.Close()
tx2, err := db2.BeginRw(ctx)
if err != nil {
return err
}
defer tx2.Rollback()
isBackfilling.Store(true)

cfg.logger.Info("Ready to insert history, waiting for sync cycle to finish")
Expand Down
5 changes: 3 additions & 2 deletions cl/sentinel/handlers/blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math"
"testing"

"github.com/erigontech/erigon-lib/kv"
"github.com/golang/snappy"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestBlobsByRangeHandler(t *testing.T) {
require.NoError(t, err)

peersPool := peers.NewPool()
blobDb := memdb.NewTestDB(t)
blobDb := memdb.NewTestDB(t, kv.ChainDB)
_, indiciesDB := setupStore(t)
store := tests.NewMockBlockReader()

Expand Down Expand Up @@ -209,7 +210,7 @@ func TestBlobsByIdentifiersHandler(t *testing.T) {
require.NoError(t, err)

peersPool := peers.NewPool()
blobDb := memdb.NewTestDB(t)
blobDb := memdb.NewTestDB(t, kv.ChainDB)
_, indiciesDB := setupStore(t)
store := tests.NewMockBlockReader()

Expand Down
2 changes: 1 addition & 1 deletion cl/sentinel/handlers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

func setupStore(t *testing.T) (freezeblocks.BeaconSnapshotReader, kv.RwDB) {
db := memdb.NewTestDB(t)
db := memdb.NewTestDB(t, kv.ChainDB)
return tests.NewMockBlockReader(), db
}

Expand Down
2 changes: 1 addition & 1 deletion cl/sentinel/sentinel_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (

func loadChain(t *testing.T) (db kv.RwDB, blocks []*cltypes.SignedBeaconBlock, f afero.Fs, preState, postState *state.CachingBeaconState, reader *tests.MockBlockReader) {
blocks, preState, postState = tests.GetPhase0Random()
db = memdb.NewTestDB(t)
db = memdb.NewTestDB(t, kv.ChainDB)
reader = tests.LoadChain(blocks, postState, db, t)

ctx := context.Background()
Expand Down
3 changes: 2 additions & 1 deletion cl/spectest/consensus_tests/fork_choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math"
"testing"

"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon/spectest"

"github.com/spf13/afero"
Expand Down Expand Up @@ -204,7 +205,7 @@ func (b *ForkChoice) Run(t *testing.T, root fs.FS, c spectest.TestCase) (err err
emitters := beaconevents.NewEventEmitter()
_, beaconConfig := clparams.GetConfigsByNetwork(clparams.MainnetNetwork)
ethClock := eth_clock.NewEthereumClock(genesisState.GenesisTime(), genesisState.GenesisValidatorsRoot(), beaconConfig)
blobStorage := blob_storage.NewBlobStore(memdb.New("/tmp"), afero.NewMemMapFs(), math.MaxUint64, &clparams.MainnetBeaconConfig, ethClock)
blobStorage := blob_storage.NewBlobStore(memdb.New("/tmp", kv.ChainDB), afero.NewMemMapFs(), math.MaxUint64, &clparams.MainnetBeaconConfig, ethClock)

validatorMonitor := monitor.NewValidatorMonitor(false, nil, nil, nil)
forkStore, err := forkchoice.NewForkChoiceStore(
Expand Down
14 changes: 8 additions & 6 deletions cmd/caplin/caplin1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ func OpenCaplinDatabase(ctx context.Context,
os.MkdirAll(dbPath, 0700)
os.MkdirAll(dataDirIndexer, 0700)

db := mdbx.NewMDBX(log.New()).Path(dbPath).Label(kv.CaplinDB).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.ChaindataTablesCfg
}).MustOpen()
blobDB := mdbx.NewMDBX(log.New()).Path(blobDbPath).Label(kv.CaplinDB).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.ChaindataTablesCfg
}).MustOpen()
db := mdbx.New(kv.CaplinDB, log.New()).Path(dbPath).
WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg { //TODO: move Caplin tables to own tables cofig
return kv.ChaindataTablesCfg
}).MustOpen()
blobDB := mdbx.New(kv.CaplinDB, log.New()).Path(blobDbPath).
WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.ChaindataTablesCfg
}).MustOpen()

tx, err := db.BeginRw(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/devnet/services/polygon/proofgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type requestGenerator struct {
}

func newRequestGenerator(sentry *mock.MockSentry, chain *core.ChainPack) (*requestGenerator, error) {
db := memdb.New("")
db := memdb.New("", kv.ChainDB)
if err := db.Update(context.Background(), func(tx kv.RwTx) error {
if err := rawdb.WriteHeader(tx, chain.TopBlock.Header()); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ func checkChainName(ctx context.Context, dirs datadir.Dirs, chainName string) er
if !exists {
return nil
}
db, err := mdbx.NewMDBX(log.New()).
Path(dirs.Chaindata).Label(kv.ChainDB).
db, err := mdbx.New(kv.ChainDB, log.New()).
Path(dirs.Chaindata).
Accede().
Open(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/erigontech/erigon-lib/config3"
"github.com/erigontech/erigon-lib/kv/temporal"
"io"
"math/big"
"os"
Expand All @@ -34,6 +32,10 @@ import (
"testing"
"time"

"github.com/erigontech/erigon-lib/config3"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/temporal"

"github.com/erigontech/erigon-lib/common/datadir"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -159,7 +161,7 @@ func runCmd(ctx *cli.Context) error {
} else {
debugLogger = logger.NewStructLogger(logconfig)
}
db := memdb.New(os.TempDir())
db := memdb.New(os.TempDir(), kv.ChainDB)
defer db.Close()
if ctx.String(GenesisFlag.Name) != "" {
gen := readGenesis(ctx.String(GenesisFlag.Name))
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"path/filepath"

"github.com/c2h5oh/datasize"
"github.com/erigontech/erigon-lib/kv"
mdbx2 "github.com/erigontech/mdbx-go/mdbx"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -133,7 +134,7 @@ func aggregateResultsFromStateTests(
//this DB is shared. means:
// - faster sequential tests: don't need create/delete db
// - less parallelism: multiple processes can open same DB but only 1 can create rw-transaction (other will wait when 1-st finish)
_db := mdbx.NewMDBX(log.New()).
_db := mdbx.New(kv.ChainDB, log.New()).
Path(dirs.Chaindata).
Flags(func(u uint) uint {
return u | mdbx2.UtterlyNoSync | mdbx2.NoMetaSync | mdbx2.NoMemInit | mdbx2.WriteMap
Expand Down
2 changes: 1 addition & 1 deletion cmd/hack/db/lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ func defragSteps(filename string, bucketsCfg kv.TableCfg, generateFs ...func(kv.
}
defer os.RemoveAll(dir)
var db kv.RwDB
db, err = kv2.NewMDBX(logger).Path(dir).WithTableCfg(func(kv.TableCfg) kv.TableCfg {
db, err = kv2.New(kv.ChainDB, logger).Path(dir).WithTableCfg(func(kv.TableCfg) kv.TableCfg {
return bucketsCfg
}).Open(context.Background())
if err != nil {
Expand Down
Loading

0 comments on commit a7c728f

Please sign in to comment.