From cebb3f568c9188a87b2cd439e2a50583201f6b1d Mon Sep 17 00:00:00 2001 From: Xiaoyu Chen Date: Tue, 12 Jul 2022 21:27:05 -0700 Subject: [PATCH 1/3] Fix loadtest --- loadtest/main.go | 25 ++++++++++++++----------- loadtest/tx.go | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/loadtest/main.go b/loadtest/main.go index 0a1992e0f0..05583cec9f 100644 --- a/loadtest/main.go +++ b/loadtest/main.go @@ -39,6 +39,7 @@ var ( ) const BATCH_SIZE = 100 +const VORTEX_DATA = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}" var FROM_MILI = sdk.NewDec(1000000) @@ -105,30 +106,32 @@ func run( wgs = append(wgs, wg) for j, account := range activeAccounts { key := GetKey(uint64(account)) - orderPlacements := []*dextypes.OrderPlacement{} + orderPlacements := []*dextypes.Order{} longPrice := uint64(j)%(longPriceCeiling-longPriceFloor) + longPriceFloor longQuantity := uint64(rand.Intn(int(quantityCeiling)-int(quantityFloor))) + quantityFloor shortPrice := uint64(j)%(shortPriceCeiling-shortPriceFloor) + shortPriceFloor shortQuantity := uint64(rand.Intn(int(quantityCeiling)-int(quantityFloor))) + quantityFloor for j := 0; j < BATCH_SIZE; j++ { - orderPlacements = append(orderPlacements, &dextypes.OrderPlacement{ + orderPlacements = append(orderPlacements, &dextypes.Order{ + Account: sdk.AccAddress(key.PubKey().Address()).String(), + ContractAddr: contractAddress, PositionDirection: dextypes.PositionDirection_LONG, Price: sdk.NewDec(int64(longPrice)).Quo(FROM_MILI), Quantity: sdk.NewDec(int64(longQuantity)).Quo(FROM_MILI), - PriceDenom: "usdc", - AssetDenom: "sei", - PositionEffect: dextypes.PositionEffect_OPEN, + PriceDenom: "SEI", + AssetDenom: "ATOM", OrderType: dextypes.OrderType_LIMIT, - Leverage: sdk.NewDec(1), - }, &dextypes.OrderPlacement{ + Data: VORTEX_DATA, + }, &dextypes.Order{ + Account: sdk.AccAddress(key.PubKey().Address()).String(), + ContractAddr: contractAddress, PositionDirection: dextypes.PositionDirection_SHORT, Price: sdk.NewDec(int64(shortPrice)).Quo(FROM_MILI), Quantity: sdk.NewDec(int64(shortQuantity)).Quo(FROM_MILI), - PriceDenom: "usdc", - AssetDenom: "sei", - PositionEffect: dextypes.PositionEffect_OPEN, + PriceDenom: "SEI", + AssetDenom: "ATOM", OrderType: dextypes.OrderType_LIMIT, - Leverage: sdk.NewDec(1), + Data: VORTEX_DATA, }) } amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", longPrice*longQuantity+shortPrice*shortQuantity, "usei")) diff --git a/loadtest/tx.go b/loadtest/tx.go index 76ae95a53d..9f7c1663b9 100644 --- a/loadtest/tx.go +++ b/loadtest/tx.go @@ -63,7 +63,7 @@ func SendTx( } } if grpcRes.TxResponse.Code != 0 { - fmt.Printf("Error: %d\n", grpcRes.TxResponse.Code) + fmt.Printf("Error: %d, %s\n", grpcRes.TxResponse.Code, grpcRes.TxResponse.RawLog) } else { mu.Lock() defer mu.Unlock() From 47edd1f94565760c6191c4e70b116858b642ff85 Mon Sep 17 00:00:00 2001 From: Xiaoyu Chen Date: Tue, 12 Jul 2022 21:34:19 -0700 Subject: [PATCH 2/3] linter --- loadtest/main.go | 42 +++++++++--------- loadtest/sign.go | 20 +++++---- loadtest/tx.go | 109 +++-------------------------------------------- 3 files changed, 38 insertions(+), 133 deletions(-) diff --git a/loadtest/main.go b/loadtest/main.go index 05583cec9f..e9eb82fafa 100644 --- a/loadtest/main.go +++ b/loadtest/main.go @@ -32,14 +32,16 @@ type EncodingConfig struct { } var ( - TEST_CONFIG EncodingConfig - TX_CLIENT typestx.ServiceClient - TX_HASH_FILE *os.File - CHAIN_ID string + TestConfig EncodingConfig + TxClient typestx.ServiceClient + TxHashFile *os.File + ChainID string ) -const BATCH_SIZE = 100 -const VORTEX_DATA = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}" +const ( + BATCH_SIZE = 100 + VORTEX_DATA = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}" +) var FROM_MILI = sdk.NewDec(1000000) @@ -48,16 +50,16 @@ func init() { interfaceRegistry := types.NewInterfaceRegistry() marshaler := codec.NewProtoCodec(interfaceRegistry) - TEST_CONFIG = EncodingConfig{ + TestConfig = EncodingConfig{ InterfaceRegistry: interfaceRegistry, Marshaler: marshaler, TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes), Amino: cdc, } - std.RegisterLegacyAminoCodec(TEST_CONFIG.Amino) - std.RegisterInterfaces(TEST_CONFIG.InterfaceRegistry) - app.ModuleBasics.RegisterLegacyAminoCodec(TEST_CONFIG.Amino) - app.ModuleBasics.RegisterInterfaces(TEST_CONFIG.InterfaceRegistry) + std.RegisterLegacyAminoCodec(TestConfig.Amino) + std.RegisterInterfaces(TestConfig.InterfaceRegistry) + app.ModuleBasics.RegisterLegacyAminoCodec(TestConfig.Amino) + app.ModuleBasics.RegisterInterfaces(TestConfig.InterfaceRegistry) } func run( @@ -76,7 +78,7 @@ func run( grpc.WithInsecure(), ) defer grpcConn.Close() - TX_CLIENT = typestx.NewServiceClient(grpcConn) + TxClient = typestx.NewServiceClient(grpcConn) userHomeDir, _ := os.UserHomeDir() filename := filepath.Join(userHomeDir, "outputs", "test_tx_hash") _ = os.Remove(filename) @@ -85,7 +87,7 @@ func run( fmt.Printf("Error opening file %s", err) return } - TX_HASH_FILE = file + TxHashFile = file var mu sync.Mutex activeAccounts := []int{} @@ -100,7 +102,7 @@ func run( wgs := []*sync.WaitGroup{} sendersList := [][]func(){} for i := 0; i < int(numberOfBlocks); i++ { - fmt.Println(fmt.Sprintf("Preparing %d-th block", i)) + fmt.Printf("Preparing %d-th block\n", i) var wg *sync.WaitGroup = &sync.WaitGroup{} var senders []func() wgs = append(wgs, wg) @@ -144,7 +146,7 @@ func run( ContractAddr: contractAddress, Funds: amount, } - txBuilder := TEST_CONFIG.TxConfig.NewTxBuilder() + txBuilder := TestConfig.TxConfig.NewTxBuilder() _ = txBuilder.SetMsgs(&msg) seqDelta := uint64(i / 2) SignTx(&txBuilder, key, seqDelta) @@ -161,9 +163,7 @@ func run( } sendersList = append(sendersList, senders) - tmp := inactiveAccounts - inactiveAccounts = activeAccounts - activeAccounts = tmp + inactiveAccounts, activeAccounts = activeAccounts, inactiveAccounts } lastHeight := getLastHeight() @@ -173,7 +173,7 @@ func run( time.Sleep(50 * time.Millisecond) newHeight = getLastHeight() } - fmt.Println(fmt.Sprintf("Sending %d-th block", i)) + fmt.Printf("Sending %d-th block\n", i) senders := sendersList[i] wg := wgs[i] @@ -213,8 +213,8 @@ func main() { shortPriceCeiling, _ := strconv.ParseUint(args[6], 10, 64) quantityFloor, _ := strconv.ParseUint(args[7], 10, 64) quantityCeiling, _ := strconv.ParseUint(args[8], 10, 64) - chainId := args[9] - CHAIN_ID = chainId + chainID := args[9] + ChainID = chainID run( contractAddress, numberOfAccounts, diff --git a/loadtest/sign.go b/loadtest/sign.go index efa2fce19f..bc55f23bd5 100644 --- a/loadtest/sign.go +++ b/loadtest/sign.go @@ -19,7 +19,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) -const NODE_URI = "tcp://localhost:26657" +const NodeURI = "tcp://localhost:26657" type AccountInfo struct { Address string `json:"address"` @@ -39,7 +39,9 @@ func GetKey(accountIdx uint64) cryptotypes.PrivKey { panic(err) } jsonFile.Close() - json.Unmarshal(byteVal, &accountInfo) + if err := json.Unmarshal(byteVal, &accountInfo); err != nil { + panic(err) + } kr, _ := keyring.New(sdk.KeyringServiceName(), "os", filepath.Join(userHomeDir, ".sei-chain"), os.Stdin) keyringAlgos, _ := kr.SupportedAlgorithms() algoStr := string(hd.Secp256k1Type) @@ -56,7 +58,7 @@ func SignTx(txBuilder *client.TxBuilder, privKey cryptotypes.PrivKey, seqDelta u sigV2 := signing.SignatureV2{ PubKey: privKey.PubKey(), Data: &signing.SingleSignatureData{ - SignMode: TEST_CONFIG.TxConfig.SignModeHandler().DefaultMode(), + SignMode: TestConfig.TxConfig.SignModeHandler().DefaultMode(), Signature: nil, }, Sequence: seqNum, @@ -65,16 +67,16 @@ func SignTx(txBuilder *client.TxBuilder, privKey cryptotypes.PrivKey, seqDelta u _ = (*txBuilder).SetSignatures(sigsV2...) sigsV2 = []signing.SignatureV2{} signerData := xauthsigning.SignerData{ - ChainID: CHAIN_ID, + ChainID: ChainID, AccountNumber: accountNum, Sequence: seqNum, } sigV2, _ = clienttx.SignWithPrivKey( - TEST_CONFIG.TxConfig.SignModeHandler().DefaultMode(), + TestConfig.TxConfig.SignModeHandler().DefaultMode(), signerData, *txBuilder, privKey, - TEST_CONFIG.TxConfig, + TestConfig.TxConfig, seqNum, ) sigsV2 = append(sigsV2, sigV2) @@ -88,14 +90,14 @@ func GetAccountNumberSequenceNumber(privKey cryptotypes.PrivKey) (uint64, uint64 panic(err) } accountRetriever := authtypes.AccountRetriever{} - cl, err := client.NewClientFromNode(NODE_URI) + cl, err := client.NewClientFromNode(NodeURI) if err != nil { panic(err) } context := client.Context{} - context = context.WithNodeURI(NODE_URI) + context = context.WithNodeURI(NodeURI) context = context.WithClient(cl) - context = context.WithInterfaceRegistry(TEST_CONFIG.InterfaceRegistry) + context = context.WithInterfaceRegistry(TestConfig.InterfaceRegistry) account, seq, err := accountRetriever.GetAccountNumberSequence(context, address) if err != nil { time.Sleep(5 * time.Second) diff --git a/loadtest/tx.go b/loadtest/tx.go index 9f7c1663b9..37c63b2ceb 100644 --- a/loadtest/tx.go +++ b/loadtest/tx.go @@ -2,13 +2,10 @@ package main import ( "context" - "encoding/json" "fmt" - "strconv" "sync" "time" - wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" "github.com/cosmos/cosmos-sdk/client" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -35,9 +32,9 @@ func SendTx( sdk.NewCoin("usei", sdk.NewInt(100000)), }) SignTx(txBuilder, key, seqDelta) - txBytes, _ := TEST_CONFIG.TxConfig.TxEncoder()((*txBuilder).GetTx()) + txBytes, _ := TestConfig.TxConfig.TxEncoder()((*txBuilder).GetTx()) return func() { - grpcRes, err := TX_CLIENT.BroadcastTx( + grpcRes, err := TxClient.BroadcastTx( context.Background(), &typestx.BroadcastTxRequest{ Mode: mode, @@ -51,7 +48,7 @@ func SendTx( // retry after a second until either succeed or fail for some other reason fmt.Printf("Mempool full\n") time.Sleep(1 * time.Second) - grpcRes, err = TX_CLIENT.BroadcastTx( + grpcRes, err = TxClient.BroadcastTx( context.Background(), &typestx.BroadcastTxRequest{ Mode: mode, @@ -67,103 +64,9 @@ func SendTx( } else { mu.Lock() defer mu.Unlock() - TX_HASH_FILE.WriteString(fmt.Sprintf("%s\n", grpcRes.TxResponse.TxHash)) + if _, err := TxHashFile.WriteString(fmt.Sprintf("%s\n", grpcRes.TxResponse.TxHash)); err != nil { + panic(err) + } } } } - -func GetLimitOrderTxBuilder( - contractAddress string, - key cryptotypes.PrivKey, - price uint64, - quantity uint64, - long bool, - open bool, - nonce uint64, -) client.TxBuilder { - txBuilder := TEST_CONFIG.TxConfig.NewTxBuilder() - var direction string - if long { - direction = LONG - } else { - direction = SHORT - } - var effect string - if open { - effect = OPEN - } else { - effect = CLOSE - } - body := map[string]interface{}{ - "limit_order": map[string]interface{}{ - "price": strconv.FormatUint(price, 10), - "quantity": strconv.FormatUint(quantity, 10), - "position_direction": direction, - "position_effect": effect, - "price_denom": "ust", - "asset_denom": "luna", - "nonce": nonce, - }, - } - amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price*quantity, "ust")) - if err != nil { - panic(err) - } - serialized_body, _ := json.Marshal(body) - msg := wasmtypes.MsgExecuteContract{ - Sender: sdk.AccAddress(key.PubKey().Address()).String(), - Contract: contractAddress, - Msg: serialized_body, - Funds: amount, - } - _ = txBuilder.SetMsgs(&msg) - return txBuilder -} - -func GetMarketOrderTxBuilder( - contractAddress string, - key cryptotypes.PrivKey, - price uint64, - quantity uint64, - long bool, - open bool, - nonce uint64, -) client.TxBuilder { - txBuilder := TEST_CONFIG.TxConfig.NewTxBuilder() - var direction string - if long { - direction = LONG - } else { - direction = SHORT - } - var effect string - if open { - effect = OPEN - } else { - effect = CLOSE - } - body := map[string]interface{}{ - "market_order": map[string]interface{}{ - "worst_price": strconv.FormatUint(price, 10), - "quantity": strconv.FormatUint(quantity, 10), - "position_direction": direction, - "position_effect": effect, - "price_denom": "ust", - "asset_denom": "luna", - "nonce": nonce, - }, - } - amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price*quantity, "ust")) - if err != nil { - panic(err) - } - serialized_body, _ := json.Marshal(body) - msg := wasmtypes.MsgExecuteContract{ - Sender: sdk.AccAddress(key.PubKey().Address()).String(), - Contract: contractAddress, - Msg: serialized_body, - Funds: amount, - } - _ = txBuilder.SetMsgs(&msg) - return txBuilder -} From 91b75e307225b9edf0ab3eb63688f5fa3a707681 Mon Sep 17 00:00:00 2001 From: Xiaoyu Chen Date: Tue, 12 Jul 2022 22:26:54 -0700 Subject: [PATCH 3/3] more linter --- loadtest/main.go | 22 +++++++++++----------- loadtest/tx.go | 7 ------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/loadtest/main.go b/loadtest/main.go index e9eb82fafa..32d4de902f 100644 --- a/loadtest/main.go +++ b/loadtest/main.go @@ -39,11 +39,11 @@ var ( ) const ( - BATCH_SIZE = 100 - VORTEX_DATA = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}" + BatchSize = 100 + VortexData = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}" ) -var FROM_MILI = sdk.NewDec(1000000) +var FromMili = sdk.NewDec(1000000) func init() { cdc := codec.NewLegacyAmino() @@ -103,7 +103,7 @@ func run( sendersList := [][]func(){} for i := 0; i < int(numberOfBlocks); i++ { fmt.Printf("Preparing %d-th block\n", i) - var wg *sync.WaitGroup = &sync.WaitGroup{} + wg := &sync.WaitGroup{} var senders []func() wgs = append(wgs, wg) for j, account := range activeAccounts { @@ -113,27 +113,27 @@ func run( longQuantity := uint64(rand.Intn(int(quantityCeiling)-int(quantityFloor))) + quantityFloor shortPrice := uint64(j)%(shortPriceCeiling-shortPriceFloor) + shortPriceFloor shortQuantity := uint64(rand.Intn(int(quantityCeiling)-int(quantityFloor))) + quantityFloor - for j := 0; j < BATCH_SIZE; j++ { + for j := 0; j < BatchSize; j++ { orderPlacements = append(orderPlacements, &dextypes.Order{ Account: sdk.AccAddress(key.PubKey().Address()).String(), ContractAddr: contractAddress, PositionDirection: dextypes.PositionDirection_LONG, - Price: sdk.NewDec(int64(longPrice)).Quo(FROM_MILI), - Quantity: sdk.NewDec(int64(longQuantity)).Quo(FROM_MILI), + Price: sdk.NewDec(int64(longPrice)).Quo(FromMili), + Quantity: sdk.NewDec(int64(longQuantity)).Quo(FromMili), PriceDenom: "SEI", AssetDenom: "ATOM", OrderType: dextypes.OrderType_LIMIT, - Data: VORTEX_DATA, + Data: VortexData, }, &dextypes.Order{ Account: sdk.AccAddress(key.PubKey().Address()).String(), ContractAddr: contractAddress, PositionDirection: dextypes.PositionDirection_SHORT, - Price: sdk.NewDec(int64(shortPrice)).Quo(FROM_MILI), - Quantity: sdk.NewDec(int64(shortQuantity)).Quo(FROM_MILI), + Price: sdk.NewDec(int64(shortPrice)).Quo(FromMili), + Quantity: sdk.NewDec(int64(shortQuantity)).Quo(FromMili), PriceDenom: "SEI", AssetDenom: "ATOM", OrderType: dextypes.OrderType_LIMIT, - Data: VORTEX_DATA, + Data: VortexData, }) } amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", longPrice*longQuantity+shortPrice*shortQuantity, "usei")) diff --git a/loadtest/tx.go b/loadtest/tx.go index 37c63b2ceb..f889c5c65c 100644 --- a/loadtest/tx.go +++ b/loadtest/tx.go @@ -13,13 +13,6 @@ import ( typestx "github.com/cosmos/cosmos-sdk/types/tx" ) -const ( - LONG = "Long" - SHORT = "Short" - OPEN = "Open" - CLOSE = "Close" -) - func SendTx( key cryptotypes.PrivKey, txBuilder *client.TxBuilder,