From ad00d7bd9cc869450189260b92c1b66f48a3f051 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Tue, 29 May 2018 13:00:44 -0600 Subject: [PATCH] Move plan methods to a trait --- src/bank.rs | 2 +- src/bin/fullnode.rs | 1 + src/plan.rs | 20 +++++++++++++++++--- src/transaction.rs | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/bank.rs b/src/bank.rs index 25c27e64c7..ba19b832a4 100644 --- a/src/bank.rs +++ b/src/bank.rs @@ -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; diff --git a/src/bin/fullnode.rs b/src/bin/fullnode.rs index 866deac5d9..ba172aed86 100644 --- a/src/bin/fullnode.rs +++ b/src/bin/fullnode.rs @@ -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; diff --git a/src/plan.rs b/src/plan.rs index efa78844c0..dc669a7fe1 100644 --- a/src/plan.rs +++ b/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; + + /// 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 { + fn final_payment(&self) -> Option { 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), diff --git a/src/transaction.rs b/src/transaction.rs index a7a866179d..9456f3c2c5 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -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;