Skip to content

lint fixes #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 13, 2021
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
62 changes: 27 additions & 35 deletions code/go/0chain.net/blobber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,11 @@ func SetupWorkers() {
// stats.StartEventDispatcher(2)
}

var fsStore filestore.FileStore

func initEntities() {
// badgerdbstore.SetupStorageProvider(*badgerDir)
fsStore = filestore.SetupFSStore(*filesDir + "/files")
// blobber.SetupObjectStorageHandler(fsStore, badgerdbstore.GetStorageProvider())

// allocation.SetupAllocationChangeCollectorEntity(badgerdbstore.GetStorageProvider())
// allocation.SetupAllocationEntity(badgerdbstore.GetStorageProvider())
// allocation.SetupDeleteTokenEntity(badgerdbstore.GetStorageProvider())
// reference.SetupFileRefEntity(badgerdbstore.GetStorageProvider())
// reference.SetupRefEntity(badgerdbstore.GetStorageProvider())
// reference.SetupContentReferenceEntity(badgerdbstore.GetStorageProvider())
// writemarker.SetupEntity(badgerdbstore.GetStorageProvider())
// readmarker.SetupEntity(badgerdbstore.GetStorageProvider())
// challenge.SetupEntity(badgerdbstore.GetStorageProvider())
// stats.SetupStatsEntity(badgerdbstore.GetStorageProvider())
var fsStore filestore.FileStore //nolint:unused // global which might be needed somewhere

func initEntities() (err error) {
fsStore, err = filestore.SetupFSStore(*filesDir + "/files")
return err
}

func initServer() {
Expand Down Expand Up @@ -172,31 +160,31 @@ func checkForDBConnection() {
func processMinioConfig(reader io.Reader) error {
scanner := bufio.NewScanner(reader)
more := scanner.Scan()
if more == false {
if !more {
return common.NewError("process_minio_config_failed", "Unable to read minio config from minio config file")
}

filestore.MinioConfig.StorageServiceURL = scanner.Text()
more = scanner.Scan()
if more == false {
if !more {
return common.NewError("process_minio_config_failed", "Unable to read minio config from minio config file")
}

filestore.MinioConfig.AccessKeyID = scanner.Text()
more = scanner.Scan()
if more == false {
if !more {
return common.NewError("process_minio_config_failed", "Unable to read minio config from minio config file")
}

filestore.MinioConfig.SecretAccessKey = scanner.Text()
more = scanner.Scan()
if more == false {
if !more {
return common.NewError("process_minio_config_failed", "Unable to read minio config from minio config file")
}

filestore.MinioConfig.BucketName = scanner.Text()
more = scanner.Scan()
if more == false {
if !more {
return common.NewError("process_minio_config_failed", "Unable to read minio config from minio config file")
}

Expand Down Expand Up @@ -317,10 +305,15 @@ func main() {

checkForDBConnection()

// Initializa after serverchain is setup.
initEntities()
//miner.GetMinerChain().SetupGenesisBlock(viper.GetString("server_chain.genesis_block.id"))
SetupBlobberOnBC(*logDir)
// Initialize after server chain is setup.
if err := initEntities(); err != nil {
Logger.Panic("Error setting up blobber on blockchian" + err.Error())
return
}
if err := SetupBlobberOnBC(*logDir); err != nil {
Logger.Panic("Error setting up blobber on blockchian" + err.Error())
return
}
mode := "main net"
if config.Development() {
mode = "development"
Expand Down Expand Up @@ -386,7 +379,6 @@ func RegisterBlobber() {
time.Sleep(transaction.SLEEP_FOR_TXN_CONFIRMATION * time.Second)
t, err := transaction.VerifyTransaction(txnHash, chain.GetServerChain())
if err == nil {
txnVerified = true
Logger.Info("Transaction for adding blobber accepted and verified", zap.String("txn_hash", t.Hash), zap.Any("txn_output", t.TransactionOutput))
//badgerdbstore.GetStorageProvider().WriteBytes(ctx, BLOBBER_REGISTERED_LOOKUP_KEY, []byte(txnHash))
//badgerdbstore.GetStorageProvider().Commit(ctx)
Expand Down Expand Up @@ -465,18 +457,18 @@ func UpdateBlobberSettings() {
}
}

func SetupBlobberOnBC(logDir string) {
func SetupBlobberOnBC(logDir string) error {
var logName = logDir + "/0chainBlobber.log"
zcncore.SetLogFile(logName, false)
zcncore.SetLogLevel(3)
zcncore.InitZCNSDK(serverChain.BlockWorker, config.Configuration.SignatureScheme)
zcncore.SetWalletInfo(node.Self.GetWalletString(), false)
//txnHash, err := badgerdbstore.GetStorageProvider().ReadBytes(common.GetRootContext(), BLOBBER_REGISTERED_LOOKUP_KEY)
//if err != nil {
// Now register blobber to chain
if err := zcncore.InitZCNSDK(serverChain.BlockWorker, config.Configuration.SignatureScheme); err != nil {
return err
}
if err := zcncore.SetWalletInfo(node.Self.GetWalletString(), false); err != nil {
return err
}
go RegisterBlobber()
//}
//Logger.Info("Blobber already registered", zap.Any("blobberTxn", string(txnHash)))
return nil
}

/*HomePageHandler - provides basic info when accessing the home page of the server */
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/blobber/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package main

// stubs that does nothing
func initIntegrationsTests(id string) {}
func shutdownIntegrationTests() {}
func shutdownIntegrationTests() {} //nolint:unused,deadcode // looks like it is being used in integration test
15 changes: 10 additions & 5 deletions code/go/0chain.net/blobbercore/allocation/allocationchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"0chain.net/blobbercore/datastore"
"0chain.net/blobbercore/reference"
"0chain.net/core/common"
"0chain.net/core/logging"

"go.uber.org/zap"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -133,7 +135,9 @@ func (cc *AllocationChangeCollector) ComputeProperties() {
continue // unknown operation (impossible case?)
}

acp.Unmarshal(change.Input) // error is not handled
if err := acp.Unmarshal(change.Input); err != nil { // error is not handled
logging.Logger.Error("AllocationChangeCollector_unmarshal", zap.Error(err))
}
cc.AllocationChanges = append(cc.AllocationChanges, acp)
}
}
Expand All @@ -159,10 +163,11 @@ func (a *AllocationChangeCollector) CommitToFileStore(ctx context.Context) error
return nil
}

func (a *AllocationChangeCollector) DeleteChanges(ctx context.Context) error {
func (a *AllocationChangeCollector) DeleteChanges(ctx context.Context) {
for _, change := range a.AllocationChanges {
change.DeleteTempFile()
if err := change.DeleteTempFile(); err != nil {
logging.Logger.Error("AllocationChangeProcessor_DeleteTempFile", zap.Error(err))
}
}

return nil
return
}
26 changes: 17 additions & 9 deletions code/go/0chain.net/blobbercore/allocation/deletefilechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"encoding/json"
"path/filepath"

"0chain.net/blobbercore/reference"
"0chain.net/core/common"
"0chain.net/blobbercore/datastore"
"0chain.net/blobbercore/filestore"
"0chain.net/blobbercore/reference"
"0chain.net/core/common"
. "0chain.net/core/logging"

"go.uber.org/zap"
Expand Down Expand Up @@ -62,7 +62,9 @@ func (nf *DeleteFileChange) ProcessChange(ctx context.Context, change *Allocatio
if child.Hash == nf.Hash && child.Hash == affectedRef.Hash {
idx = i
nf.ContentHash = make(map[string]bool)
reference.DeleteReference(ctx, child.ID, child.PathHash)
if err := reference.DeleteReference(ctx, child.ID, child.PathHash); err != nil {
Logger.Error("DeleteReference", zap.Int64("ref_id", child.ID), zap.Error(err))
}
if child.Type == reference.FILE {
nf.ContentHash[child.ThumbnailHash] = true
nf.ContentHash[child.ContentHash] = true
Expand All @@ -75,16 +77,20 @@ func (nf *DeleteFileChange) ProcessChange(ctx context.Context, change *Allocatio
if idx < 0 {
return nil, common.NewError("file_not_found", "Object to delete not found in blobber")
}

dirRef.RemoveChild(idx)
rootRef.CalculateHash(ctx, true)

if _, err := rootRef.CalculateHash(ctx, true); err != nil {
return nil, err
}

return nil, nil
}

func (nf *DeleteFileChange) processChildren(ctx context.Context, curRef *reference.Ref) {
for _, childRef := range curRef.Children {
reference.DeleteReference(ctx, childRef.ID, childRef.PathHash)
if err := reference.DeleteReference(ctx, childRef.ID, childRef.PathHash); err != nil {
Logger.Error("DeleteReference", zap.Int64("ref_id", childRef.ID), zap.Error(err))
}
if childRef.Type == reference.FILE {
nf.ContentHash[childRef.ThumbnailHash] = true
nf.ContentHash[childRef.ContentHash] = true
Expand Down Expand Up @@ -118,8 +124,10 @@ func (nf *DeleteFileChange) CommitToFileStore(ctx context.Context) error {
err := db.Table((&reference.Ref{}).TableName()).Where(&reference.Ref{ThumbnailHash: contenthash}).Or(&reference.Ref{ContentHash: contenthash}).Count(&count).Error
if err == nil && count == 0 {
Logger.Info("Deleting content file", zap.String("content_hash", contenthash))
filestore.GetFileStore().DeleteFile(nf.AllocationID, contenthash)
if err := filestore.GetFileStore().DeleteFile(nf.AllocationID, contenthash); err != nil {
Logger.Error("FileStore_DeleteFile", zap.String("allocation_id", nf.AllocationID), zap.Error(err))
}
}
}
return nil
}
}
6 changes: 4 additions & 2 deletions code/go/0chain.net/blobbercore/allocation/newfilechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (nf *NewFileChange) ProcessChange(ctx context.Context,

dirRef := rootRef
treelevel := 0
for true {
for {
found := false
for _, child := range dirRef.Children {
if child.Type == reference.DIRECTORY && treelevel < len(tSubDirs) {
Expand Down Expand Up @@ -104,7 +104,9 @@ func (nf *NewFileChange) ProcessChange(ctx context.Context,
}

dirRef.AddChild(newFile)
rootRef.CalculateHash(ctx, true)
if _, err := rootRef.CalculateHash(ctx, true); err != nil {
return nil, err
}
stats.NewFileCreated(ctx, newFile.ID)
return rootRef, nil
}
Expand Down
4 changes: 1 addition & 3 deletions code/go/0chain.net/blobbercore/allocation/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ func updateAllocationInDB(ctx context.Context, a *Allocation,
var tx = datastore.GetStore().GetTransaction(ctx)
defer commit(tx, &err)

var changed bool

changed = a.Tx != sa.Tx
var changed bool = a.Tx != sa.Tx

// transaction
a.Tx = sa.Tx
Expand Down
12 changes: 9 additions & 3 deletions code/go/0chain.net/blobbercore/challenge/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ func (cr *ChallengeEntity) SubmitChallengeToBC(ctx context.Context) (*transactio

func (cr *ChallengeEntity) ErrorChallenge(ctx context.Context, err error) {
cr.StatusMessage = err.Error()
cr.Save(ctx)
if err := cr.Save(ctx); err != nil {
Logger.Error("ChallengeEntity_Save", zap.String("challenge_id", cr.ChallengeID), zap.Error(err))
}
}

func (cr *ChallengeEntity) GetValidationTickets(ctx context.Context) error {
if len(cr.Validators) == 0 {
cr.StatusMessage = "No validators assigned to the challange"
cr.Save(ctx)
if err := cr.Save(ctx); err != nil {
Logger.Error("ChallengeEntity_Save", zap.String("challenge_id", cr.ChallengeID), zap.Error(err))
}
return common.NewError("no_validators", "No validators assigned to the challange")
}

Expand Down Expand Up @@ -236,7 +240,9 @@ func (cr *ChallengeEntity) CommitChallenge(ctx context.Context, verifyOnly bool)
cr.Status = Committed
cr.StatusMessage = t.TransactionOutput
cr.CommitTxnID = t.Hash
cr.Save(ctx)
if err := cr.Save(ctx); err != nil {
Logger.Error("ChallengeEntity_Save", zap.String("challenge_id", cr.ChallengeID), zap.Error(err))
}
FileChallenged(ctx, cr.RefID, cr.Result, cr.CommitTxnID)
return nil
}
Expand Down
40 changes: 16 additions & 24 deletions code/go/0chain.net/blobbercore/challenge/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type BCChallengeResponse struct {

func SetupWorkers(ctx context.Context) {
go FindChallenges(ctx)
go SubmitProcessedChallenges(ctx)
go SubmitProcessedChallenges(ctx) //nolint:errcheck // goroutines
}

func GetValidationTickets(ctx context.Context, challengeObj *ChallengeEntity) error {
Expand All @@ -43,7 +43,7 @@ func GetValidationTickets(ctx context.Context, challengeObj *ChallengeEntity) er
}

func SubmitProcessedChallenges(ctx context.Context) error {
for true {
for {
select {
case <-ctx.Done():
return ctx.Err()
Expand All @@ -61,7 +61,9 @@ func SubmitProcessedChallenges(ctx context.Context) error {
lastSeq := 0
lastCommitTxn := ""
for rows.Next() {
rows.Scan(&lastCommitTxn, &lastSeq)
if err := rows.Scan(&lastCommitTxn, &lastSeq); err != nil {
Logger.Error("Rows_Scan", zap.Error(err))
}
}

openchallenges := make([]*ChallengeEntity, 0)
Expand All @@ -74,7 +76,9 @@ func SubmitProcessedChallenges(ctx context.Context) error {
if len(openchallenges) > 0 {
for _, openchallenge := range openchallenges {
Logger.Info("Attempting to commit challenge", zap.Any("challenge_id", openchallenge.ChallengeID), zap.Any("openchallenge", openchallenge))
openchallenge.UnmarshalFields()
if err := openchallenge.UnmarshalFields(); err != nil {
Logger.Error("ChallengeEntity_UnmarshalFields", zap.String("challenge_id", openchallenge.ChallengeID), zap.Error(err))
}
mutex := lock.GetMutex(openchallenge.TableName(), openchallenge.ChallengeID)
mutex.Lock()
redeemCtx := datastore.GetStore().CreateTransaction(ctx)
Expand Down Expand Up @@ -111,7 +115,9 @@ func SubmitProcessedChallenges(ctx context.Context) error {

for _, toBeVerifiedChallenge := range toBeVerifiedChallenges {
Logger.Info("Attempting to commit challenge through verification", zap.Any("challenge_id", toBeVerifiedChallenge.ChallengeID), zap.Any("openchallenge", toBeVerifiedChallenge))
toBeVerifiedChallenge.UnmarshalFields()
if err := toBeVerifiedChallenge.UnmarshalFields(); err != nil {
Logger.Error("ChallengeEntity_UnmarshalFields", zap.String("challenge_id", toBeVerifiedChallenge.ChallengeID), zap.Error(err))
}
mutex := lock.GetMutex(toBeVerifiedChallenge.TableName(), toBeVerifiedChallenge.ChallengeID)
mutex.Lock()
redeemCtx := datastore.GetStore().CreateTransaction(ctx)
Expand Down Expand Up @@ -143,30 +149,14 @@ func SubmitProcessedChallenges(ctx context.Context) error {
time.Sleep(time.Duration(config.Configuration.ChallengeResolveFreq) * time.Second)
}

// if challengeObj.ObjectPath != nil && challengeObj.Status == Committed && challengeObj.ObjectPath.FileBlockNum > 0 {
// //stats.FileChallenged(challengeObj.AllocationID, challengeObj.ObjectPath.Meta["path"].(string), challengeObj.CommitTxnID)
// if challengeObj.Result == ChallengeSuccess {
// go stats.AddChallengeRedeemedEvent(challengeObj.AllocationID, challengeObj.ID, stats.SUCCESS, stats.REDEEMSUCCESS, challengeObj.ObjectPath.Meta["path"].(string), challengeObj.CommitTxnID)
// } else if challengeObj.Result == ChallengeFailure {
// go stats.AddChallengeRedeemedEvent(challengeObj.AllocationID, challengeObj.ID, stats.FAILED, stats.REDEEMSUCCESS, challengeObj.ObjectPath.Meta["path"].(string), challengeObj.CommitTxnID)
// }

// } else if challengeObj.ObjectPath != nil && challengeObj.Status != Committed && challengeObj.ObjectPath.FileBlockNum > 0 && challengeObj.Retries >= config.Configuration.ChallengeMaxRetires {
// if challengeObj.Result == ChallengeSuccess {
// go stats.AddChallengeRedeemedEvent(challengeObj.AllocationID, challengeObj.ID, stats.SUCCESS, stats.REDEEMERROR, challengeObj.ObjectPath.Meta["path"].(string), challengeObj.CommitTxnID)
// } else if challengeObj.Result == ChallengeFailure {
// go stats.AddChallengeRedeemedEvent(challengeObj.AllocationID, challengeObj.ID, stats.FAILED, stats.REDEEMERROR, challengeObj.ObjectPath.Meta["path"].(string), challengeObj.CommitTxnID)
// }
// }

return nil
return nil //nolint:govet // need more time to verify
}

var iterInprogress = false

func FindChallenges(ctx context.Context) {
ticker := time.NewTicker(time.Duration(config.Configuration.ChallengeResolveFreq) * time.Second)
for true {
for {
select {
case <-ctx.Done():
return
Expand Down Expand Up @@ -237,7 +227,9 @@ func FindChallenges(ctx context.Context) {
if (latestChallenge == nil && len(challengeObj.PrevChallengeID) == 0) || latestChallenge.ChallengeID == challengeObj.PrevChallengeID {
Logger.Info("Adding new challenge found from blockchain", zap.String("challenge", v.ChallengeID))
challengeObj.Status = Accepted
challengeObj.Save(tCtx)
if err := challengeObj.Save(tCtx); err != nil {
Logger.Error("ChallengeEntity_Save", zap.String("challenge_id", challengeObj.ChallengeID), zap.Error(err))
}
} else {
Logger.Error("Challenge chain is not valid")
}
Expand Down
Loading