2020-07-14 01:56:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2023-03-26 03:36:23 +00:00
|
|
|
"context"
|
2023-07-12 17:45:38 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-02-12 13:33:09 +00:00
|
|
|
"path/filepath"
|
2023-07-12 17:45:38 +00:00
|
|
|
"strings"
|
2021-04-19 07:25:26 +00:00
|
|
|
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2020-07-14 01:56:29 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-09-18 10:41:01 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2023-03-10 03:27:12 +00:00
|
|
|
|
2023-06-09 06:46:58 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
|
|
|
|
kv2 "github.com/ledgerwatch/erigon-lib/kv/mdbx"
|
|
|
|
|
2023-03-10 03:27:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
2023-06-02 03:35:26 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/state/temporal"
|
|
|
|
"github.com/ledgerwatch/erigon/core/systemcontracts"
|
2023-03-10 03:27:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/migrations"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
2020-07-14 01:56:29 +00:00
|
|
|
)
|
|
|
|
|
2023-07-12 17:45:38 +00:00
|
|
|
func expandHomeDir(dirpath string) string {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return dirpath
|
|
|
|
}
|
|
|
|
prefix := fmt.Sprintf("~%c", os.PathSeparator)
|
|
|
|
if strings.HasPrefix(dirpath, prefix) {
|
|
|
|
return filepath.Join(home, dirpath[len(prefix):])
|
|
|
|
} else if dirpath == "~" {
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
return dirpath
|
|
|
|
}
|
|
|
|
|
2020-07-14 01:56:29 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "integration",
|
2021-05-26 10:35:39 +00:00
|
|
|
Short: "long and heavy integration tests for Erigon",
|
2020-07-14 01:56:29 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2023-07-12 17:45:38 +00:00
|
|
|
datadirCli = expandHomeDir(datadirCli)
|
2021-04-19 07:25:26 +00:00
|
|
|
if chaindata == "" {
|
2022-06-07 04:00:37 +00:00
|
|
|
chaindata = filepath.Join(datadirCli, "chaindata")
|
2023-07-12 17:45:38 +00:00
|
|
|
} else {
|
|
|
|
chaindata = expandHomeDir(chaindata)
|
2021-04-19 07:25:26 +00:00
|
|
|
}
|
2020-07-14 01:56:29 +00:00
|
|
|
},
|
2020-08-20 03:52:27 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
2022-10-21 05:36:17 +00:00
|
|
|
defer debug.Exit()
|
2020-08-20 03:52:27 +00:00
|
|
|
},
|
2020-07-14 01:56:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
func RootCommand() *cobra.Command {
|
2022-10-21 05:36:17 +00:00
|
|
|
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging.Flags)
|
2020-08-19 11:46:20 +00:00
|
|
|
return rootCmd
|
2020-07-14 01:56:29 +00:00
|
|
|
}
|
2020-10-13 22:42:04 +00:00
|
|
|
|
2022-08-12 14:45:09 +00:00
|
|
|
func dbCfg(label kv.Label, path string) kv2.MdbxOpts {
|
2022-12-20 15:59:23 +00:00
|
|
|
const ThreadsLimit = 9_000
|
|
|
|
limiterB := semaphore.NewWeighted(ThreadsLimit)
|
2023-10-12 07:11:46 +00:00
|
|
|
opts := kv2.NewMDBX(log.New()).Path(path).Label(label).RoTxsLimiter(limiterB).Accede()
|
2023-10-22 01:57:07 +00:00
|
|
|
//if label == kv.ChainDB {
|
|
|
|
// opts = opts.MapSize(8 * datasize.TB)
|
|
|
|
//}
|
2022-06-09 02:45:30 +00:00
|
|
|
if databaseVerbosity != -1 {
|
|
|
|
opts = opts.DBVerbosity(kv.DBVerbosityLvl(databaseVerbosity))
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2023-05-10 18:36:27 +00:00
|
|
|
func openDB(opts kv2.MdbxOpts, applyMigrations bool, logger log.Logger) (kv.RwDB, error) {
|
2022-07-15 06:17:07 +00:00
|
|
|
// integration tool don't intent to create db, then easiest way to open db - it's pass mdbx.Accede flag, which allow
|
|
|
|
// to read all options from DB, instead of overriding them
|
2023-10-12 07:11:46 +00:00
|
|
|
opts = opts.Accede()
|
2022-12-02 14:01:43 +00:00
|
|
|
|
2022-06-09 02:45:30 +00:00
|
|
|
db := opts.MustOpen()
|
2020-10-28 09:52:15 +00:00
|
|
|
if applyMigrations {
|
2022-06-09 02:45:30 +00:00
|
|
|
migrator := migrations.NewMigrator(opts.GetLabel())
|
|
|
|
has, err := migrator.HasPendingMigrations(db)
|
2021-03-01 04:02:22 +00:00
|
|
|
if err != nil {
|
2023-05-07 06:28:15 +00:00
|
|
|
return nil, err
|
2020-10-28 09:52:15 +00:00
|
|
|
}
|
2021-03-01 04:02:22 +00:00
|
|
|
if has {
|
2023-05-10 18:36:27 +00:00
|
|
|
logger.Info("Re-Opening DB in exclusive mode to apply DB migrations")
|
2021-03-01 04:02:22 +00:00
|
|
|
db.Close()
|
2022-06-09 02:45:30 +00:00
|
|
|
db = opts.Exclusive().MustOpen()
|
2023-05-10 18:36:27 +00:00
|
|
|
if err := migrator.Apply(db, datadirCli, logger); err != nil {
|
2023-05-07 06:28:15 +00:00
|
|
|
return nil, err
|
2021-03-01 04:02:22 +00:00
|
|
|
}
|
|
|
|
db.Close()
|
2022-06-09 02:45:30 +00:00
|
|
|
db = opts.MustOpen()
|
2021-03-01 04:02:22 +00:00
|
|
|
}
|
2020-10-28 09:52:15 +00:00
|
|
|
}
|
2023-03-26 03:36:23 +00:00
|
|
|
|
|
|
|
if opts.GetLabel() == kv.ChainDB {
|
|
|
|
var h3 bool
|
|
|
|
var err error
|
|
|
|
if err := db.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
h3, err = kvcfg.HistoryV3.Enabled(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2023-05-07 06:28:15 +00:00
|
|
|
return nil, err
|
2023-03-26 03:36:23 +00:00
|
|
|
}
|
|
|
|
if h3 {
|
2023-08-18 16:10:35 +00:00
|
|
|
_, _, agg := allSnapshots(context.Background(), db, logger)
|
2023-06-02 03:35:26 +00:00
|
|
|
tdb, err := temporal.New(db, agg, systemcontracts.SystemContractCodeLookup[chain])
|
2023-03-26 03:36:23 +00:00
|
|
|
if err != nil {
|
2023-05-07 06:28:15 +00:00
|
|
|
return nil, err
|
2023-03-26 03:36:23 +00:00
|
|
|
}
|
|
|
|
db = tdb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:28:15 +00:00
|
|
|
return db, nil
|
2020-10-28 09:52:15 +00:00
|
|
|
}
|