-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
546 lines (467 loc) · 16.9 KB
/
identity.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"bytes"
"errors"
"fmt"
"reflect"
"sort"
"github.com/DCNT-developer/dcnt/common/constants"
. "github.com/DCNT-developer/dcnt/common/identity"
. "github.com/DCNT-developer/dcnt/common/identityEntries"
"github.com/DCNT-developer/dcnt/common/interfaces"
"github.com/DCNT-developer/dcnt/common/primitives"
log "github.com/sirupsen/logrus"
)
var _ = DecodeIdentityChainStructureFromExtIDs
// identLogger is the general logger for all identity related logs. You can add additional fields,
// or create more context loggers off of this
var identLogger = packageLogger.WithFields(log.Fields{"subpack": "identity"})
var (
// Time window for identity to require registration: 24hours = 144 blocks
TIME_WINDOW uint32 = 144
// Where all Identities register
MAIN_FACTOM_IDENTITY_LIST = "888888001750ede0eff4b05f0c3f557890b256450cabbb84cada937f9c258327"
)
// GetSigningKey will return the signing key of the identity, and it's type
// Returns:
// -1 --> Follower
// 0 --> Audit Server
// 1 --> Federated
func (st *State) GetSigningKey(id interfaces.IHash) (interfaces.IHash, int) {
getReturnStatInt := func(stat uint8) int {
if stat == constants.IDENTITY_PENDING_FEDERATED_SERVER || stat == constants.IDENTITY_FEDERATED_SERVER {
return 1
}
if stat == constants.IDENTITY_AUDIT_SERVER || stat == constants.IDENTITY_PENDING_AUDIT_SERVER {
return 0
}
return -1
}
auth := st.IdentityControl.GetAuthority(id)
if auth != nil {
key := auth.SigningKey.Fixed()
hash, _ := primitives.NewShaHash(key[:])
if !(hash == nil || hash.IsZero()) {
return hash, getReturnStatInt(auth.Status)
}
}
identity := st.IdentityControl.GetIdentity(id)
if identity != nil {
return identity.SigningKey, getReturnStatInt(identity.Status)
}
return nil, -1
}
func (st *State) GetNetworkSkeletonKey() (rval interfaces.IHash) {
defer func() {
if rval != nil && reflect.ValueOf(rval).IsNil() {
rval = nil // convert an interface that is nil to a nil interface
primitives.LogNilHashBug("State.GetNetworkSkeletonKey() saw an interface that was nil")
}
}()
id := st.IdentityControl.GetIdentity(st.GetNetworkSkeletonIdentity())
if id == nil {
// There should always be a skeleton identity. It cannot be removed
// If there is one, just use the bootstrap key
return st.GetNetworkBootStrapKey()
}
key := id.SigningKey
// If the key is all 0s, use the bootstrap key
if key.IsSameAs(primitives.NewZeroHash()) || key == nil {
return st.GetNetworkBootStrapKey()
} else {
return key
}
}
// Add the skeleton identity and try to build it
func (st *State) IntiateNetworkSkeletonIdentity() error {
skel := st.GetNetworkSkeletonIdentity()
st.IdentityControl.SetSkeletonIdentity(skel)
// This populates the identity with keys found
err := st.AddIdentityFromChainID(skel)
if err != nil {
return err
}
return nil
}
func (st *State) InitiateNetworkIdentityRegistration() error {
reg := st.GetNetworkIdentityRegistrationChain()
st.IdentityControl.SetIdentityRegistration(reg)
// This populates the identity with keys found
err := st.AddIdentityFromChainID(reg)
if err != nil {
return err
}
return nil
}
type IdentityEntry struct {
Entry interfaces.IEBEntry
Timestamp interfaces.Timestamp
Blockheight uint32
}
// LoadIdentityByEntry is only useful when initial is set to false. If initial is false, it will track changes
// in an identity that corresponds to an authority. If initial is true, then calling ProcessIdentityEntry directly will
// have the same result.
func (st *State) LoadIdentityByEntry(ent interfaces.IEBEntry, height uint32, dblockTimestamp interfaces.Timestamp, d *DBState) {
flog := identLogger.WithFields(st.Logger.Data).WithField("func", "LoadIdentityByEntry")
if ent == nil {
return
}
affectAblock := d != nil && d.DirectoryBlock.GetDatabaseHeight() == height+1
if !affectAblock {
d = nil
}
// Not an entry identities care about
if bytes.Compare(ent.GetChainID().Bytes()[:3], []byte{0x88, 0x88, 0x88}) != 0 {
return
}
var a interfaces.IAdminBlock
if d != nil {
a = d.AdminBlock
} else {
a = nil
}
_, err := st.IdentityControl.ProcessIdentityEntryWithABlockUpdate(ent, height, dblockTimestamp, a, true)
if err != nil {
flog.Errorf(err.Error())
}
}
// Called by AddServer Message
func ProcessIdentityToAdminBlock(st *State, chainID interfaces.IHash, servertype int) bool {
flog := identLogger.WithFields(st.Logger.Data).WithField("func", "ProcessIdentityToAdminBlock")
err := st.AddIdentityFromChainID(chainID)
if err != nil {
flog.Errorf("Failed to process AddServerMessage AddIdentityFromChainID for %s : %s", chainID.String()[:10], err.Error())
st.LogPrintf("process", "Failed to process AddServerMessage for %s : %s", chainID.String()[:10], err.Error())
return false
}
id := st.IdentityControl.GetIdentity(chainID)
if id != nil {
if ok, err := id.IsPromteable(); !ok {
flog.Errorf("Failed to process AddServerMessage id.IsPromteable for %s : %s", chainID.String()[:10], err.Error())
st.LogPrintf("process", "Failed to process AddServerMessage for %s : %s", chainID.String()[:10], err.Error())
return false
}
} else {
flog.Errorf("Failed to process AddServerMessage: IdentityControl.GetIdentity %s", "New Fed/Audit server ["+chainID.String()[:10]+"] does not have an identity associated to it")
st.LogPrintf("process", "Failed to process AddServerMessage: %s", "New Fed/Audit server ["+chainID.String()[:10]+"] does not have an identity associated to it")
return false
}
// Add to admin block
if servertype == 0 {
id.Status = constants.IDENTITY_PENDING_FEDERATED_SERVER
st.LogPrintf("executeMsg", "Add server 2 %x", chainID.Bytes()[3:6])
st.LeaderPL.AdminBlock.AddFedServer(chainID)
} else if servertype == 1 {
id.Status = constants.IDENTITY_PENDING_AUDIT_SERVER
st.LeaderPL.AdminBlock.AddAuditServer(chainID)
}
st.IdentityControl.SetIdentity(chainID, id)
st.LeaderPL.AdminBlock.AddFederatedServerSigningKey(chainID, id.SigningKey.Fixed())
st.LeaderPL.AdminBlock.AddMatryoshkaHash(chainID, id.MatryoshkaHash)
for _, a := range id.AnchorKeys {
st.LeaderPL.AdminBlock.AddFederatedServerBitcoinAnchorKey(chainID, a.KeyLevel, a.KeyType, a.SigningKey)
}
if !id.CoinbaseAddress.IsZero() {
st.LeaderPL.AdminBlock.AddCoinbaseAddress(chainID, id.CoinbaseAddress)
}
st.LeaderPL.AdminBlock.AddEfficiency(chainID, id.Efficiency)
st.LogPrintf("executeMsg", "Added server %x", chainID.Bytes()[3:6])
return true
}
// Verifies if is authority
// Return true if authority, false if not
func (st *State) VerifyIsAuthority(cid interfaces.IHash) bool {
return st.IdentityControl.GetAuthority(cid) != nil
}
// AddIdentityFromChainID will add an identity to our list to watch and sync it.
func (st *State) AddIdentityFromChainID(cid interfaces.IHash) error {
id := st.IdentityControl.GetIdentity(cid)
if id == nil {
id = NewIdentity()
id.IdentityChainID = cid
st.IdentityControl.SetIdentity(cid, id)
}
err := st.AddIdentityEblocks(cid, true)
if err != nil {
return err
}
// This is the initial call, so the current height should not trigger a keychange in the admin block
st.SyncIdentities(nil)
return nil
}
// SyncIdentities will run through the identities and sync any identities that need to be updated.
// If the current height is equal to the eblock+1, then this entry can trigger a change in key to the admin block
func (st *State) SyncIdentities(d *DBState) {
// This will search an eblock, and sync it's entries if found.
getAndSyncEntries := func(ieb EntryBlockMarker) error {
eblock, err := st.DB.FetchEBlock(ieb.KeyMr)
if err != nil || eblock == nil {
return fmt.Errorf("eblock missing")
}
entries := make([]IdentityEntry, len(eblock.GetEntryHashes()))
for i, e := range eblock.GetEntryHashes() {
if e.IsMinuteMarker() {
continue
}
entry, err := st.DB.FetchEntry(e)
if err != nil || entry == nil {
return fmt.Errorf("eblock missing entries")
}
entries[i] = IdentityEntry{entry, ieb.DblockTimestamp, ieb.DBHeight}
}
// We have all the entries, so now we can process each entry and advance our eblock syncing
for _, ie := range entries {
st.LoadIdentityByEntry(ie.Entry, ieb.DBHeight, ieb.DblockTimestamp, d)
}
st.IdentityControl.ProcessOldEntries()
return nil
}
SyncIdentitiesLoop:
for _, id := range st.IdentityControl.Identities {
change := false
hasManage := !(id.ManagementChainID == nil || id.ManagementChainID.IsZero())
for !id.IdentityChainSync.Synced() {
// If we find a management chain id, we need to add all of it's Eblocks to it's list to be synced before we
// synced it's management chain
eb := id.IdentityChainSync.NextEBlock()
if eb == nil {
// The registration chain might not have eblocks to be parsed yet, since it is added on bootstrap
// and the db might yet have these blocks.
if id.Status != constants.IDENTITY_REGISTRATION_CHAIN {
panic(fmt.Sprintf("NextEblock was nil, but the identity chain was not fully synced. ID: %s", id.IdentityChainID.String()))
}
continue SyncIdentitiesLoop
}
// Fetch the eblock and process the entries
err := getAndSyncEntries(*eb)
if err != nil {
break
}
// No error means it was synced, so update our eblock sync list
id.IdentityChainSync.BlockParsed(*eb)
change = true
}
if !hasManage {
// Found a manage chain
if id.ManagementChainID != nil && !id.ManagementChainID.IsZero() {
err := st.AddIdentityEblocks(id.ManagementChainID, false)
if err != nil {
panic(fmt.Sprintf("Could not add managment eblocks: %s", err.Error()))
}
}
}
if change {
st.IdentityControl.SetIdentity(id.IdentityChainID, id)
}
change = false
for !id.ManagementChainSync.Synced() {
// If we find a management chain id, we need to add all of it's Eblocks to it's list to be synced before we
// synced it's management chain
eb := id.ManagementChainSync.NextEBlock()
if eb == nil {
break // Manage chain might not have any eblocks to be processed
}
// If the root chain is not synced, we cannot sync past it
if !id.IdentityChainSync.Synced() && eb.DBHeight > id.IdentityChainSync.Current.DBHeight {
break
}
// Fetch the eblock and process the entries
err := getAndSyncEntries(*eb)
if err != nil {
break
}
// No error means it was synced, so update our eblock sync list
id.ManagementChainSync.BlockParsed(*eb)
change = true
}
if change {
st.IdentityControl.SetIdentity(id.IdentityChainID, id)
}
}
}
// AddIdentityEblocks will find all eblocks for a root/management chain and add them to the sync list
func (st *State) AddIdentityEblocks(cid interfaces.IHash, rootChain bool) error {
id := st.IdentityControl.GetIdentity(cid)
if id == nil {
return fmt.Errorf("[%s] identity not found", cid.String()[:10])
}
// A management chain was found. We need to add eblocks to it's synclist
eblocks, err := st.DB.FetchAllEBlocksByChain(cid)
if err != nil {
return fmt.Errorf("This is a problem. Eblocks were not able to be fetched for %s", cid.String()[:10])
}
markers := make([]EntryBlockMarker, 0)
for _, eb := range eblocks {
if eb.GetDatabaseHeight() > st.LLeaderHeight {
continue
}
keymr, err := eb.KeyMR()
if err != nil {
return fmt.Errorf("Keymr of eblock was unable to be computed")
}
dblock, err := st.DB.FetchDBlockByHeight(eb.GetDatabaseHeight())
if err != nil {
return fmt.Errorf("DBlock at %d not found on disk", eb.GetDatabaseHeight())
}
markers = append(markers, EntryBlockMarker{keymr, eb.GetHeader().GetEBSequence(), eb.GetDatabaseHeight(), dblock.GetTimestamp()})
//markers[i] = EntryBlockMarker{keymr, eb.GetHeader().GetEBSequence(), eb.GetDatabaseHeight(), dblock.GetTimestamp()}
}
sort.Sort(EntryBlockMarkerList(markers))
for _, m := range markers {
if rootChain {
id.IdentityChainSync.AddNewHeadMarker(m)
} else {
id.ManagementChainSync.AddNewHeadMarker(m)
}
}
return nil
}
// AddNewIdentityEblocks will scan the new eblock list and identify any eblocks of interest.
// If an eblock belongs to a current identity, we add it to the eblock lists of the identity
// to be synced.
func (st *State) AddNewIdentityEblocks(eblocks []interfaces.IEntryBlock, dblockTimestamp interfaces.Timestamp) {
for _, eb := range eblocks {
if bytes.Compare(eb.GetChainID().Bytes()[:3], []byte{0x88, 0x88, 0x88}) == 0 {
// Can belong to an identity chain id
id := st.IdentityControl.GetIdentity(eb.GetChainID())
if id != nil {
keymr, err := eb.KeyMR()
if err != nil {
continue // This shouldn't ever happen
}
// It belongs to an identity that we are watching
if id.IdentityChainID.IsSameAs(eb.GetChainID()) {
// Identity chain eblock
id.IdentityChainSync.AddNewHead(keymr, eb.GetHeader().GetEBSequence(), eb.GetDatabaseHeight(), dblockTimestamp)
} else if id.ManagementChainID.IsSameAs(eb.GetChainID()) {
// Manage chain eblock
id.ManagementChainSync.AddNewHead(keymr, eb.GetHeader().GetEBSequence(), eb.GetDatabaseHeight(), dblockTimestamp)
}
st.IdentityControl.SetIdentity(id.IdentityChainID, id)
}
}
}
}
/*************************
Old methods used to
lookup an identity
in blockchain
*************************/
// FetchIdentityChainEntriesInCreateOrder will grab all entries in a chain for an identity in the order they were created.
func (s *State) FetchIdentityChainEntriesInCreateOrder(chainid interfaces.IHash) ([]IdentityEntry, error) {
head, err := s.DB.FetchHeadIndexByChainID(chainid)
if err != nil {
return nil, err
}
if head == nil {
return nil, fmt.Errorf("chain %x does not exist", chainid.Fixed())
}
// Get Eblocks
var blocks []interfaces.IEntryBlock
next := head
for {
if next.IsZero() {
break
}
// Get the EBlock, and add to list to parse
block, err := s.DB.FetchEBlock(next)
if err != nil {
return nil, err
}
blocks = append(blocks, block)
next = block.GetHeader().GetPrevKeyMR()
}
var entries []IdentityEntry
// Walk through eblocks in reverse order to get entries
for i := len(blocks) - 1; i >= 0; i-- {
eb := blocks[i]
height := eb.GetDatabaseHeight()
// Get the timestamp
dblock, err := s.DB.FetchDBlockByHeight(height)
if err != nil {
return nil, err
}
ts := dblock.GetTimestamp()
ehashes := eb.GetEntryHashes()
for _, e := range ehashes {
if e.IsMinuteMarker() {
continue
}
entry, err := s.DB.FetchEntry(e)
if err != nil {
return nil, err
}
entries = append(entries, IdentityEntry{entry, ts, height})
}
}
return entries, nil
}
func (st *State) LookupIdentityInBlockchainByChainID(cid interfaces.IHash) error {
identityRegisterChain, _ := primitives.HexToHash(MAIN_FACTOM_IDENTITY_LIST)
// No root entries, means no identity
rootEntries, err := st.FetchIdentityChainEntriesInCreateOrder(cid)
if err != nil {
st.IdentityControl.RemoveIdentity(cid)
return err
}
// ** Step 1 **
// First we need to determine if the identity is registered. We will have to parse the entire
// register chain (TODO: This should probably be optimized)
regEntries, err := st.FetchIdentityChainEntriesInCreateOrder(identityRegisterChain)
if err != nil {
st.IdentityControl.RemoveIdentity(cid)
return err
}
parseEntryList := func(list []IdentityEntry) {
for _, e := range list {
// Instead of calling LoadIdentityByEntry, we can call process directly, as this is initializing
// an identity.
st.IdentityControl.ProcessIdentityEntry(e.Entry, e.Blockheight, e.Timestamp, true)
}
st.IdentityControl.ProcessOldEntries()
}
for _, e := range regEntries {
// Instead of calling LoadIdentityByEntry, we can call process directly, as this is initializing
// an identity.
if e.Entry == nil {
continue
}
// We only care about the identity passed, so ignore all other entries
if len(e.Entry.ExternalIDs()) == 5 && bytes.Compare(e.Entry.ExternalIDs()[2], cid.Bytes()) == 0 {
st.IdentityControl.ProcessIdentityEntry(e.Entry, e.Blockheight, e.Timestamp, true)
}
}
st.IdentityControl.ProcessOldEntries()
// ** Step 2 **
// Parse the identity's chain id, which will give us the management chain ID
parseEntryList(rootEntries)
// ** Step 3 **
// Parse the entries contained in the management chain (if exists!)
id := st.IdentityControl.GetIdentity(cid)
if id == nil {
st.IdentityControl.RemoveIdentity(cid)
return fmt.Errorf("Identity was not found")
}
// The id stops here
if id.ManagementChainID.IsZero() {
st.IdentityControl.RemoveIdentity(cid)
return fmt.Errorf("No management chain found for identity")
}
manageEntries, err := st.FetchIdentityChainEntriesInCreateOrder(id.ManagementChainID)
if err != nil {
st.IdentityControl.RemoveIdentity(cid)
return err
}
parseEntryList(manageEntries)
// ** Step 4 **
// Check if it is promotable
id = st.IdentityControl.GetIdentity(cid)
if ok, err := id.IsPromteable(); !ok {
st.IdentityControl.RemoveIdentity(cid)
return errors.New("Error: Identity not full - " + err.Error())
}
return nil
}