Skip to content

Commit 3844fec

Browse files
authored
Use network setting for max TTL extension of a contract. (#2254)
1 parent 197fcc0 commit 3844fec

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

  • cmd/soroban-cli/src/commands/contract

cmd/soroban-cli/src/commands/contract/extend.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::{
44
log::extract_events,
55
print::Print,
66
xdr::{
7-
Error as XdrError, ExtendFootprintTtlOp, ExtensionPoint, LedgerEntry, LedgerEntryChange,
8-
LedgerEntryData, LedgerFootprint, Limits, Memo, Operation, OperationBody, Preconditions,
7+
ConfigSettingEntry, ConfigSettingId, Error as XdrError, ExtendFootprintTtlOp,
8+
ExtensionPoint, LedgerEntry, LedgerEntryChange, LedgerEntryData, LedgerFootprint,
9+
LedgerKey, LedgerKeyConfigSetting, Limits, Memo, Operation, OperationBody, Preconditions,
910
SequenceNumber, SorobanResources, SorobanTransactionData, SorobanTransactionDataExt,
1011
Transaction, TransactionExt, TransactionMeta, TransactionMetaV3, TransactionMetaV4,
1112
TtlEntry, WriteXdr,
@@ -24,8 +25,6 @@ use crate::{
2425
key, rpc, wasm, Pwd,
2526
};
2627

27-
const MAX_LEDGERS_TO_EXTEND: u32 = 535_679;
28-
2928
#[derive(Parser, Debug, Clone)]
3029
#[group(skip)]
3130
pub struct Cmd {
@@ -92,6 +91,10 @@ pub enum Error {
9291
Locator(#[from] locator::Error),
9392
#[error(transparent)]
9493
IntError(#[from] TryFromIntError),
94+
#[error("Failed to fetch state archival settings from network")]
95+
StateArchivalSettingsNotFound,
96+
#[error("Ledgers to extend ({requested}) exceeds network maximum ({max})")]
97+
LedgersToExtendTooLarge { requested: u32, max: u32 },
9598
}
9699

97100
impl Cmd {
@@ -112,14 +115,41 @@ impl Cmd {
112115
Ok(())
113116
}
114117

115-
fn ledgers_to_extend(&self) -> u32 {
116-
let res = u32::min(self.ledgers_to_extend, MAX_LEDGERS_TO_EXTEND);
117-
if res < self.ledgers_to_extend {
118-
tracing::warn!(
119-
"Ledgers to extend is too large, using max value of {MAX_LEDGERS_TO_EXTEND}"
120-
);
118+
async fn get_max_entry_ttl(client: &rpc::Client) -> Result<u32, Error> {
119+
let key = LedgerKey::ConfigSetting(LedgerKeyConfigSetting {
120+
config_setting_id: ConfigSettingId::StateArchival,
121+
});
122+
123+
let entries = client.get_full_ledger_entries(&[key]).await?;
124+
125+
if let Some(entry) = entries.entries.first() {
126+
if let LedgerEntryData::ConfigSetting(ConfigSettingEntry::StateArchival(settings)) =
127+
&entry.val
128+
{
129+
return Ok(settings.max_entry_ttl);
130+
}
131+
}
132+
133+
Err(Error::StateArchivalSettingsNotFound)
134+
}
135+
136+
async fn ledgers_to_extend(&self, client: &rpc::Client) -> Result<u32, Error> {
137+
let max_entry_ttl = Self::get_max_entry_ttl(client).await?;
138+
139+
tracing::trace!(
140+
"Checking ledgers_to_extend: requested={}, max_entry_ttl={}",
141+
self.ledgers_to_extend,
142+
max_entry_ttl
143+
);
144+
145+
if self.ledgers_to_extend > max_entry_ttl {
146+
return Err(Error::LedgersToExtendTooLarge {
147+
requested: self.ledgers_to_extend,
148+
max: max_entry_ttl,
149+
});
121150
}
122-
res
151+
152+
Ok(self.ledgers_to_extend)
123153
}
124154
}
125155

@@ -141,7 +171,7 @@ impl NetworkRunnable for Cmd {
141171
let keys = self.key.parse_keys(&config.locator, &network)?;
142172
let client = network.rpc_client()?;
143173
let source_account = config.source_account().await?;
144-
let extend_to = self.ledgers_to_extend();
174+
let extend_to = self.ledgers_to_extend(&client).await?;
145175

146176
// Get the account sequence number
147177
let account_details = client

0 commit comments

Comments
 (0)