-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
207 lines (164 loc) · 5.6 KB
/
main.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
package main
import (
"log"
"github.com/lomocoin/blockchain-parsing/lomocoin"
"github.com/lomocoin/blockchain-parsing/lib/others/blockdb"
"github.com/syndtr/goleveldb/leveldb"
"github.com/lomocoin/blockchain-parsing/events"
"encoding/csv"
"strconv"
"os"
"github.com/therecipe/qt/widgets"
"fmt"
"os/user"
)
func main() {
widgets.NewQApplication(len(os.Args), os.Args)
var (
echoGroup = widgets.NewQGroupBox2("BlockFile Path", nil)
blockChainFilePath = widgets.NewQLineEdit(nil)
)
blockChainFilePath.SetPlaceholderText("Enter the block file path")
var (
inputMaskGroup = widgets.NewQGroupBox2("Block LevelDB Path", nil)
blockLeveldbPath = widgets.NewQLineEdit(nil)
)
blockLeveldbPath.SetPlaceholderText("Enter the leveldb path")
var (
blockHeightIndexGroup = widgets.NewQGroupBox2("Block Height", nil)
blockStartIndex = widgets.NewQLineEdit(nil)
blockEndIndex = widgets.NewQLineEdit(nil)
)
blockStartIndex.SetPlaceholderText("Enter the start block height")
blockEndIndex.SetPlaceholderText("Enter the end block height")
var (
eventState = 2
radioButtonGrop = widgets.NewQButtonGroup(nil)
radioButtonBox = widgets.NewQGroupBox2("Event", nil)
radioButtonCsv = widgets.NewQRadioButton2("CSV", nil)
radioButtonConsole = widgets.NewQRadioButton2("Console", nil)
)
radioButtonConsole.SetChecked(true)
radioButtonGrop.AddButton(radioButtonCsv, 1)
radioButtonGrop.AddButton(radioButtonConsole, 2)
var textEditConsole = widgets.NewQTextEdit(nil)
textEditConsole.SetPlaceholderText("Console log......")
textEditConsole.SetMaximumHeight(300)
var echoLayout = widgets.NewQGridLayout(nil)
echoLayout.AddWidget3(blockChainFilePath, 1, 0, 1, 2, 0)
echoGroup.SetLayout(echoLayout)
echoGroup.SetMaximumHeight(100)
var inputMaskLayout = widgets.NewQGridLayout(nil)
inputMaskLayout.AddWidget3(blockLeveldbPath, 1, 0, 1, 2, 0)
inputMaskGroup.SetLayout(inputMaskLayout)
inputMaskGroup.SetMaximumHeight(100)
var radioLayout = widgets.NewQGridLayout(nil)
radioLayout.AddWidget(radioButtonCsv, 0, 0, 0)
radioLayout.AddWidget(radioButtonConsole, 0, 1, 0)
radioButtonBox.SetLayout(radioLayout)
radioButtonBox.SetMaximumHeight(100)
var blockHeightLayout = widgets.NewQGridLayout(nil)
blockHeightLayout.AddWidget(blockStartIndex, 0, 0, 0)
blockHeightLayout.AddWidget(blockEndIndex, 0, 1, 0)
blockHeightIndexGroup.SetLayout(blockHeightLayout)
blockHeightIndexGroup.SetMaximumHeight(100)
radioButtonGrop.ConnectButtonClicked2(func(id int) {
eventState = id;
})
var runButton = widgets.NewQPushButton2("RUN", nil)
runButton.ConnectClicked(func(checked bool) {
startApplication(blockChainFilePath.Text(), blockLeveldbPath.Text(), blockStartIndex.Text(), blockEndIndex.Text(), eventState, textEditConsole)
})
var layout = widgets.NewQGridLayout2()
layout.AddWidget(echoGroup, 0, 0, 0)
layout.AddWidget(inputMaskGroup, 0, 1, 0)
layout.AddWidget3(radioButtonBox, 1, 0, 1, 2, 0)
layout.AddWidget3(blockHeightIndexGroup, 2, 0, 1, 2, 0)
layout.AddWidget3(runButton, 3, 0, 1, 2, 0)
layout.AddWidget3(textEditConsole, 4, 0, 1, 2, 0)
var window = widgets.NewQMainWindow(nil, 0)
window.SetWindowTitle("LomoCoin BlockChain Parsing")
window.SetMaximumWidth(800)
window.SetMinimumWidth(800)
window.SetMaximumHeight(650)
window.SetMinimumHeight(650)
var centralWidget = widgets.NewQWidget(window, 0)
centralWidget.SetLayout(layout)
window.SetCentralWidget(centralWidget)
window.Show()
widgets.QApplication_Exec()
}
func startApplication(blockFilePath string, levelPath string, startHeight string, endHeight string, state int, console *widgets.QTextEdit) {
Magic := [4]byte{0xa6, 0xb8, 0xc9, 0xd5}
try(func() {
BlockDatabase := blockdb.NewBlockDB(blockFilePath, Magic)
db, leveldbErr := leveldb.OpenFile(levelPath, nil)
if leveldbErr != nil {
panic("Leveldb failed to open, please check the path Or delete the LOCK file under the leveldb directory.")
}
start_block, _ := strconv.Atoi(startHeight)
end_block, _ := strconv.Atoi(endHeight)
if start_block < 1 {
panic("starting height must be greater than 0")
}
var writeObj *csv.Writer
// csv
if state == 1 {
user, userError := user.Current()
if nil == userError {
file, err := os.Create(user.HomeDir + "/Desktop/block.csv")
if err != nil {
panic(err)
}
defer file.Close()
file.WriteString("\xEF\xBB\xBF")
writeObj = csv.NewWriter(file)
writeObj.Write([]string{"block_index", "block_hash", "tx_time", "tx_id", "tx_in", "tx_out"})
} else {
panic(userError)
}
}
for blockIndex := 0; blockIndex <= end_block; blockIndex++ {
dat, er := BlockDatabase.FetchNextBlock()
if dat == nil || er != nil {
log.Println("END of DB file")
break
}
bl, er := lomocoin.NewBlock(dat[:])
if er != nil {
println("Block inconsistent:", er.Error())
break
}
if blockIndex < start_block {
continue
}
dbBlockHeight := bl.LeveldbFindBlockHeightWhereHash(db, bl.Hash.Hash)
// Fork block
if blockIndex != dbBlockHeight {
blockIndex = dbBlockHeight
}
console.InsertPlainText(fmt.Sprintf("Current block height: %v \n", blockIndex))
bl.BuildTxList()
if state == 1 {
events.WriteCSV(bl, blockIndex, writeObj)
}
if state == 2 {
events.Put(bl, console)
}
}
os.Remove(levelPath+"/LOCK")
if state == 1 {
console.InsertPlainText("\nFile export completion!\n")
}
}, func(er interface{}) {
console.InsertPlainText(fmt.Sprintf("\nError: %v \n", er))
})
}
func try(function func(), handler func(interface{})) {
defer func() {
if error := recover(); error != nil {
handler(error)
}
}()
function()
}