Skip to content

Commit

Permalink
Switch to Bytes type for send_message data param (FuelLabs#3625)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfurfaro authored Dec 19, 2022
1 parent f93b64b commit 495ad00
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ dep flags;
dep u128;
dep u256;
dep vec;
dep message;
dep bytes;
dep message;
dep prelude;

use core::*;
22 changes: 12 additions & 10 deletions sway-lib-std/src/message.sw
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
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
///
/// * `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<u64>, 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.
// Otherwise, we allocate adjacent space on the heap for the data and the recipient and copy the
// data and recipient values there
if !msg_data.is_empty() {
size = msg_data.len();
recipient_heap_buffer = alloc::<u64>(4 + size);
recipient_heap_buffer.write(recipient);
let data_heap_buffer = recipient_heap_buffer.add::<b256>(1);
msg_data.buf.ptr.copy_to::<u64>(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::<b256>(1);
msg_data.buf.ptr.copy_bytes_to(data_pointer, size);
}

let mut index = 0;
let outputs = output_count();

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);
}

Expand Down
7 changes: 2 additions & 5 deletions test/src/sdk-harness/test_projects/token_ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 9 additions & 2 deletions test/src/sdk-harness/test_projects/token_ops/src/main.sw
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -53,6 +53,13 @@ impl TestFuelCoin for Contract {
}

fn send_message(recipient: b256, msg_data: Vec<u64>, 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);
}
}

0 comments on commit 495ad00

Please sign in to comment.