Skip to content

Commit

Permalink
Fixes for auto impl of AbiEncode; encoding version and better tests (#…
Browse files Browse the repository at this point in the history
…5481)

## Description

This PR is a continuation of #5306.

- I am fixing some code reviews issues that were raised in the other PR;
- I am incorporating the encoding version inside the JSON ABI as:

```json
          {
            "configurables": [],
             "encoding": "1",  <- look here
            "functions": [
              {
                "attributes": null,
                "inputs": [],
                "name": "main",
                "output": {
                  "name": "",
                  "type": 13,
                  "typeArguments": null
                }
              }
            ],
```

This field is a string to allow any kind of versioning we choose.

- This PR has also improved testing and making more explicit how each
type is being encoded.
## Dependencies

- [x] FuelLabs/fuel-abi-types#17

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
xunilrj committed Feb 1, 2024
1 parent 2ac7030 commit 913968f
Show file tree
Hide file tree
Showing 26 changed files with 298 additions and 118 deletions.
48 changes: 15 additions & 33 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ fuels-accounts = "0.54.0"
# Dependencies from the `forc-wallet` repository:
forc-wallet = "0.4.2"

# Dependencies from the `fuel-abi-types` repository:
fuel-abi-types = "0.4.0"

[workspace.package]
edition = "2021"
authors = ["Fuel Labs <contact@fuel.sh>"]
Expand Down
2 changes: 1 addition & 1 deletion forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ anyhow = "1"
cid = "0.10"
forc-tracing = { version = "0.49.1", path = "../forc-tracing" }
forc-util = { version = "0.49.1", path = "../forc-util" }
fuel-abi-types = "0.1"
fuel-abi-types = { workspace = true }
futures = "0.3"
git2 = { version = "0.17.2", features = [
"vendored-libgit2",
Expand Down
10 changes: 8 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use forc_util::{
default_output_directory, find_file_name, kebab_to_snake_case, print_compiling,
print_on_failure, print_warnings,
};
use fuel_abi_types::program_abi;
use fuel_abi_types::abi::program as program_abi;
use petgraph::{
self, dot,
visit::{Bfs, Dfs, EdgeRef, Walker},
Expand Down Expand Up @@ -1820,6 +1820,8 @@ pub fn compile(
metrics
);

const NEW_ENCODING_VERSION: &str = "1";

let mut program_abi = match pkg.target {
BuildTarget::Fuel => {
let mut types = vec![];
Expand All @@ -1833,7 +1835,11 @@ pub fn compile(
},
engines.te(),
engines.de(),
&mut types
&mut types,
profile
.experimental
.new_encoding
.then(|| NEW_ENCODING_VERSION.into()),
),
Some(sway_build_config.clone()),
metrics
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ forc-tracing = { version = "0.49.1", path = "../../forc-tracing" }
forc-tx = { version = "0.49.1", path = "../forc-tx" }
forc-util = { version = "0.49.1", path = "../../forc-util" }
forc-wallet = { workspace = true }
fuel-abi-types = "0.3"
fuel-abi-types = { workspace = true }
fuel-core-client = { workspace = true, features = ["subscriptions"] }
fuel-crypto = { workspace = true }
fuel-tx = { workspace = true, features = ["builder"] }
Expand Down
2 changes: 1 addition & 1 deletion forc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository.workspace = true
[dependencies]
anyhow = "1"
forc-pkg = { version = "0.49.1", path = "../forc-pkg" }
fuel-abi-types = "0.2"
fuel-abi-types = { workspace = true }
fuel-tx = { workspace = true, features = ["builder"] }
fuel-vm = { workspace = true, features = ["random"] }
rand = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Command {
pub tests: bool,

#[clap(long)]
/// Experimental flags for the "new encoding" feature
/// Experimental flag for the "new encoding" feature
pub experimental_new_encoding: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/contract_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Command {
pub salt: Salt,

#[clap(long)]
/// Experimental flags for the "new encoding" feature
/// Experimental flag for the "new encoding" feature
pub experimental_new_encoding: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/predicate_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Command {
pub build_profile: BuildProfile,

#[clap(long)]
/// Experimental flags for the "new encoding" feature
/// Experimental flag for the "new encoding" feature
pub experimental_new_encoding: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Command {
pub test_threads: Option<usize>,

#[clap(long)]
/// Experimental flags for the "new encoding" feature
/// Experimental flag for the "new encoding" feature
pub experimental_new_encoding: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ etk-asm = { package = "fuel-etk-asm", version = "0.3.1-dev", features = [
] }
etk-dasm = { package = "fuel-etk-dasm", version = "0.3.1-dev" }
etk-ops = { package = "fuel-etk-ops", version = "0.3.1-dev" }
fuel-abi-types = "0.1"
fuel-abi-types = { workspace = true }
fuel-vm = { workspace = true, features = ["serde"] }
graph-cycles = "0.1.0"
hashbrown = "0.13.1"
Expand Down
6 changes: 5 additions & 1 deletion sway-core/src/abi_generation/fuel_abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fuel_abi_types::program_abi;
use fuel_abi_types::abi::program as program_abi;
use sway_types::integer_bits::IntegerBits;

use crate::{
Expand All @@ -21,6 +21,7 @@ pub fn generate_program_abi(
type_engine: &TypeEngine,
decl_engine: &DeclEngine,
types: &mut Vec<program_abi::TypeDeclaration>,
encoding: Option<program_abi::Version>,
) -> program_abi::ProgramABI {
match &ctx.program.kind {
TyProgramKind::Contract { abi_entries, .. } => {
Expand All @@ -35,6 +36,7 @@ pub fn generate_program_abi(
let messages_types = generate_messages_types(ctx, type_engine, decl_engine, types);
let configurables = generate_configurables(ctx, type_engine, decl_engine, types);
program_abi::ProgramABI {
encoding,
types: types.to_vec(),
functions,
logged_types: Some(logged_types),
Expand All @@ -51,6 +53,7 @@ pub fn generate_program_abi(
let messages_types = generate_messages_types(ctx, type_engine, decl_engine, types);
let configurables = generate_configurables(ctx, type_engine, decl_engine, types);
program_abi::ProgramABI {
encoding,
types: types.to_vec(),
functions,
logged_types: Some(logged_types),
Expand All @@ -59,6 +62,7 @@ pub fn generate_program_abi(
}
}
_ => program_abi::ProgramABI {
encoding,
types: vec![],
functions: vec![],
logged_types: None,
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/asm_generation/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::EvmAbiResult;

#[derive(Clone, Debug)]
pub enum ProgramABI {
Fuel(fuel_abi_types::program_abi::ProgramABI),
Fuel(fuel_abi_types::abi::program::ProgramABI),
Evm(EvmAbiResult),
MidenVM(()),
}
11 changes: 3 additions & 8 deletions sway-core/src/semantic_analysis/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ impl TyProgram {
build_config: Option<&BuildConfig>,
) -> Result<Self, ErrorEmitted> {
let mut namespace = Namespace::init_root(initial_namespace);
let ctx =
TypeCheckContext::from_root(&mut namespace, engines).with_kind(parsed.kind.clone());

let ctx = if let Some(build_config) = build_config {
ctx.with_experimental_flags(build_config.experimental)
} else {
ctx
};
let ctx = TypeCheckContext::from_root(&mut namespace, engines)
.with_kind(parsed.kind.clone())
.with_experimental_flags(build_config.map(|x| x.experimental));

let ParseProgram { root, kind } = parsed;

Expand Down
6 changes: 5 additions & 1 deletion sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,11 @@ impl<'a> TypeCheckContext<'a> {
.insert_for_type(self.engines, type_id);
}

pub(crate) fn with_experimental_flags(self, experimental: ExperimentalFlags) -> Self {
pub(crate) fn with_experimental_flags(self, experimental: Option<ExperimentalFlags>) -> Self {
let Some(experimental) = experimental else {
return self;
};

Self {
experimental,
..self
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/transform/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
use sway_ast::Literal;
use sway_types::{
constants::{
ALLOW_DEAD_CODE_NAME, ALLOW_DEPRECATED_NAME, CFG_PROGRAM_TYPE_ARG_NAME, CFG_TARGET_ARG_NAME,
ALLOW_DEAD_CODE_NAME, ALLOW_DEPRECATED_NAME, CFG_EXPERIMENTAL_NEW_ENCODING,
CFG_PROGRAM_TYPE_ARG_NAME, CFG_TARGET_ARG_NAME,
},
Ident, Span, Spanned,
};
Expand Down Expand Up @@ -100,6 +101,7 @@ impl AttributeKind {
AttributeKind::Cfg => Some(vec![
CFG_TARGET_ARG_NAME.to_string(),
CFG_PROGRAM_TYPE_ARG_NAME.to_string(),
CFG_EXPERIMENTAL_NEW_ENCODING.to_string(),
]),
AttributeKind::Deprecated => None,
}
Expand Down
Loading

0 comments on commit 913968f

Please sign in to comment.