[lbry] rework claimtrie config and param

This commit is contained in:
Roy Lee 2021-07-12 16:08:47 -07:00
parent ceba136a70
commit 6b55968ccd
11 changed files with 74 additions and 63 deletions

View file

@ -148,7 +148,7 @@ func btcdMain(serverChan chan<- *server) error {
return nil return nil
} }
param.SetNetwork(activeNetParams.Params.Net, netName(activeNetParams)) // prep the claimtrie params param.SetNetwork(activeNetParams.Params.Net) // prep the claimtrie params
// Create server and start it. // Create server and start it.
server, err := newServer(cfg.Listeners, cfg.AgentBlacklist, server, err := newServer(cfg.Listeners, cfg.AgentBlacklist,

View file

@ -3,6 +3,7 @@ package claimtrie
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"path/filepath"
"runtime" "runtime"
"sort" "sort"
@ -58,18 +59,17 @@ type ClaimTrie struct {
cleanups []func() error cleanups []func() error
} }
func New(record bool) (*ClaimTrie, error) { func New(cfg config.Config) (*ClaimTrie, error) {
cfg := config.GenerateConfig(param.ClaimtrieDataFolder)
var cleanups []func() error var cleanups []func() error
blockRepo, err := blockrepo.NewPebble(cfg.BlockRepoPebble.Path) blockRepo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.BlockRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new block repo: %w", err) return nil, fmt.Errorf("new block repo: %w", err)
} }
cleanups = append(cleanups, blockRepo.Close) cleanups = append(cleanups, blockRepo.Close)
temporalRepo, err := temporalrepo.NewPebble(cfg.TemporalRepoPebble.Path) temporalRepo, err := temporalrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.TemporalRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new temporal repo: %w", err) return nil, fmt.Errorf("new temporal repo: %w", err)
} }
@ -77,7 +77,7 @@ func New(record bool) (*ClaimTrie, error) {
// Initialize repository for changes to nodes. // Initialize repository for changes to nodes.
// The cleanup is delegated to the Node Manager. // The cleanup is delegated to the Node Manager.
nodeRepo, err := noderepo.NewPebble(cfg.NodeRepoPebble.Path) nodeRepo, err := noderepo.NewPebble(filepath.Join(cfg.DataDir, cfg.NodeRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new node repo: %w", err) return nil, fmt.Errorf("new node repo: %w", err)
} }
@ -91,7 +91,7 @@ func New(record bool) (*ClaimTrie, error) {
// Initialize repository for MerkleTrie. // Initialize repository for MerkleTrie.
// The cleanup is delegated to MerkleTrie. // The cleanup is delegated to MerkleTrie.
trieRepo, err := merkletrierepo.NewPebble(cfg.MerkleTrieRepoPebble.Path) trieRepo, err := merkletrierepo.NewPebble(filepath.Join(cfg.DataDir, cfg.MerkleTrieRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new trie repo: %w", err) return nil, fmt.Errorf("new trie repo: %w", err)
} }
@ -128,15 +128,15 @@ func New(record bool) (*ClaimTrie, error) {
height: previousHeight, height: previousHeight,
} }
if record { if cfg.Record {
chainRepo, err := chainrepo.NewPebble(cfg.ChainRepoPebble.Path) chainRepo, err := chainrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ChainRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new change change repo: %w", err) return nil, fmt.Errorf("new change change repo: %w", err)
} }
cleanups = append(cleanups, chainRepo.Close) cleanups = append(cleanups, chainRepo.Close)
ct.chainRepo = chainRepo ct.chainRepo = chainRepo
reportedBlockRepo, err := blockrepo.NewPebble(cfg.ReportedBlockRepoPebble.Path) reportedBlockRepo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ReportedBlockRepoPebble.Path))
if err != nil { if err != nil {
return nil, fmt.Errorf("new reported block repo: %w", err) return nil, fmt.Errorf("new reported block repo: %w", err)
} }

View file

@ -3,6 +3,7 @@ package claimtrie
import ( import (
"testing" "testing"
"github.com/btcsuite/btcd/claimtrie/config"
"github.com/btcsuite/btcd/claimtrie/merkletrie" "github.com/btcsuite/btcd/claimtrie/merkletrie"
"github.com/btcsuite/btcd/claimtrie/node" "github.com/btcsuite/btcd/claimtrie/node"
"github.com/btcsuite/btcd/claimtrie/param" "github.com/btcsuite/btcd/claimtrie/param"
@ -13,9 +14,11 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var cfg = config.DefaultConfig
func setup(t *testing.T) { func setup(t *testing.T) {
param.SetNetwork(wire.TestNet, "") param.SetNetwork(wire.TestNet)
param.ClaimtrieDataFolder = t.TempDir() cfg.DataDir = t.TempDir()
} }
func b(s string) []byte { func b(s string) []byte {
@ -35,7 +38,7 @@ func TestFixedHashes(t *testing.T) {
r := require.New(t) r := require.New(t)
setup(t) setup(t)
ct, err := New(false) ct, err := New(cfg)
r.NoError(err) r.NoError(err)
defer func() { defer func() {
err = ct.Close() err = ct.Close()
@ -75,7 +78,7 @@ func TestNormalizationFork(t *testing.T) {
setup(t) setup(t)
param.NormalizedNameForkHeight = 2 param.NormalizedNameForkHeight = 2
ct, err := New(false) ct, err := New(cfg)
r.NoError(err) r.NoError(err)
r.NotNil(ct) r.NotNil(ct)
defer func() { defer func() {
@ -139,7 +142,7 @@ func TestActivationsOnNormalizationFork(t *testing.T) {
setup(t) setup(t)
param.NormalizedNameForkHeight = 4 param.NormalizedNameForkHeight = 4
ct, err := New(false) ct, err := New(cfg)
r.NoError(err) r.NoError(err)
r.NotNil(ct) r.NotNil(ct)
defer func() { defer func() {
@ -185,7 +188,7 @@ func TestNormalizationSortOrder(t *testing.T) {
// alas, it's now part of our history; we hereby test it to keep it that way // alas, it's now part of our history; we hereby test it to keep it that way
setup(t) setup(t)
param.NormalizedNameForkHeight = 2 param.NormalizedNameForkHeight = 2
ct, err := New(false) ct, err := New(cfg)
r.NoError(err) r.NoError(err)
r.NotNil(ct) r.NotNil(ct)
defer func() { defer func() {

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"log" "log"
"path/filepath"
"strconv" "strconv"
"github.com/btcsuite/btcd/claimtrie/block/blockrepo" "github.com/btcsuite/btcd/claimtrie/block/blockrepo"
@ -32,7 +33,7 @@ var blockLastCmd = &cobra.Command{
Short: "Show the Merkle Hash of the last block", Short: "Show the Merkle Hash of the last block",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
repo, err := blockrepo.NewPebble(localConfig.ReportedBlockRepoPebble.Path) repo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ReportedBlockRepoPebble.Path))
if err != nil { if err != nil {
log.Fatalf("can't open reported block repo: %s", err) log.Fatalf("can't open reported block repo: %s", err)
} }
@ -59,7 +60,7 @@ var blockListCmd = &cobra.Command{
Args: cobra.RangeArgs(1, 2), Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
repo, err := blockrepo.NewPebble(localConfig.ReportedBlockRepoPebble.Path) repo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ReportedBlockRepoPebble.Path))
if err != nil { if err != nil {
log.Fatalf("can't open reported block repo: %s", err) log.Fatalf("can't open reported block repo: %s", err)
} }
@ -104,7 +105,7 @@ var blockNameCmd = &cobra.Command{
Args: cobra.RangeArgs(2, 2), Args: cobra.RangeArgs(2, 2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
repo, err := blockrepo.NewPebble(localConfig.BlockRepoPebble.Path) repo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.BlockRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("can't open reported block repo: %w", err) return fmt.Errorf("can't open reported block repo: %w", err)
} }
@ -129,7 +130,7 @@ var blockNameCmd = &cobra.Command{
return fmt.Errorf("load previous height: %w", err) return fmt.Errorf("load previous height: %w", err)
} }
trieRepo, err := merkletrierepo.NewPebble(localConfig.MerkleTrieRepoPebble.Path) trieRepo, err := merkletrierepo.NewPebble(filepath.Join(cfg.DataDir, cfg.MerkleTrieRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("can't open merkle trie repo: %w", err) return fmt.Errorf("can't open merkle trie repo: %w", err)
} }
@ -140,7 +141,7 @@ var blockNameCmd = &cobra.Command{
if len(args) > 1 { if len(args) > 1 {
trie.Dump(args[1], param.AllClaimsInMerkleForkHeight >= int32(height)) trie.Dump(args[1], param.AllClaimsInMerkleForkHeight >= int32(height))
} else { } else {
tmpRepo, err := temporalrepo.NewPebble(localConfig.TemporalRepoPebble.Path) tmpRepo, err := temporalrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.TemporalRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("can't open temporal repo: %w", err) return fmt.Errorf("can't open temporal repo: %w", err)
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"path/filepath"
"strconv" "strconv"
"github.com/btcsuite/btcd/claimtrie" "github.com/btcsuite/btcd/claimtrie"
@ -11,6 +12,7 @@ import (
"github.com/btcsuite/btcd/claimtrie/block/blockrepo" "github.com/btcsuite/btcd/claimtrie/block/blockrepo"
"github.com/btcsuite/btcd/claimtrie/chain/chainrepo" "github.com/btcsuite/btcd/claimtrie/chain/chainrepo"
"github.com/btcsuite/btcd/claimtrie/change" "github.com/btcsuite/btcd/claimtrie/change"
"github.com/btcsuite/btcd/claimtrie/config"
"github.com/btcsuite/btcd/claimtrie/node" "github.com/btcsuite/btcd/claimtrie/node"
"github.com/cockroachdb/pebble" "github.com/cockroachdb/pebble"
@ -48,7 +50,7 @@ var chainDumpCmd = &cobra.Command{
} }
} }
chainRepo, err := chainrepo.NewPebble(localConfig.ChainRepoPebble.Path) chainRepo, err := chainrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ChainRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("open node repo: %w", err) return fmt.Errorf("open node repo: %w", err)
} }
@ -93,24 +95,25 @@ var chainReplayCmd = &cobra.Command{
} }
} }
err = os.RemoveAll(localConfig.NodeRepoPebble.Path) err = os.RemoveAll(filepath.Join(cfg.DataDir, cfg.NodeRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("delete node repo: %w", err) return fmt.Errorf("delete node repo: %w", err)
} }
fmt.Printf("Deleted node repo\n") fmt.Printf("Deleted node repo\n")
chainRepo, err := chainrepo.NewPebble(localConfig.ChainRepoPebble.Path) chainRepo, err := chainrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ChainRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("open change repo: %w", err) return fmt.Errorf("open change repo: %w", err)
} }
reportedBlockRepo, err := blockrepo.NewPebble(localConfig.ReportedBlockRepoPebble.Path) reportedBlockRepo, err := blockrepo.NewPebble(filepath.Join(cfg.DataDir, cfg.ReportedBlockRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("open block repo: %w", err) return fmt.Errorf("open block repo: %w", err)
} }
ct, err := claimtrie.New(false) cfg := config.DefaultConfig
ct, err := claimtrie.New(cfg)
if err != nil { if err != nil {
return fmt.Errorf("create claimtrie: %w", err) return fmt.Errorf("create claimtrie: %w", err)
} }

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"math" "math"
"path/filepath"
"strconv" "strconv"
"github.com/btcsuite/btcd/claimtrie/node" "github.com/btcsuite/btcd/claimtrie/node"
@ -29,7 +30,7 @@ var nodeDumpCmd = &cobra.Command{
Args: cobra.RangeArgs(1, 2), Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
repo, err := noderepo.NewPebble(localConfig.NodeRepoPebble.Path) repo, err := noderepo.NewPebble(filepath.Join(cfg.DataDir, cfg.NodeRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("open node repo: %w", err) return fmt.Errorf("open node repo: %w", err)
} }
@ -66,7 +67,7 @@ var nodeReplayCmd = &cobra.Command{
Args: cobra.RangeArgs(1, 2), Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
repo, err := noderepo.NewPebble(localConfig.NodeRepoPebble.Path) repo, err := noderepo.NewPebble(filepath.Join(cfg.DataDir, cfg.NodeRepoPebble.Path))
if err != nil { if err != nil {
return fmt.Errorf("open node repo: %w", err) return fmt.Errorf("open node repo: %w", err)
} }

View file

@ -8,11 +8,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var localConfig *config.DBConfig var cfg = config.DefaultConfig
func init() { func init() {
param.SetNetwork(wire.MainNet, "mainnet") param.SetNetwork(wire.MainNet)
localConfig = config.GenerateConfig(param.ClaimtrieDataFolder)
} }
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{

View file

@ -23,7 +23,7 @@ var temporalCmd = &cobra.Command{
func runListNodes(cmd *cobra.Command, args []string) error { func runListNodes(cmd *cobra.Command, args []string) error {
repo, err := temporalrepo.NewPebble(localConfig.TemporalRepoPebble.Path) repo, err := temporalrepo.NewPebble(cfg.TemporalRepoPebble.Path)
if err != nil { if err != nil {
log.Fatalf("can't open reported block repo: %s", err) log.Fatalf("can't open reported block repo: %s", err)
} }

View file

@ -2,33 +2,43 @@ package config
import ( import (
"path/filepath" "path/filepath"
"github.com/btcsuite/btcutil"
) )
func GenerateConfig(folder string) *DBConfig { var DefaultConfig = Config{
return &DBConfig{ Record: false,
BlockRepoPebble: pebbleConfig{ RamTrie: false,
Path: filepath.Join(folder, "blocks_pebble_db"),
}, DataDir: filepath.Join(btcutil.AppDataDir("chain", false), "data", "mainnet", "claim_dbs"),
NodeRepoPebble: pebbleConfig{
Path: filepath.Join(folder, "node_change_pebble_db"), BlockRepoPebble: pebbleConfig{
}, Path: "blocks_pebble_db",
TemporalRepoPebble: pebbleConfig{ },
Path: filepath.Join(folder, "temporal_pebble_db"), NodeRepoPebble: pebbleConfig{
}, Path: "node_change_pebble_db",
MerkleTrieRepoPebble: pebbleConfig{ },
Path: filepath.Join(folder, "merkletrie_pebble_db"), TemporalRepoPebble: pebbleConfig{
}, Path: "temporal_pebble_db",
ChainRepoPebble: pebbleConfig{ },
Path: filepath.Join(folder, "chain_pebble_db"), MerkleTrieRepoPebble: pebbleConfig{
}, Path: "merkletrie_pebble_db",
ReportedBlockRepoPebble: pebbleConfig{ },
Path: filepath.Join(folder, "reported_blocks_pebble_db"), ChainRepoPebble: pebbleConfig{
}, Path: "chain_pebble_db",
} },
ReportedBlockRepoPebble: pebbleConfig{
Path: "reported_blocks_pebble_db",
},
} }
// DBConfig is the container of all configurations. // Config is the container of all configurations.
type DBConfig struct { type Config struct {
Record bool
RamTrie bool
DataDir string
BlockRepoPebble pebbleConfig BlockRepoPebble pebbleConfig
NodeRepoPebble pebbleConfig NodeRepoPebble pebbleConfig
TemporalRepoPebble pebbleConfig TemporalRepoPebble pebbleConfig

View file

@ -45,7 +45,7 @@ func TestSimpleAddClaim(t *testing.T) {
r := require.New(t) r := require.New(t)
param.SetNetwork(wire.TestNet, "") param.SetNetwork(wire.TestNet)
repo, err := noderepo.NewPebble(t.TempDir()) repo, err := noderepo.NewPebble(t.TempDir())
r.NoError(err) r.NoError(err)

View file

@ -2,8 +2,6 @@ package param
import ( import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"path/filepath"
) )
var ( var (
@ -20,16 +18,12 @@ var (
NormalizedNameForkHeight int32 NormalizedNameForkHeight int32
AllClaimsInMerkleForkHeight int32 AllClaimsInMerkleForkHeight int32
ClaimtrieDataFolder string
) )
func SetNetwork(net wire.BitcoinNet, subfolder string) { func SetNetwork(net wire.BitcoinNet) {
MaxActiveDelay = 4032 MaxActiveDelay = 4032
ActiveDelayFactor = 32 ActiveDelayFactor = 32
MaxNodeManagerCacheSize = 16000 MaxNodeManagerCacheSize = 16000
appDir := btcutil.AppDataDir("chain", false)
ClaimtrieDataFolder = filepath.Join(appDir, "data", subfolder, "claim_dbs")
switch net { switch net {
case wire.MainNet: case wire.MainNet: