Skip to content

Commit 731bc58

Browse files
authored
Make state recover from empty if the recovery height is lower than the current state height (#513)
1 parent b480fe1 commit 731bc58

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

blockchain/blockchain.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package blockchain
99
import (
1010
"context"
1111
"math/big"
12+
"os"
1213
"strconv"
1314
"sync"
1415
"sync/atomic"
@@ -961,13 +962,26 @@ func (bc *blockchain) startExistingBlockchain(recoveryHeight uint64) error {
961962
}
962963
startHeight = 1
963964
}
964-
if recoveryHeight > 0 && startHeight <= recoveryHeight {
965+
if recoveryHeight > 0 {
965966
for bc.tipHeight > recoveryHeight {
966967
if err := bc.dao.deleteTipBlock(); err != nil {
967968
return err
968969
}
969970
bc.tipHeight--
970971
}
972+
if startHeight > bc.tipHeight {
973+
startHeight = 0
974+
// Delete existing state DB and reinitialize it
975+
if err := os.Remove(bc.config.Chain.TrieDBPath); err != nil {
976+
return errors.Wrap(err, "failed to delete existing state DB")
977+
}
978+
if err := DefaultStateFactoryOption()(bc, bc.config); err != nil {
979+
return errors.Wrap(err, "failed to reinitialize state DB")
980+
}
981+
if err := bc.sf.Start(context.Background()); err != nil {
982+
return errors.Wrap(err, "failed to start state factory")
983+
}
984+
}
971985
}
972986
for i := startHeight; i <= bc.tipHeight; i++ {
973987
blk, err := bc.getBlockByHeight(i)

blockchain/blockchain_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ func TestStartExistingBlockchain(t *testing.T) {
10981098
}()
10991099

11001100
require.NoError(addTestingTsfBlocks(bc))
1101-
require.True(5 == bc.TipHeight())
1101+
require.Equal(uint64(5), bc.TipHeight())
11021102

11031103
// delete state db and recover to tip
11041104
testutil.CleanupPath(t, testTriePath)
@@ -1114,7 +1114,7 @@ func TestStartExistingBlockchain(t *testing.T) {
11141114
height, _ := chain.sf.Height()
11151115
require.Equal(bc.TipHeight(), height)
11161116

1117-
// recover to height 3
1117+
// recover to height 3 from empty state DB
11181118
testutil.CleanupPath(t, testTriePath)
11191119
sf, err = factory.NewFactory(cfg, factory.DefaultTrieOption())
11201120
require.NoError(err)
@@ -1125,7 +1125,13 @@ func TestStartExistingBlockchain(t *testing.T) {
11251125
require.NoError(chain.startExistingBlockchain(3))
11261126
height, _ = chain.sf.Height()
11271127
require.Equal(bc.TipHeight(), height)
1128-
require.True(3 == height)
1128+
require.Equal(uint64(3), height)
1129+
1130+
// recover to height 2 from an existing state DB with Height 3
1131+
require.NoError(chain.startExistingBlockchain(2))
1132+
height, _ = chain.sf.Height()
1133+
require.Equal(bc.TipHeight(), height)
1134+
require.Equal(uint64(2), height)
11291135
}
11301136

11311137
func addCreatorToFactory(sf factory.Factory) error {

server/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
_ "go.uber.org/automaxprocs"
2626
"go.uber.org/zap"
2727

28+
"github.com/iotexproject/iotex-core/blockchain"
2829
"github.com/iotexproject/iotex-core/config"
2930
"github.com/iotexproject/iotex-core/pkg/log"
3031
"github.com/iotexproject/iotex-core/pkg/probe"
@@ -50,6 +51,7 @@ func main() {
5051
signal.Notify(stop, os.Interrupt)
5152
signal.Notify(stop, syscall.SIGTERM)
5253
ctx, cancel := context.WithCancel(context.Background())
54+
ctxWithValue := context.WithValue(ctx, blockchain.RecoveryHeightKey, uint64(recoveryHeight))
5355
stopped := make(chan struct{})
5456
livenessCtx, livenessCancel := context.WithCancel(context.Background())
5557

@@ -93,7 +95,7 @@ func main() {
9395
}
9496
}
9597

96-
itx.StartServer(ctx, svr, probeSvr, cfg)
98+
itx.StartServer(ctxWithValue, svr, probeSvr, cfg)
9799
close(stopped)
98100
<-livenessCtx.Done()
99101
}

0 commit comments

Comments
 (0)