Skip to content

Commit 512fcda

Browse files
committed
Add syncing of mining chain
1 parent 549269a commit 512fcda

File tree

9 files changed

+528
-225
lines changed

9 files changed

+528
-225
lines changed

block/mining_chain.go

Lines changed: 218 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131
MIMinedTokenIDKey string = "TokenID"
3232
MIMinedTokenLevelKey string = "Token_level"
3333
MIMinedTokenNumberKey string = "Token_number"
34-
MICreditDeatilsKey string = "Credit_details"
34+
MICreditDetailsKey string = "Credit_details"
3535
MIPledgeDetailsKey string = "Quorum_pledge_details"
3636
MIMiningQuorumSignatureKey string = "Quorum_signature"
3737
MIEpochKey string = "Epoch"
@@ -43,7 +43,7 @@ type MiningChainBlockInfo struct {
4343
MinerDID string `json:"minerDID"`
4444
TokenID string `json:"tokenID"`
4545
TokenLevel int `json:"tokenLevel"`
46-
TokenNumber int `json:"tokenNumber"`
46+
TokenNumber uint64 `json:"tokenNumber"`
4747
CreditDetails map[string]interface{} `json:"creditDetails"`
4848
PledgeDetails []PledgeDetail `json:"pledgeDetails"`
4949
QuorumSignature []QuorumSignature `json:"quorumSignature"`
@@ -68,7 +68,7 @@ func NewMiningInfo(ctcb map[string]*MiningChain, mi *MiningChainBlockInfo) map[s
6868
nmcbi[MIMinedTokenNumberKey] = mi.TokenNumber
6969

7070
if len(mi.CreditDetails) > 0 {
71-
nmcbi[MICreditDeatilsKey] = mi.CreditDetails
71+
nmcbi[MICreditDetailsKey] = mi.CreditDetails
7272
}
7373
if len(mi.PledgeDetails) > 0 {
7474
nmcbi[MIPledgeDetailsKey] = mi.PledgeDetails
@@ -298,10 +298,20 @@ func (mb *MiningChain) GetMiningChainBlockNumber() (uint64, error) {
298298
}
299299
}
300300

301+
// GetMiningChainID retrieves the mining chain ID.
301302
func GetMiningChainID() string {
302303
return RubixMiningChainID
303304
}
304305

306+
// GetMiningInfos retrieves the mining info map.
307+
func (mb *MiningChain) GetMiningInfos() (map[string]interface{}, error) {
308+
infos, ok := mb.bm[MiningChainBlockInfoKey].(map[string]interface{})
309+
if !ok {
310+
return nil, fmt.Errorf("mining infos not found or invalid")
311+
}
312+
return infos, nil
313+
}
314+
305315
// // GetHash retrieves the block hash.
306316
// func (mb *MiningBlock) GetHash() (string, error) {
307317
// hash, ok := mb.bm[MiningChainBlockHashKey].(string)
@@ -311,20 +321,209 @@ func GetMiningChainID() string {
311321
// return hash, nil
312322
// }
313323

314-
// // GetSignature retrieves the block signature.
315-
// func (mb *MiningBlock) GetSignature() (string, error) {
316-
// sig, ok := mb.bm[MiningChainBlockSignatureKey].(string)
317-
// if !ok {
318-
// return "", fmt.Errorf("block signature not found or invalid")
319-
// }
320-
// return sig, nil
321-
// }
324+
// GetMiningChainBlockSignature retrieves the block signature from MiningChainBlock.
325+
func (mb *MiningChain) GetMiningChainBlockSignature() (string, error) {
326+
val, ok := mb.bm[MiningChainBlockSignatureKey]
327+
if !ok {
328+
return "", fmt.Errorf("block signature key not found")
329+
}
330+
sig, ok := val.(string)
331+
if !ok {
332+
return "", fmt.Errorf("block signature is not a string")
333+
}
334+
return sig, nil
335+
}
322336

323-
// // GetMiningInfos retrieves the mining info map.
324-
// func (mb *MiningBlock) GetMiningInfos() (map[string]interface{}, error) {
325-
// infos, ok := mb.bm[MiningChainBlockInfoKey].(map[string]interface{})
326-
// if !ok {
327-
// return nil, fmt.Errorf("mining infos not found or invalid")
328-
// }
329-
// return infos, nil
330-
// }
337+
// GetMiningID retrieves the MiningID from MiningChainBlockInfo.
338+
func (mb *MiningChain) GetMiningID() (string, error) {
339+
infos, err := mb.GetMiningInfos()
340+
if err != nil {
341+
return "", err
342+
}
343+
val, ok := infos[MIMiningIDKey]
344+
if !ok {
345+
return "", fmt.Errorf("mining ID key not found")
346+
}
347+
miningID, ok := val.(string)
348+
if !ok {
349+
return "", fmt.Errorf("mining ID is not a string")
350+
}
351+
return miningID, nil
352+
}
353+
354+
// GetMinerDID retrieves the MinerDID from MiningChainBlockInfo.
355+
func (mb *MiningChain) GetMinerDID() (string, error) {
356+
infos, err := mb.GetMiningInfos()
357+
if err != nil {
358+
return "", err
359+
}
360+
val, ok := infos[MIMinerDID]
361+
if !ok {
362+
return "", fmt.Errorf("miner DID key not found")
363+
}
364+
minerDID, ok := val.(string)
365+
if !ok {
366+
return "", fmt.Errorf("miner DID is not a string")
367+
}
368+
return minerDID, nil
369+
}
370+
371+
// GetTokenID retrieves the TokenID from MiningChainBlockInfo.
372+
func (mb *MiningChain) GetTokenID() (string, error) {
373+
infos, err := mb.GetMiningInfos()
374+
if err != nil {
375+
return "", err
376+
}
377+
val, ok := infos[MIMinedTokenIDKey]
378+
if !ok {
379+
return "", fmt.Errorf("token ID key not found")
380+
}
381+
tokenID, ok := val.(string)
382+
if !ok {
383+
return "", fmt.Errorf("token ID is not a string")
384+
}
385+
return tokenID, nil
386+
}
387+
388+
// GetTokenLevel retrieves the TokenLevel from MiningChainBlockInfo.
389+
func (mb *MiningChain) GetTokenLevel() (int, error) {
390+
infos, err := mb.GetMiningInfos()
391+
if err != nil {
392+
return 0, err
393+
}
394+
val, ok := infos[MIMinedTokenLevelKey]
395+
if !ok {
396+
return 0, fmt.Errorf("token level key not found")
397+
}
398+
switch v := val.(type) {
399+
case float64:
400+
if v != float64(int(v)) {
401+
return 0, fmt.Errorf("token level is not an integer: %v", v)
402+
}
403+
return int(v), nil
404+
case int64:
405+
return int(v), nil
406+
case int:
407+
return v, nil
408+
default:
409+
return 0, fmt.Errorf("token level is not a number: %T", v)
410+
}
411+
}
412+
413+
// GetTokenNumber retrieves the TokenNumber from MiningChainBlockInfo.
414+
func (mb *MiningChain) GetTokenNumber() (int, error) {
415+
infos, err := mb.GetMiningInfos()
416+
if err != nil {
417+
return 0, err
418+
}
419+
val, ok := infos[MIMinedTokenNumberKey]
420+
if !ok {
421+
return 0, fmt.Errorf("token number key not found")
422+
}
423+
switch v := val.(type) {
424+
case float64:
425+
if v != float64(int(v)) {
426+
return 0, fmt.Errorf("token number is not an integer: %v", v)
427+
}
428+
return int(v), nil
429+
case int64:
430+
return int(v), nil
431+
case int:
432+
return v, nil
433+
default:
434+
return 0, fmt.Errorf("token number is not a number: %T", v)
435+
}
436+
}
437+
438+
// GetCreditDetails retrieves the CreditDetails from MiningChainBlockInfo.
439+
func (mb *MiningChain) GetCreditDetails() (map[string]interface{}, error) {
440+
infos, err := mb.GetMiningInfos()
441+
if err != nil {
442+
return nil, err
443+
}
444+
val, ok := infos[MICreditDetailsKey]
445+
if !ok {
446+
return nil, fmt.Errorf("credit details key not found")
447+
}
448+
creditDetails, ok := val.(map[string]interface{})
449+
if !ok {
450+
return nil, fmt.Errorf("credit details is not a map")
451+
}
452+
return creditDetails, nil
453+
}
454+
455+
// GetPledgeDetails retrieves the PledgeDetails from MiningChainBlockInfo.
456+
func (mb *MiningChain) GetPledgeDetails() ([]interface{}, error) {
457+
infos, err := mb.GetMiningInfos()
458+
if err != nil {
459+
return nil, err
460+
}
461+
val, ok := infos[MIPledgeDetailsKey]
462+
if !ok {
463+
return nil, fmt.Errorf("pledge details key not found")
464+
}
465+
pledgeDetails, ok := val.([]interface{})
466+
if !ok {
467+
return nil, fmt.Errorf("pledge details is not a slice")
468+
}
469+
return pledgeDetails, nil
470+
}
471+
472+
// GetQuorumSignature retrieves the QuorumSignature from MiningChainBlockInfo.
473+
func (mb *MiningChain) GetQuorumSignature() ([]interface{}, error) {
474+
infos, err := mb.GetMiningInfos()
475+
if err != nil {
476+
return nil, err
477+
}
478+
val, ok := infos[MIMiningQuorumSignatureKey]
479+
if !ok {
480+
return nil, fmt.Errorf("quorum signature key not found")
481+
}
482+
quorumSignature, ok := val.([]interface{})
483+
if !ok {
484+
return nil, fmt.Errorf("quorum signature is not a slice")
485+
}
486+
return quorumSignature, nil
487+
}
488+
489+
// GetEpoch retrieves the Epoch from MiningChainBlockInfo.
490+
func (mb *MiningChain) GetEpoch() (int, error) {
491+
infos, err := mb.GetMiningInfos()
492+
if err != nil {
493+
return 0, err
494+
}
495+
val, ok := infos[MIEpochKey]
496+
if !ok {
497+
return 0, fmt.Errorf("epoch key not found")
498+
}
499+
switch v := val.(type) {
500+
case float64:
501+
if v != float64(int(v)) {
502+
return 0, fmt.Errorf("epoch is not an integer: %v", v)
503+
}
504+
return int(v), nil
505+
case int64:
506+
return int(v), nil
507+
case int:
508+
return v, nil
509+
default:
510+
return 0, fmt.Errorf("epoch is not a number: %T", v)
511+
}
512+
}
513+
514+
// GetPreviousMiningID retrieves the PreviousMiningID from MiningChainBlockInfo.
515+
func (mb *MiningChain) GetPreviousMiningID() (string, error) {
516+
infos, err := mb.GetMiningInfos()
517+
if err != nil {
518+
return "", err
519+
}
520+
val, ok := infos[MIPreviousMiningIDKey]
521+
if !ok {
522+
return "", fmt.Errorf("previous mining ID key not found")
523+
}
524+
prevMiningID, ok := val.(string)
525+
if !ok {
526+
return "", fmt.Errorf("previous mining ID is not a string")
527+
}
528+
return prevMiningID, nil
529+
}

core/core.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
APICheckPinRole string = "/api/check-pin-role"
6464
APISyncGenesisAndLatestBlock string = "/api/sync-gennesis-n-lastest-block"
6565
APIUpdateCreditsAndWeekEpoch string = "/api/update-credits-and-week-epoch"
66+
APISyncMiningChain string = "/api/sync-mining-chain"
6667
)
6768

6869
const (
@@ -74,7 +75,7 @@ const (
7475
TestNetDir string = "TestNet"
7576
TestNetDIDDir string = "TestNetDID/"
7677
MaxDecimalPlaces int = 3
77-
AddressForMiningChainSync string = "12D3KooWGppoT2BwPddMG3uLdwAar2b5yuFG1KCviELYtoFuQPAs.bafybmib6gtwq7kqlvfqzi4sbv7fpxuwp3smjro5bujtbckaajkbdkls3b4"
78+
AddressForMiningChainSync string = "12D3KooWGppoT2BwPddMG3uLdwAar2b5yuFG1KCviELYtoFuQPAs" //bafybmib6gtwq7kqlvfqzi4sbv7fpxuwp3smjro5bujtbckaajkbdkls3b4
7879
)
7980

8081
const (

0 commit comments

Comments
 (0)