diff --git a/crates/core/src/scenarios/README.md b/crates/core/src/scenarios/README.md index 02fb64fde..41686112d 100644 --- a/crates/core/src/scenarios/README.md +++ b/crates/core/src/scenarios/README.md @@ -137,6 +137,13 @@ never takes effect. An account not yet in the surfnet is fetched from the remote or its owner with its owned accounts, is marked offline. Keep-alive is kept in memory only: after a restart on a persistent database it stops until an importing override plays again. +An override can turn keep-alive off for the account it writes with `"keepAlive": []`, as `keep_alive: []` +does for a template. From that override's slot on, nothing rewrites the account, including keep-alive +that earlier overrides started, so its fields age as the clock moves on, for example to test a stale +price. The override must write a value for this to take effect. Without the field, the template's +keep-alive applies as usual. To keep fresh an account a transaction reads but no scenario names, give +it an override of its own. + ### Override Templates Directly using the `surfnet_registerScenario` endpoint requires building out a map of account keys that are specific to the schema of the account that is being written to. This is a cumbersome process in most cases. diff --git a/crates/core/src/scenarios/keep_alive.rs b/crates/core/src/scenarios/keep_alive.rs index 9ca123dac..470def75b 100644 --- a/crates/core/src/scenarios/keep_alive.rs +++ b/crates/core/src/scenarios/keep_alive.rs @@ -15,6 +15,8 @@ pub struct KeepAliveSource { pub address: Pubkey, /// The override's values, which input and PDA addresses resolve against. pub values: HashMap, + /// The override set `keepAlive: []`: keep-alive stops rewriting `address`. + pub off: bool, } /// Binds a template's keep-alive import to the account it rewrites every slot, resolved when an diff --git a/crates/core/src/surfnet/svm.rs b/crates/core/src/surfnet/svm.rs index aa3fcd7e8..683a176e2 100644 --- a/crates/core/src/surfnet/svm.rs +++ b/crates/core/src/surfnet/svm.rs @@ -3552,15 +3552,22 @@ impl SurfnetSvm { // Joins keep-alive only once its write succeeds: a failed override leaves the account as // if no scenario had touched it. + let keep_alive_off = override_instance + .keep_alive + .as_ref() + .is_some_and(Vec::is_empty); let mut keep_alive_source = None; if scheduled - && let Some(template) = template_registry().get(&override_instance.template_id) - && !template.keep_alive.is_empty() + && (keep_alive_off + || template_registry() + .get(&override_instance.template_id) + .is_some_and(|template| !template.keep_alive.is_empty())) { keep_alive_source = Some(KeepAliveSource { - template: template.id.clone(), + template: override_instance.template_id.clone(), address: account_pubkey, values: override_instance.values.clone(), + off: keep_alive_off, }); } @@ -3941,6 +3948,12 @@ impl SurfnetSvm { /// Adds the accounts that `touched` makes keep-alive imports rewrite. fn enroll_keep_alive(&mut self, touched: Vec) { for source in touched { + if source.off { + self.keep_alive_bindings + .retain(|binding| binding.address != source.address); + info!("Keep-alive stops rewriting {}", source.address); + continue; + } let Some(template) = template_registry().get(&source.template) else { continue; }; @@ -9796,4 +9809,45 @@ mod tests { .expect("the override must notify the account's subscribers"); assert_eq!(update.data.decode(), Some(account.data)); } + + #[tokio::test] + async fn an_empty_keep_alive_list_stops_keep_alive_on_the_account_it_writes() { + let (mut svm, _simnet_events_rx, _geyser_events_rx) = SurfnetSvm::default(); + let (account_pubkey, instance) = scheduled_override_fixture(&mut svm); + let other = Pubkey::new_unique(); + for address in [account_pubkey, other] { + svm.keep_alive_bindings.insert(KeepAliveBinding { + template: "an-earlier-override".to_string(), + import: 0, + address, + }); + } + let mut play = async |slot: u64, keep_alive: serde_json::Value, values| { + let mut instance = instance.clone().with_values(values); + instance.keep_alive = serde_json::from_value(keep_alive).unwrap(); + svm.scheduled_overrides.store(slot, vec![instance]).unwrap(); + svm.materialize_overrides_for_slot(&None, slot) + .await + .unwrap(); + svm.keep_alive_bindings + .iter() + .map(|binding| binding.address) + .collect::>() + }; + let mut both = vec![account_pubkey, other]; + both.sort(); + + let failed = HashMap::from([("nope".to_string(), serde_json::json!(1))]); + assert_eq!(play(500, serde_json::json!([]), failed).await, both); + assert_eq!( + play(501, serde_json::json!([{}]), instance.values.clone()).await, + both, + "a failed write and a non-empty list stop nothing" + ); + assert_eq!( + play(502, serde_json::json!([]), instance.values.clone()).await, + vec![other], + "only the written account stops" + ); + } } diff --git a/crates/sdk-node/surfpool-sdk/kit/generated/OverrideInstance.ts b/crates/sdk-node/surfpool-sdk/kit/generated/OverrideInstance.ts index 0bf5926b1..d7d9cb87c 100644 --- a/crates/sdk-node/surfpool-sdk/kit/generated/OverrideInstance.ts +++ b/crates/sdk-node/surfpool-sdk/kit/generated/OverrideInstance.ts @@ -36,6 +36,11 @@ enabled: boolean, * Whether to fetch fresh account data just before transaction execution */ fetchBeforeUse?: boolean, +/** + * Keep-alive for the account this override writes. Absent, the template's keep-alive applies; + * `[]` turns it off. Only `[]` is supported. + */ +keepAlive?: Array, /** * Account address to override - use pubkey for known addresses or pda for derived addresses */ diff --git a/crates/types/src/rpc_endpoints.json b/crates/types/src/rpc_endpoints.json index bc5b02991..58ea59acc 100644 --- a/crates/types/src/rpc_endpoints.json +++ b/crates/types/src/rpc_endpoints.json @@ -861,6 +861,7 @@ "label": "Option (An optional label for this override instance)", "enabled": "bool (Indicates whether this override instance is enabled)", "fetchBeforeUse": "bool (Indicates whether to fetch the latest on-chain account data before applying overrides)", + "keepAlive": "Option<[]> (Optional. Omit it to keep the template's keep-alive. [] turns keep-alive off for the account this override writes, from this slot on, including keep-alive that earlier overrides started. Only [] is supported)", "account": "AccountAddress (The account this override targets, as {\"pubkey\": \"\"} or {\"pda\": {\"programId\": \"\", \"seeds\": [ ... ]}})" } } diff --git a/crates/types/src/scenarios.rs b/crates/types/src/scenarios.rs index 9a2207cd6..50d3782a7 100644 --- a/crates/types/src/scenarios.rs +++ b/crates/types/src/scenarios.rs @@ -592,6 +592,14 @@ pub struct OverrideInstance { #[serde(default)] #[cfg_attr(feature = "ts-bindings", ts(as = "Option", optional))] pub fetch_before_use: bool, + /// Keep-alive for the account this override writes. Absent, the template's keep-alive applies; + /// `[]` turns it off. Only `[]` is supported. + #[schemars( + description = "Optional. Omit it to keep the template's keep-alive. [] turns keep-alive off for the account this override writes, from this slot on, including keep-alive that earlier overrides started. Only [] is supported" + )] + #[serde(default, skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "ts-bindings", ts(type = "Array", optional))] + pub keep_alive: Option>, /// Account address to override - use pubkey for known addresses or pda for derived addresses #[schemars( description = "Account address: either {\"pubkey\": \"base58_address\"} or {\"pda\": {\"programId\": \"...\", \"seeds\": [...]}}" @@ -609,6 +617,7 @@ impl OverrideInstance { label: None, enabled: true, fetch_before_use: false, + keep_alive: None, account, } } @@ -2664,4 +2673,31 @@ templates: ); } } + + #[test] + fn keep_alive_is_absent_unless_given() { + let instance = |keep_alive: Option| { + let mut value = json!({ + "id": "o", "templateId": "t", "values": {}, "scenarioRelativeSlot": 0, + "enabled": true, "account": { "pubkey": "11111111111111111111111111111111" } + }); + if let Some(keep_alive) = keep_alive { + value["keepAlive"] = keep_alive; + } + serde_json::from_value::(value).unwrap() + }; + + let absent = instance(None); + assert_eq!(absent.keep_alive, None); + assert!( + serde_json::to_value(&absent) + .unwrap() + .get("keepAlive") + .is_none() + ); + + let off = instance(Some(json!([]))); + assert_eq!(off.keep_alive, Some(vec![])); + assert_eq!(serde_json::to_value(&off).unwrap()["keepAlive"], json!([])); + } }