Skip to content

Commit

Permalink
Rename ContractAddress to ContractId (#20)
Browse files Browse the repository at this point in the history
For a better consistency with the specs, its preferable we keep the same
names in the implementation and the documents.
  • Loading branch information
vlopes11 authored and xgreenx committed Dec 20, 2022
1 parent a5d8a9c commit 7bbc936
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 57 deletions.
3 changes: 1 addition & 2 deletions fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ pub mod consts;
pub mod crypto;

pub use transaction::{
Address, Color, ContractAddress, Hash, Input, Output, Salt, Transaction, ValidationError,
Witness,
Address, Color, ContractId, Hash, Input, Output, Salt, Transaction, ValidationError, Witness,
};
24 changes: 11 additions & 13 deletions fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{io, mem};
mod types;
mod validation;

pub use types::{Address, Color, ContractAddress, Hash, Input, Output, Salt, Witness};
pub use types::{Address, Color, ContractId, Hash, Input, Output, Salt, Witness};
pub use validation::ValidationError;

const WORD_SIZE: usize = mem::size_of::<Word>();
Expand Down Expand Up @@ -78,7 +78,7 @@ pub enum Transaction {
maturity: Word,
bytecode_witness_index: u8,
salt: Salt,
static_contracts: Vec<ContractAddress>,
static_contracts: Vec<ContractId>,
inputs: Vec<Input>,
outputs: Vec<Output>,
witnesses: Vec<Witness>,
Expand Down Expand Up @@ -127,9 +127,7 @@ impl bytes::SizedBytes for Transaction {

Self::Create {
static_contracts, ..
} => {
TRANSACTION_CREATE_FIXED_SIZE + static_contracts.len() * ContractAddress::size_of()
}
} => TRANSACTION_CREATE_FIXED_SIZE + static_contracts.len() * ContractId::size_of(),
};

n + inputs + outputs + witnesses
Expand Down Expand Up @@ -165,7 +163,7 @@ impl Transaction {
maturity: Word,
bytecode_witness_index: u8,
salt: Salt,
static_contracts: Vec<ContractAddress>,
static_contracts: Vec<ContractId>,
inputs: Vec<Input>,
outputs: Vec<Output>,
witnesses: Vec<Witness>,
Expand Down Expand Up @@ -241,7 +239,7 @@ impl Transaction {
});
}

pub fn input_contracts(&self) -> impl Iterator<Item = &ContractAddress> {
pub fn input_contracts(&self) -> impl Iterator<Item = &ContractId> {
self.inputs()
.iter()
.filter_map(|input| match input {
Expand Down Expand Up @@ -330,7 +328,7 @@ impl Transaction {
..
} => Some(
TRANSACTION_CREATE_FIXED_SIZE
+ ContractAddress::size_of() * static_contracts.len()
+ ContractId::size_of() * static_contracts.len()
+ inputs
.iter()
.take(index)
Expand Down Expand Up @@ -599,15 +597,15 @@ impl io::Write for Transaction {

let salt = salt.into();

if buf.len() < static_contracts_len * ContractAddress::size_of() {
if buf.len() < static_contracts_len * ContractId::size_of() {
return Err(bytes::eof());
}

let mut static_contracts = vec![ContractAddress::default(); static_contracts_len];
n += ContractAddress::size_of() * static_contracts_len;
let mut static_contracts = vec![ContractId::default(); static_contracts_len];
n += ContractId::size_of() * static_contracts_len;
for static_contract in static_contracts.iter_mut() {
static_contract.copy_from_slice(&buf[..ContractAddress::size_of()]);
buf = &buf[ContractAddress::size_of()..];
static_contract.copy_from_slice(&buf[..ContractId::size_of()]);
buf = &buf[ContractId::size_of()..];
}

let mut inputs = vec![Input::default(); inputs_len];
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/src/transaction/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ pub use witness::Witness;

key!(Address, 32);
key!(Color, 32);
key!(ContractAddress, 32);
key!(ContractId, 32);
key!(Hash, 32);
key!(Salt, 32);
8 changes: 4 additions & 4 deletions fuel-tx/src/transaction/types/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Address, Color, ContractAddress, Hash};
use super::{Address, Color, ContractId, Hash};
use crate::bytes::{self, SizedBytes};

use fuel_asm::Word;
Expand All @@ -22,7 +22,7 @@ const INPUT_CONTRACT_SIZE: usize = WORD_SIZE // Identifier
+ Hash::size_of() // UTXO Id
+ Hash::size_of() // Balance root
+ Hash::size_of() // State root
+ ContractAddress::size_of(); // Contract address
+ ContractId::size_of(); // Contract address

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum InputRepr {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub enum Input {
utxo_id: Hash,
balance_root: Hash,
state_root: Hash,
contract_id: ContractAddress,
contract_id: ContractId,
},
}

Expand Down Expand Up @@ -122,7 +122,7 @@ impl Input {
utxo_id: Hash,
balance_root: Hash,
state_root: Hash,
contract_id: ContractAddress,
contract_id: ContractId,
) -> Self {
Self::Contract {
utxo_id,
Expand Down
8 changes: 4 additions & 4 deletions fuel-tx/src/transaction/types/output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Address, Color, ContractAddress, Hash};
use super::{Address, Color, ContractId, Hash};
use crate::bytes::{self, SizedBytes};

use fuel_asm::Word;
Expand All @@ -19,7 +19,7 @@ const OUTPUT_CONTRACT_SIZE: usize = WORD_SIZE // Identifier
+ Hash::size_of(); // State root

const OUTPUT_CONTRACT_CREATED_SIZE: usize = WORD_SIZE // Identifier
+ ContractAddress::size_of(); // Contract Id
+ ContractId::size_of(); // Contract Id

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum OutputRepr {
Expand Down Expand Up @@ -96,7 +96,7 @@ pub enum Output {
},

ContractCreated {
contract_id: ContractAddress,
contract_id: ContractId,
},
}

Expand Down Expand Up @@ -148,7 +148,7 @@ impl Output {
Self::Variable { to, amount, color }
}

pub const fn contract_created(contract_id: ContractAddress) -> Self {
pub const fn contract_created(contract_id: ContractId) -> Self {
Self::ContractCreated { contract_id }
}

Expand Down
24 changes: 12 additions & 12 deletions fuel-tx/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn input() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
]);
}
Expand All @@ -143,7 +143,7 @@ fn output() {
Output::withdrawal(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::change(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::variable(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::contract_created(ContractAddress::random(rng)),
Output::contract_created(ContractId::random(rng)),
]);
}

Expand All @@ -156,7 +156,7 @@ fn transaction() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
);
let o = Output::coin(Address::random(rng), rng.next_u64(), Color::random(rng));
let w = Witness::random(rng);
Expand Down Expand Up @@ -238,7 +238,7 @@ fn transaction() {
rng.next_u64(),
rng.next_u32().to_be_bytes()[0],
Salt::random(rng),
vec![ContractAddress::random(rng)],
vec![ContractId::random(rng)],
vec![i.clone()],
vec![o.clone()],
vec![w.clone()],
Expand Down Expand Up @@ -301,25 +301,25 @@ fn create_input_coin_data_offset() {
let bytecode_witness_index = 0x00;
let salt = Salt::random(rng);

let static_contracts: Vec<Vec<ContractAddress>> = vec![
let static_contracts: Vec<Vec<ContractId>> = vec![
vec![],
vec![ContractAddress::random(rng)],
vec![ContractAddress::random(rng), ContractAddress::random(rng)],
vec![ContractId::random(rng)],
vec![ContractId::random(rng), ContractId::random(rng)],
];
let inputs: Vec<Vec<Input>> = vec![
vec![],
vec![Input::contract(
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)],
vec![
Input::contract(
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng)
ContractId::random(rng)
);
2
],
Expand Down Expand Up @@ -417,20 +417,20 @@ fn script_input_coin_data_offset() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)],
vec![
Input::contract(
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
Input::contract(
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
],
];
Expand Down
6 changes: 3 additions & 3 deletions fuel-tx/tests/txid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn id() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
],
];
Expand All @@ -164,7 +164,7 @@ fn id() {
Output::withdrawal(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::change(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::variable(Address::random(rng), rng.next_u64(), Color::random(rng)),
Output::contract_created(ContractAddress::random(rng)),
Output::contract_created(ContractId::random(rng)),
],
];

Expand All @@ -182,7 +182,7 @@ fn id() {
];
let static_contracts = vec![
vec![],
vec![ContractAddress::random(rng), ContractAddress::random(rng)],
vec![ContractId::random(rng), ContractId::random(rng)],
];

for inputs in inputs.iter() {
Expand Down
8 changes: 4 additions & 4 deletions fuel-tx/tests/valid_cases/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)
.validate(
1,
Expand All @@ -110,7 +110,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)
.validate(1, &[], &[])
.err()
Expand All @@ -124,7 +124,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)
.validate(
1,
Expand All @@ -146,7 +146,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
)
.validate(
1,
Expand Down
8 changes: 4 additions & 4 deletions fuel-tx/tests/valid_cases/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
],
)
Expand All @@ -59,7 +59,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
],
)
Expand All @@ -85,7 +85,7 @@ fn contract() {
Hash::random(rng),
Hash::random(rng),
Hash::random(rng),
ContractAddress::random(rng),
ContractId::random(rng),
),
],
)
Expand Down Expand Up @@ -129,7 +129,7 @@ fn contract_created() {
let mut rng_base = StdRng::seed_from_u64(8586);
let rng = &mut rng_base;

Output::contract_created(ContractAddress::random(rng))
Output::contract_created(ContractId::random(rng))
.validate(1, &[])
.unwrap();
}
Loading

0 comments on commit 7bbc936

Please sign in to comment.