From 495ad00b734b19cb21234a7e03042a4e11ca763c Mon Sep 17 00:00:00 2001 From: Nick Furfaro Date: Mon, 19 Dec 2022 15:46:47 -0700 Subject: [PATCH] Switch to `Bytes` type for `send_message` data param (#3625) --- sway-lib-std/src/lib.sw | 2 +- sway-lib-std/src/message.sw | 22 ++++++++++--------- .../test_projects/token_ops/mod.rs | 7 ++---- .../test_projects/token_ops/src/main.sw | 11 ++++++++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/sway-lib-std/src/lib.sw b/sway-lib-std/src/lib.sw index 8c477e27237..0681c0fa3e2 100644 --- a/sway-lib-std/src/lib.sw +++ b/sway-lib-std/src/lib.sw @@ -34,8 +34,8 @@ dep flags; dep u128; dep u256; dep vec; -dep message; dep bytes; +dep message; dep prelude; use core::*; diff --git a/sway-lib-std/src/message.sw b/sway-lib-std/src/message.sw index 76b5a002d51..1a829cce509 100644 --- a/sway-lib-std/src/message.sw +++ b/sway-lib-std/src/message.sw @@ -1,11 +1,12 @@ library message; -use ::alloc::alloc; +use ::alloc::alloc_bytes; +use ::bytes::Bytes; use ::outputs::{Output, output_count, output_type}; use ::revert::revert; -use ::vec::Vec; use ::error_signals::FAILED_SEND_MESSAGE_SIGNAL; + /// Sends a message `msg_data` to `recipient` with a `coins` amount of the base asset /// /// ### Arguments @@ -13,8 +14,8 @@ use ::error_signals::FAILED_SEND_MESSAGE_SIGNAL; /// * `recipient` - The address of the message recipient /// * `msg_data` - Arbitrary length message data /// * `coins` - Amount of base asset to send -pub fn send_message(recipient: b256, msg_data: Vec, coins: u64) { - let mut recipient_heap_buffer = __addr_of(recipient); +pub fn send_message(recipient: b256, msg_data: Bytes, coins: u64) { + let mut recipient_and_msg_data_pointer = __addr_of(recipient); let mut size = 0; // If msg_data is empty, we just ignore it and pass `smo` a pointer to the inner value of recipient. @@ -22,11 +23,11 @@ pub fn send_message(recipient: b256, msg_data: Vec, coins: u64) { // data and recipient values there if !msg_data.is_empty() { size = msg_data.len(); - recipient_heap_buffer = alloc::(4 + size); - recipient_heap_buffer.write(recipient); - let data_heap_buffer = recipient_heap_buffer.add::(1); - msg_data.buf.ptr.copy_to::(data_heap_buffer, size); - }; + recipient_and_msg_data_pointer = alloc_bytes(32 + size); + recipient_and_msg_data_pointer.write(recipient); + let data_pointer = recipient_and_msg_data_pointer.add::(1); + msg_data.buf.ptr.copy_bytes_to(data_pointer, size); + } let mut index = 0; let outputs = output_count(); @@ -34,13 +35,14 @@ pub fn send_message(recipient: b256, msg_data: Vec, coins: u64) { while index < outputs { let type_of_output = output_type(index); if let Output::Message = type_of_output { - asm(r1: recipient_heap_buffer, r2: size * 8, r3: index, r4: coins) { + asm(r1: recipient_and_msg_data_pointer, r2: size, r3: index, r4: coins) { smo r1 r2 r3 r4; }; return; } index += 1; } + revert(FAILED_SEND_MESSAGE_SIGNAL); } diff --git a/test/src/sdk-harness/test_projects/token_ops/mod.rs b/test/src/sdk-harness/test_projects/token_ops/mod.rs index f63457c7b44..5e6a18c96bb 100644 --- a/test/src/sdk-harness/test_projects/token_ops/mod.rs +++ b/test/src/sdk-harness/test_projects/token_ops/mod.rs @@ -387,11 +387,8 @@ async fn can_send_message_output_with_data() { assert_eq!(*fuelcoin_id, **message_receipt.sender().unwrap()); assert_eq!(&recipient_address, message_receipt.recipient().unwrap()); assert_eq!(amount, message_receipt.amount().unwrap()); - assert_eq!(24, message_receipt.len().unwrap()); - assert_eq!( - vec![0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 75, 0, 0, 0, 0, 0, 0, 0, 50], - message_receipt.data().unwrap() - ); + assert_eq!(8, message_receipt.len().unwrap()); + assert_eq!(vec![100, 75, 50], message_receipt.data().unwrap()); } #[tokio::test] diff --git a/test/src/sdk-harness/test_projects/token_ops/src/main.sw b/test/src/sdk-harness/test_projects/token_ops/src/main.sw index 732e328d89c..0249b0b7648 100644 --- a/test/src/sdk-harness/test_projects/token_ops/src/main.sw +++ b/test/src/sdk-harness/test_projects/token_ops/src/main.sw @@ -1,6 +1,6 @@ contract; -use std::{context::balance_of, message::send_message, token::*}; +use std::{bytes::Bytes, context::balance_of, message::send_message, token::*}; abi TestFuelCoin { fn mint_coins(mint_amount: u64); @@ -53,6 +53,13 @@ impl TestFuelCoin for Contract { } fn send_message(recipient: b256, msg_data: Vec, coins: u64) { - send_message(recipient, msg_data, coins); + let mut data = Bytes::new(); + if msg_data.len() > 0 { + data.push(msg_data.get(0).unwrap()); + data.push(msg_data.get(1).unwrap()); + data.push(msg_data.get(2).unwrap()); + } + + send_message(recipient, data, coins); } }