forked from wunder3605/noderank
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnoderank.go
More file actions
298 lines (259 loc) · 7.17 KB
/
noderank.go
File metadata and controls
298 lines (259 loc) · 7.17 KB
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
288
289
290
291
292
293
294
295
296
297
298
// Copyright by StreamNet team
// 功能描述:
// 1. 拼装新增证实交易请求;
// 2. 获取被证实节点的排名:将dag中的证实交易按一定顺序构造全拓扑序,以分页的方式获取指定n个证实交易作为输入,使用pagerank算法计算出这些节点的排名。
package noderank
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/awalterschulze/gographviz"
"github.com/triasteam/pagerank"
"io/ioutil"
"log"
"math"
"net/http"
url2 "net/url"
"sort"
"strconv"
"strings"
"time"
)
// Response ...
type Response struct {
Blocks string `json:"blocks"`
Duration int `json:"duration"`
}
type message struct {
TeeNum int64 `json:"tee_num"`
TeeContent []teectx `json:"tee_content"`
}
type teectx struct {
Attester string `json:"attester"`
Attestee string `json:"attestee"`
Score float64 `json:"score"`
Time string `json:"time,omitempty"`
Nonce int64 `json:"nonce,omitempty"`
}
type teescore struct {
Attestee string `json:"attestee"`
Score float64 `json:"score"`
}
// TeeSoreSlice ...
type TeeSoreSlice []teescore
var url = "http://localhost:14700"
var addr = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX"
var (
file = flag.String("file", "noderank/config.yaml", "IOTA CONFIGURATION")
)
// AddAttestationInfo ...
func AddAttestationInfo(addr1 string, url string, info []string) error {
raw := new(teectx)
raw.Attester = info[0]
raw.Attestee = info[1]
raw.Nonce, _ = strconv.ParseInt(info[3], 10, 64)
raw.Time = info[4]
score, err := strconv.ParseFloat(info[2], 64)
if err != nil {
return err
}
raw.Score = score
m := new(message)
m.TeeNum = 1
m.TeeContent = []teectx{*raw}
ms, err := json.Marshal(m)
if err != nil {
return err
}
if addr1 == "" {
addr1 = addr
}
d := time.Now()
ds := d.Format("20060102")
data := "{\"command\":\"storeMessage\",\"address\":" + addr1 + ",\"message\":" + url2.QueryEscape(string(ms[:])) + ",\"tag\":\"" + ds + "TEE\"}"
_, err = doPost(url, []byte(data))
if err != nil {
return err
}
return nil
}
// 根据条件获取到的所有证实交易都会参与noderank计算,结果返回前numRank的得分和对应得证实交易数(由于待输出数据是以attestee作为key得map中保存,
// 最终输出得teectx最终数量会少于实际)。
// uri StreamNet服务restful地址, peroid表示把全部交易按每页100个分页后所取页数,numRank 取排名后前numRank个被证实节点
func GetRank(uri string, period int64, numRank int64) ([]teescore, []teectx, error) {
data := "{\"command\":\"getBlocksInPeriodStatement\",\"period\":" + strconv.FormatInt(period, 10) + "}"
r, err := doPost(uri, []byte(data))
if err != nil {
fmt.Println("do post error, data = ", data)
panic(err)
}
return CaculateRank(r, period, numRank)
}
// CaculateRank ...
func CaculateRank(r []byte, period int64, numRank int64) ([]teescore, []teectx, error) {
var result Response
err := json.Unmarshal(r, &result)
if err != nil {
fmt.Println("unmarshal Response error, r = ", r)
return nil, nil, err
}
var msgArr []string
err = json.Unmarshal([]byte(result.Blocks), &msgArr)
if err != nil {
fmt.Println("unmarshal string array error, result.Blocks = ", result.Blocks)
return nil, nil, err
}
graph := pagerank.NewGraph()
cm := make(map[string]teectx)
rArr0 := []teectx{}
for _, m2 := range msgArr {
msgT, err := url2.QueryUnescape(m2)
if err != nil {
fmt.Println("QueryUnescape error, m2 = ", m2)
return nil, nil, err
}
var msg message
err = json.Unmarshal([]byte(msgT), &msg)
if err != nil {
fmt.Println("unmarshal message error, msgT = ", msgT)
return nil, nil, err
}
rArr := msg.TeeContent
for _, r := range rArr {
if math.IsNaN(r.Score) || math.IsInf(r.Score, 0) {
fmt.Println("un invalid rank param. score : ", r.Score)
} else {
if r.Score == 0 {
fmt.Println("un invalid rank param. score is zero.")
}
graph.Link(r.Attester, r.Attestee, r.Score)
cm[r.Attestee] = teectx{r.Attester, r.Attestee, r.Score, "", 0}
rArr0 = append(rArr0, r)
}
}
}
var rst []teescore
var teectxslice []teectx
graph.Rank(0.85, 0.0001, func(attestee string, score float64) {
tee := teescore{attestee, FloatRound(score, 8)}
rst = append(rst, tee)
})
sort.Sort(TeeSoreSlice(rst)) // 把计算结果按得分高低排序
if len(rst) < 1 {
return nil, nil, nil
}
endIdx := int64(len(rst))
if endIdx > numRank {
endIdx = numRank
}
rst = rst[0:endIdx] // 返回得分较大的 endIdx 个元素
// for _, r := range rst {
// if v, ok := cm[r.Attestee]; ok {
// teectxslice = append(teectxslice, v)
// }
// }
// 以结果的Attestee作为key
scoreMap := make(map[string]float64)
for _, r := range rst {
scoreMap[r.Attestee] = r.Score
}
// 遍历数组,获取前n个排名的被实节点对应的证实交易。
for _, r := range rArr0 {
if scoreMap[r.Attestee] != 0 {
teectxslice = append(teectxslice, r)
}
}
return rst, teectxslice, nil
}
// FloatRound ...
func FloatRound(f float64, n int) float64 {
format := "%." + strconv.Itoa(n) + "f"
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
return res
}
// PrintHCGraph 辅助方法,用来打印结果
func PrintHCGraph(uri string, period string) error {
data := "{\"command\":\"getBlocksInPeriodStatement\",\"period\":" + period + "}"
r, err := doPost(uri, []byte(data))
if err != nil {
return err
}
var result Response
err = json.Unmarshal(r, &result)
if err != nil {
fmt.Println(r)
}
fmt.Println(result.Duration)
fmt.Println(result.Blocks)
var msgArr []string
err = json.Unmarshal([]byte(result.Blocks), &msgArr)
if err != nil {
log.Panic(err)
}
graph := gographviz.NewGraph()
for _, m2 := range msgArr {
msgT, err := url2.QueryUnescape(m2)
if err != nil {
log.Panicln(err)
}
fmt.Println("message : " + msgT)
var msg message
err = json.Unmarshal([]byte(msgT), &msg)
if err != nil {
log.Panic(err)
}
rArr := msg.TeeContent
for _, r := range rArr {
//score := strconv.FormatUint(uint64(r.Score), 10) // TODO add this score info
graph.AddNode("G", r.Attestee, nil)
graph.AddNode("G", r.Attester, nil)
graph.AddEdge(r.Attester, r.Attestee, true, nil)
if err != nil {
log.Panic(err)
}
}
}
output := graph.String()
fmt.Println(output)
return nil
}
func doPost(uri string, d []byte) ([]byte, error) {
if uri == "" {
uri = url
}
fmt.Println("node rank request iota url is: ", uri)
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(d))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-IOTA-API-Version", "1")
client := &http.Client{}
res, err := client.Do(req)
fmt.Println("request result:", res, ", err:", err)
if err != nil {
return nil, err
}
defer res.Body.Close()
r, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return r, nil
}
// Len ...
func (t TeeSoreSlice) Len() int {
return len(t)
}
// Swap ...
func (t TeeSoreSlice) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
// Less ...
func (t TeeSoreSlice) Less(i, j int) bool {
if t[i].Score != t[j].Score {
return t[i].Score > t[j].Score
}
return strings.Compare(t[j].Attestee, t[i].Attestee) > 0
}