Skip to content
Permalink
Browse files

Merge pull request #686 from input-output-hk/rest_account

Rest account
  • Loading branch information...
NicolasDP committed May 15, 2019
2 parents 323c2e4 + c1e96b6 commit cee51f94421f7edfa2c3c9493107c0405132d5fb
@@ -39,11 +39,11 @@ pub enum Error {
expected: &'static str,
actual: String,
},
DataInvalid(Box<StdError + 'static>),
DataInvalid(Box<StdError + Send + Sync + 'static>),
}

impl Error {
pub fn data_invalid(cause: impl StdError + 'static) -> Self {
pub fn data_invalid(cause: impl StdError + Send + Sync + 'static) -> Self {
Error::DataInvalid(Box::new(cause))
}
}
@@ -7,19 +7,34 @@
use crate::value::*;
use imhamt::{Hamt, InsertError, UpdateError};
use std::collections::hash_map::DefaultHasher;
use std::fmt::{self, Display, Formatter};
use std::hash::Hash;

/// Possible errors during an account operation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LedgerError {
NonExistent,
AlreadyExists,
MismatchCounter,
NeedTotalWithdrawal,
NonZero,
ValueError(ValueError),
}

impl Display for LedgerError {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
match self {
LedgerError::NonExistent => "Account does not exist",
LedgerError::AlreadyExists => "Account already exists",
LedgerError::NeedTotalWithdrawal => {
"Operation counter reached its maximum and next operation must be full withdrawal"
}
LedgerError::NonZero => "Removed account is not empty",
LedgerError::ValueError(_) => "Value calculation failed",
}
.fmt(formatter)
}
}

impl From<ValueError> for LedgerError {
fn from(e: ValueError) -> Self {
LedgerError::ValueError(e)
@@ -95,6 +110,10 @@ impl AccountState {
pub fn get_value(&self) -> Value {
self.value
}

pub fn get_counter(&self) -> u32 {
self.counter.into()
}
}

/// Spending counter associated to an account.
@@ -126,6 +145,12 @@ impl From<u32> for SpendingCounter {
}
}

impl Into<u32> for SpendingCounter {
fn into(self) -> u32 {
self.0
}
}

/// The public ledger of all accounts associated with their current state
#[derive(Clone)]
pub struct Ledger<ID: Hash + Eq>(Hamt<DefaultHasher, ID, AccountState>);
@@ -152,6 +177,13 @@ impl<ID: Clone + Eq + Hash> Ledger<ID> {
self.0.contains_key(identifier)
}

/// Get account state
///
/// If the identifier does not match any account, error out
pub fn get_state(&self, account: &ID) -> Result<&AccountState, LedgerError> {
self.0.lookup(account).ok_or(LedgerError::NonExistent)
}

/// Remove an account from this ledger
///
/// If the account still have value > 0, then error
@@ -464,6 +464,10 @@ impl Ledger {
self.static_params.as_ref()
}

pub fn accounts(&self) -> &account::Ledger {
&self.accounts
}

pub fn get_ledger_parameters(&self) -> LedgerParameters {
LedgerParameters {
fees: *self.settings.linear_fees,

0 comments on commit cee51f9

Please sign in to comment.
You can’t perform that action at this time.