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

all: add support for new state expiration ledger entries #913

Merged
merged 24 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/soroban-rpc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
env:
SOROBAN_RPC_INTEGRATION_TESTS_ENABLED: true
SOROBAN_RPC_INTEGRATION_TESTS_CAPTIVE_CORE_BIN: /usr/bin/stellar-core
PROTOCOL_20_CORE_DEBIAN_PKG_VERSION: 19.13.1-1458.431e4e324.focal~soroban~settings~override
PROTOCOL_20_CORE_DEBIAN_PKG_VERSION: 19.13.1-1462.22b9bb384.focal~vnext
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ version = "0.9.4"
[workspace.dependencies.soroban-env-host]
version = "0.0.17"
git = "https://github.com/stellar/rs-soroban-env"
rev = "ee3896617f5eeb06d6346e2fbf70cb44d0823b27"
rev = "eb5a9ba053a7b64a8eff605db625525378f7bea0"

[workspace.dependencies.soroban-spec]
version = "0.9.1"
git = "https://github.com/sisuresh/rs-soroban-sdk"
rev = "e15e552974a1070fc48027384bdee9ae9f539943"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "18b8fa1a358aa84afd196e2f6d44942798c8f335"
# path = "../rs-soroban-sdk/soroban-spec"

[workspace.dependencies.soroban-spec-rust]
version = "0.9.1"
git = "https://github.com/sisuresh/rs-soroban-sdk"
rev = "e15e552974a1070fc48027384bdee9ae9f539943"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "18b8fa1a358aa84afd196e2f6d44942798c8f335"
# path = "../rs-soroban-sdk/soroban-spec-rust"

[workspace.dependencies.soroban-spec-json]
Expand All @@ -44,13 +44,13 @@ path = "./cmd/crates/soroban-spec-tools"

[workspace.dependencies.soroban-sdk]
version = "0.9.1"
git = "https://github.com/sisuresh/rs-soroban-sdk"
rev = "e15e552974a1070fc48027384bdee9ae9f539943"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "18b8fa1a358aa84afd196e2f6d44942798c8f335"

[workspace.dependencies.soroban-ledger-snapshot]
version = "0.9.1"
git = "https://github.com/sisuresh/rs-soroban-sdk"
rev = "e15e552974a1070fc48027384bdee9ae9f539943"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "18b8fa1a358aa84afd196e2f6d44942798c8f335"

[workspace.dependencies.soroban-cli]
version = "0.9.4"
Expand All @@ -59,7 +59,7 @@ path = "cmd/soroban-cli"
[workspace.dependencies.stellar-xdr]
version = "0.0.17"
git = "https://github.com/stellar/rs-stellar-xdr"
rev = "e2a9cbf72d94941de1bde6ba34a38e1f49328567"
rev = "39904e09941046dab61e6e35fc89e31bf2dea1cd"
default-features = false

[workspace.dependencies]
Expand Down
24 changes: 24 additions & 0 deletions cmd/crates/soroban-test/tests/it/invoke_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ fn invoke_auth() {
.assert()
.stdout(format!("\"{DEFAULT_PUB_KEY}\"\n"))
.success();

// Invoke it again without providing the contract, to exercise the deployment
sandbox
.new_assert_cmd("contract")
.arg("invoke")
.arg("--id=1")
.arg("--")
.arg("auth")
.arg(&format!("--addr={DEFAULT_PUB_KEY}"))
.arg("--world=world")
.assert()
.stdout(format!("\"{DEFAULT_PUB_KEY}\"\n"))
.success();
}

#[tokio::test]
Expand Down Expand Up @@ -290,6 +303,17 @@ fn invoke_with_source(sandbox: &TestEnv, source: &str) {
"--world=world",
]);
assert_eq!(cmd.unwrap(), "[\"Hello\",\"world\"]");

// Invoke it again without providing the contract, to exercise the deployment
let cmd = sandbox.invoke(&[
"--source-account",
source,
"--id=1",
"--",
"hello",
"--world=world",
]);
assert_eq!(cmd.unwrap(), "[\"Hello\",\"world\"]");
}

#[test]
Expand Down
55 changes: 21 additions & 34 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, ContractEntryBodyType,
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,13 @@ 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 {
expiration_ledger_seq,
..
})
| LedgerEntryData::ContractCode(ContractCodeEntry {
LedgerEntryData::Expiration(ExpirationEntry {
expiration_ledger_seq,
..
}),
Expand All @@ -229,16 +226,21 @@ impl Cmd {
.iter()
.map(|(k, v)| {
let new_k = k.as_ref().clone();
let new_v = v.as_ref().clone();
let new_v = v.0.as_ref().clone();
let new_e = v.1;
(
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
} else {
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_e
},
),
)
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -273,7 +275,6 @@ impl Cmd {
utils::contract_id_from_str(wasm_hash)
.map_err(|e| Error::CannotParseContractId(wasm_hash.clone(), e))?,
),
body_type: ContractEntryBodyType::DataEntry,
}));
} else {
ScVal::LedgerKeyContractInstance
Expand All @@ -283,25 +284,11 @@ impl Cmd {
Ok(LedgerKey::ContractData(LedgerKeyContractData {
contract: ScAddress::Contract(Hash(contract_id)),
durability: self.durability.into(),
body_type: ContractEntryBodyType::DataEntry,
key,
}))
}
}

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))
}
23 changes: 5 additions & 18 deletions cmd/soroban-cli/src/commands/contract/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use soroban_env_host::{
budget::Budget,
storage::Storage,
xdr::{
self, ContractCodeEntry, ContractCodeEntryBody, ContractDataDurability, ContractDataEntry,
ContractDataEntryBody, ContractDataEntryData, ContractEntryBodyType, ContractExecutable,
self, ContractCodeEntry, ContractDataDurability, ContractDataEntry, ContractExecutable,
Error as XdrError, LedgerEntryData, LedgerKey, LedgerKeyContractCode,
LedgerKeyContractData, ScAddress, ScContractInstance, ScVal,
},
Expand Down Expand Up @@ -171,38 +170,26 @@ pub fn get_contract_wasm_from_storage(
contract: ScAddress::Contract(contract_id.into()),
key: ScVal::LedgerKeyContractInstance,
durability: ContractDataDurability::Persistent,
body_type: ContractEntryBodyType::DataEntry,
});
match storage.get(&key.into(), &Budget::default()) {
Ok(rc) => match rc.as_ref() {
xdr::LedgerEntry {
data:
LedgerEntryData::ContractData(ContractDataEntry {
body:
ContractDataEntryBody::DataEntry(ContractDataEntryData {
val: ScVal::ContractInstance(ScContractInstance { executable, .. }),
..
}),
val: ScVal::ContractInstance(ScContractInstance { executable, .. }),
..
}),
..
} => match executable {
ContractExecutable::Wasm(hash) => {
if let Ok(rc) = storage.get(
&LedgerKey::ContractCode(LedgerKeyContractCode {
hash: hash.clone(),
body_type: ContractEntryBodyType::DataEntry,
})
.into(),
&LedgerKey::ContractCode(LedgerKeyContractCode { hash: hash.clone() })
.into(),
&Budget::default(),
) {
match rc.as_ref() {
xdr::LedgerEntry {
data:
LedgerEntryData::ContractCode(ContractCodeEntry {
body: ContractCodeEntryBody::DataEntry(code),
..
}),
data: LedgerEntryData::ContractCode(ContractCodeEntry { code, .. }),
..
} => Ok(code.to_vec()),
_ => Err(FromWasmError::NotFound),
Expand Down
14 changes: 9 additions & 5 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 @@ -360,16 +360,19 @@ impl Cmd {
{
state.ledger_entries.push((
Box::new(source_account_ledger_key),
Box::new(default_account_ledger_entry(source_account.clone())),
(
Box::new(default_account_ledger_entry(source_account.clone())),
None,
),
));
}

let snap = Rc::new(state.clone());
let mut storage = Storage::with_recording_footprint(snap);
let storage = Storage::with_recording_footprint(snap);
let spec_entries = if let Some(spec) = self.spec_entries()? {
spec
} else {
utils::get_contract_spec_from_storage(&mut storage, &state.sequence_number, contract_id)
utils::get_contract_spec_from_state(&state, contract_id)
.map_err(Error::CannotParseContractSpec)?
};
let budget = Budget::default();
Expand Down Expand Up @@ -430,7 +433,8 @@ impl Cmd {
log_budget(&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
Loading