Refactor to support loading an existing ledger
This commit is contained in:
parent
b8655e30d4
commit
876d7995e1
|
@ -48,60 +48,51 @@ impl Accountant {
|
|||
entries
|
||||
}
|
||||
|
||||
pub fn deposit_signed(self: &mut Self, to: PublicKey, data: u64, sig: Signature) -> Result<()> {
|
||||
let event = Event::new_claim(to, data, sig);
|
||||
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> Result<Signature> {
|
||||
use event::{get_pubkey, sign_claim_data};
|
||||
let to = get_pubkey(keypair);
|
||||
let sig = sign_claim_data(&n, keypair);
|
||||
let event = Event::new_claim(to, n, sig);
|
||||
if !self.historian.verify_event(&event) {
|
||||
return Err(AccountingError::InvalidEvent);
|
||||
}
|
||||
if let Err(SendError(_)) = self.historian.sender.send(event) {
|
||||
return Err(AccountingError::SendError);
|
||||
self.process_verified_event(event, true).map(|_| sig)
|
||||
}
|
||||
|
||||
if self.balances.contains_key(&to) {
|
||||
if let Some(x) = self.balances.get_mut(&to) {
|
||||
*x += data;
|
||||
}
|
||||
} else {
|
||||
self.balances.insert(to, data);
|
||||
fn is_deposit(allow_deposits: bool, from: &PublicKey, to: &PublicKey) -> bool {
|
||||
allow_deposits && from == to
|
||||
}
|
||||
|
||||
Ok(())
|
||||
pub fn process_event(self: &mut Self, event: Event<u64>) -> Result<()> {
|
||||
if !self.historian.verify_event(&event) {
|
||||
return Err(AccountingError::InvalidEvent);
|
||||
}
|
||||
self.process_verified_event(event, false)
|
||||
}
|
||||
|
||||
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> Result<Signature> {
|
||||
use event::{get_pubkey, sign_claim_data};
|
||||
let key = get_pubkey(keypair);
|
||||
let sig = sign_claim_data(&n, keypair);
|
||||
self.deposit_signed(key, n, sig).map(|_| sig)
|
||||
}
|
||||
|
||||
pub fn transfer_signed(
|
||||
fn process_verified_event(
|
||||
self: &mut Self,
|
||||
from: PublicKey,
|
||||
to: PublicKey,
|
||||
data: u64,
|
||||
sig: Signature,
|
||||
event: Event<u64>,
|
||||
allow_deposits: bool,
|
||||
) -> Result<()> {
|
||||
match event {
|
||||
Event::Tick => Ok(()),
|
||||
Event::Transaction { from, to, data, .. } => {
|
||||
if !Self::is_deposit(allow_deposits, &from, &to) {
|
||||
if self.get_balance(&from).unwrap_or(0) < data {
|
||||
return Err(AccountingError::InsufficientFunds);
|
||||
}
|
||||
|
||||
let event = Event::Transaction {
|
||||
from,
|
||||
to,
|
||||
data,
|
||||
sig,
|
||||
};
|
||||
if !self.historian.verify_event(&event) {
|
||||
return Err(AccountingError::InvalidEvent);
|
||||
}
|
||||
|
||||
if let Err(SendError(_)) = self.historian.sender.send(event) {
|
||||
return Err(AccountingError::SendError);
|
||||
}
|
||||
|
||||
if !Self::is_deposit(allow_deposits, &from, &to) {
|
||||
if let Some(x) = self.balances.get_mut(&from) {
|
||||
*x -= data;
|
||||
}
|
||||
}
|
||||
|
||||
if self.balances.contains_key(&to) {
|
||||
if let Some(x) = self.balances.get_mut(&to) {
|
||||
|
@ -110,9 +101,10 @@ impl Accountant {
|
|||
} else {
|
||||
self.balances.insert(to, data);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transfer(
|
||||
self: &mut Self,
|
||||
|
@ -123,7 +115,13 @@ impl Accountant {
|
|||
use event::{get_pubkey, sign_transaction_data};
|
||||
let from = get_pubkey(keypair);
|
||||
let sig = sign_transaction_data(&n, keypair, &to);
|
||||
self.transfer_signed(from, to, n, sig).map(|_| sig)
|
||||
let event = Event::Transaction {
|
||||
from,
|
||||
to,
|
||||
data: n,
|
||||
sig,
|
||||
};
|
||||
self.process_event(event).map(|_| sig)
|
||||
}
|
||||
|
||||
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<u64> {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::io;
|
||||
use accountant::Accountant;
|
||||
use event::{PublicKey, Signature};
|
||||
//use serde::Serialize;
|
||||
use event::{Event, PublicKey, Signature};
|
||||
|
||||
pub struct AccountantSkel {
|
||||
pub obj: Accountant,
|
||||
|
@ -9,11 +8,6 @@ pub struct AccountantSkel {
|
|||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum Request {
|
||||
Deposit {
|
||||
key: PublicKey,
|
||||
val: u64,
|
||||
sig: Signature,
|
||||
},
|
||||
Transfer {
|
||||
from: PublicKey,
|
||||
to: PublicKey,
|
||||
|
@ -41,14 +35,14 @@ impl AccountantSkel {
|
|||
|
||||
pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> {
|
||||
match msg {
|
||||
Request::Deposit { key, val, sig } => {
|
||||
if let Err(err) = self.obj.deposit_signed(key, val, sig) {
|
||||
println!("Deposit error: {:?}", err);
|
||||
}
|
||||
None
|
||||
}
|
||||
Request::Transfer { from, to, val, sig } => {
|
||||
if let Err(err) = self.obj.transfer_signed(from, to, val, sig) {
|
||||
let event = Event::Transaction {
|
||||
from,
|
||||
to,
|
||||
data: val,
|
||||
sig,
|
||||
};
|
||||
if let Err(err) = self.obj.process_event(event) {
|
||||
println!("Transfer error: {:?}", err);
|
||||
}
|
||||
None
|
||||
|
|
Loading…
Reference in New Issue