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

daod: implement Encodable for Box<T> and create isolated error case in demo.rs

lunar-mining 3 лет назад
Родитель
Сommit
1a853df52b

+ 2 - 0
bin/daod/src/dao_contract/exec/validate.rs

@@ -6,6 +6,7 @@ use pasta_curves::{
 
 use darkfi::{
     crypto::{coin::Coin, keypair::PublicKey, types::DrkCircuitField},
+    util::serial::{Encodable, SerialDecodable, SerialEncodable},
     Error as DarkFiError,
 };
 
@@ -56,6 +57,7 @@ impl From<DarkFiError> for Error {
     }
 }
 
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct CallData {
     pub proposal: pallas::Base,
     pub coin_0: pallas::Base,

+ 5 - 1
bin/daod/src/dao_contract/mint/validate.rs

@@ -1,6 +1,9 @@
 use std::any::{Any, TypeId};
 
-use darkfi::crypto::{keypair::PublicKey, types::DrkCircuitField};
+use darkfi::{
+    crypto::{keypair::PublicKey, types::DrkCircuitField},
+    util::serial::{Encodable, SerialDecodable, SerialEncodable},
+};
 
 use crate::{
     dao_contract::{DaoBulla, State},
@@ -43,6 +46,7 @@ pub enum Error {}
 
 type Result<T> = std::result::Result<T, Error>;
 
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct CallData {
     pub dao_bulla: DaoBulla,
 }

+ 1 - 0
bin/daod/src/dao_contract/propose/validate.rs

@@ -46,6 +46,7 @@ impl From<DarkFiError> for Error {
     }
 }
 
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct CallData {
     pub header: Header,
     pub inputs: Vec<Input>,

+ 5 - 2
bin/daod/src/dao_contract/state.rs

@@ -5,9 +5,12 @@ use pasta_curves::{
 };
 use std::{any::Any, collections::HashMap, hash::Hasher};
 
-use darkfi::crypto::{constants::MERKLE_DEPTH, merkle_node::MerkleNode, nullifier::Nullifier};
+use darkfi::{
+    crypto::{constants::MERKLE_DEPTH, merkle_node::MerkleNode, nullifier::Nullifier},
+    util::serial::{Encodable, SerialDecodable, SerialEncodable},
+};
 
-#[derive(Clone)]
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct DaoBulla(pub pallas::Base);
 
 type MerkleTree = BridgeTree<MerkleNode, MERKLE_DEPTH>;

+ 1 - 0
bin/daod/src/dao_contract/vote/validate.rs

@@ -49,6 +49,7 @@ impl From<DarkFiError> for Error {
     }
 }
 
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct CallData {
     pub header: Header,
     pub inputs: Vec<Input>,

+ 12 - 0
bin/daod/src/demo.rs

@@ -166,6 +166,7 @@ fn sign(signature_secrets: Vec<SecretKey>) -> Vec<Signature> {
 type ContractId = String;
 type FuncId = String;
 
+//#[derive(Clone, SerialEncodable)]
 pub struct FuncCall {
     pub contract_id: ContractId,
     pub func_id: FuncId,
@@ -173,6 +174,17 @@ pub struct FuncCall {
     pub proofs: Vec<Proof>,
 }
 
+pub trait TestTrait: Encodable {
+    fn foo(&self);
+}
+
+#[derive(Clone, SerialEncodable)]
+pub struct TestStruct {
+    // Bang!
+    // This usage of Encodable fails. Note: Encodable is implemented for Box<T>.
+    //pub test: Box<dyn TestTrait>,
+}
+
 pub trait CallDataBase {
     // Public values for verifying the proofs
     // Needed so we can convert internal types so they can be used in Proof::verify()

+ 1 - 0
bin/daod/src/example_contract/foo/validate.rs

@@ -30,6 +30,7 @@ impl From<DarkFiError> for Error {
     }
 }
 
+#[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct CallData {
     pub public_value: pallas::Base,
     pub signature_public: PublicKey,

+ 9 - 0
src/util/serial.rs

@@ -581,6 +581,15 @@ impl Decodable for Box<[u8]> {
     }
 }
 
+impl<T: Encodable> Encodable for Box<T> {
+    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+        let mut len = 0;
+        let t: &T = &**self;
+        len += t.encode(&mut s)?;
+        Ok(len)
+    }
+}
+
 impl Encodable for blake3::Hash {
     fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
         s.write_slice(self.as_bytes())?;