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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "fmt"
  5. "time"
  6. )
  7. type BlockChainCommitStatus int
  8. const (
  9. BlockChainCommitInit BlockChainCommitStatus = iota
  10. BlockChainCommitSuccess
  11. BlockChainCommitFailed
  12. )
  13. type BlockChain struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. CommitID string `xorm:"INDEX NOT NULL"`
  16. Contributor string `xorm:"INDEX NOT NULL"`
  17. ContractAddress string `xorm:"INDEX NOT NULL"`
  18. Status BlockChainCommitStatus `xorm:"INDEX NOT NULL DEFAULT 0"`
  19. Amount int64 `xorm:"INDEX"`
  20. UserID int64 `xorm:"INDEX"`
  21. RepoID int64 `xorm:"INDEX"`
  22. TransactionHash string `xorm:"INDEX"`
  23. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  24. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  25. DeletedAt time.Time `xorm:"deleted"`
  26. User *User `xorm:"-"`
  27. Repo *Repository `xorm:"-"`
  28. }
  29. func getBlockChainByID(e Engine, id int64) (*BlockChain, error) {
  30. blockChain := new(BlockChain)
  31. has, err := e.ID(id).Get(blockChain)
  32. if err != nil {
  33. return nil, err
  34. } else if !has {
  35. return nil, fmt.Errorf("get block_chain by id failed(%d)", id)
  36. }
  37. return blockChain, nil
  38. }
  39. func GetBlockChainByID(id int64) (*BlockChain, error) {
  40. return getBlockChainByID(x, id)
  41. }
  42. func getBlockChainByCommitID(e Engine, commitID string) (*BlockChain, error) {
  43. blockChain := new(BlockChain)
  44. has, err := e.Where("commit_id = ?", commitID).Get(blockChain)
  45. if err != nil {
  46. return nil, err
  47. } else if !has {
  48. return nil, fmt.Errorf("get block_chain by commitID failed(%s)", commitID)
  49. }
  50. return blockChain, nil
  51. }
  52. func GetBlockChainByCommitID(commitID string) (*BlockChain, error) {
  53. return getBlockChainByCommitID(x, commitID)
  54. }
  55. func updateBlockChainCols(e Engine, blockChain *BlockChain, cols ...string) error {
  56. _, err := e.ID(blockChain.ID).Cols(cols...).Update(blockChain)
  57. return err
  58. }
  59. func UpdateBlockChainCols(blockChain *BlockChain, cols ...string) error {
  60. return updateBlockChainCols(x, blockChain, cols...)
  61. }
  62. func GetBlockChainUnSuccessCommits() ([]*BlockChain, error) {
  63. blockChains := make([]*BlockChain, 0, 10)
  64. return blockChains, x.
  65. Where("status != ?", BlockChainCommitSuccess).
  66. Find(&blockChains)
  67. }
  68. func InsertBlockChain(blockChain *BlockChain) (_ *BlockChain, err error) {
  69. if _, err := x.Insert(blockChain); err != nil {
  70. return nil, err
  71. }
  72. return blockChain, nil
  73. }