Skip to content

Commit

Permalink
Feat: min slot bid svm (#291)
Browse files Browse the repository at this point in the history
add optional parameter for svm bids to sepcify the minimum slot where the bid is applicable.
If simulation fails for any reason and the simulation context has a lower slot compared to the bid, we will retry hoping that the RPC will catch up soon.
  • Loading branch information
m30m authored Dec 19, 2024
1 parent 28a69c9 commit 20375fa
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 22 deletions.
5 changes: 5 additions & 0 deletions auction-server/api-types/src/bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use {
DisplayFromStr,
},
solana_sdk::{
clock::Slot,
signature::Signature,
transaction::VersionedTransaction,
},
Expand Down Expand Up @@ -258,6 +259,10 @@ pub struct BidCreateSvm {
#[schema(example = "SGVsbG8sIFdvcmxkIQ==", value_type = String)]
#[serde(with = "crate::serde::transaction_svm")]
pub transaction: VersionedTransaction,
/// The minimum slot required for the bid to be executed successfully
/// None if the bid can be executed at any recent slot
#[schema(example = 293106477, value_type = Option<u64>)]
pub slot: Option<Slot>,
}

#[derive(Serialize, Deserialize, ToSchema, Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions auction-server/src/auction/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ impl ApiTrait<Svm> for Svm {
initiation_time: OffsetDateTime::now_utc(),
chain_data: entities::BidChainDataCreateSvm {
transaction: bid_create_svm.transaction.clone(),
slot: bid_create_svm.slot,
},
})
}
Expand Down
2 changes: 2 additions & 0 deletions auction-server/src/auction/entities/bid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use {
},
express_relay_api_types::bid as api,
solana_sdk::{
clock::Slot,
pubkey::Pubkey,
signature::Signature,
transaction::VersionedTransaction,
Expand Down Expand Up @@ -239,6 +240,7 @@ pub struct BidCreate<T: ChainTrait> {
#[derive(Clone, Debug)]
pub struct BidChainDataCreateSvm {
pub transaction: VersionedTransaction,
pub slot: Option<Slot>,
}

#[derive(Clone, Debug)]
Expand Down
13 changes: 10 additions & 3 deletions auction-server/src/auction/service/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
client_error,
rpc_response::{
Response,
RpcResponseContext,
RpcResult,
},
},
Expand Down Expand Up @@ -166,12 +167,14 @@ impl Simulator {
Ok(result.value)
}

/// Fetches multiple accounts from the RPC in chunks
/// There is no guarantee that all the accounts will be fetched with the same slot
async fn get_multiple_accounts_chunked(
&self,
keys: &[Pubkey],
) -> RpcResult<Vec<Option<Account>>> {
let mut result = vec![];
let mut last_context = None;
let mut context_with_min_slot: Option<RpcResponseContext> = None;
const MAX_RPC_ACCOUNT_LIMIT: usize = 100;
// Ensure at least one call is made, even if keys is empty
let key_chunks = if keys.is_empty() {
Expand All @@ -189,11 +192,15 @@ impl Simulator {
for chunk_result in chunk_results {
let chunk_result = chunk_result?;
result.extend(chunk_result.value);
last_context = Some(chunk_result.context);
if context_with_min_slot.is_none()
|| context_with_min_slot.as_ref().unwrap().slot > chunk_result.context.slot
{
context_with_min_slot = Some(chunk_result.context);
}
}
Ok(Response {
value: result,
context: last_context.unwrap(), // Safe because we ensured at least one call was made
context: context_with_min_slot.unwrap(), // Safe because we ensured at least one call was made
})
}

Expand Down
70 changes: 52 additions & 18 deletions auction-server/src/auction/service/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ use {
U256,
},
},
litesvm::types::FailedTransactionMetadata,
solana_sdk::{
address_lookup_table::state::AddressLookupTable,
clock::Slot,
commitment_config::CommitmentConfig,
compute_budget,
instruction::CompiledInstruction,
Expand Down Expand Up @@ -578,25 +580,57 @@ impl Service<Svm> {
}

pub async fn simulate_bid(&self, bid: &entities::BidCreate<Svm>) -> Result<(), RestError> {
let response = self
.config
.chain_config
.simulator
.simulate_transaction(&bid.chain_data.transaction)
.await;
let result = response.map_err(|e| {
tracing::error!("Error while simulating bid: {:?}", e);
RestError::TemporarilyUnavailable
})?;
match result.value {
Err(err) => {
let msgs = err.meta.logs;
Err(RestError::SimulationError {
result: Default::default(),
reason: msgs.join("\n"),
})
const RETRY_LIMIT: usize = 5;
const RETRY_DELAY: Duration = Duration::from_millis(100);
let mut retry_count = 0;
let bid_slot = bid.chain_data.slot.unwrap_or_default();

let should_retry = |result_slot: Slot,
retry_count: usize,
err: &FailedTransactionMetadata|
-> bool {
if result_slot < bid_slot && retry_count < RETRY_LIMIT {
tracing::warn!(
"Simulation failed with stale slot. Simulation slot: {}, Bid Slot: {}, Retry count: {}, Error: {:?}",
result_slot,
bid_slot,
retry_count,
err
);
true
} else {
false
}
Ok(_) => Ok(()),
};

loop {
let response = self
.config
.chain_config
.simulator
.simulate_transaction(&bid.chain_data.transaction)
.await;
let result = response.map_err(|e| {
tracing::error!("Error while simulating bid: {:?}", e);
RestError::TemporarilyUnavailable
})?;
return match result.value {
Err(err) => {
if should_retry(result.context.slot, retry_count, &err) {
tokio::time::sleep(RETRY_DELAY).await;
retry_count += 1;
continue;
}
let msgs = err.meta.logs;
Err(RestError::SimulationError {
result: Default::default(),
reason: msgs.join("\n"),
})
}
// Not important to check if bid slot is less than simulation slot if simulation is successful
// since we want to fix incorrect verifications due to stale slot
Ok(_) => Ok(()),
};
}
}

Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/examples/simpleSearcherLimo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class SimpleSearcherLimo {
config.relayerSigner,
config.feeReceiverRelayer
);
bid.slot = opportunity.slot;

bid.transaction.recentBlockhash =
this.latestChainUpdate[this.chainId].blockhash;
Expand Down
1 change: 1 addition & 0 deletions sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export class Client {

return {
chain_id: bid.chainId,
slot: bid.slot,
transaction: bid.transaction
.serialize({ requireAllSignatures: false })
.toString("base64"),
Expand Down
7 changes: 7 additions & 0 deletions sdk/js/src/serverTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export interface components {
* @example solana
*/
chain_id: string;
/**
* Format: int64
* @description The minimum slot required for the bid to be executed successfully
* None if the bid can be executed at any recent slot
* @example 293106477
*/
slot?: number | null;
/**
* @description The transaction for bid.
* @example SGVsbG8sIFdvcmxkIQ==
Expand Down
6 changes: 6 additions & 0 deletions sdk/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ export type BidSvm = {
* @example solana
*/
chainId: ChainId;
/**
* @description The minimum slot required for the bid to be executed successfully
* None if the bid can be executed at any recent slot
* @example 293106477
*/
slot?: number | null;
/**
* @description The execution environment for the bid.
*/
Expand Down
3 changes: 3 additions & 0 deletions sdk/python/express_relay/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ class PostBidMessageParamsSvm(BaseModel):
method: A string literal "post_bid".
chain_id: The chain ID to bid on.
transaction: The transaction including the bid.
slot: The minimum slot required for the bid to be executed successfully
None if the bid can be executed at any recent slot
"""

method: Literal["post_bid"]
chain_id: str
transaction: SvmTransaction
slot: int | None


def get_discriminator_value(v: Any) -> str:
Expand Down
3 changes: 3 additions & 0 deletions sdk/python/express_relay/models/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ class BidSvm(BaseModel):
Attributes:
transaction: The transaction including the bid
chain_id: The chain ID to bid on.
slot: The minimum slot required for the bid to be executed successfully
None if the bid can be executed at any recent slot
"""

transaction: SvmTransaction
chain_id: str
slot: int | None


class _OrderPydanticAnnotation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def generate_bid(self, opp: OpportunitySvm) -> BidSvm:
transaction.partial_sign(
[self.private_key], recent_blockhash=latest_chain_update.blockhash
)
bid = BidSvm(transaction=transaction, chain_id=self.chain_id)
bid = BidSvm(transaction=transaction, chain_id=self.chain_id, slot=opp.slot)
return bid

async def generate_take_order_ixs(
Expand Down

0 comments on commit 20375fa

Please sign in to comment.