-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.go
199 lines (181 loc) · 6.74 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
package main
import (
"fmt"
"net/http"
"time"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/sec-bit/zkPoD-node/net/rlpx"
)
var Logger = NewSimpleLogger("zkPoD-node")
var ETHKey *keystore.Key
var BConf BasicConfig
var ServerAddr *rlpx.Addr
var AliceNodeStart bool = false
var BobNodeStart bool = false
var AliceTxMap map[string]Transaction
var BobTxMap map[string]BobTransaction
var DepositLockMap map[string]int64
func main() {
var err error
// read command
config := initCli()
// read basic config in file.
BConf, err = readBasicFile(config.BasicConfig)
if err != nil {
panic(err)
}
if config.Operation == OPERATION_START {
initMap()
err = saveBasicFile(BConf)
if err != nil {
panic(err)
}
ETHKey, err = readKeyStore(BConf, config.Password)
if err != nil {
panic(err)
}
b := preparePOD(BConf.InitKeyPath)
if !b {
panic("failed to prepare setup bin")
}
err := initDir(BConf.BobDir, BConf.AliceDir)
if err != nil {
panic(err)
}
DBConn, err = connectSQLite(DEFAULT_DB_PATH)
if err != nil {
panic(err)
}
SetupPOD()
} else {
HandleCmdReq(config)
}
}
// SetupPOD setups api server and node-
func SetupPOD() {
Log := Logger.NewSessionLogger()
fmt.Printf(" ██╗ ████████╗ ████████╗ \n")
fmt.Printf(" ██║ ██╔════██╗ ██╔════███╗\n")
fmt.Printf("███████╗ ██║ ██╗ ██║ ██║ ███╗ ██║ ██║\n")
fmt.Printf("╚══███╔╝ ██║ ██╔╝ ████████╔╝ ██╔══██╗ ██║ ██║\n")
fmt.Printf(" ███╔╝ █████╔╝ ██╔═════╝ ██╔╝ ██║ ██║ ██║\n")
fmt.Printf(" ███╔╝ ██╔═██╗ ██║ ██╗ ██╔╝ ██║ ███╔╝\n")
fmt.Printf("███████╗ ██║ ██╗ ██║ ╚███╔═╝ ████████╔═╝\n")
fmt.Printf("╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚══╝ ╚═══════╝\n")
go SetupAPIServe(Log)
SetupNode(Log)
}
//SetupAPIServe setups the api server for command line requests and web requests.
func SetupAPIServe(Log ILogger) {
http.HandleFunc("/config/read", ReadCfgAPIHandler)
http.HandleFunc("/config/reload", ReLoadConfigAPIHandler)
http.HandleFunc("/s/publish/init", InitPublishDataAPIHandler)
http.HandleFunc("/s/publish", PublishDataAPIHandler)
http.HandleFunc("/s/close", CloseDataAPIHandler)
http.HandleFunc("/s/withdraw/data", AliceWithdrawFromDataAPIHandler)
http.HandleFunc("/s/withdraw/tx", AliceWithdrawFromTxAPIHandler)
http.HandleFunc("/b/purchase", BobPurchaseDataAPIHandler)
http.HandleFunc("/b/deposit", BobDepositETHAPIHandler)
http.HandleFunc("/b/undeposit", BobUnDepositETHAPIHandler)
http.HandleFunc("/b/withdraw", BobWithdrawETHAPIHandler)
fmt.Printf("====================≖‿≖✧====================\n")
fmt.Printf("API server start...\n")
fmt.Printf("port=%v\n\n", BConf.Port)
http.ListenAndServe(":"+BConf.Port, nil)
}
//SetupNode setups the node for p2p net.
func SetupNode(Log ILogger) {
Log.Infof("setup node...")
for {
if GAdminAuth == nil || ZkPoDExchangeClient == nil {
time.Sleep(2 * time.Second)
continue
}
BobNodeStart = true
fmt.Printf("\n====================(●'◡'●)ノ♥====================\n")
fmt.Printf("Bob node start....\n\n")
break
}
for {
if BConf.NetIP == "" || GAdminAuth == nil || ZkPoDExchangeClient == nil {
time.Sleep(2 * time.Second)
continue
}
fmt.Printf("\n====================(づ。◕‿‿◕。)づ====================\n")
fmt.Printf("Alice node start....\n\n")
err := AliceStartNode(BConf.NetIP, ETHKey, Log)
if err != nil {
Log.Errorf("failed to start node.")
AliceNodeStart = false
}
}
}
//HandleCmdReq handles requests from command line.
func HandleCmdReq(config Config) {
Log := Logger.NewSessionLogger()
switch config.Operation {
case OPERATION_ALICE_INITDATA:
Log.Debugf("start initdata for publishing...config path=%v", config.InitPublishConfigPath)
if config.InitPublishConfigPath == "" {
Log.Warnf("parameter is incomplete. please input 'init'")
panic("parameter is incomplete")
}
AliceInitDataNode(config.InitPublishConfigPath, Log)
case OPERATION_ALICE_PUBLISH:
Log.Debugf("start publish data...merkle root=%v, eth value=%v", config.MerkleRoot, config.ETHValue)
if config.MerkleRoot == "" {
Log.Warnf("parameter is incomplete. please input 'mkl' and 'eth'")
panic("parameter is incomplete")
}
AlicePublishData(config.MerkleRoot, config.ETHValue, Log)
case OPERATION_ALICE_CLOSE:
Log.Debugf("start close published data...merkle root=%v", config.MerkleRoot)
if config.MerkleRoot == "" {
Log.Warnf("parameter is incomplete. please input 'mkl'")
panic("parameter is incomplete")
}
AliceCloseData(config.MerkleRoot, Log)
case OPERATION_WITHDRAW:
if config.MerkleRoot != "" {
Log.Debugf("start withdraw for Alice...merkle root=%v", config.MerkleRoot)
AliceWithdrawETHForData(config.MerkleRoot, Log)
} else if config.SessionID != "" {
Log.Debugf("start withdraw for Alice...session id=%v", config.SessionID)
AliceWithdrawETHForTx(config.SessionID, Log)
} else if config.AliceAddress != "" {
Log.Debugf("start withdraw for Bob...Alice address=%v", config.AliceAddress)
BobWithdrawETH(config.AliceAddress, Log)
} else {
Log.Warnf("parameters are incomplete. please input 'mkl' or 'addr'")
panic("parameters are incomplete")
}
case OPERATION_BOB_PURCHASE:
Log.Debugf("start purchase data...config path=%v", config.PurchaseConfigPath)
if config.PurchaseConfigPath == "" {
Log.Warnf("parameter is incomplete. please input 'data'")
panic("parameter is incomplete")
}
requestData, err := readRequestData(config.PurchaseConfigPath)
if err != nil {
panic(err)
}
BobPurchaseData(requestData, Log)
case OPERATION_BOB_DEPOSIT:
Log.Debugf("start deposit for Bob...Alice address=%v, eth value=%v", config.AliceAddress, config.ETHValue)
if config.AliceAddress == "" || config.ETHValue == "" {
Log.Warnf("parameters are incomplete. please input 'data'")
panic("parameters are incomplete")
}
BobDepositETH(config.ETHValue, config.AliceAddress, Log)
case OPERATION_BOB_UNDEPOSIT:
Log.Debugf("start undeposit for Bob...Alice address=%v", config.AliceAddress)
if config.AliceAddress == "" {
Log.Warnf("parameter is incomplete. please input 'addr'")
panic("parameter is incomplete")
}
BobUnDepositETH(config.AliceAddress, Log)
default:
Log.Warnf("invalid operation. operation=%v", config.Operation)
panic("invalid operation")
}
}