Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make state recover from empty if the recovery height is lower than the current state height #513

Merged
merged 2 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package blockchain
import (
"context"
"math/big"
"os"
"strconv"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -961,13 +962,26 @@ func (bc *blockchain) startExistingBlockchain(recoveryHeight uint64) error {
}
startHeight = 1
}
if recoveryHeight > 0 && startHeight <= recoveryHeight {
if recoveryHeight > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add unit test to cover this case.

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)
Expand Down
12 changes: 9 additions & 3 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ func TestStartExistingBlockchain(t *testing.T) {
}()

require.NoError(addTestingTsfBlocks(bc))
require.True(5 == bc.TipHeight())
require.Equal(uint64(5), bc.TipHeight())

// delete state db and recover to tip
testutil.CleanupPath(t, testTriePath)
Expand All @@ -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)
Expand All @@ -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.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.Equal(uint64(2), height)
}

func addCreatorToFactory(sf factory.Factory) error {
Expand Down
4 changes: 3 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())

Expand Down Expand Up @@ -93,7 +95,7 @@ func main() {
}
}

itx.StartServer(ctx, svr, probeSvr, cfg)
itx.StartServer(ctxWithValue, svr, probeSvr, cfg)
close(stopped)
<-livenessCtx.Done()
}
Expand Down