prepare.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net/http"
  10. "os"
  11. "sync"
  12. "time"
  13. log2 "github.com/sirupsen/logrus"
  14. "gorm.io/driver/mysql"
  15. "gorm.io/gorm"
  16. "gorm.io/gorm/logger"
  17. "gorm.io/gorm/schema"
  18. )
  19. // 生成bid
  20. var apiUrl, dsn string
  21. var timeout, nunber, bType, gNum int
  22. func main() {
  23. flag.StringVar(&apiUrl, "url", "127.0.0.1:8888/bifApi/v1/createAddress", "离线API接口地址")
  24. flag.StringVar(&dsn, "dsn", "root:rootPassword@tcp(192.168.150.40:23306)/dna_test", "mysql连接地址")
  25. flag.IntVar(&nunber, "num", 10, "生成bid的数量")
  26. flag.IntVar(&timeout, "timeout", 3, "请求超时时间")
  27. flag.IntVar(&bType, "type", 2, "生成的bid的类型")
  28. flag.IntVar(&gNum, "g_num", 100, "协程个数")
  29. flag.Parse()
  30. if bType != 1 && bType != 2 {
  31. log2.Error("bid类型非法")
  32. return
  33. }
  34. if nunber < 1 {
  35. log2.Error("生成bid数量非法")
  36. return
  37. }
  38. if gNum < 1 {
  39. log2.Error("协程个数非法")
  40. return
  41. }
  42. utcZone := time.FixedZone("UTC", 0)
  43. time.Local = utcZone
  44. // 连接mysql
  45. newLogger := logger.New(
  46. log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
  47. logger.Config{
  48. SlowThreshold: time.Second, // 慢 SQL 阈值
  49. LogLevel: logger.Silent, // Log level
  50. Colorful: false, // 禁用彩色打印
  51. },
  52. )
  53. dsn := fmt.Sprintf("%s?charset=utf8&parseTime=True", dsn)
  54. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger,
  55. NamingStrategy: schema.NamingStrategy{
  56. TablePrefix: "t_",
  57. SingularTable: false,
  58. },
  59. })
  60. if err != nil {
  61. log2.WithError(err).Error("init db failed")
  62. return
  63. }
  64. client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
  65. log2.Info("开始执行时间: ", time.Now())
  66. var wg sync.WaitGroup
  67. nunber = nunber / gNum
  68. for i := 0; i < gNum; i++ {
  69. wg.Add(1)
  70. go task(client, db, &wg)
  71. }
  72. wg.Wait()
  73. log2.Info("结束时间: ", time.Now())
  74. }
  75. func task(client *http.Client, db *gorm.DB, wg *sync.WaitGroup) {
  76. defer wg.Done()
  77. for i := 1; i <= nunber; i++ {
  78. objJson, err := json.Marshal(&Req{
  79. KeyType: 1,
  80. })
  81. if err != nil {
  82. log2.WithError(err).Error("marshal request body failed")
  83. return
  84. }
  85. payload := bytes.NewReader(objJson)
  86. req, err := http.NewRequest("POST", apiUrl, payload)
  87. if err != nil {
  88. log2.WithError(err).Error("build request failed")
  89. return
  90. }
  91. req.Header.Add("Content-Type", "application/json")
  92. res, err := client.Do(req)
  93. if err != nil {
  94. log2.WithError(err).Error("request failed")
  95. return
  96. }
  97. body, err := io.ReadAll(res.Body)
  98. if err != nil {
  99. log2.WithError(err).Error("request failed")
  100. return
  101. }
  102. var resp Resp
  103. err = json.Unmarshal(body, &resp)
  104. if err != nil {
  105. log2.WithError(err).Error("unmarshal response failed")
  106. return
  107. }
  108. if resp.Code != http.StatusOK || resp.Message != "ok" {
  109. log2.Error(resp.Message)
  110. return
  111. }
  112. if resp.Data.EncAddress == "" || resp.Data.EncPrivateKey == "" || resp.Data.EncPublicKey == "" {
  113. log2.WithField("data", resp.Data).Error("response data missing")
  114. return
  115. }
  116. switch bType {
  117. case 1: // 链账户
  118. accountBid := AccountBid{
  119. Bid: resp.Data.EncAddress,
  120. PublicKey: resp.Data.EncPrivateKey,
  121. PrivateKey: resp.Data.EncPublicKey,
  122. }
  123. err = db.Model(&AccountBid{}).Create(&accountBid).Error
  124. if err != nil {
  125. log2.WithError(err).Error("insert bid-account failed")
  126. continue
  127. }
  128. case 2: // NFT
  129. nftBid := NftBid{
  130. Bid: resp.Data.EncAddress,
  131. PublicKey: resp.Data.EncPrivateKey,
  132. PrivateKey: resp.Data.EncPublicKey,
  133. }
  134. err = db.Model(&NftBid{}).Create(&nftBid).Error
  135. if err != nil {
  136. log2.WithError(err).Error("insert bid-nft failed")
  137. continue
  138. }
  139. }
  140. res.Body.Close()
  141. }
  142. }
  143. type Req struct {
  144. KeyType int `json:"keyType"`
  145. }
  146. type Resp struct {
  147. Code int `json:"code"`
  148. Message string `json:"message"`
  149. Data Data `json:"data"`
  150. }
  151. type Data struct {
  152. EncAddress string `json:"encAddress"`
  153. EncPublicKey string `json:"encPublicKey"`
  154. EncPrivateKey string `json:"encPrivateKey"`
  155. }
  156. // 链账户关联表
  157. type AccountBid struct {
  158. Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT;comment:主键id" json:"id"`
  159. Bid string `gorm:"column:bid;type:char(40);comment:星火bid;NOT NULL" json:"bid"`
  160. PublicKey string `gorm:"column:public_key;type:varchar(100);comment:公钥;NOT NULL" json:"public_key"`
  161. PrivateKey string `gorm:"column:private_key;type:varchar(100);comment:私钥;NOT NULL" json:"private_key"`
  162. Status int `gorm:"column:status;type:tinyint(4);default:2;comment:激活状态 1:已激活 2:未激活;NOT NULL" json:"status"`
  163. Account string `gorm:"column:account;type:char(42);comment:文昌链链账户地址;NOT NULL" json:"account"`
  164. CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"created_at"`
  165. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"updated_at"`
  166. }
  167. // NFT关联表
  168. type NftBid struct {
  169. Id int64 `gorm:"column:id;type:bigint(20);primary_key;comment:主键id" json:"id"`
  170. Bid string `gorm:"column:bid;type:char(40);comment:星火bid;NOT NULL" json:"bid"`
  171. NftId int64 `gorm:"column:nft_id;type:bigint(20);default:0;comment:关联文昌链极速网的[ t_dna_nfts ]表主键id关联文昌链dna_nfts表id;NOT NULL" json:"nft_id"`
  172. PublicKey string `gorm:"column:public_key;type:varchar(100);comment:公钥;NOT NULL" json:"public_key"`
  173. PrivateKey string `gorm:"column:private_key;type:varchar(100);comment:私钥;NOT NULL" json:"private_key"`
  174. CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"created_at"`
  175. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"updated_at"`
  176. }