-
Notifications
You must be signed in to change notification settings - Fork 38
/
cmd.go
175 lines (151 loc) · 6.14 KB
/
cmd.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
package main
import (
"encoding/json"
"io/ioutil"
"net/url"
)
//BobPurchaseData purchases data.
func BobPurchaseData(requestData RequestData, Log ILogger) {
data, err := json.Marshal(requestData)
if err != nil {
Log.Warnf("failed to marshal request data. err=%v", err)
return
}
Log.Debugf("data sigma merkle root=%v, Alice ip=%v", requestData.MerkleRoot, requestData.AliceIP)
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/b/purchase"
body := make(url.Values)
body.Add("request_data", string(data))
Log.Debugf("start send request for purchasing data. url=%v, request data=%v", urlStr, string(data))
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("purchasing data finish.")
Log.Debugf("%s", string(responseBody))
}
//BobDepositETH deposit ETH to a Alice in contract.
func BobDepositETH(value string, AliceAddress string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/b/deposit"
body := make(url.Values)
body.Add("value", value)
body.Add("address", AliceAddress)
Log.Debugf("Bob deposit to Alice. value=%v, Alice address=%v", value, AliceAddress)
Log.Debugf("start send request to deposit ETH. url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request to deposit ETH finish.")
Log.Debugf("%s", string(responseBody))
}
//BobUnDepositETH undeposits ETH from a Alice in contract.
func BobUnDepositETH(AliceAddress string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/b/undeposit"
body := make(url.Values)
body.Add("address", AliceAddress)
Log.Debugf("Bob undeposit from Alice. Alice address=%v", AliceAddress)
Log.Debugf("start send request to undeposit ETH. url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request to undeposit ETH finish.")
Log.Debugf("%s", string(responseBody))
}
//BobWithdrawETH withdraw ETH from a Alice in contract.
func BobWithdrawETH(AliceAddress string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/b/withdraw"
body := make(url.Values)
body.Add("address", AliceAddress)
Log.Debugf("Bob withdraws for contract. Alice address=%v", AliceAddress)
Log.Debugf("start send request to withdraw for contract. url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request to withdraw for contract finish.")
Log.Debugf("%s", string(responseBody))
}
//AliceInitDataNode initialize data for publishing.
func AliceInitDataNode(filepath string, Log ILogger) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
Log.Warnf("read config file error! filepath=%s, err=%v", filepath, err)
panic(err.Error())
}
// Log.Debugf("data=%v", string(data))
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/s/publish/init"
body := make(url.Values)
body.Add("request_data", string(data))
Log.Debugf("start send request for publishing data...url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request for publishing data finish.")
Log.Debugf("%s", string(responseBody))
}
// AlicePublishData sends a request to server
// to initializes data and publishes data to contract by Alice.
func AlicePublishData(merkleRoot string, value string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/s/publish"
body := make(url.Values)
body.Add("merkleRoot", merkleRoot)
body.Add("value", value)
Log.Debugf("start send request for publishing data...url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request for publishing data finish.")
Log.Debugf("%s", string(responseBody))
}
//AliceCloseData closes pushlished data in contract.
func AliceCloseData(merkleRoot string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/s/close"
body := make(url.Values)
body.Add("merkle_root", merkleRoot)
Log.Debugf("start send request for closing data...url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request for closing data finish.")
Log.Debugf("%s", string(responseBody))
}
//AliceWithdrawETHForData withdraw ETH from a Alice in contract.
func AliceWithdrawETHForData(merkle_root string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/s/withdraw/data"
body := make(url.Values)
body.Add("merkle_root", merkle_root)
Log.Debugf("start send request for withdrawing ETH...url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request for withdrawing ETH finish.")
Log.Debugf("%s", string(responseBody))
}
//AliceWithdrawETHForTx withdraw ETH from a Alice in contract.
func AliceWithdrawETHForTx(sessionID string, Log ILogger) {
urlStr := REQUEST_URL_LOCAL_HOST + ":" + BConf.Port + "/s/withdraw/tx"
body := make(url.Values)
body.Add("session_id", sessionID)
Log.Debugf("Bob withdraws for contract. sessionID=%v", sessionID)
Log.Debugf("start send request to withdraw for contract...url=%v", urlStr)
responseBody, err := sendRequest(body, REQUEST_METHOD_POST, urlStr, Log)
if err != nil {
Log.Warnf("read response error! url=%s, err=%v", urlStr, err)
panic(err.Error())
}
Log.Debugf("send request to withdraw for contract finish.")
Log.Debugf("%s", string(responseBody))
}