From 959d9989af4ff24ce4967ba2a534d73e052a517e Mon Sep 17 00:00:00 2001 From: lizhefeng Date: Tue, 5 Feb 2019 10:48:24 -0800 Subject: [PATCH 1/2] Make state recover from empty if the recovery height is lower than the current state height --- blockchain/blockchain.go | 16 +++++++++++++++- blockchain/blockchain_test.go | 12 +++++++++--- server/main.go | 4 +++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 12ab9f1d07..f064e6323d 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -9,6 +9,7 @@ package blockchain import ( "context" "math/big" + "os" "strconv" "sync" "sync/atomic" @@ -961,13 +962,26 @@ func (bc *blockchain) startExistingBlockchain(recoveryHeight uint64) error { } startHeight = 1 } - if recoveryHeight > 0 && startHeight <= recoveryHeight { + if recoveryHeight > 0 { for bc.tipHeight > recoveryHeight { if err := bc.dao.deleteTipBlock(); err != nil { return err } bc.tipHeight-- } + if startHeight > bc.tipHeight { + startHeight = 0 + // Delete existing state DB and reinitialize it + if err := os.Remove(bc.config.Chain.TrieDBPath); err != nil { + return errors.Wrap(err, "failed to delete existing state DB") + } + if err := DefaultStateFactoryOption()(bc, bc.config); err != nil { + return errors.Wrap(err, "failed to reinitialize state DB") + } + if err := bc.sf.Start(context.Background()); err != nil { + return errors.Wrap(err, "failed to start state factory") + } + } } for i := startHeight; i <= bc.tipHeight; i++ { blk, err := bc.getBlockByHeight(i) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index 03c961778b..ff3f57bfb6 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -1098,7 +1098,7 @@ func TestStartExistingBlockchain(t *testing.T) { }() require.NoError(addTestingTsfBlocks(bc)) - require.True(5 == bc.TipHeight()) + require.True(bc.TipHeight() == 5) // delete state db and recover to tip testutil.CleanupPath(t, testTriePath) @@ -1114,7 +1114,7 @@ func TestStartExistingBlockchain(t *testing.T) { height, _ := chain.sf.Height() require.Equal(bc.TipHeight(), height) - // recover to height 3 + // recover to height 3 from empty state DB testutil.CleanupPath(t, testTriePath) sf, err = factory.NewFactory(cfg, factory.DefaultTrieOption()) require.NoError(err) @@ -1125,7 +1125,13 @@ func TestStartExistingBlockchain(t *testing.T) { require.NoError(chain.startExistingBlockchain(3)) height, _ = chain.sf.Height() require.Equal(bc.TipHeight(), height) - require.True(3 == height) + require.True(height == 3) + + // recover to height 2 from an existing state DB with Height 3 + require.NoError(chain.startExistingBlockchain(2)) + height, _ = chain.sf.Height() + require.Equal(bc.TipHeight(), height) + require.True(height == 2) } func addCreatorToFactory(sf factory.Factory) error { diff --git a/server/main.go b/server/main.go index 7965cd9d84..995f52c0da 100644 --- a/server/main.go +++ b/server/main.go @@ -25,6 +25,7 @@ import ( _ "go.uber.org/automaxprocs" "go.uber.org/zap" + "github.com/iotexproject/iotex-core/blockchain" "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/pkg/log" "github.com/iotexproject/iotex-core/pkg/probe" @@ -50,6 +51,7 @@ func main() { signal.Notify(stop, os.Interrupt) signal.Notify(stop, syscall.SIGTERM) ctx, cancel := context.WithCancel(context.Background()) + ctxWithValue := context.WithValue(ctx, blockchain.RecoveryHeightKey, uint64(recoveryHeight)) stopped := make(chan struct{}) livenessCtx, livenessCancel := context.WithCancel(context.Background()) @@ -93,7 +95,7 @@ func main() { } } - itx.StartServer(ctx, svr, probeSvr, cfg) + itx.StartServer(ctxWithValue, svr, probeSvr, cfg) close(stopped) <-livenessCtx.Done() } From b8b0f914565dd28489ca7554adb2554e9b2aa67c Mon Sep 17 00:00:00 2001 From: lizhefeng Date: Tue, 5 Feb 2019 16:55:38 -0800 Subject: [PATCH 2/2] address comment --- blockchain/blockchain_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index ff3f57bfb6..fccb7d5f9e 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -1098,7 +1098,7 @@ func TestStartExistingBlockchain(t *testing.T) { }() require.NoError(addTestingTsfBlocks(bc)) - require.True(bc.TipHeight() == 5) + require.Equal(uint64(5), bc.TipHeight()) // delete state db and recover to tip testutil.CleanupPath(t, testTriePath) @@ -1125,13 +1125,13 @@ func TestStartExistingBlockchain(t *testing.T) { require.NoError(chain.startExistingBlockchain(3)) height, _ = chain.sf.Height() require.Equal(bc.TipHeight(), height) - require.True(height == 3) + require.Equal(uint64(3), height) // recover to height 2 from an existing state DB with Height 3 require.NoError(chain.startExistingBlockchain(2)) height, _ = chain.sf.Height() require.Equal(bc.TipHeight(), height) - require.True(height == 2) + require.Equal(uint64(2), height) } func addCreatorToFactory(sf factory.Factory) error {