Skip to content

Commit 132f070

Browse files
authored
Fix/blobber reg (#643)
1 parent ad877da commit 132f070

38 files changed

+641
-548
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
name: Lints
1818
runs-on: [self-hosted,docker-builds]
1919
steps:
20-
- name: Setup go 1.17
20+
- name: Setup go 1.18
2121
uses: actions/setup-go@v2
2222
with:
23-
go-version: '1.17' # The Go version to download (if necessary) and use.
23+
go-version: '1.18' # The Go version to download (if necessary) and use.
2424

2525
- name: Clone blobber
2626
uses: actions/checkout@v2
@@ -41,10 +41,10 @@ jobs:
4141
name: Unit Tests
4242
runs-on: [self-hosted,docker-builds]
4343
steps:
44-
- name: Setup go 1.17
44+
- name: Setup go 1.18
4545
uses: actions/setup-go@v2
4646
with:
47-
go-version: '1.17' # The Go version to download (if necessary) and use.
47+
go-version: '1.18' # The Go version to download (if necessary) and use.
4848

4949
- name: Clone blobber
5050
uses: actions/checkout@v2
@@ -66,10 +66,10 @@ jobs:
6666
runs-on: [ubuntu-20.04]
6767
timeout-minutes: 40
6868
steps:
69-
- name: Setup go 1.17
69+
- name: Setup go 1.18
7070
uses: actions/setup-go@v2
7171
with:
72-
go-version: '1.17' # The Go version to download (if necessary) and use.
72+
go-version: '1.18' # The Go version to download (if necessary) and use.
7373

7474
# - uses: actions/cache@v2
7575
# with:
@@ -101,6 +101,6 @@ jobs:
101101
102102
echo =========================[ run tests ]=========================
103103
#sudo make integration-tests
104-
go17=$(which go)
104+
go=$(which go)
105105
root=$(pwd)
106-
sudo CGO_ENABLED=1 root=$root integration=1 $go17 test -tags bn256 ./...
106+
sudo CGO_ENABLED=1 root=$root integration=1 $go test -tags bn256 ./...

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ local-init:
3333
#[ -d ./dev.local/data/blobber/config ] && rm -rf ./dev.local/data/blobber/config
3434
cp -r ./config ./dev.local/data/blobber/
3535
ifeq ($(UNAME_OS),Darwin)
36-
cd ./dev.local/data/blobber/config/ && find . -name "*.yaml" -exec sed -i '' "s/postgres/127.0.0.1/g" {} \;
36+
cd ./dev.local/data/blobber/config/ && find . -name "*.yaml" -exec sed -i '' "s/host: postgres/host: 127.0.0.1/g" {} \;
3737
else
38-
cd ./dev.local/data/blobber/config/ && sed -i "s/postgres/127.0.0.1/g" ./0chain_blobber.yaml
38+
cd ./dev.local/data/blobber/config/ && sed -i "s/host: postgres/host: 127.0.0.1/g" ./0chain_blobber.yaml
3939
endif
4040
cd ./dev.local/data/blobber && [ -d files ] || mkdir files
4141
cd ./dev.local/data/blobber && [ -d data ] || mkdir data

code/go/0chain.net/blobber/config.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"time"
78

89
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
10+
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
911
"github.com/0chain/blobber/code/go/0chain.net/core/transaction"
1012
"github.com/spf13/viper"
1113
)
1214

13-
func setupConfig() {
14-
fmt.Print("[2/12] load config")
15+
func setupConfig(configDir string, deploymentMode int) {
16+
fmt.Print("> load config")
1517
// setup default
1618
config.SetupDefaultConfig()
1719

1820
// setup config file
1921
config.SetupConfig(configDir)
2022

21-
// load config
2223
config.Configuration.DeploymentMode = byte(deploymentMode)
2324
config.Configuration.ChainID = viper.GetString("server_chain.id")
2425
config.Configuration.SignatureScheme = viper.GetString("server_chain.signature_scheme")
@@ -50,8 +51,6 @@ func setupConfig() {
5051
config.Configuration.MinioWorkerFreq = viper.GetInt64("minio.worker_frequency")
5152
config.Configuration.MinioUseSSL = viper.GetBool("minio.use_ssl")
5253

53-
config.Configuration.Capacity = viper.GetInt64("capacity")
54-
5554
config.Configuration.DBAutoMigrate = viper.GetBool("db.automigrate")
5655
config.Configuration.PGUserName = viper.GetString("pg.user")
5756
config.Configuration.PGPassword = viper.GetString("pg.password")
@@ -66,13 +65,7 @@ func setupConfig() {
6665
config.Configuration.DBPassword = viper.GetString("db.password")
6766
config.Configuration.DBTablesToKeep = viper.GetStringSlice("db.keep_tables")
6867

69-
config.Configuration.Capacity = viper.GetInt64("capacity")
70-
config.Configuration.ReadPrice = viper.GetFloat64("read_price")
71-
config.Configuration.WritePrice = viper.GetFloat64("write_price")
7268
config.Configuration.PriceInUSD = viper.GetBool("price_in_usd")
73-
config.Configuration.MinLockDemand = viper.GetFloat64("min_lock_demand")
74-
config.Configuration.MaxOfferDuration = viper.GetDuration("max_offer_duration")
75-
config.Configuration.ChallengeCompletionTime = viper.GetDuration("challenge_completion_time")
7669

7770
config.Configuration.ReadLockTimeout = int64(
7871
viper.GetDuration("read_lock_timeout") / time.Second,
@@ -90,10 +83,6 @@ func setupConfig() {
9083
if w := config.Configuration.DelegateWallet; len(w) != 64 {
9184
log.Fatal("invalid delegate wallet:", w)
9285
}
93-
config.Configuration.MinStake = int64(viper.GetFloat64("min_stake") * 1e10)
94-
config.Configuration.MaxStake = int64(viper.GetFloat64("max_stake") * 1e10)
95-
config.Configuration.NumDelegates = viper.GetInt("num_delegates")
96-
config.Configuration.ServiceCharge = viper.GetFloat64("service_charge")
9786

9887
config.Configuration.MinSubmit = viper.GetInt("min_submit")
9988
if config.Configuration.MinSubmit < 1 {
@@ -117,3 +106,36 @@ func setupConfig() {
117106

118107
fmt.Print(" [OK]\n")
119108
}
109+
110+
func reloadConfig() error {
111+
fmt.Print("> reload config")
112+
113+
db := datastore.GetStore().GetDB()
114+
115+
s, ok := config.Get(context.TODO(), db)
116+
if ok {
117+
if err := s.CopyTo(&config.Configuration); err != nil {
118+
return err
119+
}
120+
fmt.Print(" [OK]\n")
121+
return nil
122+
}
123+
124+
config.Configuration.Capacity = viper.GetInt64("capacity")
125+
config.Configuration.ChallengeCompletionTime = viper.GetDuration("challenge_completion_time")
126+
config.Configuration.MaxOfferDuration = viper.GetDuration("max_offer_duration")
127+
config.Configuration.MaxStake = int64(viper.GetFloat64("max_stake") * 1e10)
128+
config.Configuration.MinLockDemand = viper.GetFloat64("min_lock_demand")
129+
config.Configuration.MinStake = int64(viper.GetFloat64("min_stake") * 1e10)
130+
config.Configuration.NumDelegates = viper.GetInt("num_delegates")
131+
config.Configuration.ReadPrice = viper.GetFloat64("read_price")
132+
config.Configuration.ServiceCharge = viper.GetFloat64("service_charge")
133+
config.Configuration.WritePrice = viper.GetFloat64("write_price")
134+
135+
if err := config.Update(context.TODO(), db); err != nil {
136+
return err
137+
}
138+
139+
fmt.Print(" [OK]\n")
140+
return nil
141+
}

code/go/0chain.net/blobber/datastore.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,55 @@ import (
1212
)
1313

1414
func setupDatabase() error {
15-
fmt.Print("\r[7/12] connect data store")
15+
fmt.Print("\r> connect data store")
1616
// check for database connection
1717
var pgDB *gorm.DB
1818
var err error
1919
for i := 0; i < 600; i++ {
20-
fmt.Printf("\r[7/12] connect(%v) data store", i)
20+
if i > 0 {
21+
fmt.Printf("\r connect(%v) data store", i)
22+
}
2123

2224
pgDB, err = datastore.GetStore().GetPgDB()
23-
if err == nil && pgDB != nil {
25+
26+
if err == nil {
2427
break
2528
}
2629

27-
if i == 599 {
30+
if i == 599 { // no more attempts
2831
logging.Logger.Error("Failed to connect to the database. Shutting the server down")
29-
return fmt.Errorf("could not get postgres db connection. Error: %v", err)
32+
return err
3033
}
3134

3235
time.Sleep(1 * time.Second)
33-
fmt.Print(" [OK]\n")
34-
3536
}
3637

37-
if !config.Configuration.DBAutoMigrate {
38-
logging.Logger.Info("Automigration is skipped")
39-
return nil
38+
if config.Configuration.DBAutoMigrate {
39+
if err := automigration.AutoMigrate(pgDB); err != nil {
40+
return fmt.Errorf("error while migrating schema: %v", err)
41+
}
4042
}
4143

42-
fmt.Println("\r[8/12] auto migrate datastore")
43-
if err := automigration.AutoMigrate(pgDB); err != nil {
44-
return fmt.Errorf("error while migrating schema: %v", err)
44+
// check for database connection
45+
for i := 0; i < 600; i++ {
46+
if i > 0 {
47+
fmt.Printf("\r connect(%v) data store", i)
48+
}
49+
50+
err = datastore.GetStore().Open()
51+
52+
if err == nil {
53+
54+
fmt.Print(" [OK]\n")
55+
break
56+
}
57+
58+
if i == 599 { // no more attempts
59+
logging.Logger.Error("Failed to connect to the database. Shutting the server down")
60+
return err
61+
}
62+
63+
time.Sleep(1 * time.Second)
4564
}
4665

4766
return nil

code/go/0chain.net/blobber/filestore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
var fsStore filestore.FileStore //nolint:unused // global which might be needed somewhere
1010

1111
func setupFileStore() (err error) {
12-
fmt.Print("[9/12] setup file store")
12+
fmt.Print("> setup file store")
1313

1414
fsStore, err = filestore.SetupFSStore(filesDir + "/files")
1515

code/go/0chain.net/blobber/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func init() {
4444
}
4545

4646
func parseFlags() {
47-
fmt.Print("[1/12] load flags")
47+
fmt.Print("> load flags")
4848
flag.Parse()
4949

5050
if filesDir == "" {

code/go/0chain.net/blobber/grpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func startGRPCServer() {
3939
panic(err)
4040
}
4141

42-
fmt.Println("[11/12] starting grpc server [OK]")
42+
fmt.Print("> starting grpc server [OK]\n")
4343
go func() {
4444
log.Fatal(grpcServer.Serve(lis))
4545
}()

code/go/0chain.net/blobber/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func startHttpServer() {
3838
go startServer(&wg, r, mode, httpsPort, true)
3939

4040
logging.Logger.Info("Ready to listen to the requests")
41-
fmt.Println("[12/12] start http server [OK]")
41+
fmt.Print("> start http server [OK]\n")
4242

4343
wg.Wait()
4444
}

code/go/0chain.net/blobber/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func setupLogging() {
12-
fmt.Print("[3/12] init logging")
12+
fmt.Print("> init logging")
1313

1414
if config.Development() {
1515
logging.InitLogging("development", logDir, "0chainBlobber.log")

code/go/0chain.net/blobber/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ import (
66
)
77

88
func main() {
9+
910
parseFlags()
1011

11-
setupConfig()
12+
setupConfig(configDir, deploymentMode)
1213

1314
setupLogging()
1415

16+
if err := setupDatabase(); err != nil {
17+
logging.Logger.Error("Error setting up data store" + err.Error())
18+
panic(err)
19+
}
20+
21+
if err := reloadConfig(); err != nil {
22+
logging.Logger.Error("Error reloading config" + err.Error())
23+
panic(err)
24+
}
25+
1526
if err := setupMinio(); err != nil {
1627
logging.Logger.Error("Error setting up minio " + err.Error())
1728
panic(err)
@@ -27,11 +38,6 @@ func main() {
2738
panic(err)
2839
}
2940

30-
if err := setupDatabase(); err != nil {
31-
logging.Logger.Error("Error setting up data store" + err.Error())
32-
panic(err)
33-
}
34-
3541
// Initialize after server chain is setup.
3642
if err := setupFileStore(); err != nil {
3743
logging.Logger.Error("Error setting up file store" + err.Error())

code/go/0chain.net/blobber/minio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func setupMinio() error {
14-
fmt.Print("[4/12] setup minio")
14+
fmt.Print("> setup minio")
1515

1616
if config.Configuration.MinioStart {
1717
fmt.Print(" + No minio [SKIP]\n")

code/go/0chain.net/blobber/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func setupNode() error {
15-
fmt.Print("[5/12] setup blobber")
15+
fmt.Print("> setup blobber")
1616

1717
reader, err := os.Open(keysFile)
1818
if err != nil {

code/go/0chain.net/blobber/worker.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/allocation"
77
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/challenge"
88
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
9+
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
910
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/handler"
1011
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/readmarker"
1112
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/writemarker"
@@ -29,29 +30,37 @@ func refreshPriceOnChain() {
2930
var REPEAT_DELAY = 60 * 60 * time.Duration(viper.GetInt("price_worker_in_hours")) // 12 hours with default settings
3031
for {
3132
time.Sleep(REPEAT_DELAY * time.Second)
32-
if err := registerBlobberOnChain(); err != nil {
33+
if err := handler.RefreshPriceOnChain(common.GetRootContext()); err != nil {
3334
logging.Logger.Error("refresh price on chain ", zap.Error(err))
3435
}
3536
}
3637
}
3738

38-
func healthCheckOnChain() {
39+
func startHealthCheck() {
3940
const REPEAT_DELAY = 60 * 15 // 15 minutes
40-
41+
var err error
4142
for {
42-
time.Sleep(REPEAT_DELAY * time.Second)
43-
txnHash, err := handler.BlobberHealthCheck()
44-
if err != nil {
45-
handler.SetBlobberHealthError(err)
43+
err = handler.SendHealthCheck()
44+
if err == nil {
45+
logging.Logger.Info("success to send heartbeat")
4646
} else {
47-
t, err := handler.TransactionVerify(txnHash)
48-
if err != nil {
49-
logging.Logger.Error("Failed to verify blobber health check", zap.Any("err", err), zap.String("txn.Hash", txnHash))
50-
} else {
51-
logging.Logger.Info("Verified blobber health check", zap.String("txn_hash", t.Hash), zap.Any("txn_output", t.TransactionOutput))
52-
}
47+
logging.Logger.Warn("failed to send heartbeat", zap.Error(err))
48+
}
49+
<-time.After(REPEAT_DELAY * time.Second)
50+
}
51+
}
5352

54-
handler.SetBlobberHealthError(err)
53+
// startRefreshSettings sync settings from chain
54+
func startRefreshSettings() {
55+
const REPEAT_DELAY = 60 * 15 // 15 minutes
56+
var err error
57+
for {
58+
err = config.Refresh(common.GetRootContext(), datastore.GetStore().GetDB())
59+
if err == nil {
60+
logging.Logger.Info("success to refresh blobber settings from chain")
61+
} else {
62+
logging.Logger.Warn("failed to refresh blobber settings from chain", zap.Error(err))
5563
}
64+
<-time.After(REPEAT_DELAY * time.Second)
5665
}
5766
}

0 commit comments

Comments
 (0)