Bugfix for reading u256.

This commit is contained in:
chriseth 2022-09-01 23:04:36 +02:00
parent d55e5fdb68
commit d886e03aa4
5 changed files with 35 additions and 9 deletions

View file

@ -65,11 +65,12 @@ impl<'a> Adjacencies<'a> {
.entry(from.clone()) .entry(from.clone())
.or_insert_with(|| { .or_insert_with(|| {
let mut result: HashMap<Node, U256> = HashMap::new(); let mut result: HashMap<Node, U256> = 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 { match from {
Node::Node(_) => { Node::Node(_) => {
// One edge from "from" to "from x token" with a capacity // 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 result
.entry(pseudo_node(*edge)) .entry(pseudo_node(*edge))
.and_modify(|c| { .and_modify(|c| {

View file

@ -1,12 +1,22 @@
use crate::types::{Address}; use crate::types::Address;
use std::fmt::{Display, Formatter};
mod adjacencies; mod adjacencies;
mod flow; mod flow;
#[derive(Eq, PartialEq, Hash, Clone)] #[derive(Debug, Eq, PartialEq, Hash, Clone)]
enum Node { pub enum Node {
Node(Address), Node(Address),
TokenEdge(Address, 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; pub use crate::flow::flow::compute_flow;

View file

@ -48,7 +48,7 @@ fn read_u256(file: &mut File) -> Result<U256, io::Error> {
let mut bytes = [0u8; 32]; let mut bytes = [0u8; 32];
file.read_exact(&mut bytes[32 - length..32])?; file.read_exact(&mut bytes[32 - length..32])?;
let high = u128::from_be_bytes(*<&[u8; 16]>::try_from(&bytes[0..16]).unwrap()); 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)) Ok(U256::new(high, low))
} }

View file

@ -1,6 +1,5 @@
use std::env; use std::env;
mod flow; mod flow;
mod io; mod io;
mod types; mod types;
@ -16,6 +15,10 @@ fn main() {
println!("Computing flow {from_str} -> {to_str} using {edges_file}"); println!("Computing flow {from_str} -> {to_str} using {edges_file}");
let edges = io::read_edges_binary(edges_file).expect("Error loading edges."); let edges = io::read_edges_binary(edges_file).expect("Error loading edges.");
print!("Read {} edges", edges.len()); println!("Read {} edges", edges.len());
flow::compute_flow(&Address::from(from_str.as_str()), &Address::from(to_str.as_str()), &edges); flow::compute_flow(
&Address::from(from_str.as_str()),
&Address::from(to_str.as_str()),
&edges,
);
} }

View file

@ -1,3 +1,5 @@
use std::fmt::{Display, Formatter};
#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd)]
pub struct Address([u8; 20]); pub struct Address([u8; 20]);
@ -18,3 +20,13 @@ impl From<&str> for Address {
Address(data) 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(())
}
}