Skip to content
Permalink
Browse files

update to ledgers

  • Loading branch information...
vincenthz committed Mar 12, 2019
1 parent a2c3fa1 commit 7267e7466acc5178d32595f85c7dbfe72f76295d
@@ -30,7 +30,7 @@ impl Environment {
pub fn new() -> Self {
let g = StdGen::new(rand::thread_rng(), 10);
Environment {
ledger: Ledger::new(HashMap::new()),
ledger: Ledger::new(),
gen: g,
users: HashMap::new(),
keys: HashMap::new(),
@@ -18,7 +18,6 @@ use std::sync::{Arc, RwLock};

#[derive(Debug)]
pub struct GenesisLeaderSelection {
ledger: Arc<RwLock<Ledger>>,
settings: Arc<RwLock<Settings>>,

bft_leaders: Vec<BftLeader>,
@@ -3,23 +3,33 @@

use crate::error::*;
use crate::transaction::*;
use crate::update::TransactionsDiff;
use crate::value::*;
use crate::{account, block, leadership, setting, utxo};
use cardano::address::Addr as OldAddress;
use chain_addr::Address;
use chain_core::property;
use std::collections::HashMap;

/// Basic ledger structure. Ledger is represented as the
/// state of unspent output values, associated with their
/// owner.
#[derive(Debug, Clone)]
/// Overall ledger structure.
///
/// This represent a given state related to utxo/old utxo/accounts/... at a given
/// point in time.
///
/// The ledger can be easily and cheaply cloned despite containing refering
/// to a lot of data (millions of utxos, thousands of accounts, ..)
#[derive(Clone)]
pub struct Ledger {
pub unspent_outputs: HashMap<UtxoPointer, Output<Address>>,
pub(crate) utxos: utxo::Ledger<Address>,
pub(crate) oldutxos: utxo::Ledger<OldAddress>,
pub(crate) accounts: account::Ledger,
}

impl Ledger {
pub fn new(input: HashMap<UtxoPointer, Output<Address>>) -> Self {
pub fn new() -> Self {
Ledger {
unspent_outputs: input,
utxos: utxo::Ledger::new(),
oldutxos: utxo::Ledger::new(),
accounts: account::Ledger::new(),
}
}
}
@@ -1,4 +1,5 @@
use crate::key::{deserialize_public_key, serialize_public_key};
use crate::ledger::Ledger;
use crate::value::Value;
use chain_addr::Kind;
use chain_core::property;
@@ -43,8 +44,8 @@ impl DelegationState {
pub fn get_stake_distribution(&self, ledger: &Ledger) -> StakeDistribution {
let mut dist = HashMap::new();

for (ptr, output) in ledger.unspent_outputs.iter() {
assert_eq!(ptr.value, output.1);
for output in ledger.utxos.iter() {
//assert_eq!(ptr.value, output.1);

// We're only interested in "group" addresses
// (i.e. containing a spending key and a stake key).
@@ -66,13 +67,13 @@ impl DelegationState {
});
// note: unwrap should be safe, the system should have a total less than overflow
stake_pool_dist.total_stake =
(stake_pool_dist.total_stake + ptr.value).unwrap();
(stake_pool_dist.total_stake + output.1).unwrap();

let member_dist = stake_pool_dist
.member_stake
.entry(stake_key.clone())
.or_insert_with(|| Value::zero());
*member_dist = (*member_dist + ptr.value).unwrap();
*member_dist = (*member_dist + output.1).unwrap();
}
}
}
@@ -2,9 +2,8 @@
//!

use crate::block::{BlockContents, Message};
use crate::ledger::Ledger;
use crate::{account, block, leadership, setting, utxo};
use cardano::address::Addr as OldAddress;
use chain_addr::Address;
use chain_core::property;

pub(crate) type Leadership = Box<
@@ -15,13 +14,6 @@ pub(crate) type Leadership = Box<
>,
>;

#[derive(Clone)]
pub struct Ledger {
pub(crate) utxos: utxo::Ledger<Address>,
pub(crate) oldutxos: utxo::Ledger<OldAddress>,
pub(crate) accounts: account::Ledger,
}

pub struct State {
pub(crate) ledger: Ledger,
pub(crate) settings: setting::Settings,
@@ -45,16 +37,6 @@ impl State {
}
}

impl Ledger {
fn new() -> Self {
Ledger {
utxos: utxo::Ledger::new(),
oldutxos: utxo::Ledger::new(),
accounts: account::Ledger::new(),
}
}
}

impl State {
pub fn new() -> Self {
State {
@@ -5,10 +5,11 @@
//!

use crate::transaction::{Output, TransactionId, TransactionIndex};
use std::collections::btree_map;
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;

use imhamt::{Hamt, InsertError, RemoveError, ReplaceError, UpdateError};
use imhamt::{Hamt, HamtIter, InsertError, RemoveError, ReplaceError, UpdateError};

/// UTXO Ledger Error
pub enum Error {
@@ -79,6 +80,43 @@ impl<OutAddress: Clone> TransactionUnspents<OutAddress> {
#[derive(Clone)]
pub struct Ledger<OutAddress>(Hamt<DefaultHasher, TransactionId, TransactionUnspents<OutAddress>>);

pub struct Iter<'a, V> {
hamt_iter: HamtIter<'a, TransactionId, TransactionUnspents<V>>,
unspents_iter: Option<btree_map::Iter<'a, TransactionIndex, Output<V>>>,
}

impl<OutAddress> Ledger<OutAddress> {
pub fn iter<'a>(&'a self) -> Iter<'a, OutAddress> {
Iter {
hamt_iter: self.0.iter(),
unspents_iter: None,
}
}
}

impl<'a, V> Iterator for Iter<'a, V> {
type Item = &'a Output<V>;

fn next(&mut self) -> Option<Self::Item> {
match &mut self.unspents_iter {
None => match self.hamt_iter.next() {
None => None,
Some(unspent) => {
self.unspents_iter = Some((unspent.1).0.iter());
self.next()
}
},
Some(o) => match o.next() {
None => {
self.unspents_iter = None;
self.next()
}
Some(x) => Some(x.1),
},
}
}
}

impl<OutAddress: Clone> Ledger<OutAddress> {
/// Create a new empty UTXO Ledger
pub fn new() -> Self {

0 comments on commit 7267e74

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