From 0ad0a6b2526213a6540854f3cbc36964bab81c66 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:07:43 +0200 Subject: [PATCH 1/4] Feat: adjust linter settings to report unused code --- .golangci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index a05e86b39..2fd5fc032 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,6 +19,28 @@ linters-settings: block-size: 2 stylecheck: initialisms: ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS"] + unused: + # Mark all struct fields that have been written to as used. + # Default: true + field-writes-are-uses: true + # Treat IncDec statement (e.g. `i++` or `i--`) as both read and write operation instead of just write. + # Default: false + post-statements-are-reads: true + # Mark all exported identifiers as used.g + # Default: true + exported-is-used: false + # Mark all exported fields as used. + # default: true + exported-fields-are-used: false + # Mark all function parameters as used. + # default: true + parameters-are-used: false + # Mark all local variables as used. + # default: true + local-variables-are-used: false + # Mark all identifiers inside generated files as used. + # Default: true + generated-is-used: true depguard: rules: main: From 8258b7ada019a5ceb0c7df47e031cc998c1c830e Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:52:14 +0200 Subject: [PATCH 2/4] Feat: check if linter panic is gone --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 2fd5fc032..35f176928 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,7 +28,7 @@ linters-settings: post-statements-are-reads: true # Mark all exported identifiers as used.g # Default: true - exported-is-used: false + # exported-is-used: false # linter panics if this is set # Mark all exported fields as used. # default: true exported-fields-are-used: false From b0e61aa4a9c8eb7d5ccec98b5ea123b0c7d53589 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Mon, 22 Apr 2024 17:20:57 +0200 Subject: [PATCH 3/4] Refactor: removed unused database --- .golangci.yml | 22 ------- pkg/storage/database/database.go | 109 ------------------------------- 2 files changed, 131 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 35f176928..a05e86b39 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,28 +19,6 @@ linters-settings: block-size: 2 stylecheck: initialisms: ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS"] - unused: - # Mark all struct fields that have been written to as used. - # Default: true - field-writes-are-uses: true - # Treat IncDec statement (e.g. `i++` or `i--`) as both read and write operation instead of just write. - # Default: false - post-statements-are-reads: true - # Mark all exported identifiers as used.g - # Default: true - # exported-is-used: false # linter panics if this is set - # Mark all exported fields as used. - # default: true - exported-fields-are-used: false - # Mark all function parameters as used. - # default: true - parameters-are-used: false - # Mark all local variables as used. - # default: true - local-variables-are-used: false - # Mark all identifiers inside generated files as used. - # Default: true - generated-is-used: true depguard: rules: main: diff --git a/pkg/storage/database/database.go b/pkg/storage/database/database.go index 10b6188eb..6d6ada48d 100644 --- a/pkg/storage/database/database.go +++ b/pkg/storage/database/database.go @@ -1,17 +1,11 @@ package database import ( - "encoding/json" - "time" - hivedb "github.com/iotaledger/hive.go/db" "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/kvstore" "github.com/iotaledger/hive.go/kvstore/mapdb" "github.com/iotaledger/hive.go/kvstore/rocksdb" - "github.com/iotaledger/hive.go/runtime/event" - "github.com/iotaledger/hive.go/runtime/ioutils" - "github.com/iotaledger/iota-core/pkg/metrics" ) var ( @@ -28,109 +22,6 @@ var ( AllowedEnginesStorageAuto = append(AllowedEnginesStorage, hivedb.EngineAuto) ) -var ( - // ErrNothingToCleanUp is returned when nothing is there to clean up in the database. - ErrNothingToCleanUp = ierrors.New("Nothing to clean up in the databases") -) - -type Cleanup struct { - Start time.Time - End time.Time -} - -func (c *Cleanup) MarshalJSON() ([]byte, error) { - cleanup := struct { - Start int64 `json:"start"` - End int64 `json:"end"` - }{ - Start: 0, - End: 0, - } - - if !c.Start.IsZero() { - cleanup.Start = c.Start.Unix() - } - - if !c.End.IsZero() { - cleanup.End = c.End.Unix() - } - - return json.Marshal(cleanup) -} - -type Events struct { - Cleanup *event.Event1[*Cleanup] - Compaction *event.Event1[bool] -} - -// Database holds the underlying KVStore and database specific functions. -type Database struct { - databaseDir string - store kvstore.KVStore - engine hivedb.Engine - metrics *metrics.DatabaseMetrics - events *Events - compactionSupported bool - compactionRunningFunc func() bool -} - -// New creates a new Database instance. -func New(databaseDirectory string, kvStore kvstore.KVStore, engine hivedb.Engine, metrics *metrics.DatabaseMetrics, events *Events, compactionSupported bool, compactionRunningFunc func() bool) *Database { - return &Database{ - databaseDir: databaseDirectory, - store: kvStore, - engine: engine, - metrics: metrics, - events: events, - compactionSupported: compactionSupported, - compactionRunningFunc: compactionRunningFunc, - } -} - -// KVStore returns the underlying KVStore. -func (db *Database) KVStore() kvstore.KVStore { - return db.store -} - -// Engine returns the database engine. -func (db *Database) Engine() hivedb.Engine { - return db.engine -} - -// Metrics returns the database metrics. -func (db *Database) Metrics() *metrics.DatabaseMetrics { - return db.metrics -} - -// Events returns the events of the database. -func (db *Database) Events() *Events { - return db.events -} - -// CompactionSupported returns whether the database engine supports compaction. -func (db *Database) CompactionSupported() bool { - return db.compactionSupported -} - -// CompactionRunning returns whether a compaction is running. -func (db *Database) CompactionRunning() bool { - if db.compactionRunningFunc == nil { - return false - } - - return db.compactionRunningFunc() -} - -// Size returns the size of the database. -func (db *Database) Size() (int64, error) { - if db.engine == hivedb.EngineMapDB { - // in-memory database does not support this method. - return 0, nil - } - - return ioutils.FolderSize(db.databaseDir) -} - // CheckEngine is a wrapper around hivedb.CheckEngine to throw a custom error message in case of engine mismatch. func CheckEngine(dbPath string, createDatabaseIfNotExists bool, dbEngine hivedb.Engine, allowedEngines ...hivedb.Engine) (hivedb.Engine, error) { tmpAllowedEngines := AllowedEnginesDefault From 568b588328d00c7ea2982d8cd78c234418bf9527 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Mon, 22 Apr 2024 20:58:01 +0200 Subject: [PATCH 4/4] Refactor: removed more unused code --- pkg/toolset/toolset.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pkg/toolset/toolset.go b/pkg/toolset/toolset.go index 9e9fc97ef..96f4b9d56 100644 --- a/pkg/toolset/toolset.go +++ b/pkg/toolset/toolset.go @@ -8,7 +8,6 @@ import ( flag "github.com/spf13/pflag" - "github.com/iotaledger/hive.go/app/configuration" "github.com/iotaledger/hive.go/ierrors" ) @@ -135,21 +134,3 @@ func printJSON(obj interface{}) error { return nil } - -//nolint:unused // we will need it at a later point in time -func loadConfigFile(filePath string, parameters map[string]any) error { - config := configuration.New() - flagset := configuration.NewUnsortedFlagSet("", flag.ContinueOnError) - - for namespace, pointerToStruct := range parameters { - config.BindParameters(flagset, namespace, pointerToStruct) - } - - if err := config.LoadFile(filePath); err != nil { - return ierrors.Wrap(err, "loading config file failed") - } - - config.UpdateBoundParameters() - - return nil -}