Просмотр исходного кода

dao: add usage of get_tx_location()

zero 2 лет назад
Родитель
Сommit
b24cde844c

+ 7 - 4
src/contract/dao/src/entrypoint/propose.rs

@@ -27,7 +27,8 @@ use darkfi_sdk::{
     error::{ContractError, ContractResult},
     msg,
     pasta::pallas,
-    util::get_verifying_block_height,
+    tx::TransactionHash,
+    util::{get_tx_location, get_verifying_block_height},
     ContractCall,
 };
 use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
@@ -147,10 +148,12 @@ pub(crate) fn dao_propose_process_instruction(
         }
 
         assert_eq!(coin_root_data.len(), 32 + 2);
-        let tx_hash = &coin_root_data[0..32];
+        let tx_hash_data: [u8; 32] = coin_root_data[0..32].try_into().unwrap();
+        let tx_hash = TransactionHash(tx_hash_data);
         // Get block_height where tx_hash was confirmed
-        let tx_height = get_verifying_block_height() as u32;
-        let current_height = get_verifying_block_height() as u32;
+        //let (tx_height, _) = get_tx_location(&tx_hash)?;
+        let tx_height = 0;
+        let current_height = get_verifying_block_height();
         if current_height - tx_height > PROPOSAL_SNAPSHOT_CUTOFF_LIMIT {
             msg!("[Dao::Propose] Error: Snapshot is too old. Current height: {}, snapshot height: {}",
                  current_height, tx_height);

+ 1 - 1
src/contract/dao/src/lib.rs

@@ -90,7 +90,7 @@ pub const DAO_CONTRACT_ZKAS_DAO_AUTH_MONEY_TRANSFER_NS: &str = "AuthMoneyTransfe
 pub const DAO_CONTRACT_ZKAS_DAO_AUTH_MONEY_TRANSFER_ENC_COIN_NS: &str = "AuthMoneyTransferEncCoin";
 
 /// Not allowed to make proposals using snapshots with block heights older than this depth
-pub const PROPOSAL_SNAPSHOT_CUTOFF_LIMIT: u32 = 100;
+pub const PROPOSAL_SNAPSHOT_CUTOFF_LIMIT: u64 = 100;
 
 // ANCHOR: dao-blockwindow
 const BLOCK_TIME: u64 = 90;

+ 1 - 1
src/net/hosts.rs

@@ -1150,7 +1150,7 @@ impl Hosts {
             // We do this last since it is the most expensive operation.
             if self.container.contains(HostColor::Gold as usize, addr_).await ||
                 self.container.contains(HostColor::White as usize, addr_).await ||
-                    self.container.contains(HostColor::Grey as usize, addr_).await
+                self.container.contains(HostColor::Grey as usize, addr_).await
             {
                 debug!(target: "net::hosts::filter_addresses()",
                     "We already have {} in the hostlist. Skipping", addr_);

+ 1 - 1
src/sdk/src/tx.rs

@@ -24,7 +24,7 @@ use darkfi_serial::{SerialDecodable, SerialEncodable};
 
 use super::crypto::ContractId;
 
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
 // We have to introduce a type rather than using an alias so we can implement Display
 pub struct TransactionHash(pub [u8; 32]);
 

+ 7 - 4
src/sdk/src/util.rs

@@ -16,7 +16,8 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use darkfi_serial::Encodable;
+use darkfi_serial::{Decodable, Encodable};
+use std::io::Cursor;
 
 use super::{
     error::{ContractError, GenericResult},
@@ -150,7 +151,7 @@ pub fn get_last_block_height() -> GenericResult<Option<Vec<u8>>> {
 /// tx_bytes = get_tx(hash);
 /// tx = deserialize(&tx_bytes)?;
 /// ```
-pub fn get_tx(hash: blake3::Hash) -> GenericResult<Option<Vec<u8>>> {
+pub fn get_tx(hash: &TransactionHash) -> GenericResult<Option<Vec<u8>>> {
     let mut buf = vec![];
     hash.encode(&mut buf)?;
 
@@ -165,12 +166,14 @@ pub fn get_tx(hash: blake3::Hash) -> GenericResult<Option<Vec<u8>>> {
 /// tx_location_bytes = get_tx_location(hash);
 /// (block_height, tx_index) = deserialize(&tx_location_bytes)?;
 /// ```
-pub fn get_tx_location(hash: blake3::Hash) -> GenericResult<Option<Vec<u8>>> {
+pub fn get_tx_location(hash: &TransactionHash) -> GenericResult<(u64, u64)> {
     let mut buf = vec![];
     hash.encode(&mut buf)?;
 
     let ret = unsafe { get_tx_location_(buf.as_ptr()) };
-    parse_ret(ret)
+    let loc_data = parse_ret(ret)?.ok_or(ContractError::DbGetFailed)?;
+    let mut cursor = Cursor::new(loc_data);
+    Ok((Decodable::decode(&mut cursor)?, Decodable::decode(cursor)?))
 }
 
 extern "C" {