Skip to content

Commit

Permalink
Remove ChainId from PredicateId calculation (#604)
Browse files Browse the repository at this point in the history
* Remove `ChainId` from `PredicateId` calculation

* Update CHANGELOG.md
  • Loading branch information
xgreenx authored Oct 9, 2023
1 parent 028ff72 commit 4bf0a35
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 190 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Breaking

- [#604](https://github.com/FuelLabs/fuel-vm/pull/604): Removed `ChainId` from `PredicateId` calculation. It changes the generated address of the predicates and may break tests or logic that uses hard-coded predicate IDs.
- [#594](https://github.com/FuelLabs/fuel-vm/pull/594): Add new predicate input validation tests. Also improves error propagation so that predicate error message better reflects the reason for invalidity.
- [#596](https://github.com/FuelLabs/fuel-vm/pull/596): Remove `core::ops::{Add, Sub}` impls from `BlockHeight`. Use `succ` and `pred` to access adjacent blocks, or perform arithmetic directly on the wrapped integer instead.

Expand Down
151 changes: 19 additions & 132 deletions fuel-tx/src/tests/valid_cases/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ fn input_coin_message_signature() {
outputs,
witnesses,
&Default::default(),
&chain_id,
&mut None,
),
_ => Ok(()),
Expand Down Expand Up @@ -210,7 +209,7 @@ fn coin_predicate() {
let txhash: Bytes32 = rng.gen();

let predicate = generate_nonempty_padded_bytes(rng);
let owner = Input::predicate_owner(&predicate, &ChainId::default());
let owner = Input::predicate_owner(&predicate);

Input::coin_predicate(
rng.gen(),
Expand All @@ -223,19 +222,11 @@ fn coin_predicate() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.unwrap();

let predicate = vec![];
let owner = Input::predicate_owner(&predicate, &ChainId::default());
let owner = Input::predicate_owner(&predicate);

let err = Input::coin_predicate(
rng.gen(),
Expand All @@ -248,15 +239,7 @@ fn coin_predicate() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.err()
.unwrap();

Expand All @@ -277,15 +260,7 @@ fn coin_predicate() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.err()
.unwrap();

Expand All @@ -298,30 +273,19 @@ fn contract() {

let txhash: Bytes32 = rng.gen();

let chain_id = ChainId::default();

Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen())
.check(
1,
&txhash,
&[Output::contract(1, rng.gen(), rng.gen())],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.unwrap();

let err = Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen())
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.err()
.unwrap();

Expand All @@ -337,7 +301,6 @@ fn contract() {
&[Output::coin(rng.gen(), rng.gen(), rng.gen())],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.err()
Expand All @@ -355,7 +318,6 @@ fn contract() {
&[Output::contract(2, rng.gen(), rng.gen())],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.err()
Expand All @@ -374,8 +336,7 @@ fn message_metadata() {
let txhash: Bytes32 = rng.gen();

let predicate = generate_nonempty_padded_bytes(rng);
let chain_id = ChainId::default();
let recipient = Input::predicate_owner(&predicate, &chain_id);
let recipient = Input::predicate_owner(&predicate);

Input::message_data_predicate(
rng.gen(),
Expand All @@ -387,15 +348,7 @@ fn message_metadata() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect("failed to validate empty message input");

let mut tx = Script::default();
Expand All @@ -422,7 +375,7 @@ fn message_metadata() {
assert_eq!(CheckError::InputWitnessIndexBounds { index: 0 }, err,);

let mut predicate = generate_nonempty_padded_bytes(rng);
let recipient = Input::predicate_owner(&predicate, &chain_id);
let recipient = Input::predicate_owner(&predicate);
predicate[0] = predicate[0].wrapping_add(1);

let err = Input::message_data_predicate(
Expand All @@ -435,15 +388,7 @@ fn message_metadata() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("Expected failure");

assert_eq!(CheckError::InputPredicateOwner { index: 1 }, err);
Expand All @@ -464,7 +409,6 @@ fn message_metadata() {
&[],
&[vec![].into()],
&Default::default(),
&chain_id,
&mut None,
)
.expect_err("expected max data length error");
Expand All @@ -481,15 +425,7 @@ fn message_metadata() {
generate_nonempty_padded_bytes(rng),
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&chain_id,
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("expected max data length error");

assert_eq!(CheckError::InputMessageDataLength { index: 1 }, err,);
Expand All @@ -506,15 +442,7 @@ fn message_metadata() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("expected max predicate length error");

assert_eq!(CheckError::InputPredicateLength { index: 1 }, err,);
Expand All @@ -532,15 +460,7 @@ fn message_metadata() {
generate_bytes(rng),
predicate_data,
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("expected max predicate data length error");

assert_eq!(CheckError::InputPredicateDataLength { index: 1 }, err,);
Expand All @@ -553,8 +473,7 @@ fn message_message_coin() {
let txhash: Bytes32 = rng.gen();

let predicate = generate_nonempty_padded_bytes(rng);
let chain_id = ChainId::default();
let recipient = Input::predicate_owner(&predicate, &chain_id);
let recipient = Input::predicate_owner(&predicate);

Input::message_coin_predicate(
rng.gen(),
Expand All @@ -565,15 +484,7 @@ fn message_message_coin() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect("failed to validate empty message input");

let mut tx = Script::default();
Expand All @@ -589,7 +500,7 @@ fn message_message_coin() {
assert_eq!(CheckError::InputWitnessIndexBounds { index: 0 }, err,);

let mut predicate = generate_nonempty_padded_bytes(rng);
let recipient = Input::predicate_owner(&predicate, &ChainId::default());
let recipient = Input::predicate_owner(&predicate);
predicate[0] = predicate[0].wrapping_add(1);

let err = Input::message_coin_predicate(
Expand All @@ -601,15 +512,7 @@ fn message_message_coin() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("Expected failure");

assert_eq!(CheckError::InputPredicateOwner { index: 1 }, err);
Expand All @@ -625,15 +528,7 @@ fn message_message_coin() {
predicate,
generate_bytes(rng),
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("expected max predicate length error");

assert_eq!(CheckError::InputPredicateLength { index: 1 }, err,);
Expand All @@ -650,15 +545,7 @@ fn message_message_coin() {
generate_bytes(rng),
predicate_data,
)
.check(
1,
&txhash,
&[],
&[],
&Default::default(),
&ChainId::default(),
&mut None,
)
.check(1, &txhash, &[], &[], &Default::default(), &mut None)
.expect_err("expected max predicate data length error");

assert_eq!(CheckError::InputPredicateDataLength { index: 1 }, err,);
Expand Down
17 changes: 6 additions & 11 deletions fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,6 @@ fn tx_id_bytecode_len() {

mod inputs {
use super::*;
use fuel_types::ChainId;
use itertools::Itertools;

#[test]
Expand All @@ -948,8 +947,7 @@ mod inputs {

let predicate = (0..1000).map(|_| rng.gen()).collect_vec();
// The predicate is an owner of the coin
let chain_id = ChainId::default();
let owner: Address = Input::predicate_owner(&predicate, &chain_id);
let owner: Address = Input::predicate_owner(&predicate);

let tx =
TransactionBuilder::create(generate_bytes(rng).into(), rng.gen(), vec![])
Expand All @@ -970,7 +968,7 @@ mod inputs {
.with_tx_params(TX_PARAMS)
.finalize();

assert!(tx.check_predicate_owners(&chain_id));
assert!(tx.check_predicate_owners());
}

#[test]
Expand Down Expand Up @@ -998,8 +996,7 @@ mod inputs {
.with_tx_params(TX_PARAMS)
.finalize();

let chain_id = ChainId::default();
assert!(!tx.check_predicate_owners(&chain_id));
assert!(!tx.check_predicate_owners());
}

#[test]
Expand All @@ -1008,8 +1005,7 @@ mod inputs {

let predicate = (0..1000).map(|_| rng.gen()).collect_vec();
// The predicate is an recipient(owner) of the message
let chain_id = ChainId::default();
let recipient: Address = Input::predicate_owner(&predicate, &chain_id);
let recipient: Address = Input::predicate_owner(&predicate);

let tx =
TransactionBuilder::create(generate_bytes(rng).into(), rng.gen(), vec![])
Expand All @@ -1029,7 +1025,7 @@ mod inputs {
.with_tx_params(TX_PARAMS)
.finalize();

assert!(tx.check_predicate_owners(&chain_id));
assert!(tx.check_predicate_owners());
}

#[test]
Expand All @@ -1056,7 +1052,6 @@ mod inputs {
.with_tx_params(TX_PARAMS)
.finalize();

let chain_id = ChainId::default();
assert!(!tx.check_predicate_owners(&chain_id));
assert!(!tx.check_predicate_owners());
}
}
Loading

0 comments on commit 4bf0a35

Please sign in to comment.