Skip to content

Commit

Permalink
impl HasCompact for Perbill and Permill (paritytech#1411)
Browse files Browse the repository at this point in the history
* impl Compact<> and HasCompact for Permill Perbill

* update parity-codec to 2.2

* add Cargo.lock

* add lock and build for runtime

* rebuild Cargo.lock after rebase
  • Loading branch information
gui1117 authored and MTDK1 committed Apr 12, 2019
1 parent 6302186 commit ae4f49a
Show file tree
Hide file tree
Showing 53 changed files with 228 additions and 157 deletions.
108 changes: 54 additions & 54 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/basic-authorship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
log = "0.4"
parity-codec = "2.1"
parity-codec = "2.2"
sr-primitives = { path = "../../core/sr-primitives" }
substrate-client = { path = "../../core/client" }
substrate-consensus-aura-primitives = { path = "../../core/consensus/aura/primitives" }
Expand Down
2 changes: 1 addition & 1 deletion core/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ substrate-primitives = { path = "../../primitives" }
sr-primitives = { path = "../../sr-primitives" }
substrate-client = { path = "../../client" }
substrate-state-machine = { path = "../../state-machine" }
parity-codec = "2.1"
parity-codec = "2.2"
parity-codec-derive = "2.1"
substrate-executor = { path = "../../executor" }
substrate-state-db = { path = "../../state-db" }
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
description = "Aura consensus algorithm for substrate"

[dependencies]
parity-codec = "2.1"
parity-codec = "2.2"
substrate-client = { path = "../../client" }
substrate-primitives = { path = "../../primitives" }
srml-support = { path = "../../../srml/support" }
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/aura/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
description = "Primitives for Aura consensus"

[dependencies]
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
substrate-client = { path = "../../../client", default-features = false }
substrate-primitives = { path = "../../../primitives", default-features = false }
srml-support = { path = "../../../../srml/support", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ futures = "0.1"
sr-version = { path = "../../sr-version" }
sr-primitives = { path = "../../sr-primitives" }
tokio = "0.1.7"
parity-codec = "2.1"
parity-codec = "2.2"
parity-codec-derive = "2.0"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/rhd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Rhododendron Round-Based consensus-algorithm for substrate"

[dependencies]
futures = "0.1.17"
parity-codec = { version = "2.1" }
parity-codec = { version = "2.2" }
parity-codec-derive = { version = "2.0" }
substrate-primitives = { path = "../../primitives" }
substrate-consensus-common = { path = "../common" }
Expand Down
2 changes: 1 addition & 1 deletion core/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
error-chain = "0.12"
parity-codec = "2.1"
parity-codec = "2.2"
sr-io = { path = "../sr-io" }
substrate-primitives = { path = "../primitives" }
substrate-trie = { path = "../trie" }
Expand Down
12 changes: 6 additions & 6 deletions core/executor/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
futures = "0.1"
parity-codec = "2.1"
parity-codec = "2.2"
parity-codec-derive = "2.0"
sr-primitives = { path = "../sr-primitives" }
substrate-consensus-common = { path = "../consensus/common" }
Expand Down
2 changes: 1 addition & 1 deletion core/finality-grandpa/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
substrate-client = { path = "../../client", default-features = false }
substrate-primitives = { path = "../../primitives", default-features = false }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
sr-primitives = { path = "../../sr-primitives", default-features = false }
sr-std = { path = "../../sr-std", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ substrate-primitives = { path = "../../core/primitives" }
substrate-consensus-common = { path = "../../core/consensus/common" }
substrate-client = { path = "../../core/client" }
sr-primitives = { path = "../../core/sr-primitives" }
parity-codec = "2.1"
parity-codec = "2.2"
parity-codec-derive = "2.1"
substrate-network-libp2p = { path = "../../core/network-libp2p" }
tokio = "0.1.11"
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
sr-std = { path = "../sr-std", default-features = false }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
rustc-hex = { version = "2.0", default-features = false }
serde = { version = "1.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jsonrpc-macros = { git="https://github.com/paritytech/jsonrpc.git" }
jsonrpc-pubsub = { git="https://github.com/paritytech/jsonrpc.git" }
log = "0.4"
parking_lot = "0.7.1"
parity-codec = "2.1"
parity-codec = "2.2"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion core/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ substrate-consensus-common = { path = "../../core/consensus/common" }
substrate-network = { path = "../../core/network" }
substrate-client = { path = "../../core/client" }
substrate-client-db = { path = "../../core/client/db" }
parity-codec = "2.1"
parity-codec = "2.2"
substrate-executor = { path = "../../core/executor" }
substrate-transaction-pool = { path = "../../core/transaction-pool" }
substrate-rpc-servers = { path = "../../core/rpc-servers" }
Expand Down
2 changes: 1 addition & 1 deletion core/sr-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rustc_version = "0.2"
[dependencies]
sr-std = { path = "../sr-std", default-features = false }
substrate-primitives = { path = "../primitives", default-features = false }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
hash-db = { git = "https://github.com/paritytech/trie", default-features = false }

environmental = { version = "~1.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/sr-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ num-traits = { version = "0.2", default-features = false }
integer-sqrt = { version = "0.1.2" }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
substrate-primitives = { path = "../primitives", default-features = false }
sr-std = { path = "../sr-std", default-features = false }
Expand Down
71 changes: 71 additions & 0 deletions core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ impl From<f32> for Permill {
}
}

impl codec::CompactAs for Permill {
type As = u32;
fn encode_as(&self) -> &u32 {
&self.0
}
fn decode_from(x: u32) -> Permill {
Permill(x)
}
}

impl From<codec::Compact<Permill>> for Permill {
fn from(x: codec::Compact<Permill>) -> Permill {
x.0
}
}

/// Perbill is parts-per-billion. It stores a value between 0 and 1 in fixed point and
/// provides a means to multiply some other value by that.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand Down Expand Up @@ -200,6 +216,22 @@ impl From<f32> for Perbill {
}
}

impl codec::CompactAs for Perbill {
type As = u32;
fn encode_as(&self) -> &u32 {
&self.0
}
fn decode_from(x: u32) -> Perbill {
Perbill(x)
}
}

impl From<codec::Compact<Perbill>> for Perbill {
fn from(x: codec::Compact<Perbill>) -> Perbill {
x.0
}
}

/// Ed25519 signature verify.
#[derive(Eq, PartialEq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
Expand Down Expand Up @@ -623,4 +655,43 @@ mod tests {
let ex = super::OpaqueExtrinsic(vec![1, 2, 3, 4]);
assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x1001020304\"".to_owned());
}

#[test]
fn compact_permill_perbill_encoding() {
let tests = [(0u32, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (1073741823, 4), (1073741824, 5), (u32::max_value(), 5)];
for &(n, l) in &tests {
let compact: codec::Compact<super::Permill> = super::Permill(n).into();
let encoded = compact.encode();
assert_eq!(encoded.len(), l);
let decoded = <codec::Compact<super::Permill>>::decode(&mut & encoded[..]).unwrap();
let permill: super::Permill = decoded.into();
assert_eq!(permill, super::Permill(n));

let compact: codec::Compact<super::Perbill> = super::Perbill(n).into();
let encoded = compact.encode();
assert_eq!(encoded.len(), l);
let decoded = <codec::Compact<super::Perbill>>::decode(&mut & encoded[..]).unwrap();
let perbill: super::Perbill = decoded.into();
assert_eq!(perbill, super::Perbill(n));
}
}

#[derive(Encode, Decode, PartialEq, Eq, Debug)]
struct WithCompact<T: codec::HasCompact> {
data: T,
}

#[test]
fn test_has_compact_permill() {
let data = WithCompact { data: super::Permill(1) };
let encoded = data.encode();
assert_eq!(data, WithCompact::<super::Permill>::decode(&mut &encoded[..]).unwrap());
}

#[test]
fn test_has_compact_perbill() {
let data = WithCompact { data: super::Perbill(1) };
let encoded = data.encode();
assert_eq!(data, WithCompact::<super::Perbill>::decode(&mut &encoded[..]).unwrap());
}
}
2 changes: 1 addition & 1 deletion core/sr-sandbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rustc_version = "0.2"
wasmi = { version = "0.4.3", optional = true }
substrate-primitives = { path = "../primitives", default-features = false }
sr-std = { path = "../sr-std", default-features = false }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }

[dev-dependencies]
wabt = "~0.7.4"
Expand Down
2 changes: 1 addition & 1 deletion core/sr-version/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
serde = { version = "1.0", default-features = false }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
sr-std = { path = "../sr-std", default-features = false }
sr-primitives = { path = "../sr-primitives", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/state-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
parking_lot = "0.7.1"
log = "0.4"
substrate-primitives = { path = "../../core/primitives" }
parity-codec = "2.1"
parity-codec = "2.2"
parity-codec-derive = "2.1"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion core/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ trie-db = { git = "https://github.com/paritytech/trie" }
trie-root = { git = "https://github.com/paritytech/trie" }
substrate-trie = { path = "../trie" }
substrate-primitives = { path = "../primitives" }
parity-codec = "2.1"
parity-codec = "2.2"
2 changes: 1 addition & 1 deletion core/test-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
substrate-client = { path = "../client" }
parity-codec = "2.1"
parity-codec = "2.2"
substrate-executor = { path = "../executor" }
substrate-consensus-common = { path = "../consensus/common" }
substrate-keyring = { path = "../../core/keyring" }
Expand Down
2 changes: 1 addition & 1 deletion core/test-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ log = { version = "0.4", optional = true }
hex-literal = { version = "0.1.0", optional = true }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "2.1", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
substrate-keyring = { path = "../keyring", optional = true }
substrate-client = { path = "../client", default-features = false }
Expand Down
Loading

0 comments on commit ae4f49a

Please sign in to comment.