diff --git a/src/flow/adjacencies.rs b/src/flow/adjacencies.rs index 682f744..7736517 100644 --- a/src/flow/adjacencies.rs +++ b/src/flow/adjacencies.rs @@ -65,11 +65,12 @@ impl<'a> Adjacencies<'a> { .entry(from.clone()) .or_insert_with(|| { let mut result: HashMap = HashMap::new(); - for edge in &self.edges[source_address_of(from)] { + // Plain edges are (from, to, token) labeled with capacity + for edge in self.edges.get(source_address_of(from)).unwrap_or(&vec![]) { match from { Node::Node(_) => { // One edge from "from" to "from x token" with a capacity - // as the max over all contributing edges (the balance of the sender) + // as the max over all "to" addresses (the balance of the sender) result .entry(pseudo_node(*edge)) .and_modify(|c| { diff --git a/src/flow/mod.rs b/src/flow/mod.rs index 7072613..326280e 100644 --- a/src/flow/mod.rs +++ b/src/flow/mod.rs @@ -1,12 +1,22 @@ -use crate::types::{Address}; +use crate::types::Address; +use std::fmt::{Display, Formatter}; mod adjacencies; mod flow; -#[derive(Eq, PartialEq, Hash, Clone)] -enum Node { +#[derive(Debug, Eq, PartialEq, Hash, Clone)] +pub enum Node { Node(Address), TokenEdge(Address, Address), } +impl Display for Node { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + match self { + Node::Node(address) => write!(f, "{address}"), + Node::TokenEdge(address, token) => write!(f, "({address} x {token})"), + } + } +} + pub use crate::flow::flow::compute_flow; diff --git a/src/io.rs b/src/io.rs index a76c720..c6b7060 100644 --- a/src/io.rs +++ b/src/io.rs @@ -48,7 +48,7 @@ fn read_u256(file: &mut File) -> Result { let mut bytes = [0u8; 32]; file.read_exact(&mut bytes[32 - length..32])?; let high = u128::from_be_bytes(*<&[u8; 16]>::try_from(&bytes[0..16]).unwrap()); - let low = u128::from_be_bytes(*<&[u8; 16]>::try_from(&bytes[0..16]).unwrap()); + let low = u128::from_be_bytes(*<&[u8; 16]>::try_from(&bytes[16..32]).unwrap()); Ok(U256::new(high, low)) } diff --git a/src/main.rs b/src/main.rs index 0aa39cd..b994d90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use std::env; - mod flow; mod io; mod types; @@ -16,6 +15,10 @@ fn main() { println!("Computing flow {from_str} -> {to_str} using {edges_file}"); let edges = io::read_edges_binary(edges_file).expect("Error loading edges."); - print!("Read {} edges", edges.len()); - flow::compute_flow(&Address::from(from_str.as_str()), &Address::from(to_str.as_str()), &edges); + println!("Read {} edges", edges.len()); + flow::compute_flow( + &Address::from(from_str.as_str()), + &Address::from(to_str.as_str()), + &edges, + ); } diff --git a/src/types/address.rs b/src/types/address.rs index d68c42f..6d7a7d8 100644 --- a/src/types/address.rs +++ b/src/types/address.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Formatter}; + #[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd)] pub struct Address([u8; 20]); @@ -18,3 +20,13 @@ impl From<&str> for Address { Address(data) } } + +impl Display for Address { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + write!(f, "0x")?; + for b in self.0 { + write!(f, "{:02x}", b)?; + } + Ok(()) + } +}