Make error a type member of FromPayload trait rather than a type variable.
Fix demo test compilation errors.
This commit is contained in:
parent
961d251178
commit
659c34a954
|
@ -18,14 +18,15 @@
|
|||
//! - `tx_b`: `[ TzeIn(tx_a, preimage_1) -> TzeOut(value, hash_2) ]`
|
||||
//! - `tx_c`: `[ TzeIn(tx_b, preimage_2) -> [any output types...] ]`
|
||||
|
||||
use blake2b_simd::Params;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
|
||||
use zcash_primitives::extensions::transparent::{
|
||||
Extension, ExtensionTxBuilder, FromPayload, ToPayload,
|
||||
use blake2b_simd::Params;
|
||||
|
||||
use zcash_primitives::{
|
||||
extensions::transparent::{Extension, ExtensionTxBuilder, FromPayload, ToPayload},
|
||||
transaction::components::{amount::Amount, OutPoint, TzeOut},
|
||||
};
|
||||
use zcash_primitives::transaction::components::{amount::Amount, OutPoint, TzeOut};
|
||||
|
||||
mod open {
|
||||
pub const MODE: usize = 0;
|
||||
|
@ -100,8 +101,10 @@ impl TryFrom<(usize, Precondition)> for Precondition {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromPayload<Error> for Precondition {
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, Error> {
|
||||
impl FromPayload for Precondition {
|
||||
type Error = Error;
|
||||
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, Self::Error> {
|
||||
match mode {
|
||||
open::MODE => {
|
||||
if payload.len() == 32 {
|
||||
|
@ -163,8 +166,10 @@ impl TryFrom<(usize, Witness)> for Witness {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromPayload<Error> for Witness {
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, Error> {
|
||||
impl FromPayload for Witness {
|
||||
type Error = Error;
|
||||
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, Self::Error> {
|
||||
match mode {
|
||||
open::MODE => {
|
||||
if payload.len() == 32 {
|
||||
|
@ -361,12 +366,14 @@ impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder<'a, B> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use blake2b_simd::Params;
|
||||
use zcash_extensions_api::transparent::{self as tze, Extension, FromPayload, ToPayload};
|
||||
|
||||
use super::{close, open, Context, Precondition, Program, Witness};
|
||||
use crate::transaction::{
|
||||
components::{Amount, OutPoint, TzeIn, TzeOut},
|
||||
Transaction, TransactionData,
|
||||
use zcash_primitives::{
|
||||
extensions::transparent::{self as tze, Extension, FromPayload, ToPayload},
|
||||
transaction::{
|
||||
components::{Amount, OutPoint, TzeIn, TzeOut},
|
||||
Transaction, TransactionData,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
@ -476,7 +483,7 @@ mod tests {
|
|||
precondition: tze::Precondition::from(0, &Precondition::open(hash_1)),
|
||||
};
|
||||
|
||||
println!("{:x?}", precondition.payload);
|
||||
// println!("{:x?}", precondition.payload);
|
||||
|
||||
let mut mtx_a = TransactionData::nu4();
|
||||
mtx_a.tze_outputs.push(out_a);
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
use crate::transaction::components::{Amount, OutPoint, TzeOut};
|
||||
use std::fmt;
|
||||
|
||||
pub trait FromPayload<E>: Sized {
|
||||
pub trait FromPayload: Sized {
|
||||
type Error;
|
||||
|
||||
/// Parses an extension type from a mode and payload.
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, E>;
|
||||
fn from_payload(mode: usize, payload: &[u8]) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait ToPayload {
|
||||
|
@ -30,6 +32,10 @@ impl Precondition {
|
|||
payload,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_to<P: FromPayload>(&self) -> Result<P, P::Error> {
|
||||
P::from_payload(self.mode, &self.payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// Data that satisfies the precondition for prior encumbered funds, enabling them to be
|
||||
|
@ -96,8 +102,8 @@ pub trait Extension<C> {
|
|||
context: &C,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
Self::P: FromPayload<Self::Error>,
|
||||
Self::W: FromPayload<Self::Error>,
|
||||
Self::P: FromPayload<Error = Self::Error>,
|
||||
Self::W: FromPayload<Error = Self::Error>,
|
||||
{
|
||||
self.verify_inner(
|
||||
&Self::P::from_payload(precondition.mode, &precondition.payload)?,
|
||||
|
|
|
@ -275,6 +275,10 @@ struct TzeInputs<'a, BuildCtx> {
|
|||
}
|
||||
|
||||
impl<'a, BuildCtx> TzeInputs<'a, BuildCtx> {
|
||||
fn default() -> Self {
|
||||
TzeInputs { builders: vec![] }
|
||||
}
|
||||
|
||||
fn push<WBuilder, W: ToPayload>(
|
||||
&mut self,
|
||||
extension_id: usize,
|
||||
|
@ -392,7 +396,7 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> {
|
|||
spends: vec![],
|
||||
outputs: vec![],
|
||||
transparent_inputs: TransparentInputs::default(),
|
||||
tze_inputs: TzeInputs { builders: vec![] },
|
||||
tze_inputs: TzeInputs::default(),
|
||||
change_address: None,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
|
@ -815,7 +819,6 @@ mod tests {
|
|||
use rand_core::OsRng;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use super::{Builder, Error};
|
||||
use crate::{
|
||||
consensus,
|
||||
consensus::TestNetwork,
|
||||
|
@ -828,6 +831,8 @@ mod tests {
|
|||
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
|
||||
};
|
||||
|
||||
use super::{Builder, Error, TzeInputs};
|
||||
|
||||
#[test]
|
||||
fn fails_on_negative_output() {
|
||||
let extsk = ExtendedSpendingKey::master(&[]);
|
||||
|
@ -854,7 +859,7 @@ mod tests {
|
|||
TestNetwork::activation_height(NetworkUpgrade::Sapling).unwrap();
|
||||
|
||||
// Create a builder with 0 fee, so we can construct t outputs
|
||||
let mut builder = builder::Builder::<TestNetwork, OsRng> {
|
||||
let mut builder = builder::Builder::<'_, TestNetwork, OsRng> {
|
||||
rng: OsRng,
|
||||
height: sapling_activation_height,
|
||||
mtx: TransactionData::new(),
|
||||
|
@ -863,6 +868,7 @@ mod tests {
|
|||
spends: vec![],
|
||||
outputs: vec![],
|
||||
transparent_inputs: TransparentInputs::default(),
|
||||
tze_inputs: TzeInputs::default(),
|
||||
change_address: None,
|
||||
phantom: PhantomData,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue