mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 19:11:02 +00:00
Re-introduce zero copy codec and add minimal polkadot client API which uses linked native runtime (#65)
* client-api type and move duty roster types to primitives * tuple implementation for slicable * mild cleanup of deserialization code * stubs which handle encoding and decoding themselves * fancier impl_stubs macro * zero-copy slicable API * minimal polkadot-client API * fix WASM API generation * move native environment stuff to substrate executor * fix warnings and grumbles
This commit is contained in:
committed by
Gav Wood
parent
f2b3bab61e
commit
a00d0e75fd
@@ -20,7 +20,7 @@
|
||||
use primitives::bytes;
|
||||
use primitives::H256;
|
||||
use rstd::vec::Vec;
|
||||
use codec::Slicable;
|
||||
use codec::{Input, Slicable};
|
||||
use transaction::UncheckedTransaction;
|
||||
|
||||
/// Used to refer to a block number.
|
||||
@@ -38,8 +38,8 @@ pub type TransactionHash = H256;
|
||||
pub struct Log(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
|
||||
impl Slicable for Log {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<u8>::from_slice(value).map(Log)
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Vec::<u8>::decode(input).map(Log)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
@@ -58,8 +58,8 @@ pub struct Digest {
|
||||
}
|
||||
|
||||
impl Slicable for Digest {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<Log>::from_slice(value).map(|logs| Digest { logs })
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Vec::<Log>::decode(input).map(|logs| Digest { logs })
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
@@ -81,11 +81,9 @@ pub struct Block {
|
||||
}
|
||||
|
||||
impl Slicable for Block {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Some(Block {
|
||||
header: try_opt!(Slicable::from_slice(value)),
|
||||
transactions: try_opt!(Slicable::from_slice(value)),
|
||||
})
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
let (header, transactions) = try_opt!(Slicable::decode(input));
|
||||
Some(Block { header, transactions })
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
@@ -136,13 +134,13 @@ impl Header {
|
||||
}
|
||||
|
||||
impl Slicable for Header {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Some(Header {
|
||||
parent_hash: try_opt!(Slicable::from_slice(value)),
|
||||
number: try_opt!(Slicable::from_slice(value)),
|
||||
state_root: try_opt!(Slicable::from_slice(value)),
|
||||
transaction_root: try_opt!(Slicable::from_slice(value)),
|
||||
digest: try_opt!(Slicable::from_slice(value)),
|
||||
parent_hash: try_opt!(Slicable::decode(input)),
|
||||
number: try_opt!(Slicable::decode(input)),
|
||||
state_root: try_opt!(Slicable::decode(input)),
|
||||
transaction_root: try_opt!(Slicable::decode(input)),
|
||||
digest: try_opt!(Slicable::decode(input)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -192,6 +190,6 @@ mod tests {
|
||||
}"#);
|
||||
|
||||
let v = header.to_vec();
|
||||
assert_eq!(Header::from_slice(&mut &v[..]).unwrap(), header);
|
||||
assert_eq!(Header::decode(&mut &v[..]).unwrap(), header);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,12 @@ extern crate serde_derive;
|
||||
extern crate serde;
|
||||
|
||||
extern crate substrate_runtime_std as rstd;
|
||||
extern crate substrate_codec as codec;
|
||||
extern crate substrate_primitives as primitives;
|
||||
#[cfg(test)]
|
||||
extern crate substrate_serializer;
|
||||
|
||||
extern crate substrate_codec as codec;
|
||||
|
||||
macro_rules! try_opt {
|
||||
($e: expr) => {
|
||||
match $e {
|
||||
|
||||
@@ -19,24 +19,25 @@
|
||||
#[cfg(feature = "std")]
|
||||
use primitives::bytes;
|
||||
use primitives;
|
||||
use codec::{Input, Slicable, NonTrivialSlicable};
|
||||
use rstd::vec::Vec;
|
||||
|
||||
/// Unique identifier of a parachain.
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Id(u64);
|
||||
pub struct Id(u32);
|
||||
|
||||
impl From<Id> for u64 {
|
||||
impl From<Id> for u32 {
|
||||
fn from(x: Id) -> Self { x.0 }
|
||||
}
|
||||
|
||||
impl From<u64> for Id {
|
||||
fn from(x: u64) -> Self { Id(x) }
|
||||
impl From<u32> for Id {
|
||||
fn from(x: u32) -> Self { Id(x) }
|
||||
}
|
||||
|
||||
impl ::codec::Slicable for Id {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
u64::from_slice(value).map(Id)
|
||||
impl Slicable for Id {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
u32::decode(input).map(Id)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
@@ -44,6 +45,80 @@ impl ::codec::Slicable for Id {
|
||||
}
|
||||
}
|
||||
|
||||
/// Identifier for a chain, either one of a number of parachains or the relay chain.
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub enum Chain {
|
||||
/// The relay chain.
|
||||
Relay,
|
||||
/// A parachain of the given index.
|
||||
Parachain(Id),
|
||||
}
|
||||
|
||||
impl Slicable for Chain {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
let disc = try_opt!(u8::decode(input));
|
||||
|
||||
match disc {
|
||||
0 => Some(Chain::Relay),
|
||||
1 => Some(Chain::Parachain(try_opt!(Slicable::decode(input)))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
match *self {
|
||||
Chain::Relay => { 0u8.as_slice_then(|s| v.extend(s)); }
|
||||
Chain::Parachain(id) => {
|
||||
1u8.as_slice_then(|s| v.extend(s));
|
||||
id.as_slice_then(|s| v.extend(s));
|
||||
}
|
||||
}
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec().as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
impl NonTrivialSlicable for Chain { }
|
||||
|
||||
/// The duty roster specifying what jobs each validator must do.
|
||||
#[derive(Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "std", derive(Default, Debug))]
|
||||
pub struct DutyRoster {
|
||||
/// Lookup from validator index to chain on which that validator has a duty to validate.
|
||||
pub validator_duty: Vec<Chain>,
|
||||
/// Lookup from validator index to chain on which that validator has a duty to guarantee
|
||||
/// availability.
|
||||
pub guarantor_duty: Vec<Chain>,
|
||||
}
|
||||
|
||||
impl Slicable for DutyRoster {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Some(DutyRoster {
|
||||
validator_duty: try_opt!(Slicable::decode(input)),
|
||||
guarantor_duty: try_opt!(Slicable::decode(input)),
|
||||
})
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
|
||||
v.extend(self.validator_duty.to_vec());
|
||||
v.extend(self.guarantor_duty.to_vec());
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
f(&self.to_vec().as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
/// Candidate parachain block.
|
||||
///
|
||||
/// https://github.com/w3f/polkadot-spec/blob/master/spec.md#candidate-para-chain-block
|
||||
@@ -124,9 +199,9 @@ pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with="bytes"))] pub
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct Activity(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
|
||||
|
||||
impl ::codec::Slicable for Activity {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
Vec::<u8>::from_slice(value).map(Activity)
|
||||
impl Slicable for Activity {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Vec::<u8>::decode(input).map(Activity)
|
||||
}
|
||||
|
||||
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//! Transaction type.
|
||||
|
||||
use rstd::vec::Vec;
|
||||
use codec::Slicable;
|
||||
use codec::{Input, Slicable};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::fmt;
|
||||
@@ -91,23 +91,23 @@ pub enum Proposal {
|
||||
}
|
||||
|
||||
impl Slicable for Proposal {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
let id = try_opt!(u8::from_slice(value).and_then(InternalFunctionId::from_u8));
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
let id = try_opt!(u8::decode(input).and_then(InternalFunctionId::from_u8));
|
||||
let function = match id {
|
||||
InternalFunctionId::SystemSetCode =>
|
||||
Proposal::SystemSetCode(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::SystemSetCode(try_opt!(Slicable::decode(input))),
|
||||
InternalFunctionId::SessionSetLength =>
|
||||
Proposal::SessionSetLength(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::SessionSetLength(try_opt!(Slicable::decode(input))),
|
||||
InternalFunctionId::SessionForceNewSession => Proposal::SessionForceNewSession,
|
||||
InternalFunctionId::StakingSetSessionsPerEra =>
|
||||
Proposal::StakingSetSessionsPerEra(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::StakingSetSessionsPerEra(try_opt!(Slicable::decode(input))),
|
||||
InternalFunctionId::StakingSetBondingDuration =>
|
||||
Proposal::StakingSetBondingDuration(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::StakingSetBondingDuration(try_opt!(Slicable::decode(input))),
|
||||
InternalFunctionId::StakingSetValidatorCount =>
|
||||
Proposal::StakingSetValidatorCount(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::StakingSetValidatorCount(try_opt!(Slicable::decode(input))),
|
||||
InternalFunctionId::StakingForceNewEra => Proposal::StakingForceNewEra,
|
||||
InternalFunctionId::GovernanceSetApprovalPpmRequired =>
|
||||
Proposal::GovernanceSetApprovalPpmRequired(try_opt!(Slicable::from_slice(value))),
|
||||
Proposal::GovernanceSetApprovalPpmRequired(try_opt!(Slicable::decode(input))),
|
||||
};
|
||||
|
||||
Some(function)
|
||||
@@ -210,25 +210,25 @@ pub enum Function {
|
||||
}
|
||||
|
||||
impl Slicable for Function {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
let id = try_opt!(u8::from_slice(value).and_then(FunctionId::from_u8));
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
let id = try_opt!(u8::decode(input).and_then(FunctionId::from_u8));
|
||||
Some(match id {
|
||||
FunctionId::TimestampSet =>
|
||||
Function::TimestampSet(try_opt!(Slicable::from_slice(value))),
|
||||
Function::TimestampSet(try_opt!(Slicable::decode(input))),
|
||||
FunctionId::SessionSetKey =>
|
||||
Function::SessionSetKey(try_opt!(Slicable::from_slice(value))),
|
||||
Function::SessionSetKey(try_opt!(Slicable::decode(input))),
|
||||
FunctionId::StakingStake => Function::StakingStake,
|
||||
FunctionId::StakingUnstake => Function::StakingUnstake,
|
||||
FunctionId::StakingTransfer => {
|
||||
let to = try_opt!(Slicable::from_slice(value));
|
||||
let amount = try_opt!(Slicable::from_slice(value));
|
||||
let to = try_opt!(Slicable::decode(input));
|
||||
let amount = try_opt!(Slicable::decode(input));
|
||||
|
||||
Function::StakingTransfer(to, amount)
|
||||
}
|
||||
FunctionId::GovernancePropose =>
|
||||
Function::GovernancePropose(try_opt!(Slicable::from_slice(value))),
|
||||
Function::GovernancePropose(try_opt!(Slicable::decode(input))),
|
||||
FunctionId::GovernanceApprove =>
|
||||
Function::GovernanceApprove(try_opt!(Slicable::from_slice(value))),
|
||||
Function::GovernanceApprove(try_opt!(Slicable::decode(input))),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -285,11 +285,11 @@ pub struct Transaction {
|
||||
}
|
||||
|
||||
impl Slicable for Transaction {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Some(Transaction {
|
||||
signed: try_opt!(Slicable::from_slice(value)),
|
||||
nonce: try_opt!(Slicable::from_slice(value)),
|
||||
function: try_opt!(Slicable::from_slice(value)),
|
||||
signed: try_opt!(Slicable::decode(input)),
|
||||
nonce: try_opt!(Slicable::decode(input)),
|
||||
function: try_opt!(Slicable::decode(input)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -321,16 +321,16 @@ pub struct UncheckedTransaction {
|
||||
}
|
||||
|
||||
impl Slicable for UncheckedTransaction {
|
||||
fn from_slice(value: &mut &[u8]) -> Option<Self> {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
// This is a little more complicated than usua since the binary format must be compatible
|
||||
// with substrate's generic `Vec<u8>` type. Basically this just means accepting that there
|
||||
// will be a prefix of u32, which has the total number of bytes following (we don't need
|
||||
// to use this).
|
||||
let _length_do_not_remove_me_see_above: u32 = try_opt!(Slicable::from_slice(value));
|
||||
let _length_do_not_remove_me_see_above: u32 = try_opt!(Slicable::decode(input));
|
||||
|
||||
Some(UncheckedTransaction {
|
||||
transaction: try_opt!(Slicable::from_slice(value)),
|
||||
signature: try_opt!(Slicable::from_slice(value)),
|
||||
transaction: try_opt!(Slicable::decode(input)),
|
||||
signature: try_opt!(Slicable::decode(input)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -398,6 +398,6 @@ mod tests {
|
||||
|
||||
let v = Slicable::to_vec(&tx);
|
||||
println!("{}", HexDisplay::from(&v));
|
||||
assert_eq!(UncheckedTransaction::from_slice(&mut &v[..]).unwrap(), tx);
|
||||
assert_eq!(UncheckedTransaction::decode(&mut &v[..]).unwrap(), tx);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user