Skip to content

Commit

Permalink
[transaction input checker] Make batch rules more robust (MystenLabs#…
Browse files Browse the repository at this point in the history
…7791)

Some checks were not performed in batch for certain transaction kinds.
While this might not be a problem now, it feels like it might cause an
issue later if these rules are changed without changing them also for
batch.
  • Loading branch information
tnowacki authored Feb 3, 2023
1 parent 4b78adc commit 6697503
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions crates/sui-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,50 @@ impl SingleTransactionKind {
}
Ok(input_objects)
}

pub fn validity_check(&self, gas_payment: &ObjectRef) -> SuiResult {
fp_ensure!(
!self.is_blocked_move_function(),
SuiError::BlockedMoveFunction
);
match self {
SingleTransactionKind::Pay(_)
| SingleTransactionKind::Call(_)
| SingleTransactionKind::Publish(_)
| SingleTransactionKind::TransferObject(_)
| SingleTransactionKind::TransferSui(_)
| SingleTransactionKind::ChangeEpoch(_)
| SingleTransactionKind::Genesis(_) => (),
SingleTransactionKind::PaySui(p) => {
fp_ensure!(!p.coins.is_empty(), SuiError::EmptyInputCoins);
fp_ensure!(
// unwrap() is safe because coins are not empty.
p.coins.first().unwrap() == gas_payment,
SuiError::UnexpectedGasPaymentObject
);
}
SingleTransactionKind::PayAllSui(pa) => {
fp_ensure!(!pa.coins.is_empty(), SuiError::EmptyInputCoins);
fp_ensure!(
// unwrap() is safe because coins are not empty.
pa.coins.first().unwrap() == gas_payment,
SuiError::UnexpectedGasPaymentObject
);
}
};
Ok(())
}

fn is_blocked_move_function(&self) -> bool {
match self {
SingleTransactionKind::Call(call) => BLOCKED_MOVE_FUNCTIONS.contains(&(
call.package,
call.module.as_str(),
call.function.as_str(),
)),
_ => false,
}
}
}

impl Display for SingleTransactionKind {
Expand Down Expand Up @@ -629,17 +673,6 @@ impl TransactionKind {
TransactionKind::Single(SingleTransactionKind::Genesis(_))
)
}

fn is_blocked_move_function(&self) -> bool {
self.single_transactions().any(|tx| match tx {
SingleTransactionKind::Call(call) => BLOCKED_MOVE_FUNCTIONS.contains(&(
call.package,
call.module.as_str(),
call.function.as_str(),
)),
_ => false,
})
}
}

impl Display for TransactionKind {
Expand Down Expand Up @@ -965,10 +998,6 @@ impl TransactionData {
}

pub fn validity_check_impl(kind: &TransactionKind, gas_payment: &ObjectRef) -> SuiResult {
fp_ensure!(
!kind.is_blocked_move_function(),
SuiError::BlockedMoveFunction
);
match kind {
TransactionKind::Batch(b) => {
fp_ensure!(
Expand Down Expand Up @@ -997,32 +1026,11 @@ impl TransactionData {
.to_string()
}
);
}
TransactionKind::Single(s) => match s {
SingleTransactionKind::Pay(_)
| SingleTransactionKind::Call(_)
| SingleTransactionKind::Publish(_)
| SingleTransactionKind::TransferObject(_)
| SingleTransactionKind::TransferSui(_)
| SingleTransactionKind::ChangeEpoch(_)
| SingleTransactionKind::Genesis(_) => (),
SingleTransactionKind::PaySui(p) => {
fp_ensure!(!p.coins.is_empty(), SuiError::EmptyInputCoins);
fp_ensure!(
// unwrap() is safe because coins are not empty.
p.coins.first().unwrap() == gas_payment,
SuiError::UnexpectedGasPaymentObject
);
}
SingleTransactionKind::PayAllSui(pa) => {
fp_ensure!(!pa.coins.is_empty(), SuiError::EmptyInputCoins);
fp_ensure!(
// unwrap() is safe because coins are not empty.
pa.coins.first().unwrap() == gas_payment,
SuiError::UnexpectedGasPaymentObject
);
for s in b {
s.validity_check(gas_payment)?
}
},
}
TransactionKind::Single(s) => s.validity_check(gas_payment)?,
}
Ok(())
}
Expand Down

0 comments on commit 6697503

Please sign in to comment.