Skip to content

Commit

Permalink
Reorgnize config
Browse files Browse the repository at this point in the history
  • Loading branch information
liangping committed Apr 14, 2020
1 parent b212eb3 commit 041c487
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 63 deletions.
3 changes: 2 additions & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::Config, commands::Command, Parameter};
use crate::{commands::Command, config::Config, Parameter};

pub struct NewCommand{}

Expand All @@ -7,6 +7,7 @@ impl Command for NewCommand{
if let Parameter::New{home, name} = params {
let x = Config::new(name, home);
x.initial();
x.genesis();
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
use crate::{
config::Config,
commands::Command,
runner::MoveRunner,
Parameter,
};

use bytecode_verifier::verifier::VerifiedModule;
use glob::glob;
use libra_types::transaction::{
parse_as_transaction_argument,
TransactionArgument,
Expand All @@ -16,6 +8,7 @@ use move_vm_state::{
//data_cache::{BlockDataCache, RemoteCache},
execution_context::SystemExecutionContext,
};
use move_vm_state::execution_context::TransactionExecutionContext;
use move_vm_types::values::values_impl::Value;
use vm::{
errors::VMResult,
Expand All @@ -25,6 +18,15 @@ use vm::{
transaction_metadata::TransactionMetadata,
};

use glob::glob;

use crate::{
commands::Command,
config::Config,
Parameter,
runner::MoveRunner,
};

pub struct RunCommand{}

impl Command for RunCommand{
Expand Down Expand Up @@ -72,11 +74,11 @@ impl Command for RunCommand{
// Execute script.
// create a Move VM and populate it with generated modules
let move_vm = MoveVM::new();
let mut ctx = SystemExecutionContext::new(&m_runner.datastore, GasUnits::new(0));
let mut ctx = TransactionExecutionContext::new(GasUnits::new(0), &m_runner.datastore);
let gas_schedule = CostTable::zero();

let mut txn_data = TransactionMetadata::default();
txn_data.sender = cfg.state.address;
txn_data.sender = cfg.address();

let result: VMResult<()> = move_vm.execute_script(script, &gas_schedule, &mut ctx, &txn_data, vec![], va_tags);

Expand Down
91 changes: 67 additions & 24 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
use serde::{Deserialize, Serialize};
use std::{fs, path::PathBuf};
//use libra_crypto::{ed25519::Ed25519PrivateKey, test_utils::TEST_SEED, PrivateKey, Uniform};
use libra_types::{ account_address::AccountAddress };

use libra_crypto::{ed25519::{Ed25519PrivateKey, Ed25519PublicKey}, traits::*, Uniform};
use libra_types::account_address::AccountAddress;
use rand::{
Rng,
rngs::{OsRng, StdRng}, SeedableRng,
};
use serde::{Deserialize, Serialize};

const DEFAULT_CONFIG_FILE: &str = "Move.toml";
const GENESIS_BLOB: &str = "genesis.blob";

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
project_name: String,
home: PathBuf,
pub home: PathBuf,
pub workspace: Workspace,
pub compile: Compile,
pub state: DevState,
pub tx: DevTransaction,
pub storage: Storage,
}

impl Config {

pub fn new(name: String, home: PathBuf) -> Self {
Self {
project_name: name,
home,
workspace: Workspace::default(),
compile: Compile::default(),
state: DevState::default(),
tx: DevTransaction::default(),
storage: Storage::default(),
}
}

pub fn load_config(home : PathBuf) -> Self {
pub fn load_config(home: PathBuf) -> Self {
println!("loaded config from {:?}", &home.join(DEFAULT_CONFIG_FILE));
let content = fs::read_to_string(&home.join( DEFAULT_CONFIG_FILE )).expect("Failed to loaded config files");
toml::from_str(&content).expect("Failed to loaded Move.toml")
let content = fs::read_to_string(&home.join(DEFAULT_CONFIG_FILE))
.expect("Failed to loaded config files");
let mut cfg: Self = toml::from_str(&content).expect("Failed to loaded Move.toml");
cfg.home = home; // replace home with the value of argument
cfg
}

pub fn initial(&self) {

fs::create_dir_all(&self.home).expect("Can not create home directory");
fs::create_dir_all(&self.module_dir()).expect("Failed to create module directory");
fs::create_dir_all(&self.script_dir()).expect("Failed to create script directory");
Expand All @@ -43,17 +52,25 @@ impl Config {
fs::write(&self.home.join(DEFAULT_CONFIG_FILE), cfg).expect("Failed to create Move.toml");
}

pub fn module_dir(&self)-> PathBuf {
pub fn genesis(&self) {
fs::write(&self.home.join(GENESIS_BLOB), []).expect("Failed to create genesis.blob");
}

pub fn module_dir(&self) -> PathBuf {
self.home.join(&self.workspace.module_dir)
}

pub fn script_dir(&self)-> PathBuf {
pub fn script_dir(&self) -> PathBuf {
self.home.join(&self.workspace.script_dir)
}

pub fn target_dir(&self)-> PathBuf {
pub fn target_dir(&self) -> PathBuf {
self.home.join(&self.workspace.target_dir)
}

pub fn address(&self) -> AccountAddress {
self.tx.address
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -97,19 +114,45 @@ impl Default for Compile {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DevState {
/// Script path
pub struct DevTransaction {
pub address: AccountAddress,
// pub private_key: String,
// pub public_key: String,
pub keypair_private_key: String,
pub keypair_public_key: Ed25519PublicKey,
pub sequence_number: u64,
}

impl Default for DevState {
fn default() -> DevState {
DevState {
address: AccountAddress::random(),
// private_key: ,
// public_key: public_key.to_string(),
impl Default for DevTransaction {
fn default() -> Self {
let (private_key, keypair_public_key) = generate_keypair();
Self {
address: AccountAddress::from_public_key(&keypair_public_key),
sequence_number: 0,
keypair_private_key: private_key.to_encoded_string().unwrap(),
keypair_public_key,
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Storage {
pub load_state_from_genesis: bool,
pub save_writeset_to_genesis: bool,
}

/// Generate an Ed25519 key pair.
fn generate_keypair() -> (Ed25519PrivateKey, Ed25519PublicKey) {
let mut seed_rng = OsRng::new().expect("can't access OsRng");
let seed: [u8; 32] = seed_rng.gen();
let mut stdrng = StdRng::from_seed(seed);
let private_key = Ed25519PrivateKey::generate(&mut stdrng);
let public_key = private_key.public_key();
(private_key, public_key)
}

#[test]
fn test_generate_keypair() {
let (private_key, public_key) = generate_keypair();
println!("{}=>{}", private_key.to_encoded_string().unwrap(), public_key);
}


57 changes: 29 additions & 28 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@

#![allow(unused_imports)]

use move_vm_runtime::{
MoveVM,
};
use move_vm_state::{
//data_cache::{BlockDataCache, RemoteCache},
execution_context::{ExecutionContext, SystemExecutionContext},
use std::{
fs,
io::Write,
path::{Path, PathBuf},
};

//use bytecode_source_map::source_map::SourceMap;
use bytecode_verifier::{
verifier::{VerifiedScript,VerifiedModule}
};
use compiler::Compiler;
use language_e2e_tests::{
account::{Account, AccountData},
data_store::FakeDataStore,
};
use vm::{
errors::VMResult,
//access::ModuleAccess,
gas_schedule::{
//AbstractMemorySize,
CostTable, GasAlgebra,
//GasCarrier,
GasUnits},
transaction_metadata::TransactionMetadata,
};
use libra_types::{
account_address::AccountAddress,
account_config,
transaction::{
//Module,
//Module,
Script,
TransactionArgument,
},
write_set::{WriteSet, WriteOp},
write_set::{WriteOp, WriteSet},
};
use compiler::Compiler;
use std::{
path::{Path, PathBuf},
fs,
io::Write,
use move_vm_runtime::MoveVM;
use move_vm_state::{
//data_cache::{BlockDataCache, RemoteCache},
execution_context::{ExecutionContext, SystemExecutionContext},
};
use stdlib::{stdlib_modules, StdLibOptions};
use move_vm_types::values::Value;
use include_dir::{include_dir, Dir};
use stdlib::{stdlib_modules, StdLibOptions};
use vm::{
errors::VMResult,
//access::ModuleAccess,
gas_schedule::{
//AbstractMemorySize,
CostTable, GasAlgebra,
//GasCarrier,
GasUnits},
transaction_metadata::TransactionMetadata,
};

use include_dir::{Dir, include_dir};

use super::config::Config;

pub struct MoveRunner {
Expand All @@ -55,7 +56,7 @@ pub struct MoveRunner {

impl MoveRunner {
pub fn new(cfg: Config) -> Self{
println!("\n Compiling with address: 0x{:?}\n", cfg.state.address);
println!("\n Compiling with address: 0x{:?}\n", cfg.address());
MoveRunner{
cfg,
stdlib: stdlib_modules(StdLibOptions::Staged).to_vec(),
Expand All @@ -65,7 +66,7 @@ impl MoveRunner {

pub fn complie_module(&mut self, path: &Path ) -> VerifiedModule {
let c = Compiler {
address: self.cfg.state.address,
address: self.cfg.address(),
skip_stdlib_deps: false,
extra_deps: self.stdlib.clone(),
..Compiler::default()
Expand All @@ -91,7 +92,7 @@ impl MoveRunner {

pub fn complie_script(&self, path: &Path) -> VerifiedScript {
let c = Compiler {
address: self.cfg.state.address,
address: self.cfg.address(),
skip_stdlib_deps: false,
extra_deps: self.stdlib.clone(),
..Compiler::default()
Expand Down

0 comments on commit 041c487

Please sign in to comment.