From 90a0a374176400b4e38529f99b9172c4dfede68c Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Fri, 22 Sep 2023 04:49:05 +0300 Subject: [PATCH] Gateway: improving client script (#10744) * Gateway: improving client script * Fixed go mod tidy --------- Co-authored-by: Bolek <1416262+bolekk@users.noreply.github.com> --- core/scripts/gateway/client/send_request.go | 64 +++++++++++++++------ core/scripts/go.mod | 1 + 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/core/scripts/gateway/client/send_request.go b/core/scripts/gateway/client/send_request.go index ad3ea1cc27c..94ff6fb17da 100644 --- a/core/scripts/gateway/client/send_request.go +++ b/core/scripts/gateway/client/send_request.go @@ -8,9 +8,11 @@ import ( "fmt" "io" "net/http" + "os" "time" "github.com/ethereum/go-ethereum/crypto" + "github.com/joho/godotenv" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/api" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/functions" @@ -27,8 +29,19 @@ func main() { s4SetVersion := flag.Uint64("s4_set_version", 0, "S4 set version") s4SetExpirationPeriod := flag.Int64("s4_set_expiration_period", 60*60*1000, "S4 how long until the entry expires from now (in milliseconds)") s4SetPayload := flag.String("s4_set_payload", "", "S4 set payload") + repeat := flag.Bool("repeat", false, "Repeat sending the request every 10 seconds") flag.Parse() + if privateKey == nil || *privateKey == "" { + if err := godotenv.Load(); err != nil { + panic(err) + } + + privateKeyEnvVar := os.Getenv("PRIVATE_KEY") + privateKey = &privateKeyEnvVar + fmt.Println("Loaded private key from .env") + } + // validate key and extract address key, err := crypto.HexToECDSA(*privateKey) if err != nil { @@ -77,8 +90,7 @@ func main() { }, } - err = msg.Sign(key) - if err != nil { + if err = msg.Sign(key); err != nil { fmt.Println("error signing message", err) return } @@ -88,26 +100,44 @@ func main() { fmt.Println("error JSON-RPC encoding", err) return } - req, err := http.NewRequestWithContext(context.Background(), "POST", *gatewayURL, bytes.NewBuffer(rawMsg)) - if err != nil { - fmt.Println("error creating an HTTP request", err) + + createRequest := func() (req *http.Request, err error) { + req, err = http.NewRequestWithContext(context.Background(), "POST", *gatewayURL, bytes.NewBuffer(rawMsg)) + if err == nil { + req.Header.Set("Content-Type", "application/json") + } return } - req.Header.Set("Content-Type", "application/json") client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - fmt.Println("error sending a request", err) - return - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Println("error sending a request", err) - return + sendRequest := func() { + req, err := createRequest() + if err != nil { + fmt.Println("error creating a request", err) + return + } + + resp, err := client.Do(req) + if err != nil { + fmt.Println("error sending a request", err) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("error sending a request", err) + return + } + + fmt.Println(string(body)) } - fmt.Println(string(body)) + sendRequest() + + for *repeat { + time.Sleep(10 * time.Second) + sendRequest() + } } diff --git a/core/scripts/go.mod b/core/scripts/go.mod index e5e9036016a..f4da1f46f42 100644 --- a/core/scripts/go.mod +++ b/core/scripts/go.mod @@ -12,6 +12,7 @@ require ( github.com/ethereum/go-ethereum v1.12.0 github.com/google/go-cmp v0.5.9 github.com/google/uuid v1.3.1 + github.com/joho/godotenv v1.4.0 github.com/manyminds/api2go v0.0.0-20171030193247-e7b693844a6f github.com/montanaflynn/stats v0.7.1 github.com/olekukonko/tablewriter v0.0.5