-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/add dec monitor #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
faheelsattar
wants to merge
11
commits into
main
Choose a base branch
from
feat/add-dec-monitor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dcaea48
add decryption monitor
faheelsattar 3f331cb
clean deps
faheelsattar 24059b3
improve logging
faheelsattar d9bfb15
clean deps
faheelsattar 56190dc
remove ununsed types
faheelsattar 8de2857
add timestamp from block header
faheelsattar 109dbb5
update docker compose
faheelsattar 5e65041
add docs
faheelsattar 6b42ea3
updated docker compose and config file to work on droplet
blockchainluffy 7be611d
fix: identity retreival and register_identity response handling
blockchainluffy 13b1b3e
feat: add blame file logging update tests to not fail if api is failing
blockchainluffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "sync" | ||
| "sync/atomic" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/ethereum/go-ethereum/ethclient" | ||
| "github.com/shutter-network/nethermind-tests/config" | ||
| ) | ||
|
|
||
| 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 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(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") | ||
|
|
||
| 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) | ||
|
|
||
| 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) | ||
|
|
||
| ticker := time.NewTicker(interval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| runFlow(client, baseURL, address, &wg) | ||
| case <-stopChan: | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // Wait for interrupt signal | ||
| <-stopChan | ||
| log.Printf("Shutting down...\n") | ||
|
|
||
| // Wait for all decryption requests to complete | ||
| wg.Wait() | ||
|
|
||
| // Print final statistics | ||
| printStatistics() | ||
| } | ||
|
|
||
| func printStatistics() { | ||
| 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 | ||
| log.Printf("Success rate: %.2f%%\n", successRate) | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
|
|
||
| encryptionData, err := getDataForEncryption(baseURL, address, "") | ||
| if err != nil { | ||
| 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 | ||
| registerReq := RegisterIdentityRequest{ | ||
| DecryptionTimestamp: int64(decryptionTimestamp), | ||
| IdentityPrefix: encryptionData["message"].IdentityPrefix, | ||
| } | ||
|
|
||
| err = registerIdentity(baseURL, registerReq) | ||
| if err != nil { | ||
| log.Fatalf("error encountered while registering identity %s", err) | ||
| return | ||
| } | ||
|
|
||
| // Launch decryption key request in separate goroutine | ||
| wg.Add(1) | ||
| 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) | ||
| 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 { | ||
| stats.validDecryptions.Add(1) | ||
| } | ||
| }(encryptionData["message"].Identity) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| 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 | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please update this to be in ms? Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in accordance with gnosis chain timestamps, so not sure what is the reason for us to change it to ms.