-
Notifications
You must be signed in to change notification settings - Fork 9
/
oracles.go
144 lines (114 loc) · 2.71 KB
/
oracles.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
package query
import (
"encoding/json"
"github.com/Gravity-Tech/gravity-core/common/account"
"github.com/Gravity-Tech/gravity-core/common/storage"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type ByValidatorRq struct {
PubKey string
}
type ByNebulaRq struct {
ChainType account.ChainType
NebulaAddress string
}
type ResultsRq struct {
ChainType account.ChainType
Height uint64
NebulaAddress string
}
func allValidators(store *storage.Storage, _ []byte) ([]byte, error) {
scores, err := store.Scores()
result := make([]string, len(scores))
if err != nil {
return make([]byte, 0), err
}
for consulPubKey, _ := range scores {
result = append(result, hexutil.Encode(consulPubKey[:]))
}
encoded, err := json.Marshal(result)
if err != nil {
return make([]byte, 0), err
}
return encoded, nil
}
func oraclesByValidator(store *storage.Storage, value []byte) (storage.OraclesByTypeMap, error) {
var rq ByValidatorRq
err := json.Unmarshal(value, &rq)
if err != nil {
return nil, err
}
pubKey, err := account.HexToValidatorPubKey(rq.PubKey)
if err != nil {
return nil, err
}
v, err := store.OraclesByConsul(pubKey)
if err != nil {
return nil, err
}
return v, nil
}
func oraclesByNebula(store *storage.Storage, value []byte) (storage.OraclesMap, error) {
var rq ByNebulaRq
err := json.Unmarshal(value, &rq)
if err != nil {
return nil, err
}
nebula, err := account.StringToNebulaId(rq.NebulaAddress, rq.ChainType)
if err != nil {
return nil, err
}
v, err := store.OraclesByNebula(nebula)
if err != nil {
return nil, err
}
return v, nil
}
func bftOraclesByNebula(store *storage.Storage, value []byte) (storage.OraclesMap, error) {
var rq ByNebulaRq
err := json.Unmarshal(value, &rq)
if err != nil {
return nil, err
}
nebula, err := account.StringToNebulaId(rq.NebulaAddress, rq.ChainType)
if err != nil {
return nil, err
}
v, err := store.BftOraclesByNebula(nebula)
if err != nil {
return nil, err
}
return v, nil
}
func results(store *storage.Storage, value []byte) ([]string, error) {
var rq ResultsRq
err := json.Unmarshal(value, &rq)
if err != nil {
return nil, err
}
nebula, err := account.StringToNebulaId(rq.NebulaAddress, rq.ChainType)
if err != nil {
return nil, err
}
v, err := store.Results(nebula, rq.Height)
if err != nil {
return nil, err
}
return v, nil
}
func nebulaOraclesIndex(store *storage.Storage, value []byte) (uint64, error) {
var rq ByNebulaRq
err := json.Unmarshal(value, &rq)
if err != nil {
return 0, err
}
nebula, err := account.StringToNebulaId(rq.NebulaAddress, rq.ChainType)
if err != nil {
return 0, err
}
v, err := store.NebulaOraclesIndex(nebula)
if err != nil {
return 0, err
}
return v, nil
}