Skip to content
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

Fix/server restart opp removal #405

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ node_modules
tsconfig.tsbuildinfo

# Ignore stored SVM keypairs
keypairs/*.json
**/keypairs/*.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UPDATE opportunity SET removal_reason = NULL WHERE removal_reason = 'server_restart';
CREATE TYPE temp_opportunity_removal_reason AS ENUM ('expired', 'invalid');
ALTER TABLE opportunity
ALTER COLUMN removal_reason TYPE temp_opportunity_removal_reason
USING removal_reason::text::temp_opportunity_removal_reason;
DROP TYPE IF EXISTS opportunity_removal_reason;
ALTER TYPE temp_opportunity_removal_reason RENAME TO opportunity_removal_reason;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE opportunity_removal_reason ADD VALUE 'server_restart';
4 changes: 4 additions & 0 deletions auction-server/src/opportunity/entities/opportunity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub enum OpportunityRemovalReason {
// TODO use internal errors instead of RestError
#[allow(dead_code)]
Invalid(RestError),
ServerRestart,
}

pub enum OpportunityVerificationResult {
Expand All @@ -114,6 +115,9 @@ impl From<OpportunityRemovalReason> for repository::OpportunityRemovalReason {
match reason {
OpportunityRemovalReason::Expired => repository::OpportunityRemovalReason::Expired,
OpportunityRemovalReason::Invalid(_) => repository::OpportunityRemovalReason::Invalid,
OpportunityRemovalReason::ServerRestart => {
repository::OpportunityRemovalReason::ServerRestart
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use {
super::{
db::OpportunityTable,
InMemoryStore,
Repository,
},
crate::api::RestError,
};

impl<T: InMemoryStore, U: OpportunityTable<T>> Repository<T, U> {
pub async fn clear_opportunities_upon_restart(&self) -> Result<(), RestError> {
self.db.clear_opportunities_upon_restart().await
}
}
20 changes: 20 additions & 0 deletions auction-server/src/opportunity/repository/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub trait OpportunityTable<T: InMemoryStore> {
opportunity: &T::Opportunity,
reason: OpportunityRemovalReason,
) -> anyhow::Result<()>;
async fn clear_opportunities_upon_restart(&self) -> Result<(), RestError>;
}

impl<T: InMemoryStore> OpportunityTable<T> for DB {
Expand Down Expand Up @@ -167,4 +168,23 @@ impl<T: InMemoryStore> OpportunityTable<T> for DB {
.await?;
Ok(())
}

async fn clear_opportunities_upon_restart(&self) -> Result<(), RestError> {
let now = OffsetDateTime::now_utc();
let chain_type =
<<T::Opportunity as entities::Opportunity>::ModelMetadata>::get_chain_type();
sqlx::query!("UPDATE opportunity SET removal_time = $1, removal_reason = $2 WHERE removal_time IS NULL AND chain_type = $3",
PrimitiveDateTime::new(now.date(), now.time()),
OpportunityRemovalReason::ServerRestart as _,
chain_type as _
)
.execute(self)
.instrument(info_span!("db_clear_opportunities_upon_restart"))
.await
.map_err(|e| {
tracing::error!("DB: Failed to clear opportunities upon restart: {}", e);
RestError::TemporarilyUnavailable
})?;
Ok(())
}
}
1 change: 1 addition & 0 deletions auction-server/src/opportunity/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use {

mod add_opportunity;
mod add_spoof_info;
mod clear_opportunities_upon_restart;
mod db;
mod get_express_relay_metadata;
mod get_in_memory_opportunities;
Expand Down
3 changes: 2 additions & 1 deletion auction-server/src/opportunity/repository/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ use {
};

#[derive(Clone, Debug, PartialEq, PartialOrd, sqlx::Type)]
#[sqlx(type_name = "opportunity_removal_reason", rename_all = "lowercase")]
#[sqlx(type_name = "opportunity_removal_reason", rename_all = "snake_case")]
pub enum OpportunityRemovalReason {
Expired,
Invalid,
ServerRestart,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use {
super::{
ChainType,
Service,
},
crate::api::RestError,
};

impl<T: ChainType> Service<T> {
pub async fn clear_opportunities_upon_restart(&self) -> Result<(), RestError> {
self.repo.clear_opportunities_upon_restart().await
}
}
1 change: 1 addition & 0 deletions auction-server/src/opportunity/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use {
};

pub mod add_opportunity;
pub mod clear_opportunities_upon_restart;
pub mod get_config;
pub mod get_live_opportunities;
pub mod get_opportunities;
Expand Down
7 changes: 6 additions & 1 deletion auction-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,14 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
let store_new = Arc::new(StoreNew::new(
store.clone(),
opportunity_service_evm,
opportunity_service_svm,
opportunity_service_svm.clone(),
auction_services.clone(),
));
// For now, we only want to clear the SVM opportunities in the db on restart.
opportunity_service_svm
.clear_opportunities_upon_restart()
.await
.map_err(|e| anyhow!("Failed to clear opportunities on restart: {:?}", e))?;

tokio::join!(
async {
Expand Down
Loading