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

Feat: min slot bid svm #291

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions auction-server/src/auction/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use {
DisplayFromStr,
},
solana_sdk::{
clock::Slot,
hash::Hash,
signature::Signature,
transaction::VersionedTransaction,
Expand Down Expand Up @@ -315,6 +316,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 Expand Up @@ -725,6 +730,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 @@ -27,6 +27,7 @@ use {
U256,
},
solana_sdk::{
clock::Slot,
pubkey::Pubkey,
signature::Signature,
transaction::VersionedTransaction,
Expand Down Expand Up @@ -241,6 +242,7 @@ pub struct BidCreate<T: ChainTrait> {
#[derive(Clone, Debug)]
pub struct BidChainDataCreateSvm {
pub transaction: VersionedTransaction,
pub slot: Option<Slot>,
}

#[derive(Clone, Debug)]
Expand Down
11 changes: 8 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 @@ -172,7 +173,7 @@ impl Simulator {
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 @@ -190,11 +191,15 @@ impl Simulator {
for chunk_result in chunk_results {
let chunk_result = chunk_result?;
result.extend(chunk_result.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a note this could lead to accounts drawn from different slots, there may be some issues with that with regard to mistaken simulation results? it's good to use the min slot below, but there may still be issues resulting from the different results having different context slots, e.g. dex router tx simulates successfully when accounts are all synced, but may fail simulation when they are drawn at different context slots

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, this can become problematic with AMM accounts. I added a comment

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
56 changes: 37 additions & 19 deletions auction-server/src/auction/service/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,25 +578,43 @@ impl Service<Svm> {
}

pub async fn simulate_bid(&self, bid: &entities::BidCreate<Svm>) -> Result<(), RestError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this function recursively will make the code more readable (instead of having loop)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered that but that was also a bit weird. Refactored a bit to make it more readable

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"),
})
}
Ok(_) => Ok(()),
const RETRY_LIMIT: usize = 5;
let mut retry_count = 0;
loop {
let response = self
.config
.chain_config
.simulator
.simulate_transaction(&bid.chain_data.transaction)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the retry should happen within the fetching? before the simulator actually runs? otherwise it's a weird interface where we're asking for a minimum slot to simulate against and then ignoring that as long as it passes simulation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also add some checks to make sure searchers aren't submitting slots that are more than some reasonable number off of where the rpc currently stands

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this some internal behaviour which is subject to change. Interface only adds the option for a searcher to guarantee successful simulation from the specified slot. How we verify this internally is not part of the interface.

I classify that as a DoS attack and don't think it's a priority

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case, maybe it's worth a comment baking that into the interface on the SDK side, or in the BidChainDataCreateSvm object in auction-server/src/auction/entities/bid.rs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a comment already there. do you think we should explain more?

.await;
let result = response.map_err(|e| {
tracing::error!("Error while simulating bid: {:?}", e);
RestError::TemporarilyUnavailable
})?;
return match result.value {
Err(err) => {
if result.context.slot < bid.chain_data.slot.unwrap_or_default()
&& retry_count < RETRY_LIMIT
{
retry_count += 1;
tracing::warn!(
"Simulation failed with stale slot. Simulation slot: {}, Bid Slot: {} Retry count: {}, Error: {:?}",
result.context.slot,
bid.chain_data.slot.unwrap_or_default(),
retry_count,
err
);
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
}
let msgs = err.meta.logs;
Err(RestError::SimulationError {
result: Default::default(),
reason: msgs.join("\n"),
})
}
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 @@ -547,6 +547,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
Loading