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 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/modules/repository"
  4. "encoding/json"
  5. "strconv"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/blockchain"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. type BlockChainInitNotify struct {
  12. RepoId int64 `json:"repoId"`
  13. ContractAddress string `json:"contractAddress"`
  14. }
  15. type BlockChainCommitNotify struct {
  16. CommitID string `json:"commitId"`
  17. TransactionHash string `json:"txHash"`
  18. }
  19. func HandleBlockChainInitNotify(ctx *context.Context) {
  20. var req BlockChainInitNotify
  21. data, _ := ctx.Req.Body().Bytes()
  22. json.Unmarshal(data, &req)
  23. repo, err := models.GetRepositoryByID(req.RepoId)
  24. if err != nil {
  25. log.Error("GetRepositoryByID failed:", err.Error())
  26. ctx.JSON(200, map[string]string{
  27. "code": "-1",
  28. "message": "internal error",
  29. })
  30. return
  31. }
  32. if repo.BlockChainStatus == models.RepoBlockChainSuccess && len(repo.ContractAddress) != 0 {
  33. log.Error("the repo has been RepoBlockChainSuccess:", req.RepoId)
  34. ctx.JSON(200, map[string]string{
  35. "code": "-1",
  36. "message": "the repo has been RepoBlockChainSuccess",
  37. })
  38. return
  39. }
  40. repo.BlockChainStatus = models.RepoBlockChainSuccess
  41. repo.ContractAddress = req.ContractAddress
  42. if err = models.UpdateRepositoryCols(repo, "block_chain_status", "contract_address"); err != nil {
  43. log.Error("UpdateRepositoryCols failed:", err.Error())
  44. ctx.JSON(200, map[string]string{
  45. "code": "-1",
  46. "message": "internal error",
  47. })
  48. return
  49. }
  50. ctx.JSON(200, map[string]string{
  51. "code": "0",
  52. "message": "",
  53. })
  54. }
  55. func HandleBlockChainCommitNotify(ctx *context.Context) {
  56. var req BlockChainCommitNotify
  57. data, _ := ctx.Req.Body().Bytes()
  58. if err := json.Unmarshal(data, &req); err != nil {
  59. log.Error("json.Unmarshal failed:", err.Error())
  60. ctx.JSON(200, map[string]string{
  61. "code": "-1",
  62. "message": "response data error",
  63. })
  64. return
  65. }
  66. blockChain, err := models.GetBlockChainByCommitID(req.CommitID)
  67. if err != nil {
  68. log.Error("GetRepositoryByID failed:", err.Error())
  69. ctx.JSON(200, map[string]string{
  70. "code": "-1",
  71. "message": "internal error",
  72. })
  73. return
  74. }
  75. if blockChain.Status == models.BlockChainCommitSuccess {
  76. log.Error("the commit has been BlockChainCommitReady:", blockChain.RepoID)
  77. ctx.JSON(200, map[string]string{
  78. "code": "-1",
  79. "message": "the commit has been BlockChainCommitReady",
  80. })
  81. return
  82. }
  83. blockChain.Status = models.BlockChainCommitSuccess
  84. blockChain.TransactionHash = req.TransactionHash
  85. if err = models.UpdateBlockChainCols(blockChain, "status", "transaction_hash"); err != nil {
  86. log.Error("UpdateBlockChainCols failed:", err.Error())
  87. ctx.JSON(200, map[string]string{
  88. "code": "-1",
  89. "message": "internal error",
  90. })
  91. return
  92. }
  93. ctx.JSON(200, map[string]string{
  94. "code": "0",
  95. "message": "",
  96. })
  97. }
  98. func HandleBlockChainUnSuccessRepos() {
  99. repos, err := models.GetBlockChainUnSuccessRepos()
  100. if err != nil {
  101. log.Error("GetBlockChainUnSuccessRepos failed:", err.Error())
  102. return
  103. }
  104. for _, repo := range repos {
  105. err = repo.GetOwner()
  106. if err != nil {
  107. log.Error("GetOwner(%s) failed:%v", repo.Name, err)
  108. continue
  109. }
  110. if len(repo.Owner.PrivateKey) == 0 || len(repo.Owner.PublicKey) == 0 {
  111. log.Error("the user has not been init in block_chain:", repo.Owner.Name)
  112. continue
  113. }
  114. strRepoID := strconv.FormatInt(repo.ID, 10)
  115. log.Info(strRepoID)
  116. _, err = blockchain.NewRepo(strRepoID, repo.Owner.PublicKey, repo.Name)
  117. if err != nil {
  118. log.Error("blockchain.NewRepo(%s) failed:%v", strRepoID, err)
  119. }
  120. }
  121. return
  122. }
  123. func HandleBlockChainUnSuccessCommits() {
  124. blockChains, err := models.GetBlockChainUnSuccessCommits()
  125. if err != nil {
  126. log.Error("GetBlockChainUnSuccessCommits failed:", err.Error())
  127. return
  128. }
  129. for _, block_chain := range blockChains {
  130. _, err = blockchain.Contribute(block_chain.ContractAddress, block_chain.Contributor, blockchain.ActionCommit, block_chain.CommitID, int(block_chain.Amount))
  131. if err != nil {
  132. log.Error("blockchain.Contribute(%s) failed:%v", block_chain.CommitID, err)
  133. }
  134. }
  135. return
  136. }
  137. func HandleBlockChainUnSuccessUsers() {
  138. users, err := models.GetBlockChainUnSuccessUsers()
  139. if err != nil {
  140. log.Error("GetBlockChainUnSuccessUsers failed:", err.Error())
  141. return
  142. }
  143. for _, user := range users {
  144. result, err := blockchain.CreateBlockchainAccount()
  145. if err != nil {
  146. log.Error("blockchain.CreateBlockchainAccount(%s) failed:%v", user.Name, err)
  147. continue
  148. }
  149. user.PublicKey = result.Payload["publickey"].(string)
  150. user.PrivateKey = result.Payload["privatekey"].(string)
  151. models.UpdateUser(user)
  152. }
  153. return
  154. }
  155. func HandleUnTransformedActions() {
  156. actions, err := models.GetUnTransformedActions()
  157. if err != nil {
  158. log.Error("GetUnTransformedActions failed:", err.Error())
  159. return
  160. }
  161. isTransformed := true
  162. for _, action := range actions {
  163. var content repository.PushCommits
  164. err = json.Unmarshal([]byte(action.Content), &content)
  165. if err != nil {
  166. isTransformed = false
  167. log.Error("json.Unmarshal action.Content(%s) failed:%v", action.Content, err)
  168. break
  169. }
  170. repo, err := models.GetRepositoryByID(action.RepoID)
  171. if err != nil {
  172. isTransformed = false
  173. log.Error("GetRepositoryByID(%d) failed:%v", action.RepoID, err)
  174. break
  175. }
  176. if repo.ContractAddress == "" {
  177. isTransformed = false
  178. log.Error("the repo(%s) has not been initialized in block_chain", repo.Name)
  179. break
  180. }
  181. for _, commit := range content.Commits {
  182. _, err = models.GetBlockChainByCommitID(commit.Sha1)
  183. if err == nil {
  184. log.Info("the commit(%s) has been transformed", commit.Sha1)
  185. continue
  186. }
  187. user, err := models.GetUserByName(commit.CommitterName)
  188. if err != nil {
  189. isTransformed = false
  190. log.Error("GetUserByName(%s) failed:%v", commit.CommitterName, err)
  191. break
  192. }
  193. blockChain := models.BlockChain{
  194. CommitID: commit.Sha1,
  195. Contributor: user.PublicKey,
  196. ContractAddress: repo.ContractAddress,
  197. Status: models.BlockChainCommitInit,
  198. Amount: 1,
  199. UserID: action.UserID,
  200. RepoID: action.RepoID,
  201. }
  202. _, err = models.InsertBlockChain(&blockChain)
  203. if err != nil {
  204. isTransformed = false
  205. log.Error("InsertBlockChain(%s) failed:%v", commit.Sha1, err)
  206. break
  207. }
  208. }
  209. }
  210. log.Info("", isTransformed)
  211. return
  212. }