Skip to content

Add helpers for read-only inputs #7101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: feat/data-utxo
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ docs/reference/src/code/examples/**/*/Forc.lock

# Insta files
*.snap.new

# Rust Rover IDE configurations
.idea
25 changes: 25 additions & 0 deletions sway-lib-std/src/codec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const GTF_INPUT_TYPE = 0x200;
const INPUT_COIN = 0u8;
const INPUT_MESSAGE = 2u8;
const INPUT_DATA_COIN = 3u8;
const INPUT_READ_ONLY_COIN = 4u8;
const INPUT_READ_ONLY_DATA_COIN = 5u8;
const INPUT_READ_ONLY_COIN_PREDICATE = 6u8;
const INPUT_READ_ONLY_DATA_COIN_PREDICATE = 7u8;

const GTF_OUTPUT_TYPE = 0x300;
const OUTPUT_DATA_COIN = 5u8;
const GTF_INPUT_COIN_PREDICATE_DATA_LENGTH = 0x20A;
Expand Down Expand Up @@ -121,6 +126,16 @@ impl BufferReader {
let _len = __gtf::<u64>(predicate_index, GTF_INPUT_COIN_PREDICATE_DATA_LENGTH);
BufferReader { ptr }
},
INPUT_READ_ONLY_COIN_PREDICATE => {
let ptr = __gtf::<raw_ptr>(predicate_index, GTF_INPUT_COIN_PREDICATE_DATA);
let _len = __gtf::<u64>(predicate_index, GTF_INPUT_COIN_PREDICATE_DATA_LENGTH);
BufferReader { ptr }
},
INPUT_READ_ONLY_DATA_COIN_PREDICATE => {
let ptr = __gtf::<raw_ptr>(predicate_index, GTF_INPUT_COIN_PREDICATE_DATA);
let _len = __gtf::<u64>(predicate_index, GTF_INPUT_COIN_PREDICATE_DATA_LENGTH);
BufferReader { ptr }
},
_ => __revert(0),
}
}
Expand All @@ -132,6 +147,16 @@ impl BufferReader {
let _len = __gtf::<u64>(data_coin_index, GTF_INPUT_DATA_COIN_DATA_LENGTH);
BufferReader { ptr }
},
INPUT_READ_ONLY_DATA_COIN => {
let ptr = __gtf::<raw_ptr>(data_coin_index, GTF_INPUT_DATA_COIN_DATA);
let _len = __gtf::<u64>(data_coin_index, GTF_INPUT_DATA_COIN_DATA_LENGTH);
BufferReader { ptr }
},
INPUT_READ_ONLY_DATA_COIN_PREDICATE => {
let ptr = __gtf::<raw_ptr>(data_coin_index, GTF_INPUT_DATA_COIN_DATA);
let _len = __gtf::<u64>(data_coin_index, GTF_INPUT_DATA_COIN_DATA_LENGTH);
BufferReader { ptr }
},
_ => __revert(0),
}
}
Expand Down
33 changes: 25 additions & 8 deletions sway-lib-std/src/inputs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ pub enum Input {
Contract: (),
/// A message input.
Message: (),
/// A read-only input.
ReadOnly: ReadOnlyInput,
}

pub enum ReadOnlyInput {
Coin: (),
DataCoin: (),
CoinPredicate: (),
DataCoinPredicate: (),
}

impl PartialEq for Input {
Expand All @@ -71,6 +80,10 @@ impl PartialEq for Input {
(Input::DataCoin, Input::DataCoin) => true,
(Input::Contract, Input::Contract) => true,
(Input::Message, Input::Message) => true,
(Input::ReadOnly(ReadOnlyInput::Coin), Input::ReadOnly(ReadOnlyInput::Coin)) => true,
(Input::ReadOnly(ReadOnlyInput::DataCoin), Input::ReadOnly(ReadOnlyInput::DataCoin)) => true,
(Input::ReadOnly(ReadOnlyInput::CoinPredicate), Input::ReadOnly(ReadOnlyInput::CoinPredicate)) => true,
(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate), Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => true,
_ => false,
}
}
Expand Down Expand Up @@ -113,6 +126,10 @@ pub fn input_type(index: u64) -> Option<Input> {
1u8 => Some(Input::Contract),
2u8 => Some(Input::Message),
3u8 => Some(Input::DataCoin),
4u8 => Some(Input::ReadOnly(ReadOnlyInput::Coin)),
5u8 => Some(Input::ReadOnly(ReadOnlyInput::DataCoin)),
6u8 => Some(Input::ReadOnly(ReadOnlyInput::CoinPredicate)),
7u8 => Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)),
_ => None,
}
}
Expand Down Expand Up @@ -205,7 +222,7 @@ fn input_pointer(index: u64) -> Option<raw_ptr> {
/// ```
pub fn input_amount(index: u64) -> Option<u64> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_AMOUNT)),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(_)) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_AMOUNT)),
Some(Input::Message) => Some(__gtf::<u64>(index, GTF_INPUT_MESSAGE_AMOUNT)),
_ => None,
}
Expand Down Expand Up @@ -302,7 +319,7 @@ where
T: AbiDecode,
{
match input_type(index) {
Some(Input::DataCoin) => Some(decode_data_coin_data_by_index::<T>(index)),
Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::DataCoin)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(decode_data_coin_data_by_index::<T>(index)),
_ => None,
}
}
Expand All @@ -329,7 +346,7 @@ where
/// ```
pub fn input_asset_id(index: u64) -> Option<AssetId> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(AssetId::from(__gtf::<b256>(index, GTF_INPUT_COIN_ASSET_ID))),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(_)) => Some(AssetId::from(__gtf::<b256>(index, GTF_INPUT_COIN_ASSET_ID))),
Some(Input::Message) => Some(AssetId::base()),
_ => None,
}
Expand Down Expand Up @@ -357,7 +374,7 @@ pub fn input_asset_id(index: u64) -> Option<AssetId> {
/// ```
pub fn input_witness_index(index: u64) -> Option<u16> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(__gtf::<u16>(index, GTF_INPUT_COIN_WITNESS_INDEX)),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::CoinPredicate)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(__gtf::<u16>(index, GTF_INPUT_COIN_WITNESS_INDEX)),
Some(Input::Message) => Some(__gtf::<u16>(index, GTF_INPUT_MESSAGE_WITNESS_INDEX)),
_ => None,
}
Expand Down Expand Up @@ -385,7 +402,7 @@ pub fn input_witness_index(index: u64) -> Option<u16> {
/// ```
pub fn input_predicate_length(index: u64) -> Option<u64> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_PREDICATE_LENGTH)),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::CoinPredicate)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_PREDICATE_LENGTH)),
Some(Input::Message) => Some(__gtf::<u64>(index, GTF_INPUT_MESSAGE_PREDICATE_LENGTH)),
_ => None,
}
Expand Down Expand Up @@ -413,7 +430,7 @@ pub fn input_predicate_length(index: u64) -> Option<u64> {
/// ```
fn input_predicate_pointer(index: u64) -> Option<raw_ptr> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(__gtf::<raw_ptr>(index, GTF_INPUT_COIN_PREDICATE)),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::CoinPredicate)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(__gtf::<raw_ptr>(index, GTF_INPUT_COIN_PREDICATE)),
Some(Input::Message) => Some(__gtf::<raw_ptr>(index, GTF_INPUT_MESSAGE_PREDICATE)),
_ => None,
}
Expand Down Expand Up @@ -478,15 +495,15 @@ pub fn input_predicate(index: u64) -> Option<Bytes> {
/// ```
pub fn input_predicate_data_length(index: u64) -> Option<u64> {
match input_type(index) {
Some(Input::Coin) | Some(Input::DataCoin) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_PREDICATE_DATA_LENGTH)),
Some(Input::Coin) | Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::CoinPredicate)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(__gtf::<u64>(index, GTF_INPUT_COIN_PREDICATE_DATA_LENGTH)),
Some(Input::Message) => Some(__gtf::<u64>(index, GTF_INPUT_MESSAGE_PREDICATE_DATA_LENGTH)),
_ => None,
}
}

pub fn input_data_coin_data_length(index: u64) -> Option<u64> {
match input_type(index) {
Some(Input::DataCoin) => Some(__gtf::<u64>(index, GTF_INPUT_DATA_COIN_DATA_LENGTH)),
Some(Input::DataCoin) | Some(Input::ReadOnly(ReadOnlyInput::DataCoin)) | Some(Input::ReadOnly(ReadOnlyInput::DataCoinPredicate)) => Some(__gtf::<u64>(index, GTF_INPUT_DATA_COIN_DATA_LENGTH)),
_ => None,
}
}
Expand Down
Loading