/
http.go
135 lines (117 loc) · 2.75 KB
/
http.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
package rpc
import (
"encoding/json"
"fmt"
"net/http"
"github.com/Gravity-Tech/gravity-core/common/account"
"github.com/Gravity-Tech/gravity-core/common/storage"
"github.com/Gravity-Tech/gravity-core/common/transactions"
)
var cfg *Config
type SetNebulaRq struct {
NebulaId string
ChainType string
MaxPulseCountInBlock uint64
MinScore uint64
}
type VotesRq struct {
Votes []VoteRq
}
type VoteRq struct {
PubKey string
Score uint64
}
func ListenRpcServer(config *Config) {
cfg = config
http.HandleFunc("/vote", vote)
http.HandleFunc("/setNebula", setNebulaHandler)
err := http.ListenAndServe(cfg.Host, nil)
if err != nil {
fmt.Printf("Error Private RPC: %s", err.Error())
}
}
func vote(w http.ResponseWriter, r *http.Request) {
var request VotesRq
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tx, err := transactions.New(cfg.pubKey, transactions.Vote, cfg.privKey)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var votes []storage.Vote
for _, v := range request.Votes {
pubKey, err := account.HexToValidatorPubKey(v.PubKey)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
votes = append(votes, storage.Vote{
PubKey: pubKey,
Score: v.Score,
})
}
b, err := json.Marshal(votes)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tx.AddValue(transactions.BytesValue{Value: b})
err = cfg.client.SendTx(tx)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
return
}
func setNebulaHandler(w http.ResponseWriter, r *http.Request) {
err := setNebula(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
return
}
func setNebula(r *http.Request) error {
var request SetNebulaRq
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
return err
}
tx, err := transactions.New(cfg.pubKey, transactions.SetNebula, cfg.privKey)
if err != nil {
return err
}
chainType, err := account.ParseChainType(request.ChainType)
if err != nil {
return err
}
nebulaId, err := account.StringToNebulaId(request.NebulaId, chainType)
if err != nil {
return err
}
nebulaInfo := storage.NebulaInfo{
MaxPulseCountInBlock: request.MaxPulseCountInBlock,
MinScore: request.MinScore,
ChainType: chainType,
Owner: cfg.pubKey,
}
b, err := json.Marshal(nebulaInfo)
if err != nil {
return err
}
tx.AddValues([]transactions.Value{
transactions.BytesValue{Value: nebulaId[:]},
transactions.BytesValue{Value: b},
})
err = cfg.client.SendTx(tx)
if err != nil {
return err
}
return nil
}