Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Rename the disasterously named to_vec to encode.
Also rename `as_slice_then` to `with_encoded`.
  • Loading branch information
gavofyork committed Feb 8, 2018
commit 9a5fa8a7a758851be8f0c1a58c80b0b7922bff9c
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cd substrate/executor/wasm && ./build.sh && cd ../../..
cd substrate/test-runtime/wasm && ./build.sh && cd ../../..
cd polkadot/runtime/wasm && ./build.sh && cd ../../..
2 changes: 1 addition & 1 deletion polkadot/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
storage = genesis_config.genesis_map();
let block = genesis::construct_genesis_block(&storage);
storage.extend(additional_storage_with_genesis(&block));
(primitives::block::Header::decode(&mut block.header.to_vec().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect())
(primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect())
};
let client = client::new_in_mem(executor, prepare_genesis)?;

Expand Down
10 changes: 5 additions & 5 deletions polkadot/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {
function: Function::StakingTransfer(two(), 69),
};
let signature = secret_for(&transaction.signed).unwrap()
.sign(&transaction.to_vec());
.sign(&transaction.encode());

UncheckedTransaction { transaction, signature }
}
Expand Down Expand Up @@ -156,7 +156,7 @@ mod tests {
let three = [3u8; 32];

TestExternalities { storage: map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].to_vec(),
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].encode(),
twox_128(b"gov:apr").to_vec() => vec![].and(&667u32),
twox_128(b"ses:len").to_vec() => vec![].and(&2u64),
twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32),
Expand Down Expand Up @@ -187,12 +187,12 @@ mod tests {

let transactions = txs.into_iter().map(|transaction| {
let signature = secret_for(&transaction.signed).unwrap()
.sign(&transaction.to_vec());
.sign(&transaction.encode());

UncheckedTransaction { transaction, signature }
}).collect::<Vec<_>>();

let transaction_root = ordered_trie_root(transactions.iter().map(Slicable::to_vec)).0.into();
let transaction_root = ordered_trie_root(transactions.iter().map(Slicable::encode)).0.into();

let header = Header {
parent_hash,
Expand All @@ -203,7 +203,7 @@ mod tests {
};
let hash = header.blake2_256();

(Block { header, transactions }.to_vec(), hash.into())
(Block { header, transactions }.encode(), hash.into())
}

fn block1() -> (Vec<u8>, Hash) {
Expand Down
28 changes: 14 additions & 14 deletions polkadot/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl Slicable for Log {
Vec::<u8>::decode(input).map(Log)
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.as_slice_then(f)
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

Expand All @@ -62,8 +62,8 @@ impl Slicable for Digest {
Vec::<Log>::decode(input).map(|logs| Digest { logs })
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.logs.as_slice_then(f)
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.logs.using_encoded(f)
}
}

Expand All @@ -86,11 +86,11 @@ impl Slicable for Block {
Some(Block { header, transactions })
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

v.extend(self.header.to_vec());
v.extend(self.transactions.to_vec());
v.extend(self.header.encode());
v.extend(self.transactions.encode());

v
}
Expand Down Expand Up @@ -140,14 +140,14 @@ impl Slicable for Header {
})
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

self.parent_hash.as_slice_then(|s| v.extend(s));
self.number.as_slice_then(|s| v.extend(s));
self.state_root.as_slice_then(|s| v.extend(s));
self.transaction_root.as_slice_then(|s| v.extend(s));
self.digest.as_slice_then(|s| v.extend(s));
self.parent_hash.using_encoded(|s| v.extend(s));
self.number.using_encoded(|s| v.extend(s));
self.state_root.using_encoded(|s| v.extend(s));
self.transaction_root.using_encoded(|s| v.extend(s));
self.digest.using_encoded(|s| v.extend(s));

v
}
Expand Down Expand Up @@ -181,7 +181,7 @@ mod tests {
}
}"#);

let v = header.to_vec();
let v = header.encode();
assert_eq!(Header::decode(&mut &v[..]).unwrap(), header);
}
}
30 changes: 15 additions & 15 deletions polkadot/primitives/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Slicable for Id {
u32::decode(input).map(Id)
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.as_slice_then(f)
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

Expand All @@ -66,21 +66,21 @@ impl Slicable for Chain {
}
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
match *self {
Chain::Relay => { 0u8.as_slice_then(|s| v.extend(s)); }
Chain::Relay => { 0u8.using_encoded(|s| v.extend(s)); }
Chain::Parachain(id) => {
1u8.as_slice_then(|s| v.extend(s));
id.as_slice_then(|s| v.extend(s));
1u8.using_encoded(|s| v.extend(s));
id.using_encoded(|s| v.extend(s));
}
}

v
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.to_vec().as_slice())
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode().as_slice())
}
}

Expand All @@ -105,17 +105,17 @@ impl Slicable for DutyRoster {
})
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

v.extend(self.validator_duty.to_vec());
v.extend(self.guarantor_duty.to_vec());
v.extend(self.validator_duty.encode());
v.extend(self.guarantor_duty.encode());

v
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.to_vec().as_slice())
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode().as_slice())
}
}

Expand Down Expand Up @@ -204,8 +204,8 @@ impl Slicable for Activity {
Vec::<u8>::decode(input).map(Activity)
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.as_slice_then(f)
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

Expand Down
84 changes: 42 additions & 42 deletions polkadot/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,38 +113,38 @@ impl Slicable for Proposal {
Some(function)
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
match *self {
Proposal::SystemSetCode(ref data) => {
(InternalFunctionId::SystemSetCode as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::SystemSetCode as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Proposal::SessionSetLength(ref data) => {
(InternalFunctionId::SessionSetLength as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::SessionSetLength as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Proposal::SessionForceNewSession => {
(InternalFunctionId::SessionForceNewSession as u8).as_slice_then(|s| v.extend(s));
(InternalFunctionId::SessionForceNewSession as u8).using_encoded(|s| v.extend(s));
}
Proposal::StakingSetSessionsPerEra(ref data) => {
(InternalFunctionId::StakingSetSessionsPerEra as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::StakingSetSessionsPerEra as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Proposal::StakingSetBondingDuration(ref data) => {
(InternalFunctionId::StakingSetBondingDuration as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::StakingSetBondingDuration as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Proposal::StakingSetValidatorCount(ref data) => {
(InternalFunctionId::StakingSetValidatorCount as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::StakingSetValidatorCount as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Proposal::StakingForceNewEra => {
(InternalFunctionId::StakingForceNewEra as u8).as_slice_then(|s| v.extend(s));
(InternalFunctionId::StakingForceNewEra as u8).using_encoded(|s| v.extend(s));
}
Proposal::GovernanceSetApprovalPpmRequired(ref data) => {
(InternalFunctionId::GovernanceSetApprovalPpmRequired as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(InternalFunctionId::GovernanceSetApprovalPpmRequired as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
}

Expand Down Expand Up @@ -228,43 +228,43 @@ impl Slicable for Function {
})
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
match *self {
Function::TimestampSet(ref data) => {
(FunctionId::TimestampSet as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(FunctionId::TimestampSet as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Function::SessionSetKey(ref data) => {
(FunctionId::SessionSetKey as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(FunctionId::SessionSetKey as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Function::StakingStake => {
(FunctionId::StakingStake as u8).as_slice_then(|s| v.extend(s));
(FunctionId::StakingStake as u8).using_encoded(|s| v.extend(s));
}
Function::StakingUnstake => {
(FunctionId::StakingUnstake as u8).as_slice_then(|s| v.extend(s));
(FunctionId::StakingUnstake as u8).using_encoded(|s| v.extend(s));
}
Function::StakingTransfer(ref to, ref amount) => {
(FunctionId::StakingTransfer as u8).as_slice_then(|s| v.extend(s));
to.as_slice_then(|s| v.extend(s));
amount.as_slice_then(|s| v.extend(s));
(FunctionId::StakingTransfer as u8).using_encoded(|s| v.extend(s));
to.using_encoded(|s| v.extend(s));
amount.using_encoded(|s| v.extend(s));
}
Function::GovernancePropose(ref data) => {
(FunctionId::GovernancePropose as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(FunctionId::GovernancePropose as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
Function::GovernanceApprove(ref data) => {
(FunctionId::GovernanceApprove as u8).as_slice_then(|s| v.extend(s));
data.as_slice_then(|s| v.extend(s));
(FunctionId::GovernanceApprove as u8).using_encoded(|s| v.extend(s));
data.using_encoded(|s| v.extend(s));
}
}

v
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(self.to_vec().as_slice())
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(self.encode().as_slice())
}
}

Expand All @@ -289,12 +289,12 @@ impl Slicable for Transaction {
})
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

self.signed.as_slice_then(|s| v.extend(s));
self.nonce.as_slice_then(|s| v.extend(s));
self.function.as_slice_then(|s| v.extend(s));
self.signed.using_encoded(|s| v.extend(s));
self.nonce.using_encoded(|s| v.extend(s));
self.function.using_encoded(|s| v.extend(s));

v
}
Expand Down Expand Up @@ -326,20 +326,20 @@ impl Slicable for UncheckedTransaction {
})
}

fn to_vec(&self) -> Vec<u8> {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();

// need to prefix with the total length as u32 to ensure it's binary comptible with
// Vec<u8>. we'll make room for it here, then overwrite once we know the length.
v.extend(&[0u8; 4]);

self.transaction.signed.as_slice_then(|s| v.extend(s));
self.transaction.nonce.as_slice_then(|s| v.extend(s));
self.transaction.function.as_slice_then(|s| v.extend(s));
self.signature.as_slice_then(|s| v.extend(s));
self.transaction.signed.using_encoded(|s| v.extend(s));
self.transaction.nonce.using_encoded(|s| v.extend(s));
self.transaction.function.using_encoded(|s| v.extend(s));
self.signature.using_encoded(|s| v.extend(s));

let length = (v.len() - 4) as u32;
length.as_slice_then(|s| v[0..4].copy_from_slice(s));
length.using_encoded(|s| v[0..4].copy_from_slice(s));

v
}
Expand Down Expand Up @@ -384,7 +384,7 @@ mod tests {
// df0f0200
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

let v = Slicable::to_vec(&tx);
let v = Slicable::encode(&tx);
println!("{}", HexDisplay::from(&v));
assert_eq!(UncheckedTransaction::decode(&mut &v[..]).unwrap(), tx);
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ impl GenesisConfig {
pub fn additional_storage_with_genesis(genesis_block: &Block) -> HashMap<Vec<u8>, Vec<u8>> {
use codec::Slicable;
map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => genesis_block.header.blake2_256().to_vec()
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => genesis_block.header.blake2_256().encode()
]
}
2 changes: 1 addition & 1 deletion polkadot/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod transaction {
///
/// On failure, return the transaction back.
pub fn check(tx: UncheckedTransaction) -> Result<CheckedTransaction, UncheckedTransaction> {
let msg = ::codec::Slicable::to_vec(&tx.transaction);
let msg = ::codec::Slicable::encode(&tx.transaction);
if ::runtime_io::ed25519_verify(&tx.signature.0, &msg, &tx.transaction.signed) {
Ok(CheckedTransaction(tx))
} else {
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/src/runtime/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn initial_checks(block: &Block) {
);

// check transaction trie root represents the transactions.
let txs = block.transactions.iter().map(Slicable::to_vec).collect::<Vec<_>>();
let txs = block.transactions.iter().map(Slicable::encode).collect::<Vec<_>>();
let txs = txs.iter().map(Vec::as_slice).collect::<Vec<_>>();
let txs_root = enumerated_trie_root(&txs).into();
info_expect_equal_hash(&header.transaction_root, &txs_root);
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
let three = [3u8; 32];

TestExternalities { storage: map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].to_vec(),
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].encode(),
twox_128(b"gov:apr").to_vec() => vec![].and(&667u32),
twox_128(b"ses:len").to_vec() => vec![].and(&2u64),
twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32),
Expand Down
Binary file not shown.
Binary file not shown.
Loading