diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index d01866078..dcc40e1b1 100755 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -1065,3 +1065,6 @@ HOST = http://192.168.202.90:3366/ HOST = http://192.168.207.34:39987 USER = cW4cMtH24eoWPE7X PASSWORD = 4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC + +[blockchain] +HOST = http://192.168.207.84:3002/ diff --git a/models/blockchain.go b/models/blockchain.go new file mode 100755 index 000000000..df7af729b --- /dev/null +++ b/models/blockchain.go @@ -0,0 +1,23 @@ +package models + +import ( + "code.gitea.io/gitea/modules/timeutil" + "time" +) + +type BlockChain struct { + ID int64 `xorm:"pk autoincr"` + Contributor string `xorm:"INDEX NOT NULL"` + ContractAddress string `xorm:"INDEX NOT NULL"` + Action string `xorm:"INDEX"` + Amount int64 `xorm:"INDEX"` + UserID int64 `xorm:"INDEX"` + RepoID int64 `xorm:"INDEX"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` + UpdatedUnix timeutil.TimeStamp `xorm:"updated"` + DeletedAt time.Time `xorm:"deleted"` + + User *User `xorm:"-"` + Repo *Repository `xorm:"-"` +} + diff --git a/models/models.go b/models/models.go index b709a2d28..4f7683774 100755 --- a/models/models.go +++ b/models/models.go @@ -128,6 +128,7 @@ func init() { new(Dataset), new(Cloudbrain), new(FileChunk), + new(BlockChain), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/repo.go b/models/repo.go index 52a194ab0..862957787 100755 --- a/models/repo.go +++ b/models/repo.go @@ -6,6 +6,7 @@ package models import ( + "code.gitea.io/gitea/modules/blockchain" "context" "crypto/md5" "errors" @@ -195,6 +196,10 @@ type Repository struct { // Avatar: ID(10-20)-md5(32) - must fit into 64 symbols Avatar string `xorm:"VARCHAR(64)"` + //blockchain + ContractAddress string `xorm:"INDEX"` + Balance int64 `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } @@ -1051,6 +1056,11 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository) (err error return err } + _, err = blockchain.NewRepo(string(repo.ID), u.PublicKey, repo.Name) + if err != nil { + log.Error("newRepo failed:", err.Error()) + } + // insert units for repo var units = make([]RepoUnit, 0, len(DefaultRepoUnits)) for _, tp := range DefaultRepoUnits { diff --git a/models/user.go b/models/user.go index 29e1a5d9f..468d42822 100755 --- a/models/user.go +++ b/models/user.go @@ -6,6 +6,7 @@ package models import ( + "code.gitea.io/gitea/modules/blockchain" "container/list" "context" "crypto/md5" @@ -35,6 +36,7 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" + "github.com/go-resty/resty/v2" "github.com/unknwon/com" "golang.org/x/crypto/argon2" "golang.org/x/crypto/bcrypt" @@ -91,6 +93,8 @@ var ( // Characters prohibited in a user name (anything except A-Za-z0-9_.-) alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`) + + restyClient *resty.Client ) // User represents the object of individual and member of organization. @@ -167,7 +171,10 @@ type User struct { Theme string `xorm:"NOT NULL DEFAULT ''"` //CloudBrain - Token string `xorm:"VARCHAR(1024)"` + Token string `xorm:"VARCHAR(1024)"` + + //BlockChain + PublicKey string `xorm` } // SearchOrganizationsOptions options to filter organizations @@ -965,6 +972,14 @@ func CreateUser(u *User) (err error) { return err } + result, err := blockchain.CreateBlockchainAccount() + if err != nil { + log.Error("createBlockchainAccount failed:", err.Error()) + return err + } + + u.PublicKey = result.Payload["publickey"].(string) + sess := x.NewSession() defer sess.Close() if err = sess.Begin(); err != nil { diff --git a/modules/blockchain/blockchain.go b/modules/blockchain/blockchain.go new file mode 100755 index 000000000..6ebf701da --- /dev/null +++ b/modules/blockchain/blockchain.go @@ -0,0 +1,14 @@ +package blockchain + +const ( + Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"` + CodeMountPath = "/code" + DataSetMountPath = "/dataset" + ModelMountPath = "/model" + BenchMarkMountPath = "/benchmark" + TaskInfoName = "/taskInfo" + + SubTaskName = "task1" + + +) diff --git a/modules/blockchain/resty.go b/modules/blockchain/resty.go new file mode 100755 index 000000000..d4f06624c --- /dev/null +++ b/modules/blockchain/resty.go @@ -0,0 +1,145 @@ +package blockchain + +import ( + "fmt" + + "code.gitea.io/gitea/modules/setting" + "github.com/go-resty/resty/v2" +) + +var ( + restyClient *resty.Client +) + +const ( + UrlCreateAccount = "createAccount" + UrlGetBalance = "getBalance" + UrlNewRepo = "newRepo" + UrlContribute = "contribute" + + Success = 0 +) + +type CreateAccountResult struct { + Code int `json:"code"` + Msg string `json:"message"` + Payload map[string]interface{} `json:"data"` +} + +type GetBalanceResult struct { + Code int `json:"code"` + Msg string `json:"message"` + Payload map[string]interface{} `json:"data"` +} + +type NewRepoResult struct { + Code int `json:"code"` + Msg string `json:"message"` + Data string `json:"data"` +} + +type ContributeResult struct { + Code int `json:"code"` + Msg string `json:"message"` + Payload map[string]interface{} `json:"data"` +} + +func getRestyClient() *resty.Client { + if restyClient == nil { + restyClient = resty.New() + } + return restyClient +} + +func CreateBlockchainAccount() (*CreateAccountResult, error) { + client := getRestyClient() + var result CreateAccountResult + + res, err := client.R(). + SetHeader("Content-Type", "application/json"). + SetResult(&result). + Get(setting.BlockChainHost + UrlCreateAccount) + + if err != nil { + return nil, fmt.Errorf("resty create account: %s", err) + } + + if result.Code != Success { + return &result, fmt.Errorf("CreateAccount err: %s", res.String()) + } + + return &result, nil +} + +func NewRepo(repoID, publicKey, repoName string) (*NewRepoResult, error) { + client := getRestyClient() + var result NewRepoResult + + res, err := client.R(). + SetHeader("Accept", "application/json"). + SetQueryParams(map[string]string{ + "repoId" : repoID, + "creator" : publicKey, + "repoName" : repoName, + }). + SetResult(&result). + Get(setting.BlockChainHost + UrlNewRepo) + + if err != nil { + return nil, fmt.Errorf("resty newRepo: %v", err) + } + + if result.Code != Success { + return &result, fmt.Errorf("newRepo err: %s", res.String()) + } + + return &result, nil +} + +func GetBalance(contractAddress, contributor string) (*GetBalanceResult, error) { + client := getRestyClient() + var result GetBalanceResult + + res, err := client.R(). + SetHeader("Accept", "application/json"). + SetQueryParams(map[string]string{ + "contractAddress" : contractAddress, + "contributor" : contributor, + }). + SetResult(&result). + Get(setting.BlockChainHost + UrlGetBalance) + + if err != nil { + return nil, fmt.Errorf("resty getBalance: %v", err) + } + + if result.Code != Success { + return &result, fmt.Errorf("getBalance err: %s", res.String()) + } + + return &result, nil +} + +func Contribute(jobID string) (*ContributeResult, error) { + client := getRestyClient() + var result ContributeResult + + res, err := client.R(). + SetHeader("Accept", "application/json"). + SetQueryParams(map[string]string{ + "contractAddress" : "", + "contributor" : "", + }). + SetResult(&result). + Get(setting.BlockChainHost + UrlContribute) + + if err != nil { + return nil, fmt.Errorf("resty contribute: %v", err) + } + + if result.Code != Success { + return &result, fmt.Errorf("contribute err: %s", res.String()) + } + + return &result, nil +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index a71617e8d..3330e79cc 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -443,6 +443,9 @@ var ( IsBenchmarkEnabled bool BenchmarkCode string BenchmarkServerHost string + + //blockchain config + BlockChainHost string ) // DateLang transforms standard language locale name to corresponding value in datetime plugin. @@ -1123,6 +1126,9 @@ func NewContext() { IsBenchmarkEnabled = sec.Key("ENABLED").MustBool(false) BenchmarkCode = sec.Key("BENCHMARKCODE").MustString("https://yangzhx:justfortest123@git.openi.org.cn/yangzhx/detection_benchmark_script.git") BenchmarkServerHost = sec.Key("HOST").MustString("http://192.168.202.90:3366/") + + sec = Cfg.Section("blockchain") + BlockChainHost = sec.Key("HOST").MustString("http://192.168.136.66:3302/") } func loadInternalToken(sec *ini.Section) string { diff --git a/modules/worker/task.go b/modules/worker/task.go index 073b16b92..48ba32c3e 100755 --- a/modules/worker/task.go +++ b/modules/worker/task.go @@ -14,7 +14,7 @@ const ( ) func SendDecompressTask(ctx context.Context, uuid string) error { - args := []tasks.Arg{{Name: "uuid", Type: "string", Value: uuid}} + args := []tasks.Arg{{Name: "uuid", Type: "string", Value: uuid},{}} task, err := tasks.NewSignature(DecompressTaskName, args) if err != nil { log.Error("NewSignature failed:", err.Error())