Unverified Commit 72a6ffde authored by Gav Wood's avatar Gav Wood Committed by GitHub
Browse files

Claim yer sale DOTs (#97)

* Add claims.

* Failing build

* Updatee to latest substrate, fix tests

* Remove unneeded

* Introduce tests with real work sig

* Use right 64 bytes of pubkey to get eth addr

* Fix for eth sig

* Fix build

* Fix wasm
parent a9767290
Pipeline #29148 passed with stages
in 14 minutes and 21 seconds
This diff is collapsed.
...@@ -102,7 +102,7 @@ pub fn run<I, T, W>(args: I, worker: W, version: cli::VersionInfo) -> error::Res ...@@ -102,7 +102,7 @@ pub fn run<I, T, W>(args: I, worker: W, version: cli::VersionInfo) -> error::Res
let (spec, mut config) = cli::parse_matches::<service::Factory, _>(load_spec, &version, "parity-polkadot", &matches)?; let (spec, mut config) = cli::parse_matches::<service::Factory, _>(load_spec, &version, "parity-polkadot", &matches)?;
match cli::execute_default::<service::Factory, _,>(spec, worker, &matches, &config, &version)? { match cli::execute_default::<service::Factory, _>(spec, worker, &matches, &config)? {
cli::Action::ExecutedInternally => (), cli::Action::ExecutedInternally => (),
cli::Action::RunService(worker) => { cli::Action::RunService(worker) => {
info!("Parity ·:· Polkadot"); info!("Parity ·:· Polkadot");
......
...@@ -289,7 +289,7 @@ impl<P, E> Worker for CollationNode<P, E> where ...@@ -289,7 +289,7 @@ impl<P, E> Worker for CollationNode<P, E> where
let work = future::lazy(move || { let work = future::lazy(move || {
let api = client.runtime_api(); let api = client.runtime_api();
let last_head = match try_fr!(api.parachain_head(&id, &para_id)) { let last_head = match try_fr!(api.parachain_head(&id, para_id)) {
Some(last_head) => last_head, Some(last_head) => last_head,
None => return future::Either::A(future::ok(())), None => return future::Either::A(future::ok(())),
}; };
......
...@@ -254,10 +254,10 @@ pub fn validate_collation<P>( ...@@ -254,10 +254,10 @@ pub fn validate_collation<P>(
let api = client.runtime_api(); let api = client.runtime_api();
let para_id = collation.receipt.parachain_index; let para_id = collation.receipt.parachain_index;
let validation_code = api.parachain_code(relay_parent, &para_id)? let validation_code = api.parachain_code(relay_parent, para_id)?
.ok_or_else(|| ErrorKind::InactiveParachain(para_id))?; .ok_or_else(|| ErrorKind::InactiveParachain(para_id))?;
let chain_head = api.parachain_head(relay_parent, &para_id)? let chain_head = api.parachain_head(relay_parent, para_id)?
.ok_or_else(|| ErrorKind::InactiveParachain(para_id))?; .ok_or_else(|| ErrorKind::InactiveParachain(para_id))?;
let params = ValidationParams { let params = ValidationParams {
......
...@@ -636,7 +636,7 @@ impl<C, TxApi> CreateProposal<C, TxApi> where ...@@ -636,7 +636,7 @@ impl<C, TxApi> CreateProposal<C, TxApi> where
let mut block_builder = BlockBuilder::at_block(&self.parent_id, &*self.client)?; let mut block_builder = BlockBuilder::at_block(&self.parent_id, &*self.client)?;
{ {
let inherents = runtime_api.inherent_extrinsics(&self.parent_id, &inherent_data)?; let inherents = runtime_api.inherent_extrinsics(&self.parent_id, inherent_data)?;
for inherent in inherents { for inherent in inherents {
block_builder.push(inherent)?; block_builder.push(inherent)?;
} }
......
...@@ -37,6 +37,8 @@ srml-timestamp = { git = "https://github.com/paritytech/substrate" } ...@@ -37,6 +37,8 @@ srml-timestamp = { git = "https://github.com/paritytech/substrate" }
srml-treasury = { git = "https://github.com/paritytech/substrate" } srml-treasury = { git = "https://github.com/paritytech/substrate" }
srml-upgrade-key = { git = "https://github.com/paritytech/substrate" } srml-upgrade-key = { git = "https://github.com/paritytech/substrate" }
sr-version = { git = "https://github.com/paritytech/substrate" } sr-version = { git = "https://github.com/paritytech/substrate" }
libsecp256k1 = "0.2.1"
tiny-keccak = "1.4.2"
[dev-dependencies] [dev-dependencies]
hex-literal = "0.1.0" hex-literal = "0.1.0"
......
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Module to process claims from Ethereum addresses.
use rstd::prelude::*;
use tiny_keccak::keccak256;
use secp256k1;
use srml_support::{StorageValue, StorageMap};
use system::ensure_signed;
use codec::Encode;
#[cfg(feature = "std")]
use sr_primitives::traits::Zero;
use balances;
/// Configuration trait.
pub trait Trait: balances::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
type EthereumAddress = [u8; 20];
type EcdsaSignature = ([u8; 32], [u8; 32], i8);
/// An event in this module.
decl_event!(
pub enum Event<T> where
B = <T as balances::Trait>::Balance,
A = <T as system::Trait>::AccountId
{
/// Someone claimed some DOTs.
Claimed(A, EthereumAddress, B),
}
);
decl_storage! {
// A macro for the Storage trait, and its implementation, for this module.
// This allows for type-safe usage of the Substrate storage database, so you can
// keep things around between blocks.
trait Store for Module<T: Trait> as Claims {
Claims get(claims) build(|config: &GenesisConfig<T>| {
config.claims.iter().map(|(a, b)| (a.clone(), b.clone())).collect::<Vec<_>>()
}): map EthereumAddress => Option<T::Balance>;
Total get(total) build(|config: &GenesisConfig<T>| {
config.claims.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n)
}): T::Balance;
}
add_extra_genesis {
config(claims): Vec<(EthereumAddress, T::Balance)>;
}
}
fn ecdsa_recover(sig: &EcdsaSignature, msg: &[u8; 32]) -> Option<[u8; 64]> {
let v = secp256k1::RecoveryId::parse(if sig.2 > 26 { sig.2 - 27 } else { sig.2 } as u8).ok()?;
let rs = (sig.0, sig.1).using_encoded(secp256k1::Signature::parse_slice).ok()?;
let pubkey = secp256k1::recover(&secp256k1::Message::parse(msg), &rs, &v).ok()?;
let mut res = [0u8; 64];
res.copy_from_slice(&pubkey.serialize()[1..65]);
Some(res)
}
fn create_msg(who: &[u8]) -> Vec<u8> {
let prefix = b"Pay DOTs to the Polkadot account:";
let mut l = prefix.len() + who.len();
let mut rev = Vec::new();
while l > 0 {
rev.push(b'0' + (l % 10) as u8);
l /= 10;
}
let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
v.extend(rev.into_iter().rev());
v.extend_from_slice(&prefix[..]);
v.extend_from_slice(who);
v
}
fn eth_recover(s: &EcdsaSignature, who: &[u8]) -> Option<EthereumAddress> {
let msg = keccak256(&create_msg(who));
let mut res = EthereumAddress::default();
res.copy_from_slice(&keccak256(&ecdsa_recover(s, &msg)?[..])[12..]);
Some(res)
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Deposit one of this module's events by using the default implementation.
fn deposit_event<T>() = default;
/// Make a claim.
fn claim(origin, ethereum_signature: EcdsaSignature) {
// This is a public call, so we ensure that the origin is some signed account.
let sender = ensure_signed(origin)?;
let signer = sender.using_encoded(|data|
eth_recover(&ethereum_signature, data)
).ok_or("Invalid Ethereum signature")?;
let balance_due = <Claims<T>>::take(&signer)
.ok_or("Ethereum address has no claim")?;
<Total<T>>::mutate(|t| if *t < balance_due {
panic!("Logic error: Pot less than the total of claims!")
} else {
*t -= balance_due
});
<balances::Module<T>>::increase_free_balance_creating(&sender, balance_due);
// Let's deposit an event to let the outside world know this happened.
Self::deposit_event(RawEvent::Claimed(sender, signer, balance_due));
}
}
}
#[cfg(test)]
mod tests {
use secp256k1;
use tiny_keccak::keccak256;
use super::*;
use sr_io::{self as runtime_io, with_externalities};
use substrate_primitives::{H256, Blake2Hasher, hexdisplay::HexDisplay};
use codec::{Decode, Encode};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sr_primitives::{
BuildStorage, traits::{BlakeTwo256, IdentityLookup}, testing::{Digest, DigestItem, Header}
};
use balances;
impl_outer_origin! {
pub enum Origin for Test {}
}
// For testing the module, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of modules we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
impl system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type Digest = Digest;
type AccountId = u64;
type Lookup = IdentityLookup<u64>;
type Header = Header;
type Event = ();
type Log = DigestItem;
}
impl balances::Trait for Test {
type Balance = u64;
type OnFreeBalanceZero = ();
type OnNewAccount = ();
type EnsureAccountLiquid = ();
type Event = ();
}
impl Trait for Test {
type Event = ();
}
type Balances = balances::Module<Test>;
type Claims = Module<Test>;
fn alice_secret() -> secp256k1::SecretKey {
secp256k1::SecretKey::parse(&keccak256(b"Alice")).unwrap()
}
fn alice_public() -> secp256k1::PublicKey {
secp256k1::PublicKey::from_secret_key(&alice_secret())
}
fn alice_eth() -> EthereumAddress {
let mut res = EthereumAddress::default();
res.copy_from_slice(&keccak256(&alice_public().serialize()[1..65])[12..]);
res
}
fn alice_sig(who: &[u8]) -> EcdsaSignature {
let msg = keccak256(&create_msg(who));
let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &alice_secret()).unwrap();
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
(sig.0, sig.1, recovery_id.serialize() as i8)
}
fn bob_secret() -> secp256k1::SecretKey {
secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap()
}
fn bob_public() -> secp256k1::PublicKey {
secp256k1::PublicKey::from_secret_key(&bob_secret())
}
fn bob_eth() -> EthereumAddress {
let mut res = EthereumAddress::default();
res.copy_from_slice(&keccak256(&bob_public().serialize()[1..65])[12..]);
res
}
fn bob_sig(who: &[u8]) -> EcdsaSignature {
let msg = keccak256(&create_msg(who));
let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &bob_secret()).unwrap();
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
(sig.0, sig.1, recovery_id.serialize() as i8)
}
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> sr_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
// We use default for brevity, but you can configure as desired if needed.
t.extend(balances::GenesisConfig::<Test>::default().build_storage().unwrap().0);
t.extend(GenesisConfig::<Test>{
claims: vec![(alice_eth(), 100)],
}.build_storage().unwrap().0);
t.into()
}
#[test]
fn basic_setup_works() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Claims::total(), 100);
assert_eq!(Claims::claims(&alice_eth()), Some(100));
assert_eq!(Claims::claims(&[0; 20]), None);
});
}
#[test]
fn claiming_works() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(&42), 0);
assert_ok!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())));
assert_eq!(Balances::free_balance(&42), 100);
});
}
#[test]
fn double_claiming_doesnt_work() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(&42), 0);
assert_ok!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())));
assert_noop!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())), "Ethereum address has no claim");
});
}
#[test]
fn non_sender_sig_doesnt_work() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(&42), 0);
assert_noop!(Claims::claim(Origin::signed(42), alice_sig(&69u64.encode())), "Ethereum address has no claim");
});
}
#[test]
fn non_claimant_doesnt_work() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Balances::free_balance(&42), 0);
assert_noop!(Claims::claim(Origin::signed(42), bob_sig(&69u64.encode())), "Ethereum address has no claim");
});
}
#[test]
fn real_eth_sig_works() {
let sig = hex!["7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c"];
let sig = EcdsaSignature::decode(&mut &sig[..]).unwrap();
let who = 42u64.encode();
let msg = create_msg(&who);
let signer = eth_recover(&sig, &who).unwrap();
assert_eq!(signer, hex!["DF67EC7EAe23D2459694685257b6FC59d1BAA1FE"]);
}
}
...@@ -20,9 +20,16 @@ ...@@ -20,9 +20,16 @@
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256. // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit="256"] #![recursion_limit="256"]
#[cfg(test)]
#[macro_use]
extern crate hex_literal;
#[macro_use] #[macro_use]
extern crate bitvec; extern crate bitvec;
extern crate tiny_keccak;
extern crate secp256k1;
#[macro_use] #[macro_use]
extern crate parity_codec_derive; extern crate parity_codec_derive;
extern crate parity_codec as codec; extern crate parity_codec as codec;
...@@ -64,6 +71,7 @@ extern crate polkadot_primitives as primitives; ...@@ -64,6 +71,7 @@ extern crate polkadot_primitives as primitives;
extern crate substrate_keyring as keyring; extern crate substrate_keyring as keyring;
mod parachains; mod parachains;
mod claims;
use rstd::prelude::*; use rstd::prelude::*;
use substrate_primitives::u32_trait::{_2, _4}; use substrate_primitives::u32_trait::{_2, _4};
...@@ -108,7 +116,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { ...@@ -108,7 +116,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot"), spec_name: create_runtime_str!("polkadot"),
impl_name: create_runtime_str!("parity-polkadot"), impl_name: create_runtime_str!("parity-polkadot"),
authoring_version: 1, authoring_version: 1,
spec_version: 104, spec_version: 105,
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
}; };
...@@ -234,6 +242,10 @@ impl sudo::Trait for Runtime { ...@@ -234,6 +242,10 @@ impl sudo::Trait for Runtime {
type Proposal = Call; type Proposal = Call;
} }
impl claims::Trait for Runtime {
type Event = Event;
}
construct_runtime!( construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, SessionKey>) where pub enum Runtime with Log(InternalLog: DigestItem<Hash, SessionKey>) where
Block = Block, Block = Block,
...@@ -259,6 +271,7 @@ construct_runtime!( ...@@ -259,6 +271,7 @@ construct_runtime!(
Parachains: parachains::{Module, Call, Storage, Config<T>, Inherent}, Parachains: parachains::{Module, Call, Storage, Config<T>, Inherent},
Sudo: sudo, Sudo: sudo,
UpgradeKey: upgrade_key, UpgradeKey: upgrade_key,
Claims: claims,
} }
); );
...@@ -293,8 +306,8 @@ impl_runtime_apis! { ...@@ -293,8 +306,8 @@ impl_runtime_apis! {
Executive::execute_block(block) Executive::execute_block(block)
} }
fn initialise_block(header: <Block as BlockT>::Header) { fn initialise_block(header: &<Block as BlockT>::Header) {
Executive::initialise_block(&header) Executive::initialise_block(header)
} }
} }
...@@ -387,7 +400,7 @@ impl_runtime_apis! { ...@@ -387,7 +400,7 @@ impl_runtime_apis! {
} }
impl fg_primitives::GrandpaApi<Block> for Runtime { impl fg_primitives::GrandpaApi<Block> for Runtime {
fn grandpa_pending_change(digest: DigestFor<Block>) fn grandpa_pending_change(digest: &DigestFor<Block>)
-> Option<ScheduledChange<BlockNumber>> -> Option<ScheduledChange<BlockNumber>>
{ {
for log in digest.logs.iter().filter_map(|l| match l { for log in digest.logs.iter().filter_map(|l| match l {
......
This diff is collapsed.
...@@ -36,6 +36,8 @@ srml-timestamp = { git = "https://github.com/paritytech/substrate", default-feat ...@@ -36,6 +36,8 @@ srml-timestamp = { git = "https://github.com/paritytech/substrate", default-feat
srml-treasury = { git = "https://github.com/paritytech/substrate", default-features = false } srml-treasury = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-upgrade-key = { git = "https://github.com/paritytech/substrate", default-features = false } srml-upgrade-key = { git = "https://github.com/paritytech/substrate", default-features = false }
sr-version = { git = "https://github.com/paritytech/substrate", default-features = false } sr-version = { git = "https://github.com/paritytech/substrate", default-features = false }
libsecp256k1 = "0.2.1"
tiny-keccak = "1.4.2"
[profile.release] [profile.release]
panic = "abort" panic = "abort"
......
...@@ -21,7 +21,7 @@ use polkadot_runtime::{ ...@@ -21,7 +21,7 @@ use polkadot_runtime::{
GenesisConfig, ConsensusConfig, CouncilSeatsConfig, DemocracyConfig, TreasuryConfig, GenesisConfig, ConsensusConfig, CouncilSeatsConfig, DemocracyConfig, TreasuryConfig,
SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, Perbill, SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, Perbill,
CouncilVotingConfig, GrandpaConfig, UpgradeKeyConfig, SudoConfig, IndicesConfig, CouncilVotingConfig, GrandpaConfig, UpgradeKeyConfig, SudoConfig, IndicesConfig,
Permill ClaimsConfig, Permill
}; };
const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/"; const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
...@@ -130,6 +130,9 @@ fn staging_testnet_config_genesis() -> GenesisConfig { ...@@ -130,6 +130,9 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
grandpa: Some(GrandpaConfig { grandpa: Some(GrandpaConfig {
authorities: initial_authorities.clone().into_iter().map(|k| (k, 1)).collect(), authorities: initial_authorities.clone().into_iter().map(|k| (k, 1)).collect(),
}), }),
claims: Some(ClaimsConfig {
claims: vec![],
}),
} }
} }
...@@ -230,6 +233,9 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: H256) -> ...@@ -230,6 +233,9 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: H256) ->
sudo: Some(SudoConfig { sudo: Some(SudoConfig {
key: upgrade_key, key: upgrade_key,
}), }),
claims: Some(ClaimsConfig {
claims: vec![],
}),
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment