Skip to content

Commit

Permalink
Merge pull request #103 from dfinance/update-libra-august
Browse files Browse the repository at this point in the history
update to latest libra
  • Loading branch information
mkurnikov authored Aug 17, 2020
2 parents 7f74857 + 5317b29 commit 37c3653
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 1,055 deletions.
1,280 changes: 476 additions & 804 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions crates/dialects/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ bech32 = "0.7.2"

utils = { path = "../utils" }

move-vm-runtime = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
move-vm-types = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
libra-state-view = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
language-e2e-tests = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
move-core-types = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
vm = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
libra-types = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
move-lang = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
libra-canonical-serialization = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
libra-crypto = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
move-ir-types = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
vm-genesis = { git = "https://github.com/dfinance/libra.git", branch = "09.07.2020" }
move-vm-runtime = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
move-vm-types = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
libra-state-view = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
language-e2e-tests = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
move-core-types = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
vm = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
libra-types = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
move-lang = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
libra-canonical-serialization = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
libra-crypto = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
move-ir-types = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }
vm-genesis = { git = "https://github.com/dfinance/libra.git", branch = "29.07.2020" }

#move-vm-runtime = { path = "../../../dvm-libra/language/move-vm/runtime" }
#move-vm-types = { path = "../../../dvm-libra/language/move-vm/types" }
Expand Down
130 changes: 0 additions & 130 deletions crates/dialects/src/lang/data_cache.rs

This file was deleted.

149 changes: 121 additions & 28 deletions crates/dialects/src/lang/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use language_e2e_tests::data_store::FakeDataStore;
use libra_types::{transaction::TransactionArgument, vm_status::VMStatus, write_set::WriteSet};
use libra_types::{transaction::TransactionArgument, write_set::WriteSet};
use move_core_types::account_address::AccountAddress;
use move_core_types::gas_schedule::{CostTable, GasAlgebra, GasUnits};

Expand All @@ -11,17 +11,18 @@ use move_vm_types::values::Value;
use vm::file_format::CompiledScript;
use vm::CompiledModule;

use crate::lang::{data_cache, PreBytecodeProgram};
use crate::lang::PreBytecodeProgram;

use crate::shared::results::{ChainStateChanges, ExecutionError};
use crate::lang::resources::ResourceWriteOp;
use crate::shared::results::{ChainStateChanges, ResourceChange, ResourceType};

pub fn vm_status_into_exec_status(vm_status: VMStatus) -> ExecutionError {
ExecutionError {
status: format!("{:?}", vm_status.major_status),
sub_status: vm_status.sub_status,
message: vm_status.message,
}
}
use libra_types::write_set::WriteOp;
use move_core_types::language_storage::{ModuleId, TypeTag};
use move_core_types::value::MoveTypeLayout;
use move_core_types::vm_status::StatusCode;
use move_vm_runtime::data_cache::TransactionEffects;

use vm::errors::{Location, PartialVMError, VMResult};

pub fn generate_bytecode(
program: PreBytecodeProgram,
Expand Down Expand Up @@ -57,41 +58,133 @@ pub fn prepare_fake_network_state(
network_state
}

fn serialize_val(val: Value, layout: MoveTypeLayout) -> VMResult<Vec<u8>> {
match val.simple_serialize(&layout) {
Some(blob) => Ok(blob),
None => {
let partial_vm_error = PartialVMError::new(StatusCode::VALUE_SERIALIZATION_ERROR);
Err(partial_vm_error.finish(Location::Undefined))
}
}
}

type ResourceChangeData = (TypeTag, Option<(MoveTypeLayout, Value)>);

fn into_resource_changes(
effect_resources: Vec<(AccountAddress, Vec<ResourceChangeData>)>,
) -> VMResult<Vec<ResourceChange>> {
let mut resources = vec![];
for (addr, resource_vals) in effect_resources {
let account_address = format!("0x{}", addr.to_string());
for (ty, val) in resource_vals {
let resource_type = ResourceType::new(ty);
match val {
None => resources.push(ResourceChange::new(
account_address.clone(),
resource_type,
ResourceWriteOp(WriteOp::Deletion),
)),
Some((layout, val)) => {
let val = serialize_val(val, layout)?;
resources.push(ResourceChange::new(
account_address.clone(),
resource_type,
ResourceWriteOp(WriteOp::Value(val)),
));
}
}
}
}
Ok(resources)
}

#[derive(Debug, serde::Serialize)]
pub struct Event {
guid: Vec<u8>,
seq_num: u64,
ty_tag: TypeTag,
val: Vec<u8>,
caller: Option<ModuleId>,
}

type EventData = (
Vec<u8>,
u64,
TypeTag,
MoveTypeLayout,
Value,
Option<ModuleId>,
);

fn into_events(effect_events: Vec<EventData>) -> VMResult<Vec<Event>> {
let mut events = vec![];
for (guid, seq_num, ty_tag, ty_layout, val, caller) in effect_events {
let val = serialize_val(val, ty_layout)?;
events.push(Event {
guid,
seq_num,
ty_tag,
val,
caller,
})
}
Ok(events)
}

fn execute_script_with_runtime_session(
data_store: &FakeDataStore,
script: Vec<u8>,
args: Vec<Value>,
sender: AccountAddress,
cost_strategy: &mut CostStrategy,
) -> VMResult<TransactionEffects> {
let vm = MoveVM::new();
let mut runtime_session = vm.new_session(data_store);

runtime_session.execute_script(script, vec![], args, sender, cost_strategy)?;
runtime_session.finish()
}

pub fn chain_state_changes(
effects: TransactionEffects,
gas_spent: u64,
) -> VMResult<ChainStateChanges> {
let TransactionEffects {
resources, events, ..
} = effects;
let resource_changes = into_resource_changes(resources)?;
let events = into_events(events)?;
Ok(ChainStateChanges {
resource_changes,
events,
gas_spent,
})
}

pub fn execute_script(
sender_address: AccountAddress,
data_store: &FakeDataStore,
script: Vec<u8>,
args: Vec<Value>,
cost_table: CostTable,
) -> Result<ChainStateChanges> {
let mut data_cache = data_cache::DataCache::new(data_store);

let total_gas = 1_000_000;
let mut cost_strategy = CostStrategy::transaction(&cost_table, GasUnits::new(total_gas));

let vm = MoveVM::new();
vm.execute_script(
let effects = execute_script_with_runtime_session(
data_store,
script,
vec![],
args,
sender_address,
&mut data_cache,
&mut cost_strategy,
)
.map_err(vm_status_into_exec_status)
.map_err(|error| error.into_vm_status())
.with_context(|| "Script execution error")?;

let events = data_cache.events();
let resource_changes = data_cache
.resource_changes()
.map_err(vm_status_into_exec_status)
.with_context(|| "Changeset serialization error")?;
let gas_spent = total_gas - cost_strategy.remaining_gas().get();
Ok(ChainStateChanges {
resource_changes,
gas_spent,
events,
})

let changes =
chain_state_changes(effects, gas_spent).map_err(|error| error.into_vm_status())?;
Ok(changes)
}

/// Convert the transaction arguments into move values.
Expand Down
1 change: 0 additions & 1 deletion crates/dialects/src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::shared::errors::{
ProjectSourceMap,
};

pub mod data_cache;
pub mod executor;
pub mod gas;
pub mod resources;
Expand Down
Loading

0 comments on commit 37c3653

Please sign in to comment.