From 62cc86dd030d5d5e46cca2dd30e0bd8c97aab6dc Mon Sep 17 00:00:00 2001 From: Marc Font Date: Tue, 18 Jul 2023 11:22:42 +0200 Subject: [PATCH 1/7] enable multiple valpubkey --- api/api.go | 111 +++++++++++++++++++++++++++++++-------------------- api/types.go | 1 + 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/api/api.go b/api/api.go index 8bbf5ce..eaa3e99 100644 --- a/api/api.go +++ b/api/api.go @@ -832,70 +832,93 @@ func (m *ApiService) handleOnchainMerkleProof(w http.ResponseWriter, req *http.R func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) - valPubKey := vars["valpubkey"] - if !IsValidPubkey(valPubKey) { - m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid validator pubkey format")) + valPubKeys := vars["valpubkey"] + if valPubKeys == "" { + m.respondError(w, http.StatusBadRequest, "no validator pubkey provided") return } - var correctFeeRelays []httpRelay - var wrongFeeRelays []httpRelay - var unregisteredRelays []httpRelay - registeredCorrectFee := false - relayers := m.cliCfg.RelayersEndpoints + // Split the valPubKeys into individual keys (assuming they are comma-separated) + keys := strings.Split(valPubKeys, ",") - for _, relay := range relayers { - url := fmt.Sprintf("%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) - resp, err := http.Get(url) - if err != nil { - m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) + var results []httpOkRelayersState + + for _, valPubKey := range keys { + if !IsValidPubkey(valPubKey) { + m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid validator pubkey format: %s", valPubKey)) + return + } + var correctFeeRelays []httpRelay + var wrongFeeRelays []httpRelay + var unregisteredRelays []httpRelay + registeredCorrectFee := false + var relays []string + + if m.Network == "mainnet" { + relays = config.MainnetRelays + } else if m.Network == "goerli" { + relays = config.GoerliRelays + } else { + m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid network: %s", m.Network)) return } - defer resp.Body.Close() - - if resp.StatusCode == http.StatusOK { - signedRegistration := &builderApiV1.SignedValidatorRegistration{} - bodyBytes, err := io.ReadAll(resp.Body) + for _, relay := range relays { + url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) + resp, err := http.Get(url) if err != nil { m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) return } + defer resp.Body.Close() - if err = json.Unmarshal(bodyBytes, signedRegistration); err != nil { - m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) - return - } + if resp.StatusCode == http.StatusOK { + signedRegistration := &builderApiV1.SignedValidatorRegistration{} - relayRegistration := httpRelay{ - RelayAddress: relay, - FeeRecipient: signedRegistration.Message.FeeRecipient.String(), - Timestamp: fmt.Sprintf("%d", signedRegistration.Message.Timestamp.UnixNano()), - } + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) + return + } - if utils.Equals(signedRegistration.Message.FeeRecipient.String(), m.Onchain.PoolAddress) { - correctFeeRelays = append(correctFeeRelays, relayRegistration) + if err = json.Unmarshal(bodyBytes, signedRegistration); err != nil { + m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) + return + } + + relayRegistration := httpRelay{ + RelayAddress: relay, + FeeRecipient: signedRegistration.Message.FeeRecipient.String(), + Timestamp: fmt.Sprintf("%d", signedRegistration.Message.Timestamp.UnixNano()), + } + + if utils.Equals(signedRegistration.Message.FeeRecipient.String(), m.Onchain.PoolAddress) { + correctFeeRelays = append(correctFeeRelays, relayRegistration) + } else { + wrongFeeRelays = append(wrongFeeRelays, relayRegistration) + } } else { - wrongFeeRelays = append(wrongFeeRelays, relayRegistration) + unregisteredRelays = append(unregisteredRelays, httpRelay{ + RelayAddress: relay, + }) } - } else { - unregisteredRelays = append(unregisteredRelays, httpRelay{ - RelayAddress: relay, - }) } - } - // Only if there are some correct registrations and no invalid ones, its ok - if len(wrongFeeRelays) == 0 && len(correctFeeRelays) > 0 { - registeredCorrectFee = true + // Only if there are some correct registrations and no invalid ones, it's ok + if len(wrongFeeRelays) == 0 && len(correctFeeRelays) > 0 { + registeredCorrectFee = true + } + + results = append(results, httpOkRelayersState{ + ValPubKey: valPubKey, + CorrectFeeRecipients: registeredCorrectFee, + CorrectFeeRelays: correctFeeRelays, + WrongFeeRelays: wrongFeeRelays, + UnregisteredRelays: unregisteredRelays, + }) } - m.respondOK(w, httpOkRelayersState{ - CorrectFeeRecipients: registeredCorrectFee, - CorrectFeeRelays: correctFeeRelays, - WrongFeeRelays: wrongFeeRelays, - UnregisteredRelays: unregisteredRelays, - }) + m.respondOK(w, results) } func (m *ApiService) handleState(w http.ResponseWriter, req *http.Request) { diff --git a/api/types.go b/api/types.go index 977b2b3..fdf612d 100644 --- a/api/types.go +++ b/api/types.go @@ -33,6 +33,7 @@ type httpOkStatus struct { } type httpOkRelayersState struct { + ValPubKey string `json:"valpubkey"` CorrectFeeRecipients bool `json:"correct_fee_recipients"` CorrectFeeRelays []httpRelay `json:"correct_fee_relayers"` WrongFeeRelays []httpRelay `json:"wrong_fee_relayers"` From 5a42c2a3fd025455f5c30c3b0cb5a16710f9b7ce Mon Sep 17 00:00:00 2001 From: Marketen Date: Tue, 5 Sep 2023 19:03:53 +0200 Subject: [PATCH 2/7] 50 max + response changes --- api/api.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/api/api.go b/api/api.go index eaa3e99..05d2a18 100644 --- a/api/api.go +++ b/api/api.go @@ -840,9 +840,16 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re // Split the valPubKeys into individual keys (assuming they are comma-separated) keys := strings.Split(valPubKeys, ",") - + // Check if the number of pubkeys exceeds the maximum limit (50 in this case) + if len(keys) > 50 { + m.respondError(w, http.StatusBadRequest, "maximum number of pubkeys exceeded (max: 50)") + return + } var results []httpOkRelayersState + allValidatorsRegisteredCorrectFee := true // Assume all validators have correct fee registrations by default + var incorrectValidators []string // Initialize an array to store incorrect validators + for _, valPubKey := range keys { if !IsValidPubkey(valPubKey) { m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid validator pubkey format: %s", valPubKey)) @@ -863,6 +870,8 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re return } + // Iterate through all the relays and check if the validator has registered with the correct fee + // on any of them for _, relay := range relays { url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) resp, err := http.Get(url) @@ -896,6 +905,7 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re correctFeeRelays = append(correctFeeRelays, relayRegistration) } else { wrongFeeRelays = append(wrongFeeRelays, relayRegistration) + registeredCorrectFee = false // Set to false if any wrong fee registration is found } } else { unregisteredRelays = append(unregisteredRelays, httpRelay{ @@ -909,6 +919,11 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re registeredCorrectFee = true } + // If the validator is incorrect, add its pubkey to the incorrectValidators array + if !registeredCorrectFee { + incorrectValidators = append(incorrectValidators, valPubKey) + } + results = append(results, httpOkRelayersState{ ValPubKey: valPubKey, CorrectFeeRecipients: registeredCorrectFee, @@ -916,9 +931,25 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re WrongFeeRelays: wrongFeeRelays, UnregisteredRelays: unregisteredRelays, }) + + // Update the allValidatorsRegisteredCorrectFee status + if !registeredCorrectFee { + allValidatorsRegisteredCorrectFee = false + } + } + + // Define and populate the response struct. + response := struct { + Validators []httpOkRelayersState + AllValidatorsCorrect bool + IncorrectValidators []string + }{ + Validators: results, + AllValidatorsCorrect: allValidatorsRegisteredCorrectFee, + IncorrectValidators: incorrectValidators, } - m.respondOK(w, results) + m.respondOK(w, response) } func (m *ApiService) handleState(w http.ResponseWriter, req *http.Request) { From 2ba4b44164844839e31402a76e24b713822c9d21 Mon Sep 17 00:00:00 2001 From: Marketen Date: Thu, 7 Sep 2023 23:03:57 +0200 Subject: [PATCH 3/7] implement concurrency + fixes --- api/api.go | 234 +++++++++++++++++++++++++++++++++------------------ api/types.go | 7 ++ 2 files changed, 158 insertions(+), 83 deletions(-) diff --git a/api/api.go b/api/api.go index 05d2a18..209da6b 100644 --- a/api/api.go +++ b/api/api.go @@ -834,122 +834,188 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re vars := mux.Vars(req) valPubKeys := vars["valpubkey"] if valPubKeys == "" { - m.respondError(w, http.StatusBadRequest, "no validator pubkey provided") + m.respondError(w, http.StatusBadRequest, "No validator pubkey provided!") return } - // Split the valPubKeys into individual keys (assuming they are comma-separated) keys := strings.Split(valPubKeys, ",") - // Check if the number of pubkeys exceeds the maximum limit (50 in this case) if len(keys) > 50 { - m.respondError(w, http.StatusBadRequest, "maximum number of pubkeys exceeded (max: 50)") + m.respondError(w, http.StatusBadRequest, "Maximum number of pubkeys exceeded (max: 50)") return } + + for _, key := range keys { + if !IsValidPubkey(key) { + m.respondError(w, http.StatusBadRequest, "Invalid validator pubkey format: "+key) + return + } + } + + results, allValid, err := m.processValidatorsConcurrently(keys) + if err != nil { + m.respondError(w, http.StatusInternalServerError, err.Error()) + return + } + + response := struct { + Validators []httpOkRelayersState + AllValidatorsCorrect bool + IncorrectValidators []string + }{ + Validators: results, + AllValidatorsCorrect: allValid, + IncorrectValidators: m.extractIncorrectValidators(results), + } + + m.respondOK(w, response) +} + +func (m *ApiService) processValidatorsConcurrently(keys []string) ([]httpOkRelayersState, bool, error) { var results []httpOkRelayersState + allValidatorsRegisteredCorrectFee := true + resultsChan := make(chan ValidatorRelayResult, len(keys)) - allValidatorsRegisteredCorrectFee := true // Assume all validators have correct fee registrations by default - var incorrectValidators []string // Initialize an array to store incorrect validators + for i, key := range keys { + go m.processSingleValidator(i, key, resultsChan) + } - for _, valPubKey := range keys { - if !IsValidPubkey(valPubKey) { - m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid validator pubkey format: %s", valPubKey)) - return + for range keys { + res := <-resultsChan + if res.Err != nil { + return nil, false, res.Err } - var correctFeeRelays []httpRelay - var wrongFeeRelays []httpRelay - var unregisteredRelays []httpRelay - registeredCorrectFee := false - var relays []string - - if m.Network == "mainnet" { - relays = config.MainnetRelays - } else if m.Network == "goerli" { - relays = config.GoerliRelays - } else { - m.respondError(w, http.StatusInternalServerError, fmt.Sprintf("invalid network: %s", m.Network)) - return + if !res.IsValidatorValid { + allValidatorsRegisteredCorrectFee = false } + results = append(results, res.ValidatorResult) + } + close(resultsChan) - // Iterate through all the relays and check if the validator has registered with the correct fee - // on any of them - for _, relay := range relays { - url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) - resp, err := http.Get(url) - if err != nil { - m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) - return - } - defer resp.Body.Close() + sort.Slice(results, func(i, j int) bool { + return results[i].ValPubKey < results[j].ValPubKey + }) - if resp.StatusCode == http.StatusOK { - signedRegistration := &builderApiV1.SignedValidatorRegistration{} + return results, allValidatorsRegisteredCorrectFee, nil +} - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) - return - } +func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsChan chan ValidatorRelayResult) { + var correctFeeRelays []httpRelay + var wrongFeeRelays []httpRelay + var unregisteredRelays []httpRelay + registeredCorrectFee := false + var relays []string + + if m.Network == "mainnet" { + relays = config.MainnetRelays + } else if m.Network == "goerli" { + relays = config.GoerliRelays + } else { + resultsChan <- ValidatorRelayResult{ + Index: idx, + Err: fmt.Errorf("invalid network: %s", m.Network), + } + return + } - if err = json.Unmarshal(bodyBytes, signedRegistration); err != nil { - m.respondError(w, http.StatusInternalServerError, "could not call relayer endpoint: "+err.Error()) - return - } + for _, relay := range relays { + url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) + resp, err := http.Get(url) + if err != nil { + resultsChan <- ValidatorRelayResult{ + Index: idx, + Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, err), + } + return + } - relayRegistration := httpRelay{ - RelayAddress: relay, - FeeRecipient: signedRegistration.Message.FeeRecipient.String(), - Timestamp: fmt.Sprintf("%d", signedRegistration.Message.Timestamp.UnixNano()), - } + bodyBytes, err := io.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + resultsChan <- ValidatorRelayResult{ + Index: idx, + Err: fmt.Errorf("error reading response from relayer %s for validator %s: %v", relay, valPubKey, err), + } + return + } - if utils.Equals(signedRegistration.Message.FeeRecipient.String(), m.Onchain.PoolAddress) { - correctFeeRelays = append(correctFeeRelays, relayRegistration) - } else { - wrongFeeRelays = append(wrongFeeRelays, relayRegistration) - registeredCorrectFee = false // Set to false if any wrong fee registration is found + // If the validator is or has been registered, the relayer will return a 200 message + // with the signed registration message. If the validator has never been registered, + // the relayer will return code 400 or 404 (depending on the relay) with the following message: + // { + // "code": 404, + // "message": "no registration found for validator 0xafcdacfb67396a41a72676f3b064bcf62e977e5ef1d8aebadeed06e97156d4f640516fb205d12211ada9a54fcc26cc58" + // } + // https://flashbots.github.io/relay-specs/#/Data/getValidatorRegistration + if resp.StatusCode == http.StatusOK { + signedRegistration := &builderApiV1.SignedValidatorRegistration{} + + if err = json.Unmarshal(bodyBytes, signedRegistration); err != nil { + resultsChan <- ValidatorRelayResult{ + Index: idx, + Err: fmt.Errorf("error unmarshalling relay response from relayer %s for validator %s: %v", relay, valPubKey, err), } + return + } + + relayRegistration := httpRelay{ + RelayAddress: relay, + FeeRecipient: signedRegistration.Message.FeeRecipient.String(), + Timestamp: fmt.Sprintf("%d", signedRegistration.Message.Timestamp.UnixNano()), + } + + // If the fee recipient matches the pool address, the relayer is registered + if utils.Equals(signedRegistration.Message.FeeRecipient.String(), m.Onchain.PoolAddress) { + correctFeeRelays = append(correctFeeRelays, relayRegistration) } else { - unregisteredRelays = append(unregisteredRelays, httpRelay{ - RelayAddress: relay, - }) + // if the fee recipient does not match the pool address, the relayer is registered but with the wrong fee recipient + wrongFeeRelays = append(wrongFeeRelays, relayRegistration) } - } - // Only if there are some correct registrations and no invalid ones, it's ok - if len(wrongFeeRelays) == 0 && len(correctFeeRelays) > 0 { - registeredCorrectFee = true - } + // else if (signedRegistration.Message.FeeRecipient.String() != m.Onchain.PoolAddress) && (signedRegistration.Message.FeeRecipient.String() != "") { + // // If the fee recipient does not match the pool address, the relayer is registered but with the wrong fee recipient + // wrongFeeRelays = append(wrongFeeRelays, relayRegistration) + // } - // If the validator is incorrect, add its pubkey to the incorrectValidators array - if !registeredCorrectFee { - incorrectValidators = append(incorrectValidators, valPubKey) + } else if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusNotFound { + // the validator is not registered with the relayer + unregisteredRelays = append(unregisteredRelays, httpRelay{RelayAddress: relay}) + } else { + // there was an error calling the relayer, so we couldnt check if the validator is/was registered with the correct + // fee recipient, so we return an error. + resultsChan <- ValidatorRelayResult{ + Index: idx, + Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, string(bodyBytes)), + } } + } + + // If there are no wrong fee relays and there are correct fee relays, the validator is registered with the correct fee recipient + // we do not accept validators that have not registered to any relay + if len(wrongFeeRelays) == 0 && len(correctFeeRelays) > 0 { + registeredCorrectFee = true + } - results = append(results, httpOkRelayersState{ + resultsChan <- ValidatorRelayResult{ + Index: idx, + ValidatorResult: httpOkRelayersState{ ValPubKey: valPubKey, CorrectFeeRecipients: registeredCorrectFee, CorrectFeeRelays: correctFeeRelays, WrongFeeRelays: wrongFeeRelays, UnregisteredRelays: unregisteredRelays, - }) - - // Update the allValidatorsRegisteredCorrectFee status - if !registeredCorrectFee { - allValidatorsRegisteredCorrectFee = false - } + }, + IsValidatorValid: registeredCorrectFee, } +} - // Define and populate the response struct. - response := struct { - Validators []httpOkRelayersState - AllValidatorsCorrect bool - IncorrectValidators []string - }{ - Validators: results, - AllValidatorsCorrect: allValidatorsRegisteredCorrectFee, - IncorrectValidators: incorrectValidators, +func (m *ApiService) extractIncorrectValidators(results []httpOkRelayersState) []string { + var incorrectValidators []string + for _, result := range results { + if !result.CorrectFeeRecipients { + incorrectValidators = append(incorrectValidators, result.ValPubKey) + } } - - m.respondOK(w, response) + return incorrectValidators } func (m *ApiService) handleState(w http.ResponseWriter, req *http.Request) { @@ -978,6 +1044,8 @@ func IsValidAddress(v string) bool { return re.MatchString(v) } +// The validator's BLS public key, uniquely identifying them. 48-bytes, hex encoded with 0x prefix, case insensitive. +// example: example: 0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7445a6d1a2753e5f3e8b1cfe39b46f43611ef74a func IsValidPubkey(v string) bool { re := regexp.MustCompile("^0x[0-9a-fA-f]{96}$") return re.MatchString(v) diff --git a/api/types.go b/api/types.go index fdf612d..a6950ab 100644 --- a/api/types.go +++ b/api/types.go @@ -138,6 +138,13 @@ type httpOkValidatorInfo struct { SubscriptionType string `json:"subscription_type"` } +type ValidatorRelayResult struct { + Index int + ValidatorResult httpOkRelayersState + IsValidatorValid bool + Err error +} + // Subscription event and the associated validator (if any) // TODO: Perhaps remove, no longer need if refactored a bit type Subscription struct { //TODO: remove From 942e14389379c2703fce1dfef05dd3d1ee1d2186 Mon Sep 17 00:00:00 2001 From: Marketen Date: Fri, 8 Sep 2023 01:30:41 +0200 Subject: [PATCH 4/7] clarify comments --- api/api.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/api/api.go b/api/api.go index 209da6b..cfdff99 100644 --- a/api/api.go +++ b/api/api.go @@ -938,13 +938,7 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh return } - // If the validator is or has been registered, the relayer will return a 200 message - // with the signed registration message. If the validator has never been registered, - // the relayer will return code 400 or 404 (depending on the relay) with the following message: - // { - // "code": 404, - // "message": "no registration found for validator 0xafcdacfb67396a41a72676f3b064bcf62e977e5ef1d8aebadeed06e97156d4f640516fb205d12211ada9a54fcc26cc58" - // } + // If the validator is or has been registered, the relayer will return a 200 message with the signed registration message. // https://flashbots.github.io/relay-specs/#/Data/getValidatorRegistration if resp.StatusCode == http.StatusOK { signedRegistration := &builderApiV1.SignedValidatorRegistration{} @@ -963,7 +957,7 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh Timestamp: fmt.Sprintf("%d", signedRegistration.Message.Timestamp.UnixNano()), } - // If the fee recipient matches the pool address, the relayer is registered + // If the fee recipient matches the pool address, the relayer is registered with the correct fee recipient (the smoothing pool one) if utils.Equals(signedRegistration.Message.FeeRecipient.String(), m.Onchain.PoolAddress) { correctFeeRelays = append(correctFeeRelays, relayRegistration) } else { @@ -971,17 +965,12 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh wrongFeeRelays = append(wrongFeeRelays, relayRegistration) } - // else if (signedRegistration.Message.FeeRecipient.String() != m.Onchain.PoolAddress) && (signedRegistration.Message.FeeRecipient.String() != "") { - // // If the fee recipient does not match the pool address, the relayer is registered but with the wrong fee recipient - // wrongFeeRelays = append(wrongFeeRelays, relayRegistration) - // } - } else if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusNotFound { - // the validator is not registered with the relayer + // If the validator has never been registered, the relayer will return code 400 or 404 (depending on the relay) unregisteredRelays = append(unregisteredRelays, httpRelay{RelayAddress: relay}) } else { - // there was an error calling the relayer, so we couldnt check if the validator is/was registered with the correct - // fee recipient, so we return an error. + // If we get here, the relayer had an internal server error, so we couldnt check if the validator is/was registered with the correct + // fee recipient. We return an error. resultsChan <- ValidatorRelayResult{ Index: idx, Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, string(bodyBytes)), From c8c5654f94206a9068db0a743a78596a3caf010c Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 19 Sep 2023 15:00:47 +0200 Subject: [PATCH 5/7] rebase main --- api/api.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/api/api.go b/api/api.go index cfdff99..a7e7fdc 100644 --- a/api/api.go +++ b/api/api.go @@ -903,19 +903,8 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh var wrongFeeRelays []httpRelay var unregisteredRelays []httpRelay registeredCorrectFee := false - var relays []string - - if m.Network == "mainnet" { - relays = config.MainnetRelays - } else if m.Network == "goerli" { - relays = config.GoerliRelays - } else { - resultsChan <- ValidatorRelayResult{ - Index: idx, - Err: fmt.Errorf("invalid network: %s", m.Network), - } - return - } + + relays := m.cliCfg.RelayersEndpoints for _, relay := range relays { url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) From 100cbfc7e81faafe006986a0c207f97988f04f39 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 19 Sep 2023 15:17:28 +0200 Subject: [PATCH 6/7] remove https:// from relay check --- api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index a7e7fdc..3ae67eb 100644 --- a/api/api.go +++ b/api/api.go @@ -907,7 +907,7 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh relays := m.cliCfg.RelayersEndpoints for _, relay := range relays { - url := fmt.Sprintf("https://%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) + url := fmt.Sprintf("%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) resp, err := http.Get(url) if err != nil { resultsChan <- ValidatorRelayResult{ From 3c28370a36c9324abfc021d5abc9b283ad9b27df Mon Sep 17 00:00:00 2001 From: alrevuelta Date: Tue, 19 Sep 2023 17:14:51 +0200 Subject: [PATCH 7/7] Single channel and move types --- api/api.go | 45 +++++++++++++++------------------------------ api/types.go | 9 +++++++-- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/api/api.go b/api/api.go index 3ae67eb..1425960 100644 --- a/api/api.go +++ b/api/api.go @@ -857,26 +857,20 @@ func (m *ApiService) handleValidatorRelayers(w http.ResponseWriter, req *http.Re return } - response := struct { - Validators []httpOkRelayersState - AllValidatorsCorrect bool - IncorrectValidators []string - }{ + m.respondOK(w, httpOkMultiRelayersState{ Validators: results, AllValidatorsCorrect: allValid, IncorrectValidators: m.extractIncorrectValidators(results), - } - - m.respondOK(w, response) + }) } func (m *ApiService) processValidatorsConcurrently(keys []string) ([]httpOkRelayersState, bool, error) { var results []httpOkRelayersState allValidatorsRegisteredCorrectFee := true - resultsChan := make(chan ValidatorRelayResult, len(keys)) + resultsChan := make(chan validatorRelayResult) - for i, key := range keys { - go m.processSingleValidator(i, key, resultsChan) + for _, key := range keys { + go m.processSingleValidator(key, resultsChan) } for range keys { @@ -891,14 +885,10 @@ func (m *ApiService) processValidatorsConcurrently(keys []string) ([]httpOkRelay } close(resultsChan) - sort.Slice(results, func(i, j int) bool { - return results[i].ValPubKey < results[j].ValPubKey - }) - return results, allValidatorsRegisteredCorrectFee, nil } -func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsChan chan ValidatorRelayResult) { +func (m *ApiService) processSingleValidator(valPubKey string, resultsChan chan validatorRelayResult) { var correctFeeRelays []httpRelay var wrongFeeRelays []httpRelay var unregisteredRelays []httpRelay @@ -910,9 +900,8 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh url := fmt.Sprintf("%s/relay/v1/data/validator_registration?pubkey=%s", relay, valPubKey) resp, err := http.Get(url) if err != nil { - resultsChan <- ValidatorRelayResult{ - Index: idx, - Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, err), + resultsChan <- validatorRelayResult{ + Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, err), } return } @@ -920,9 +909,8 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh bodyBytes, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { - resultsChan <- ValidatorRelayResult{ - Index: idx, - Err: fmt.Errorf("error reading response from relayer %s for validator %s: %v", relay, valPubKey, err), + resultsChan <- validatorRelayResult{ + Err: fmt.Errorf("error reading response from relayer %s for validator %s: %v", relay, valPubKey, err), } return } @@ -933,9 +921,8 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh signedRegistration := &builderApiV1.SignedValidatorRegistration{} if err = json.Unmarshal(bodyBytes, signedRegistration); err != nil { - resultsChan <- ValidatorRelayResult{ - Index: idx, - Err: fmt.Errorf("error unmarshalling relay response from relayer %s for validator %s: %v", relay, valPubKey, err), + resultsChan <- validatorRelayResult{ + Err: fmt.Errorf("error unmarshalling relay response from relayer %s for validator %s: %v", relay, valPubKey, err), } return } @@ -960,9 +947,8 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh } else { // If we get here, the relayer had an internal server error, so we couldnt check if the validator is/was registered with the correct // fee recipient. We return an error. - resultsChan <- ValidatorRelayResult{ - Index: idx, - Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, string(bodyBytes)), + resultsChan <- validatorRelayResult{ + Err: fmt.Errorf("error calling relayer %s for validator %s: %v", relay, valPubKey, string(bodyBytes)), } } } @@ -973,8 +959,7 @@ func (m *ApiService) processSingleValidator(idx int, valPubKey string, resultsCh registeredCorrectFee = true } - resultsChan <- ValidatorRelayResult{ - Index: idx, + resultsChan <- validatorRelayResult{ ValidatorResult: httpOkRelayersState{ ValPubKey: valPubKey, CorrectFeeRecipients: registeredCorrectFee, diff --git a/api/types.go b/api/types.go index a6950ab..242646b 100644 --- a/api/types.go +++ b/api/types.go @@ -40,6 +40,12 @@ type httpOkRelayersState struct { UnregisteredRelays []httpRelay `json:"unregistered_relayers"` } +type httpOkMultiRelayersState struct { + Validators []httpOkRelayersState `json:"validators"` + AllValidatorsCorrect bool `json:"all_validators_correct"` + IncorrectValidators []string `json:"incorrect_validators"` +} + type httpRelay struct { RelayAddress string `json:"relay_address"` FeeRecipient string `json:"fee_recipient"` @@ -138,8 +144,7 @@ type httpOkValidatorInfo struct { SubscriptionType string `json:"subscription_type"` } -type ValidatorRelayResult struct { - Index int +type validatorRelayResult struct { ValidatorResult httpOkRelayersState IsValidatorValid bool Err error