-
Notifications
You must be signed in to change notification settings - Fork 211
/
oracle_client.go
160 lines (127 loc) · 4.13 KB
/
oracle_client.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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/crypto"
"github.com/spacemeshos/go-spacemesh/log"
"io"
"math/big"
"net/http"
"sync"
)
const register = "register"
const unregister = "unregister"
const validate = "validatemap"
const defaultOracleServerAddress = "http://localhost:3030"
// serverAddress is the oracle server we're using
var serverAddress = defaultOracleServerAddress
func setServerAddress(addr string) {
serverAddress = addr
}
type requester interface {
Get(api, data string) []byte
}
type httpRequester struct {
url string
c *http.Client
}
func newHTTPRequester(url string) *httpRequester {
return &httpRequester{url, &http.Client{}}
}
func (hr *httpRequester) Get(api, data string) []byte {
var jsonStr = []byte(data)
log.Debug("Sending oracle request : %s ", jsonStr)
req, err := http.NewRequest("POST", hr.url+"/"+api, bytes.NewBuffer(jsonStr))
if err != nil {
log.Panic("httpRequester panicked: %v", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := hr.c.Do(req)
if err != nil {
log.Panic("httpRequester panicked: %v", err)
}
buf := bytes.NewBuffer([]byte{})
_, err = io.Copy(buf, resp.Body)
if err != nil {
log.Panic("httpRequester panicked: %v", err)
}
resp.Body.Close()
return buf.Bytes()
}
// oracleClient is a temporary replacement fot the real oracle. its gets accurate results from a server.
type oracleClient struct {
world uint64
client requester
eMtx sync.Mutex
instMtx map[uint32]*sync.Mutex
eligibilityMap map[uint32]map[string]struct{}
}
// newOracleClient creates a new client to query the oracle. It generates a random worldID.
func newOracleClient() *oracleClient {
b, err := crypto.GetRandomBytes(4)
if err != nil {
log.Panic("newOracleClient panicked: %v", err)
}
world := big.NewInt(0).SetBytes(b).Uint64()
return newClientWithWorldID(world)
}
// newClientWithWorldID creates a new client with a specific worldid
func newClientWithWorldID(world uint64) *oracleClient {
c := newHTTPRequester(serverAddress)
instMtx := make(map[uint32]*sync.Mutex)
eligibilityMap := make(map[uint32]map[string]struct{})
return &oracleClient{world: world, client: c, eligibilityMap: eligibilityMap, instMtx: instMtx}
}
func registerQuery(world uint64, id string, honest bool) string {
return fmt.Sprintf(`{ "World": %d, "ID": "%v", "Honest": %t }`, world, id, honest)
}
func validateQuery(world uint64, instid uint32, committeeSize int) string {
return fmt.Sprintf(`{ "World": %d, "InstanceID": %d, "CommitteeSize": %d}`, world, instid, committeeSize)
}
// Register asks the oracle server to add this node to the active set
func (oc *oracleClient) Register(honest bool, id string) {
oc.client.Get(register, registerQuery(oc.world, id, honest))
}
// Unregister asks the oracle server to de-list this node from the active set
func (oc *oracleClient) Unregister(honest bool, id string) {
oc.client.Get(unregister, registerQuery(oc.world, id, honest))
}
type validList struct {
IDs []string `json:"IDs"`
}
func hashInstanceAndK(instanceID types.LayerID, K int32) uint32 {
kInBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(kInBytes, uint32(K))
h := newHasherU32()
val := h.Hash(instanceID.ToBytes(), kInBytes)
return val
}
// Eligible checks whether a given ID is in the eligible list or not. it fetches the list once and gives answers locally after that.
func (oc *oracleClient) Eligible(layer types.LayerID, round int32, committeeSize int, id types.NodeID, sig []byte) (bool, error) {
instID := hashInstanceAndK(layer, round)
// make special instance ID
oc.eMtx.Lock()
if r, ok := oc.eligibilityMap[instID]; ok {
_, valid := r[id.Key]
oc.eMtx.Unlock()
return valid, nil
}
req := validateQuery(oc.world, instID, committeeSize)
resp := oc.client.Get(validate, req)
res := &validList{}
err := json.Unmarshal(resp, res)
if err != nil {
panic(err)
}
elgMap := make(map[string]struct{})
for _, v := range res.IDs {
elgMap[v] = struct{}{}
}
_, valid := elgMap[id.Key]
oc.eligibilityMap[instID] = elgMap
oc.eMtx.Unlock()
return valid, nil
}