From 74228af0584013b17ab1fdf8418924e1ab42c6fb Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 7 Jun 2023 19:07:12 +0200 Subject: [PATCH] contract/consensus: Use Money's Input struct for the Consensus::Stake params. --- .../consensus/src/entrypoint/stake_v1.rs | 63 +++++++++---------- src/contract/money/src/model.rs | 10 +-- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/contract/consensus/src/entrypoint/stake_v1.rs b/src/contract/consensus/src/entrypoint/stake_v1.rs index 4b30add6b..929c4a258 100644 --- a/src/contract/consensus/src/entrypoint/stake_v1.rs +++ b/src/contract/consensus/src/entrypoint/stake_v1.rs @@ -83,6 +83,34 @@ pub(crate) fn consensus_stake_process_instruction_v1( let self_ = &calls[call_idx as usize]; let params: ConsensusStakeParamsV1 = deserialize(&self_.data[1..])?; + // Check previous call is money contract + // FIXME: This changes with Money::Fee + if call_idx == 0 { + msg!("[ConsensusStakeV1] Error: previous_call_idx will be out of bounds"); + return Err(MoneyError::CallIdxOutOfBounds.into()) + } + + // Verify previous call corresponds to Money::StakeV1 (0x06) + let previous_call_idx = call_idx - 1; + let previous = &calls[previous_call_idx as usize]; + if previous.contract_id.inner() != MONEY_CONTRACT_ID.inner() { + msg!("[ConsensusStakeV1] Error: Previous contract call is not money contract"); + return Err(MoneyError::StakePreviousCallNotMoneyContract.into()) + } + + if previous.data[0] != 0x06 { + msg!("[ConsensusStakeV1] Error: Previous call function mismatch"); + return Err(MoneyError::PreviousCallFunctionMismatch.into()) + } + + // Verify that the previous call's input is the same as this one's + let previous_params: MoneyStakeParamsV1 = deserialize(&previous.data[1..])?; + let previous_input = &previous_params.input; + if previous_input != ¶ms.input { + msg!("[ConsensusStakeV1] Error: Previous call input mismatch"); + return Err(MoneyError::PreviousCallInputMismatch.into()) + } + // Access the necessary databases where there is information to // validate this state transition. let consensus_coins_db = db_lookup(cid, CONSENSUS_CONTRACT_COINS_TREE)?; @@ -117,41 +145,6 @@ pub(crate) fn consensus_stake_process_instruction_v1( return Err(MoneyError::StakeMissingNullifier.into()) } - // Check previous call is money contract - if call_idx == 0 { - msg!("[ConsensusStakeV1] Error: previous_call_idx will be out of bounds"); - return Err(MoneyError::SpendHookOutOfBounds.into()) - } - - let previous_call_idx = call_idx - 1; - let previous = &calls[previous_call_idx as usize]; - if previous.contract_id.inner() != MONEY_CONTRACT_ID.inner() { - msg!("[ConsensusStakeV1] Error: Previous contract call is not money contract"); - return Err(MoneyError::StakePreviousCallNotMoneyContract.into()) - } - - // Verify previous call corresponds to Money::StakeV1 (0x06) - if previous.data[0] != 0x06 { - msg!("[ConsensusStakeV1] Error: Previous call function mismatch"); - return Err(MoneyError::PreviousCallFunctionMissmatch.into()) - } - - // Verify previous call input is the same as this calls StakeInput - let previous_params: MoneyStakeParamsV1 = deserialize(&previous.data[1..])?; - let previous_input = &previous_params.input; - if previous_input != input { - msg!("[ConsensusStakeV1] Error: Previous call input mismatch"); - return Err(MoneyError::PreviousCallInputMissmatch.into()) - } - - // If spend hook is set, check its correctness - if previous_input.spend_hook != pallas::Base::ZERO && - previous_input.spend_hook != CONSENSUS_CONTRACT_ID.inner() - { - msg!("[ConsensusStakeV1] Error: Invoking contract call does not match spend hook in input"); - return Err(MoneyError::SpendHookMismatch.into()) - } - // Newly created coin for this call is in the output. Here we gather it, // and we also check that it hasn't existed before. let coin = serialize(&output.coin); diff --git a/src/contract/money/src/model.rs b/src/contract/money/src/model.rs index 761ae25e0..5b73f022c 100644 --- a/src/contract/money/src/model.rs +++ b/src/contract/money/src/model.rs @@ -113,14 +113,6 @@ pub struct ConsensusInput { pub signature_public: PublicKey, } -impl PartialEq for Input { - fn eq(&self, other: &ConsensusInput) -> bool { - self.value_commit == other.value_commit && - self.nullifier == other.nullifier && - self.merkle_root == other.merkle_root - } -} - /// A contract call's anonymous output #[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)] pub struct Output { @@ -244,7 +236,7 @@ pub struct MoneyUnstakeUpdateV1 { // ANCHOR: ConsensusStakeParams pub struct ConsensusStakeParamsV1 { /// Burnt token revealed info - pub input: ConsensusInput, + pub input: Input, /// Anonymous output pub output: ConsensusOutput, }