package main import ( "bytes" "encoding/json" "flag" "fmt" "io" "log" "net/http" "os" "sync" "time" log2 "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" ) // 生成bid var apiUrl, dsn string var timeout, nunber, bType, gNum int func main() { flag.StringVar(&apiUrl, "url", "127.0.0.1:8888/bifApi/v1/createAddress", "离线API接口地址") flag.StringVar(&dsn, "dsn", "root:rootPassword@tcp(192.168.150.40:23306)/dna_test", "mysql连接地址") flag.IntVar(&nunber, "num", 10, "生成bid的数量") flag.IntVar(&timeout, "timeout", 3, "请求超时时间") flag.IntVar(&bType, "type", 2, "生成的bid的类型") flag.IntVar(&gNum, "g_num", 100, "协程个数") flag.Parse() if bType != 1 && bType != 2 { log2.Error("bid类型非法") return } if nunber < 1 { log2.Error("生成bid数量非法") return } if gNum < 1 { log2.Error("协程个数非法") return } utcZone := time.FixedZone("UTC", 0) time.Local = utcZone // 连接mysql newLogger := logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer logger.Config{ SlowThreshold: time.Second, // 慢 SQL 阈值 LogLevel: logger.Silent, // Log level Colorful: false, // 禁用彩色打印 }, ) dsn := fmt.Sprintf("%s?charset=utf8&parseTime=True", dsn) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger, NamingStrategy: schema.NamingStrategy{ TablePrefix: "t_", SingularTable: false, }, }) if err != nil { log2.WithError(err).Error("init db failed") return } client := &http.Client{Timeout: time.Duration(timeout) * time.Second} log2.Info("开始执行时间: ", time.Now()) var wg sync.WaitGroup nunber = nunber / gNum for i := 0; i < gNum; i++ { wg.Add(1) go task(client, db, &wg) } wg.Wait() log2.Info("结束时间: ", time.Now()) } func task(client *http.Client, db *gorm.DB, wg *sync.WaitGroup) { defer wg.Done() for i := 1; i <= nunber; i++ { objJson, err := json.Marshal(&Req{ KeyType: 1, }) if err != nil { log2.WithError(err).Error("marshal request body failed") return } payload := bytes.NewReader(objJson) req, err := http.NewRequest("POST", apiUrl, payload) if err != nil { log2.WithError(err).Error("build request failed") return } req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err != nil { log2.WithError(err).Error("request failed") return } body, err := io.ReadAll(res.Body) if err != nil { log2.WithError(err).Error("request failed") return } var resp Resp err = json.Unmarshal(body, &resp) if err != nil { log2.WithError(err).Error("unmarshal response failed") return } if resp.Code != http.StatusOK || resp.Message != "ok" { log2.Error(resp.Message) return } if resp.Data.EncAddress == "" || resp.Data.EncPrivateKey == "" || resp.Data.EncPublicKey == "" { log2.WithField("data", resp.Data).Error("response data missing") return } switch bType { case 1: // 链账户 accountBid := AccountBid{ Bid: resp.Data.EncAddress, PublicKey: resp.Data.EncPrivateKey, PrivateKey: resp.Data.EncPublicKey, } err = db.Model(&AccountBid{}).Create(&accountBid).Error if err != nil { log2.WithError(err).Error("insert bid-account failed") continue } case 2: // NFT nftBid := NftBid{ Bid: resp.Data.EncAddress, PublicKey: resp.Data.EncPrivateKey, PrivateKey: resp.Data.EncPublicKey, } err = db.Model(&NftBid{}).Create(&nftBid).Error if err != nil { log2.WithError(err).Error("insert bid-nft failed") continue } } res.Body.Close() } } type Req struct { KeyType int `json:"keyType"` } type Resp struct { Code int `json:"code"` Message string `json:"message"` Data Data `json:"data"` } type Data struct { EncAddress string `json:"encAddress"` EncPublicKey string `json:"encPublicKey"` EncPrivateKey string `json:"encPrivateKey"` } // 链账户关联表 type AccountBid struct { Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT;comment:主键id" json:"id"` Bid string `gorm:"column:bid;type:char(40);comment:星火bid;NOT NULL" json:"bid"` PublicKey string `gorm:"column:public_key;type:varchar(100);comment:公钥;NOT NULL" json:"public_key"` PrivateKey string `gorm:"column:private_key;type:varchar(100);comment:私钥;NOT NULL" json:"private_key"` Status int `gorm:"column:status;type:tinyint(4);default:2;comment:激活状态 1:已激活 2:未激活;NOT NULL" json:"status"` Account string `gorm:"column:account;type:char(42);comment:文昌链链账户地址;NOT NULL" json:"account"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"updated_at"` } // NFT关联表 type NftBid struct { Id int64 `gorm:"column:id;type:bigint(20);primary_key;comment:主键id" json:"id"` Bid string `gorm:"column:bid;type:char(40);comment:星火bid;NOT NULL" json:"bid"` NftId int64 `gorm:"column:nft_id;type:bigint(20);default:0;comment:关联文昌链极速网的[ t_dna_nfts ]表主键id关联文昌链dna_nfts表id;NOT NULL" json:"nft_id"` PublicKey string `gorm:"column:public_key;type:varchar(100);comment:公钥;NOT NULL" json:"public_key"` PrivateKey string `gorm:"column:private_key;type:varchar(100);comment:私钥;NOT NULL" json:"private_key"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"updated_at"` }