Skip to content

Commit

Permalink
patch mock-enclave for strong seq
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Apr 25, 2022
1 parent ae12cd3 commit f667a0a
Show file tree
Hide file tree
Showing 18 changed files with 667 additions and 2,226 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [

# a Myers Diff algo impl
'crates/skw-myers-diff',
'crates/skw-vm-patch',

# sgx enclave host
'crates/skw-sgx-ipfs',
Expand Down
293 changes: 164 additions & 129 deletions crates/skw-vm-interface/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ pub fn decode_hex(s: &str) -> Vec<u8> {
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
.collect()
}

fn pad_size(size: usize) -> [u8; 4] {
let mut v = [0, 0, 0, 0];
v[3] = (size & 0xff) as u8;
v[2] = ((size >> 8) & 0xff) as u8;
v[1] = ((size >> 16) & 0xff) as u8;
v[0] = ((size >> 24) & 0xff) as u8;
v
}

fn unpad_size(size: &[u8; 4]) -> usize {
if size.len() != 4 {
panic!("Invalid size");
}
return (
size[3] as usize |
((size[2] as usize) << 8) |
((size[1] as usize) << 16) |
((size[0] as usize) << 24)
).into();
}

#[derive(Clap)]
struct CliArgs {
#[clap(long)]
Expand Down Expand Up @@ -88,13 +110,7 @@ fn main() {
tracing_span_tree::span_tree().enable();
}


let decoded_call = bs58::decode(&cli_args.params.unwrap_or_default()).into_vec().unwrap();
let state_patch: StatePatch = bs58::decode(&cli_args.state_patch.unwrap_or_default()).into_vec().unwrap();

let params: Input = BorshDeserialize::try_from_slice(&decoded_call).expect("input parsing failed");

let mut outcomes = Outputs::default();
let mut script = Script::default();
let mut state_root: CryptoHash = decode_hex(&cli_args.state_root.as_str())
.try_into()
Expand All @@ -106,10 +122,10 @@ fn main() {
let store = create_store();
match state_patch.len() {
0 => {
store.read_from_patch(state_path, &state_patch[..]).unwrap();
store.load_state_from_file(state_path).unwrap();
},
_ => {
store.load_state_from_file(state_path).unwrap();
store.read_from_patch(state_path, &state_patch[..]).unwrap();
}
}

Expand All @@ -119,138 +135,157 @@ fn main() {
&"empty".to_string(),
);

for input in params.ops.iter() {

script.update_account(input.origin.as_ref().unwrap_or(&"empty".to_string()));

let mut outcome: Option<ExecutionResult> = None;
let mut view_outcome: Option<ViewResult> = None;

match input.transaction_action.as_str() {
"create_account" => {
assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);

script.create_account(
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
"transfer" => {
assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);

script.transfer(
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
"call" => {
assert!(
input.method.is_some(),
"method must be provided when transaction_action is set"
);

assert!(
input.args.is_some(),
"args must be provided when transaction_action is set"
);

outcome = Some(script.call(
&input.receiver,
&input.method.as_ref().unwrap(),
&input.args.as_ref().unwrap().as_bytes(),
u128::from(input.amount.unwrap_or(0)) * 10u128.pow(24),
));
},
"view_method_call" => {
assert!(
input.method.is_some(),
"method must be provided when transaction_action is set"
);

assert!(
input.args.is_some(),
"args must be provided when transaction_action is set"
);

view_outcome = Some(script.view_method_call(
&input.receiver,
&input.method.as_ref().unwrap(),
&input.args.as_ref().unwrap().as_bytes(),
));
},
"delete_account" => {
script.delete_account(
&input.receiver,
&input.to.as_ref().unwrap(),
);
},
"deploy" => {
assert!(
input.wasm_blob_path.is_some(),
"wasm_file must be provided when transaction_action is set"
);
let decoded_call = bs58::decode(&cli_args.params.unwrap_or_default()).into_vec().unwrap();

assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);
let mut all_outcomes: Vec<u8> = Vec::new();
let decoded_call_len = decoded_call.len();
let mut offset = 0;

let wasm_path = PathBuf::from(input.wasm_blob_path.as_ref().unwrap());
let code = fs::read(&wasm_path).unwrap();
while offset < decoded_call_len {
let size = unpad_size(&decoded_call[offset..offset + 4].try_into().unwrap());

script.deploy(
&code,
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
_ => {}
}
let call_index = unpad_size(&decoded_call[offset + 4..offset + 8].try_into().unwrap());
let params: Input = BorshDeserialize::try_from_slice(&decoded_call[offset + 8..offset + 4 + size]).expect("input parsing failed");
let mut outcome_of_call = Outputs::default();

for input in params.ops.iter() {
script.update_account(input.origin.as_ref().unwrap_or(&"empty".to_string()));

state_root = script.state_root();
let mut execution_result: InterfaceOutcome = InterfaceOutcome::default();

match &outcome {
Some(outcome) => {
execution_result.outcome_logs = outcome.logs().clone();
execution_result.outcome_receipt_ids = outcome.receipt_ids().clone();
execution_result.outcome_gas_burnt = outcome.gas_burnt().0;
execution_result.outcome_tokens_burnt = outcome.tokens_burnt();
execution_result.outcome_executor_id = outcome.executor_id().to_string();
execution_result.outcome_status = match outcome.status() {
ExecutionStatus::SuccessValue(x) => Some(x),
_ => None,
};
let mut outcome: Option<ExecutionResult> = None;
let mut view_outcome: Option<ViewResult> = None;

match input.transaction_action.as_str() {
"create_account" => {
assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);

script.create_account(
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
"transfer" => {
assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);

script.transfer(
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
"call" => {
assert!(
input.method.is_some(),
"method must be provided when transaction_action is set"
);

assert!(
input.args.is_some(),
"args must be provided when transaction_action is set"
);

outcome = Some(script.call(
&input.receiver,
&input.method.as_ref().unwrap(),
&input.args.as_ref().unwrap().as_bytes(),
u128::from(input.amount.unwrap_or(0)) * 10u128.pow(24),
));
},
"view_method_call" => {
assert!(
input.method.is_some(),
"method must be provided when transaction_action is set"
);

assert!(
input.args.is_some(),
"args must be provided when transaction_action is set"
);

view_outcome = Some(script.view_method_call(
&input.receiver,
&input.method.as_ref().unwrap(),
&input.args.as_ref().unwrap().as_bytes(),
));
},
"delete_account" => {
script.delete_account(
&input.receiver,
&input.to.as_ref().unwrap(),
);
},
"deploy" => {
assert!(
input.wasm_blob_path.is_some(),
"wasm_file must be provided when transaction_action is set"
);

assert!(
input.amount.is_some(),
"amount must be provided when transaction_action is set"
);

let wasm_path = PathBuf::from(input.wasm_blob_path.as_ref().unwrap());
let code = fs::read(&wasm_path).unwrap();

script.deploy(
&code,
&input.receiver,
u128::from(input.amount.unwrap()) * 10u128.pow(24),
);
},
_ => {}
}
_ => {}
}

match &view_outcome {
Some(outcome) => {
execution_result.view_result_log = outcome.logs().clone();
execution_result.view_result = outcome.unwrap().clone();

state_root = script.state_root();
let mut execution_result: InterfaceOutcome = InterfaceOutcome::default();

match &outcome {
Some(outcome) => {
execution_result.outcome_logs = outcome.logs().clone();
execution_result.outcome_receipt_ids = outcome.receipt_ids().clone();
execution_result.outcome_gas_burnt = outcome.gas_burnt().0;
execution_result.outcome_tokens_burnt = outcome.tokens_burnt();
execution_result.outcome_executor_id = outcome.executor_id().to_string();
execution_result.outcome_status = match outcome.status() {
ExecutionStatus::SuccessValue(x) => Some(x),
_ => None,
};
}
_ => {}
}
_ => {}

match &view_outcome {
Some(outcome) => {
execution_result.view_result_log = outcome.logs().clone();
execution_result.view_result = outcome.unwrap().clone();
}
_ => {}
}

outcome_of_call.ops.push(execution_result);
script.sync_state_root();
}

outcome_of_call.state_root = state_root;
let mut buffer: Vec<u8> = Vec::new();
outcome_of_call.serialize(&mut buffer).unwrap();

outcomes.ops.push(execution_result);
script.sync_state_root();
all_outcomes.extend_from_slice(&pad_size(buffer.len() + 4)[..]);
all_outcomes.extend_from_slice(&pad_size(call_index)[..]);
all_outcomes.extend_from_slice(&buffer[..]);

offset += 4 + size;
}

if cli_args.dump_state {
script.write_to_file(&cli_args.state_file, &mut state_root);
}

outcomes.state_root = state_root;

let mut buffer: Vec<u8> = Vec::new();
outcomes.serialize(&mut buffer).unwrap();

println!("{:?}", bs58::encode(buffer).into_string());
// println!("{:?}", all_outcomes);
println!("{:?}", bs58::encode(all_outcomes).into_string());
}
18 changes: 18 additions & 0 deletions crates/skw-vm-patch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "skw-vm-patch"
version = '0.1.0'
authors = ['SkyeKiwi <[email protected]>', "Near Inc <[email protected]>"]
edition = '2018'
homepage = 'https://skye.kiwi'
repository = 'https://github.com/skyekiwi/skyekiwi-network'
license = 'GPL-3.0'
readme = 'README.md'
description = """
Patch a series of state to the original state file
"""

[dependencies]
skw-myers-diff = { path = "../skw-myers-diff" }
clap = "=3.0.0-beta.2"
clap_derive = "=3.0.0-beta.2"
bs58 = "0.4.0"
Loading

0 comments on commit f667a0a

Please sign in to comment.