-
Notifications
You must be signed in to change notification settings - Fork 15
feat(core): add random relayer selection endpoint #28
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
Merged
joshstevens19
merged 13 commits into
joshstevens19:master
from
shiyasmohd:shiyasmohd/select-random-relayer
Oct 21, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b40faa8
feat(core): add random relayer selection endpoint
shiyasmohd f5b62c2
fix(core): cargo clippy
shiyasmohd 6c8da09
feat(sdk): add send random api support for rust and ts sdk
shiyasmohd 361d482
refactor(core): update send random api route to kebab case
shiyasmohd 812b4de
docs: add docs for sending txns to random relayer
shiyasmohd 8748f74
feat(core): add allowed_random_relayers config for send random endpoint
shiyasmohd 7f7f403
docs: add docs for allowed_random_relayers in config
shiyasmohd 5597054
feat(sdk): expose sendRandom method in typescript RelayerClient
shiyasmohd 27ec449
fix(core): change allowed_random_relayers to opt-in model
shiyasmohd 9f6574a
Update changelog.mdx
joshstevens19 f9ef82d
Update send_random_transaction.rs
joshstevens19 493e88a
fix: some changes to the sdks and docs
joshstevens19 7b421f6
remove
joshstevens19 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
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
63 changes: 63 additions & 0 deletions
63
crates/core/src/transaction/api/send_random_transaction.rs
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,63 @@ | ||
| use crate::app_state::AppState; | ||
| use crate::network::ChainId; | ||
| use crate::relayer::Relayer; | ||
| use crate::shared::{bad_request, not_found, HttpError}; | ||
| use crate::transaction::api::send_transaction::send_transaction; | ||
| use crate::transaction::api::{RelayTransactionRequest, SendTransactionResult}; | ||
| use axum::{ | ||
| extract::{Path, State}, | ||
| http::HeaderMap, | ||
| Json, | ||
| }; | ||
| use rand::seq::SliceRandom; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Handles random relayer selection for transaction requests | ||
| /// across multiple relayers on the same chain. | ||
| /// | ||
| /// This endpoint selects a random available (non-paused, non-internal) relayer | ||
| /// and forwards the transaction request to it. | ||
| pub async fn send_transaction_random( | ||
| State(state): State<Arc<AppState>>, | ||
| Path(chain_id): Path<ChainId>, | ||
| headers: HeaderMap, | ||
| Json(transaction): Json<RelayTransactionRequest>, | ||
| ) -> Result<Json<SendTransactionResult>, HttpError> { | ||
| state.validate_allowed_passed_basic_auth(&headers)?; | ||
| let relayer = select_random_relayer(&state, &chain_id).await?; | ||
| let result = send_transaction(relayer, transaction, &state, &headers).await?; | ||
| Ok(Json(result)) | ||
| } | ||
|
|
||
| /// Selects a random available relayer for the specified chain. | ||
| /// | ||
| /// Filters out paused, internal-only, and relayers only allowed for random selection. | ||
| /// Note: The random relayer feature must be explicitly enabled via `allowed_random_relayers` | ||
| /// config for the network, otherwise all relayers will be filtered out. | ||
| async fn select_random_relayer( | ||
| state: &Arc<AppState>, | ||
| chain_id: &ChainId, | ||
| ) -> Result<Relayer, HttpError> { | ||
| let relayers = state.db.get_all_relayers_for_chain(chain_id).await?; | ||
|
|
||
| if relayers.is_empty() { | ||
| return Err(not_found(format!("No relayers found for chain {}", chain_id))); | ||
| } | ||
|
|
||
| let mut rng = rand::thread_rng(); | ||
| // TODO: it should be smart enough to also only pick the one with enough native funds to send the tx | ||
| let available_relayers: Vec<_> = relayers | ||
| .into_iter() | ||
| .filter(|r| { | ||
| !r.paused | ||
| && !state.relayer_internal_only.restricted(&r.address, &r.chain_id) | ||
| && state.relayers_allowed_for_random.is_allowed(&r.address, &r.chain_id) | ||
| }) | ||
| .collect(); | ||
| available_relayers.choose(&mut rng).cloned().ok_or_else(|| { | ||
| bad_request(format!( | ||
| "No available relayers for chain {} (all relayers are paused, internal-only, or not allowed for random selection)", | ||
| chain_id | ||
| )) | ||
| }) | ||
| } |
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
42 changes: 42 additions & 0 deletions
42
crates/e2e-tests/src/tests/transactions/send_random_fails.rs
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,42 @@ | ||
| use crate::tests::test_runner::TestRunner; | ||
| use anyhow::anyhow; | ||
| use rrelayer_core::transaction::api::{RelayTransactionRequest, TransactionSpeed}; | ||
| use rrelayer_core::transaction::types::TransactionData; | ||
| use tracing::info; | ||
|
|
||
| impl TestRunner { | ||
| /// run single with: | ||
| /// RRELAYER_PROVIDERS="raw" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="privy" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="aws_secret_manager" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="aws_kms" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="gcp_secret_manager" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="turnkey" make run-test-debug TEST=send_random_fails | ||
| /// RRELAYER_PROVIDERS="pkcs11" make run-test-debug TEST=send_random_fails | ||
| pub async fn send_random_fails(&self) -> anyhow::Result<()> { | ||
| info!("Testing simple eth transfer..."); | ||
|
|
||
| let relayer = self.create_and_fund_relayer("send-random-fails").await?; | ||
| info!("Created relayer: {:?}", relayer); | ||
|
|
||
| let tx_request = RelayTransactionRequest { | ||
| to: self.config.anvil_accounts[1], | ||
| value: alloy::primitives::utils::parse_ether("0.1")?.into(), | ||
| data: TransactionData::empty(), | ||
| speed: Some(TransactionSpeed::FAST), | ||
| external_id: Some("send-random-fails".to_string()), | ||
| blobs: None, | ||
| }; | ||
|
|
||
| let relayer_client = self | ||
| .relayer_client | ||
| .client | ||
| .transaction() | ||
| .send_random(self.config.chain_id, &tx_request, None) | ||
| .await; | ||
| match relayer_client { | ||
| Err(_) => Ok(()), | ||
| Ok(_) => Err(anyhow!("Should not send random tx")), | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.