zcash_client_backend/fees/
fixed.rs1use core::marker::PhantomData;
4
5use zcash_primitives::transaction::fees::{fixed::FeeRule as FixedFeeRule, transparent};
6use zcash_protocol::{
7 ShieldedPool,
8 consensus::{self, BlockHeight},
9 memo::MemoBytes,
10 value::{BalanceError, Zatoshis},
11};
12
13use crate::data_api::anchor_retention::PoolMigrationParams;
14use crate::data_api::{ChangeReceivers, InputSource, wallet::TargetHeight};
15
16use super::{
17 ChangeError, ChangeStrategy, DustOutputPolicy, EphemeralBalance, SplitPolicy,
18 TransactionBalance,
19 common::{SinglePoolBalanceConfig, single_pool_output_balance},
20 sapling as sapling_fees,
21};
22
23#[cfg(feature = "transparent-inputs")]
24use super::TransparentChangePolicy;
25#[cfg(feature = "orchard")]
26use super::orchard as orchard_fees;
27#[cfg(feature = "orchard")]
28use zcash_primitives::transaction::builder::BundlePadding;
29
30pub struct SingleOutputChangeStrategy<I> {
35 fee_rule: FixedFeeRule,
36 change_memo: Option<MemoBytes>,
37 fallback_change_pool: ShieldedPool,
38 dust_output_policy: DustOutputPolicy,
39 #[cfg(feature = "transparent-inputs")]
40 transparent_change_policy: TransparentChangePolicy,
41 meta_source: PhantomData<I>,
42}
43
44impl<I> SingleOutputChangeStrategy<I> {
45 pub fn new(
51 fee_rule: FixedFeeRule,
52 change_memo: Option<MemoBytes>,
53 fallback_change_pool: ShieldedPool,
54 dust_output_policy: DustOutputPolicy,
55 ) -> Self {
56 Self {
57 fee_rule,
58 change_memo,
59 fallback_change_pool,
60 dust_output_policy,
61 #[cfg(feature = "transparent-inputs")]
62 transparent_change_policy: TransparentChangePolicy::ShieldChange,
63 meta_source: PhantomData,
64 }
65 }
66
67 #[cfg(feature = "transparent-inputs")]
74 pub fn with_transparent_change_policy(
75 mut self,
76 transparent_change_policy: TransparentChangePolicy,
77 ) -> Self {
78 self.transparent_change_policy = transparent_change_policy;
79 self
80 }
81}
82
83impl<I: InputSource> ChangeStrategy for SingleOutputChangeStrategy<I> {
84 type FeeRule = FixedFeeRule;
85 type Error = BalanceError;
86 type MetaSource = I;
87 type AccountMetaT = ChangeReceivers;
88
89 fn fee_rule(&self) -> &Self::FeeRule {
90 &self.fee_rule
91 }
92
93 fn fetch_wallet_meta<P: consensus::Parameters>(
94 &self,
95 _params: &P,
96 meta_source: &Self::MetaSource,
97 account: <Self::MetaSource as InputSource>::AccountId,
98 _target_height: TargetHeight,
99 _exclude: &[<Self::MetaSource as crate::data_api::InputSource>::NoteRef],
100 ) -> Result<Self::AccountMetaT, <Self::MetaSource as crate::data_api::InputSource>::Error> {
101 meta_source.get_change_receivers(account)
102 }
103
104 fn compute_balance<P: consensus::Parameters, NoteRefT: Clone>(
105 &self,
106 params: &P,
107 target_height: TargetHeight,
108 anchor_height: BlockHeight,
109 zip318: &PoolMigrationParams,
110 transparent_inputs: &[impl transparent::InputView],
111 transparent_outputs: &[impl transparent::OutputView],
112 sapling: &impl sapling_fees::BundleView<NoteRefT>,
113 #[cfg(feature = "orchard")] orchard: &impl orchard_fees::BundleView<NoteRefT>,
114 #[cfg(feature = "orchard")] ironwood: &impl orchard_fees::BundleView<NoteRefT>,
115 ephemeral_balance: Option<EphemeralBalance>,
116 wallet_meta: &Self::AccountMetaT,
117 ) -> Result<TransactionBalance, ChangeError<Self::Error, NoteRefT>> {
118 let split_policy = SplitPolicy::single_output();
119 let cfg = SinglePoolBalanceConfig::new(
120 params,
121 &self.fee_rule,
122 &self.dust_output_policy,
123 self.fee_rule.fixed_fee(),
124 &split_policy,
125 self.fallback_change_pool,
126 #[cfg(feature = "transparent-inputs")]
127 self.transparent_change_policy,
128 Zatoshis::ZERO,
129 0,
130 );
131
132 single_pool_output_balance(
133 cfg,
134 None,
135 wallet_meta,
136 target_height,
137 transparent_inputs,
138 transparent_outputs,
139 sapling,
140 #[cfg(feature = "orchard")]
141 orchard,
142 #[cfg(feature = "orchard")]
143 ironwood,
144 #[cfg(feature = "orchard")]
146 BundlePadding::DEFAULT,
147 anchor_height,
148 zip318,
149 self.change_memo.as_ref(),
150 ephemeral_balance,
151 )
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 #![allow(deprecated)]
159
160 use crate::data_api::anchor_retention::{AnchorRetentionInterval, PoolMigrationParams};
161 use crate::fees::tests::all_change_receivers;
162 use ::transparent::bundle::TxOut;
163 use zcash_primitives::transaction::fees::{
164 fixed::FeeRule as FixedFeeRule, zip317::MINIMUM_FEE,
165 };
166 use zcash_protocol::consensus::BlockHeight;
167 use zcash_protocol::{
168 ShieldedPool,
169 consensus::{Network, NetworkUpgrade, Parameters},
170 value::Zatoshis,
171 };
172
173 use super::SingleOutputChangeStrategy;
174 use crate::{
175 data_api::{testing::MockWalletDb, wallet::input_selection::SaplingPayment},
176 fees::{
177 ChangeError, ChangeStrategy, ChangeValue, DustOutputPolicy,
178 tests::{TestSaplingInput, TestTransparentInput},
179 },
180 };
181
182 #[cfg(feature = "orchard")]
183 use crate::fees::orchard as orchard_fees;
184
185 #[test]
186 fn change_without_dust() {
187 let fee_rule = FixedFeeRule::non_standard(MINIMUM_FEE);
188 let change_strategy = SingleOutputChangeStrategy::<MockWalletDb>::new(
189 fee_rule,
190 None,
191 ShieldedPool::Sapling,
192 DustOutputPolicy::default(),
193 );
194
195 let result = change_strategy.compute_balance(
197 &Network::TestNetwork,
198 Network::TestNetwork
199 .activation_height(NetworkUpgrade::Nu5)
200 .unwrap()
201 .into(),
202 BlockHeight::from_u32(1),
203 &PoolMigrationParams::new(
204 zcash_protocol::zip318::BlockSpacing::UNIFORM,
205 AnchorRetentionInterval::ZIP_318,
206 ),
207 &[] as &[TestTransparentInput],
208 &[] as &[TxOut<Zatoshis>],
209 &(
210 sapling::builder::BundleType::DEFAULT,
211 &[TestSaplingInput {
212 note_id: 0,
213 value: Zatoshis::const_from_u64(60000),
214 }][..],
215 &[SaplingPayment::new(Zatoshis::const_from_u64(40000))][..],
216 ),
217 #[cfg(feature = "orchard")]
218 &orchard_fees::EmptyBundleView,
219 #[cfg(feature = "orchard")]
220 &orchard_fees::EmptyBundleView,
221 None,
222 &all_change_receivers(),
223 );
224
225 assert_matches!(
226 result,
227 Ok(balance) if
228 balance.proposed_change() == [ChangeValue::sapling(Zatoshis::const_from_u64(10000), None)] &&
229 balance.fee_required() == MINIMUM_FEE
230 );
231 }
232
233 #[test]
234 fn dust_change() {
235 let fee_rule = FixedFeeRule::non_standard(MINIMUM_FEE);
236 let change_strategy = SingleOutputChangeStrategy::<MockWalletDb>::new(
237 fee_rule,
238 None,
239 ShieldedPool::Sapling,
240 DustOutputPolicy::default(),
241 );
242
243 let result = change_strategy.compute_balance(
245 &Network::TestNetwork,
246 Network::TestNetwork
247 .activation_height(NetworkUpgrade::Nu5)
248 .unwrap()
249 .into(),
250 BlockHeight::from_u32(1),
251 &PoolMigrationParams::new(
252 zcash_protocol::zip318::BlockSpacing::UNIFORM,
253 AnchorRetentionInterval::ZIP_318,
254 ),
255 &[] as &[TestTransparentInput],
256 &[] as &[TxOut<Zatoshis>],
257 &(
258 sapling::builder::BundleType::DEFAULT,
259 &[
260 TestSaplingInput {
261 note_id: 0,
262 value: Zatoshis::const_from_u64(40000),
263 },
264 TestSaplingInput {
266 note_id: 0,
267 value: Zatoshis::const_from_u64(10100),
268 },
269 ][..],
270 &[SaplingPayment::new(Zatoshis::const_from_u64(40000))][..],
271 ),
272 #[cfg(feature = "orchard")]
273 &orchard_fees::EmptyBundleView,
274 #[cfg(feature = "orchard")]
275 &orchard_fees::EmptyBundleView,
276 None,
277 &all_change_receivers(),
278 );
279
280 assert_matches!(
281 result,
282 Err(ChangeError::InsufficientFunds { available, required })
283 if available == Zatoshis::const_from_u64(50100) && required == Zatoshis::const_from_u64(60000)
284 );
285 }
286}