From 62194eb7701d827156d110f44fc085d1ff4d56c2 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Mon, 25 Nov 2019 14:42:03 -0500 Subject: [PATCH] [VM] Address module cleanup (#32) * Cleaned up address module * fmt --- vm/src/address/mod.rs | 13 ++++++------- vm/{src/address/test.rs => tests/address_test.rs} | 9 ++------- 2 files changed, 8 insertions(+), 14 deletions(-) rename vm/{src/address/test.rs => tests/address_test.rs} (98%) diff --git a/vm/src/address/mod.rs b/vm/src/address/mod.rs index a8bb8bd4b801..8e0610fee006 100644 --- a/vm/src/address/mod.rs +++ b/vm/src/address/mod.rs @@ -1,7 +1,6 @@ mod errors; mod network; mod protocol; -mod test; pub use self::errors::Error; pub use self::network::Network; pub use self::protocol::Protocol; @@ -163,7 +162,7 @@ impl Address { bz } /// Returns encoded string from Address - pub fn to_string(&self, network: Option) -> Result { + pub fn to_string(&self, network: Option) -> String { match network { Some(net) => encode(self, net), None => encode(self, Network::Testnet), @@ -192,7 +191,7 @@ impl Address { } /// encode converts the address into a string -fn encode(addr: &Address, network: Network) -> Result { +fn encode(addr: &Address, network: Network) -> String { match addr.protocol { Protocol::Secp256k1 | Protocol::Actor | Protocol::BLS => { let mut ingest = addr.payload(); @@ -202,23 +201,23 @@ fn encode(addr: &Address, network: Network) -> Result { // payload bytes followed by calculated checksum bz.extend(cksm.clone()); - Ok(format!( + format!( "{}{}{}", network.to_prefix(), addr.protocol().to_string(), ADDRESS_ENCODER.encode(bz.as_mut()), - )) + ) } Protocol::ID => { let mut buf = [0; 1023]; buf.copy_from_slice(&addr.payload()); let mut readable = &buf[..]; - Ok(format!( + format!( "{}{}{}", network.to_prefix(), addr.protocol().to_string(), leb128::read::unsigned(&mut readable).expect("should read encoded bytes"), - )) + ) } } } diff --git a/vm/src/address/test.rs b/vm/tests/address_test.rs similarity index 98% rename from vm/src/address/test.rs rename to vm/tests/address_test.rs index ccd30c83d03f..ad2be4a9fda9 100644 --- a/vm/src/address/test.rs +++ b/vm/tests/address_test.rs @@ -1,6 +1,4 @@ -#![cfg(all(test))] - -use crate::address::{ +use vm::address::{ checksum, validate_checksum, Address, Error, Network, Protocol, BLS_PUB_LEN, PAYLOAD_HASH_LEN, }; @@ -36,10 +34,7 @@ struct AddressTestVec { fn test_address(addr: Address, protocol: Protocol, expected: &'static str) { // Test encoding to string - assert_eq!( - expected.to_owned(), - addr.to_string(Some(Network::Testnet)).unwrap() - ); + assert_eq!(expected.to_owned(), addr.to_string(Some(Network::Testnet))); // Test decoding from string let decoded = Address::from_string(expected.to_owned()).unwrap();