From a428f469ac78ee227d74d7f8ee86ca9ac5a19501 Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 21 Sep 2023 13:34:59 +0300 Subject: [PATCH 1/2] Gateway: improving client script --- core/scripts/gateway/client/send_request.go | 64 +++++++++++++++------ 1 file changed, 47 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() + } } From 59fedb94f3af56de5cb37624836b6665faf57ffb Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 21 Sep 2023 19:55:18 +0300 Subject: [PATCH 2/2] Fixed go mod tidy --- core/scripts/go.mod | 1 + core/scripts/go.sum | 1 + 2 files changed, 2 insertions(+) diff --git a/core/scripts/go.mod b/core/scripts/go.mod index 5b02b11fc47..59ec94c5bd5 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 diff --git a/core/scripts/go.sum b/core/scripts/go.sum index 2d511a938b2..177d3214ba7 100644 --- a/core/scripts/go.sum +++ b/core/scripts/go.sum @@ -791,6 +791,7 @@ github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+ github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=