From 9d2dcea6e429eea077a42cd5026e8dbeb551ebcb Mon Sep 17 00:00:00 2001 From: Jay Jie Date: Tue, 12 Dec 2023 22:27:57 -0800 Subject: [PATCH] refactor: chain.go and query.go in e2e/ --- e2e/chain.go | 54 ++++++++++++++++++++++------- e2e/query.go | 95 ++++++++++++++++------------------------------------ 2 files changed, 69 insertions(+), 80 deletions(-) diff --git a/e2e/chain.go b/e2e/chain.go index 7491b4d7..1b6efed3 100644 --- a/e2e/chain.go +++ b/e2e/chain.go @@ -3,6 +3,7 @@ package e2e import ( "fmt" "os" + "sync" tmrand "github.com/cometbft/cometbft/libs/rand" @@ -53,28 +54,55 @@ func (c *chain) configDir() string { } func (c *chain) createAndInitValidators(count int) error { + // Preallocate with a capacity to avoid rellocation + c.validators = make([]*validator, 0, count) + + var wg sync.WaitGroup + errChan := make(chan error, count) + for i := 0; i < count; i++ { - node := c.createValidator(i) + wg.Add(1) + go func(index int) { + defer wg.Done() + + node := c.createValidator(index) + + // generate genesis files + if err := node.init(); err != nil { + errChan <- err + return + } + + // create keys + if err := c.createKeys(node); err != nil { + errChan <- err + return + } + + c.validators = append(c.validators, node) + }(i) + } - // generate genesis files - if err := node.init(); err != nil { + wg.Wait() + close(errChan) + + // Return the first error that occurred, if any + for err := range errChan { + if err != nil { return err } + } - c.validators = append(c.validators, node) + return nil +} - // create keys - if err := node.createKey("val"); err != nil { - return err - } - if err := node.createNodeKey(); err != nil { - return err - } - if err := node.createConsensusKey(); err != nil { +func (c *chain) createKeys(node *validator) error { + keys := []string{"val", "node", "consensus"} + for _, key := range keys { + if err := node.createKey(key); err != nil { return err } } - return nil } diff --git a/e2e/query.go b/e2e/query.go index 3a731cd0..e6b7e9b3 100644 --- a/e2e/query.go +++ b/e2e/query.go @@ -3,6 +3,7 @@ package e2e import ( "encoding/json" "fmt" + "io" "net/http" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1" @@ -10,23 +11,32 @@ import ( wasmstoragetypes "github.com/sedaprotocol/seda-chain/x/wasm-storage/types" ) -func queryTx(endpoint, txHash string) error { - resp, err := http.Get(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs/%s", endpoint, txHash)) +func query(endpoint, path string, v interface{}) error { + resp, err := http.Get(fmt.Sprintf("%s/%s", endpoint, path)) if err != nil { return fmt.Errorf("failed to execute HTTP request: %w", err) } - defer resp.Body.Close() if resp.StatusCode != 200 { - return fmt.Errorf("tx query returned non-200 status: %d", resp.StatusCode) + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("query returned non-200 status: %d, body: %s", resp.StatusCode, body) } - var result map[string]interface{} - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + if err := json.NewDecoder(resp.Body).Decode(v); err != nil { return fmt.Errorf("failed to read response body: %w", err) } + return nil +} + +func queryTx(endpoint, txHash string) error { + var result map[string]interface{} + err := query(endpoint, fmt.Sprintf("cosmos/tx/v1beta1/txs/%s", txHash), &result) + if err != nil { + return err + } + txResp := result["tx_response"].(map[string]interface{}) if v := txResp["code"]; v.(float64) != 0 { return fmt.Errorf("tx %s failed with status code %v", txHash, v) @@ -37,85 +47,36 @@ func queryTx(endpoint, txHash string) error { func queryGovProposal(endpoint string, proposalID int) (govtypes.QueryProposalResponse, error) { var govProposalResp govtypes.QueryProposalResponse - - path := fmt.Sprintf("%s/cosmos/gov/v1/proposals/%d", endpoint, proposalID) - - body, err := httpGet(path) - if err != nil { - return govProposalResp, fmt.Errorf("failed to execute HTTP request: %w", err) - } - if err := cdc.UnmarshalJSON(body, &govProposalResp); err != nil { - return govProposalResp, err - } - return govProposalResp, nil + err := query(endpoint, fmt.Sprintf("cosmos/gov/v1/proposals/%d", proposalID), &govProposalResp) + return govProposalResp, err } func queryDataRequestWasm(endpoint string, drHash string) (wasmstoragetypes.QueryDataRequestWasmResponse, error) { var res wasmstoragetypes.QueryDataRequestWasmResponse - - body, err := httpGet(fmt.Sprintf("%s/seda-chain/wasm-storage/data_request_wasm/%s", endpoint, drHash)) - if err != nil { - return res, err - } - - if err = cdc.UnmarshalJSON(body, &res); err != nil { - return res, err - } - return res, nil + err := query(endpoint, fmt.Sprintf("seda-chain/wasm-storage/data_request_wasm/%s", drHash), &res) + return res, err } func queryOverlayWasm(endpoint string, hash string) (wasmstoragetypes.QueryOverlayWasmResponse, error) { var res wasmstoragetypes.QueryOverlayWasmResponse - - body, err := httpGet(fmt.Sprintf("%s/seda-chain/wasm-storage/overlay_wasm/%s", endpoint, hash)) - if err != nil { - return res, err - } - - if err = cdc.UnmarshalJSON(body, &res); err != nil { - return res, err - } - return res, nil + err := query(endpoint, fmt.Sprintf("seda-chain/wasm-storage/overlay_wasm/%s", hash), &res) + return res, err } func queryDataRequestWasms(endpoint string) (wasmstoragetypes.QueryDataRequestWasmsResponse, error) { var res wasmstoragetypes.QueryDataRequestWasmsResponse - - body, err := httpGet(fmt.Sprintf("%s/seda-chain/wasm-storage/data_request_wasms", endpoint)) - if err != nil { - return res, err - } - - if err = cdc.UnmarshalJSON(body, &res); err != nil { - return res, err - } - return res, nil + err := query(endpoint, "seda-chain/wasm-storage/data_request_wasms", &res) + return res, err } func queryOverlayWasms(endpoint string) (wasmstoragetypes.QueryOverlayWasmsResponse, error) { var res wasmstoragetypes.QueryOverlayWasmsResponse - - body, err := httpGet(fmt.Sprintf("%s/seda-chain/wasm-storage/overlay_wasms", endpoint)) - if err != nil { - return res, err - } - - if err = cdc.UnmarshalJSON(body, &res); err != nil { - return res, err - } - return res, nil + err := query(endpoint, "seda-chain/wasm-storage/overlay_wasms", &res) + return res, err } func queryProxyContractRegistry(endpoint string) (wasmstoragetypes.QueryProxyContractRegistryResponse, error) { var res wasmstoragetypes.QueryProxyContractRegistryResponse - - body, err := httpGet(fmt.Sprintf("%s/seda-chain/wasm-storage/proxy_contract_registry", endpoint)) - if err != nil { - return res, err - } - - if err = cdc.UnmarshalJSON(body, &res); err != nil { - return res, err - } - return res, nil + err := query(endpoint, "seda-chain/wasm-storage/proxy_contract_registry", &res) + return res, err }