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

Bugfix: Zero predicate_gas_used field during validation of the produced block #1722

Merged
merged 2 commits into from
Mar 4, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Breaking

- [#1722](https://github.com/FuelLabs/fuel-core/pull/1722): Bugfix: Zero `predicate_gas_used` field during validation of the produced block.
- [#1714](https://github.com/FuelLabs/fuel-core/pull/1714): The change bumps the `fuel-vm` to `0.47.1`. It breaks several breaking changes into the protocol:
- All malleable fields are zero during the execution and unavailable through the GTF getters. Accessing them via the memory directly is still possible, but they are zero.
- The `Transaction` doesn't define the gas price anymore. The gas price is defined by the block producer and recorded in the `Mint` transaction at the end of the block. A price of future blocks can be fetched through a [new API nedopoint](https://github.com/FuelLabs/fuel-core/issues/1641) and the price of the last block can be fetch or via the block or another [API endpoint](https://github.com/FuelLabs/fuel-core/issues/1647).
Expand Down
89 changes: 87 additions & 2 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ mod tests {
TxPointer as TxPointerTraitTrait,
},
input::{
coin::CoinSigned,
coin::{
CoinPredicate,
CoinSigned,
},
contract,
Input,
},
Expand Down Expand Up @@ -98,7 +101,10 @@ mod tests {
Word,
},
fuel_vm::{
checked_transaction::CheckError,
checked_transaction::{
CheckError,
EstimatePredicates,
},
interpreter::ExecutableTransaction,
script_with_data_offset,
util::test_helpers::TestBuilder as TxBuilder,
Expand Down Expand Up @@ -2831,6 +2837,85 @@ mod tests {
assert_eq!(time.0, receipts[0].val().unwrap());
}

#[test]
fn tx_with_coin_predicate_included_by_block_producer_and_accepted_by_validator() {
let mut rng = StdRng::seed_from_u64(2322u64);
let predicate: Vec<u8> = vec![op::ret(RegId::ONE)].into_iter().collect();
let owner = Input::predicate_owner(&predicate);
let amount = 1000;

let config = Config {
utxo_validation_default: true,
..Default::default()
};

let mut tx = TransactionBuilder::script(
vec![op::ret(RegId::ONE)].into_iter().collect(),
vec![],
)
.max_fee_limit(amount)
.add_input(Input::coin_predicate(
rng.gen(),
owner,
amount,
AssetId::BASE,
rng.gen(),
0,
predicate,
vec![],
))
.add_output(Output::Change {
to: Default::default(),
amount: 0,
asset_id: Default::default(),
})
.finalize();
tx.estimate_predicates(&config.consensus_parameters.clone().into())
.unwrap();
let db = &mut Database::default();

// insert coin into state
if let Input::CoinPredicate(CoinPredicate {
utxo_id,
owner,
amount,
asset_id,
tx_pointer,
..
}) = tx.inputs()[0]
{
let mut coin = CompressedCoin::default();
coin.set_owner(owner);
coin.set_amount(amount);
coin.set_asset_id(asset_id);
coin.set_tx_pointer(tx_pointer);
db.storage::<Coins>().insert(&utxo_id, &coin).unwrap();
}

let producer = create_executor(db.clone(), config.clone());

let ExecutionResult {
block,
skipped_transactions,
..
} = producer
.execute_without_commit(ExecutionTypes::Production(Components {
header_to_produce: PartialBlockHeader::default(),
transactions_source: OnceTransactionsSource::new(vec![tx.into()]),
gas_price: 1,
gas_limit: u64::MAX,
}))
.unwrap()
.into_result();
assert!(skipped_transactions.is_empty());

let validator = create_executor(db.clone(), config);
let result = validator.execute_without_commit::<OnceTransactionsSource>(
ExecutionTypes::Validation(block),
);
assert!(result.is_ok(), "{result:?}")
}

#[cfg(feature = "relayer")]
mod relayer {
use super::*;
Expand Down
38 changes: 36 additions & 2 deletions crates/services/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,9 @@ where
.check_predicates(&CheckPredicateParams::from(
&self.config.consensus_parameters,
))
.map_err(|_| {
.map_err(|e| {
ExecutorError::TransactionValidity(
TransactionValidityError::InvalidPredicate(tx_id),
TransactionValidityError::Validation(e),
)
})?;
debug_assert!(checked_tx.checks().contains(Checks::Predicates));
Expand Down Expand Up @@ -1025,6 +1025,40 @@ where
debug_assert_eq!(tx.id(&self.config.consensus_parameters.chain_id), tx_id);
}

for (original_input, produced_input) in checked_tx
.transaction()
.inputs()
.iter()
.zip(tx.inputs_mut())
{
let predicate_gas_used = original_input.predicate_gas_used();

if let Some(gas_used) = predicate_gas_used {
match produced_input {
Input::CoinPredicate(CoinPredicate {
predicate_gas_used, ..
})
| Input::MessageCoinPredicate(MessageCoinPredicate {
predicate_gas_used,
..
})
| Input::MessageDataPredicate(MessageDataPredicate {
predicate_gas_used,
..
}) => {
*predicate_gas_used = gas_used;
}
_ => {
debug_assert!(false, "This error is not possible unless VM changes the order of inputs, \
or we added a new predicate inputs.");
Comment on lines +1052 to +1053
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
debug_assert!(false, "This error is not possible unless VM changes the order of inputs, \
or we added a new predicate inputs.");
debug_assert!(false, "This error is not possible unless VM changed the order of inputs, \
or we added a new predicate input.");

return Err(ExecutorError::InvalidTransactionOutcome {
transaction_id: tx_id,
})
}
}
}
}

// We always need to update inputs with storage state before execution,
// because VM zeroes malleable fields during the execution.
self.compute_inputs(tx.inputs_mut(), tx_st_transaction.as_mut())?;
Expand Down
6 changes: 0 additions & 6 deletions crates/types/src/services/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,6 @@ pub enum TransactionValidityError {
ContractDoesNotExist(ContractId),
#[error("Contract output index isn't valid: {0:#x}")]
InvalidContractInputIndex(UtxoId),
#[error("The transaction contains predicate inputs which aren't enabled: {0:#x}")]
PredicateExecutionDisabled(TxId),
#[error(
"The transaction contains a predicate which failed to validate: TransactionId({0:#x})"
)]
InvalidPredicate(TxId),
#[error("Transaction validity: {0:#?}")]
Validation(CheckError),
}
Expand Down
Loading