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

Read and write xdr to file for persistent storage #24

Merged
merged 10 commits into from
Jul 8, 2022
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
74 changes: 38 additions & 36 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
edition = "2021"

[dependencies]
stellar-contract-env-host = { git = "https://github.com/stellar/rs-stellar-contract-env", rev = "42fc83ac", features = ["vm"] }
stellar-contract-env-host = { git = "https://github.com/stellar/rs-stellar-contract-env", rev = "9919061", features = ["vm"] }
# stellar-contract-env-host = { path = "../rs-stellar-contract-env/stellar-contract-env-host", features = ["vm"] }
clap = { version = "3.1.18", features = ["derive", "env"] }
base64 = "0.13.0"
Expand Down
109 changes: 107 additions & 2 deletions src/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::{fmt::Debug, fs, io};
use std::{
fmt::Debug,
fs::{self},
io,
rc::Rc,
};

use clap::Parser;
use stellar_contract_env_host::{
storage::Storage,
xdr::{Error as XdrError, ScVal, ScVec},
Host, HostError, Vm,
};
Expand All @@ -12,6 +18,8 @@ use crate::strval::{self, StrValError};
pub struct Invoke {
#[clap(long, parse(from_os_str))]
file: std::path::PathBuf,
#[clap(long, parse(from_os_str), default_value("ledger.xdr"))]
snapshot_file: std::path::PathBuf,
#[clap(long = "fn")]
function: String,
#[clap(long = "arg", multiple_occurrences = true)]
Expand All @@ -30,10 +38,101 @@ pub enum Error {
Host(#[from] HostError),
}

pub mod snapshot {
use std::fs::File;

use super::{Error, HostError};
use stellar_contract_env_host::{
im_rc::OrdMap,
storage::SnapshotSource,
xdr::{LedgerEntry, LedgerKey, ReadXdr, WriteXdr},
};

pub struct Snap {
pub ledger_entries: OrdMap<LedgerKey, LedgerEntry>,
}

impl SnapshotSource for Snap {
fn get(&self, key: &LedgerKey) -> Result<LedgerEntry, HostError> {
match self.ledger_entries.get(key) {
Some(v) => Ok(v.clone()),
None => Err(HostError::General("missing entry")),
}
}
fn has(&self, key: &LedgerKey) -> Result<bool, HostError> {
Ok(self.ledger_entries.contains_key(key))
}
}

// snapshot_file format is the LedgerKey followed by the
// corresponding LedgerEntry, all in xdr.
// Ex.
// LedgerKey1LedgerEntry1LedgerKey2LedgerEntry2
// ...

pub fn read(input_file: &std::path::PathBuf) -> Result<OrdMap<LedgerKey, LedgerEntry>, Error> {
let mut res = OrdMap::new();

let mut file = match File::open(input_file) {
Ok(f) => f,
Err(e) => {
//File doesn't exist, so treat this as an empty database and the file will be created later
if e.kind() == std::io::ErrorKind::NotFound {
return Ok(res);
}
return Err(Error::Io(e));
}
};

while let (Ok(lk), Ok(le)) = (
LedgerKey::read_xdr(&mut file),
LedgerEntry::read_xdr(&mut file),
) {
res.insert(lk, le);
}

Ok(res)
}

pub fn commit(
mut new_state: OrdMap<LedgerKey, LedgerEntry>,
storage_map: &OrdMap<LedgerKey, Option<LedgerEntry>>,
output_file: &std::path::PathBuf,
) -> Result<(), Error> {
//Need to start off with the existing snapshot (new_state) since it's possible the storage_map did not touch every existing entry
let mut file = File::create(output_file)?;
for (lk, ole) in storage_map {
if let Some(le) = ole {
new_state.insert(lk.clone(), le.clone());
} else {
new_state.remove(lk);
}
}

for (lk, le) in new_state {
lk.write_xdr(&mut file)?;
le.write_xdr(&mut file)?;
}

Ok(())
}
}

impl Invoke {
pub fn run(&self) -> Result<(), Error> {
let contents = fs::read(&self.file).unwrap();
let h = Host::default();

// Initialize storage and host
// TODO: allow option to separate input and output file
Copy link
Member

Choose a reason for hiding this comment

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

Nice idea.

let ledger_entries = snapshot::read(&self.snapshot_file)?;
let snap = Rc::new(snapshot::Snap {
ledger_entries: ledger_entries.clone(),
});
let storage = Storage::with_recording_footprint(snap);

let h = Host::with_storage(storage);

//TODO: contractID should be user specified
let vm = Vm::new(&h, [0; 32].into(), &contents).unwrap();
let args = self
.args
Expand All @@ -43,6 +142,12 @@ impl Invoke {
let res = vm.invoke_function(&h, &self.function, &ScVec(args.try_into()?))?;
let res_str = strval::to_string(&h, res);
println!("{}", res_str);

let storage = h
.recover_storage()
.map_err(|_h| HostError::General("could not get storage from host"))?;

snapshot::commit(ledger_entries, &storage.map, &self.snapshot_file)?;
Ok(())
}
}
12 changes: 10 additions & 2 deletions src/strval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub fn from_string(_h: &Host, s: &str) -> Result<ScVal, StrValError> {
}
}
"u64" => ScVal::Object(Some(ScObject::U64(val.parse()?))),
"sym" => ScVal::Symbol(
val.as_bytes()
.try_into()
.map_err(|_| StrValError::InvalidValue)?,
),
_ => return Err(StrValError::UnknownType),
};
Ok(val)
Expand All @@ -76,8 +81,11 @@ pub fn to_string(_h: &Host, v: ScVal) -> String {
ScVal::I32(v) => format!("i32:{}", v),
ScVal::U32(v) => format!("u32:{}", v),
ScVal::U63(v) => format!("i64:{}", v),
ScVal::Static(_) => todo!(),
ScVal::Symbol(_) => todo!(),
ScVal::Static(_) => "todo!".to_string(), //TODO:fix this
ScVal::Symbol(v) => format!(
"sym:{}",
std::str::from_utf8(v.as_slice()).expect("non-UTF-8 in symbol")
),
ScVal::Bitset(_) => todo!(),
ScVal::Status(_) => todo!(),
ScVal::Object(None) => panic!(""),
Expand Down