-
Notifications
You must be signed in to change notification settings - Fork 1
/
permissionLayer_NEO.go
310 lines (277 loc) · 7.72 KB
/
permissionLayer_NEO.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
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
299
300
301
302
303
304
305
306
307
308
309
310
package dht_permission_layers
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"sync"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/archoncloud/archoncloud-go/blockchainAPI/neo"
"github.com/archoncloud/archoncloud-go/blockchainAPI/neo/client_utils"
. "github.com/archoncloud/archoncloud-go/blockchainAPI/registered_sp"
"github.com/archoncloud/archon-dht/common"
permLayer "github.com/archoncloud/archon-dht/permission_layer"
)
// NEO
var neo_blockTime time.Duration = time.Duration(30) // minutes
// FIXME after beta this needs to be the actual neo blocktime
const cacheFilepath_NEO string = ".sp_profiles_cache/" + string(permLayer.NeoPermissionId) + "/"
var validationInProgress_NEO = struct {
sync.RWMutex
m map[peer.ID]bool
}{m: make(map[peer.ID]bool)}
var peerID2NEOAddrs = struct {
sync.RWMutex
m map[peer.ID]string
}{m: make(map[peer.ID]string)}
var inPeerstoreMap_NEO = struct {
sync.RWMutex
m map[peer.ID]bool
}{m: make(map[peer.ID]bool)}
var checkedButInvalid_NEO = struct {
sync.RWMutex
m map[peer.ID]bool
}{m: make(map[peer.ID]bool)}
type Neo struct {
}
func (n Neo) ID() permLayer.PermissionLayerID {
return permLayer.NeoPermissionId
}
func (n Neo) Permissioned() bool {
return true
}
func (n Neo) ValidatePeersPtrArr(bootstrapPeers []*peer.AddrInfo, timeout time.Duration) (res []*peer.AddrInfo, err error) {
var pBootstrapPeers []peer.AddrInfo
for _, p := range bootstrapPeers {
pBootstrapPeers = append(pBootstrapPeers, *p)
}
pValidatedPeers, err := n.ValidatePeers(pBootstrapPeers, timeout)
var validatedPeers []*peer.AddrInfo
for _, v := range pValidatedPeers {
if v.ID != "" {
mV := v
validatedPeers = append(validatedPeers, &mV)
}
}
return validatedPeers, err
}
// TODO REFACTOR PEERSTORE MAP FUNCTIONS TO BE GENERIC ACROSS ETH,NEO ETC
// for now this should work
func (n Neo) inPeerstore(pid peer.ID) bool {
inPeerstoreMap_NEO.Lock()
b := inPeerstoreMap_NEO.m[pid]
inPeerstoreMap_NEO.Unlock()
return b
}
func (n Neo) inCheckedButInvalid(pid peer.ID) bool {
checkedButInvalid_NEO.Lock()
b := checkedButInvalid_NEO.m[pid]
checkedButInvalid_NEO.Unlock()
return b
}
func (n Neo) IsInPeerstoreMap(tocheckPeers []peer.AddrInfo) (isIn, notIn []peer.AddrInfo) {
var iisIn []peer.AddrInfo
var nnotIn []peer.AddrInfo
for i := 0; i < len(tocheckPeers); i++ {
if n.inPeerstore(tocheckPeers[i].ID) {
iisIn = append(iisIn, tocheckPeers[i])
} else {
if !n.inCheckedButInvalid(tocheckPeers[i].ID) {
nnotIn = append(nnotIn, tocheckPeers[i])
}
}
}
return iisIn, nnotIn
}
func (n Neo) PutInPeerstoreMap(validated, notValidated []peer.AddrInfo) {
inPeerstoreMap_NEO.Lock()
if inPeerstoreMap_NEO.m == nil {
inPeerstoreMap_NEO.m = make(map[peer.ID]bool)
}
inPeerstoreMap_NEO.Unlock()
checkedButInvalid_NEO.Lock()
if checkedButInvalid_NEO.m == nil {
checkedButInvalid_NEO.m = make(map[peer.ID]bool)
}
checkedButInvalid_NEO.Unlock()
for i := 0; i < len(validated); i++ {
if len(validated[i].ID) > 0 {
inPeerstoreMap_NEO.Lock()
inPeerstoreMap_NEO.m[validated[i].ID] = true
inPeerstoreMap_NEO.Unlock()
}
}
for i := 0; i < len(notValidated); i++ {
if len(notValidated[i].ID) > 0 {
checkedButInvalid_NEO.Lock()
checkedButInvalid_NEO.m[notValidated[i].ID] = true
checkedButInvalid_NEO.Unlock()
}
}
return
}
func (n Neo) ValidatePeer(pid peer.ID, timeout time.Duration) (bool, error) {
// TODO USE TIMEOUT
return ValidatePeerIDNEO(pid)
}
func (n Neo) ValidatePeers(bootstrapPeers []peer.AddrInfo, timeout time.Duration) (res []peer.AddrInfo, err error) {
inPeerstore, notInPeerstore := n.IsInPeerstoreMap(bootstrapPeers)
validated, notValidated, err := ValidateBootstrapPeersNEO(notInPeerstore, timeout)
n.PutInPeerstoreMap(validated, notValidated)
for i := 0; i < len(inPeerstore); i++ {
validated = append(validated, inPeerstore[i])
}
if err.Error() == "" {
err = nil
}
return validated, err
}
func (n Neo) UpdateIndividualSPProfileCache(pid peer.ID) {
// check if in cache
filepath := filepath.FromSlash(cacheFilepath_NEO + pid.Pretty())
spDataFilepath := common.DefaultToExecutable(filepath)
info, err := os.Stat(spDataFilepath)
if err != nil {
if !os.IsNotExist(err) {
if time.Since(info.ModTime()) < neo_blockTime*time.Minute && info.Size() > 0 {
return
}
}
} else {
if time.Since(info.ModTime()) < neo_blockTime*time.Minute && info.Size() > 0 {
return
}
}
// not in cache
// check if have neoAddress
peerID2NEOAddrs.Lock()
var spAddress string
neoAddress, ok := peerID2NEOAddrs.m[pid]
if ok {
spAddress = neoAddress
} else {
// if dont have neo address
neoAddress, err := client_utils.GetNodeID2Address(pid.Pretty())
if err != nil {
return
}
var isAllZeros bool = true
for i := 0; i < len(neoAddress); i++ {
if neoAddress[i] != byte(0) {
isAllZeros = false
}
}
if isAllZeros {
return
}
inGoodStanding := neo.IsSpRegistered(neoAddress)
if !inGoodStanding {
return
}
if peerID2NEOAddrs.m == nil {
peerID2NEOAddrs.m = make(map[peer.ID]string)
}
peerID2NEOAddrs.m[pid] = neoAddress
spAddress = neoAddress
}
peerID2NEOAddrs.Unlock()
// get spprofile
sp, err := client_utils.GetRegisteredSP(spAddress)
if err != nil {
common.LogError.Println("UpdateSPProfileCache() error: ", pid, err)
}
sp.NodeID = pid.Pretty()
sp.Url = "" // for compare to old below
marshalledSP, err := json.Marshal(sp)
if err != nil {
common.LogError.Println("UpdateSPProfileCache() error: ", pid, err)
}
// Check if sp stats are different
SpFilenames.Lock()
if SpFilenames.M[spDataFilepath] == nil {
SpFilenames.M[spDataFilepath] = new(permLayer.SafeFilename)
}
ptr := SpFilenames.M[spDataFilepath]
SpFilenames.Unlock()
ptr.Lock()
defer ptr.Unlock()
// get old cached sp profile
oldSPData, err := ioutil.ReadFile(spDataFilepath)
if err == nil {
if len(oldSPData) > 0 {
oldSPProf := new(RegisteredSp)
json_err := json.Unmarshal(oldSPData, &oldSPProf)
if json_err != nil {
common.LogError.Println("UpdateSPProfileCache() error: ", pid, json_err)
}
oldSPProf.Url = "" // set this empty like sp
marshalledOldSP, err := json.Marshal(oldSPProf)
if err == nil {
// compare old w new
if len(marshalledSP) == len(marshalledOldSP) {
var same bool = true
for j := 0; j < len(marshalledSP); j++ {
if marshalledSP[j] != marshalledOldSP[j] {
same = false
break
}
}
if same {
currentTime := time.Now().Local()
os.Chtimes(spDataFilepath, currentTime, currentTime)
return
}
}
}
}
}
// STORE IN CACHE THIS SPPROFILE
if len(marshalledSP) > 0 {
ioutil.WriteFile(spDataFilepath, marshalledSP, 0644)
} else {
common.LogError.Println("UpdateSPProfileCache() error: len(marshalledSP) < 1 ", pid)
}
}
func (n Neo) UpdateSPProfileCache(pids []peer.ID) {
go func() {
err := os.MkdirAll(path.Dir(cacheFilepath_NEO), os.ModeDir|os.ModePerm)
if err != nil {
common.LogError.Println("UpdateSPProfileCache() error: ", err)
}
for i := 0; i < len(pids); i++ {
go func(pid peer.ID, nn Neo) {
nn.UpdateIndividualSPProfileCache(pid)
}(pids[i], n)
}
}()
//common.LogDebug.Println("Ethereum UpdateSPProfileCache() returns")
return
}
func (n Neo) CompareBlockHeights(lhs, rhs permLayer.VersionData) (int, error) {
// TODO
return 0, nil
}
func (n Neo) GetBlockHeight() (string, error) {
return neo.GetBlockHeight()
}
func (n Neo) GetBlockHash(blockHeight string) (string, error) {
return neo.GetBlockHash(blockHeight)
}
func (n Neo) NewVersionData() (v *permLayer.VersionData, err error) {
height, err := n.GetBlockHeight()
if err != nil {
return
}
hash, err := n.GetBlockHash(height)
if err != nil {
return
}
v = &permLayer.VersionData{
BlockHeight: height,
BlockHash: hash,
}
return
}
// END NEO