From dcaea486fe6c85133b26f7c7d1a6db9f95f8c81b Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 25 Mar 2025 13:49:26 +0100 Subject: [PATCH 01/11] add decryption monitor --- go.mod | 6 + go.sum | 10 ++ main.go | 6 + template.env | 4 + tests/decryptionmonitor.go | 259 +++++++++++++++++++++++++++++++++++++ 5 files changed, 285 insertions(+) create mode 100644 tests/decryptionmonitor.go diff --git a/go.mod b/go.mod index 90cf5f8..f990aa5 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,11 @@ require ( gotest.tools v2.2.0+incompatible ) +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect +) + require ( github.com/Microsoft/go-winio v0.6.2 // indirect github.com/bits-and-blooms/bitset v1.10.0 // indirect @@ -39,6 +44,7 @@ require ( github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/rs/zerolog v1.34.0 github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/supranational/blst v0.3.12 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect diff --git a/go.sum b/go.sum index 0ccd908..d823e87 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,7 @@ github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJ github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I= @@ -74,6 +75,7 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= @@ -189,6 +191,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -225,8 +229,11 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99 github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE= github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= +github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= @@ -319,9 +326,12 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= diff --git a/main.go b/main.go index e52d0dd..4e92983 100644 --- a/main.go +++ b/main.go @@ -65,6 +65,12 @@ func main() { runCollector() wg.Done() }() + case "decryption-monitor": + wg.Add(1) + go func() { + tests.RunDecryptionMonitor() + wg.Done() + }() default: log.Printf("Unknown mode: %s", m) } diff --git a/template.env b/template.env index 9aeec46..b505638 100644 --- a/template.env +++ b/template.env @@ -15,3 +15,7 @@ NODE_URL="https://erpc.chiado.staging.shutter.network" WAIT_TX_TIMEOUT=10 TEST_DURATION=1 +#DECRYPTION MONITOR +SHUTTER_API="http://shutter-api.shutter.network/api" +API_REQUEST_INTERVAL="60" +SHUTTER_REGISTRY_CALLER_ADDRESS="0x228DefCF37Da29475F0EE2B9E4dfAeDc3b0746bc" diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go new file mode 100644 index 0000000..448f38c --- /dev/null +++ b/tests/decryptionmonitor.go @@ -0,0 +1,259 @@ +package tests + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "os" + "os/signal" + "strconv" + "sync" + "sync/atomic" + "syscall" + "time" + + "github.com/rs/zerolog/log" +) + +type RegisterIdentityRequest struct { + DecryptionTimestamp int64 `json:"decryptionTimestamp"` + IdentityPrefix string `json:"identityPrefix"` +} + +type DecryptionStats struct { + totalDecryptions atomic.Int64 + validDecryptions atomic.Int64 + invalidDecryptions atomic.Int64 +} + +var ( + stats DecryptionStats + stopChan = make(chan os.Signal, 1) +) + +type RegisterIdentityResponse struct { + Eon uint64 `json:"eon"` + Identity string `json:"identity"` + IdentityPrefix string `json:"identity_prefix"` + EonKey string `json:"eon_key"` + TxHash string `json:"tx_hash"` +} + +type GetDecryptionKeyResponse struct { + DecryptionKey string `json:"decryption_key"` + Identity string `json:"identity"` + DecryptionTimestamp uint64 `json:"decryption_timestamp"` +} + +type GetDataForEncryptionResponse struct { + Eon uint64 `json:"eon"` + Identity string `json:"identity"` + IdentityPrefix string `json:"identity_prefix"` + EonKey string `json:"eon_key"` + EpochID string `json:"epoch_id"` +} + +type ErrorResponse struct { + Description string `json:"description,omitempty"` + Metadata string `json:"metadata,omitempty"` + StatusCode int `json:"statusCode"` +} + +func RunDecryptionMonitor() { + baseURL := os.Getenv("SHUTTER_API") + seconds, err := strconv.Atoi(os.Getenv("API_REQUEST_INTERVAL")) + if err != nil { + log.Err(err).Msg("incorrect api request interval") + return + } + interval := time.Duration(seconds) * time.Second + address := os.Getenv("SHUTTER_REGISTRY_CALLER_ADDRESS") + + fmt.Printf("Starting performance monitoring\n") + fmt.Printf("Base URL: %s\n", baseURL) + fmt.Printf("Interval: %v\n\n", interval) + + // Setup graceful shutdown + signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM) + + var wg sync.WaitGroup + + // Start monitoring in separate goroutine + go func() { + runFlow(baseURL, address, &wg) + + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + runFlow(baseURL, address, &wg) + case <-stopChan: + return + } + } + }() + + // Wait for interrupt signal + <-stopChan + fmt.Printf("\nShutting down...\n") + + // Wait for all decryption requests to complete + wg.Wait() + + // Print final statistics + printStatistics() +} + +func printStatistics() { + fmt.Printf("\n=== Final Decryption Statistics ===\n") + fmt.Printf("Total decryption attempts: %d\n", stats.totalDecryptions.Load()) + fmt.Printf("Successful decryptions: %d\n", stats.validDecryptions.Load()) + fmt.Printf("Failed decryptions: %d\n", stats.invalidDecryptions.Load()) + + total := stats.totalDecryptions.Load() + if total > 0 { + successRate := float64(stats.validDecryptions.Load()) / float64(total) * 100 + fmt.Printf("Success rate: %.2f%%\n", successRate) + } +} + +func runFlow(baseURL, address string, wg *sync.WaitGroup) { + timestamp := time.Now().Format("2006-01-02 15:04:05") + fmt.Printf("\n=== Performance Check at %s ===\n", timestamp) + + encryptionData, err := getDataForEncryption(baseURL, address, "") + if err != nil { + log.Err(err).Msg("error in get data for encryption endpoint") + return + } + + decryptionTime := time.Now().Add(20 * time.Second) + registerReq := RegisterIdentityRequest{ + DecryptionTimestamp: decryptionTime.Unix(), + IdentityPrefix: encryptionData["message"].IdentityPrefix, + } + + err = registerIdentity(baseURL, registerReq) + if err != nil { + log.Err(err).Msg("error encountered while registering identity") + return + } + + // Launch decryption key request in separate goroutine + wg.Add(1) + go func(identity string, decryptTime time.Time) { + defer wg.Done() + waitDuration := time.Until(decryptTime.Add(2 * time.Second)) + + time.Sleep(waitDuration) + stats.totalDecryptions.Add(1) + err := getDecryptionKey(baseURL, identity) + if err != nil { + log.Err(err).Msg("error encountered while get decryption key") + stats.invalidDecryptions.Add(1) + return + } + + stats.validDecryptions.Add(1) + }(encryptionData["message"].Identity, decryptionTime) +} + +func getDataForEncryption(baseURL, address, identityPrefix string) (map[string]GetDataForEncryptionResponse, error) { + params := url.Values{} + params.Add("address", address) + if identityPrefix != "" { + params.Add("identityPrefix", identityPrefix) + } + + url := fmt.Sprintf("%s/get_data_for_encryption?%s", baseURL, params.Encode()) + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + var errorResp ErrorResponse + if err := json.Unmarshal(body, &errorResp); err != nil { + return nil, fmt.Errorf("failed to parse error response: %v", err) + } + return nil, err + + } + + var response map[string]GetDataForEncryptionResponse + + if err := json.Unmarshal(body, &response); err != nil { + return nil, fmt.Errorf("failed to parse response: %v", err) + } + + return response, nil +} + +func registerIdentity(baseURL string, req RegisterIdentityRequest) error { + jsonData, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("failed to marshal request: %v", err) + } + + resp, err := http.Post( + baseURL+"/register_identity", + "application/json", + bytes.NewBuffer(jsonData), + ) + if err != nil { + return fmt.Errorf("request failed: %v", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read response: %v", err) + } + + fmt.Println("StatusCode registerIdentity", resp.StatusCode, req.DecryptionTimestamp) + + if resp.StatusCode != http.StatusOK { + var errorResp ErrorResponse + if err := json.Unmarshal(body, &errorResp); err != nil { + return fmt.Errorf("failed to parse error response: %v", err) + } + return fmt.Errorf("response error %v", errorResp.Description) + } + + return nil +} + +func getDecryptionKey(baseURL, identity string) error { + params := url.Values{} + params.Add("identity", identity) + + url := fmt.Sprintf("%s/get_decryption_key?%s", baseURL, params.Encode()) + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read response: %v", err) + } + fmt.Println("StatusCode getDecryptionKey", resp.StatusCode, identity) + if resp.StatusCode != http.StatusOK { + var errorResp ErrorResponse + if err := json.Unmarshal(body, &errorResp); err != nil { + return fmt.Errorf("failed to parse error response, %v", err) + } + return fmt.Errorf("failed to parse error response, %s", errorResp.Description) + } + return nil +} From 3f331cb3aa198b1f32c6115fe21562db0d872dbd Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 25 Mar 2025 13:50:31 +0100 Subject: [PATCH 02/11] clean deps --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f990aa5..f05298e 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( github.com/tklauser/numcpus v0.6.1 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect golang.org/x/crypto v0.23.0 // indirect - golang.org/x/sync v0.7.0 // indirect + golang.org/x/sync v0.7.0 golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect rsc.io/tmplfunc v0.0.3 // indirect From 24059b3eff5a91bb5494ecbd57794a778c03e302 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 25 Mar 2025 14:05:59 +0100 Subject: [PATCH 03/11] improve logging --- tests/decryptionmonitor.go | 39 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index 448f38c..d629568 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/url" "os" @@ -14,8 +15,6 @@ import ( "sync/atomic" "syscall" "time" - - "github.com/rs/zerolog/log" ) type RegisterIdentityRequest struct { @@ -66,15 +65,15 @@ func RunDecryptionMonitor() { baseURL := os.Getenv("SHUTTER_API") seconds, err := strconv.Atoi(os.Getenv("API_REQUEST_INTERVAL")) if err != nil { - log.Err(err).Msg("incorrect api request interval") + log.Fatalf("incorrect api request interval %s", err) return } interval := time.Duration(seconds) * time.Second address := os.Getenv("SHUTTER_REGISTRY_CALLER_ADDRESS") - fmt.Printf("Starting performance monitoring\n") - fmt.Printf("Base URL: %s\n", baseURL) - fmt.Printf("Interval: %v\n\n", interval) + log.Printf("Starting performance monitoring\n") + log.Printf("Base URL: %s\n", baseURL) + log.Printf("Interval: %v\n\n", interval) // Setup graceful shutdown signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM) @@ -100,7 +99,7 @@ func RunDecryptionMonitor() { // Wait for interrupt signal <-stopChan - fmt.Printf("\nShutting down...\n") + log.Printf("Shutting down...\n") // Wait for all decryption requests to complete wg.Wait() @@ -110,25 +109,25 @@ func RunDecryptionMonitor() { } func printStatistics() { - fmt.Printf("\n=== Final Decryption Statistics ===\n") - fmt.Printf("Total decryption attempts: %d\n", stats.totalDecryptions.Load()) - fmt.Printf("Successful decryptions: %d\n", stats.validDecryptions.Load()) - fmt.Printf("Failed decryptions: %d\n", stats.invalidDecryptions.Load()) + log.Printf("\n=== Final Decryption Statistics ===\n") + log.Printf("Total decryption attempts: %d\n", stats.totalDecryptions.Load()) + log.Printf("Successful decryptions: %d\n", stats.validDecryptions.Load()) + log.Printf("Failed decryptions: %d\n", stats.invalidDecryptions.Load()) total := stats.totalDecryptions.Load() if total > 0 { successRate := float64(stats.validDecryptions.Load()) / float64(total) * 100 - fmt.Printf("Success rate: %.2f%%\n", successRate) + log.Printf("Success rate: %.2f%%\n", successRate) } } func runFlow(baseURL, address string, wg *sync.WaitGroup) { timestamp := time.Now().Format("2006-01-02 15:04:05") - fmt.Printf("\n=== Performance Check at %s ===\n", timestamp) + log.Printf("\n=== Performance Check at %s ===\n", timestamp) encryptionData, err := getDataForEncryption(baseURL, address, "") if err != nil { - log.Err(err).Msg("error in get data for encryption endpoint") + log.Fatalf("error in get data for encryption endpoint %s", err) return } @@ -140,7 +139,7 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup) { err = registerIdentity(baseURL, registerReq) if err != nil { - log.Err(err).Msg("error encountered while registering identity") + log.Fatalf("error encountered while registering identity %s", err) return } @@ -154,12 +153,11 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup) { stats.totalDecryptions.Add(1) err := getDecryptionKey(baseURL, identity) if err != nil { - log.Err(err).Msg("error encountered while get decryption key") + log.Fatalf("error encountered while getting decryption key%s", err) stats.invalidDecryptions.Add(1) - return + } else { + stats.validDecryptions.Add(1) } - - stats.validDecryptions.Add(1) }(encryptionData["message"].Identity, decryptionTime) } @@ -220,8 +218,6 @@ func registerIdentity(baseURL string, req RegisterIdentityRequest) error { return fmt.Errorf("failed to read response: %v", err) } - fmt.Println("StatusCode registerIdentity", resp.StatusCode, req.DecryptionTimestamp) - if resp.StatusCode != http.StatusOK { var errorResp ErrorResponse if err := json.Unmarshal(body, &errorResp); err != nil { @@ -247,7 +243,6 @@ func getDecryptionKey(baseURL, identity string) error { if err != nil { return fmt.Errorf("failed to read response: %v", err) } - fmt.Println("StatusCode getDecryptionKey", resp.StatusCode, identity) if resp.StatusCode != http.StatusOK { var errorResp ErrorResponse if err := json.Unmarshal(body, &errorResp); err != nil { From d9bfb15a7e87a48162fc0cee9d2282e8bfc8204c Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 25 Mar 2025 14:06:33 +0100 Subject: [PATCH 04/11] clean deps --- go.mod | 6 ------ go.sum | 10 ---------- 2 files changed, 16 deletions(-) diff --git a/go.mod b/go.mod index f05298e..88d4083 100644 --- a/go.mod +++ b/go.mod @@ -15,11 +15,6 @@ require ( gotest.tools v2.2.0+incompatible ) -require ( - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect -) - require ( github.com/Microsoft/go-winio v0.6.2 // indirect github.com/bits-and-blooms/bitset v1.10.0 // indirect @@ -44,7 +39,6 @@ require ( github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/rs/zerolog v1.34.0 github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/supranational/blst v0.3.12 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect diff --git a/go.sum b/go.sum index d823e87..0ccd908 100644 --- a/go.sum +++ b/go.sum @@ -38,7 +38,6 @@ github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJ github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I= @@ -75,7 +74,6 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= @@ -191,8 +189,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -229,11 +225,8 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99 github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE= github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= -github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= -github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= @@ -326,12 +319,9 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= From 56190dc34a147ee7e0a1ee242a8c328dd1bd067a Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 25 Mar 2025 14:12:04 +0100 Subject: [PATCH 05/11] remove ununsed types --- tests/decryptionmonitor.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index d629568..5b09830 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -33,20 +33,6 @@ var ( stopChan = make(chan os.Signal, 1) ) -type RegisterIdentityResponse struct { - Eon uint64 `json:"eon"` - Identity string `json:"identity"` - IdentityPrefix string `json:"identity_prefix"` - EonKey string `json:"eon_key"` - TxHash string `json:"tx_hash"` -} - -type GetDecryptionKeyResponse struct { - DecryptionKey string `json:"decryption_key"` - Identity string `json:"identity"` - DecryptionTimestamp uint64 `json:"decryption_timestamp"` -} - type GetDataForEncryptionResponse struct { Eon uint64 `json:"eon"` Identity string `json:"identity"` From 8de28575fd3616b02677344b3793e8ae41a32068 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Thu, 3 Apr 2025 17:32:04 +0200 Subject: [PATCH 06/11] add timestamp from block header --- main.go | 4 ++-- template.env | 1 + tests/decryptionmonitor.go | 42 +++++++++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 4e92983..319955c 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { var modes []string var cfg config.Config if len(os.Args[1:]) == 0 { - cfg := config.LoadConfig() + cfg = config.LoadConfig() log.Println(cfg.Mode) mode := cfg.Mode @@ -68,7 +68,7 @@ func main() { case "decryption-monitor": wg.Add(1) go func() { - tests.RunDecryptionMonitor() + tests.RunDecryptionMonitor(cfg) wg.Done() }() default: diff --git a/template.env b/template.env index b505638..fb42f53 100644 --- a/template.env +++ b/template.env @@ -19,3 +19,4 @@ TEST_DURATION=1 SHUTTER_API="http://shutter-api.shutter.network/api" API_REQUEST_INTERVAL="60" SHUTTER_REGISTRY_CALLER_ADDRESS="0x228DefCF37Da29475F0EE2B9E4dfAeDc3b0746bc" +DEC_KEY_WAIT_INTERVAL="20" \ No newline at end of file diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index 5b09830..a4bbabd 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -2,6 +2,7 @@ package tests import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -15,6 +16,9 @@ import ( "sync/atomic" "syscall" "time" + + "github.com/ethereum/go-ethereum/ethclient" + "github.com/shutter-network/nethermind-tests/config" ) type RegisterIdentityRequest struct { @@ -47,7 +51,7 @@ type ErrorResponse struct { StatusCode int `json:"statusCode"` } -func RunDecryptionMonitor() { +func RunDecryptionMonitor(cfg config.Config) { baseURL := os.Getenv("SHUTTER_API") seconds, err := strconv.Atoi(os.Getenv("API_REQUEST_INTERVAL")) if err != nil { @@ -66,9 +70,14 @@ func RunDecryptionMonitor() { var wg sync.WaitGroup + client, err := ethclient.Dial(cfg.NodeURL) + if err != nil { + log.Fatalf("Failed to connect to the Ethereum client: %v", err) + return + } // Start monitoring in separate goroutine go func() { - runFlow(baseURL, address, &wg) + runFlow(client, baseURL, address, &wg) ticker := time.NewTicker(interval) defer ticker.Stop() @@ -76,7 +85,7 @@ func RunDecryptionMonitor() { for { select { case <-ticker.C: - runFlow(baseURL, address, &wg) + runFlow(client, baseURL, address, &wg) case <-stopChan: return } @@ -107,7 +116,7 @@ func printStatistics() { } } -func runFlow(baseURL, address string, wg *sync.WaitGroup) { +func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGroup) { timestamp := time.Now().Format("2006-01-02 15:04:05") log.Printf("\n=== Performance Check at %s ===\n", timestamp) @@ -116,10 +125,14 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup) { log.Fatalf("error in get data for encryption endpoint %s", err) return } - - decryptionTime := time.Now().Add(20 * time.Second) + block, err := client.BlockByNumber(context.Background(), nil) + if err != nil { + log.Fatalf("error from rpc while requesting for block %s", err) + return + } + decryptionTimestamp := block.Header().Time + 10 registerReq := RegisterIdentityRequest{ - DecryptionTimestamp: decryptionTime.Unix(), + DecryptionTimestamp: int64(decryptionTimestamp), IdentityPrefix: encryptionData["message"].IdentityPrefix, } @@ -131,20 +144,25 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup) { // Launch decryption key request in separate goroutine wg.Add(1) - go func(identity string, decryptTime time.Time) { + go func(identity string) { defer wg.Done() - waitDuration := time.Until(decryptTime.Add(2 * time.Second)) - time.Sleep(waitDuration) + seconds, err := strconv.Atoi(os.Getenv("DEC_KEY_WAIT_INTERVAL")) + if err != nil { + log.Fatalf("incorrect decryption key wait interval %s", err) + return + } + + time.Sleep(time.Duration(seconds) * time.Second) stats.totalDecryptions.Add(1) - err := getDecryptionKey(baseURL, identity) + err = getDecryptionKey(baseURL, identity) if err != nil { log.Fatalf("error encountered while getting decryption key%s", err) stats.invalidDecryptions.Add(1) } else { stats.validDecryptions.Add(1) } - }(encryptionData["message"].Identity, decryptionTime) + }(encryptionData["message"].Identity) } func getDataForEncryption(baseURL, address, identityPrefix string) (map[string]GetDataForEncryptionResponse, error) { From 109dbb507f67ce32f9c1af835f1d5f37c8a7b98a Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Mon, 28 Apr 2025 10:23:00 +0200 Subject: [PATCH 07/11] update docker compose --- config/config.go | 47 +++++++++++++++++++++++--------------- docker-compose.yaml | 5 +++- tests/decryptionmonitor.go | 26 ++++++--------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/config/config.go b/config/config.go index b331f3a..1ed2c39 100644 --- a/config/config.go +++ b/config/config.go @@ -10,16 +10,21 @@ import ( ) type Config struct { - Mode string - PrivateKey string - ChiadoURL string - ChiadoSendInterval time.Duration - GnosisURL string - GnosisSendInterval time.Duration - Interval time.Duration - Timeout time.Duration - TestDuration time.Duration - NodeURL string + Mode string + PrivateKey string + ChiadoURL string + ChiadoSendInterval time.Duration + GnosisURL string + GnosisSendInterval time.Duration + Interval time.Duration + Timeout time.Duration + TestDuration time.Duration + NodeURL string + ShutterAPI string + ShutterRegistryCaller string + ShutterRegistryCallerNonce string + ApiRequestInterval time.Duration + DecryptionKeyWaitInterval time.Duration } func LoadConfig() Config { @@ -30,15 +35,19 @@ func LoadConfig() Config { } config := Config{ - Mode: os.Getenv("MODE"), - PrivateKey: os.Getenv("PRIVATE_KEY"), - ChiadoURL: os.Getenv("CHIADO_URL"), - ChiadoSendInterval: time.Duration(GetEnvAsInt("CHIADO_SEND_INTERVAL")) * time.Second, - GnosisURL: os.Getenv("GNOSIS_URL"), - GnosisSendInterval: time.Duration(GetEnvAsInt("GNOSIS_SEND_INTERVAL")) * time.Second, - Timeout: time.Duration(GetEnvAsInt("WAIT_TX_TIMEOUT")) * time.Second, - TestDuration: time.Duration(GetEnvAsInt("TEST_DURATION")) * time.Second, - NodeURL: os.Getenv("NODE_URL"), + Mode: os.Getenv("MODE"), + PrivateKey: os.Getenv("PRIVATE_KEY"), + ChiadoURL: os.Getenv("CHIADO_URL"), + ChiadoSendInterval: time.Duration(GetEnvAsInt("CHIADO_SEND_INTERVAL")) * time.Second, + GnosisURL: os.Getenv("GNOSIS_URL"), + GnosisSendInterval: time.Duration(GetEnvAsInt("GNOSIS_SEND_INTERVAL")) * time.Second, + Timeout: time.Duration(GetEnvAsInt("WAIT_TX_TIMEOUT")) * time.Second, + TestDuration: time.Duration(GetEnvAsInt("TEST_DURATION")) * time.Second, + NodeURL: os.Getenv("NODE_URL"), + ShutterAPI: os.Getenv("SHUTTER_API"), + ApiRequestInterval: time.Duration(GetEnvAsInt("API_REQUEST_INTERVAL")) * time.Second, + ShutterRegistryCaller: os.Getenv("SHUTTER_REGISTRY_CALLER_ADDRESS"), + DecryptionKeyWaitInterval: time.Duration(GetEnvAsInt("DEC_KEY_WAIT_INTERVAL")) * time.Second, } return config diff --git a/docker-compose.yaml b/docker-compose.yaml index 4339342..c590447 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -17,5 +17,8 @@ services: - NODE_URL=${NODE_URL} - WAIT_TX_TIMEOUT=${WAIT_TX_TIMEOUT} - TEST_DURATION=${TEST_DURATION} - + - SHUTTER_API=${SHUTTER_API} + - SHUTTER_REGISTRY_CALLER_ADDRESS=${SHUTTER_REGISTRY_CALLER_ADDRESS} + - API_REQUEST_INTERVAL=${API_REQUEST_INTERVAL} + - DEC_KEY_WAIT_INTERVAL=${DEC_KEY_WAIT_INTERVAL} command: ./main diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index a4bbabd..622096a 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -11,7 +11,6 @@ import ( "net/url" "os" "os/signal" - "strconv" "sync" "sync/atomic" "syscall" @@ -52,14 +51,9 @@ type ErrorResponse struct { } func RunDecryptionMonitor(cfg config.Config) { - baseURL := os.Getenv("SHUTTER_API") - seconds, err := strconv.Atoi(os.Getenv("API_REQUEST_INTERVAL")) - if err != nil { - log.Fatalf("incorrect api request interval %s", err) - return - } - interval := time.Duration(seconds) * time.Second - address := os.Getenv("SHUTTER_REGISTRY_CALLER_ADDRESS") + baseURL := cfg.ShutterAPI + interval := cfg.ApiRequestInterval + address := cfg.ShutterRegistryCaller log.Printf("Starting performance monitoring\n") log.Printf("Base URL: %s\n", baseURL) @@ -77,7 +71,7 @@ func RunDecryptionMonitor(cfg config.Config) { } // Start monitoring in separate goroutine go func() { - runFlow(client, baseURL, address, &wg) + runFlow(client, baseURL, address, &wg, cfg) ticker := time.NewTicker(interval) defer ticker.Stop() @@ -85,7 +79,7 @@ func RunDecryptionMonitor(cfg config.Config) { for { select { case <-ticker.C: - runFlow(client, baseURL, address, &wg) + runFlow(client, baseURL, address, &wg, cfg) case <-stopChan: return } @@ -116,7 +110,7 @@ func printStatistics() { } } -func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGroup) { +func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { timestamp := time.Now().Format("2006-01-02 15:04:05") log.Printf("\n=== Performance Check at %s ===\n", timestamp) @@ -147,13 +141,7 @@ func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGro go func(identity string) { defer wg.Done() - seconds, err := strconv.Atoi(os.Getenv("DEC_KEY_WAIT_INTERVAL")) - if err != nil { - log.Fatalf("incorrect decryption key wait interval %s", err) - return - } - - time.Sleep(time.Duration(seconds) * time.Second) + time.Sleep(cfg.DecryptionKeyWaitInterval) stats.totalDecryptions.Add(1) err = getDecryptionKey(baseURL, identity) if err != nil { From 5e65041b2f0ba1c8c594317edd63d1ef7e62c391 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Mon, 28 Apr 2025 10:31:44 +0200 Subject: [PATCH 08/11] add docs --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2faecdb..91d6f02 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,12 @@ GNOSIS_SEND_INTERVAL="600" NODE_URL="https://erpc.chiado.staging.shutter.network" WAIT_TX_TIMEOUT=10 TEST_DURATION=1 + +#DECRYPTION MONITOR +SHUTTER_API="http://shutter-api.shutter.network/api" +API_REQUEST_INTERVAL="60" +SHUTTER_REGISTRY_CALLER_ADDRESS="0x228DefCF37Da29475F0EE2B9E4dfAeDc3b0746bc" +DEC_KEY_WAIT_INTERVAL="20" ``` - `MODE=chiado`: Sends transactions at intervals defined by `CHIADO_SEND_INTERVAL` to the Chiado URL. @@ -41,6 +47,7 @@ TEST_DURATION=1 - waits for a timeout defined by `WAIT_TX_TIMEOUT` - then sends the next one at nonce `n+ 1` - test is run for the duration defined in `TEST_DURATION` +- `MODE=decryption-monitor`: Sends an HTTP request to register an identity at intervals defined by `API_REQUEST_INTERVAL` on the Shutter API, and monitors the release of relevant decryption keys by requesting them from the Shutter API at intervals defined by `DEC_KEY_WAIT_INTERVAL`. - Multiple tests can be run at the same time by separating the different modes with a comma, i.e. `MODE="chiado,gnosis"`. From 6b42ea3c6e37b4d23aad9563a9c409d3f99c651c Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Wed, 14 May 2025 11:50:44 +0530 Subject: [PATCH 09/11] updated docker compose and config file to work on droplet --- config/config.go | 10 ++++------ docker-compose.yaml | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/config/config.go b/config/config.go index 1ed2c39..dd8c409 100644 --- a/config/config.go +++ b/config/config.go @@ -5,8 +5,6 @@ import ( "os" "strconv" "time" - - "github.com/joho/godotenv" ) type Config struct { @@ -29,10 +27,10 @@ type Config struct { func LoadConfig() Config { // Load the .env file - err := godotenv.Load() - if err != nil { - log.Fatalf("Error loading .env file") - } + // err := godotenv.Load() + // if err != nil { + // log.Fatalf("Error loading .env file") + // } config := Config{ Mode: os.Getenv("MODE"), diff --git a/docker-compose.yaml b/docker-compose.yaml index c590447..2d53ef9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -8,17 +8,17 @@ services: volumes: - ./logs:/app/logs environment: - - PRIVATE_KEY=${PRIVATE_KEY} - - MODE=${MODE} - - CHIADO_URL=${CHIADO_URL} - - CHIADO_SEND_INTERVAL=${CHIADO_SEND_INTERVAL} - - GNOSIS_URL=${GNOSIS_URL} - - GNOSIS_SEND_INTERVAL=${GNOSIS_SEND_INTERVAL} - - NODE_URL=${NODE_URL} - - WAIT_TX_TIMEOUT=${WAIT_TX_TIMEOUT} - - TEST_DURATION=${TEST_DURATION} - - SHUTTER_API=${SHUTTER_API} - - SHUTTER_REGISTRY_CALLER_ADDRESS=${SHUTTER_REGISTRY_CALLER_ADDRESS} - - API_REQUEST_INTERVAL=${API_REQUEST_INTERVAL} - - DEC_KEY_WAIT_INTERVAL=${DEC_KEY_WAIT_INTERVAL} - command: ./main + PRIVATE_KEY: + MODE: + CHIADO_URL: + CHIADO_SEND_INTERVAL: + GNOSIS_URL: + GNOSIS_SEND_INTERVAL: + NODE_URL: + WAIT_TX_TIMEOUT: + TEST_DURATION: + SHUTTER_API: + SHUTTER_REGISTRY_CALLER_ADDRESS: + API_REQUEST_INTERVAL: + DEC_KEY_WAIT_INTERVAL: + # command: ./main From 7be611dde1b3588404ef29da954078d5315488c3 Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Mon, 16 Jun 2025 16:59:21 +0530 Subject: [PATCH 10/11] fix: identity retreival and register_identity response handling --- tests/decryptionmonitor.go | 61 +++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index 622096a..f81f9af 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -2,7 +2,6 @@ package tests import ( "bytes" - "context" "encoding/json" "fmt" "io" @@ -16,7 +15,6 @@ import ( "syscall" "time" - "github.com/ethereum/go-ethereum/ethclient" "github.com/shutter-network/nethermind-tests/config" ) @@ -25,6 +23,18 @@ type RegisterIdentityRequest struct { IdentityPrefix string `json:"identityPrefix"` } +type RegisterIdentityResponse struct { + Message RegisterIdentityMessage `json:"message"` +} + +type RegisterIdentityMessage struct { + Eon int64 `json:"eon"` + Identity string `json:"identity"` + IdentityPrefix string `json:"identity_prefix"` + EonKey string `json:"eon_key"` + TxHash string `json:"tx_hash"` +} + type DecryptionStats struct { totalDecryptions atomic.Int64 validDecryptions atomic.Int64 @@ -64,14 +74,9 @@ func RunDecryptionMonitor(cfg config.Config) { var wg sync.WaitGroup - client, err := ethclient.Dial(cfg.NodeURL) - if err != nil { - log.Fatalf("Failed to connect to the Ethereum client: %v", err) - return - } // Start monitoring in separate goroutine go func() { - runFlow(client, baseURL, address, &wg, cfg) + runFlow(baseURL, address, &wg, cfg) ticker := time.NewTicker(interval) defer ticker.Stop() @@ -79,7 +84,7 @@ func RunDecryptionMonitor(cfg config.Config) { for { select { case <-ticker.C: - runFlow(client, baseURL, address, &wg, cfg) + runFlow(baseURL, address, &wg, cfg) case <-stopChan: return } @@ -110,7 +115,7 @@ func printStatistics() { } } -func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { +func runFlow(baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { timestamp := time.Now().Format("2006-01-02 15:04:05") log.Printf("\n=== Performance Check at %s ===\n", timestamp) @@ -119,18 +124,13 @@ func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGro log.Fatalf("error in get data for encryption endpoint %s", err) return } - block, err := client.BlockByNumber(context.Background(), nil) - if err != nil { - log.Fatalf("error from rpc while requesting for block %s", err) - return - } - decryptionTimestamp := block.Header().Time + 10 + decryptionTimestamp := time.Now().Unix() + 10 registerReq := RegisterIdentityRequest{ - DecryptionTimestamp: int64(decryptionTimestamp), + DecryptionTimestamp: decryptionTimestamp, IdentityPrefix: encryptionData["message"].IdentityPrefix, } - err = registerIdentity(baseURL, registerReq) + identity, err := registerIdentity(baseURL, registerReq) if err != nil { log.Fatalf("error encountered while registering identity %s", err) return @@ -140,17 +140,18 @@ func runFlow(client *ethclient.Client, baseURL, address string, wg *sync.WaitGro wg.Add(1) go func(identity string) { defer wg.Done() - time.Sleep(cfg.DecryptionKeyWaitInterval) + fmt.Printf("Requesting decryption key for identity: %s\n, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) stats.totalDecryptions.Add(1) err = getDecryptionKey(baseURL, identity) if err != nil { log.Fatalf("error encountered while getting decryption key%s", err) stats.invalidDecryptions.Add(1) } else { + fmt.Printf("Decryption key retrieved successfully for identity: %s\n, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) stats.validDecryptions.Add(1) } - }(encryptionData["message"].Identity) + }(identity) } func getDataForEncryption(baseURL, address, identityPrefix string) (map[string]GetDataForEncryptionResponse, error) { @@ -189,10 +190,10 @@ func getDataForEncryption(baseURL, address, identityPrefix string) (map[string]G return response, nil } -func registerIdentity(baseURL string, req RegisterIdentityRequest) error { +func registerIdentity(baseURL string, req RegisterIdentityRequest) (string, error) { jsonData, err := json.Marshal(req) if err != nil { - return fmt.Errorf("failed to marshal request: %v", err) + return "", fmt.Errorf("failed to marshal request: %v", err) } resp, err := http.Post( @@ -201,24 +202,28 @@ func registerIdentity(baseURL string, req RegisterIdentityRequest) error { bytes.NewBuffer(jsonData), ) if err != nil { - return fmt.Errorf("request failed: %v", err) + return "", fmt.Errorf("request failed: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return fmt.Errorf("failed to read response: %v", err) + return "", fmt.Errorf("failed to read response: %v", err) } if resp.StatusCode != http.StatusOK { var errorResp ErrorResponse if err := json.Unmarshal(body, &errorResp); err != nil { - return fmt.Errorf("failed to parse error response: %v", err) + return "", fmt.Errorf("failed to parse error response: %v", err) } - return fmt.Errorf("response error %v", errorResp.Description) + return "", fmt.Errorf("response error %v", errorResp.Description) } - - return nil + var response RegisterIdentityResponse + if err := json.Unmarshal(body, &response); err != nil { + return "", fmt.Errorf("failed to parse response: %v", err) + } + fmt.Printf("Identity registered successfully: %s | tx: %s\n", response.Message.Identity, response.Message.TxHash) + return response.Message.Identity, nil } func getDecryptionKey(baseURL, identity string) error { From 13b1b3e032ec129497136b44eec50d316f0c60df Mon Sep 17 00:00:00 2001 From: blockchainluffy Date: Mon, 16 Jun 2025 17:18:57 +0530 Subject: [PATCH 11/11] feat: add blame file logging update tests to not fail if api is failing --- config/config.go | 2 ++ docker-compose.yaml | 1 + template.env | 3 ++- tests/decryptionmonitor.go | 46 ++++++++++++++++++++++++++++++++------ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index dd8c409..06c2b36 100644 --- a/config/config.go +++ b/config/config.go @@ -23,6 +23,7 @@ type Config struct { ShutterRegistryCallerNonce string ApiRequestInterval time.Duration DecryptionKeyWaitInterval time.Duration + BlameFolder string } func LoadConfig() Config { @@ -46,6 +47,7 @@ func LoadConfig() Config { ApiRequestInterval: time.Duration(GetEnvAsInt("API_REQUEST_INTERVAL")) * time.Second, ShutterRegistryCaller: os.Getenv("SHUTTER_REGISTRY_CALLER_ADDRESS"), DecryptionKeyWaitInterval: time.Duration(GetEnvAsInt("DEC_KEY_WAIT_INTERVAL")) * time.Second, + BlameFolder: os.Getenv("DECRYPTION_BLAME_FOLDER"), } return config diff --git a/docker-compose.yaml b/docker-compose.yaml index 2d53ef9..a2b2326 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -21,4 +21,5 @@ services: SHUTTER_REGISTRY_CALLER_ADDRESS: API_REQUEST_INTERVAL: DEC_KEY_WAIT_INTERVAL: + DECRYPTION_BLAME_FOLDER: # command: ./main diff --git a/template.env b/template.env index fb42f53..cd9dae0 100644 --- a/template.env +++ b/template.env @@ -19,4 +19,5 @@ TEST_DURATION=1 SHUTTER_API="http://shutter-api.shutter.network/api" API_REQUEST_INTERVAL="60" SHUTTER_REGISTRY_CALLER_ADDRESS="0x228DefCF37Da29475F0EE2B9E4dfAeDc3b0746bc" -DEC_KEY_WAIT_INTERVAL="20" \ No newline at end of file +DEC_KEY_WAIT_INTERVAL="20" +DECRYPTION_BLAME_FOLDER="decryptionBlameDir" \ No newline at end of file diff --git a/tests/decryptionmonitor.go b/tests/decryptionmonitor.go index f81f9af..c9ad99d 100644 --- a/tests/decryptionmonitor.go +++ b/tests/decryptionmonitor.go @@ -39,6 +39,7 @@ type DecryptionStats struct { totalDecryptions atomic.Int64 validDecryptions atomic.Int64 invalidDecryptions atomic.Int64 + errorMessages []string } var ( @@ -99,10 +100,10 @@ func RunDecryptionMonitor(cfg config.Config) { wg.Wait() // Print final statistics - printStatistics() + printStatistics(cfg) } -func printStatistics() { +func printStatistics(cfg config.Config) { log.Printf("\n=== Final Decryption Statistics ===\n") log.Printf("Total decryption attempts: %d\n", stats.totalDecryptions.Load()) log.Printf("Successful decryptions: %d\n", stats.validDecryptions.Load()) @@ -113,6 +114,32 @@ func printStatistics() { successRate := float64(stats.validDecryptions.Load()) / float64(total) * 100 log.Printf("Success rate: %.2f%%\n", successRate) } + + // Write to blame file if BlameFolder is set + if cfg.BlameFolder != "" { + blameFile := fmt.Sprintf("%s/%d.blame", cfg.BlameFolder, time.Now().Unix()) + f, err := os.Create(blameFile) + if err != nil { + log.Printf("Failed to create blame file: %v", err) + return + } + defer f.Close() + fmt.Fprintf(f, "=== Final Decryption Statistics ===\n") + fmt.Fprintf(f, "Total decryption attempts: %d\n", stats.totalDecryptions.Load()) + fmt.Fprintf(f, "Successful decryptions: %d\n", stats.validDecryptions.Load()) + fmt.Fprintf(f, "Failed decryptions: %d\n", stats.invalidDecryptions.Load()) + if total > 0 { + successRate := float64(stats.validDecryptions.Load()) / float64(total) * 100 + fmt.Fprintf(f, "Success rate: %.2f%%\n", successRate) + } + if len(stats.errorMessages) > 0 { + fmt.Fprintf(f, "\nError Messages:\n") + for _, msg := range stats.errorMessages { + fmt.Fprintf(f, "%s\n", msg) + } + } + log.Printf("Wrote statistics to blame file: %s", blameFile) + } } func runFlow(baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { @@ -121,7 +148,9 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { encryptionData, err := getDataForEncryption(baseURL, address, "") if err != nil { - log.Fatalf("error in get data for encryption endpoint %s", err) + log.Printf("error in get data for encryption endpoint %s", err) + stats.invalidDecryptions.Add(1) + stats.errorMessages = append(stats.errorMessages, fmt.Sprintf("Error in get data for encryption endpoint: %s", err)) return } decryptionTimestamp := time.Now().Unix() + 10 @@ -132,7 +161,9 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { identity, err := registerIdentity(baseURL, registerReq) if err != nil { - log.Fatalf("error encountered while registering identity %s", err) + log.Printf("error encountered while registering identity %s", err) + stats.invalidDecryptions.Add(1) + stats.errorMessages = append(stats.errorMessages, fmt.Sprintf("Error encountered while registering identity: %s", err)) return } @@ -141,14 +172,15 @@ func runFlow(baseURL, address string, wg *sync.WaitGroup, cfg config.Config) { go func(identity string) { defer wg.Done() time.Sleep(cfg.DecryptionKeyWaitInterval) - fmt.Printf("Requesting decryption key for identity: %s\n, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("Requesting decryption key for identity: %s, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) stats.totalDecryptions.Add(1) err = getDecryptionKey(baseURL, identity) if err != nil { - log.Fatalf("error encountered while getting decryption key%s", err) + log.Printf("error encountered while getting decryption key %s", err) stats.invalidDecryptions.Add(1) + stats.errorMessages = append(stats.errorMessages, fmt.Sprintf("Error encountered while getting decryption key: %s", err)) } else { - fmt.Printf("Decryption key retrieved successfully for identity: %s\n, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("Decryption key retrieved successfully for identity: %s, at time: %s\n", identity, time.Now().Format("2006-01-02 15:04:05")) stats.validDecryptions.Add(1) } }(identity)