Skip to content

Commit

Permalink
Get CLI to compile and fix most TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Sep 1, 2023
1 parent f1cd7c6 commit 0e414da
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 55 deletions.
50 changes: 20 additions & 30 deletions cmd/soroban-cli/src/commands/contract/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::{

use clap::{command, Parser};
use soroban_env_host::xdr::{
BumpFootprintExpirationOp, ContractCodeEntry, ContractDataEntry, Error as XdrError,
ExtensionPoint, Hash, LedgerEntry, LedgerEntryChange, LedgerEntryData, LedgerFootprint,
LedgerKey, LedgerKeyContractCode, LedgerKeyContractData, Memo, MuxedAccount, Operation,
OperationBody, Preconditions, ReadXdr, ScAddress, ScSpecTypeDef, ScVal, SequenceNumber,
SorobanResources, SorobanTransactionData, Transaction, TransactionExt, TransactionMeta,
TransactionMetaV3, Uint256,
BumpFootprintExpirationOp, Error as XdrError, ExpirationEntry, ExtensionPoint, Hash,
LedgerEntry, LedgerEntryChange, LedgerEntryData, LedgerFootprint, LedgerKey,
LedgerKeyContractCode, LedgerKeyContractData, Memo, MuxedAccount, Operation, OperationBody,
Preconditions, ReadXdr, ScAddress, ScSpecTypeDef, ScVal, SequenceNumber, SorobanResources,
SorobanTransactionData, Transaction, TransactionExt, TransactionMeta, TransactionMetaV3,
Uint256,
};
use stellar_strkey::DecodeError;

Expand Down Expand Up @@ -195,16 +195,19 @@ impl Cmd {
return Err(Error::LedgerEntryNotFound);
}

// TODO: double-check that this match is correct
match (&operations[0].changes[0], &operations[0].changes[1]) {
(
LedgerEntryChange::State(_),
LedgerEntryChange::Updated(LedgerEntry {
data:
LedgerEntryData::ContractData(ContractDataEntry { .. })
| LedgerEntryData::ContractCode(ContractCodeEntry { .. }),
LedgerEntryData::Expiration(ExpirationEntry {
expiration_ledger_seq,
..
}),
..
}),
) => Ok(0), // TODO: How to get expiration ledger now?
) => Ok(*expiration_ledger_seq), // TODO: How to get expiration ledger now?
_ => Err(Error::LedgerEntryNotFound),
}
}
Expand All @@ -228,15 +231,15 @@ impl Cmd {
(
Box::new(new_k.clone()),
(
Box::new(if needle == new_k {
let (new_v, new_expiration) =
bump_entry(&new_v, self.ledgers_to_expire);
expiration_ledger_seq = Some(new_expiration);
new_v
Box::new(new_v),
if needle == new_k {
// It must have an expiration since it's a contract data entry
let old_expiration = v.1.unwrap();
expiration_ledger_seq = Some(old_expiration + self.ledgers_to_expire);
expiration_ledger_seq
} else {
new_v
}),
new_e,
new_e
},
),
)
})
Expand Down Expand Up @@ -286,19 +289,6 @@ impl Cmd {
}
}

fn bump_entry(v: &LedgerEntry, ledgers_to_expire: u32) -> (LedgerEntry, u32) {
let mut new_v = v.clone();
let mut new_expiration_ledger_seq = 0;
if let LedgerEntryData::ContractData(ref mut data) = new_v.data {
data.expiration_ledger_seq += ledgers_to_expire;
new_expiration_ledger_seq = data.expiration_ledger_seq;
} else if let LedgerEntryData::ContractCode(ref mut code) = new_v.data {
code.expiration_ledger_seq += ledgers_to_expire;
new_expiration_ledger_seq = code.expiration_ledger_seq;
}
(new_v, new_expiration_ledger_seq)
}

fn contract_id(s: &str) -> Result<[u8; 32], Error> {
utils::contract_id_from_str(s).map_err(|e| Error::CannotParseContractId(s.to_string(), e))
}
7 changes: 4 additions & 3 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{fmt::Debug, fs, io, rc::Rc};
use clap::{arg, command, value_parser, Parser};
use ed25519_dalek::Keypair;
use heck::ToKebabCase;
use soroban_env_host::e2e_invoke::get_ledger_changes;
use soroban_env_host::e2e_invoke::{get_ledger_changes, ExpirationEntryMap};
use soroban_env_host::xdr::ReadXdr;
use soroban_env_host::{
budget::Budget,
Expand Down Expand Up @@ -356,7 +356,7 @@ impl Cmd {
Box::new(source_account_ledger_key),
(
Box::new(default_account_ledger_entry(source_account.clone())),
None, // TODO: Should this be no expiry, or a value?
None,
),
));
}
Expand Down Expand Up @@ -423,7 +423,8 @@ impl Cmd {
crate::log::host_events(&events.0);
log_events(footprint, &[contract_auth.try_into()?], &[], Some(&budget));

let ledger_changes = get_ledger_changes(&budget, &storage, &state)?;
let ledger_changes =
get_ledger_changes(&budget, &storage, &state, ExpirationEntryMap::new())?;
let mut expiration_ledger_bumps: HashMap<LedgerKey, u32> = HashMap::new();
for ledger_entry_change in ledger_changes {
if let Some(exp_change) = ledger_entry_change.expiration_change {
Expand Down
27 changes: 15 additions & 12 deletions cmd/soroban-cli/src/commands/contract/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::{

use clap::{command, Parser};
use soroban_env_host::xdr::{
ContractCodeEntry, ContractDataDurability, ContractDataEntry, Error as XdrError,
ExtensionPoint, Hash, LedgerEntry, LedgerEntryChange, LedgerEntryData, LedgerFootprint,
LedgerKey, LedgerKeyContractCode, LedgerKeyContractData, Memo, MuxedAccount, Operation,
OperationBody, OperationMeta, Preconditions, ReadXdr, RestoreFootprintOp, ScAddress,
ScSpecTypeDef, ScVal, SequenceNumber, SorobanResources, SorobanTransactionData, Transaction,
TransactionExt, TransactionMeta, TransactionMetaV3, Uint256,
ContractDataDurability, Error as XdrError, ExpirationEntry, ExtensionPoint, Hash, LedgerEntry,
LedgerEntryChange, LedgerEntryData, LedgerFootprint, LedgerKey, LedgerKeyContractCode,
LedgerKeyContractData, Memo, MuxedAccount, Operation, OperationBody, OperationMeta,
Preconditions, ReadXdr, RestoreFootprintOp, ScAddress, ScSpecTypeDef, ScVal, SequenceNumber,
SorobanResources, SorobanTransactionData, Transaction, TransactionExt, TransactionMeta,
TransactionMetaV3, Uint256,
};
use stellar_strkey::DecodeError;

Expand Down Expand Up @@ -256,22 +256,25 @@ impl Cmd {
}
}

// TODO: Replace Option<()> with bool.
fn parse_operations(ops: &[OperationMeta]) -> Option<u32> {
ops.get(0).and_then(|op| {
op.changes.iter().find_map(|entry| match entry {
LedgerEntryChange::Updated(LedgerEntry {
data:
LedgerEntryData::ContractData(ContractDataEntry { .. })
| LedgerEntryData::ContractCode(ContractCodeEntry { .. }),
LedgerEntryData::Expiration(ExpirationEntry {
expiration_ledger_seq,
..
}),
..
})
| LedgerEntryChange::Created(LedgerEntry {
data:
LedgerEntryData::ContractData(ContractDataEntry { .. })
| LedgerEntryData::ContractCode(ContractCodeEntry { .. }),
LedgerEntryData::Expiration(ExpirationEntry {
expiration_ledger_seq,
..
}),
..
}) => Some(0), // TODO: How to get expiration now?
}) => Some(*expiration_ledger_seq),
_ => None,
})
})
Expand Down
22 changes: 12 additions & 10 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn add_contract_code_to_ledger_entries(
};
for (k, e) in &mut *entries {
if **k == code_key {
**e = code_entry;
*e = (Box::new(code_entry), Some(min_persistent_entry_expiration));
return Ok(hash);
}
}
Expand Down Expand Up @@ -121,7 +121,10 @@ pub fn add_contract_to_ledger_entries(
};
for (k, e) in &mut *entries {
if **k == contract_key {
**e = contract_entry;
*e = (
Box::new(contract_entry),
Some(min_persistent_entry_expiration),
);
return;
}
}
Expand All @@ -135,15 +138,12 @@ pub fn add_contract_to_ledger_entries(
}

pub fn bump_ledger_entry_expirations<S: BuildHasher>(
entries: &mut [(Box<LedgerKey>, Box<LedgerEntry>)],
entries: &mut Vec<(Box<LedgerKey>, (Box<LedgerEntry>, Option<u32>))>,
lookup: &HashMap<LedgerKey, u32, S>,
) {
for (k, e) in &mut *entries {
for (k, (_, expiration)) in &mut *entries {
if let Some(min_expiration) = lookup.get(k.as_ref()) {
if let LedgerEntryData::ContractData(entry) = &mut e.data {
// TODO: How to update the expiration entry?
// entry.expiration_ledger_seq = *min_expiration;
}
*expiration = Some(*min_expiration);
}
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn contract_id_from_str(contract_id: &str) -> Result<[u8; 32], stellar_strke
/// Might return an error
pub fn get_contract_spec_from_storage(
storage: &mut Storage,
current_ledger_seq: &u32,
_current_ledger_seq: &u32,
contract_id: [u8; 32],
) -> Result<Vec<ScSpecEntry>, FromWasmError> {
let key = LedgerKey::ContractData(LedgerKeyContractData {
Expand All @@ -221,7 +221,9 @@ pub fn get_contract_spec_from_storage(
..
} => match executable {
ContractExecutable::Token => {
// TODO: How to identify that entry is expired now?
// TODO: How to identify that an entry is expired now?
// I(fons) don't think that the storage contains expiration entries and I don't
// think we can get it from the state in all case (e.g. Token contracts)
// if expiration_ledger_seq <= current_ledger_seq {
// return Err(FromWasmError::NotFound);
// }
Expand Down

0 comments on commit 0e414da

Please sign in to comment.