Skip to content

Commit

Permalink
Merge pull request #111 from swanchain/fix-check-repo
Browse files Browse the repository at this point in the history
Fix check repo
  • Loading branch information
Normalnoise authored Jul 16, 2024
2 parents 09b2e20 + 37ab360 commit 7d0a517
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var CurrentCommit string

var NetWorkTag string

const BuildVersion = "0.6.0"
const BuildVersion = "0.6.1"

const UBITaskImageIntelCpu = "filswan/ubi-worker-cpu-intel:v2.0"
const UBITaskImageIntelGpu = "filswan/ubi-worker-gpu-intel:v2.0"
Expand Down
8 changes: 7 additions & 1 deletion cmd/computing-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/swanchain/go-computing-provider/build"
"github.com/swanchain/go-computing-provider/conf"
"github.com/swanchain/go-computing-provider/internal/db"
"github.com/urfave/cli/v2"
"os"
Expand Down Expand Up @@ -63,7 +64,12 @@ func main() {
return fmt.Errorf("CP_PATH: %s, no such directory", cpRepoPath)
}
os.Setenv("CP_PATH", cpRepoPath)
db.InitDb(cpRepoPath)
if !strings.EqualFold(c.Args().First(), initCmd.Name) {
if !conf.Exists(cpRepoPath) {
return fmt.Errorf("repo at '%s' not exist, please initialize it", cpRepoPath)
}
db.InitDb(cpRepoPath)
}

return nil
},
Expand Down
2 changes: 0 additions & 2 deletions cmd/computing-provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ func createAccount(cpRepoPath, ownerAddress, beneficiaryAddress string, workerAd
}

auth.Nonce = big.NewInt(int64(nonce))
suggestGasPrice = suggestGasPrice.Mul(suggestGasPrice, big.NewInt(3))
suggestGasPrice = suggestGasPrice.Div(suggestGasPrice, big.NewInt(2))
auth.GasFeeCap = suggestGasPrice
auth.Context = context.Background()

Expand Down
37 changes: 21 additions & 16 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,7 @@ func requiredFieldsAreGivenForSeparate(metaData toml.MetaData) bool {
func GenerateAndUpdateConfigFile(cpRepoPath string, multiAddress, nodeName string, port int) error {
fmt.Println("Checking if repo exists")

ok, err := Exists(cpRepoPath)
if err != nil {
return err
}
if ok {
if Exists(cpRepoPath) {
return fmt.Errorf("repo at '%s' is already initialized", cpRepoPath)
}

Expand Down Expand Up @@ -253,6 +249,17 @@ func GenerateAndUpdateConfigFile(cpRepoPath string, multiAddress, nodeName strin
if err = toml.NewEncoder(configFile).Encode(configTmpl); err != nil {
return err
}

file, err := os.Create(path.Join(cpRepoPath, "provider.db"))
if err != nil {
return err
}
defer file.Close()

if err = os.MkdirAll(path.Join(cpRepoPath, "keystore"), 0755); err != nil {
return fmt.Errorf("failed to create keystore, error: %v", err)
}

fmt.Printf("Initialized CP repo at '%s'. \n", cpRepoPath)
return nil
}
Expand Down Expand Up @@ -303,17 +310,15 @@ func generateDefaultConfig() ComputeNode {
}
}

func Exists(cpPath string) (bool, error) {
func Exists(cpPath string) bool {
_, err := os.Stat(filepath.Join(cpPath, "keystore"))
notexist := os.IsNotExist(err)
if notexist {
err = nil

_, err = os.Stat(filepath.Join(cpPath, "provider.db"))
notexist = os.IsNotExist(err)
if notexist {
err = nil
}
KeyStoreNoExist := os.IsNotExist(err)
err = nil
_, err = os.Stat(filepath.Join(cpPath, "provider.db"))
providerNotExist := os.IsNotExist(err)

if KeyStoreNoExist && providerNotExist {
return false
}
return !notexist, err
return true
}
3 changes: 1 addition & 2 deletions internal/computing/cp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ func StatisticalSources(c *gin.Context) {
location, err := getLocation()
if err != nil {
logs.GetLogger().Error(err)
c.JSON(http.StatusInternalServerError, util.CreateErrorResponse(util.GetLocationError))
return
location = "-"
}

k8sService := NewK8sService()
Expand Down
8 changes: 1 addition & 7 deletions internal/computing/cron_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var TaskMap sync.Map
type CronTask struct {
nodeId string
ownerAddress string
location string
}

func NewCronTask(nodeId string) *CronTask {
Expand All @@ -37,12 +36,7 @@ func NewCronTask(nodeId string) *CronTask {
return nil
}

location, err := getLocation()
if err != nil {
logs.GetLogger().Error(err)
}

return &CronTask{nodeId: nodeId, ownerAddress: ownerAddress, location: location}
return &CronTask{nodeId: nodeId, ownerAddress: ownerAddress}
}

func (task *CronTask) RunTask() {
Expand Down
3 changes: 1 addition & 2 deletions internal/computing/ubi.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,7 @@ func GetCpResource(c *gin.Context) {
location, err := getLocation()
if err != nil {
logs.GetLogger().Error(err)
c.JSON(http.StatusInternalServerError, util.CreateErrorResponse(util.ServerError, err.Error()))
return
location = "-"
}

dockerService := NewDockerService()
Expand Down
3 changes: 1 addition & 2 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
_ "embed"
logging "github.com/ipfs/go-log/v2"
"github.com/swanchain/go-computing-provider/internal/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -12,10 +11,10 @@ import (
const cpDBName = "provider.db"

var DB *gorm.DB
var dblog = logging.Logger("db")

func InitDb(cpRepoPath string) {
var err error

DB, err = gorm.Open(sqlite.Open(path.Join(cpRepoPath, cpDBName)), &gorm.Config{})
if err != nil {
panic("failed to connect database")
Expand Down

0 comments on commit 7d0a517

Please sign in to comment.