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

Correct usage of terms "ABI" and "JSON ABI" #4847

Merged
merged 2 commits into from
Jul 24, 2023
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
57 changes: 32 additions & 25 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use sway_core::fuel_prelude::fuel_tx::ConsensusParameters;
pub use sway_core::Programs;
use sway_core::{
abi_generation::{
evm_json_abi,
fuel_json_abi::{self, JsonAbiContext},
evm_abi,
fuel_abi::{self, AbiContext},
},
asm_generation::ProgramABI,
decl_engine::DeclRefFunction,
Expand Down Expand Up @@ -428,27 +428,12 @@ impl BuiltPackage {
Ok(())
}

/// Writes BuiltPackage to `output_dir`.
pub fn write_output(
&self,
minify: MinifyOpts,
pkg_name: &str,
output_dir: &Path,
) -> Result<()> {
if !output_dir.exists() {
fs::create_dir_all(output_dir)?;
}
// Place build artifacts into the output directory.
let bin_path = output_dir.join(pkg_name).with_extension("bin");

self.write_bytecode(&bin_path)?;

let program_abi_stem = format!("{pkg_name}-abi");
let program_abi_path = output_dir.join(program_abi_stem).with_extension("json");
/// Writes the ABI in JSON format to the given `path`.
pub fn write_json_abi(&self, path: &Path, minify: MinifyOpts) -> Result<()> {
match &self.program_abi {
ProgramABI::Fuel(program_abi) => {
if !program_abi.functions.is_empty() {
let file = File::create(program_abi_path)?;
let file = File::create(path)?;
let res = if minify.json_abi {
serde_json::to_writer(&file, &program_abi)
} else {
Expand All @@ -459,7 +444,7 @@ impl BuiltPackage {
}
ProgramABI::Evm(program_abi) => {
if !program_abi.is_empty() {
let file = File::create(program_abi_path)?;
let file = File::create(path)?;
let res = if minify.json_abi {
serde_json::to_writer(&file, &program_abi)
} else {
Expand All @@ -472,6 +457,28 @@ impl BuiltPackage {
ProgramABI::MidenVM(_) => (),
}

Ok(())
}

/// Writes BuiltPackage to `output_dir`.
pub fn write_output(
&self,
minify: MinifyOpts,
pkg_name: &str,
output_dir: &Path,
) -> Result<()> {
if !output_dir.exists() {
fs::create_dir_all(output_dir)?;
}
// Place build artifacts into the output directory.
let bin_path = output_dir.join(pkg_name).with_extension("bin");

self.write_bytecode(&bin_path)?;

let program_abi_stem = format!("{pkg_name}-abi");
let json_abi_path = output_dir.join(program_abi_stem).with_extension("json");
self.write_json_abi(&json_abi_path, minify.clone())?;

info!(" Bytecode size: {} bytes", self.bytecode.bytes.len());
// Additional ops required depending on the program type
match self.tree_type {
Expand Down Expand Up @@ -1814,10 +1821,10 @@ pub fn compile(
ProgramABI::Fuel(time_expr!(
"generate JSON ABI program",
"generate_json_abi",
fuel_json_abi::generate_json_abi_program(
&mut JsonAbiContext {
fuel_abi::generate_program_abi(
&mut AbiContext {
program: typed_program,
json_abi_with_callpaths: profile.json_abi_with_callpaths,
abi_with_callpaths: profile.json_abi_with_callpaths,
},
engines.te(),
engines.de(),
Expand All @@ -1841,7 +1848,7 @@ pub fn compile(
let abi = time_expr!(
"generate JSON ABI program",
"generate_json_abi",
evm_json_abi::generate_json_abi_program(typed_program, engines),
evm_abi::generate_abi_program(typed_program, engines),
Some(sway_build_config.clone()),
metrics
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use crate::{
Engines, TypeArgument, TypeEngine, TypeId, TypeInfo,
};

pub fn generate_json_abi_program(program: &TyProgram, engines: &Engines) -> EvmAbiResult {
pub fn generate_abi_program(program: &TyProgram, engines: &Engines) -> EvmAbiResult {
let type_engine = engines.te();
let decl_engine = engines.de();
match &program.kind {
TyProgramKind::Contract { abi_entries, .. } => abi_entries
.iter()
.map(|x| generate_json_abi_function(x, type_engine, decl_engine))
.map(|x| generate_abi_function(x, type_engine, decl_engine))
.collect(),
TyProgramKind::Script { main_function, .. }
| TyProgramKind::Predicate { main_function, .. } => {
vec![generate_json_abi_function(
vec![generate_abi_function(
main_function,
type_engine,
decl_engine,
Expand All @@ -28,7 +28,7 @@ pub fn generate_json_abi_program(program: &TyProgram, engines: &Engines) -> EvmA
}

/// Gives back a string that represents the type, considering what it resolves to
fn get_json_type_str(
fn get_type_str(
type_id: &TypeId,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
Expand All @@ -37,20 +37,20 @@ fn get_json_type_str(
if type_id.is_generic_parameter(type_engine, decl_engine, resolved_type_id) {
format!(
"generic {}",
json_abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
)
} else {
match (type_engine.get(*type_id), type_engine.get(resolved_type_id)) {
(TypeInfo::Custom { .. }, TypeInfo::Struct { .. }) => {
format!(
"struct {}",
json_abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
)
}
(TypeInfo::Custom { .. }, TypeInfo::Enum { .. }) => {
format!(
"enum {}",
json_abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
)
}
(TypeInfo::Tuple(fields), TypeInfo::Tuple(resolved_fields)) => {
Expand All @@ -68,19 +68,15 @@ fn get_json_type_str(
(TypeInfo::Custom { .. }, _) => {
format!(
"generic {}",
json_abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
abi_str(&type_engine.get(*type_id), type_engine, decl_engine)
)
}
_ => json_abi_str(&type_engine.get(*type_id), type_engine, decl_engine),
_ => abi_str(&type_engine.get(*type_id), type_engine, decl_engine),
}
}
}

pub fn json_abi_str(
type_info: &TypeInfo,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
) -> String {
pub fn abi_str(type_info: &TypeInfo, type_engine: &TypeEngine, decl_engine: &DeclEngine) -> String {
use TypeInfo::*;
match type_info {
Unknown => "unknown".into(),
Expand All @@ -100,7 +96,7 @@ pub fn json_abi_str(
Tuple(fields) => {
let field_strs = fields
.iter()
.map(|field| json_abi_str_type_arg(field, type_engine, decl_engine))
.map(|field| abi_str_type_arg(field, type_engine, decl_engine))
.collect::<Vec<String>>();
format!("({})", field_strs.join(", "))
}
Expand All @@ -123,30 +119,24 @@ pub fn json_abi_str(
Array(elem_ty, length) => {
format!(
"{}[{}]",
json_abi_str_type_arg(elem_ty, type_engine, decl_engine),
abi_str_type_arg(elem_ty, type_engine, decl_engine),
length.val()
)
}
Storage { .. } => "contract storage".into(),
RawUntypedPtr => "raw untyped ptr".into(),
RawUntypedSlice => "raw untyped slice".into(),
Ptr(ty) => {
format!(
"__ptr {}",
json_abi_str_type_arg(ty, type_engine, decl_engine)
)
format!("__ptr {}", abi_str_type_arg(ty, type_engine, decl_engine))
}
Slice(ty) => {
format!(
"__slice {}",
json_abi_str_type_arg(ty, type_engine, decl_engine)
)
format!("__slice {}", abi_str_type_arg(ty, type_engine, decl_engine))
}
Alias { ty, .. } => json_abi_str_type_arg(ty, type_engine, decl_engine),
Alias { ty, .. } => abi_str_type_arg(ty, type_engine, decl_engine),
}
}

pub fn json_abi_param_type(
pub fn abi_param_type(
type_info: &TypeInfo,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
Expand All @@ -167,7 +157,7 @@ pub fn json_abi_param_type(
Tuple(fields) => ethabi::ParamType::Tuple(
fields
.iter()
.map(|f| json_abi_param_type(&type_engine.get(f.type_id), type_engine, decl_engine))
.map(|f| abi_param_type(&type_engine.get(f.type_id), type_engine, decl_engine))
.collect::<Vec<ethabi::ParamType>>(),
),
Struct(decl_ref) => {
Expand All @@ -176,7 +166,7 @@ pub fn json_abi_param_type(
decl.fields
.iter()
.map(|f| {
json_abi_param_type(
abi_param_type(
&type_engine.get(f.type_argument.type_id),
type_engine,
decl_engine,
Expand All @@ -185,7 +175,7 @@ pub fn json_abi_param_type(
.collect::<Vec<ethabi::ParamType>>(),
)
}
Array(elem_ty, ..) => ethabi::ParamType::Array(Box::new(json_abi_param_type(
Array(elem_ty, ..) => ethabi::ParamType::Array(Box::new(abi_param_type(
&type_engine.get(elem_ty.type_id),
type_engine,
decl_engine,
Expand All @@ -194,7 +184,7 @@ pub fn json_abi_param_type(
}
}

pub(self) fn generate_json_abi_function(
pub(self) fn generate_abi_function(
fn_decl: &TyFunctionDecl,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
Expand All @@ -206,7 +196,7 @@ pub(self) fn generate_json_abi_function(
.map(|x| ethabi::Param {
name: x.name.to_string(),
kind: ethabi::ParamType::Address,
internal_type: Some(get_json_type_str(
internal_type: Some(get_type_str(
&x.type_argument.type_id,
type_engine,
decl_engine,
Expand All @@ -219,7 +209,7 @@ pub(self) fn generate_json_abi_function(
let output_type = ethabi::Param {
name: String::default(),
kind: ethabi::ParamType::Address,
internal_type: Some(get_json_type_str(
internal_type: Some(get_type_str(
&fn_decl.return_type.type_id,
type_engine,
decl_engine,
Expand All @@ -238,10 +228,10 @@ pub(self) fn generate_json_abi_function(
})
}

pub(self) fn json_abi_str_type_arg(
pub(self) fn abi_str_type_arg(
type_arg: &TypeArgument,
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
) -> String {
json_abi_str(&type_engine.get(type_arg.type_id), type_engine, decl_engine)
abi_str(&type_engine.get(type_arg.type_id), type_engine, decl_engine)
}
Loading