1. add upload commit info function
2. add query commit info function
This commit is contained in:
parent
b96ca4e737
commit
6873e3b60a
|
|
@ -8,5 +8,5 @@ var (
|
|||
RepoCurBalanceDownOverFlow int64 = -1005 // 当前项目总 Token 数低于0
|
||||
UserBblanceNotEnough int64 = -1006 // 用户余额不足
|
||||
|
||||
ContractAddress string = "0x48e9Ff2BbA3e6C4aCa2F93749F051E1008DD0F45"
|
||||
ContractAddress string = "0x7E1b1605f299D0E6179B206B0688Ff8762E0974a"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[{"constant":true,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"}],"name":"selectUserBalance","outputs":[{"name":"","type":"string[]"},{"name":"","type":"string[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"minusUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"addUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"payer","type":"string"},{"name":"payee","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"transferUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"username","type":"string"}],"name":"selectUserAllBalance","outputs":[{"name":"","type":"string[]"},{"name":"","type":"string[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"token_name","type":"string"},{"name":"owner","type":"string"},{"name":"total_supply","type":"uint256"},{"name":"cur_supply","type":"uint256"},{"name":"username","type":"string[]"},{"name":"balance","type":"uint256[]"}],"name":"createRepo","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"token_name","type":"string"}],"name":"selectRepoBasicInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"int256"},{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
|
||||
[{"constant":true,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"}],"name":"selectUserBalance","outputs":[{"name":"","type":"string[]"},{"name":"","type":"string[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"minusUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"commit_hash","type":"string"}],"name":"selectCommitInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"commit_hash","type":"string"},{"name":"repo_id","type":"string"},{"name":"author","type":"string"},{"name":"email","type":"string"},{"name":"time","type":"string"},{"name":"content","type":"string"},{"name":"commit_diff","type":"string"}],"name":"addCommitData","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"username","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"addUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"payer","type":"string"},{"name":"payee","type":"string"},{"name":"token_name","type":"string"},{"name":"amount","type":"uint256"}],"name":"transferUserBalance","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"username","type":"string"}],"name":"selectUserAllBalance","outputs":[{"name":"","type":"string[]"},{"name":"","type":"string[]"},{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"token_name","type":"string"},{"name":"owner","type":"string"},{"name":"total_supply","type":"uint256"},{"name":"cur_supply","type":"uint256"},{"name":"username","type":"string[]"},{"name":"balance","type":"uint256[]"}],"name":"createRepo","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"token_name","type":"string"}],"name":"selectRepoBasicInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"int256"},{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -7,12 +7,14 @@ contract OpenSource {
|
|||
TableFactory tableFactory;
|
||||
string constant REPO_TABLE = "repo";
|
||||
string constant USER_TABLE = "user";
|
||||
string constant COMMIT_TABLE = "commit";
|
||||
|
||||
constructor() public {
|
||||
tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
|
||||
// the parameters of createTable are tableName,keyField,"vlaueFiled1,vlaueFiled2,vlaueFiled3,..."
|
||||
tableFactory.createTable(REPO_TABLE, "token_name", "owner,total_supply,cur_supply");
|
||||
tableFactory.createTable(USER_TABLE, "user", "token_name,balance");
|
||||
tableFactory.createTable(COMMIT_TABLE, "commit_hash", "repo_id,author,email,time,content,commit_diff");
|
||||
}
|
||||
|
||||
// create repo
|
||||
|
|
@ -331,4 +333,61 @@ contract OpenSource {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// add commit info
|
||||
function addCommitData(string memory commit_hash, string memory repo_id, string memory author, string memory email,
|
||||
string memory time, string memory content, string memory commit_diff)
|
||||
public
|
||||
returns (int256)
|
||||
{
|
||||
Table commit_table = tableFactory.openTable(COMMIT_TABLE);
|
||||
Entry commit_entry = commit_table.newEntry();
|
||||
commit_entry.set("commit_hash", commit_hash);
|
||||
commit_entry.set("repo_id", repo_id);
|
||||
commit_entry.set("author", author);
|
||||
commit_entry.set("email", email);
|
||||
commit_entry.set("time", time);
|
||||
commit_entry.set("content", content);
|
||||
commit_entry.set("commit_diff", commit_diff);
|
||||
|
||||
int256 count = commit_table.insert(commit_hash, commit_entry);
|
||||
return count;
|
||||
}
|
||||
|
||||
// select commit info
|
||||
function selectCommitInfo(string memory commit_hash)
|
||||
public
|
||||
view
|
||||
returns (string memory, string memory, string memory, string memory, string memory, string memory, string memory)
|
||||
{
|
||||
Table commit_table = tableFactory.openTable(COMMIT_TABLE);
|
||||
Condition condition = commit_table.newCondition();
|
||||
Entries entries = commit_table.select(commit_hash, condition);
|
||||
if (entries.size() == 0) {
|
||||
return ("", "", "", "","","","");
|
||||
}
|
||||
Entry entry = entries.get(entries.size()-1);
|
||||
return (entry.getString("commit_hash"), entry.getString("repo_id"), entry.getString("author"), entry.getString("email"),
|
||||
entry.getString("time"), entry.getString("content"), entry.getString("commit_diff"));
|
||||
}
|
||||
|
||||
// // add push info
|
||||
// function addPushData(string memory push_id, string memory push_number, string memory repo_id, string memory reponame,
|
||||
// string memory ownername, string memory username, string memory branch, string memory size, string memory branch, string memory branch)
|
||||
// public
|
||||
// returns (int256)
|
||||
// {
|
||||
// Table commit_table = tableFactory.openTable(COMMIT_TABLE);
|
||||
// Entry commit_entry = commit_table.newEntry();
|
||||
// commit_entry.set("commit_hash", commit_hash);
|
||||
// commit_entry.set("repo_id", repo_id);
|
||||
// commit_entry.set("author", author);
|
||||
// commit_entry.set("email", email);
|
||||
// commit_entry.set("time", time);
|
||||
// commit_entry.set("content", content);
|
||||
// commit_entry.set("commit_diff", commit_diff);
|
||||
|
||||
// int256 count = commit_table.insert(commit_hash, commit_entry);
|
||||
// return count;
|
||||
// }
|
||||
}
|
||||
|
|
@ -10,6 +10,15 @@ type FiscoBcos struct {
|
|||
CurSupply uint64 `json:"cur_supply"` // 项目当前金额
|
||||
TokenBalance [][]interface{} `json:"token_balance"` // 用户初始Token分配
|
||||
|
||||
// repo commit 相关
|
||||
CommitHash string `json:"commit_hash"`
|
||||
RepoID string `json:"repo_id"`
|
||||
Author string `json:"author"`
|
||||
Email string `json:"email"`
|
||||
Time string `json:"time"`
|
||||
Content string `json:"message"`
|
||||
CommitDiff string `json:"commit_diff"`
|
||||
|
||||
// user 相关
|
||||
Username string `json:"username"` // 用户名
|
||||
Amount uint64 `json:"amount"` // 转账金额
|
||||
|
|
|
|||
|
|
@ -2,10 +2,16 @@ package structs
|
|||
|
||||
// UploadCommitOption is to upload commit info to blockchain
|
||||
type UploadCommitOption struct {
|
||||
Sha string `json:"sha" binding:"Required"` // sha 值
|
||||
Branch string `json:"branch" binding:"Required"` // 分支
|
||||
Ownername string `json:"ownername" binding:"Required"` // 项目创始人
|
||||
Reponame string `json:"reponame" binding:"Required"` // 项目名
|
||||
Username string `json:"username" binding:"Required"` // 用户名
|
||||
ModificationLines int `json:"modification_lines" binding:"Required"` //修改行数
|
||||
CommitHash string `json:"commit_hash"`
|
||||
RepoID string `json:"repo_id"`
|
||||
Author string `json:"author"`
|
||||
Email string `json:"email"`
|
||||
Time string `json:"time"`
|
||||
Content string `json:"content"`
|
||||
CommitDiff string `json:"commit_diff"`
|
||||
}
|
||||
|
||||
type ResponseCommit struct {
|
||||
UploadCommitOption
|
||||
Response
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,10 @@ var (
|
|||
ResRepoBalanceOverFlow = &Response{Status: 2, Message: "username balance overflow when create repo!"} // username balance overflow when create repo!
|
||||
ResRepoUnsucc = &Response{Status: 3, Message: "create repo unsuccessfully!"} // create repo unsuccessfully!
|
||||
ResRepoCurBalanceUpOverFlow = &Response{Status: 4, Message: "repo current balance up overflow when user adds amount!"} // username balance overflow when create repo!
|
||||
ResRepoCurBalanceDownOverFlow = &Response{Status: 4, Message: "repo current balance less than 0 when user adds amount!"}
|
||||
ResRepoCurBalanceDownOverFlow = &Response{Status: 6, Message: "repo current balance less than 0 when user adds amount!"}
|
||||
|
||||
ResCommitUploadSucc = &Response{Status: 0, Message: "commit upload successfully!"}
|
||||
ResCommitUploadUnsucc = &Response{Status: 7, Message: "commit upload unsuccessfully!"}
|
||||
|
||||
ResUserNotExisted = &Response{Status: 100, Message: "user not exist!"}
|
||||
ResUserAddAmountSucc = &Response{Status: 0, Message: "user adds amount successfully!"}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,20 @@ func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger
|
|||
logger)
|
||||
case "query repo basic info":
|
||||
repo.QueryBasic(ctx, opt.TokenName, logger)
|
||||
case "upload commit info":
|
||||
repo.UploadCommitInfo(ctx,
|
||||
api.UploadCommitOption{
|
||||
CommitHash: opt.CommitHash,
|
||||
RepoID: opt.RepoID,
|
||||
Author: opt.Author,
|
||||
Email: opt.Email,
|
||||
Time: opt.Time,
|
||||
Content: opt.Content,
|
||||
CommitDiff: opt.CommitDiff,
|
||||
},
|
||||
logger)
|
||||
case "query commit info":
|
||||
repo.SelectCommitInfo(ctx, opt.CommitHash, logger)
|
||||
case "query user balance of single repo":
|
||||
user.SelectUserBalance(ctx, opt.Username, opt.TokenName, logger)
|
||||
case "query user balance of all repos":
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/FISCO-BCOS/go-sdk/client"
|
||||
"github.com/FISCO-BCOS/go-sdk/conf"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/sulenn/trustie-fisco-bcos/contract"
|
||||
"github.com/sulenn/trustie-fisco-bcos/contract/opensource"
|
||||
"github.com/sulenn/trustie-fisco-bcos/modules/structs"
|
||||
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
|
||||
|
||||
"gitea.com/macaron/macaron"
|
||||
)
|
||||
|
||||
var ()
|
||||
|
||||
// 查询 commit 数据
|
||||
func SelectCommitInfo(ctx *macaron.Context, commitHash string, logger *log.Logger) {
|
||||
if commitHash == "" {
|
||||
ctx.JSON(http.StatusOK, api.StringEmpty)
|
||||
return
|
||||
}
|
||||
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// load the contract
|
||||
contractAddress := common.HexToAddress(contract.ContractAddress)
|
||||
instance, err := opensource.NewOpenSource(contractAddress, client)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
|
||||
|
||||
_, repoID, author, email, time, message, commitDiff, err := openSourceSession.SelectCommitInfo(commitHash) // call Insert API
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("commitHash: %v, repoID: %v, author: %v, email: %v, curStimeupply: %v, message: %v, commitDiff: %v \n", commitHash, repoID, author, email, time, message, commitDiff)
|
||||
|
||||
ctx.JSON(http.StatusOK, &structs.ResponseCommit{
|
||||
Response: structs.Response{
|
||||
Status: 0,
|
||||
Message: "query success!",
|
||||
},
|
||||
UploadCommitOption: structs.UploadCommitOption{
|
||||
CommitHash: commitHash,
|
||||
RepoID: repoID,
|
||||
Author: author,
|
||||
Email: email,
|
||||
Time: time,
|
||||
Content: message,
|
||||
CommitDiff: commitDiff,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/FISCO-BCOS/go-sdk/client"
|
||||
"github.com/FISCO-BCOS/go-sdk/conf"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/sulenn/trustie-fisco-bcos/contract"
|
||||
"github.com/sulenn/trustie-fisco-bcos/contract/opensource"
|
||||
"github.com/sulenn/trustie-fisco-bcos/modules/structs"
|
||||
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
|
||||
|
||||
"gitea.com/macaron/macaron"
|
||||
)
|
||||
|
||||
var ()
|
||||
|
||||
// 上传 commit 数据
|
||||
func UploadCommitInfo(ctx *macaron.Context, opt api.UploadCommitOption, logger *log.Logger) {
|
||||
commitHash := opt.CommitHash
|
||||
repoID := opt.RepoID
|
||||
author := opt.Author
|
||||
email := opt.Email
|
||||
time := opt.Time
|
||||
content := opt.Content
|
||||
commitDiff := opt.CommitDiff
|
||||
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// load the contract
|
||||
contractAddress := common.HexToAddress(contract.ContractAddress)
|
||||
instance, err := opensource.NewOpenSource(contractAddress, client)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
|
||||
|
||||
tx, receipt, err := openSourceSession.AddCommitData(commitHash, repoID, author, email, time, content, commitDiff) // call Insert API
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
logger.Printf("tx sent: %s\n", tx.Hash().Hex())
|
||||
// insertedLines, err := strconv.Atoi(receipt.Output[2:])
|
||||
code, err := parseOutput(opensource.OpenSourceABI, "addCommitData", receipt)
|
||||
if err != nil {
|
||||
logger.Panic("error when transfer string to int: ", err)
|
||||
}
|
||||
if code.Int64() > 0 {
|
||||
logger.Printf("inserted lines: %v\n", code)
|
||||
ctx.JSON(http.StatusOK, structs.ResCommitUploadSucc)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, structs.ResCommitUploadUnsucc)
|
||||
}
|
||||
Loading…
Reference in New Issue