-
Notifications
You must be signed in to change notification settings - Fork 1
/
repair.go
287 lines (249 loc) · 8.37 KB
/
repair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"github.com/tendermint/iavl"
tmstate "github.com/tendermint/tendermint/state"
tmstore "github.com/tendermint/tendermint/store"
tmtypes "github.com/tendermint/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"github.com/amolabs/amoabci/amo"
)
const (
merkleTreeCacheSize = 10000
)
func repair(amoRoot string, doFix bool, rewindMerkle bool) {
fmt.Println("Inspecting data root:", amoRoot)
//// open
fn := amoRoot + "/data/state.json"
flag := os.O_RDONLY
if doFix {
flag = os.O_RDWR
}
amoStateFile, err := os.OpenFile(fn, flag, 0)
if err != nil {
fmt.Println(err)
return
}
defer amoStateFile.Close()
merkleDB, err := tmdb.NewGoLevelDB("merkle", amoRoot+"/data")
if err != nil {
fmt.Println(err)
return
}
defer merkleDB.Close()
bsdb, err := tmdb.NewGoLevelDB("blockstore", amoRoot+"/data")
if err != nil {
fmt.Println(err)
return
}
defer bsdb.Close()
sdb, err := tmdb.NewGoLevelDB("state", amoRoot+"/data")
if err != nil {
fmt.Println(err)
return
}
defer sdb.Close()
//// load
amoState := amo.State{}
err = amoState.LoadFrom(amoStateFile)
if err != nil {
fmt.Println(err)
return
}
amoMt, err := iavl.NewMutableTree(merkleDB, merkleTreeCacheSize)
if err != nil {
fmt.Println(err)
return
}
amoMt.Load()
tmBlockStore := tmstore.NewBlockStore(bsdb)
tmBlockStoreState := tmstore.LoadBlockStoreStateJSON(bsdb)
tmState := tmstate.LoadState(sdb)
//// display
display(amoMt, amoState, tmBlockStoreState, tmState)
//// repair
fmt.Println("Repair TM state from block store...")
orgHeight := tmState.LastBlockHeight
nblk := tmBlockStore.LoadBlock(tmState.LastBlockHeight + 1)
for nblk != nil && matchStateAndBlock(tmState, nblk) == false {
// rewind to find the matching state
nblk = tmBlockStore.LoadBlock(tmState.LastBlockHeight)
if nblk == nil {
fmt.Println("Unable to get rewind target block")
return
}
// tmState.height will be nblk.height - 1 after this.
curBlk := tmBlockStore.LoadBlock(nblk.Height - 1)
tmState, err = stateFromBlock(tmState, nblk, curBlk)
if err != nil {
fmt.Println(err)
return
}
}
fmt.Printf("Rewinded TM state by %d blocks\n",
orgHeight-tmState.LastBlockHeight)
if tmBlockStoreState.Height > tmState.LastBlockHeight+1 {
tmBlockStoreState.Height = tmState.LastBlockHeight + 1
}
fmt.Println("Repair AMO merkle tree...")
ver, _ := amoMt.Load()
orgVersion := ver
appHash := amoMt.Hash()
prevHash := appHash // should be null?
// amoMt.Version equals to the block height where the app hash is written.
// Hence, greater by one than the last block height.
for ver > tmState.LastBlockHeight+1 {
prevHash = appHash
ver, err = amoMt.LoadVersion(ver - 1)
if err != nil {
fmt.Println(err)
return
}
appHash = amoMt.Hash()
if rewindMerkle || bytes.Equal(appHash, prevHash) {
// Ok. No change in appHash, so no change in index db. We don't
// have to touch index db. Rewind was safe in this case.
} else {
fmt.Println("Unable to rewind merkle db")
return
}
}
fmt.Printf("Rewinded AMO merkle db by %d\n", orgVersion-ver)
fmt.Println("Check TM state again with AMO merkle db...")
if !bytes.Equal(tmState.AppHash, appHash) {
tmState.AppHash = appHash
}
fmt.Println("Repair AMO state...")
amoState.MerkleVersion = amoMt.Version()
amoState.LastHeight = tmState.LastBlockHeight
amoState.Height = tmState.LastBlockHeight
amoState.LastAppHash = tmState.AppHash
amoState.AppHash = tmState.AppHash
// TODO: ProtocolVersion, CounterDue, NextDraftID
display(amoMt, amoState, tmBlockStoreState, tmState)
//// save
if !doFix {
fmt.Println("repair result not saved. provide -f flag to save.")
return
}
fmt.Println("saving repair result...")
amoState.SaveTo(amoStateFile)
amoMt.LoadVersionForOverwriting(amoState.MerkleVersion)
tmBlockStoreState.Save(bsdb)
tmstate.SaveState(sdb, tmState)
// cleaning up tx index
indexDB, err := tmdb.NewGoLevelDB("index", amoRoot+"/data")
if err != nil {
fmt.Println(err)
return
}
defer indexDB.Close()
// If validator set has not changed, then these dbs have no change either.
// indexDelegator = tmdb.NewPrefixDB(indexDB, []byte("delegator")
// indexValidator = tmdb.NewPrefixDB(indexDB, []byte("validator")
// indexEffStake = tmdb.NewPrefixDB(indexDB, []byte("effstake")
indexBlockTx := tmdb.NewPrefixDB(indexDB, []byte("blocktx"))
indexTxBlock := tmdb.NewPrefixDB(indexDB, []byte("txblock"))
defer indexBlockTx.Close()
defer indexTxBlock.Close()
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(amoState.LastHeight+1))
iter, err := indexBlockTx.Iterator(b, nil)
for ; iter.Valid(); iter.Next() {
fmt.Println("cleaning height", binary.BigEndian.Uint64(iter.Key()))
indexBlockTx.DeleteSync(iter.Key())
}
iter, err = indexTxBlock.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
txHash := hex.EncodeToString(iter.Key())
h := int64(binary.BigEndian.Uint64(iter.Value()))
if h > amoState.LastHeight {
fmt.Println("cleaning tx", strings.ToUpper(txHash))
indexTxBlock.DeleteSync(iter.Key())
}
}
}
func matchStateAndBlock(tmState tmstate.State, blk *tmtypes.Block) bool {
if !tmState.LastBlockID.Equals(blk.LastBlockID) {
fmt.Println("LastBlockID mismatch!!!!")
return false
}
if !bytes.Equal(tmState.NextValidators.Hash(), blk.NextValidatorsHash) {
fmt.Println("NextValidatorsHash mismatch!!!!")
return false
}
if !bytes.Equal(tmState.Validators.Hash(), blk.ValidatorsHash) {
fmt.Println("ValidatorsHash mismatch!!!!")
return false
}
if !bytes.Equal(tmState.ConsensusParams.Hash(), blk.ConsensusHash) {
fmt.Println("ConsensusParamsHash mismatch!!!!")
return false
}
if !bytes.Equal(tmState.LastResultsHash, blk.LastResultsHash) {
fmt.Println("LastResultsHash mismatch!!!!")
return false
}
if !bytes.Equal(tmState.AppHash, blk.AppHash) {
fmt.Println("AppHash mismatch!!!!")
return false
}
return true
}
func stateFromBlock(tmState tmstate.State, nextBlk *tmtypes.Block, curBlk *tmtypes.Block) (tmstate.State, error) {
if tmState.LastHeightValidatorsChanged >= nextBlk.Height ||
tmState.LastHeightConsensusParamsChanged >= nextBlk.Height {
return tmState, errors.New("unable to rewind")
}
tmState.LastBlockHeight = nextBlk.Height - 1
tmState.LastBlockID = nextBlk.LastBlockID
if curBlk != nil {
tmState.LastBlockTime = curBlk.Time
}
// tmState.NextValidators
// tmState.Validators
// tmState.ConsensusParams
tmState.LastResultsHash = nextBlk.LastResultsHash
tmState.AppHash = nextBlk.AppHash
return tmState, nil
}
func display(mt *iavl.MutableTree, amoState amo.State, tmBsj tmstore.BlockStoreStateJSON, tmState tmstate.State) {
mtVersion := mt.Version()
fmt.Printf("AMO merkle tree ----------------------------\n")
fmt.Printf(" .Version = %d\n", mtVersion)
fmt.Printf(" .Hash = %X\n", mt.Hash())
mt.LazyLoadVersion(mtVersion - 1)
fmt.Printf(" .Hash (-1) = %X\n", mt.Hash())
mt.LazyLoadVersion(mtVersion - 2)
fmt.Printf(" .Hash (-2) = %X\n", mt.Hash())
mt.LazyLoadVersion(mtVersion - 3)
fmt.Printf(" .Hash (-3) = %X\n", mt.Hash())
mt.Load()
fmt.Printf("AMO state ----------------------------------\n")
fmt.Printf(" .MerkleVersion = %d\n", amoState.MerkleVersion)
fmt.Printf(" .LastHeight = %d\n", amoState.LastHeight)
fmt.Printf(" .Height = %d\n", amoState.Height)
fmt.Printf(" .LastAppHash = %X\n", amoState.LastAppHash)
fmt.Printf(" .AppHash = %X\n", amoState.AppHash)
fmt.Printf("TM block store state -----------------------\n")
fmt.Printf(" .height = %d\n", tmBsj.Height)
fmt.Printf("TM state -----------------------------------\n")
fmt.Printf(" .LastBlockHeight = %d\n", tmState.LastBlockHeight)
fmt.Printf(" .LastBlockID = %s\n", tmState.LastBlockID)
fmt.Printf(" .LastBlockTime = %s\n", tmState.LastBlockTime)
fmt.Printf(" .NextValidators = %X\n", tmState.NextValidators.Hash())
fmt.Printf(" .Validators = %X\n", tmState.Validators.Hash())
fmt.Printf(" .LastValidators = %X\n", tmState.LastValidators.Hash())
fmt.Printf(" (vals changed) = %d\n", tmState.LastHeightValidatorsChanged)
fmt.Printf(" .ConsensusParams = %X\n", tmState.ConsensusParams.Hash())
fmt.Printf(" (cons changed) = %d\n", tmState.LastHeightConsensusParamsChanged)
fmt.Printf(" .LastResultsHash = %X\n", tmState.LastResultsHash)
fmt.Printf(" .AppHash = %X\n", tmState.AppHash)
}