-
Notifications
You must be signed in to change notification settings - Fork 4
/
candidate.go
50 lines (41 loc) · 1.04 KB
/
candidate.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
package api
import (
"encoding/json"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/gorilla/mux"
"net/http"
"strings"
)
func GetCandidate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pubkey := types.Hex2Bytes(strings.TrimLeft(vars["pubkey"], "Mp"))
cState, err := GetStateForRequest(r)
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(w).Encode(Response{
Code: 404,
Log: "State for given height not found",
})
return
}
candidate := cState.GetStateCandidate(pubkey)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if candidate == nil {
w.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(w).Encode(Response{
Code: 404,
Log: "Candidate not found",
})
return
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: struct {
Candidate Candidate `json:"candidate"`
}{
Candidate: makeResponseCandidate(*candidate, true),
},
})
}