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

Forc deploy #99

Merged
merged 51 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5abd08a
remove typescript ignores
leviathanbeak May 26, 2021
6c532fa
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak May 26, 2021
df93ff3
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak May 26, 2021
2a902f8
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 1, 2021
aba7c8e
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 15, 2021
7d04824
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 17, 2021
447ae06
handle equal sign with ! correctly
leviathanbeak Jun 17, 2021
371e505
handle more whitespace cases
leviathanbeak Jun 17, 2021
5ec69ff
handle multiline comments and inline comments
leviathanbeak Jun 21, 2021
9079f32
add newline
leviathanbeak Jun 21, 2021
a4c8431
add space after quote
leviathanbeak Jun 21, 2021
be79950
added tests
leviathanbeak Jun 21, 2021
257a4b1
fixed multiline */
leviathanbeak Jun 21, 2021
374b401
fix adding inline comment
leviathanbeak Jun 21, 2021
8f51779
add "}" before moving to new line or new comment
leviathanbeak Jun 21, 2021
e953be6
Merge branch 'multiline-comments' into formatter-tests
leviathanbeak Jun 21, 2021
5f664d8
improve comments tests
leviathanbeak Jun 21, 2021
0691f1e
add more multiline checks
leviathanbeak Jun 21, 2021
2ef1db1
update tests
leviathanbeak Jun 22, 2021
4abf2c2
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll into formatt…
leviathanbeak Jun 22, 2021
1eb8238
check for None option when checking whitespace
leviathanbeak Jun 22, 2021
f0f6779
check that last char is not None
leviathanbeak Jun 22, 2021
c902319
add comma to ignore list
leviathanbeak Jun 22, 2021
b32f920
added more extensive testcase dealing with keywords on multiple lines
leviathanbeak Jun 22, 2021
b34458f
handle keywords on different lines case
leviathanbeak Jun 22, 2021
4bb5c18
added code type
leviathanbeak Jun 22, 2021
607d8bf
added custom type to handle enum,struct commas
leviathanbeak Jun 23, 2021
b968368
added custom types test
leviathanbeak Jun 23, 2021
33de9dc
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll into improve…
leviathanbeak Jun 23, 2021
82b453f
added more tests to handle custom types and comments
leviathanbeak Jun 24, 2021
fc8ee87
handle adding line of custom type
leviathanbeak Jun 24, 2021
176d5c2
improved multiline comment logic
leviathanbeak Jun 24, 2021
ec18781
refactored, and added correct multiline comment and custom types form…
leviathanbeak Jun 24, 2021
d953dcb
improve adding comma logic to struct or enum
leviathanbeak Jun 24, 2021
ca40089
use ropey and parser in formatter
leviathanbeak Jun 27, 2021
cedd82f
make public
leviathanbeak Jun 27, 2021
e276eeb
handle custom types in traversal
leviathanbeak Jun 27, 2021
786c1f6
remove custom types logic
leviathanbeak Jun 27, 2021
93f5fcc
use traversal
leviathanbeak Jun 27, 2021
efcbfc8
traverse ast before formatting
leviathanbeak Jun 27, 2021
36a4814
use updated formatter
leviathanbeak Jun 27, 2021
357c3ba
use updated formatter
leviathanbeak Jun 27, 2021
a06fbcd
move functions to helpers
leviathanbeak Jun 28, 2021
2a8f08e
added forc deploy
leviathanbeak Jun 28, 2021
0b6af5c
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll into forc-de…
leviathanbeak Jun 29, 2021
bccdd75
added src constant
leviathanbeak Jul 1, 2021
fa8265f
added todo comment
leviathanbeak Jul 1, 2021
db72816
added manifest not found message
leviathanbeak Jul 1, 2021
24df7f4
added more constants
leviathanbeak Jul 1, 2021
465b224
use forc_build and add more detailed error messages
leviathanbeak Jul 1, 2021
951dd2d
remove pub from fn
leviathanbeak Jul 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions forc/src/cli/commands/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use structopt::{self, StructOpt};

use crate::ops::forc_deploy;

#[derive(Debug, StructOpt)]
pub(crate) struct Command {}
pub struct Command {}

pub(crate) fn exec(command: Command) -> Result<(), String> {
todo!()
match forc_deploy::deploy(command) {
Err(e) => Err(e.message),
_ => Ok(()),
}
}
2 changes: 1 addition & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use analysis::Command as AnalysisCommand;
use benchmark::Command as BenchmarkCommand;
pub use build::Command as BuildCommand;
use coverage::Command as CoverageCommand;
use deploy::Command as DeployCommand;
pub use deploy::Command as DeployCommand;
pub use format::Command as FormatCommand;
use init::Command as InitCommand;
use mvprun::Command as MvprunCommand;
Expand Down
50 changes: 7 additions & 43 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{cli::BuildCommand, utils::helpers::find_manifest_dir};
use crate::{
cli::BuildCommand,
utils::helpers::{find_manifest_dir, get_main_file, read_manifest},
};
use line_col::LineColLookup;
use source_span::{
fmt::{Color, Formatter, Style},
Expand All @@ -8,12 +11,12 @@ use std::fs::File;
use std::io::{self, Write};
use termcolor::{BufferWriter, Color as TermColor, ColorChoice, ColorSpec, WriteColor};

use crate::utils::manifest::{Dependency, DependencyDetails, Manifest};
use crate::utils::manifest::{Dependency, DependencyDetails};
use core_lang::{
BuildConfig, BytecodeCompilationResult, CompilationResult, FinalizedAsm, LibraryExports,
Namespace,
};
use std::{fs, path::PathBuf};
use std::path::PathBuf;

pub fn build(command: BuildCommand) -> Result<Vec<u8>, String> {
let BuildCommand {
Expand Down Expand Up @@ -76,7 +79,7 @@ pub fn build(command: BuildCommand) -> Result<Vec<u8>, String> {

/// Takes a dependency and returns a namespace of exported things from that dependency
/// trait implementations are included as well
fn compile_dependency_lib<'source, 'manifest>(
pub fn compile_dependency_lib<'source, 'manifest>(
project_path: &PathBuf,
dependency_name: &'manifest str,
dependency_lib: &Dependency,
Expand Down Expand Up @@ -138,28 +141,6 @@ fn compile_dependency_lib<'source, 'manifest>(
Ok(())
}

fn read_manifest(manifest_dir: &PathBuf) -> Result<Manifest, String> {
let manifest_path = {
let mut man = manifest_dir.clone();
man.push(crate::utils::constants::MANIFEST_FILE_NAME);
man
};
let manifest_path_str = format!("{:?}", manifest_path);
let manifest = match std::fs::read_to_string(manifest_path) {
Ok(o) => o,
Err(e) => {
return Err(format!(
"failed to read manifest at {:?}: {}",
manifest_path_str, e
))
}
};
match toml::from_str(&manifest) {
Ok(o) => Ok(o),
Err(e) => Err(format!("Error parsing manifest: {}.", e)),
}
}

fn compile_library<'source, 'manifest>(
source: &'source str,
proj_name: &str,
Expand Down Expand Up @@ -378,23 +359,6 @@ fn write_yellow(txt: &str) -> io::Result<()> {
Ok(())
}

fn get_main_file(
manifest_of_dep: &Manifest,
manifest_dir: &PathBuf,
) -> Result<&'static mut String, String> {
let main_path = {
let mut code_dir = manifest_dir.clone();
code_dir.push("src");
code_dir.push(&manifest_of_dep.project.entry);
code_dir
};

// some hackery to get around lifetimes for now, until the AST returns a non-lifetime-bound AST
let main_file = fs::read_to_string(&main_path).map_err(|e| e.to_string())?;
let main_file = Box::new(main_file);
let main_file: &'static mut String = Box::leak(main_file);
return Ok(main_file);
}
fn compile_to_asm<'source, 'manifest>(
source: &'source str,
proj_name: &str,
Expand Down
135 changes: 135 additions & 0 deletions forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use core_lang::{compile_to_bytecode, parse, BuildConfig, Namespace};
use fuel_tx::{crypto::hash, ContractAddress, Output, Salt, Transaction};

use crate::{
cli::DeployCommand,
utils::helpers::{find_manifest_dir, get_main_file, read_manifest},
};
use std::{fmt, io};

use super::forc_build::compile_dependency_lib;

pub fn deploy(_: DeployCommand) -> Result<(), DeployError> {
let curr_dir = std::env::current_dir()?;

match find_manifest_dir(&curr_dir) {
Some(manifest_dir) => {
let build_config = BuildConfig::root_from_manifest_path(manifest_dir.clone());
let manifest = read_manifest(&manifest_dir)?;
let mut namespace: Namespace = Default::default();

// compile dependencies
if let Some(ref deps) = manifest.dependencies {
for (dependency_name, dependency_details) in deps.iter() {
compile_dependency_lib(
&curr_dir,
&dependency_name,
&dependency_details,
&mut namespace,
)?;
}
}
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved

// compile this program with all of its dependencies
let main_file = get_main_file(&manifest, &manifest_dir)?;

// parse it and check is it a contract
match parse(main_file) {
core_lang::CompileResult::Ok {
value: parse_tree,
warnings: _,
errors: _,
} => {
if let Some(_) = &parse_tree.contract_ast {
// create Transaction::Create from contract file
let compiled_contract =
compile_contract(main_file, namespace, build_config)?;
let tx = create_contract_tx(compiled_contract);
println!("{:?}", tx);
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
} else {
Err("Project is not a contract".into())
}
}
_ => Err("Project does not compile".into()),
}
}
None => Err("Manifest file does not exist".into()),
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn compile_contract(
contract_file: &str,
namespace: Namespace,
build_config: BuildConfig,
) -> Result<Vec<u8>, DeployError> {
let result = compile_to_bytecode(contract_file, &namespace, build_config);

match result {
core_lang::BytecodeCompilationResult::Success { bytes, warnings: _ } => Ok(bytes),
_ => Err("Failed to compile".into()),
}
}

fn create_contract_tx(compiled_contract: Vec<u8>) -> Transaction {
let gas_price = 0;
let gas_limit = 10000000;
let maturity = 0;
let bytecode_witness_index = 0;
let witnesses = vec![compiled_contract.into()];

let salt = Salt::new([0; 32]);
let static_contracts = vec![];
let inputs = vec![];

let zero_hash = hash("0".as_bytes());

let outputs = vec![Output::ContractCreated {
contract_id: ContractAddress::new(zero_hash.into()),
}];

Transaction::create(
gas_price,
gas_limit,
maturity,
bytecode_witness_index,
salt,
static_contracts,
inputs,
outputs,
witnesses,
)
}

pub struct DeployError {
pub message: String,
}

impl fmt::Display for DeployError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self)
}
}

impl From<&str> for DeployError {
fn from(s: &str) -> Self {
DeployError {
message: s.to_string(),
}
}
}

impl From<String> for DeployError {
fn from(s: String) -> Self {
DeployError { message: s }
}
}

impl From<io::Error> for DeployError {
fn from(e: io::Error) -> Self {
DeployError {
message: e.to_string(),
}
}
}
1 change: 1 addition & 0 deletions forc/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod forc_build;
pub mod forc_deploy;
pub mod forc_fmt;
pub mod forc_init;
41 changes: 41 additions & 0 deletions forc/src/utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::manifest::Manifest;
use std::path::PathBuf;

// Continually go up in the file tree until a manifest (Forc.toml) is found.
Expand All @@ -16,3 +17,43 @@ pub fn find_manifest_dir(starter_path: &PathBuf) -> Option<PathBuf> {
}
None
}

pub fn read_manifest(manifest_dir: &PathBuf) -> Result<Manifest, String> {
let manifest_path = {
let mut man = manifest_dir.clone();
man.push(crate::utils::constants::MANIFEST_FILE_NAME);
man
};
let manifest_path_str = format!("{:?}", manifest_path);
let manifest = match std::fs::read_to_string(manifest_path) {
Ok(o) => o,
Err(e) => {
return Err(format!(
"failed to read manifest at {:?}: {}",
manifest_path_str, e
))
}
};
match toml::from_str(&manifest) {
Ok(o) => Ok(o),
Err(e) => Err(format!("Error parsing manifest: {}.", e)),
}
}

pub fn get_main_file(
manifest_of_dep: &Manifest,
manifest_dir: &PathBuf,
) -> Result<&'static mut String, String> {
let main_path = {
let mut code_dir = manifest_dir.clone();
code_dir.push("src");
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
code_dir.push(&manifest_of_dep.project.entry);
code_dir
};

// some hackery to get around lifetimes for now, until the AST returns a non-lifetime-bound AST
let main_file = std::fs::read_to_string(&main_path).map_err(|e| e.to_string())?;
let main_file = Box::new(main_file);
let main_file: &'static mut String = Box::leak(main_file);
return Ok(main_file);
}