Skip to content

Commit

Permalink
Make state recover from empty if the recovery height is lower than th…
Browse files Browse the repository at this point in the history
…e current state height (#513)
  • Loading branch information
lizhefeng authored Feb 6, 2019
1 parent b480fe1 commit 731bc58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
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 {
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

0 comments on commit 731bc58

Please sign in to comment.