Move plan methods to a trait
This commit is contained in:
		| @@ -9,7 +9,7 @@ use chrono::prelude::*; | ||||
| use entry::Entry; | ||||
| use hash::Hash; | ||||
| use mint::Mint; | ||||
| use plan::{Payment, Plan, Witness}; | ||||
| use plan::{Payment, PaymentPlan, Plan, Witness}; | ||||
| use rayon::prelude::*; | ||||
| use signature::{KeyPair, PublicKey, Signature}; | ||||
| use std::collections::hash_map::Entry::Occupied; | ||||
|   | ||||
| @@ -11,6 +11,7 @@ use pnet::datalink; | ||||
| use solana::bank::Bank; | ||||
| use solana::crdt::ReplicatedData; | ||||
| use solana::entry::Entry; | ||||
| use solana::plan::PaymentPlan; | ||||
| use solana::server::Server; | ||||
| use solana::signature::{KeyPair, KeyPairUtil}; | ||||
| use solana::transaction::Instruction; | ||||
|   | ||||
							
								
								
									
										20
									
								
								src/plan.rs
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/plan.rs
									
									
									
									
									
								
							| @@ -36,6 +36,18 @@ pub struct Payment { | ||||
|     pub to: PublicKey, | ||||
| } | ||||
|  | ||||
| pub trait PaymentPlan { | ||||
|     /// Return Payment if the spending plan requires no additional Witnesses. | ||||
|     fn final_payment(&self) -> Option<Payment>; | ||||
|  | ||||
|     /// Return true if the plan spends exactly `spendable_tokens`. | ||||
|     fn verify(&self, spendable_tokens: i64) -> bool; | ||||
|  | ||||
|     /// Apply a witness to the spending plan to see if the plan can be reduced. | ||||
|     /// If so, modify the plan in-place. | ||||
|     fn apply_witness(&mut self, witness: &Witness); | ||||
| } | ||||
|  | ||||
| #[repr(C)] | ||||
| #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] | ||||
| pub enum Plan { | ||||
| @@ -73,9 +85,11 @@ impl Plan { | ||||
|             (Condition::Signature(from), Payment { tokens, to: from }), | ||||
|         ) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl PaymentPlan for Plan { | ||||
|     /// Return Payment if the spending plan requires no additional Witnesses. | ||||
|     pub fn final_payment(&self) -> Option<Payment> { | ||||
|     fn final_payment(&self) -> Option<Payment> { | ||||
|         match *self { | ||||
|             Plan::Pay(ref payment) => Some(payment.clone()), | ||||
|             _ => None, | ||||
| @@ -83,7 +97,7 @@ impl Plan { | ||||
|     } | ||||
|  | ||||
|     /// Return true if the plan spends exactly `spendable_tokens`. | ||||
|     pub fn verify(&self, spendable_tokens: i64) -> bool { | ||||
|     fn verify(&self, spendable_tokens: i64) -> bool { | ||||
|         match *self { | ||||
|             Plan::Pay(ref payment) | Plan::After(_, ref payment) => { | ||||
|                 payment.tokens == spendable_tokens | ||||
| @@ -96,7 +110,7 @@ impl Plan { | ||||
|  | ||||
|     /// Apply a witness to the spending plan to see if the plan can be reduced. | ||||
|     /// If so, modify the plan in-place. | ||||
|     pub fn apply_witness(&mut self, witness: &Witness) { | ||||
|     fn apply_witness(&mut self, witness: &Witness) { | ||||
|         let new_payment = match *self { | ||||
|             Plan::After(ref cond, ref payment) if cond.is_satisfied(witness) => Some(payment), | ||||
|             Plan::Race((ref cond, ref payment), _) if cond.is_satisfied(witness) => Some(payment), | ||||
|   | ||||
| @@ -3,7 +3,7 @@ | ||||
| use bincode::serialize; | ||||
| use chrono::prelude::*; | ||||
| use hash::Hash; | ||||
| use plan::{Condition, Payment, Plan}; | ||||
| use plan::{Condition, Payment, PaymentPlan, Plan}; | ||||
| use signature::{KeyPair, KeyPairUtil, PublicKey, Signature, SignatureUtil}; | ||||
|  | ||||
| pub const SIGNED_DATA_OFFSET: usize = 112; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user