Skip to content

Commit

Permalink
Implementing Market Balance (#524)
Browse files Browse the repository at this point in the history
* Implementing market balance

* Lint fix

* Adding requested changes

* Update blockchain/state_manager/src/lib.rs

Co-authored-by: Purple Hair Rust Bard <AshantiMutinta@gmail.com>

* Update blockchain/state_manager/src/lib.rs

Co-authored-by: Purple Hair Rust Bard <AshantiMutinta@gmail.com>

* Update blockchain/state_manager/src/lib.rs

Co-authored-by: Purple Hair Rust Bard <AshantiMutinta@gmail.com>

* Update lib.rs

Co-authored-by: Purple Hair Rust Bard <AshantiMutinta@gmail.com>
  • Loading branch information
1 parent 7143e42 commit a2cab73
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion blockchain/state_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
mod errors;
pub mod utils;
pub use self::errors::*;
use actor::{init, miner, power, ActorState, INIT_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR};
use actor::{
init, market, miner, power, ActorState, BalanceTable, INIT_ACTOR_ADDR,
STORAGE_MARKET_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR,
};
use address::{Address, BLSPublicKey, Payload, BLS_PUB_LEN};
use async_log::span;
use async_std::sync::RwLock;
Expand All @@ -26,6 +29,13 @@ use std::sync::Arc;
/// Intermediary for retrieving state objects and updating actor states
pub type CidPair = (Cid, Cid);

#[allow(dead_code)]
#[derive(Default)]
pub struct MarketBalance {
escrow: BigUint,
locked: BigUint,
}

pub struct StateManager<DB> {
bs: Arc<DB>,
cache: RwLock<HashMap<TipsetKeys, CidPair>>,
Expand Down Expand Up @@ -234,4 +244,31 @@ where
)),
}
}

pub fn lookup_id(&self, addr: &Address, ts: &Tipset) -> Result<Address, Error> {
let state_tree = StateTree::new_from_root(self.bs.as_ref(), ts.parent_state())?;
state_tree.lookup_id(addr).map_err(Error::State)
}

pub fn market_balance(&mut self, addr: &Address, ts: &Tipset) -> Result<MarketBalance, Error> {
let market_state: market::State =
self.load_actor_state(&*STORAGE_MARKET_ACTOR_ADDR, ts.parent_state())?;

let new_addr = self.lookup_id(addr, ts)?;

let out = MarketBalance {
escrow: {
let et = BalanceTable::from_root(self.bs.as_ref(), &market_state.escrow_table)
.map_err(|_x| Error::State("Failed to build Escrow Table".to_string()))?;
et.get(&new_addr).unwrap_or_default()
},
locked: {
let lt = BalanceTable::from_root(self.bs.as_ref(), &market_state.locked_table)
.map_err(|_x| Error::State("Failed to build Locked Table".to_string()))?;
lt.get(&new_addr).unwrap_or_default()
},
};

Ok(out)
}
}

0 comments on commit a2cab73

Please sign in to comment.