Skip to content
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

Increase size of inputs, outputs and witnesses to uint16 #698

Merged
merged 8 commits into from
Mar 14, 2024
Merged
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
12 changes: 6 additions & 6 deletions .npm/packages/fuel-tx/index.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('fuel-tx [cjs]', () => {
})

it('should serialize and deserialize UtxoId correctly', () => {
let utxo_id = new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a");
let utxo_id = new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b00001a");
let bytes = utxo_id.to_bytes();
let utxo_id2 = tx.UtxoId.from_bytes(bytes);
expect(utxo_id.toString()).to.equal(utxo_id2.toString())
Expand All @@ -36,7 +36,7 @@ describe('fuel-tx [cjs]', () => {
it('should serialize and deserialize all input variants correctly', () => {
[
tx.Input.coin_predicate(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Address.zeroed(),
1234n,
tx.AssetId.zeroed(),
Expand All @@ -46,15 +46,15 @@ describe('fuel-tx [cjs]', () => {
[5, 6, 7, 8],
),
tx.Input.coin_signed(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Address.zeroed(),
BigInt(1234),
tx.AssetId.zeroed(),
new tx.TxPointer("0123456789ab"),
2,
),
tx.Input.contract(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Bytes32.zeroed(),
tx.Bytes32.zeroed(),
new tx.TxPointer("0123456789ab"),
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('fuel-tx [cjs]', () => {
[tx.Mint, tx.Transaction.mint(
new tx.TxPointer("0123456789ab"),
new tx.InputContract(
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18"),
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18ab"),
tx.Bytes32.zeroed(),
tx.Bytes32.zeroed(),
new tx.TxPointer("0123456789ab"),
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('fuel-tx [cjs]', () => {

it('should validate input correctly', () => {
let input = tx.Input.coin_signed(
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18"),
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18ab"),
tx.Address.from_bytes(hexToBytes("f1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e")),
10599410012256088338n,
tx.AssetId.from_bytes(hexToBytes("2cafad611543e0265d89f1c2b60d9ebf5d56ad7e23d9827d6b522fd4d6e44bc3")),
Expand Down
12 changes: 6 additions & 6 deletions .npm/packages/fuel-tx/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('fuel-tx [mjs]', () => {
})

it('should serialize and deserialize UtxoId correctly', () => {
let utxo_id = new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a");
let utxo_id = new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a");
let bytes = utxo_id.to_bytes();
let utxo_id2 = tx.UtxoId.from_bytes(bytes);
expect(utxo_id.toString()).to.equal(utxo_id2.toString())
Expand All @@ -36,7 +36,7 @@ describe('fuel-tx [mjs]', () => {
it('should serialize and deserialize all input variants correctly', () => {
[
tx.Input.coin_predicate(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Address.zeroed(),
BigInt(1234),
tx.AssetId.zeroed(),
Expand All @@ -46,15 +46,15 @@ describe('fuel-tx [mjs]', () => {
[5, 6, 7, 8],
),
tx.Input.coin_signed(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Address.zeroed(),
BigInt(1234),
tx.AssetId.zeroed(),
new tx.TxPointer("0123456789ab"),
2,
),
tx.Input.contract(
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b1a"),
new tx.UtxoId("0x0c0000000000000000000000000000000000000000000000000000000000000b001a"),
tx.Bytes32.zeroed(),
tx.Bytes32.zeroed(),
new tx.TxPointer("0123456789ab"),
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('fuel-tx [mjs]', () => {
[tx.Mint, tx.Transaction.mint(
new tx.TxPointer("0123456789ab"),
new tx.InputContract(
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18"),
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18ab"),
tx.Bytes32.zeroed(),
tx.Bytes32.zeroed(),
new tx.TxPointer("0123456789ab"),
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('fuel-tx [mjs]', () => {

it('should validate input correctly', () => {
let input = tx.Input.coin_signed(
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18"),
new tx.UtxoId("0xc49d65de61cf04588a764b557d25cc6c6b4bc0d7429227e2a21e61c213b3a3e2:18ab"),
tx.Address.from_bytes(hexToBytes("f1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e")),
10599410012256088338n,
tx.AssetId.from_bytes(hexToBytes("2cafad611543e0265d89f1c2b60d9ebf5d56ad7e23d9827d6b522fd4d6e44bc3")),
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
#### Breaking

- [#692](https://github.com/FuelLabs/fuel-vm/pull/692): Add GTF getters for tx size and address.
- [#698](https://github.com/FuelLabs/fuel-vm/pull/698): Store input, output and witness limits to u16, while keeping the values limited to 255.

## [Version 0.47.1]

Expand Down
14 changes: 9 additions & 5 deletions fuel-tx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
input,
output,
transaction::{
field,
field::{
self,
BytecodeLength,
BytecodeWitnessIndex,
Maturity,
Expand Down Expand Up @@ -110,7 +110,7 @@ pub struct TransactionBuilder<Tx> {
// We take the key by reference so this lib won't have the responsibility to properly
// zeroize the keys
// Maps signing keys -> witness indexes
sign_keys: BTreeMap<SecretKey, u8>,
sign_keys: BTreeMap<SecretKey, u16>,
}

impl TransactionBuilder<Script> {
Expand Down Expand Up @@ -401,9 +401,13 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
}

/// Adds a secret to the builder, and adds a corresponding witness if it's a new entry
fn upsert_secret(&mut self, secret_key: SecretKey) -> u8 {
let witness_len = u8::try_from(self.witnesses().len())
.expect("The number of witnesses can't exceed `u8::MAX`");
fn upsert_secret(&mut self, secret_key: SecretKey) -> u16 {
let witness_len = u16::try_from(self.witnesses().len())
.expect("The number of witnesses can't exceed `u16::MAX`");

if u32::from(witness_len) > self.params.tx_params.max_witnesses {
panic!("Max witnesses exceeded");
}

let witness_index = self.sign_keys.entry(secret_key).or_insert_with(|| {
// if this private key hasn't been used before,
Expand Down
4 changes: 2 additions & 2 deletions fuel-tx/src/tests/valid_cases/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn input_coin_message_signature() {
amount,
asset_id,
tx_pointer,
witness_index as u8,
witness_index as u16,
)
})
.expect("Failed to validate transaction");
Expand All @@ -125,7 +125,7 @@ fn input_coin_message_signature() {
nonce,
amount,
data.clone(),
witness_index as u8,
witness_index as u16,
)
})
.expect("Failed to validate transaction");
Expand Down
6 changes: 3 additions & 3 deletions fuel-tx/src/tests/valid_cases/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn contract() {
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand All @@ -48,7 +48,7 @@ fn contract() {
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand All @@ -68,7 +68,7 @@ fn contract() {
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand Down
12 changes: 6 additions & 6 deletions fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Transaction {
}

pub fn create(
bytecode_witness_index: u8,
bytecode_witness_index: u16,
policies: Policies,
salt: Salt,
mut storage_slots: Vec<StorageSlot>,
Expand Down Expand Up @@ -366,7 +366,7 @@ pub trait Executable: field::Inputs + field::Outputs + field::Witnesses {
amount: Word,
asset_id: AssetId,
tx_pointer: TxPointer,
witness_index: u8,
witness_index: u16,
) {
let owner = Input::owner(owner);

Expand Down Expand Up @@ -396,7 +396,7 @@ pub trait Executable: field::Inputs + field::Outputs + field::Witnesses {
nonce: Nonce,
amount: Word,
data: Vec<u8>,
witness_index: u8,
witness_index: u16,
) {
let input = if data.is_empty() {
Input::message_coin_signed(sender, recipient, amount, nonce, witness_index)
Expand Down Expand Up @@ -702,8 +702,8 @@ pub mod field {
}

pub trait BytecodeWitnessIndex {
fn bytecode_witness_index(&self) -> &u8;
fn bytecode_witness_index_mut(&mut self) -> &mut u8;
fn bytecode_witness_index(&self) -> &u16;
fn bytecode_witness_index_mut(&mut self) -> &mut u16;
fn bytecode_witness_index_offset(&self) -> usize {
Self::bytecode_witness_index_offset_static()
}
Expand Down Expand Up @@ -873,7 +873,7 @@ pub mod typescript {

#[wasm_bindgen]
pub fn create(
bytecode_witness_index: u8,
bytecode_witness_index: u16,
policies: Policies,
salt: crate::Salt,
storage_slots: Vec<crate::StorageSlot>,
Expand Down
12 changes: 6 additions & 6 deletions fuel-tx/src/transaction/consensus_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ impl Default for PredicateParameters {
#[cfg_attr(feature = "serde", serde(default))]
pub struct TxParameters {
/// Maximum number of inputs.
pub max_inputs: u8,
pub max_inputs: u16,
/// Maximum number of outputs.
pub max_outputs: u8,
pub max_outputs: u16,
/// Maximum number of witnesses.
pub max_witnesses: u32,
/// Maximum gas per transaction.
Expand Down Expand Up @@ -257,13 +257,13 @@ impl TxParameters {
}

/// Replace the max inputs with the given argument
pub const fn with_max_inputs(mut self, max_inputs: u8) -> Self {
pub const fn with_max_inputs(mut self, max_inputs: u16) -> Self {
self.max_inputs = max_inputs;
self
}

/// Replace the max outputs with the given argument
pub const fn with_max_outputs(mut self, max_outputs: u8) -> Self {
pub const fn with_max_outputs(mut self, max_outputs: u16) -> Self {
self.max_outputs = max_outputs;
self
}
Expand Down Expand Up @@ -400,8 +400,8 @@ pub mod default_parameters {
use fuel_types::ChainId;

pub const CONTRACT_MAX_SIZE: u64 = ContractParameters::DEFAULT.contract_max_size;
pub const MAX_INPUTS: u8 = TxParameters::DEFAULT.max_inputs;
pub const MAX_OUTPUTS: u8 = TxParameters::DEFAULT.max_outputs;
pub const MAX_INPUTS: u16 = TxParameters::DEFAULT.max_inputs;
pub const MAX_OUTPUTS: u16 = TxParameters::DEFAULT.max_outputs;
pub const MAX_WITNESSES: u32 = TxParameters::DEFAULT.max_witnesses;
pub const MAX_GAS_PER_TX: u64 = TxParameters::DEFAULT.max_gas_per_tx;

Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/transaction/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub trait Chargeable: field::Inputs + field::Witnesses + field::Policies {

/// Returns the gas used by the inputs.
fn gas_used_by_inputs(&self, gas_costs: &GasCosts) -> Word {
let mut witness_cache: HashSet<u8> = HashSet::new();
let mut witness_cache: HashSet<u16> = HashSet::new();
self.inputs()
.iter()
.filter(|input| match input {
Expand Down
16 changes: 7 additions & 9 deletions fuel-tx/src/transaction/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ mod tests {

fn invert_utxo_id(utxo_id: &mut UtxoId) {
let mut tx_id = *utxo_id.tx_id();
let mut out_idx = [utxo_id.output_index()];

invert(&mut tx_id);
invert(&mut out_idx);
let out_idx = utxo_id.output_index().not();

*utxo_id = UtxoId::new(tx_id, out_idx[0])
*utxo_id = UtxoId::new(tx_id, out_idx)
}

fn invert_storage_slot(storage_slot: &mut StorageSlot) {
Expand Down Expand Up @@ -609,7 +607,7 @@ mod tests {
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
),
Input::coin_predicate(
rng.gen(),
Expand All @@ -627,7 +625,7 @@ mod tests {
rng.gen(),
rng.next_u64(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
),
Input::message_coin_predicate(
rng.gen(),
Expand All @@ -643,7 +641,7 @@ mod tests {
rng.gen(),
rng.next_u64(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.gen(),
generate_nonempty_padded_bytes(rng),
),
Input::message_data_predicate(
Expand All @@ -663,7 +661,7 @@ mod tests {
vec![],
vec![
Output::coin(rng.gen(), rng.next_u64(), rng.gen()),
Output::contract(rng.next_u32().to_be_bytes()[0], rng.gen(), rng.gen()),
Output::contract(rng.gen(), rng.gen(), rng.gen()),
Output::change(rng.gen(), rng.next_u64(), rng.gen()),
Output::variable(rng.gen(), rng.next_u64(), rng.gen()),
Output::contract_created(rng.gen(), rng.gen()),
Expand Down Expand Up @@ -705,7 +703,7 @@ mod tests {

for storage_slots in storage_slots.iter() {
let tx = Transaction::create(
rng.next_u32().to_be_bytes()[0],
rng.gen(),
rng.gen(),
rng.gen(),
storage_slots.clone(),
Expand Down
6 changes: 3 additions & 3 deletions fuel-tx/src/transaction/types/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl CreateMetadata {
#[derivative(Eq, PartialEq, Hash)]
pub struct Create {
pub(crate) bytecode_length: Word,
pub(crate) bytecode_witness_index: u8,
pub(crate) bytecode_witness_index: u16,
pub(crate) policies: Policies,
pub(crate) storage_slots: Vec<StorageSlot>,
pub(crate) inputs: Vec<Input>,
Expand Down Expand Up @@ -373,12 +373,12 @@ mod field {

impl BytecodeWitnessIndex for Create {
#[inline(always)]
fn bytecode_witness_index(&self) -> &u8 {
fn bytecode_witness_index(&self) -> &u16 {
&self.bytecode_witness_index
}

#[inline(always)]
fn bytecode_witness_index_mut(&mut self) -> &mut u8 {
fn bytecode_witness_index_mut(&mut self) -> &mut u16 {
&mut self.bytecode_witness_index
}

Expand Down
Loading
Loading