You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockchain.go 7.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/base"
  5. "code.gitea.io/gitea/modules/blockchain"
  6. "code.gitea.io/gitea/modules/context"
  7. "code.gitea.io/gitea/modules/log"
  8. "encoding/json"
  9. "net/http"
  10. "strconv"
  11. )
  12. type BlockChainInitNotify struct {
  13. RepoId int64 `json:"repoId"`
  14. ContractAddress string `json:"contractAddress"`
  15. }
  16. type BlockChainCommitNotify struct {
  17. CommitID string `json:"commitId"`
  18. TransactionHash string `json:"txHash"`
  19. }
  20. const (
  21. tplBlockChainIndex base.TplName = "repo/blockchain/index"
  22. )
  23. func BlockChainIndex(ctx *context.Context) {
  24. repo := ctx.Repo.Repository
  25. if repo.ContractAddress == "" || ctx.User.PublicKey == "" {
  26. log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", repo.RepoID, ctx.User.ID, ctx.Data["msgID"])
  27. ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex)
  28. return
  29. }
  30. res, err := blockchain.GetBalance(repo.ContractAddress, ctx.User.PublicKey)
  31. if err != nil {
  32. log.Error("GetBalance(%s) failed:%s", ctx.User.PublicKey, err, ctx.Data["msgID"])
  33. ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex)
  34. return
  35. }
  36. ctx.Data["balance"] = res.Data
  37. ctx.Data["PageIsBlockChain"] = true
  38. ctx.HTML(200, tplBlockChainIndex)
  39. }
  40. func HandleBlockChainInitNotify(ctx *context.Context) {
  41. var req BlockChainInitNotify
  42. data, _ := ctx.Req.Body().Bytes()
  43. json.Unmarshal(data, &req)
  44. repo, err := models.GetRepositoryByID(req.RepoId)
  45. if err != nil {
  46. log.Error("GetRepositoryByID failed:%v", err.Error(), ctx.Data["msgID"])
  47. ctx.JSON(200, map[string]string{
  48. "code": "-1",
  49. "message": "internal error",
  50. })
  51. return
  52. }
  53. if repo.BlockChainStatus == models.RepoBlockChainSuccess && len(repo.ContractAddress) != 0 {
  54. log.Error("the repo has been RepoBlockChainSuccess:%d", req.RepoId, ctx.Data["msgID"])
  55. ctx.JSON(200, map[string]string{
  56. "code": "-1",
  57. "message": "the repo has been RepoBlockChainSuccess",
  58. })
  59. return
  60. }
  61. repo.BlockChainStatus = models.RepoBlockChainSuccess
  62. repo.ContractAddress = req.ContractAddress
  63. if err = repo.UpdateBlockChain(); err != nil {
  64. log.Error("UpdateRepositoryCols failed:%v", err.Error(), ctx.Data["msgID"])
  65. ctx.JSON(200, map[string]string{
  66. "code": "-1",
  67. "message": "internal error",
  68. })
  69. return
  70. }
  71. ctx.JSON(200, map[string]string{
  72. "code": "0",
  73. "message": "",
  74. })
  75. }
  76. func HandleBlockChainCommitNotify(ctx *context.Context) {
  77. var req BlockChainCommitNotify
  78. data, _ := ctx.Req.Body().Bytes()
  79. if err := json.Unmarshal(data, &req); err != nil {
  80. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  81. ctx.JSON(200, map[string]string{
  82. "code": "-1",
  83. "message": "response data error",
  84. })
  85. return
  86. }
  87. blockChain, err := models.GetBlockChainByCommitID(req.CommitID)
  88. if err != nil {
  89. log.Error("GetRepositoryByID failed:%v", err.Error(), ctx.Data["msgID"])
  90. ctx.JSON(200, map[string]string{
  91. "code": "-1",
  92. "message": "internal error",
  93. })
  94. return
  95. }
  96. if blockChain.Status == models.BlockChainCommitSuccess {
  97. log.Error("the commit has been BlockChainCommitReady:%s", blockChain.RepoID, ctx.Data["msgID"])
  98. ctx.JSON(200, map[string]string{
  99. "code": "-1",
  100. "message": "the commit has been BlockChainCommitReady",
  101. })
  102. return
  103. }
  104. blockChain.Status = models.BlockChainCommitSuccess
  105. blockChain.TransactionHash = req.TransactionHash
  106. if err = models.UpdateBlockChainCols(blockChain, "status", "transaction_hash"); err != nil {
  107. log.Error("UpdateBlockChainCols failed:%v", err.Error(), ctx.Data["msgID"])
  108. ctx.JSON(200, map[string]string{
  109. "code": "-1",
  110. "message": "internal error",
  111. })
  112. return
  113. }
  114. ctx.JSON(200, map[string]string{
  115. "code": "0",
  116. "message": "",
  117. })
  118. }
  119. func HandleBlockChainUnSuccessRepos() {
  120. repos, err := models.GetBlockChainUnSuccessRepos()
  121. if err != nil {
  122. log.Error("GetBlockChainUnSuccessRepos failed:", err.Error())
  123. return
  124. }
  125. for _, repo := range repos {
  126. err = repo.GetOwner()
  127. if err != nil {
  128. log.Error("GetOwner(%s) failed:%v", repo.Name, err)
  129. continue
  130. }
  131. if len(repo.Owner.PrivateKey) == 0 || len(repo.Owner.PublicKey) == 0 {
  132. log.Error("the user has not been init in block_chain:", repo.Owner.Name)
  133. continue
  134. }
  135. strRepoID := strconv.FormatInt(repo.ID, 10)
  136. _, err = blockchain.NewRepo(strRepoID, repo.Owner.PublicKey, repo.Name)
  137. if err != nil {
  138. log.Error("blockchain.NewRepo(%s) failed:%v", strRepoID, err)
  139. }
  140. }
  141. return
  142. }
  143. func HandleBlockChainUnSuccessCommits() {
  144. blockChains, err := models.GetBlockChainUnSuccessCommits()
  145. if err != nil {
  146. log.Error("GetBlockChainUnSuccessCommits failed:", err.Error())
  147. return
  148. }
  149. for _, block_chain := range blockChains {
  150. _, err = blockchain.Contribute(block_chain.ContractAddress, block_chain.Contributor, block_chain.CommitID, block_chain.Amount)
  151. if err != nil {
  152. log.Error("blockchain.Contribute(%s) failed:%v", block_chain.CommitID, err)
  153. }
  154. }
  155. return
  156. }
  157. func HandleBlockChainUnSuccessUsers() {
  158. users, err := models.GetBlockChainUnSuccessUsers()
  159. if err != nil {
  160. log.Error("GetBlockChainUnSuccessUsers failed:", err.Error())
  161. return
  162. }
  163. for _, user := range users {
  164. result, err := blockchain.CreateBlockchainAccount()
  165. if err != nil {
  166. log.Error("blockchain.CreateBlockchainAccount(%s) failed:%v", user.Name, err)
  167. continue
  168. }
  169. user.PublicKey = result.Payload["publickey"].(string)
  170. user.PrivateKey = result.Payload["privatekey"].(string)
  171. models.UpdateUser(user)
  172. }
  173. return
  174. }
  175. func HandleBlockChainMergedPulls() {
  176. prs, err := models.GetUnTransformedMergedPullRequests()
  177. if err != nil {
  178. log.Error("GetUnTransformedMergedPullRequests failed:", err.Error())
  179. return
  180. }
  181. for _, pr := range prs {
  182. _, err = models.GetBlockChainByPrID(pr.ID)
  183. if err == nil {
  184. log.Info("the pr(%s) has been transformed", pr.MergedCommitID)
  185. continue
  186. }
  187. err = pr.LoadIssue()
  188. if err != nil {
  189. log.Error("LoadIssue(%s) failed:%v", pr.MergedCommitID, err)
  190. continue
  191. }
  192. poster, err := models.GetUserByID(pr.Issue.PosterID)
  193. if err != nil {
  194. log.Error("GetUserByID(%s) failed:%v", pr.MergedCommitID, err)
  195. continue
  196. }
  197. if len(poster.PrivateKey) == 0 || len(poster.PublicKey) == 0 {
  198. log.Error("the user has not been init in block_chain:", poster.Name)
  199. continue
  200. }
  201. repo, err := models.GetRepositoryByID(pr.HeadRepoID)
  202. if err != nil {
  203. log.Error("GetUserByID(%s) failed:%v", pr.MergedCommitID, err)
  204. continue
  205. }
  206. if len(repo.ContractAddress) == 0 {
  207. log.Error("the repo(%s) has not been initialized in block_chain", repo.Name)
  208. continue
  209. }
  210. blockChain := models.BlockChain{
  211. Contributor: poster.PublicKey,
  212. PrID: pr.ID,
  213. CommitID: pr.MergedCommitID,
  214. ContractAddress: repo.ContractAddress,
  215. Status: models.BlockChainCommitInit,
  216. Amount: int64(pr.Amount),
  217. UserID: poster.ID,
  218. RepoID: pr.HeadRepoID,
  219. }
  220. _, err = models.InsertBlockChain(&blockChain)
  221. if err != nil {
  222. log.Error("InsertBlockChain(%s) failed:%v", pr.MergedCommitID, err)
  223. continue
  224. }
  225. pr.IsTransformed = true
  226. pr.UpdateCols("is_transformed")
  227. _, err = blockchain.Contribute(repo.ContractAddress, poster.PublicKey, pr.MergedCommitID, int64(pr.Amount))
  228. if err != nil {
  229. log.Error("Contribute(%s) failed:%v", pr.MergedCommitID, err)
  230. }
  231. }
  232. return
  233. }
  234. func HandleBlockChainUnSuccessIssues() {
  235. issues, err := models.GetBlockChainUnSuccessCommits()
  236. if err != nil {
  237. log.Error("GetBlockChainUnSuccessIssues failed:", err.Error())
  238. return
  239. }
  240. for _, issue := range issues {
  241. _, err = blockchain.SetIssue(issue.ContractAddress, issue.Contributor, issue.ID, issue.Amount)
  242. if err != nil {
  243. log.Error("SetIssue(%s) failed:%v", issue.CommitID, err)
  244. }
  245. }
  246. return
  247. }