Ver Fonte

contract/money/pow_reward: removed obselete ECVRF

skoupidi há 2 anos atrás
pai
commit
a3a747df39

+ 3 - 13
src/contract/money/src/client/pow_reward_v1.rs

@@ -23,10 +23,7 @@ use darkfi::{
 };
 use darkfi_sdk::{
     blockchain::expected_reward,
-    crypto::{
-        ecvrf::VrfProof, note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, PublicKey,
-        SecretKey,
-    },
+    crypto::{note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, PublicKey, SecretKey},
     pasta::pallas,
 };
 use log::{debug, info};
@@ -65,7 +62,7 @@ impl PoWRewardRevealed {
 
 /// Struct holding necessary information to build a `Money::PoWRewardV1` contract call.
 pub struct PoWRewardCallBuilder {
-    /// Caller's secret key, used for signing and VRF proof generation
+    /// Caller's secret key, used for signing
     pub secret: SecretKey,
     /// Reward recipient's public key
     pub recipient: PublicKey,
@@ -153,14 +150,7 @@ impl PoWRewardCallBuilder {
             note: encrypted_note,
         };
 
-        info!("Building Consensus::ProposalV1 VRF proof");
-        let mut vrf_input = Vec::with_capacity(32 + blake3::OUT_LEN + 32);
-        vrf_input.extend_from_slice(&pallas::Base::from(self.last_nonce).to_repr());
-        vrf_input.extend_from_slice(self.fork_previous_hash.as_bytes());
-        vrf_input.extend_from_slice(&pallas::Base::from(self.block_height).to_repr());
-        let vrf_proof = VrfProof::prove(self.secret, &vrf_input);
-
-        let params = MoneyPoWRewardParamsV1 { input: c_input, output: c_output, vrf_proof };
+        let params = MoneyPoWRewardParamsV1 { input: c_input, output: c_output };
         let debris = PoWRewardCallDebris { params, proofs: vec![proof] };
         Ok(debris)
     }

+ 8 - 23
src/contract/money/src/entrypoint/pow_reward_v1.rs

@@ -24,7 +24,7 @@ use darkfi_sdk::{
     error::{ContractError, ContractResult},
     merkle_add, msg,
     pasta::pallas,
-    util::{get_last_block_info, get_verifying_block_height},
+    util::{get_last_block_height, get_verifying_block_height},
     ContractCall,
 };
 use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
@@ -88,36 +88,21 @@ pub(crate) fn money_pow_reward_process_instruction_v1(
         return Err(MoneyError::PoWRewardCallOnGenesisBlock.into())
     }
 
-    // Grab last block information to use in the VRF
-    let Some(last_block_info) = get_last_block_info()? else {
-        msg!("[PoWRewardV1] Error: Could not receive last block from db");
-        return Err(MoneyError::PoWRewardRetrieveLastBlockError.into())
-    };
-    let height: u64 = deserialize(&last_block_info[..8])?;
-
     // Verify this contract call is verified against next block height
-    if verifying_block_height != height + 1 {
+    let Some(last_block_height) = get_last_block_height()? else {
+        msg!("[PoWRewardV1] Error: Could not receive last block height from db");
+        return Err(MoneyError::PoWRewardRetrieveLastBlockHeightError.into())
+    };
+    let last_block_height: u64 = deserialize(&last_block_height)?;
+    if verifying_block_height != last_block_height + 1 {
         msg!(
             "[PoWRewardV1] Error: Call is executed for block height {}, not next one: {}",
             verifying_block_height,
-            height
+            last_block_height
         );
         return Err(MoneyError::PoWRewardCallNotOnNextBlockHeight.into())
     }
 
-    // Construct VRF input
-    let mut vrf_input = Vec::with_capacity(32 + blake3::OUT_LEN + 32);
-    let nonce: u64 = deserialize(&last_block_info[8..16])?;
-    vrf_input.extend_from_slice(&pallas::Base::from(nonce).to_repr());
-    vrf_input.extend_from_slice(&last_block_info[16..]);
-    vrf_input.extend_from_slice(&pallas::Base::from(verifying_block_height).to_repr());
-
-    // Verify VRF proof
-    if !params.vrf_proof.verify(params.input.signature_public, &vrf_input) {
-        msg!("[PoWRewardV1] Error: VRF proof couldn't be verified");
-        return Err(MoneyError::PoWRewardErroneousVrfProof.into())
-    }
-
     // Only DARK_TOKEN_ID can be minted as PoW reward.
     if params.input.token_id != *DARK_TOKEN_ID {
         msg!("[PoWRewardV1] Error: Clear input used non-native token");

+ 6 - 10
src/contract/money/src/error.rs

@@ -88,15 +88,12 @@ pub enum MoneyError {
     #[error("Call is executed on genesis block height")]
     PoWRewardCallOnGenesisBlock,
 
-    #[error("Could not retrieve last block from db")]
-    PoWRewardRetrieveLastBlockError,
+    #[error("Could not retrieve last block height from db")]
+    PoWRewardRetrieveLastBlockHeightError,
 
     #[error("Call is not executed on next block height")]
     PoWRewardCallNotOnNextBlockHeight,
 
-    #[error("Eta VRF proof couldn't be verified")]
-    PoWRewardErroneousVrfProof,
-
     #[error("No inputs in fee call")]
     FeeMissingInputs,
 
@@ -136,12 +133,11 @@ impl From<MoneyError> for ContractError {
             MoneyError::GenesisCallNonGenesisBlock => Self::Custom(23),
             MoneyError::MissingNullifier => Self::Custom(24),
             MoneyError::PoWRewardCallOnGenesisBlock => Self::Custom(25),
-            MoneyError::PoWRewardRetrieveLastBlockError => Self::Custom(26),
+            MoneyError::PoWRewardRetrieveLastBlockHeightError => Self::Custom(26),
             MoneyError::PoWRewardCallNotOnNextBlockHeight => Self::Custom(27),
-            MoneyError::PoWRewardErroneousVrfProof => Self::Custom(28),
-            MoneyError::FeeMissingInputs => Self::Custom(29),
-            MoneyError::InsufficientFee => Self::Custom(30),
-            MoneyError::CoinMerkleRootNotFound => Self::Custom(31),
+            MoneyError::FeeMissingInputs => Self::Custom(28),
+            MoneyError::InsufficientFee => Self::Custom(29),
+            MoneyError::CoinMerkleRootNotFound => Self::Custom(30),
         }
     }
 }

+ 2 - 4
src/contract/money/src/model/mod.rs

@@ -18,8 +18,8 @@
 
 use darkfi_sdk::{
     crypto::{
-        ecvrf::VrfProof, note::AeadEncryptedNote, pasta_prelude::PrimeField, poseidon_hash,
-        BaseBlind, FuncId, MerkleNode, PublicKey, ScalarBlind, SecretKey,
+        note::AeadEncryptedNote, pasta_prelude::PrimeField, poseidon_hash, BaseBlind, FuncId,
+        MerkleNode, PublicKey, ScalarBlind, SecretKey,
     },
     error::ContractError,
     pasta::pallas,
@@ -276,8 +276,6 @@ pub struct MoneyPoWRewardParamsV1 {
     pub input: ClearInput,
     /// Anonymous output
     pub output: Output,
-    /// VRF proof for block rank calculation
-    pub vrf_proof: VrfProof,
 }
 
 /// State update for `Money::PoWReward`

+ 5 - 7
src/runtime/import/util.rs

@@ -274,12 +274,11 @@ pub(crate) fn get_blockchain_time(mut ctx: FunctionEnvMut<Env>) -> i64 {
 }
 
 /// Grabs last block from the `Blockchain` overlay and then copies its
-/// height, nonce and previous block hash into the VM, by appending the data
-/// to the VM's object store.
+/// height to the VM's object store.
 ///
 /// On success, returns the index of the new object in the object store.
 /// Otherwise, returns an error code.
-pub(crate) fn get_last_block_info(mut ctx: FunctionEnvMut<Env>) -> i64 {
+pub(crate) fn get_last_block_height(mut ctx: FunctionEnvMut<Env>) -> i64 {
     let (env, mut store) = ctx.data_and_store_mut();
     let cid = &env.contract_id;
 
@@ -305,13 +304,12 @@ pub(crate) fn get_last_block_info(mut ctx: FunctionEnvMut<Env>) -> i64 {
     };
 
     // Subtract used gas. Here we count the size of the object.
-    env.subtract_gas(&mut store, (8 + 8 + blake3::OUT_LEN) as u64);
+    // u64 is 8 bytes.
+    env.subtract_gas(&mut store, 8);
 
     // Create the return object
-    let mut ret = Vec::with_capacity(8 + 8 + blake3::OUT_LEN);
+    let mut ret = Vec::with_capacity(8);
     ret.extend_from_slice(&darkfi_serial::serialize(&block.header.height));
-    ret.extend_from_slice(&darkfi_serial::serialize(&block.header.nonce));
-    ret.extend_from_slice(block.header.previous.as_bytes());
 
     // Copy Vec<u8> to the VM
     let mut objects = env.objects.borrow_mut();

+ 2 - 2
src/runtime/vm_runtime.rs

@@ -294,10 +294,10 @@ impl Runtime {
                     import::util::get_blockchain_time,
                 ),
 
-                "get_last_block_info_" => Function::new_typed_with_env(
+                "get_last_block_height_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    import::util::get_last_block_info,
+                    import::util::get_last_block_height,
                 ),
             }
         };

+ 5 - 5
src/sdk/src/util.rs

@@ -107,13 +107,13 @@ pub fn get_blockchain_time() -> GenericResult<Option<Vec<u8>>> {
     parse_ret(ret)
 }
 
-/// Only exec() can call this. Will return last block information.
+/// Only exec() can call this. Will return last block height.
 ///
 /// ```
-/// last_block_info = get_last_block_info();
+/// last_block_height = get_last_block_height();
 /// ```
-pub fn get_last_block_info() -> GenericResult<Option<Vec<u8>>> {
-    let ret = unsafe { get_last_block_info_() };
+pub fn get_last_block_height() -> GenericResult<Option<Vec<u8>>> {
+    let ret = unsafe { get_last_block_height_() };
     parse_ret(ret)
 }
 
@@ -126,5 +126,5 @@ extern "C" {
     fn get_verifying_block_height_() -> u64;
     fn get_verifying_block_height_epoch_() -> u64;
     fn get_blockchain_time_() -> i64;
-    fn get_last_block_info_() -> i64;
+    fn get_last_block_height_() -> i64;
 }