From 03b688172f65434370c8282eb1b697940416505a Mon Sep 17 00:00:00 2001 From: xunilrj Date: Wed, 19 Jun 2024 10:21:23 +0100 Subject: [PATCH 01/15] unit tests for std::Bytes buffer ownership --- sway-ir/src/error.rs | 20 ++++++++++++--- sway-ir/src/printer.rs | 47 ++++++++++++++++++++++++++--------- sway-ir/src/verify.rs | 50 +++++++++++++++++++++++++++++++------ sway-lib-std/src/bytes.sw | 52 ++++++++++++++++++++++++--------------- 4 files changed, 127 insertions(+), 42 deletions(-) diff --git a/sway-ir/src/error.rs b/sway-ir/src/error.rs index 44fa75b1892..62bc6b0e900 100644 --- a/sway-ir/src/error.rs +++ b/sway-ir/src/error.rs @@ -33,7 +33,7 @@ pub enum IrError { VerifyContractCallBadTypes(String), VerifyGepElementTypeNonPointer, VerifyGepFromNonPointer(String), - VerifyGepInconsistentTypes, + VerifyGepInconsistentTypes(String, Option), VerifyGepOnNonAggregate, VerifyGetNonExistentPointer, VerifyInsertElementOfIncorrectType, @@ -67,11 +67,21 @@ pub enum IrError { VerifyStoreToNonPointer(String), VerifyUntypedValuePassedToFunction, } +impl IrError { + pub(crate) fn get_problematic_value(&self) -> Option<&Value> { + match self { + Self::VerifyGepInconsistentTypes(_, v) => v.as_ref(), + _ => None, + } + } +} impl std::error::Error for IrError {} use std::fmt; +use crate::Value; + impl fmt::Display for IrError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self { @@ -191,8 +201,12 @@ impl fmt::Display for IrError { IrError::VerifyGepElementTypeNonPointer => { write!(f, "Verification failed: GEP on a non-pointer.") } - IrError::VerifyGepInconsistentTypes => { - write!(f, "Verification failed: Struct field type mismatch.") + IrError::VerifyGepInconsistentTypes(error, _) => { + write!( + f, + "Verification failed: Struct field type mismatch: ({}).", + error + ) } IrError::VerifyGepFromNonPointer(ty) => { write!( diff --git a/sway-ir/src/printer.rs b/sway-ir/src/printer.rs index f14286ccda9..b454bcc29fb 100644 --- a/sway-ir/src/printer.rs +++ b/sway-ir/src/printer.rs @@ -23,7 +23,7 @@ use crate::{ }; #[derive(Debug)] -enum Doc { +pub(crate) enum Doc { Empty, Space, Comma, @@ -42,7 +42,7 @@ enum Doc { } impl Doc { - fn text>(s: S) -> Self { + pub(crate) fn text>(s: S) -> Self { Doc::Text(s.into()) } @@ -50,7 +50,7 @@ impl Doc { Doc::Line(Box::new(doc)) } - fn text_line>(s: S) -> Self { + pub(crate) fn text_line>(s: S) -> Self { Doc::Line(Box::new(Doc::Text(s.into()))) } @@ -66,7 +66,7 @@ impl Doc { Doc::Parens(Box::new(Doc::list_sep(docs, Doc::Comma))) } - fn append(self, doc: Doc) -> Doc { + pub(crate) fn append(self, doc: Doc) -> Doc { match (&self, &doc) { (Doc::Empty, _) => doc, (_, Doc::Empty) => self, @@ -81,7 +81,7 @@ impl Doc { } } - fn build(self) -> String { + pub(crate) fn build(self) -> String { build_doc(self, 0) } } @@ -90,17 +90,32 @@ impl Doc { /// /// The output from this function must always be suitable for [crate::parser::parse]. pub fn to_string(context: &Context) -> String { + context_print(context, &|_, doc| doc) +} + +pub(crate) fn context_print(context: &Context, map_doc: &impl Fn(&Value, Doc) -> Doc) -> String { let mut md_namer = MetadataNamer::default(); context .modules .iter() .fold(Doc::Empty, |doc, (_, module)| { - doc.append(module_to_doc(context, &mut md_namer, module)) + doc.append(module_to_doc(context, &mut md_namer, module, map_doc)) }) .append(md_namer.to_doc(context)) .build() } +pub(crate) fn block_print( + context: &Context, + function: Function, + block: Block, + map_doc: &impl Fn(&Value, Doc) -> Doc, +) -> String { + let mut md_namer = MetadataNamer::default(); + let mut namer = Namer::new(function); + block_to_doc(context, &mut md_namer, &mut namer, &block, map_doc).build() +} + pub struct ModulePrinterResult; impl AnalysisResultT for ModulePrinterResult {} @@ -116,7 +131,8 @@ pub fn module_printer_pass( module_to_doc( context, &mut md_namer, - context.modules.get(module.0).unwrap() + context.modules.get(module.0).unwrap(), + &|_, doc| doc ) .append(md_namer.to_doc(context)) .build() @@ -132,7 +148,8 @@ pub fn module_print(context: &Context, _analyses: &AnalysisResults, module: Modu module_to_doc( context, &mut md_namer, - context.modules.get(module.0).unwrap() + context.modules.get(module.0).unwrap(), + &|_, doc| doc ) .append(md_namer.to_doc(context)) .build() @@ -148,7 +165,8 @@ pub fn function_print(context: &Context, function: Function) { context, &mut md_namer, &mut Namer::new(function), - context.functions.get(function.0).unwrap() + context.functions.get(function.0).unwrap(), + &|_, doc| doc ) .append(md_namer.to_doc(context)) .build() @@ -170,6 +188,7 @@ fn module_to_doc<'a>( context: &'a Context, md_namer: &mut MetadataNamer, module: &'a ModuleContent, + map_doc: &impl Fn(&Value, Doc) -> Doc, ) -> Doc { Doc::line(Doc::Text(format!( "{} {{", @@ -207,6 +226,7 @@ fn module_to_doc<'a>( md_namer, &mut Namer::new(*function), &context.functions[function.0], + map_doc, ) }) .collect(), @@ -268,6 +288,7 @@ fn function_to_doc<'a>( md_namer: &mut MetadataNamer, namer: &mut Namer, function: &'a FunctionContent, + map_doc: &impl Fn(&Value, Doc) -> Doc, ) -> Doc { let public = if function.is_public { "pub " } else { "" }; let entry = if function.is_entry { "entry " } else { "" }; @@ -373,7 +394,7 @@ fn function_to_doc<'a>( function .blocks .iter() - .map(|block| block_to_doc(context, md_namer, namer, block)) + .map(|block| block_to_doc(context, md_namer, namer, block, map_doc)) .collect(), Doc::line(Doc::Empty), ), @@ -389,6 +410,7 @@ fn block_to_doc( md_namer: &mut MetadataNamer, namer: &mut Namer, block: &Block, + mut map_doc: &impl Fn(&Value, Doc) -> Doc, ) -> Doc { let block_content = &context.blocks[block.0]; Doc::line( @@ -410,7 +432,10 @@ fn block_to_doc( .append(Doc::List( block .instruction_iter(context) - .map(|ins| instruction_to_doc(context, md_namer, namer, block, &ins)) + .map(|current_value| { + let mut doc = instruction_to_doc(context, md_namer, namer, block, ¤t_value); + (map_doc)(¤t_value, doc) + }) .collect(), )) } diff --git a/sway-ir/src/verify.rs b/sway-ir/src/verify.rs index af987ab4a25..3cbd99f813b 100644 --- a/sway-ir/src/verify.rs +++ b/sway-ir/src/verify.rs @@ -13,9 +13,10 @@ use crate::{ irtype::Type, local_var::LocalVar, metadata::{MetadataIndex, Metadatum}, + printer, value::{Value, ValueDatum}, AnalysisResult, AnalysisResultT, AnalysisResults, BinaryOpKind, Block, BlockArgument, - BranchToWithArgs, Module, Pass, PassMutability, ScopedPass, TypeOption, UnaryOpKind, + BranchToWithArgs, Doc, Module, Pass, PassMutability, ScopedPass, TypeOption, UnaryOpKind, }; pub struct ModuleVerifierResult; @@ -119,10 +120,32 @@ impl<'eng> Context<'eng> { } .verify_instructions(); - if r.is_err() { - println!("{}", self); - println!("{}", cur_function.get_name(self)); - println!("{}", cur_block.get_label(self)); + // Help to understand the verification failure + // If the error knows the problematic value, prints everything with the error highlighted, + // if not, print only the block to help pinpoint the issue + if let Err(error) = &r { + println!( + "Verification failed at {}::{}", + cur_function.get_name(self), + cur_block.get_label(self) + ); + + let block = if let Some(problematic_value) = error.get_problematic_value() { + printer::context_print(self, &|current_value: &Value, doc: Doc| { + if *current_value == *problematic_value { + doc.append(Doc::text_line(format!( + "\x1b[0;31m^ {}\x1b[0m", + error.to_string() + ))) + } else { + doc + } + }) + } else { + printer::block_print(self, cur_function, cur_block, &|_, doc| doc) + }; + + println!("{}", block); } r?; @@ -291,7 +314,7 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { base, elem_ptr_ty, indices, - } => self.verify_get_elem_ptr(base, elem_ptr_ty, indices)?, + } => self.verify_get_elem_ptr(&ins, base, elem_ptr_ty, indices)?, InstOp::GetLocal(local_var) => self.verify_get_local(local_var)?, InstOp::GetConfig(_, name) => self.verify_get_config(self.cur_module, name)?, InstOp::IntToPtr(value, ty) => self.verify_int_to_ptr(value, ty)?, @@ -731,6 +754,7 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { fn verify_get_elem_ptr( &self, + ins: &Value, base: &Value, elem_ptr_ty: &Type, indices: &[Value], @@ -747,7 +771,10 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { }; if indices.is_empty() { - return Err(IrError::VerifyGepInconsistentTypes); + return Err(IrError::VerifyGepInconsistentTypes( + "Empty Indices".into(), + Some(*base), + )); } // Fetch the field type from the vector of Values. If the value is a constant int then @@ -769,7 +796,14 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { }); if self.opt_ty_not_eq(&Some(elem_inner_ty), &index_ty) { - return Err(IrError::VerifyGepInconsistentTypes); + return Err(IrError::VerifyGepInconsistentTypes( + format!( + "Element type \"{}\" versus index type {:?}", + elem_inner_ty.as_string(self.context), + index_ty.map(|x| x.as_string(self.context)) + ), + Some(*ins), + )); } Ok(()) diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index b3efba7f4b5..8da07d1216b 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -74,14 +74,20 @@ impl From for RawBytes { /// assert(raw_bytes.capacity == 3); /// ``` fn from(slice: raw_slice) -> Self { + let cap = slice.number_of_bytes(); + let ptr = alloc_bytes(cap); + asm(to: ptr, from: slice.ptr(), cap: cap) { + mcp to from cap; + } Self { - ptr: slice.ptr(), - cap: slice.number_of_bytes(), + ptr, + cap, } } } /// A type used to represent raw bytes. +/// It has ownership over the bytes, stored in a heap-allocated buffer. pub struct Bytes { /// A barebones struct for the bytes. buf: RawBytes, @@ -915,29 +921,35 @@ impl Clone for Bytes { impl AbiEncode for Bytes { fn abi_encode(self, buffer: Buffer) -> Buffer { - let mut buffer = self.len.abi_encode(buffer); - - let mut i = 0; - while i < self.len { - let item = self.get(i).unwrap(); - buffer = item.abi_encode(buffer); - i += 1; - } - - buffer + self.as_raw_slice().abi_encode(buffer) } } impl AbiDecode for Bytes { fn abi_decode(ref mut buffer: BufferReader) -> Bytes { let len = u64::abi_decode(buffer); - let data = buffer.read_bytes(len); - Bytes { - buf: RawBytes { - ptr: data.ptr(), - cap: len, - }, - len, - } + let slice= buffer.read_bytes(len); + Bytes::from(slice) } } + +#[test] +fn ok_bytes_buffer_ownership() { + let mut original_array = [1u8, 2u8, 3u8, 4u8]; + let slice = raw_slice::from_parts::(__addr_of(original_array), 4); + + // Check Bytes duplicates the original slice + let mut bytes = Bytes::from(slice); + bytes.set(0, 5); + assert(original_array[0] == 1); + + // slice = [5, 2, 3, 4] + let encoded_slice = encode(bytes); + let encoded_bytes = Bytes::from(encoded_slice); + + // Check abi_decode duplicates the encoded slice + let mut bytes = abi_decode::(encoded_slice); + bytes.set(0, 6); + assert(encoded_bytes.get(0) == Some(5)); +} + From 96870f91c074fe672241e605348d5583bd072c0f Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 21 Jun 2024 08:33:04 +0100 Subject: [PATCH 02/15] send incomplete types to method app argument type check second pass --- .../src/language/ty/declaration/function.rs | 12 +++++-- .../typed_expression/method_application.rs | 34 ++++++++++++++----- sway-core/src/type_system/id.rs | 22 +++++++++--- sway-core/src/type_system/priv_prelude.rs | 2 +- sway-lib-std/src/bytes.sw | 3 +- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/sway-core/src/language/ty/declaration/function.rs b/sway-core/src/language/ty/declaration/function.rs index cf18cf0305e..c6242dc6526 100644 --- a/sway-core/src/language/ty/declaration/function.rs +++ b/sway-core/src/language/ty/declaration/function.rs @@ -602,9 +602,15 @@ impl TyFunctionSig { } pub fn is_concrete(&self, engines: &Engines) -> bool { - self.return_type.is_concrete(engines) - && self.parameters.iter().all(|p| p.is_concrete(engines)) - && self.type_parameters.iter().all(|p| p.is_concrete(engines)) + self.return_type.is_concrete(engines, IncludeNumeric::No) + && self + .parameters + .iter() + .all(|p| p.is_concrete(engines, IncludeNumeric::No)) + && self + .type_parameters + .iter() + .all(|p| p.is_concrete(engines, IncludeNumeric::No)) } /// Returns a String representing the function. diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs index a76a9339c19..64d43ddd5c7 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs @@ -46,11 +46,25 @@ pub(crate) fn type_check_method_application( .by_ref() .with_help_text("") .with_type_annotation(type_engine.insert(engines, TypeInfo::Unknown, None)); + // Ignore errors in method parameters // On the second pass we will throw the errors if they persist. let arg_handler = Handler::default(); let arg_opt = ty::TyExpression::type_check(&arg_handler, ctx, arg).ok(); + + // Check this type needs a second pass let has_errors = arg_handler.has_errors(); + let is_not_concrete = arg_opt + .as_ref() + .map(|x| { + x.return_type + .extract_inner_types(engines, IncludeSelf::Yes) + .iter() + .any(|x| !x.is_concrete(engines, IncludeNumeric::Yes)) + }) + .unwrap_or_default(); + let needs_second_pass = has_errors || is_not_concrete; + if index == 0 { // We want to emit errors in the self parameter and ignore TraitConstraintNotSatisfied with Placeholder // which may be recoverable on the second pass. @@ -66,7 +80,8 @@ pub(crate) fn type_check_method_application( }); handler.append(arg_handler); } - args_opt_buf.push_back((arg_opt, has_errors)); + + args_opt_buf.push_back((arg_opt, needs_second_pass)); } // resolve the method name to a typed function declaration and type_check @@ -114,6 +129,14 @@ pub(crate) fn type_check_method_application( } else { index }; + + let tid = method + .parameters + .get(param_index) + .unwrap() + .type_argument + .type_id; + // This arg_opt is None because it failed in the first pass. // We now try to type check it again, this time with the type annotation. let ctx = ctx @@ -121,14 +144,7 @@ pub(crate) fn type_check_method_application( .with_help_text( "Function application argument type must match function parameter type.", ) - .with_type_annotation( - method - .parameters - .get(param_index) - .unwrap() - .type_argument - .type_id, - ); + .with_type_annotation(tid); args_buf.push_back( ty::TyExpression::type_check(handler, ctx, arg) .unwrap_or_else(|err| ty::TyExpression::error(err, span.clone(), engines)), diff --git a/sway-core/src/type_system/id.rs b/sway-core/src/type_system/id.rs index 3c9fe98c480..ce3a124ac4d 100644 --- a/sway-core/src/type_system/id.rs +++ b/sway-core/src/type_system/id.rs @@ -32,6 +32,11 @@ pub enum IncludeSelf { No, } +pub enum IncludeNumeric { + Yes, + No, +} + /// A identifier to uniquely refer to our type terms #[derive(PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd, Debug)] pub struct TypeId(usize); @@ -480,17 +485,26 @@ impl TypeId { })) } - pub(crate) fn is_concrete(&self, engines: &Engines) -> bool { + pub(crate) fn is_concrete(&self, engines: &Engines, include_numeric: IncludeNumeric) -> bool { let nested_types = (*self).extract_nested_types(engines); - !nested_types.into_iter().any(|x| { - matches!( + !nested_types.into_iter().any(|x| match include_numeric { + IncludeNumeric::Yes => matches!( x, TypeInfo::UnknownGeneric { .. } | TypeInfo::Custom { .. } | TypeInfo::Placeholder(..) | TypeInfo::TraitType { .. } | TypeInfo::TypeParam(..) - ) + | TypeInfo::Numeric + ), + IncludeNumeric::No => matches!( + x, + TypeInfo::UnknownGeneric { .. } + | TypeInfo::Custom { .. } + | TypeInfo::Placeholder(..) + | TypeInfo::TraitType { .. } + | TypeInfo::TypeParam(..) + ), }) } diff --git a/sway-core/src/type_system/priv_prelude.rs b/sway-core/src/type_system/priv_prelude.rs index bb40d738d00..b28ac22152d 100644 --- a/sway-core/src/type_system/priv_prelude.rs +++ b/sway-core/src/type_system/priv_prelude.rs @@ -16,6 +16,6 @@ pub use super::{ type_parameter::TypeParameter, }, engine::TypeEngine, - id::{IncludeSelf, TypeId}, + id::{IncludeNumeric, IncludeSelf, TypeId}, info::{AbiEncodeSizeHint, AbiName, TypeInfo, TypeSourceInfo}, }; diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index 8da07d1216b..7723303640c 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -949,7 +949,6 @@ fn ok_bytes_buffer_ownership() { // Check abi_decode duplicates the encoded slice let mut bytes = abi_decode::(encoded_slice); - bytes.set(0, 6); - assert(encoded_bytes.get(0) == Some(5)); + __log(bytes); } From edbb6b8ebef6c0a7072267ecb23b8f85c5173334 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 08:24:57 +0100 Subject: [PATCH 03/15] fix Bytes test --- sway-lib-std/src/bytes.sw | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index 7723303640c..93bef23925b 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -943,12 +943,17 @@ fn ok_bytes_buffer_ownership() { bytes.set(0, 5); assert(original_array[0] == 1); - // slice = [5, 2, 3, 4] + // At this point, slice equals [5, 2, 3, 4] let encoded_slice = encode(bytes); - let encoded_bytes = Bytes::from(encoded_slice); - // Check abi_decode duplicates the encoded slice + // `Bytes` should duplicate the underlying buffer, + // so when we write to it, it should not change + // `encoded_slice` let mut bytes = abi_decode::(encoded_slice); - __log(bytes); + bytes.set(0, 6); + assert(bytes.get(0) == Some(6)); + + let mut bytes = abi_decode::(encoded_slice); + assert(bytes.get(0) == Some(5)); } From d997cd3b03a1b6859f0f955dae77cb8a88397fd4 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 08:55:04 +0100 Subject: [PATCH 04/15] tests for String and Vec --- sway-lib-std/src/bytes.sw | 2 +- sway-lib-std/src/string.sw | 33 +++++++++++++++++++++++++ sway-lib-std/src/vec.sw | 49 +++++++++++++++++++++++++++----------- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index 93bef23925b..e8269597ea3 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -928,7 +928,7 @@ impl AbiEncode for Bytes { impl AbiDecode for Bytes { fn abi_decode(ref mut buffer: BufferReader) -> Bytes { let len = u64::abi_decode(buffer); - let slice= buffer.read_bytes(len); + let slice = buffer.read_bytes(len); Bytes::from(slice) } } diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 97b6cf76df3..69e33228577 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -352,3 +352,36 @@ impl AbiDecode for String { } } } + +#[test] +fn ok_string_buffer_ownership() { + use ::option::Option::Some; + + let mut string_slice = "hi"; + let mut string = String::from_ascii_str(string_slice); + + // change first char to 'H' + let mut bytes = string.as_bytes(); + bytes.set(0, 72); + + // Check the string changed, but not the original slice + assert(string.as_bytes().get(0) == Some(72)); + assert(string_slice == "hi"); + + // encoded bytes should be Hi + let encoded_bytes = encode(string); + let string = abi_decode::(encoded_bytes); + + // change first char to 'P' + string.as_bytes().set(0, 80); + + // Check decoded string is "Pi" + assert(string.as_bytes().get(0) == Some(80)); + + // Check original string slice has not changed + assert(string_slice == "hi"); + + // Check encoded bytes has not changed + let mut bytes = abi_decode::(encoded_bytes); + assert(bytes.get(0) == Some(72)); +} \ No newline at end of file diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 491e8a728e5..04b63d30025 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -1,7 +1,7 @@ //! A vector type for dynamically sized arrays outside of storage. library; -use ::alloc::{alloc, realloc}; +use ::alloc::{alloc, alloc_bytes, realloc}; use ::assert::assert; use ::option::Option::{self, *}; use ::convert::From; @@ -131,9 +131,14 @@ impl RawVec { impl From for RawVec { fn from(slice: raw_slice) -> Self { + let cap = slice.number_of_bytes(); + let ptr = alloc_bytes(cap); + asm(to: ptr, from: slice.ptr(), cap: cap) { + mcp to from cap; + } Self { - ptr: slice.ptr(), - cap: slice.len::(), + ptr, + cap, } } } @@ -672,17 +677,8 @@ where { fn abi_decode(ref mut buffer: BufferReader) -> Vec { let len = u64::abi_decode(buffer); - - let mut v = Vec::with_capacity(len); - - let mut i = 0; - while i < len { - let item = T::abi_decode(buffer); - v.push(item); - i += 1; - } - - v + let slice = buffer.read_bytes(len); + Vec::::from(slice) } } @@ -702,3 +698,28 @@ impl Iterator for VecIter { self.values.get(self.index - 1) } } + + +#[test] +fn ok_vec_buffer_ownership() { + let mut original_array = [1u8, 2u8, 3u8, 4u8]; + let slice = raw_slice::from_parts::(__addr_of(original_array), 4); + + // Check Vec duplicates the original slice + let mut bytes = Vec::::from(slice); + bytes.set(0, 5); + assert(original_array[0] == 1); + + // At this point, slice equals [5, 2, 3, 4] + let encoded_slice = encode(bytes); + + // `Vec` should duplicate the underlying buffer, + // so when we write to it, it should not change + // `encoded_slice` + let mut bytes = abi_decode::>(encoded_slice); + bytes.set(0, 6); + assert(bytes.get(0) == Some(6)); + + let mut bytes = abi_decode::>(encoded_slice); + assert(bytes.get(0) == Some(5)); +} From ff5ec8b09e4623d415083ad646beb1e120dc3582 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 09:06:00 +0100 Subject: [PATCH 05/15] forc fmt std lib --- sway-lib-std/src/bytes.sw | 8 ++------ sway-lib-std/src/string.sw | 6 +++--- sway-lib-std/src/vec.sw | 8 ++------ 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index e8269597ea3..3b81fd9bb32 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -79,10 +79,7 @@ impl From for RawBytes { asm(to: ptr, from: slice.ptr(), cap: cap) { mcp to from cap; } - Self { - ptr, - cap, - } + Self { ptr, cap } } } @@ -945,7 +942,7 @@ fn ok_bytes_buffer_ownership() { // At this point, slice equals [5, 2, 3, 4] let encoded_slice = encode(bytes); - + // `Bytes` should duplicate the underlying buffer, // so when we write to it, it should not change // `encoded_slice` @@ -956,4 +953,3 @@ fn ok_bytes_buffer_ownership() { let mut bytes = abi_decode::(encoded_slice); assert(bytes.get(0) == Some(5)); } - diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 69e33228577..4c1d69a7148 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -357,7 +357,7 @@ impl AbiDecode for String { fn ok_string_buffer_ownership() { use ::option::Option::Some; - let mut string_slice = "hi"; + let mut string_slice = "hi"; let mut string = String::from_ascii_str(string_slice); // change first char to 'H' @@ -377,11 +377,11 @@ fn ok_string_buffer_ownership() { // Check decoded string is "Pi" assert(string.as_bytes().get(0) == Some(80)); - + // Check original string slice has not changed assert(string_slice == "hi"); // Check encoded bytes has not changed let mut bytes = abi_decode::(encoded_bytes); assert(bytes.get(0) == Some(72)); -} \ No newline at end of file +} diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 04b63d30025..06c9abbca35 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -136,10 +136,7 @@ impl From for RawVec { asm(to: ptr, from: slice.ptr(), cap: cap) { mcp to from cap; } - Self { - ptr, - cap, - } + Self { ptr, cap } } } @@ -699,7 +696,6 @@ impl Iterator for VecIter { } } - #[test] fn ok_vec_buffer_ownership() { let mut original_array = [1u8, 2u8, 3u8, 4u8]; @@ -712,7 +708,7 @@ fn ok_vec_buffer_ownership() { // At this point, slice equals [5, 2, 3, 4] let encoded_slice = encode(bytes); - + // `Vec` should duplicate the underlying buffer, // so when we write to it, it should not change // `encoded_slice` From 8c1db081c9463004757676ad427abdfed7164edf Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 09:08:41 +0100 Subject: [PATCH 06/15] fixing clippy issues --- sway-ir/src/printer.rs | 4 ++-- sway-ir/src/verify.rs | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/sway-ir/src/printer.rs b/sway-ir/src/printer.rs index b454bcc29fb..19b90c92861 100644 --- a/sway-ir/src/printer.rs +++ b/sway-ir/src/printer.rs @@ -410,7 +410,7 @@ fn block_to_doc( md_namer: &mut MetadataNamer, namer: &mut Namer, block: &Block, - mut map_doc: &impl Fn(&Value, Doc) -> Doc, + map_doc: &impl Fn(&Value, Doc) -> Doc, ) -> Doc { let block_content = &context.blocks[block.0]; Doc::line( @@ -433,7 +433,7 @@ fn block_to_doc( block .instruction_iter(context) .map(|current_value| { - let mut doc = instruction_to_doc(context, md_namer, namer, block, ¤t_value); + let doc = instruction_to_doc(context, md_namer, namer, block, ¤t_value); (map_doc)(¤t_value, doc) }) .collect(), diff --git a/sway-ir/src/verify.rs b/sway-ir/src/verify.rs index 3cbd99f813b..3a8777289e3 100644 --- a/sway-ir/src/verify.rs +++ b/sway-ir/src/verify.rs @@ -133,10 +133,7 @@ impl<'eng> Context<'eng> { let block = if let Some(problematic_value) = error.get_problematic_value() { printer::context_print(self, &|current_value: &Value, doc: Doc| { if *current_value == *problematic_value { - doc.append(Doc::text_line(format!( - "\x1b[0;31m^ {}\x1b[0m", - error.to_string() - ))) + doc.append(Doc::text_line(format!("\x1b[0;31m^ {}\x1b[0m", error))) } else { doc } From 4bac8b5ff328689fbb87b1456729ca9f213654b2 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 09:38:54 +0100 Subject: [PATCH 07/15] fix Vec AbiDecode --- sway-lib-std/src/vec.sw | 15 ++++++++++++--- .../test_programs/bytes_inline_tests/src/main.sw | 4 ++-- .../test_programs/vec_inline_tests/src/main.sw | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 06c9abbca35..2131593b62a 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -131,7 +131,7 @@ impl RawVec { impl From for RawVec { fn from(slice: raw_slice) -> Self { - let cap = slice.number_of_bytes(); + let cap = slice.len::(); let ptr = alloc_bytes(cap); asm(to: ptr, from: slice.ptr(), cap: cap) { mcp to from cap; @@ -674,8 +674,17 @@ where { fn abi_decode(ref mut buffer: BufferReader) -> Vec { let len = u64::abi_decode(buffer); - let slice = buffer.read_bytes(len); - Vec::::from(slice) + + let mut v = Vec::with_capacity(len); + + let mut i = 0; + while i < len { + let item = T::abi_decode(buffer); + v.push(item); + i += 1; + } + + v } } diff --git a/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw index 3d69727a4a2..d5107addd94 100644 --- a/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw @@ -888,7 +888,7 @@ fn bytes_from_raw_slice() { }; let mut bytes = Bytes::from(slice); - assert(bytes.ptr() == slice.ptr()); + assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer assert(bytes.len() == slice.number_of_bytes()); } @@ -921,7 +921,7 @@ fn bytes_raw_slice_into() { let bytes: Bytes = slice.into(); - assert(bytes.ptr() == slice.ptr()); + assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer assert(bytes.len() == slice.number_of_bytes()); } diff --git a/test/src/in_language_tests/test_programs/vec_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/vec_inline_tests/src/main.sw index 5ac6a8fef32..78e2df5d1e5 100644 --- a/test/src/in_language_tests/test_programs/vec_inline_tests/src/main.sw +++ b/test/src/in_language_tests/test_programs/vec_inline_tests/src/main.sw @@ -641,7 +641,7 @@ fn vec_from_raw_slice() { }; let mut vec: Vec = Vec::from(slice); - assert(vec.ptr() == slice.ptr()); + assert(vec.ptr() != slice.ptr()); // Vec should own its buffer assert(vec.len() == slice.len::()); } @@ -683,7 +683,7 @@ fn vec_raw_slice_into() { let vec: Vec = slice.into(); - assert(vec.ptr() == slice.ptr()); + assert(vec.ptr() != slice.ptr()); // Vec should own its buffer assert(vec.len() == slice.len::()); } From dd56eda2831ecb9ae722716b34d0bb1e71d046ff Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 10:09:53 +0100 Subject: [PATCH 08/15] fix Vec buffer ownership --- sway-lib-std/src/vec.sw | 14 +- .../json_abi_oracle_new_encoding.json | 120 ++++++++++++++++++ .../call_basic_storage/src/main.sw | 2 +- 3 files changed, 130 insertions(+), 6 deletions(-) diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 2131593b62a..32e55ddfe80 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -1,7 +1,7 @@ //! A vector type for dynamically sized arrays outside of storage. library; -use ::alloc::{alloc, alloc_bytes, realloc}; +use ::alloc::{alloc, realloc}; use ::assert::assert; use ::option::Option::{self, *}; use ::convert::From; @@ -132,9 +132,13 @@ impl RawVec { impl From for RawVec { fn from(slice: raw_slice) -> Self { let cap = slice.len::(); - let ptr = alloc_bytes(cap); - asm(to: ptr, from: slice.ptr(), cap: cap) { - mcp to from cap; + let ptr = alloc::(cap); + asm( + to: ptr, + from: slice.ptr(), + qty_bytes: slice.number_of_bytes(), + ) { + mcp to from qty_bytes; } Self { ptr, cap } } @@ -674,7 +678,7 @@ where { fn abi_decode(ref mut buffer: BufferReader) -> Vec { let len = u64::abi_decode(buffer); - + let mut v = Vec::with_capacity(len); let mut i = 0; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json index b6a290205a9..b17832db76c 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json @@ -7,7 +7,15 @@ "typeArguments": null }, "name": "BOOL", +<<<<<<< HEAD "offset": 6912 +======= +<<<<<<< HEAD + "offset": 6848 +======= + "offset": 6936 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -16,7 +24,15 @@ "typeArguments": null }, "name": "U8", +<<<<<<< HEAD "offset": 7048 +======= +<<<<<<< HEAD + "offset": 6984 +======= + "offset": 7072 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -25,7 +41,15 @@ "typeArguments": null }, "name": "ANOTHER_U8", +<<<<<<< HEAD "offset": 6840 +======= +<<<<<<< HEAD + "offset": 6776 +======= + "offset": 6864 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -34,7 +58,15 @@ "typeArguments": null }, "name": "U16", +<<<<<<< HEAD "offset": 6992 +======= +<<<<<<< HEAD + "offset": 6928 +======= + "offset": 7016 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -43,7 +75,15 @@ "typeArguments": null }, "name": "U32", +<<<<<<< HEAD "offset": 7032 +======= +<<<<<<< HEAD + "offset": 6968 +======= + "offset": 7056 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -52,7 +92,15 @@ "typeArguments": null }, "name": "U64", +<<<<<<< HEAD "offset": 7040 +======= +<<<<<<< HEAD + "offset": 6976 +======= + "offset": 7064 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -61,7 +109,15 @@ "typeArguments": null }, "name": "U256", +<<<<<<< HEAD "offset": 7000 +======= +<<<<<<< HEAD + "offset": 6936 +======= + "offset": 7024 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -70,7 +126,15 @@ "typeArguments": null }, "name": "B256", +<<<<<<< HEAD "offset": 6880 +======= +<<<<<<< HEAD + "offset": 6816 +======= + "offset": 6904 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -79,7 +143,15 @@ "typeArguments": [] }, "name": "CONFIGURABLE_STRUCT", +<<<<<<< HEAD "offset": 6952 +======= +<<<<<<< HEAD + "offset": 6888 +======= + "offset": 6976 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -88,7 +160,15 @@ "typeArguments": [] }, "name": "CONFIGURABLE_ENUM_A", +<<<<<<< HEAD "offset": 6920 +======= +<<<<<<< HEAD + "offset": 6856 +======= + "offset": 6944 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -97,7 +177,15 @@ "typeArguments": [] }, "name": "CONFIGURABLE_ENUM_B", +<<<<<<< HEAD "offset": 6936 +======= +<<<<<<< HEAD + "offset": 6872 +======= + "offset": 6960 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -106,7 +194,15 @@ "typeArguments": null }, "name": "ARRAY_BOOL", +<<<<<<< HEAD "offset": 6848 +======= +<<<<<<< HEAD + "offset": 6784 +======= + "offset": 6872 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -115,7 +211,15 @@ "typeArguments": null }, "name": "ARRAY_U64", +<<<<<<< HEAD "offset": 6856 +======= +<<<<<<< HEAD + "offset": 6792 +======= + "offset": 6880 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -124,7 +228,15 @@ "typeArguments": null }, "name": "TUPLE_BOOL_U64", +<<<<<<< HEAD "offset": 6976 +======= +<<<<<<< HEAD + "offset": 6912 +======= + "offset": 7000 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) }, { "configurableType": { @@ -133,7 +245,15 @@ "typeArguments": null }, "name": "STR_4", +<<<<<<< HEAD "offset": 6968 +======= +<<<<<<< HEAD + "offset": 6904 +======= + "offset": 6992 +>>>>>>> ef917df9a (fix Vec buffer ownership) +>>>>>>> 085e75b03 (fix Vec buffer ownership) } ], "encoding": "1", diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw index d3e66648a08..8afdfc06f97 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x186c989267434c784f7785be57b0db0a490ec382bbf98a6108a840c63b99be99; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x9fd665bb5b0ff768013b7f9b08f76b762bdc7426b11d76bd023c53433e9d21ed; +const CONTRACT_ID = 0xc98486039347d1f5fa68e3c557565aa8da0b9ca1b74207404e64e50afe0766c9; fn main() -> u64 { let addr = abi(BasicStorage, CONTRACT_ID); From 79ca7116ba4bfe691cb96220532e6559b86cdc50 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 10:22:12 +0100 Subject: [PATCH 09/15] fix rebase issues --- .../json_abi_oracle_new_encoding.json | 592 ++++++++++-------- .../call_basic_storage/src/main.sw | 2 +- 2 files changed, 327 insertions(+), 267 deletions(-) diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json index b17832db76c..749153669f7 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json @@ -1,12 +1,13 @@ { - "configurables": [ - { - "configurableType": { - "name": "", - "type": 5, - "typeArguments": null - }, - "name": "BOOL", + "configurables": [ + { + "configurableType": { + "name": "", + "type": 5, + "typeArguments": null + }, + "name": "BOOL", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6912 ======= @@ -16,14 +17,18 @@ "offset": 6936 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 13, - "typeArguments": null - }, - "name": "U8", +======= + "offset": 6856 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 13, + "typeArguments": null + }, + "name": "U8", +<<<<<<< HEAD <<<<<<< HEAD "offset": 7048 ======= @@ -33,14 +38,18 @@ "offset": 7072 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 13, - "typeArguments": null - }, - "name": "ANOTHER_U8", +======= + "offset": 6992 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 13, + "typeArguments": null + }, + "name": "ANOTHER_U8", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6840 ======= @@ -50,14 +59,18 @@ "offset": 6864 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 9, - "typeArguments": null - }, - "name": "U16", +======= + "offset": 6784 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 9, + "typeArguments": null + }, + "name": "U16", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6992 ======= @@ -67,14 +80,18 @@ "offset": 7016 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 11, - "typeArguments": null - }, - "name": "U32", +======= + "offset": 6936 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 11, + "typeArguments": null + }, + "name": "U32", +<<<<<<< HEAD <<<<<<< HEAD "offset": 7032 ======= @@ -84,14 +101,18 @@ "offset": 7056 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 11, - "typeArguments": null - }, - "name": "U64", +======= + "offset": 6976 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 11, + "typeArguments": null + }, + "name": "U64", +<<<<<<< HEAD <<<<<<< HEAD "offset": 7040 ======= @@ -101,14 +122,18 @@ "offset": 7064 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 10, - "typeArguments": null - }, - "name": "U256", +======= + "offset": 6984 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 10, + "typeArguments": null + }, + "name": "U256", +<<<<<<< HEAD <<<<<<< HEAD "offset": 7000 ======= @@ -118,14 +143,18 @@ "offset": 7024 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 4, - "typeArguments": null - }, - "name": "B256", +======= + "offset": 6944 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 4, + "typeArguments": null + }, + "name": "B256", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6880 ======= @@ -135,14 +164,18 @@ "offset": 6904 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 8, - "typeArguments": [] - }, - "name": "CONFIGURABLE_STRUCT", +======= + "offset": 6824 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 8, + "typeArguments": [] + }, + "name": "CONFIGURABLE_STRUCT", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6952 ======= @@ -152,14 +185,18 @@ "offset": 6976 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 6, - "typeArguments": [] - }, - "name": "CONFIGURABLE_ENUM_A", +======= + "offset": 6896 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 6, + "typeArguments": [] + }, + "name": "CONFIGURABLE_ENUM_A", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6920 ======= @@ -169,14 +206,18 @@ "offset": 6944 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 6, - "typeArguments": [] - }, - "name": "CONFIGURABLE_ENUM_B", +======= + "offset": 6864 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 6, + "typeArguments": [] + }, + "name": "CONFIGURABLE_ENUM_B", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6936 ======= @@ -186,14 +227,18 @@ "offset": 6960 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 2, - "typeArguments": null - }, - "name": "ARRAY_BOOL", +======= + "offset": 6880 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 2, + "typeArguments": null + }, + "name": "ARRAY_BOOL", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6848 ======= @@ -203,14 +248,18 @@ "offset": 6872 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 3, - "typeArguments": null - }, - "name": "ARRAY_U64", +======= + "offset": 6792 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 3, + "typeArguments": null + }, + "name": "ARRAY_U64", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6856 ======= @@ -220,14 +269,18 @@ "offset": 6880 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 1, - "typeArguments": null - }, - "name": "TUPLE_BOOL_U64", +======= + "offset": 6800 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 1, + "typeArguments": null + }, + "name": "TUPLE_BOOL_U64", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6976 ======= @@ -237,14 +290,18 @@ "offset": 7000 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - }, - { - "configurableType": { - "name": "", - "type": 7, - "typeArguments": null - }, - "name": "STR_4", +======= + "offset": 6920 +>>>>>>> 51fa76df5 (fix rebase issues) + }, + { + "configurableType": { + "name": "", + "type": 7, + "typeArguments": null + }, + "name": "STR_4", +<<<<<<< HEAD <<<<<<< HEAD "offset": 6968 ======= @@ -254,152 +311,155 @@ "offset": 6992 >>>>>>> ef917df9a (fix Vec buffer ownership) >>>>>>> 085e75b03 (fix Vec buffer ownership) - } - ], - "encoding": "1", - "functions": [ - { - "attributes": null, - "inputs": [], - "name": "main", - "output": { - "name": "", - "type": 0, - "typeArguments": null - } - } - ], - "loggedTypes": [], - "messagesTypes": [], - "types": [ - { - "components": [], - "type": "()", - "typeId": 0, - "typeParameters": null - }, - { - "components": [ - { - "name": "__tuple_element", - "type": 5, - "typeArguments": null - }, - { - "name": "__tuple_element", - "type": 12, - "typeArguments": null - } - ], - "type": "(_, _)", - "typeId": 1, - "typeParameters": null - }, - { - "components": [ - { - "name": "__array_element", - "type": 5, - "typeArguments": null - } - ], - "type": "[_; 3]", - "typeId": 2, - "typeParameters": null - }, - { - "components": [ - { - "name": "__array_element", - "type": 12, - "typeArguments": null - } - ], - "type": "[_; 3]", - "typeId": 3, - "typeParameters": null - }, - { - "components": null, - "type": "b256", - "typeId": 4, - "typeParameters": null - }, - { - "components": null, - "type": "bool", - "typeId": 5, - "typeParameters": null - }, - { - "components": [ - { - "name": "A", - "type": 5, - "typeArguments": null - }, - { - "name": "B", - "type": 12, - "typeArguments": null - } +======= + "offset": 6912 +>>>>>>> 51fa76df5 (fix rebase issues) + } ], - "type": "enum ConfigurableEnum", - "typeId": 6, - "typeParameters": null - }, - { - "components": null, - "type": "str[4]", - "typeId": 7, - "typeParameters": null - }, - { - "components": [ - { - "name": "a", - "type": 5, - "typeArguments": null - }, - { - "name": "b", - "type": 12, - "typeArguments": null - } + "encoding": "1", + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } ], - "type": "struct ConfigurableStruct", - "typeId": 8, - "typeParameters": null - }, - { - "components": null, - "type": "u16", - "typeId": 9, - "typeParameters": null - }, - { - "components": null, - "type": "u256", - "typeId": 10, - "typeParameters": null - }, - { - "components": null, - "type": "u32", - "typeId": 11, - "typeParameters": null - }, - { - "components": null, - "type": "u64", - "typeId": 12, - "typeParameters": null - }, - { - "components": null, - "type": "u8", - "typeId": 13, - "typeParameters": null - } - ] + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": [], + "type": "()", + "typeId": 0, + "typeParameters": null + }, + { + "components": [ + { + "name": "__tuple_element", + "type": 5, + "typeArguments": null + }, + { + "name": "__tuple_element", + "type": 12, + "typeArguments": null + } + ], + "type": "(_, _)", + "typeId": 1, + "typeParameters": null + }, + { + "components": [ + { + "name": "__array_element", + "type": 5, + "typeArguments": null + } + ], + "type": "[_; 3]", + "typeId": 2, + "typeParameters": null + }, + { + "components": [ + { + "name": "__array_element", + "type": 12, + "typeArguments": null + } + ], + "type": "[_; 3]", + "typeId": 3, + "typeParameters": null + }, + { + "components": null, + "type": "b256", + "typeId": 4, + "typeParameters": null + }, + { + "components": null, + "type": "bool", + "typeId": 5, + "typeParameters": null + }, + { + "components": [ + { + "name": "A", + "type": 5, + "typeArguments": null + }, + { + "name": "B", + "type": 12, + "typeArguments": null + } + ], + "type": "enum ConfigurableEnum", + "typeId": 6, + "typeParameters": null + }, + { + "components": null, + "type": "str[4]", + "typeId": 7, + "typeParameters": null + }, + { + "components": [ + { + "name": "a", + "type": 5, + "typeArguments": null + }, + { + "name": "b", + "type": 12, + "typeArguments": null + } + ], + "type": "struct ConfigurableStruct", + "typeId": 8, + "typeParameters": null + }, + { + "components": null, + "type": "u16", + "typeId": 9, + "typeParameters": null + }, + { + "components": null, + "type": "u256", + "typeId": 10, + "typeParameters": null + }, + { + "components": null, + "type": "u32", + "typeId": 11, + "typeParameters": null + }, + { + "components": null, + "type": "u64", + "typeId": 12, + "typeParameters": null + }, + { + "components": null, + "type": "u8", + "typeId": 13, + "typeParameters": null + } + ] } \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw index 8afdfc06f97..cb0df5febe8 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x186c989267434c784f7785be57b0db0a490ec382bbf98a6108a840c63b99be99; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xc98486039347d1f5fa68e3c557565aa8da0b9ca1b74207404e64e50afe0766c9; +const CONTRACT_ID = 0xe82914343e787908e5661aeaea720e48248f9d99e14d57677936164fbda70f69; fn main() -> u64 { let addr = abi(BasicStorage, CONTRACT_ID); From 4b43dd130c5512bde1f0b3ad20212af930a498e3 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sat, 22 Jun 2024 12:55:18 +0100 Subject: [PATCH 10/15] improving std lib code --- sway-lib-core/src/ops.sw | 21 ++++++++++++++ sway-lib-core/src/raw_slice.sw | 2 ++ sway-lib-std/src/bytes.sw | 4 +-- sway-lib-std/src/string.sw | 53 ++++++++++++---------------------- sway-lib-std/src/vec.sw | 8 ++--- 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/sway-lib-core/src/ops.sw b/sway-lib-core/src/ops.sw index 1229ca8e649..c34e30099d1 100644 --- a/sway-lib-core/src/ops.sw +++ b/sway-lib-core/src/ops.sw @@ -608,6 +608,27 @@ impl Eq for raw_ptr { } } +impl Eq for raw_slice { + fn eq(self, other: Self) -> bool { + let (l_ptr, l_len) = asm(l: self) { + l: (raw_ptr, u64) + }; + + let (r_ptr, r_len) = asm(l: self) { + r: (raw_ptr, u64) + }; + + if l_len != r_len { + return false; + } + + asm(result, r2: l_ptr, r3: r_ptr, r4: l_len) { + meq result r2 r3 r4; + result: bool + } + } +} + /// Trait to evaluate if one value is greater or less than another of the same type. pub trait Ord { /// Evaluates if one value of the same type is greater than another. diff --git a/sway-lib-core/src/raw_slice.sw b/sway-lib-core/src/raw_slice.sw index 13bd0affb90..90c684e9261 100644 --- a/sway-lib-core/src/raw_slice.sw +++ b/sway-lib-core/src/raw_slice.sw @@ -160,3 +160,5 @@ impl raw_slice { into_parts(self).1 } } + + diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index 3b81fd9bb32..e6363fd2ad4 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -76,8 +76,8 @@ impl From for RawBytes { fn from(slice: raw_slice) -> Self { let cap = slice.number_of_bytes(); let ptr = alloc_bytes(cap); - asm(to: ptr, from: slice.ptr(), cap: cap) { - mcp to from cap; + if cap > 0 { + slice.ptr().copy_to::(ptr, cap); } Self { ptr, cap } } diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 4c1d69a7148..4bd93662abc 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -138,9 +138,9 @@ impl String { pub fn from_ascii_str(s: str) -> Self { let str_size = s.len(); let str_ptr = s.as_ptr(); - + let slice = raw_slice::from_parts::(str_ptr, str_size); Self { - bytes: Bytes::from(raw_slice::from_parts::(str_ptr, str_size)), + bytes: Bytes::from(slice), } } @@ -234,18 +234,6 @@ impl String { } } -impl From for String { - fn from(b: Bytes) -> Self { - Self { bytes: b } - } -} - -impl From for Bytes { - fn from(s: String) -> Bytes { - s.as_bytes() - } -} - impl AsRawSlice for String { /// Returns a raw slice to all of the elements in the string. fn as_raw_slice(self) -> raw_slice { @@ -308,13 +296,25 @@ impl From for raw_slice { /// } /// ``` fn from(s: String) -> raw_slice { - raw_slice::from(s.as_bytes()) + s.as_raw_slice() + } +} + +impl From for String { + fn from(b: Bytes) -> Self { + b.as_raw_slice().into() + } +} + +impl From for Bytes { + fn from(s: String) -> Bytes { + s.as_raw_slice().into() } } impl Eq for String { fn eq(self, other: Self) -> bool { - self.bytes == other.as_bytes() + self.as_raw_slice() == other.as_raw_slice() } } @@ -326,30 +326,13 @@ impl Hash for String { impl AbiEncode for String { fn abi_encode(self, buffer: Buffer) -> Buffer { - // Encode the length - let mut buffer = self.bytes.len().abi_encode(buffer); - - // Encode each byte of the string - let mut i = 0; - while i < self.bytes.len() { - let item = self.bytes.get(i).unwrap(); - buffer = item.abi_encode(buffer); - i += 1; - } - - buffer + self.as_raw_slice().abi_encode(buffer) } } impl AbiDecode for String { fn abi_decode(ref mut buffer: BufferReader) -> Self { - // Get length and string data - let len = u64::abi_decode(buffer); - let data = buffer.read_bytes(len); - // Create string from the ptr and len as parts of a raw_slice - String { - bytes: Bytes::from(raw_slice::from_parts::(data.ptr(), len)), - } + raw_slice::abi_decode(buffer).into() } } diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 32e55ddfe80..d8819ff8456 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -133,12 +133,8 @@ impl From for RawVec { fn from(slice: raw_slice) -> Self { let cap = slice.len::(); let ptr = alloc::(cap); - asm( - to: ptr, - from: slice.ptr(), - qty_bytes: slice.number_of_bytes(), - ) { - mcp to from qty_bytes; + if cap > 0 { + slice.ptr().copy_to::(ptr, cap); } Self { ptr, cap } } From 762918f862cb7670773365636ea3894f8778206c Mon Sep 17 00:00:00 2001 From: xunilrj Date: Sun, 23 Jun 2024 16:32:36 +0100 Subject: [PATCH 11/15] grow encode buffer when needed --- sway-core/src/ir_generation/function.rs | 225 ++++++++++++++++++ .../ast_node/expression/intrinsic_function.rs | 34 +-- sway-ir/src/error.rs | 5 +- sway-ir/src/verify.rs | 4 +- sway-lib-std/src/bytes.sw | 45 ++-- sway-lib-std/src/string.sw | 86 +++---- 6 files changed, 299 insertions(+), 100 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index bbd2f6e5d7b..29eec7f1260 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -1721,6 +1721,231 @@ impl<'eng> FnCompiler<'eng> { Ok(increase_len(&mut s.current_block, context, len, 8 - offset)) } + // Grow the buffer if needed + fn grow_if_needed( + s: &mut FnCompiler<'_>, + context: &mut Context, + ptr: Value, + cap: Value, + len: Value, + needed_size: Value, + ) -> (Value, Value) { + assert!(ptr.get_type(context).unwrap().is_ptr(context)); + assert!(cap.get_type(context).unwrap().is_uint64(context)); + + let ptr_u8 = Type::new_ptr(context, Type::get_uint8(context)); + + // merge block has two arguments: ptr, cap + let merge_block = s.function.create_block(context, None); + let merge_block_ptr = Value::new_argument( + context, + BlockArgument { + block: merge_block, + idx: 0, + ty: ptr_u8, + }, + ); + merge_block.add_arg(context, merge_block_ptr.clone()); + let merge_block_cap = Value::new_argument( + context, + BlockArgument { + block: merge_block, + idx: 1, + ty: Type::get_uint64(context), + }, + ); + merge_block.add_arg(context, merge_block_cap); + + let true_block_begin = s.function.create_block(context, None); + let false_block_begin = s.function.create_block(context, None); + + // if cap + needed_size > cap + let needed_cap = s.current_block.append(context).binary_op( + BinaryOpKind::Add, + cap, + needed_size, + ); + let needs_realloc = s.current_block.append(context).cmp( + Predicate::GreaterThan, + needed_cap, + cap, + ); + s.current_block.append(context).conditional_branch( + needs_realloc, + true_block_begin, + false_block_begin, + vec![], + vec![], + ); + + // needs realloc block + // new_cap = cap * 2 + // aloc new_cap + // mcp hp old_ptr len + // hp: ptr u8 + s.current_block = true_block_begin; + let u8 = Type::get_uint8(context); + let ptr_u8 = Type::new_ptr(context, u8); + + let two = Constant::new_uint(context, 64, 2); + let two = Value::new_constant(context, two); + let new_cap = + s.current_block + .append(context) + .binary_op(BinaryOpKind::Mul, cap, two); + + let new_ptr = s.current_block.append(context).asm_block( + vec![ + AsmArg { + name: Ident::new_no_span("new_cap".into()), + initializer: Some(new_cap.clone()), + }, + AsmArg { + name: Ident::new_no_span("old_ptr".into()), + initializer: Some(ptr.clone()), + }, + AsmArg { + name: Ident::new_no_span("len".into()), + initializer: Some(len.clone()), + }, + ], + vec![ + AsmInstruction { + op_name: Ident::new_no_span("aloc".into()), + args: vec![Ident::new_no_span("new_cap".into())], + immediate: None, + metadata: None, + }, + AsmInstruction { + op_name: Ident::new_no_span("mcp".into()), + args: vec![ + Ident::new_no_span("hp".into()), + Ident::new_no_span("old_ptr".into()), + Ident::new_no_span("len".into()), + ], + immediate: None, + metadata: None, + }, + ], + ptr_u8, + Some(Ident::new_no_span("hp".into())), + ); + + s.current_block + .append(context) + .branch(merge_block, vec![new_ptr, new_cap]); + + // dont need realloc block + s.current_block = false_block_begin; + s.current_block + .append(context) + .branch(merge_block, vec![ptr, cap]); + + s.current_block = merge_block; + + assert!(merge_block_ptr.get_type(context).unwrap().is_ptr(context)); + assert!(merge_block_cap + .get_type(context) + .unwrap() + .is_uint64(context)); + + (merge_block_ptr, merge_block_cap) + } + + fn to_constant( + s: &mut FnCompiler<'_>, + context: &mut Context, + needed_size: u64, + ) -> Value { + let needed_size = Constant::new_uint(context, 64, needed_size); + Value::new_constant(context, needed_size) + } + + let (ptr, cap) = match &*item_type { + TypeInfo::Boolean => { + let needed_size = to_constant(self, context, 1); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::UnsignedInteger(IntegerBits::Eight) => { + let needed_size = to_constant(self, context, 1); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::UnsignedInteger(IntegerBits::Sixteen) => { + let needed_size = to_constant(self, context, 2); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::UnsignedInteger(IntegerBits::ThirtyTwo) => { + let needed_size = to_constant(self, context, 4); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour) => { + let needed_size = to_constant(self, context, 8); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::UnsignedInteger(IntegerBits::V256) | TypeInfo::B256 => { + let needed_size = to_constant(self, context, 32); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::StringArray(string_len) => { + let needed_size = to_constant(self, context, 8 + string_len.val() as u64); + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + TypeInfo::StringSlice | TypeInfo::RawUntypedSlice => { + let uint64 = Type::get_uint64(context); + let return_type = Type::new_struct(context, vec![uint64, uint64]); + let return_type = Type::new_ptr(context, return_type); + + let name = self.lexical_map.insert_anon(); + let item_local = self + .function + .new_local_var( + context, + name, + item.get_type(context).unwrap(), + None, + false, + ) + .map_err(|ir_error| { + CompileError::InternalOwned(ir_error.to_string(), Span::dummy()) + })?; + let item_local_value = + self.current_block.append(context).get_local(item_local); + self.current_block + .append(context) + .store(item_local_value, item); + + let item_ptr_len_value = self.current_block.append(context).asm_block( + vec![AsmArg { + name: Ident::new_no_span("item".into()), + initializer: Some(item_local_value.clone()), + }], + vec![], + return_type, + Some(Ident::new_no_span("item".into())), + ); + + let ptr = self.current_block.append(context).get_elem_ptr_with_idx( + item_ptr_len_value, + uint64, + 0, + ); + let ptr = self.current_block.append(context).load(ptr); + let ptr_u8 = Type::new_ptr(context, Type::get_uint8(context)); + let ptr = self.current_block.append(context).int_to_ptr(ptr, ptr_u8); + + let needed_size = self.current_block.append(context).get_elem_ptr_with_idx( + item_ptr_len_value, + uint64, + 1, + ); + let needed_size = self.current_block.append(context).load(needed_size); + + grow_if_needed(self, context, ptr, cap, len, needed_size) + } + _ => return Err(CompileError::EncodingUnsupportedType { span: item_span }), + }; + + // Append the value into the buffer let new_len = match &*item_type { TypeInfo::Boolean => { assert!(item.get_type(context).unwrap().is_bool(context)); diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 0a2ffbcc99f..9c324cf1289 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -247,21 +247,25 @@ fn type_check_encode_append( }; // only supported types - match &*engines.te().get(item_type) { - TypeInfo::Boolean - | TypeInfo::UnsignedInteger(IntegerBits::Eight) - | TypeInfo::UnsignedInteger(IntegerBits::Sixteen) - | TypeInfo::UnsignedInteger(IntegerBits::ThirtyTwo) - | TypeInfo::UnsignedInteger(IntegerBits::SixtyFour) - | TypeInfo::UnsignedInteger(IntegerBits::V256) - | TypeInfo::B256 - | TypeInfo::StringArray(_) - | TypeInfo::StringSlice - | TypeInfo::RawUntypedSlice => {} - _ => { - return Err(handler.emit_err(CompileError::EncodingUnsupportedType { span: item_span })) - } - }; + if item_type.is_concrete(engines, IncludeNumeric::Yes) { + match &*engines.te().get(item_type) { + TypeInfo::Boolean + | TypeInfo::UnsignedInteger(IntegerBits::Eight) + | TypeInfo::UnsignedInteger(IntegerBits::Sixteen) + | TypeInfo::UnsignedInteger(IntegerBits::ThirtyTwo) + | TypeInfo::UnsignedInteger(IntegerBits::SixtyFour) + | TypeInfo::UnsignedInteger(IntegerBits::V256) + | TypeInfo::B256 + | TypeInfo::StringArray(_) + | TypeInfo::StringSlice + | TypeInfo::RawUntypedSlice => {} + _ => { + return Err( + handler.emit_err(CompileError::EncodingUnsupportedType { span: item_span }) + ) + } + }; + } let kind = ty::TyIntrinsicFunctionKind { kind, diff --git a/sway-ir/src/error.rs b/sway-ir/src/error.rs index 62bc6b0e900..6e0b510476d 100644 --- a/sway-ir/src/error.rs +++ b/sway-ir/src/error.rs @@ -32,7 +32,7 @@ pub enum IrError { VerifyConditionExprNotABool, VerifyContractCallBadTypes(String), VerifyGepElementTypeNonPointer, - VerifyGepFromNonPointer(String), + VerifyGepFromNonPointer(String, Option), VerifyGepInconsistentTypes(String, Option), VerifyGepOnNonAggregate, VerifyGetNonExistentPointer, @@ -70,6 +70,7 @@ pub enum IrError { impl IrError { pub(crate) fn get_problematic_value(&self) -> Option<&Value> { match self { + Self::VerifyGepFromNonPointer(_, v) => v.as_ref(), Self::VerifyGepInconsistentTypes(_, v) => v.as_ref(), _ => None, } @@ -208,7 +209,7 @@ impl fmt::Display for IrError { error ) } - IrError::VerifyGepFromNonPointer(ty) => { + IrError::VerifyGepFromNonPointer(ty, _) => { write!( f, "Verification failed: Struct access must be to a pointer value, not a {ty}." diff --git a/sway-ir/src/verify.rs b/sway-ir/src/verify.rs index 3a8777289e3..e0f8abd9542 100644 --- a/sway-ir/src/verify.rs +++ b/sway-ir/src/verify.rs @@ -758,7 +758,9 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { ) -> Result<(), IrError> { use crate::constant::ConstantValue; - let base_ty = self.get_ptr_type(base, IrError::VerifyGepFromNonPointer)?; + let base_ty = self.get_ptr_type(base, |s| { + IrError::VerifyGepFromNonPointer(s, Some(ins.clone())) + })?; if !base_ty.is_aggregate(self.context) { return Err(IrError::VerifyGepOnNonAggregate); } diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index e6363fd2ad4..be34a532c1b 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -74,17 +74,14 @@ impl From for RawBytes { /// assert(raw_bytes.capacity == 3); /// ``` fn from(slice: raw_slice) -> Self { - let cap = slice.number_of_bytes(); - let ptr = alloc_bytes(cap); - if cap > 0 { - slice.ptr().copy_to::(ptr, cap); + Self { + ptr: slice.ptr(), + cap: slice.number_of_bytes(), } - Self { ptr, cap } } } /// A type used to represent raw bytes. -/// It has ownership over the bytes, stored in a heap-allocated buffer. pub struct Bytes { /// A barebones struct for the bytes. buf: RawBytes, @@ -924,32 +921,18 @@ impl AbiEncode for Bytes { impl AbiDecode for Bytes { fn abi_decode(ref mut buffer: BufferReader) -> Bytes { - let len = u64::abi_decode(buffer); - let slice = buffer.read_bytes(len); - Bytes::from(slice) + raw_slice::abi_decode(buffer).into() } } + + #[test] -fn ok_bytes_buffer_ownership() { - let mut original_array = [1u8, 2u8, 3u8, 4u8]; - let slice = raw_slice::from_parts::(__addr_of(original_array), 4); - - // Check Bytes duplicates the original slice - let mut bytes = Bytes::from(slice); - bytes.set(0, 5); - assert(original_array[0] == 1); - - // At this point, slice equals [5, 2, 3, 4] - let encoded_slice = encode(bytes); - - // `Bytes` should duplicate the underlying buffer, - // so when we write to it, it should not change - // `encoded_slice` - let mut bytes = abi_decode::(encoded_slice); - bytes.set(0, 6); - assert(bytes.get(0) == Some(6)); - - let mut bytes = abi_decode::(encoded_slice); - assert(bytes.get(0) == Some(5)); -} +fn xxx () { + let mut b = Bytes::new(); + b.push(1); + b.push(2); + + __log(encode(b)); + assert(false); +} \ No newline at end of file diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 4bd93662abc..97b6cf76df3 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -138,9 +138,9 @@ impl String { pub fn from_ascii_str(s: str) -> Self { let str_size = s.len(); let str_ptr = s.as_ptr(); - let slice = raw_slice::from_parts::(str_ptr, str_size); + Self { - bytes: Bytes::from(slice), + bytes: Bytes::from(raw_slice::from_parts::(str_ptr, str_size)), } } @@ -234,6 +234,18 @@ impl String { } } +impl From for String { + fn from(b: Bytes) -> Self { + Self { bytes: b } + } +} + +impl From for Bytes { + fn from(s: String) -> Bytes { + s.as_bytes() + } +} + impl AsRawSlice for String { /// Returns a raw slice to all of the elements in the string. fn as_raw_slice(self) -> raw_slice { @@ -296,25 +308,13 @@ impl From for raw_slice { /// } /// ``` fn from(s: String) -> raw_slice { - s.as_raw_slice() - } -} - -impl From for String { - fn from(b: Bytes) -> Self { - b.as_raw_slice().into() - } -} - -impl From for Bytes { - fn from(s: String) -> Bytes { - s.as_raw_slice().into() + raw_slice::from(s.as_bytes()) } } impl Eq for String { fn eq(self, other: Self) -> bool { - self.as_raw_slice() == other.as_raw_slice() + self.bytes == other.as_bytes() } } @@ -326,45 +326,29 @@ impl Hash for String { impl AbiEncode for String { fn abi_encode(self, buffer: Buffer) -> Buffer { - self.as_raw_slice().abi_encode(buffer) + // Encode the length + let mut buffer = self.bytes.len().abi_encode(buffer); + + // Encode each byte of the string + let mut i = 0; + while i < self.bytes.len() { + let item = self.bytes.get(i).unwrap(); + buffer = item.abi_encode(buffer); + i += 1; + } + + buffer } } impl AbiDecode for String { fn abi_decode(ref mut buffer: BufferReader) -> Self { - raw_slice::abi_decode(buffer).into() + // Get length and string data + let len = u64::abi_decode(buffer); + let data = buffer.read_bytes(len); + // Create string from the ptr and len as parts of a raw_slice + String { + bytes: Bytes::from(raw_slice::from_parts::(data.ptr(), len)), + } } } - -#[test] -fn ok_string_buffer_ownership() { - use ::option::Option::Some; - - let mut string_slice = "hi"; - let mut string = String::from_ascii_str(string_slice); - - // change first char to 'H' - let mut bytes = string.as_bytes(); - bytes.set(0, 72); - - // Check the string changed, but not the original slice - assert(string.as_bytes().get(0) == Some(72)); - assert(string_slice == "hi"); - - // encoded bytes should be Hi - let encoded_bytes = encode(string); - let string = abi_decode::(encoded_bytes); - - // change first char to 'P' - string.as_bytes().set(0, 80); - - // Check decoded string is "Pi" - assert(string.as_bytes().get(0) == Some(80)); - - // Check original string slice has not changed - assert(string_slice == "hi"); - - // Check encoded bytes has not changed - let mut bytes = abi_decode::(encoded_bytes); - assert(bytes.get(0) == Some(72)); -} From e675db460c82636ac22083c27fa48514a562fef8 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 24 Jun 2024 12:55:25 +0100 Subject: [PATCH 12/15] grow encoded buffer if needed --- sway-core/src/ir_generation/function.rs | 76 ++++---- sway-ir/src/verify.rs | 5 +- sway-lib-core/src/codec.sw | 173 ++++++----------- sway-lib-core/src/ops.sw | 2 +- .../json_abi_oracle_new_encoding.json | 174 ++++++++++++++++++ .../src/impls.sw | 6 - .../src/impls.sw | 6 - .../dereferencing_operator_index/src/impls.sw | 6 - .../dereferencing_operator_star/src/impls.sw | 6 - .../src/impls.sw | 6 - .../src/impls.sw | 6 - .../src/impls.sw | 6 - .../src/impls.sw | 6 - .../src/impls.sw | 6 - .../language/slice/slice_contract/src/main.sw | 3 +- .../string_slice_contract/src/main.sw | 3 +- .../json_abi_oracle_new_encoding.json | 2 +- .../array_of_structs_caller/src/main.sw | 2 +- .../asset_ops_test/src/main.sw | 4 +- .../bal_opcode/src/main.sw | 2 +- .../call_abi_with_tuples/src/main.sw | 2 +- .../call_basic_storage/src/main.sw | 2 +- .../src/main.sw | 2 +- .../call_increment_contract/src/main.sw | 2 +- .../call_storage_enum/src/main.sw | 2 +- .../caller_auth_test/src/main.sw | 2 +- .../caller_context_test/src/main.sw | 2 +- .../nested_struct_args_caller/src/main.sw | 2 +- .../storage_access_caller/src/main.sw | 2 +- .../contract_multi_test/src/main.sw | 6 +- test/update-contract-ids.sh | 43 +++++ 31 files changed, 339 insertions(+), 228 deletions(-) create mode 100755 test/update-contract-ids.sh diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 29eec7f1260..1fff336f3e5 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -1745,7 +1745,7 @@ impl<'eng> FnCompiler<'eng> { ty: ptr_u8, }, ); - merge_block.add_arg(context, merge_block_ptr.clone()); + merge_block.add_arg(context, merge_block_ptr); let merge_block_cap = Value::new_argument( context, BlockArgument { @@ -1759,10 +1759,10 @@ impl<'eng> FnCompiler<'eng> { let true_block_begin = s.function.create_block(context, None); let false_block_begin = s.function.create_block(context, None); - // if cap + needed_size > cap + // if len + needed_size > cap let needed_cap = s.current_block.append(context).binary_op( BinaryOpKind::Add, - cap, + len, needed_size, ); let needs_realloc = s.current_block.append(context).cmp( @@ -1798,15 +1798,15 @@ impl<'eng> FnCompiler<'eng> { vec![ AsmArg { name: Ident::new_no_span("new_cap".into()), - initializer: Some(new_cap.clone()), + initializer: Some(new_cap), }, AsmArg { name: Ident::new_no_span("old_ptr".into()), - initializer: Some(ptr.clone()), + initializer: Some(ptr), }, AsmArg { name: Ident::new_no_span("len".into()), - initializer: Some(len.clone()), + initializer: Some(len), }, ], vec![ @@ -1853,7 +1853,7 @@ impl<'eng> FnCompiler<'eng> { } fn to_constant( - s: &mut FnCompiler<'_>, + _s: &mut FnCompiler<'_>, context: &mut Context, needed_size: u64, ) -> Value { @@ -1892,49 +1892,47 @@ impl<'eng> FnCompiler<'eng> { } TypeInfo::StringSlice | TypeInfo::RawUntypedSlice => { let uint64 = Type::get_uint64(context); - let return_type = Type::new_struct(context, vec![uint64, uint64]); - let return_type = Type::new_ptr(context, return_type); + let u64_u64_type = Type::new_struct(context, vec![uint64, uint64]); + let ptr_u64_u64_type = Type::new_ptr(context, u64_u64_type); + // convert "item" to { u64, u64 } + let item = self.current_block.append(context).asm_block( + vec![AsmArg { + name: Ident::new_no_span("item".into()), + initializer: Some(item), + }], + vec![], + u64_u64_type, + Some(Ident::new_no_span("item".into())), + ); + + // save item to local _anon let name = self.lexical_map.insert_anon(); let item_local = self .function - .new_local_var( - context, - name, - item.get_type(context).unwrap(), - None, - false, - ) + .new_local_var(context, name, u64_u64_type, None, false) .map_err(|ir_error| { CompileError::InternalOwned(ir_error.to_string(), Span::dummy()) })?; - let item_local_value = + let ptr_to_local_item = self.current_block.append(context).get_local(item_local); self.current_block .append(context) - .store(item_local_value, item); - - let item_ptr_len_value = self.current_block.append(context).asm_block( - vec![AsmArg { - name: Ident::new_no_span("item".into()), - initializer: Some(item_local_value.clone()), - }], - vec![], - return_type, - Some(Ident::new_no_span("item".into())), - ); - - let ptr = self.current_block.append(context).get_elem_ptr_with_idx( - item_ptr_len_value, - uint64, - 0, - ); - let ptr = self.current_block.append(context).load(ptr); - let ptr_u8 = Type::new_ptr(context, Type::get_uint8(context)); - let ptr = self.current_block.append(context).int_to_ptr(ptr, ptr_u8); - + .store(ptr_to_local_item, item); + + // _anon.0 = ptr + // let ptr = self.current_block.append(context).get_elem_ptr_with_idx( + // item_local_value, + // uint64, + // 0, + // ); + // let ptr = self.current_block.append(context).load(ptr); + // let ptr_u8 = Type::new_ptr(context, Type::get_uint8(context)); + // let ptr = self.current_block.append(context).int_to_ptr(ptr, ptr_u8); + + // _anon.1 = len let needed_size = self.current_block.append(context).get_elem_ptr_with_idx( - item_ptr_len_value, + ptr_to_local_item, uint64, 1, ); diff --git a/sway-ir/src/verify.rs b/sway-ir/src/verify.rs index e0f8abd9542..01532a11ee8 100644 --- a/sway-ir/src/verify.rs +++ b/sway-ir/src/verify.rs @@ -758,9 +758,8 @@ impl<'a, 'eng> InstructionVerifier<'a, 'eng> { ) -> Result<(), IrError> { use crate::constant::ConstantValue; - let base_ty = self.get_ptr_type(base, |s| { - IrError::VerifyGepFromNonPointer(s, Some(ins.clone())) - })?; + let base_ty = + self.get_ptr_type(base, |s| IrError::VerifyGepFromNonPointer(s, Some(*ins)))?; if !base_ty.is_aggregate(self.context) { return Err(IrError::VerifyGepOnNonAggregate); } diff --git a/sway-lib-core/src/codec.sw b/sway-lib-core/src/codec.sw index cc43cdbf2eb..56446be522e 100644 --- a/sway-lib-core/src/codec.sw +++ b/sway-lib-core/src/codec.sw @@ -5071,119 +5071,6 @@ where } } -#[test] -fn ok_abi_encoding() { - // bool - assert_encoding_and_decoding(false, [0u8]); - assert_encoding_and_decoding(true, [1u8]); - - // numbers - assert_encoding_and_decoding(0u8, [0u8]); - assert_encoding_and_decoding(255u8, [255u8]); - - assert_encoding_and_decoding(0u16, [0u8, 0u8]); - assert_encoding_and_decoding(128u16, [0u8, 128u8]); - assert_encoding_and_decoding(65535u16, [255u8, 255u8]); - - assert_encoding_and_decoding(0u32, [0u8, 0u8, 0u8, 0u8]); - assert_encoding_and_decoding(128u32, [0u8, 0u8, 0u8, 128u8]); - assert_encoding_and_decoding(4294967295u32, [255u8, 255u8, 255u8, 255u8]); - - assert_encoding_and_decoding(0u64, [0u8; 8]); - assert_encoding_and_decoding(128u64, [0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 128u8]); - assert_encoding_and_decoding(18446744073709551615u64, [255u8; 8]); - - assert_encoding_and_decoding( - 0x0000000000000000000000000000000000000000000000000000000000000000u256, - [0u8; 32], - ); - assert_encoding_and_decoding( - 0xAA000000000000000000000000000000000000000000000000000000000000BBu256, - [ - 0xAAu8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, - 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, - 0u8, 0u8, 0u8, 0xBBu8, - ], - ); - assert_encoding_and_decoding( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256, - [255u8; 32], - ); - - assert_encoding_and_decoding( - 0x0000000000000000000000000000000000000000000000000000000000000000, - [0u8; 32], - ); - assert_encoding_and_decoding( - 0xAA000000000000000000000000000000000000000000000000000000000000BB, - [ - 0xAAu8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, - 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, - 0u8, 0u8, 0u8, 0xBBu8, - ], - ); - assert_encoding_and_decoding( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - [255u8; 32], - ); - - // strings - assert_encoding_and_decoding( - "Hello", - [0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 5u8, 72u8, 101u8, 108u8, 108u8, 111u8], - ); - - assert_encoding( - { - let a: str[1] = __to_str_array("a"); - a - }, - [97u8], - ); - assert_encoding( - { - let a: str[2] = __to_str_array("aa"); - a - }, - [97u8, 97u8], - ); - assert_encoding( - { - let a: str[3] = __to_str_array("aaa"); - a - }, - [97u8, 97u8, 97u8], - ); - assert_encoding( - { - let a: str[4] = __to_str_array("aaaa"); - a - }, - [97u8, 97u8, 97u8, 97u8], - ); - assert_encoding( - { - let a: str[5] = __to_str_array("aaaaa"); - a - }, - [97u8, 97u8, 97u8, 97u8, 97u8], - ); - - // arrays - assert_encoding([255u8; 1], [255u8; 1]); - assert_encoding([255u8; 2], [255u8; 2]); - assert_encoding([255u8; 3], [255u8; 3]); - assert_encoding([255u8; 4], [255u8; 4]); - assert_encoding([255u8; 5], [255u8; 5]); - - let array = abi_decode::<[u8; 1]>(to_slice([255u8])); - assert_eq(array[0], 255u8); - - let array = abi_decode::<[u8; 2]>(to_slice([255u8, 254u8])); - assert_eq(array[0], 255u8); - assert_eq(array[1], 254u8); -} - pub fn contract_call( contract_id: b256, method_name: str, @@ -5259,3 +5146,63 @@ where let mut buffer = BufferReader::from_second_parameter(); T::abi_decode(buffer) } + + +#[test] +fn ok_abi_encoding() { + // strings + assert_encoding_and_decoding( + "Hello", + [0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 5u8, 72u8, 101u8, 108u8, 108u8, 111u8], + ); + + assert_encoding( + { + let a: str[1] = __to_str_array("a"); + a + }, + [97u8], + ); + assert_encoding( + { + let a: str[2] = __to_str_array("aa"); + a + }, + [97u8, 97u8], + ); + assert_encoding( + { + let a: str[3] = __to_str_array("aaa"); + a + }, + [97u8, 97u8, 97u8], + ); + assert_encoding( + { + let a: str[4] = __to_str_array("aaaa"); + a + }, + [97u8, 97u8, 97u8, 97u8], + ); + assert_encoding( + { + let a: str[5] = __to_str_array("aaaaa"); + a + }, + [97u8, 97u8, 97u8, 97u8, 97u8], + ); + + // arrays + assert_encoding([255u8; 1], [255u8; 1]); + assert_encoding([255u8; 2], [255u8; 2]); + assert_encoding([255u8; 3], [255u8; 3]); + assert_encoding([255u8; 4], [255u8; 4]); + assert_encoding([255u8; 5], [255u8; 5]); + + let array = abi_decode::<[u8; 1]>(to_slice([255u8])); + assert_eq(array[0], 255u8); + + let array = abi_decode::<[u8; 2]>(to_slice([255u8, 254u8])); + assert_eq(array[0], 255u8); + assert_eq(array[1], 254u8); +} \ No newline at end of file diff --git a/sway-lib-core/src/ops.sw b/sway-lib-core/src/ops.sw index c34e30099d1..af05253ad12 100644 --- a/sway-lib-core/src/ops.sw +++ b/sway-lib-core/src/ops.sw @@ -614,7 +614,7 @@ impl Eq for raw_slice { l: (raw_ptr, u64) }; - let (r_ptr, r_len) = asm(l: self) { + let (r_ptr, r_len) = asm(r: self) { r: (raw_ptr, u64) }; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json index 749153669f7..4eb6b8467de 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json @@ -1,4 +1,5 @@ { +<<<<<<< HEAD "configurables": [ { "configurableType": { @@ -315,6 +316,179 @@ "offset": 6912 >>>>>>> 51fa76df5 (fix rebase issues) } +======= + "configurables": [ + { + "configurableType": { + "name": "", + "type": 5, + "typeArguments": null + }, + "name": "BOOL", + "offset": 6848 + }, + { + "configurableType": { + "name": "", + "type": 13, + "typeArguments": null + }, + "name": "U8", + "offset": 6984 + }, + { + "configurableType": { + "name": "", + "type": 13, + "typeArguments": null + }, + "name": "ANOTHER_U8", + "offset": 6776 + }, + { + "configurableType": { + "name": "", + "type": 9, + "typeArguments": null + }, + "name": "U16", + "offset": 6928 + }, + { + "configurableType": { + "name": "", + "type": 11, + "typeArguments": null + }, + "name": "U32", + "offset": 6968 + }, + { + "configurableType": { + "name": "", + "type": 11, + "typeArguments": null + }, + "name": "U64", + "offset": 6976 + }, + { + "configurableType": { + "name": "", + "type": 10, + "typeArguments": null + }, + "name": "U256", + "offset": 6936 + }, + { + "configurableType": { + "name": "", + "type": 4, + "typeArguments": null + }, + "name": "B256", + "offset": 6816 + }, + { + "configurableType": { + "name": "", + "type": 8, + "typeArguments": [] + }, + "name": "CONFIGURABLE_STRUCT", + "offset": 6888 + }, + { + "configurableType": { + "name": "", + "type": 6, + "typeArguments": [] + }, + "name": "CONFIGURABLE_ENUM_A", + "offset": 6856 + }, + { + "configurableType": { + "name": "", + "type": 6, + "typeArguments": [] + }, + "name": "CONFIGURABLE_ENUM_B", + "offset": 6872 + }, + { + "configurableType": { + "name": "", + "type": 2, + "typeArguments": null + }, + "name": "ARRAY_BOOL", + "offset": 6784 + }, + { + "configurableType": { + "name": "", + "type": 3, + "typeArguments": null + }, + "name": "ARRAY_U64", + "offset": 6792 + }, + { + "configurableType": { + "name": "", + "type": 1, + "typeArguments": null + }, + "name": "TUPLE_BOOL_U64", + "offset": 6912 + }, + { + "configurableType": { + "name": "", + "type": 7, + "typeArguments": null + }, + "name": "STR_4", + "offset": 6904 + } + ], + "encoding": "1", + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": [], + "type": "()", + "typeId": 0, + "typeParameters": null + }, + { + "components": [ + { + "name": "__tuple_element", + "type": 5, + "typeArguments": null + }, + { + "name": "__tuple_element", + "type": 12, + "typeArguments": null + } +>>>>>>> 9cf7bc30c (grow encoded buffer if needed) ], "encoding": "1", "functions": [ diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_structs/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_structs/src/impls.sw index 17482f14082..48d303d8307 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_structs/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_structs/src/impls.sw @@ -219,12 +219,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_tuples/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_tuples/src/impls.sw index 17482f14082..48d303d8307 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_tuples/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_dot_on_tuples/src/impls.sw @@ -219,12 +219,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_index/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_index/src/impls.sw index 17482f14082..48d303d8307 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_index/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_index/src/impls.sw @@ -219,12 +219,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_star/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_star/src/impls.sw index 17482f14082..48d303d8307 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_star/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/dereferencing_operator_star/src/impls.sw @@ -219,12 +219,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/passing_and_returning_references_to_and_from_functions/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/passing_and_returning_references_to_and_from_functions/src/impls.sw index 46564dda772..96245cc3e99 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/passing_and_returning_references_to_and_from_functions/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/passing_and_returning_references_to_and_from_functions/src/impls.sw @@ -166,12 +166,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_passed_and_returned_to_and_from_functions/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_passed_and_returned_to_and_from_functions/src/impls.sw index 0d80e865299..358110721ed 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_passed_and_returned_to_and_from_functions/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_passed_and_returned_to_and_from_functions/src/impls.sw @@ -226,12 +226,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_expressions/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_expressions/src/impls.sw index bf6a011f69c..4777f1a50e8 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_expressions/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_expressions/src/impls.sw @@ -228,12 +228,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_values/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_values/src/impls.sw index dad85dd3649..54d20de59f6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_values/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/reassigning_via_references_to_values/src/impls.sw @@ -221,12 +221,6 @@ impl TestInstance for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl TestInstance for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/referencing_local_vars_and_values/src/impls.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/referencing_local_vars_and_values/src/impls.sw index 38feceeb687..f5f64d206a6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/references/referencing_local_vars_and_values/src/impls.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/references/referencing_local_vars_and_values/src/impls.sw @@ -182,12 +182,6 @@ impl New for raw_slice { } } -impl Eq for raw_slice { - fn eq(self, other: Self) -> bool { - self.ptr() == other.ptr() && self.number_of_bytes() == other.number_of_bytes() - } -} - impl New for () { fn new() -> Self { () diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw index c86ce974d88..b0edb611c91 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw @@ -12,7 +12,8 @@ impl MyContract for Contract { #[test] fn test_success() { - let caller = abi(MyContract, CONTRACT_ID); + let contract_id = 0xbd583b56a289d6a8779db919e9c10ddc6a5875d91325820250fc9bfbe0c802b4; // AUTO-CONTRACT-ID . + let caller = abi(MyContract, contract_id); let data = 1u64; let slice = raw_slice::from_parts::(__addr_of(&data), 1); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw index 0a57dc67e6e..f9393eacd5b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw @@ -12,7 +12,8 @@ impl MyContract for Contract { #[test] fn test_success() { - let caller = abi(MyContract, CONTRACT_ID); + let contract_id = 0x3b4075709c0f64e015dc717460a755c2f51f43acb127f6f2d23775bb682c2ff4; // AUTO-CONTRACT-ID . + let caller = abi(MyContract, contract_id); let result = caller.test_function("a"); assert(result == "a") } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json index 58d148218d5..d7877393dee 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json @@ -7,7 +7,7 @@ "typeArguments": null }, "name": "SOME_U256", - "offset": 672 + "offset": 616 } ], "encoding": "1", diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw index ad7f5d4f116..b2ad4752d62 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw @@ -6,7 +6,7 @@ use std::hash::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x14ed3cd06c2947248f69d54bfa681fe40d26267be84df7e19e253622b7921bbe; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xb9c932420d73aedd16ae8942f27d09e469a449797e3f82970e7c8d5cd45fd33f; +const CONTRACT_ID = 0x219c709a7ff21ca3ee3746ed592a114c45a7edac09a4642af497a70e82cc6cc2; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release fn main() -> u64 { let addr = abi(TestContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw index 51a2c5dfbb8..2ac1cc498af 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw @@ -9,12 +9,12 @@ use test_fuel_coin_abi::*; #[cfg(experimental_new_encoding = false)] const FUEL_COIN_CONTRACT_ID = 0xec2277ebe007ade87e3d797c3b1e070dcd542d5ef8f038b471f262ef9cebc87c; #[cfg(experimental_new_encoding = true)] -const FUEL_COIN_CONTRACT_ID = 0x1a88d0982d216958d18378b6784614b75868a542dc05f8cc85cf3da44268c76c; +const FUEL_COIN_CONTRACT_ID = 0x4310867f369075df5771b1d2ac0965d3f28c782afebba2e6f18ebada3a4b367c; // AUTO-CONTRACT-ID ../../test_contracts/test_fuel_coin_contract --release #[cfg(experimental_new_encoding = false)] const BALANCE_CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df; #[cfg(experimental_new_encoding = true)] -const BALANCE_CONTRACT_ID = 0x04594851a2bd620dffc360f3976d747598b9184218dc55ff3e839f417ca345ac; +const BALANCE_CONTRACT_ID = 0x59c48403dc907aed220498297c0b9df42f33aa84e7220d708fdf6c181f8b983e; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release fn main() -> bool { let default_gas = 1_000_000_000_000; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw index 34bec41ab32..968f4f9a626 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw @@ -5,7 +5,7 @@ use balance_test_abi::BalanceTest; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x04594851a2bd620dffc360f3976d747598b9184218dc55ff3e839f417ca345ac; +const CONTRACT_ID = 0x59c48403dc907aed220498297c0b9df42f33aa84e7220d708fdf6c181f8b983e; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release fn main() -> bool { let balance_test_contract = abi(BalanceTest, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw index b73f151de97..e5e84db851c 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw @@ -6,7 +6,7 @@ use abi_with_tuples::{MyContract, Location, Person}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x5175a6a984a6d8f92622afd3d987f5b778f5741c56d55ee5993cc368b9afee10; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x2435d33fbcdcaea63ea81348866a3d565a84cc90f68a18513b0cef78ee3faa03; +const CONTRACT_ID = 0x3074ab63db09d304ee45814a51e523dca4faf3bd695870286c9d1a8ac648797b; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release fn main() -> bool { let the_abi = abi(MyContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw index cb0df5febe8..cd5925dcbd7 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x186c989267434c784f7785be57b0db0a490ec382bbf98a6108a840c63b99be99; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xe82914343e787908e5661aeaea720e48248f9d99e14d57677936164fbda70f69; +const CONTRACT_ID = 0x9054333d06523ac906531d373a44e8530788ce80605f5c9b10df4df07185474c; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release fn main() -> u64 { let addr = abi(BasicStorage, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw index 95be5119a91..4e171ed5f16 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw @@ -5,7 +5,7 @@ use contract_with_type_aliases_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x0cbeb6efe3104b460be769bdc4ea101ebf16ccc16f2d7b667ec3e1c7f5ce35b5; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xbcd811d9f9d67f81935a466ccb8b20a1ee505277ccae89ad57b31435e33caa8d; +const CONTRACT_ID = 0xb5f80b90e3cc37497f3b4b715ab1e60e3147c2818b3c2baebd7a2319cddbca20; // AUTO-CONTRACT-ID ../../test_contracts/contract_with_type_aliases --release fn main() { let caller = abi(MyContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw index 3d36dbdf1f5..db1d7b35437 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw @@ -6,7 +6,7 @@ use dynamic_contract_call::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xd1b4047af7ef111c023ab71069e01dc2abfde487c0a0ce1268e4f447e6c6e4c2; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xfa28a2cb8354101b787f75b144170dd4373578e342eebe485661b3683427680c; +const CONTRACT_ID = 0x713cb9dc1a1229ee12ddae923d01edd790a3d74991cfe6706dd2fe145de5cb8d; // AUTO-CONTRACT-ID ../../test_contracts/increment_contract --release fn main() -> bool { let the_abi = abi(Incrementor, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw index a86e564b4f6..a320e2d5640 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw @@ -5,7 +5,7 @@ use storage_enum_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xb54eb67f943fda15981308ef55b2cb2afe1ed5907c483d2d92d010ab39549644; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xfd91339494a1600ffb24d90eb7b67582e11fee5936e01704fb17a5445eb3f47e; +const CONTRACT_ID = 0x01ef84bfa822ac64e71da6cf61db8a71213d9d0308509ae4d17488cf48bb5988; // AUTO-CONTRACT-ID ../../test_contracts/storage_enum_contract --release fn main() -> u64 { let caller = abi(StorageEnum, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw index 6a054b04052..b513c867043 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw @@ -5,7 +5,7 @@ use auth_testing_abi::AuthTesting; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xc2eec20491b53aab7232cbd27c31d15417b4e9daf0b89c74cc242ef1295f681f; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xeb42a6ba3f230441d7ade26f54cfb9f64fe8dd6397cc685fefc7f3d082d4d2ce; +const CONTRACT_ID = 0x63aca707cebd7e52d0158214d7e21a7f9f7e909e75c49167e59abf9ed684a98e; // AUTO-CONTRACT-ID ../../test_contracts/auth_testing_contract --release // should be false in the case of a script fn main() -> bool { diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw index dc9a744fb74..d3df82b5b60 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw @@ -6,7 +6,7 @@ use context_testing_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x6054c11cda000f5990373a4d61929396165be4dfdd61d5b7bd26da60ab0d8577; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x2d12f460ffd7da3e1281253e4a63f26a5a3cbc8cffd365a4d6fa6444caa7d48f; +const CONTRACT_ID = 0xdfc9f94f537270b794b9637ee5dc51763b07f44d0a2007c77de650108cc3b897; // AUTO-CONTRACT-ID ../../test_contracts/context_testing_contract --release fn main() -> bool { let gas: u64 = u64::max(); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw index ce6698c04ca..509602a72fa 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw @@ -5,7 +5,7 @@ use nested_struct_args_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xe63d33a1b3a6903808b379f6a41a72fa8a370e8b76626775e7d9d2f9c4c5da40; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xbefd6660ae89502652b2ab4e536edf3403208e308a329c06af6017dec242cc37; +const CONTRACT_ID = 0x316c68593dfc369cdeaa707b416022a5186e5222aff745dfa963b9eb077b88bf; // AUTO-CONTRACT-ID ../../test_contracts/nested_struct_args_contract --release fn main() -> bool { let caller = abi(NestedStructArgs, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw index 5b4090ffd3d..afbcdb716a1 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw @@ -6,7 +6,7 @@ use std::hash::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x3bc28acd66d327b8c1b9624c1fabfc07e9ffa1b5d71c2832c3bfaaf8f4b805e9; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x54f3c43fb35959d0ec76ae22d58811be2a697df1901422dab0e0f9da5887f858; +const CONTRACT_ID = 0xe57d3bb9924ada4b9a9a9ed1965a254362926c24f176ede505bc19d2a3283716; // AUTO-CONTRACT-ID ../../test_contracts/storage_access_contract --release fn main() -> bool { let caller = abi(StorageAccess, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw index 61e416f17bd..5021e69074d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw @@ -17,14 +17,16 @@ fn test_foo() { #[test(should_revert)] fn test_fail() { - let caller = abi(MyContract, CONTRACT_ID); + let contract_id = 0x61e6641ec2ef2f69e5fcbdbb74b87e3c0f5570dcf24a37ce324033223c7eb464; // AUTO-CONTRACT-ID . + let caller = abi(MyContract, contract_id); let result = caller.test_function {}(); assert(result == false) } #[test] fn test_success() { - let caller = abi(MyContract, CONTRACT_ID); + let contract_id = 0x61e6641ec2ef2f69e5fcbdbb74b87e3c0f5570dcf24a37ce324033223c7eb464; // AUTO-CONTRACT-ID . + let caller = abi(MyContract, contract_id); let result = caller.test_function {}(); assert(result == true) } diff --git a/test/update-contract-ids.sh b/test/update-contract-ids.sh new file mode 100755 index 00000000000..f125bf31e1b --- /dev/null +++ b/test/update-contract-ids.sh @@ -0,0 +1,43 @@ +#! /bin/bash + +BOLD_RED='\033[1;31m' +BOLD_GREEN="\033[1;32m" +BOLD_YELLOW='\033[1;33m' +BOLD_WHITE='\033[1;97m' +NC='\033[0m' + +function join_by { + local d=${1-} f=${2-} + if shift 2; then + printf %s "$f" "${@/#/$d}" + fi +} + +grep --include \*.sw -Hno "// AUTO-CONTRACT-ID" . -R | while read line ; do + PARTS=($(echo $line | sed 's/:/ /g')) + FOLDER=$(dirname ${PARTS[0]}) + FILE=${PARTS[0]} + LINE=${PARTS[1]} + + CONTRACT_ARGS=($(sed "$LINE!d" $FILE)) + CONTRACT_ARGS=$(join_by " " ${CONTRACT_ARGS[@]:6}) + + if [[ $CONTRACT_ARGS ]]; then + PROJ=$(realpath "$FOLDER/..") + echo -e "${BOLD_WHITE}$PROJ${NC}" + + pushd "$FOLDER/.." >> /dev/null + CONTRACT_ID=($(cargo r -p forc --release -- contract-id --path $CONTRACT_ARGS 2> /dev/null)) + CONTRACT_ID=${CONTRACT_ID[16]} # change here if the output of forc change + + if [[ $CONTRACT_ID ]]; then + popd >> /dev/null + sed -i "${LINE}s/0x[a-zA-Z0-9]*/$CONTRACT_ID/g" $FILE + echo -e " ${BOLD_GREEN}ok${NC} ($CONTRACT_ID)" + else + echo -e " ${BOLD_RED}error${NC}" + cargo r -p forc --release -- contract-id --release + popd >> /dev/null + fi + fi +done From 69df3fb12d1845442ef3881d022fbcf903084583 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 24 Jun 2024 12:56:09 +0100 Subject: [PATCH 13/15] clippy and fmt --- sway-core/src/ir_generation/function.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 1fff336f3e5..b5551134547 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -1893,7 +1893,6 @@ impl<'eng> FnCompiler<'eng> { TypeInfo::StringSlice | TypeInfo::RawUntypedSlice => { let uint64 = Type::get_uint64(context); let u64_u64_type = Type::new_struct(context, vec![uint64, uint64]); - let ptr_u64_u64_type = Type::new_ptr(context, u64_u64_type); // convert "item" to { u64, u64 } let item = self.current_block.append(context).asm_block( From 0184dad2d1d8eee950b1205eeaf061f43979a6e3 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 24 Jun 2024 14:00:38 +0100 Subject: [PATCH 14/15] fix rebase issues --- .../json_abi_oracle_new_encoding.json | 604 ++++-------------- .../language/slice/slice_contract/src/main.sw | 2 +- .../string_slice_contract/src/main.sw | 2 +- .../json_abi_oracle_new_encoding.json | 2 +- .../array_of_structs_caller/src/main.sw | 2 +- .../asset_ops_test/src/main.sw | 4 +- .../bal_opcode/src/main.sw | 2 +- .../call_abi_with_tuples/src/main.sw | 2 +- .../call_basic_storage/src/main.sw | 2 +- .../src/main.sw | 2 +- .../call_increment_contract/src/main.sw | 2 +- .../call_storage_enum/src/main.sw | 2 +- .../caller_auth_test/src/main.sw | 2 +- .../caller_context_test/src/main.sw | 2 +- .../nested_struct_args_caller/src/main.sw | 2 +- .../storage_access_caller/src/main.sw | 2 +- .../contract_multi_test/src/main.sw | 4 +- 17 files changed, 143 insertions(+), 497 deletions(-) diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json index 4eb6b8467de..b6a290205a9 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json @@ -1,322 +1,4 @@ { -<<<<<<< HEAD - "configurables": [ - { - "configurableType": { - "name": "", - "type": 5, - "typeArguments": null - }, - "name": "BOOL", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6912 -======= -<<<<<<< HEAD - "offset": 6848 -======= - "offset": 6936 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6856 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 13, - "typeArguments": null - }, - "name": "U8", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 7048 -======= -<<<<<<< HEAD - "offset": 6984 -======= - "offset": 7072 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6992 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 13, - "typeArguments": null - }, - "name": "ANOTHER_U8", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6840 -======= -<<<<<<< HEAD - "offset": 6776 -======= - "offset": 6864 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6784 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 9, - "typeArguments": null - }, - "name": "U16", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6992 -======= -<<<<<<< HEAD - "offset": 6928 -======= - "offset": 7016 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6936 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 11, - "typeArguments": null - }, - "name": "U32", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 7032 -======= -<<<<<<< HEAD - "offset": 6968 -======= - "offset": 7056 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6976 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 11, - "typeArguments": null - }, - "name": "U64", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 7040 -======= -<<<<<<< HEAD - "offset": 6976 -======= - "offset": 7064 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6984 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 10, - "typeArguments": null - }, - "name": "U256", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 7000 -======= -<<<<<<< HEAD - "offset": 6936 -======= - "offset": 7024 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6944 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 4, - "typeArguments": null - }, - "name": "B256", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6880 -======= -<<<<<<< HEAD - "offset": 6816 -======= - "offset": 6904 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6824 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 8, - "typeArguments": [] - }, - "name": "CONFIGURABLE_STRUCT", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6952 -======= -<<<<<<< HEAD - "offset": 6888 -======= - "offset": 6976 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6896 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 6, - "typeArguments": [] - }, - "name": "CONFIGURABLE_ENUM_A", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6920 -======= -<<<<<<< HEAD - "offset": 6856 -======= - "offset": 6944 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6864 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 6, - "typeArguments": [] - }, - "name": "CONFIGURABLE_ENUM_B", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6936 -======= -<<<<<<< HEAD - "offset": 6872 -======= - "offset": 6960 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6880 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 2, - "typeArguments": null - }, - "name": "ARRAY_BOOL", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6848 -======= -<<<<<<< HEAD - "offset": 6784 -======= - "offset": 6872 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6792 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 3, - "typeArguments": null - }, - "name": "ARRAY_U64", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6856 -======= -<<<<<<< HEAD - "offset": 6792 -======= - "offset": 6880 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6800 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 1, - "typeArguments": null - }, - "name": "TUPLE_BOOL_U64", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6976 -======= -<<<<<<< HEAD - "offset": 6912 -======= - "offset": 7000 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6920 ->>>>>>> 51fa76df5 (fix rebase issues) - }, - { - "configurableType": { - "name": "", - "type": 7, - "typeArguments": null - }, - "name": "STR_4", -<<<<<<< HEAD -<<<<<<< HEAD - "offset": 6968 -======= -<<<<<<< HEAD - "offset": 6904 -======= - "offset": 6992 ->>>>>>> ef917df9a (fix Vec buffer ownership) ->>>>>>> 085e75b03 (fix Vec buffer ownership) -======= - "offset": 6912 ->>>>>>> 51fa76df5 (fix rebase issues) - } -======= "configurables": [ { "configurableType": { @@ -325,7 +7,7 @@ "typeArguments": null }, "name": "BOOL", - "offset": 6848 + "offset": 6912 }, { "configurableType": { @@ -334,7 +16,7 @@ "typeArguments": null }, "name": "U8", - "offset": 6984 + "offset": 7048 }, { "configurableType": { @@ -343,7 +25,7 @@ "typeArguments": null }, "name": "ANOTHER_U8", - "offset": 6776 + "offset": 6840 }, { "configurableType": { @@ -352,7 +34,7 @@ "typeArguments": null }, "name": "U16", - "offset": 6928 + "offset": 6992 }, { "configurableType": { @@ -361,7 +43,7 @@ "typeArguments": null }, "name": "U32", - "offset": 6968 + "offset": 7032 }, { "configurableType": { @@ -370,7 +52,7 @@ "typeArguments": null }, "name": "U64", - "offset": 6976 + "offset": 7040 }, { "configurableType": { @@ -379,7 +61,7 @@ "typeArguments": null }, "name": "U256", - "offset": 6936 + "offset": 7000 }, { "configurableType": { @@ -388,7 +70,7 @@ "typeArguments": null }, "name": "B256", - "offset": 6816 + "offset": 6880 }, { "configurableType": { @@ -397,7 +79,7 @@ "typeArguments": [] }, "name": "CONFIGURABLE_STRUCT", - "offset": 6888 + "offset": 6952 }, { "configurableType": { @@ -406,7 +88,7 @@ "typeArguments": [] }, "name": "CONFIGURABLE_ENUM_A", - "offset": 6856 + "offset": 6920 }, { "configurableType": { @@ -415,7 +97,7 @@ "typeArguments": [] }, "name": "CONFIGURABLE_ENUM_B", - "offset": 6872 + "offset": 6936 }, { "configurableType": { @@ -424,7 +106,7 @@ "typeArguments": null }, "name": "ARRAY_BOOL", - "offset": 6784 + "offset": 6848 }, { "configurableType": { @@ -433,7 +115,7 @@ "typeArguments": null }, "name": "ARRAY_U64", - "offset": 6792 + "offset": 6856 }, { "configurableType": { @@ -442,7 +124,7 @@ "typeArguments": null }, "name": "TUPLE_BOOL_U64", - "offset": 6912 + "offset": 6976 }, { "configurableType": { @@ -451,7 +133,7 @@ "typeArguments": null }, "name": "STR_4", - "offset": 6904 + "offset": 6968 } ], "encoding": "1", @@ -488,152 +170,116 @@ "type": 12, "typeArguments": null } ->>>>>>> 9cf7bc30c (grow encoded buffer if needed) ], - "encoding": "1", - "functions": [ - { - "attributes": null, - "inputs": [], - "name": "main", - "output": { - "name": "", - "type": 0, - "typeArguments": null - } - } + "type": "(_, _)", + "typeId": 1, + "typeParameters": null + }, + { + "components": [ + { + "name": "__array_element", + "type": 5, + "typeArguments": null + } ], - "loggedTypes": [], - "messagesTypes": [], - "types": [ - { - "components": [], - "type": "()", - "typeId": 0, - "typeParameters": null - }, - { - "components": [ - { - "name": "__tuple_element", - "type": 5, - "typeArguments": null - }, - { - "name": "__tuple_element", - "type": 12, - "typeArguments": null - } - ], - "type": "(_, _)", - "typeId": 1, - "typeParameters": null - }, - { - "components": [ - { - "name": "__array_element", - "type": 5, - "typeArguments": null - } - ], - "type": "[_; 3]", - "typeId": 2, - "typeParameters": null - }, - { - "components": [ - { - "name": "__array_element", - "type": 12, - "typeArguments": null - } - ], - "type": "[_; 3]", - "typeId": 3, - "typeParameters": null - }, - { - "components": null, - "type": "b256", - "typeId": 4, - "typeParameters": null - }, - { - "components": null, - "type": "bool", - "typeId": 5, - "typeParameters": null - }, - { - "components": [ - { - "name": "A", - "type": 5, - "typeArguments": null - }, - { - "name": "B", - "type": 12, - "typeArguments": null - } - ], - "type": "enum ConfigurableEnum", - "typeId": 6, - "typeParameters": null - }, - { - "components": null, - "type": "str[4]", - "typeId": 7, - "typeParameters": null - }, - { - "components": [ - { - "name": "a", - "type": 5, - "typeArguments": null - }, - { - "name": "b", - "type": 12, - "typeArguments": null - } - ], - "type": "struct ConfigurableStruct", - "typeId": 8, - "typeParameters": null - }, - { - "components": null, - "type": "u16", - "typeId": 9, - "typeParameters": null - }, - { - "components": null, - "type": "u256", - "typeId": 10, - "typeParameters": null - }, - { - "components": null, - "type": "u32", - "typeId": 11, - "typeParameters": null - }, - { - "components": null, - "type": "u64", - "typeId": 12, - "typeParameters": null - }, - { - "components": null, - "type": "u8", - "typeId": 13, - "typeParameters": null - } - ] + "type": "[_; 3]", + "typeId": 2, + "typeParameters": null + }, + { + "components": [ + { + "name": "__array_element", + "type": 12, + "typeArguments": null + } + ], + "type": "[_; 3]", + "typeId": 3, + "typeParameters": null + }, + { + "components": null, + "type": "b256", + "typeId": 4, + "typeParameters": null + }, + { + "components": null, + "type": "bool", + "typeId": 5, + "typeParameters": null + }, + { + "components": [ + { + "name": "A", + "type": 5, + "typeArguments": null + }, + { + "name": "B", + "type": 12, + "typeArguments": null + } + ], + "type": "enum ConfigurableEnum", + "typeId": 6, + "typeParameters": null + }, + { + "components": null, + "type": "str[4]", + "typeId": 7, + "typeParameters": null + }, + { + "components": [ + { + "name": "a", + "type": 5, + "typeArguments": null + }, + { + "name": "b", + "type": 12, + "typeArguments": null + } + ], + "type": "struct ConfigurableStruct", + "typeId": 8, + "typeParameters": null + }, + { + "components": null, + "type": "u16", + "typeId": 9, + "typeParameters": null + }, + { + "components": null, + "type": "u256", + "typeId": 10, + "typeParameters": null + }, + { + "components": null, + "type": "u32", + "typeId": 11, + "typeParameters": null + }, + { + "components": null, + "type": "u64", + "typeId": 12, + "typeParameters": null + }, + { + "components": null, + "type": "u8", + "typeId": 13, + "typeParameters": null + } + ] } \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw index b0edb611c91..287d2c55cec 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_contract/src/main.sw @@ -12,7 +12,7 @@ impl MyContract for Contract { #[test] fn test_success() { - let contract_id = 0xbd583b56a289d6a8779db919e9c10ddc6a5875d91325820250fc9bfbe0c802b4; // AUTO-CONTRACT-ID . + let contract_id = 0xeaed1cfcdb58af14ed2a84f4d825522aad4cd9579100086cc48828662b5ecbfb; // AUTO-CONTRACT-ID . let caller = abi(MyContract, contract_id); let data = 1u64; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw index f9393eacd5b..ab0768f66dc 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/string_slice/string_slice_contract/src/main.sw @@ -12,7 +12,7 @@ impl MyContract for Contract { #[test] fn test_success() { - let contract_id = 0x3b4075709c0f64e015dc717460a755c2f51f43acb127f6f2d23775bb682c2ff4; // AUTO-CONTRACT-ID . + let contract_id = 0xe1669acd3f6d70b9ede9953ae1694225c8005416f4d8e01017070d4d3f9c4254; // AUTO-CONTRACT-ID . let caller = abi(MyContract, contract_id); let result = caller.test_function("a"); assert(result == "a") diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json index d7877393dee..95064cff5f6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/u256/u256_abi/json_abi_oracle_new_encoding.json @@ -7,7 +7,7 @@ "typeArguments": null }, "name": "SOME_U256", - "offset": 616 + "offset": 704 } ], "encoding": "1", diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw index b2ad4752d62..e9daf7ba52f 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/array_of_structs_caller/src/main.sw @@ -6,7 +6,7 @@ use std::hash::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x14ed3cd06c2947248f69d54bfa681fe40d26267be84df7e19e253622b7921bbe; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x219c709a7ff21ca3ee3746ed592a114c45a7edac09a4642af497a70e82cc6cc2; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release +const CONTRACT_ID = 0xd3b9729f720d72af0370e6444a2894531cf17eea61fd54ec5789f0724eee1168; // AUTO-CONTRACT-ID ../../test_contracts/array_of_structs_contract --release fn main() -> u64 { let addr = abi(TestContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw index 2ac1cc498af..c23eb640723 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/asset_ops_test/src/main.sw @@ -9,12 +9,12 @@ use test_fuel_coin_abi::*; #[cfg(experimental_new_encoding = false)] const FUEL_COIN_CONTRACT_ID = 0xec2277ebe007ade87e3d797c3b1e070dcd542d5ef8f038b471f262ef9cebc87c; #[cfg(experimental_new_encoding = true)] -const FUEL_COIN_CONTRACT_ID = 0x4310867f369075df5771b1d2ac0965d3f28c782afebba2e6f18ebada3a4b367c; // AUTO-CONTRACT-ID ../../test_contracts/test_fuel_coin_contract --release +const FUEL_COIN_CONTRACT_ID = 0x1a88d0982d216958d18378b6784614b75868a542dc05f8cc85cf3da44268c76c; // AUTO-CONTRACT-ID ../../test_contracts/test_fuel_coin_contract --release #[cfg(experimental_new_encoding = false)] const BALANCE_CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df; #[cfg(experimental_new_encoding = true)] -const BALANCE_CONTRACT_ID = 0x59c48403dc907aed220498297c0b9df42f33aa84e7220d708fdf6c181f8b983e; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release +const BALANCE_CONTRACT_ID = 0x0d5fb0c109082f5784f6cd5979d9d25cab2d7307baf4a7661dea7d285c970e9f; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release fn main() -> bool { let default_gas = 1_000_000_000_000; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw index 968f4f9a626..db944a5f3f0 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/bal_opcode/src/main.sw @@ -5,7 +5,7 @@ use balance_test_abi::BalanceTest; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xf6cd545152ac83225e8e7df2efb5c6fa6e37bc9b9e977b5ea8103d28668925df; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x59c48403dc907aed220498297c0b9df42f33aa84e7220d708fdf6c181f8b983e; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release +const CONTRACT_ID = 0x0d5fb0c109082f5784f6cd5979d9d25cab2d7307baf4a7661dea7d285c970e9f; // AUTO-CONTRACT-ID ../../test_contracts/balance_test_contract --release fn main() -> bool { let balance_test_contract = abi(BalanceTest, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw index e5e84db851c..3db5fdcbf13 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_abi_with_tuples/src/main.sw @@ -6,7 +6,7 @@ use abi_with_tuples::{MyContract, Location, Person}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x5175a6a984a6d8f92622afd3d987f5b778f5741c56d55ee5993cc368b9afee10; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x3074ab63db09d304ee45814a51e523dca4faf3bd695870286c9d1a8ac648797b; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release +const CONTRACT_ID = 0xebdd83d8e1fdacab31519c9e3e31f9c0d89d55addf137fb08eefe717b27ac416; // AUTO-CONTRACT-ID ../../test_contracts/abi_with_tuples_contract --release fn main() -> bool { let the_abi = abi(MyContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw index cd5925dcbd7..c3fb7775bec 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_basic_storage/src/main.sw @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad}; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x186c989267434c784f7785be57b0db0a490ec382bbf98a6108a840c63b99be99; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x9054333d06523ac906531d373a44e8530788ce80605f5c9b10df4df07185474c; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release +const CONTRACT_ID = 0x90a296126b5d457583c13911b77da9c3d677be6fab9853c062af3ac299307807; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release fn main() -> u64 { let addr = abi(BasicStorage, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw index 4e171ed5f16..f61fa0948a1 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_contract_with_type_aliases/src/main.sw @@ -5,7 +5,7 @@ use contract_with_type_aliases_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x0cbeb6efe3104b460be769bdc4ea101ebf16ccc16f2d7b667ec3e1c7f5ce35b5; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xb5f80b90e3cc37497f3b4b715ab1e60e3147c2818b3c2baebd7a2319cddbca20; // AUTO-CONTRACT-ID ../../test_contracts/contract_with_type_aliases --release +const CONTRACT_ID = 0x492686beae51c5bc145dd4c97f855960da2476cb8ce76e2eeb622f04a8872635; // AUTO-CONTRACT-ID ../../test_contracts/contract_with_type_aliases --release fn main() { let caller = abi(MyContract, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw index db1d7b35437..0cffc6d5b07 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_increment_contract/src/main.sw @@ -6,7 +6,7 @@ use dynamic_contract_call::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xd1b4047af7ef111c023ab71069e01dc2abfde487c0a0ce1268e4f447e6c6e4c2; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x713cb9dc1a1229ee12ddae923d01edd790a3d74991cfe6706dd2fe145de5cb8d; // AUTO-CONTRACT-ID ../../test_contracts/increment_contract --release +const CONTRACT_ID = 0x75eed289a08fbf6d0033e78b3875642e3846cbf0caa3b927028e727f6fda8a03; // AUTO-CONTRACT-ID ../../test_contracts/increment_contract --release fn main() -> bool { let the_abi = abi(Incrementor, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw index a320e2d5640..8e1c3642bd6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/call_storage_enum/src/main.sw @@ -5,7 +5,7 @@ use storage_enum_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xb54eb67f943fda15981308ef55b2cb2afe1ed5907c483d2d92d010ab39549644; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x01ef84bfa822ac64e71da6cf61db8a71213d9d0308509ae4d17488cf48bb5988; // AUTO-CONTRACT-ID ../../test_contracts/storage_enum_contract --release +const CONTRACT_ID = 0x11d9e436e9fe7f2f42d54e3cfb92bb0f51190f4dae98da838680063dfb322842; // AUTO-CONTRACT-ID ../../test_contracts/storage_enum_contract --release fn main() -> u64 { let caller = abi(StorageEnum, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw index b513c867043..dc2ca052d0b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_auth_test/src/main.sw @@ -5,7 +5,7 @@ use auth_testing_abi::AuthTesting; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xc2eec20491b53aab7232cbd27c31d15417b4e9daf0b89c74cc242ef1295f681f; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x63aca707cebd7e52d0158214d7e21a7f9f7e909e75c49167e59abf9ed684a98e; // AUTO-CONTRACT-ID ../../test_contracts/auth_testing_contract --release +const CONTRACT_ID = 0xf3b68b9e2ade5b886f4d2bd22bbfd928d418f9532a3a185956c1ca472a04dc0e; // AUTO-CONTRACT-ID ../../test_contracts/auth_testing_contract --release // should be false in the case of a script fn main() -> bool { diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw index d3df82b5b60..76b2d41167b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/caller_context_test/src/main.sw @@ -6,7 +6,7 @@ use context_testing_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x6054c11cda000f5990373a4d61929396165be4dfdd61d5b7bd26da60ab0d8577; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xdfc9f94f537270b794b9637ee5dc51763b07f44d0a2007c77de650108cc3b897; // AUTO-CONTRACT-ID ../../test_contracts/context_testing_contract --release +const CONTRACT_ID = 0x35322be00b08676502fe28a25c7c2095c32e1b1d781529afd400a5d9cc1eaf0c; // AUTO-CONTRACT-ID ../../test_contracts/context_testing_contract --release fn main() -> bool { let gas: u64 = u64::max(); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw index 509602a72fa..fd353f9c22d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/nested_struct_args_caller/src/main.sw @@ -5,7 +5,7 @@ use nested_struct_args_abi::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0xe63d33a1b3a6903808b379f6a41a72fa8a370e8b76626775e7d9d2f9c4c5da40; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0x316c68593dfc369cdeaa707b416022a5186e5222aff745dfa963b9eb077b88bf; // AUTO-CONTRACT-ID ../../test_contracts/nested_struct_args_contract --release +const CONTRACT_ID = 0x35f286cf49aa8ecab05e8f41852cb461145ec6e39205dbd073726922a49803e1; // AUTO-CONTRACT-ID ../../test_contracts/nested_struct_args_contract --release fn main() -> bool { let caller = abi(NestedStructArgs, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw index afbcdb716a1..62a96c7500b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/require_contract_deployment/storage_access_caller/src/main.sw @@ -6,7 +6,7 @@ use std::hash::*; #[cfg(experimental_new_encoding = false)] const CONTRACT_ID = 0x3bc28acd66d327b8c1b9624c1fabfc07e9ffa1b5d71c2832c3bfaaf8f4b805e9; #[cfg(experimental_new_encoding = true)] -const CONTRACT_ID = 0xe57d3bb9924ada4b9a9a9ed1965a254362926c24f176ede505bc19d2a3283716; // AUTO-CONTRACT-ID ../../test_contracts/storage_access_contract --release +const CONTRACT_ID = 0x07b8d231a9ac4216d950511533d28a3713c7c19f20d47f6f74ab653e38d03264; // AUTO-CONTRACT-ID ../../test_contracts/storage_access_contract --release fn main() -> bool { let caller = abi(StorageAccess, CONTRACT_ID); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw index 5021e69074d..d8c76d7d7f5 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/unit_tests/workspace_test/contract_multi_test/src/main.sw @@ -17,7 +17,7 @@ fn test_foo() { #[test(should_revert)] fn test_fail() { - let contract_id = 0x61e6641ec2ef2f69e5fcbdbb74b87e3c0f5570dcf24a37ce324033223c7eb464; // AUTO-CONTRACT-ID . + let contract_id = 0x9441eabeb185919c42dd33260bb9e92c7059222299d8e7f9ba004448b93b442e; // AUTO-CONTRACT-ID . let caller = abi(MyContract, contract_id); let result = caller.test_function {}(); assert(result == false) @@ -25,7 +25,7 @@ fn test_fail() { #[test] fn test_success() { - let contract_id = 0x61e6641ec2ef2f69e5fcbdbb74b87e3c0f5570dcf24a37ce324033223c7eb464; // AUTO-CONTRACT-ID . + let contract_id = 0x9441eabeb185919c42dd33260bb9e92c7059222299d8e7f9ba004448b93b442e; // AUTO-CONTRACT-ID . let caller = abi(MyContract, contract_id); let result = caller.test_function {}(); assert(result == true) From 837b70ae63ccc3949f5da1171036e75bac5b0901 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 24 Jun 2024 14:31:58 +0100 Subject: [PATCH 15/15] fix CI build --- sway-lib-core/src/codec.sw | 3 +-- sway-lib-core/src/raw_slice.sw | 2 -- sway-lib-std/src/bytes.sw | 31 ++++++++++++++++--------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/sway-lib-core/src/codec.sw b/sway-lib-core/src/codec.sw index 56446be522e..653299a46b9 100644 --- a/sway-lib-core/src/codec.sw +++ b/sway-lib-core/src/codec.sw @@ -5147,7 +5147,6 @@ where T::abi_decode(buffer) } - #[test] fn ok_abi_encoding() { // strings @@ -5205,4 +5204,4 @@ fn ok_abi_encoding() { let array = abi_decode::<[u8; 2]>(to_slice([255u8, 254u8])); assert_eq(array[0], 255u8); assert_eq(array[1], 254u8); -} \ No newline at end of file +} diff --git a/sway-lib-core/src/raw_slice.sw b/sway-lib-core/src/raw_slice.sw index 90c684e9261..13bd0affb90 100644 --- a/sway-lib-core/src/raw_slice.sw +++ b/sway-lib-core/src/raw_slice.sw @@ -160,5 +160,3 @@ impl raw_slice { into_parts(self).1 } } - - diff --git a/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index be34a532c1b..6eaf70be6b2 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -74,10 +74,12 @@ impl From for RawBytes { /// assert(raw_bytes.capacity == 3); /// ``` fn from(slice: raw_slice) -> Self { - Self { - ptr: slice.ptr(), - cap: slice.number_of_bytes(), + let cap = slice.number_of_bytes(); + let ptr = alloc_bytes(cap); + if cap > 0 { + slice.ptr().copy_to::(ptr, cap); } + Self { ptr, cap } } } @@ -826,9 +828,7 @@ impl From for raw_slice { /// assert(slice.number_of_bytes() == 3); /// ``` fn from(bytes: Bytes) -> raw_slice { - asm(ptr: (bytes.buf.ptr(), bytes.len)) { - ptr: raw_slice - } + bytes.as_raw_slice() } } @@ -925,14 +925,15 @@ impl AbiDecode for Bytes { } } +#[test] +fn ok_bytes_buffer_ownership() { + let mut original_array = [1u8, 2u8, 3u8, 4u8]; + let slice = raw_slice::from_parts::(__addr_of(original_array), 4); + // Check Bytes duplicates the original slice + let mut bytes = Bytes::from(slice); + bytes.set(0, 5); + assert(original_array[0] == 1); + +} -#[test] -fn xxx () { - let mut b = Bytes::new(); - b.push(1); - b.push(2); - - __log(encode(b)); - assert(false); -} \ No newline at end of file