From 328705f5797a88a415d01ae42b34e26aa3c23c4a Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Sat, 10 Jul 2021 16:21:20 -0700 Subject: [PATCH] [lbry] claimtrie: support replay of chain changes --- blockchain/chain.go | 2 +- claimtrie/claimtrie.go | 45 +++++++++++++++++++++++++++++-------- claimtrie/claimtrie_test.go | 11 ++++----- claimtrie/cmd/cmd/chain.go | 3 +-- claimtrie/config/config.go | 3 +++ 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index 3a00eba5..a213d929 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -1814,7 +1814,7 @@ func New(config *Config) (*BlockChain, error) { return nil, err } - ct, err := claimtrie.New() + ct, err := claimtrie.New(true) if err != nil { return nil, err } diff --git a/claimtrie/claimtrie.go b/claimtrie/claimtrie.go index 37e0a220..f27294f7 100644 --- a/claimtrie/claimtrie.go +++ b/claimtrie/claimtrie.go @@ -6,9 +6,10 @@ import ( "runtime" "sort" - "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/claimtrie/block" "github.com/btcsuite/btcd/claimtrie/block/blockrepo" + "github.com/btcsuite/btcd/claimtrie/chain" + "github.com/btcsuite/btcd/claimtrie/chain/chainrepo" "github.com/btcsuite/btcd/claimtrie/change" "github.com/btcsuite/btcd/claimtrie/config" "github.com/btcsuite/btcd/claimtrie/merkletrie" @@ -18,6 +19,8 @@ import ( "github.com/btcsuite/btcd/claimtrie/param" "github.com/btcsuite/btcd/claimtrie/temporal" "github.com/btcsuite/btcd/claimtrie/temporal/temporalrepo" + + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" ) @@ -27,6 +30,9 @@ type ClaimTrie struct { // Repository for reported block hashes (debugging purpose). reportedBlockRepo block.Repo + // Repository for raw changes recieved from chain. + chainRepo chain.Repo + // Repository for calculated block hashes. blockRepo block.Repo @@ -46,13 +52,13 @@ type ClaimTrie struct { // Write buffer for batching changes written to repo. // flushed before block is appended. - // changes []change.Change + changes []change.Change // Registrered cleanup functions which are invoked in the Close() in reverse order. cleanups []func() error } -func New() (*ClaimTrie, error) { +func New(record bool) (*ClaimTrie, error) { cfg := config.GenerateConfig(param.ClaimtrieDataFolder) var cleanups []func() error @@ -126,12 +132,25 @@ func New() (*ClaimTrie, error) { merkleTrie: trie, height: previousHeight, - - reportedBlockRepo: reportedBlockRepo, - - cleanups: cleanups, } + if record { + chainRepo, err := chainrepo.NewPebble(cfg.ChainRepoPebble.Path) + if err != nil { + return nil, fmt.Errorf("new change change repo: %w", err) + } + cleanups = append(cleanups, chainRepo.Close) + ct.chainRepo = chainRepo + + reportedBlockRepo, err := blockrepo.NewPebble(cfg.ReportedBlockRepoPebble.Path) + if err != nil { + return nil, fmt.Errorf("new reported block repo: %w", err) + } + cleanups = append(cleanups, reportedBlockRepo.Close) + ct.reportedBlockRepo = reportedBlockRepo + } + ct.cleanups = cleanups + return ct, nil } @@ -210,6 +229,15 @@ func (ct *ClaimTrie) SpendSupport(name []byte, op wire.OutPoint, id node.ClaimID func (ct *ClaimTrie) AppendBlock() error { ct.height++ + + if len(ct.changes) > 0 && ct.chainRepo != nil { + err := ct.chainRepo.Save(ct.height, ct.changes) + if err != nil { + return fmt.Errorf("chain change repo save: %w", err) + } + ct.changes = ct.changes[:0] + } + names, err := ct.nodeManager.IncrementHeightTo(ct.height) if err != nil { return fmt.Errorf("node mgr increment: %w", err) @@ -348,7 +376,6 @@ func (ct *ClaimTrie) Close() error { return fmt.Errorf("cleanup: %w", err) } } - ct.cleanups = nil return nil } @@ -362,7 +389,7 @@ func (ct *ClaimTrie) forwardNodeChange(chg change.Change) error { return fmt.Errorf("node manager handle change: %w", err) } - //ct.changes = append(ct.changes, chg) + ct.changes = append(ct.changes, chg) return nil } diff --git a/claimtrie/claimtrie_test.go b/claimtrie/claimtrie_test.go index dd0cbc43..decbea0a 100644 --- a/claimtrie/claimtrie_test.go +++ b/claimtrie/claimtrie_test.go @@ -3,10 +3,11 @@ package claimtrie import ( "testing" - "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/claimtrie/merkletrie" "github.com/btcsuite/btcd/claimtrie/node" "github.com/btcsuite/btcd/claimtrie/param" + + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/stretchr/testify/require" @@ -34,7 +35,7 @@ func TestFixedHashes(t *testing.T) { r := require.New(t) setup(t) - ct, err := New() + ct, err := New(false) r.NoError(err) defer func() { err = ct.Close() @@ -74,7 +75,7 @@ func TestNormalizationFork(t *testing.T) { setup(t) param.NormalizedNameForkHeight = 2 - ct, err := New() + ct, err := New(false) r.NoError(err) r.NotNil(ct) defer func() { @@ -138,7 +139,7 @@ func TestActivationsOnNormalizationFork(t *testing.T) { setup(t) param.NormalizedNameForkHeight = 4 - ct, err := New() + ct, err := New(false) r.NoError(err) r.NotNil(ct) defer func() { @@ -184,7 +185,7 @@ func TestNormalizationSortOrder(t *testing.T) { // alas, it's now part of our history; we hereby test it to keep it that way setup(t) param.NormalizedNameForkHeight = 2 - ct, err := New() + ct, err := New(false) r.NoError(err) r.NotNil(ct) defer func() { diff --git a/claimtrie/cmd/cmd/chain.go b/claimtrie/cmd/cmd/chain.go index 96744da5..ae98fdba 100644 --- a/claimtrie/cmd/cmd/chain.go +++ b/claimtrie/cmd/cmd/chain.go @@ -110,8 +110,7 @@ var chainReplayCmd = &cobra.Command{ return fmt.Errorf("open block repo: %w", err) } - // FIXME: pass record flag into claimtrie - ct, err := claimtrie.New() + ct, err := claimtrie.New(false) if err != nil { return fmt.Errorf("create claimtrie: %w", err) } diff --git a/claimtrie/config/config.go b/claimtrie/config/config.go index ee3b3d50..504cf074 100644 --- a/claimtrie/config/config.go +++ b/claimtrie/config/config.go @@ -18,6 +18,9 @@ func GenerateConfig(folder string) *DBConfig { MerkleTrieRepoPebble: pebbleConfig{ Path: filepath.Join(folder, "merkletrie_pebble_db"), }, + ChainRepoPebble: pebbleConfig{ + Path: filepath.Join(folder, "chain_pebble_db"), + }, ReportedBlockRepoPebble: pebbleConfig{ Path: filepath.Join(folder, "reported_blocks_pebble_db"), },