Skip to content

Commit

Permalink
Add relayer reset endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 11, 2023
1 parent b4d2f04 commit 03176ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,39 @@ impl Database {
block_txs: block_txs as u64,
})
}

pub async fn purge_unsent_txs(&self, relayer_id: &str) -> eyre::Result<()> {
let unsent_txs = self.get_unsent_txs().await?;

let unsent_tx_ids: Vec<_> = unsent_txs
.into_iter()
.filter(|tx| tx.relayer_id == relayer_id)
.map(|tx| tx.id)
.collect();

sqlx::query(
r#"
DELETE FROM transactions
WHERE id = ANY($1::TEXT[])
"#,
)
.bind(&unsent_tx_ids)
.execute(&self.pool)
.await?;

sqlx::query(
r#"
UPDATE relayers
SET nonce = current_nonce
WHERE id = $1
"#,
)
.bind(relayer_id)
.execute(&self.pool)
.await?;

Ok(())
}
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tower_http::validate_request::ValidateRequestHeaderLayer;

use self::routes::relayer::{
create_relayer, create_relayer_api_key, get_relayer, get_relayers,
relayer_rpc, update_relayer,
purge_unsent_txs, relayer_rpc, update_relayer,
};
use self::routes::transaction::{get_tx, get_txs, send_tx};
use crate::app::App;
Expand Down Expand Up @@ -77,6 +77,7 @@ pub async fn spawn_server(

let mut admin_routes = Router::new()
.route("/relayer", post(create_relayer))
.route("/relayer/:relayer_id/reset", post(purge_unsent_txs))
.route("/relayers", get(get_relayers))
.route(
"/relayer/:relayer_id",
Expand Down
13 changes: 13 additions & 0 deletions src/server/routes/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ pub async fn get_relayer(
Ok(Json(relayer_info))
}

/// Resets the relayer
/// deletes all unsent txs
/// and resets nonce to the current confirmed nonce
#[tracing::instrument(skip(app))]
pub async fn purge_unsent_txs(
State(app): State<Arc<App>>,
Path(relayer_id): Path<String>,
) -> Result<(), ApiError> {
app.db.purge_unsent_txs(&relayer_id).await?;

Ok(())
}

#[tracing::instrument(skip(app))]
pub async fn relayer_rpc(
State(app): State<Arc<App>>,
Expand Down

0 comments on commit 03176ec

Please sign in to comment.