Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions code/go/0chain.net/blobbercore/handler/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func RegisterBlobber(ctx context.Context) (string, error) {

time.Sleep(transaction.SLEEP_FOR_TXN_CONFIRMATION * time.Second)

// initialize storage node (ie blobber)
txn, err := transaction.NewTransactionEntity()
if err != nil {
return "", err
Expand All @@ -76,10 +77,21 @@ func RegisterBlobber(ctx context.Context) (string, error) {
return "", err
}

// check storage node (ie blobber): is it already registered?
sRegisteredNodes, _ := GetBlobbers()

for _, sRegisteredNode := range sRegisteredNodes {
if sn.ID == string(sRegisteredNode.ID) || sn.BaseURL == sRegisteredNode.BaseURL {
Logger.Info("Failed during registering blobber to the mining network, it's duplicated")
return "", errors.New("Duplicated")
}
}

snBytes, err := json.Marshal(sn)
if err != nil {
return "", err
}

Logger.Info("Adding blobber to the blockchain.")
err = txn.ExecuteSmartContract(transaction.STORAGE_CONTRACT_ADDRESS,
transaction.ADD_BLOBBER_SC_NAME, string(snBytes), 0)
Expand Down
40 changes: 39 additions & 1 deletion code/go/0chain.net/blobbercore/handler/zcncore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"sync"
"encoding/json"

"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zcncore"
Expand All @@ -11,6 +12,7 @@ type ZCNStatus struct {
wg *sync.WaitGroup
success bool
balance int64
info string
}

func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, info string) {
Expand All @@ -23,6 +25,16 @@ func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, info string) {
zcn.balance = value
}

func (zcn *ZCNStatus) OnInfoAvailable(op int, status int, info string, err string) {
defer zcn.wg.Done()
if status == zcncore.StatusSuccess {
zcn.success = true
} else {
zcn.success = false
}
zcn.info = info
}

func (zcn *ZCNStatus) OnTransactionComplete(t *zcncore.Transaction, status int) {
defer zcn.wg.Done()
if status == zcncore.StatusSuccess {
Expand All @@ -49,7 +61,7 @@ func CheckBalance() (float64, error) {
wg.Add(1)
err := zcncore.GetBalance(statusBar)
if err != nil {
return 0, common.NewError("check_balance_failed", "Call to GetBalance failed with err: "+err.Error())
return 0, common.NewError("check_balance_failed", "Call to GetBalance failed with err: " + err.Error())
}
wg.Wait()
if !statusBar.success {
Expand All @@ -58,6 +70,32 @@ func CheckBalance() (float64, error) {
return zcncore.ConvertToToken(statusBar.balance), nil
}

func GetBlobbers() ([]*zcncore.Blobber, error) {
var info struct {
Nodes []*zcncore.Blobber
}

wg := &sync.WaitGroup{}
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)

err := zcncore.GetBlobbers(statusBar)
if err != nil {
return info.Nodes, common.NewError("get_blobbers_failed", "Call to GetBlobbers failed with err: " + err.Error())
}
wg.Wait()

if !statusBar.success {
return info.Nodes, nil
}

if err = json.Unmarshal([]byte(statusBar.info), &info); err != nil {
return info.Nodes, common.NewError("get_blobbers_failed", "Decoding response to GetBlobbers failed with err: " + err.Error())
}

return info.Nodes, nil
}

func CallFaucet() error {
wg := &sync.WaitGroup{}
statusBar := &ZCNStatus{wg: wg}
Expand Down