forked from elastos/Elastos.ELA
/
RollBack.go
62 lines (52 loc) · 1.41 KB
/
RollBack.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
package main
import (
"Elastos.ELA/common"
"Elastos.ELA/common/serialization"
"Elastos.ELA/core/store/ChainStore"
"Elastos.ELA/core/store/LevelDBStore"
"bytes"
"fmt"
"os"
"strconv"
)
func main() {
wantHeight, _ := strconv.Atoi(os.Args[1])
wantheightInt32 := uint32(wantHeight)
st, err := LevelDBStore.NewLevelDBStore("Chain")
if err != nil {
fmt.Println("connect leveldb failed!")
fmt.Println(err)
}
chain := &ChainStore.ChainStore{
IStore: st,
}
key := bytes.NewBuffer(nil)
key.WriteByte(byte(0x40))
heightByte, _ := st.Get(key.Bytes())
r := bytes.NewReader(heightByte)
var blockHash common.Uint256
blockHash.Deserialize(r)
realHeight, _ := serialization.ReadUint32(r)
if wantheightInt32 >= realHeight {
fmt.Println("Current height of blockchain is %d, you can't do this, man.", realHeight)
return
}
for i := realHeight; i > wantheightInt32; i-- {
fmt.Println("i is", i)
blockHashBefore, _ := chain.GetBlockHash(i)
fmt.Println("blockhash before")
fmt.Println(blockHashBefore)
block, _ := chain.GetBlock(blockHashBefore)
chain.BatchInit()
chain.RollbackTrimemedBlock(block)
chain.RollbackBlockHash(block)
chain.RollbackTransactions(block)
chain.RollbackUnspendUTXOs(block)
chain.RollbackUnspend(block)
chain.RollbackCurrentBlock(block)
chain.BatchFinish()
blockHashAfter, _ := chain.GetBlockHash(i)
fmt.Println("blockhash after")
fmt.Println(blockHashAfter)
}
}