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

This commit is contained in:
lunar-mining
2022-09-02 13:07:43 +02:00
parent fd9c32f729
commit 1a853df52b
8 changed files with 36 additions and 3 deletions

View File

@@ -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,

View File

@@ -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,
}

View File

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

View File

@@ -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>;

View File

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

View File

@@ -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()

View File

@@ -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,

View File

@@ -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())?;