From abf4fa58616571de162661072b9fd57a446058ee Mon Sep 17 00:00:00 2001 From: Igor Mandrigin <mandrigin@users.noreply.github.com> Date: Wed, 28 Oct 2020 13:17:18 +0100 Subject: [PATCH] Don't require MDBX if you don't specify it (#1313) * fix compilation * add mdbx flag to the tg binary * fix makefile * fixups * fix makefile * fix readme * fix linters * fix tests --- Makefile | 18 +++++++++++------- README.md | 39 --------------------------------------- ethdb/kv_lmdb.go | 6 ++++++ ethdb/kv_mdbx.go | 9 +++++++++ ethdb/mdbx/mdbx.go | 2 ++ ethdb/object_db.go | 16 +++++++++------- ethdb/object_db_nomdbx.go | 7 +++++++ 7 files changed, 44 insertions(+), 53 deletions(-) create mode 100644 ethdb/object_db_nomdbx.go diff --git a/Makefile b/Makefile index 3c7a57eb7c..f65ed5660e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GOBIN = $(CURDIR)/build/bin GOBUILD = env GO111MODULE=on go build -trimpath -GOTEST = go test ./... -p 1 +GOTEST = go test ./... -p 1 --tags 'mdbx' LATEST_COMMIT ?= $(shell git log -n 1 origin/master --pretty=format:"%H") ifeq ($(LATEST_COMMIT),) @@ -31,19 +31,19 @@ docker-compose: docker-compose up geth: - $(GOBUILD) -o $(GOBIN)/tg -ldflags "-X main.gitCommit=${GIT_COMMIT}" ./cmd/tg + $(GOBUILD) -o $(GOBIN)/tg -tags "mdbx" -ldflags "-X main.gitCommit=${GIT_COMMIT}" ./cmd/tg @echo "Done building." @echo "Run \"$(GOBIN)/tg\" to launch turbo-geth." tg: @echo "Building mdbx" cd ethdb/mdbx/dist/ && make clean && make libmdbx.a && cat config.h - $(GOBUILD) -o $(GOBIN)/tg -ldflags "-X main.gitCommit=${GIT_COMMIT}" ./cmd/tg + $(GOBUILD) -o $(GOBIN)/tg -tags "mdbx" -ldflags "-X main.gitCommit=${GIT_COMMIT}" ./cmd/tg @echo "Done building." @echo "Run \"$(GOBIN)/tg\" to launch turbo-geth." hack: - $(GOBUILD) -o $(GOBIN)/hack ./cmd/hack + $(GOBUILD) -o $(GOBIN)/hack -tags "mdbx" ./cmd/hack @echo "Done building." @echo "Run \"$(GOBIN)/hack\" to launch hack." @@ -102,8 +102,8 @@ headers: @echo "Run \"$(GOBIN)/integration\" to run headers download PoC." db-tools: - $(GOBUILD) -o $(GOBIN)/lmdb_stat github.com/ledgerwatch/lmdb-go/cmd/lmdb_stat - $(GOBUILD) -o $(GOBIN)/lmdb_copy github.com/ledgerwatch/lmdb-go/cmd/lmdb_copy + $(GOBUILD) -o $(GOBIN)/lmdb_stat -tags "mdbx" github.com/ledgerwatch/lmdb-go/cmd/lmdb_stat + $(GOBUILD) -o $(GOBIN)/lmdb_copy -tags "mdbx" github.com/ledgerwatch/lmdb-go/cmd/lmdb_copy cd ethdb/mdbx/dist/ && make tools cp ethdb/mdbx/dist/mdbx_stat $(GOBIN) @@ -125,7 +125,7 @@ test-lmdb: semantics/z3/build/libz3.a TEST_DB=lmdb $(GOTEST) test-mdbx: semantics/z3/build/libz3.a ethdb/mdbx/dist/libmdbx.a - TEST_DB=mdbx $(GOTEST) + TEST_DB=mdbx $(GOTEST_MDBX) lint: lintci @@ -133,19 +133,23 @@ lintci: semantics/z3/build/libz3.a ethdb/mdbx/dist/libmdbx.a @echo "--> Running linter for code diff versus commit $(LATEST_COMMIT)" @./build/bin/golangci-lint run \ --new-from-rev=$(LATEST_COMMIT) \ + --build-tags="mdbx" \ --config ./.golangci/step1.yml \ --exclude "which can be annoying to use" @./build/bin/golangci-lint run \ --new-from-rev=$(LATEST_COMMIT) \ + --build-tags="mdbx" \ --config ./.golangci/step2.yml @./build/bin/golangci-lint run \ --new-from-rev=$(LATEST_COMMIT) \ + --build-tags="mdbx" \ --config ./.golangci/step3.yml @./build/bin/golangci-lint run \ --new-from-rev=$(LATEST_COMMIT) \ + --build-tags="mdbx" \ --config ./.golangci/step4.yml lintci-deps: diff --git a/README.md b/README.md index 3e589b07ce..c9360c3ddc 100644 --- a/README.md +++ b/README.md @@ -251,42 +251,3 @@ it impacts performance - one of main TG optimisations: "reduce Disk random acces "Blocks Execution stage" still does much random reads - this is reason why it's slowest stage. We do not recommend run multiple genesis syncs on same Disk. If genesis sync passed, then it's fine to run multiple TG on same Disk. - -## 2. `go run` doesn't work `no such file or directory: <>./ethdb/mdbx/dist/libmdbx.a` - -If you are trying to run our binaries with `go run` you might end up with an issue like that - -``` -> go run ./cmd/integration print_stages --chaindata /Volumes/OWC1/ddd-dev/tg/chaindata -go: downloading github.com/spf13/cobra v1.1.1 -# github.com/ethereum/evmc/v7/bindings/go/evmc -host.c:33:20: warning: unused function 'go_exported_functions_type_checks' [-Wunused-function] -# github.com/ledgerwatch/turbo-geth/ethdb/mdbx -clang: error: no such file or directory: '<..>/turbo-geth/ethdb/mdbx/dist/libmdbx.a' -``` - -To fix that, do `make all` and after that `go run` should work successfully. - -``` -> make all -<...> - -> go run ./cmd/integration print_stages --chaindata /Volumes/OWC_DISK_3/ddd-dev/tg/chaindata -# github.com/ethereum/evmc/v7/bindings/go/evmc -host.c:33:20: warning: unused function 'go_exported_functions_type_checks' [-Wunused-function] -Headers 11104855 -BlockHashes 11104855 -Bodies 11098821 -Senders 11098816 -Execution 11098816 -IntermediateHashes 11098816 -HashState 11098816 -AccountHistoryIndex 11098816 -StorageHistoryIndex 11098816 -LogIndex 11098816 -CallTraces 0 -TxLookup 11098816 -TxPool 11098816 -Finish 11098816 -INFO [10-28|09:29:17.907] database closed (LMDB) lmdb=chaindata -``` diff --git a/ethdb/kv_lmdb.go b/ethdb/kv_lmdb.go index 436b87dbf2..068a8dbc8b 100644 --- a/ethdb/kv_lmdb.go +++ b/ethdb/kv_lmdb.go @@ -18,6 +18,8 @@ import ( "github.com/prometheus/tsdb/fileutil" ) +var _ DbCopier = &LmdbKV{} + const ( NonExistingDBI dbutils.DBI = 999_999_999 ) @@ -276,6 +278,10 @@ type LmdbKV struct { func NewLMDB() LmdbOpts { return LmdbOpts{bucketsCfg: DefaultBucketConfigs} } +func (db *LmdbKV) NewDbWithTheSameParameters() *ObjectDatabase { + opts := db.opts + return NewObjectDatabase(NewLMDB().Set(opts).MustOpen()) +} // Close closes db // All transactions must be closed before closing the database. diff --git a/ethdb/kv_mdbx.go b/ethdb/kv_mdbx.go index b98b724d63..dd0df46ee8 100644 --- a/ethdb/kv_mdbx.go +++ b/ethdb/kv_mdbx.go @@ -1,3 +1,5 @@ +//+build mdbx + package ethdb import ( @@ -17,6 +19,8 @@ import ( "github.com/ledgerwatch/turbo-geth/log" ) +var _ DbCopier = &MdbxKV{} + type MdbxOpts struct { inMem bool exclusive bool @@ -261,6 +265,11 @@ func (db *MdbxKV) Close() { } +func (db *MdbxKV) NewDbWithTheSameParameters() *ObjectDatabase { + opts := db.opts + return NewObjectDatabase(NewMDBX().Set(opts).MustOpen()) +} + func (db *MdbxKV) DiskSize(_ context.Context) (uint64, error) { stats, err := db.env.Stat() if err != nil { diff --git a/ethdb/mdbx/mdbx.go b/ethdb/mdbx/mdbx.go index 1a78b8c59b..b805de93da 100644 --- a/ethdb/mdbx/mdbx.go +++ b/ethdb/mdbx/mdbx.go @@ -1,3 +1,5 @@ +// +build mdbx + /* Package lmdb provides bindings to the lmdb C API. The package bindings are fairly low level and are designed to provide a minimal interface that prevents diff --git a/ethdb/object_db.go b/ethdb/object_db.go index 3084e62312..0961031319 100644 --- a/ethdb/object_db.go +++ b/ethdb/object_db.go @@ -39,6 +39,10 @@ var ( dbPutTimer = metrics.NewRegisteredTimer("db/put", nil) ) +type DbCopier interface { + NewDbWithTheSameParameters() *ObjectDatabase +} + // ObjectDatabase - is an object-style interface of DB accessing type ObjectDatabase struct { kv KV @@ -321,13 +325,11 @@ func (db *ObjectDatabase) SetKV(kv KV) { func (db *ObjectDatabase) MemCopy() *ObjectDatabase { var mem *ObjectDatabase // Open the db and recover any potential corruptions - switch db.kv.(type) { - case *LmdbKV: - opts := db.kv.(*LmdbKV).opts - mem = NewObjectDatabase(NewLMDB().Set(opts).MustOpen()) - case *MdbxKV: - opts := db.kv.(*MdbxKV).opts - mem = NewObjectDatabase(NewMDBX().Set(opts).MustOpen()) + switch t := db.kv.(type) { + case DbCopier: + mem = t.NewDbWithTheSameParameters() + default: + panic(fmt.Sprintf("MemCopy is not implemented for type %T", t)) } if err := db.kv.View(context.Background(), func(readTx Tx) error { diff --git a/ethdb/object_db_nomdbx.go b/ethdb/object_db_nomdbx.go new file mode 100644 index 0000000000..62b6ad1a38 --- /dev/null +++ b/ethdb/object_db_nomdbx.go @@ -0,0 +1,7 @@ +//+build !mdbx + +package ethdb + +func NewMDBX() LmdbOpts { + panic("to use MDBX, compile with -tags 'mdbx'") +} -- GitLab