Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch from bs58 to multibase for base58 #56

Merged
merged 6 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
status:
patch: off
77 changes: 77 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: build

on: [push, pull_request]

jobs:
build:
name: Build
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
toolchain: [stable]
runs-on: ${{ matrix.platform }}

steps:
- name: Checkout Sources
uses: actions/checkout@v3

- name: Cache Dependencies & Build Outputs
uses: actions/cache@v3
with:
path: ~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
components: rustfmt, clippy

- name: Check Code Format
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- name: Code Lint
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --all-features --workspace -- -D warnings

- name: Code Lint Without Default Features
uses: actions-rs/cargo@v1
with:
command: clippy
args: --no-default-features --workspace -- -D warnings

- name: Test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --workspace

coverage:
name: Code Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v3

- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Generate Code Coverage
uses: actions-rs/tarpaulin@v0.1
with:
args: --all-features

- name: Upload Code Coverage
uses: codecov/codecov-action@v3
31 changes: 0 additions & 31 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ default = ["url"]

[dependencies]
arrayref = "0.3"
bs58 = "0.4.0"
byteorder = "1.3.1"
data-encoding = "2.1"
multibase = "0.9.1"
multihash = { version = "0.16", default-features = false, features = ["std", "multihash-impl", "identity"] }
percent-encoding = "2.1.0"
serde = "1.0.70"
Expand Down
10 changes: 6 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{net, fmt, error, io, num, str, string};
use std::{error, fmt, io, net, num, str, string};
use unsigned_varint::decode;

pub type Result<T> = ::std::result::Result<T, Error>;
Expand All @@ -25,7 +25,9 @@ impl fmt::Display for Error {
Error::InvalidUvar(e) => write!(f, "failed to decode unsigned varint: {}", e),
Error::ParsingError(e) => write!(f, "failed to parse: {}", e),
Error::UnknownProtocolId(id) => write!(f, "unknown protocol id: {}", id),
Error::UnknownProtocolString(string) => write!(f, "unknown protocol string: {}", string),
Error::UnknownProtocolString(string) => {
write!(f, "unknown protocol string: {}", string)
}
}
}
}
Expand Down Expand Up @@ -53,8 +55,8 @@ impl From<multihash::Error> for Error {
}
}

impl From<bs58::decode::Error> for Error {
fn from(err: bs58::decode::Error) -> Error {
impl From<multibase::Error> for Error {
fn from(err: multibase::Error) -> Error {
Error::ParsingError(err.into())
}
}
Expand Down
57 changes: 35 additions & 22 deletions src/from_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,21 @@ fn from_url_inner(url: &str, lossy: bool) -> std::result::Result<Multiaddr, From
// Note: if you add support for a new scheme, please update the documentation as well.
"ws" | "wss" | "http" | "https" => from_url_inner_http_ws(url, lossy),
"unix" => from_url_inner_path(url, lossy),
_ => Err(FromUrlErr::UnsupportedScheme)
_ => Err(FromUrlErr::UnsupportedScheme),
}
}

/// Called when `url.scheme()` is an Internet-like URL.
fn from_url_inner_http_ws(url: url::Url, lossy: bool) -> std::result::Result<Multiaddr, FromUrlErr> {
fn from_url_inner_http_ws(
url: url::Url,
lossy: bool,
) -> std::result::Result<Multiaddr, FromUrlErr> {
let (protocol, lost_path, default_port) = match url.scheme() {
"ws" => (Protocol::Ws(url.path().to_owned().into()), false, 80),
"wss" => (Protocol::Wss(url.path().to_owned().into()), false, 443),
"http" => (Protocol::Http, true, 80),
"https" => (Protocol::Https, true, 443),
_ => unreachable!("We only call this function for one of the given schemes; qed")
_ => unreachable!("We only call this function for one of the given schemes; qed"),
};

let port = Protocol::Tcp(url.port().unwrap_or(default_port));
Expand All @@ -82,12 +85,13 @@ fn from_url_inner_http_ws(url: url::Url, lossy: bool) -> std::result::Result<Mul
return Err(FromUrlErr::BadUrl);
};

if !lossy && (
!url.username().is_empty() ||
url.password().is_some() ||
(lost_path && url.path() != "/" && !url.path().is_empty()) ||
url.query().is_some() || url.fragment().is_some()
) {
if !lossy
&& (!url.username().is_empty()
|| url.password().is_some()
|| (lost_path && url.path() != "/" && !url.path().is_empty())
|| url.query().is_some()
|| url.fragment().is_some())
{
return Err(FromUrlErr::InformationLoss);
}

Expand All @@ -101,15 +105,15 @@ fn from_url_inner_http_ws(url: url::Url, lossy: bool) -> std::result::Result<Mul
fn from_url_inner_path(url: url::Url, lossy: bool) -> std::result::Result<Multiaddr, FromUrlErr> {
let protocol = match url.scheme() {
"unix" => Protocol::Unix(url.path().to_owned().into()),
_ => unreachable!("We only call this function for one of the given schemes; qed")
_ => unreachable!("We only call this function for one of the given schemes; qed"),
};

if !lossy && (
!url.username().is_empty() ||
url.password().is_some() ||
url.query().is_some() ||
url.fragment().is_some()
) {
if !lossy
&& (!url.username().is_empty()
|| url.password().is_some()
|| url.query().is_some()
|| url.fragment().is_some())
{
return Err(FromUrlErr::InformationLoss);
}

Expand Down Expand Up @@ -137,16 +141,15 @@ impl fmt::Display for FromUrlErr {
}
}

impl error::Error for FromUrlErr {
}
impl error::Error for FromUrlErr {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_garbage_doesnt_panic() {
for _ in 0 .. 50 {
for _ in 0..50 {
let url = (0..16).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
let url = String::from_utf8_lossy(&url);
assert!(from_url(&url).is_err());
Expand Down Expand Up @@ -223,7 +226,7 @@ mod tests {
fn wrong_scheme() {
match from_url("foo://127.0.0.1") {
Err(FromUrlErr::UnsupportedScheme) => {}
_ => panic!()
_ => panic!(),
}
}

Expand Down Expand Up @@ -271,13 +274,23 @@ mod tests {
#[test]
fn ws_path() {
let addr = from_url("ws://1.2.3.4:1000/foo/bar").unwrap();
assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/x-parity-ws/%2ffoo%2fbar".parse().unwrap());
assert_eq!(
addr,
"/ip4/1.2.3.4/tcp/1000/x-parity-ws/%2ffoo%2fbar"
.parse()
.unwrap()
);

let addr = from_url("ws://1.2.3.4:1000/").unwrap();
assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/ws".parse().unwrap());

let addr = from_url("wss://1.2.3.4:1000/foo/bar").unwrap();
assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/x-parity-wss/%2ffoo%2fbar".parse().unwrap());
assert_eq!(
addr,
"/ip4/1.2.3.4/tcp/1000/x-parity-wss/%2ffoo%2fbar"
.parse()
.unwrap()
);

let addr = from_url("wss://1.2.3.4:1000").unwrap();
assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/wss".parse().unwrap());
Expand Down
Loading