Move plan methods to a trait

This commit is contained in:
Greg Fitzgerald 2018-05-29 13:00:44 -06:00
parent 8d1f82c34d
commit ad00d7bd9c
4 changed files with 20 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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