forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamped implementation of
send_message
(FuelLabs#2796)
This updates the wrapper for the `smo` opcode to follow the example of the wrapper for the `tro` opcode (send_message_output()`). closes FuelLabs#2899 Co-authored-by: Braqzen <[email protected]> Co-authored-by: Braqzen <[email protected]> Co-authored-by: Mohammad Fawaz <[email protected]>
- Loading branch information
1 parent
438aa02
commit dd2104e
Showing
7 changed files
with
160 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ dep flags; | |
dep u128; | ||
dep u256; | ||
dep vec; | ||
dep message; | ||
dep prelude; | ||
|
||
use core::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
library message; | ||
|
||
use ::alloc::alloc; | ||
use ::outputs::{Output, output_count, output_type}; | ||
use ::mem::{addr_of, copy}; | ||
use ::revert::revert; | ||
use ::vec::Vec; | ||
|
||
const FAILED_SEND_MESSAGE_SIGNAL = 0xffff_ffff_ffff_0002; | ||
|
||
/// Sends a message to `recipient` of length `msg_len` through `output` with amount of `coins` | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `recipient` - The address of the message recipient | ||
/// * `msg_data` - arbitrary length message data | ||
/// * `coins` - Amount of base asset sent | ||
pub fn send_message(recipient: b256, msg_data: Vec<u64>, coins: u64) { | ||
let mut recipient_heap_buffer = 0; | ||
let mut data_heap_buffer = 0; | ||
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() { | ||
recipient_heap_buffer = addr_of(recipient); | ||
} else { | ||
size = msg_data.len() * 8; | ||
data_heap_buffer = alloc(size); | ||
recipient_heap_buffer = alloc(32); | ||
copy(msg_data.buf.ptr, data_heap_buffer, size); | ||
copy(addr_of(recipient), recipient_heap_buffer, 32); | ||
}; | ||
|
||
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, r3: index, r4: coins) { | ||
smo r1 r2 r3 r4; | ||
}; | ||
return; | ||
} | ||
index += 1; | ||
} | ||
revert(FAILED_SEND_MESSAGE_SIGNAL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters