Skip to content

Commit

Permalink
[VM] Address module cleanup (#32)
Browse files Browse the repository at this point in the history
* Cleaned up address module

* fmt
  • Loading branch information
austinabell authored Nov 25, 2019
1 parent ae84675 commit 62194eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
13 changes: 6 additions & 7 deletions vm/src/address/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -163,7 +162,7 @@ impl Address {
bz
}
/// Returns encoded string from Address
pub fn to_string(&self, network: Option<Network>) -> Result<String, String> {
pub fn to_string(&self, network: Option<Network>) -> String {
match network {
Some(net) => encode(self, net),
None => encode(self, Network::Testnet),
Expand Down Expand Up @@ -192,7 +191,7 @@ impl Address {
}

/// encode converts the address into a string
fn encode(addr: &Address, network: Network) -> Result<String, String> {
fn encode(addr: &Address, network: Network) -> String {
match addr.protocol {
Protocol::Secp256k1 | Protocol::Actor | Protocol::BLS => {
let mut ingest = addr.payload();
Expand All @@ -202,23 +201,23 @@ fn encode(addr: &Address, network: Network) -> Result<String, String> {

// 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"),
))
)
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions vm/src/address/test.rs → vm/tests/address_test.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 62194eb

Please sign in to comment.