diff --git a/Cargo.lock b/Cargo.lock index 479f9033a3ca..02e1077af5ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2220,7 +2220,7 @@ dependencies = [ [[package]] name = "kusama-runtime" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-benchmarking", @@ -4076,7 +4076,7 @@ checksum = "feb3b2b1033b8a60b4da6ee470325f887758c95d5320f52f9ce0df055a55940e" [[package]] name = "polkadot" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "assert_cmd", "futures 0.3.5", @@ -4089,7 +4089,7 @@ dependencies = [ [[package]] name = "polkadot-availability-store" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "derive_more 0.99.6", "exit-future", @@ -4115,7 +4115,7 @@ dependencies = [ [[package]] name = "polkadot-cli" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "frame-benchmarking-cli", "futures 0.3.5", @@ -4139,7 +4139,7 @@ dependencies = [ [[package]] name = "polkadot-collator" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "futures 0.3.5", "futures-timer 2.0.2", @@ -4166,7 +4166,7 @@ dependencies = [ [[package]] name = "polkadot-erasure-coding" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "derive_more 0.15.0", "parity-scale-codec", @@ -4178,7 +4178,7 @@ dependencies = [ [[package]] name = "polkadot-network" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "arrayvec 0.4.12", "bytes 0.5.4", @@ -4227,7 +4227,7 @@ dependencies = [ [[package]] name = "polkadot-parachain" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "derive_more 0.99.6", "log 0.4.8", @@ -4246,7 +4246,7 @@ dependencies = [ [[package]] name = "polkadot-primitives" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-system", @@ -4268,7 +4268,7 @@ dependencies = [ [[package]] name = "polkadot-rpc" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "jsonrpc-core", "pallet-transaction-payment-rpc", @@ -4287,7 +4287,7 @@ dependencies = [ [[package]] name = "polkadot-runtime" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-benchmarking", @@ -4356,7 +4356,7 @@ dependencies = [ [[package]] name = "polkadot-runtime-common" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-benchmarking", @@ -4398,7 +4398,7 @@ dependencies = [ [[package]] name = "polkadot-service" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "env_logger", "frame-benchmarking", @@ -4457,7 +4457,7 @@ dependencies = [ [[package]] name = "polkadot-statement-table" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "parity-scale-codec", "polkadot-primitives", @@ -4466,7 +4466,7 @@ dependencies = [ [[package]] name = "polkadot-test-runtime" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-executive", @@ -4542,7 +4542,7 @@ dependencies = [ [[package]] name = "polkadot-validation" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "derive_more 0.14.1", @@ -7546,7 +7546,7 @@ dependencies = [ [[package]] name = "test-parachain-adder" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "dlmalloc", "parity-scale-codec", @@ -7585,7 +7585,7 @@ dependencies = [ [[package]] name = "test-parachain-halt" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "substrate-wasm-builder-runner 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -8439,7 +8439,7 @@ dependencies = [ [[package]] name = "westend-runtime" -version = "0.7.33" +version = "0.7.34-dev" dependencies = [ "bitvec", "frame-benchmarking", diff --git a/Cargo.toml b/Cargo.toml index d47c3cefd945..5baf461b565e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ path = "src/main.rs" [package] name = "polkadot" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/availability-store/Cargo.toml b/availability-store/Cargo.toml index 44e0d4b006d6..ad9a67a2b776 100644 --- a/availability-store/Cargo.toml +++ b/availability-store/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "polkadot-availability-store" description = "Persistent database for parachain data" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 59154fef7b15..276e3eebbe1f 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-cli" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Polkadot Relay-chain Client Node" edition = "2018" diff --git a/collator/Cargo.toml b/collator/Cargo.toml index 502e3d6fb50f..f8b89ec11c3c 100644 --- a/collator/Cargo.toml +++ b/collator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-collator" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Collator node implementation" edition = "2018" diff --git a/erasure-coding/Cargo.toml b/erasure-coding/Cargo.toml index 6a0b0adfbe05..6d036c3eb6b7 100644 --- a/erasure-coding/Cargo.toml +++ b/erasure-coding/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-erasure-coding" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/network/Cargo.toml b/network/Cargo.toml index a4e49c014421..71b40b0999ec 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-network" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Polkadot-specific networking protocol" edition = "2018" diff --git a/parachain/Cargo.toml b/parachain/Cargo.toml index b8e34bb54fee..c7ca4b7be039 100644 --- a/parachain/Cargo.toml +++ b/parachain/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-parachain" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Types and utilities for creating and working with parachains" edition = "2018" diff --git a/parachain/test-parachains/adder/Cargo.toml b/parachain/test-parachains/adder/Cargo.toml index f594625900cd..3639dac5964d 100644 --- a/parachain/test-parachains/adder/Cargo.toml +++ b/parachain/test-parachains/adder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "test-parachain-adder" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Test parachain which adds to a number as its state transition" edition = "2018" diff --git a/parachain/test-parachains/halt/Cargo.toml b/parachain/test-parachains/halt/Cargo.toml index e61157037f3f..1bd912e5dbea 100644 --- a/parachain/test-parachains/halt/Cargo.toml +++ b/parachain/test-parachains/halt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "test-parachain-halt" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] description = "Test parachain which executes forever" edition = "2018" diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index c841cb73a255..0493edc03867 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-primitives" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 0f80ba6c59ba..16004fd5a750 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -79,12 +79,14 @@ pub use runtime_primitives::OpaqueExtrinsic as UncheckedExtrinsic; /// Custom validity errors used in Polkadot while validating transactions. #[repr(u8)] pub enum ValidityError { - /// The ethereum signature is invalid. + /// The Ethereum signature is invalid. InvalidEthereumSignature = 0, /// The signer has no claim. SignerHasNoClaim = 1, /// No permission to execute the call. NoPermission = 2, + /// An invalid statement was made for a claim. + InvalidStatement = 3, } impl From for u8 { diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 179d9480790b..738c60153eff 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-rpc" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index ed2da2e213c1..67de17db8eae 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-runtime-common" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/runtime/common/src/claims.rs b/runtime/common/src/claims.rs index 87b11fd0f5bc..38378b961501 100644 --- a/runtime/common/src/claims.rs +++ b/runtime/common/src/claims.rs @@ -16,25 +16,26 @@ //! Module to process claims from Ethereum addresses. -use sp_std::prelude::*; +use sp_std::{prelude::*, fmt::Debug}; use sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover}; -use frame_support::{decl_event, decl_storage, decl_module, decl_error}; -use frame_support::traits::{Currency, Get, VestingSchedule}; -use system::{ensure_root, ensure_none}; +use frame_support::{ + decl_event, decl_storage, decl_module, decl_error, ensure, + traits::{Currency, Get, VestingSchedule}, weights::{Pays, DispatchClass}, dispatch::IsSubType +}; +use system::{ensure_signed, ensure_root, ensure_none}; use codec::{Encode, Decode}; #[cfg(feature = "std")] use serde::{self, Serialize, Deserialize, Serializer, Deserializer}; #[cfg(feature = "std")] use sp_runtime::traits::Zero; -use sp_runtime::traits::CheckedSub; use sp_runtime::{ - RuntimeDebug, + traits::{CheckedSub, SignedExtension, DispatchInfoOf}, RuntimeDebug, DispatchResult, transaction_validity::{ - TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction, TransactionSource, + TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction, + TransactionSource, TransactionValidityError, }, }; use primitives::ValidityError; -use system; type CurrencyOf = <::VestingSchedule as VestingSchedule<::AccountId>>::Currency; type BalanceOf = as Currency<::AccountId>>::Balance; @@ -47,6 +48,32 @@ pub trait Trait: system::Trait { type Prefix: Get<&'static [u8]>; } +/// The kind of a statement this account needs to make for a claim to be valid. +#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum StatementKind { + /// One kind of statement; this is the default. + Default, + /// Another kind of statement(!). + Alternative, +} + +impl StatementKind { + /// Convert this to the (English) statement it represents. + fn to_text(self) -> &'static [u8] { + match self { + StatementKind::Default => &b"Default"[..], + StatementKind::Alternative => &b"Alternative"[..], + } + } +} + +impl Default for StatementKind { + fn default() -> Self { + StatementKind::Default + } +} + /// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account). /// /// This gets serialized to the 0x-prefixed hex representation. @@ -109,11 +136,15 @@ decl_error! { InvalidEthereumSignature, /// Ethereum address has no claim. SignerHasNoClaim, + /// Account ID sending tx has no claim. + SenderHasNoClaim, /// The destination is already vesting and cannot be the target of a further claim. DestinationVesting, /// There's not enough in the pot to pay out some unvested amount. Generally implies a logic /// error. PotUnderflow, + /// A needed statement was not included. + InvalidStatement, } } @@ -123,10 +154,10 @@ decl_storage! { // keep things around between blocks. trait Store for Module as Claims { Claims get(fn claims) build(|config: &GenesisConfig| { - config.claims.iter().map(|(a, b)| (a.clone(), b.clone())).collect::>() + config.claims.iter().map(|(a, b, _, _)| (a.clone(), b.clone())).collect::>() }): map hasher(identity) EthereumAddress => Option>; Total get(fn total) build(|config: &GenesisConfig| { - config.claims.iter().fold(Zero::zero(), |acc: BalanceOf, &(_, n)| acc + n) + config.claims.iter().fold(Zero::zero(), |acc: BalanceOf, &(_, b, _, _)| acc + b) }): BalanceOf; /// Vesting schedule for a claim. /// First balance is the total amount that should be held for vesting. @@ -135,9 +166,23 @@ decl_storage! { Vesting get(fn vesting) config(): map hasher(identity) EthereumAddress => Option<(BalanceOf, BalanceOf, T::BlockNumber)>; + + /// The statement kind that must be signed, if any. + Signing build(|config: &GenesisConfig| { + config.claims.iter() + .filter_map(|(a, _, _, s)| Some((a.clone(), s.clone()?))) + .collect::>() + }): map hasher(identity) EthereumAddress => Option; + + /// Pre-claimed Ethereum accounts, by the Account ID that they are claimed to. + Preclaims build(|config: &GenesisConfig| { + config.claims.iter() + .filter_map(|(a, _, i, _)| Some((i.clone()?, a.clone()))) + .collect::>() + }): map hasher(identity) T::AccountId => Option; } add_extra_genesis { - config(claims): Vec<(EthereumAddress, BalanceOf)>; + config(claims): Vec<(EthereumAddress, BalanceOf, Option, Option)>; } } @@ -183,39 +228,22 @@ decl_module! { /// /// Total Complexity: O(1) /// ---------------------------- - /// Base Weight: 622.6 µs + /// Base Weight: 269.7 µs /// DB Weight: - /// - Read: Claims, Total, Claims Vesting, Vesting Vesting, Balance Lock, Account - /// - Write: Vesting Vesting, Account, Balance Lock, Total, Claim, Claims Vesting + /// - Read: Signing, Claims, Total, Claims Vesting, Vesting Vesting, Balance Lock, Account + /// - Write: Vesting Vesting, Account, Balance Lock, Total, Claim, Claims Vesting, Signing + /// Validate Unsigned: +188.7 µs /// - #[weight = T::DbWeight::get().reads_writes(6, 6) + 650_000_000] + #[weight = T::DbWeight::get().reads_writes(7, 7) + 270_000_000 + 190_000_000] fn claim(origin, dest: T::AccountId, ethereum_signature: EcdsaSignature) { ensure_none(origin)?; let data = dest.using_encoded(to_ascii_hex); - let signer = Self::eth_recover(ðereum_signature, &data) + let signer = Self::eth_recover(ðereum_signature, &data, &[][..]) .ok_or(Error::::InvalidEthereumSignature)?; + ensure!(Signing::get(&signer).is_none(), Error::::InvalidStatement); - let balance_due = >::get(&signer) - .ok_or(Error::::SignerHasNoClaim)?; - - let new_total = Self::total().checked_sub(&balance_due).ok_or(Error::::PotUnderflow)?; - - // Check if this claim should have a vesting schedule. - if let Some(vs) = >::get(&signer) { - // If this fails, destination account already has a vesting schedule - // applied to it, and this claim should not be processed. - T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2) - .map_err(|_| Error::::DestinationVesting)?; - } - - CurrencyOf::::deposit_creating(&dest, balance_due); - >::put(new_total); - >::remove(&signer); - >::remove(&signer); - - // Let's deposit an event to let the outside world know this happened. - Self::deposit_event(RawEvent::Claimed(dest, signer, balance_due)); + Self::process_claim(signer, dest)?; } /// Mint a new claim to collect DOTs. @@ -235,16 +263,23 @@ decl_module! { /// /// Total Complexity: O(1) /// --------------------- - /// Base Weight: 25.64 µs + /// Base Weight: 10.46 µs /// DB Weight: /// - Reads: Total - /// - Writes: Total, Claims, Vesting + /// - Writes: Total, Claims + /// - Maybe Write: Vesting, Statement /// - #[weight = T::DbWeight::get().reads_writes(1, 3) + 25_000_000] + #[weight = + T::DbWeight::get().reads_writes(1, 2) + + T::DbWeight::get().writes(vesting_schedule.is_some().into()) + + T::DbWeight::get().writes(statement.is_some().into()) + + 10_000_000 + ] fn mint_claim(origin, who: EthereumAddress, value: BalanceOf, vesting_schedule: Option<(BalanceOf, BalanceOf, T::BlockNumber)>, + statement: Option, ) { ensure_root(origin)?; @@ -253,6 +288,102 @@ decl_module! { if let Some(vs) = vesting_schedule { >::insert(who, vs); } + if let Some(s) = statement { + Signing::insert(who, s); + } + } + + /// Make a claim to collect your DOTs by signing a statement. + /// + /// The dispatch origin for this call must be _None_. + /// + /// Unsigned Validation: + /// A call to `claim_attest` is deemed valid if the signature provided matches + /// the expected signed message of: + /// + /// > Ethereum Signed Message: + /// > (configured prefix string)(address)(statement) + /// + /// and `address` matches the `dest` account; the `statement` must match that which is + /// expected according to your purchase arrangement. + /// + /// Parameters: + /// - `dest`: The destination account to payout the claim. + /// - `ethereum_signature`: The signature of an ethereum signed message + /// matching the format described above. + /// - `statement`: The identity of the statement which is being attested to in the signature. + /// + /// + /// The weight of this call is invariant over the input parameters. + /// - One `eth_recover` operation which involves a keccak hash and a + /// ecdsa recover. + /// - Four storage reads to check if a claim exists for the user, to + /// get the current pot size, to see if there exists a vesting schedule, to get the + /// required statement. + /// - Up to one storage write for adding a new vesting schedule. + /// - One `deposit_creating` Currency call. + /// - One storage write to update the total. + /// - Two storage removals for vesting and claims information. + /// - One deposit event. + /// + /// Total Complexity: O(1) + /// ---------------------------- + /// Base Weight: 270.2 µs + /// DB Weight: + /// - Read: Signing, Claims, Total, Claims Vesting, Vesting Vesting, Balance Lock, Account + /// - Write: Vesting Vesting, Account, Balance Lock, Total, Claim, Claims Vesting, Signing + /// Validate Unsigned: +190.1 µs + /// + #[weight = T::DbWeight::get().reads_writes(7, 7) + 270_000_000 + 190_000_000] + fn claim_attest(origin, + dest: T::AccountId, + ethereum_signature: EcdsaSignature, + statement: Vec, + ) { + ensure_none(origin)?; + + let data = dest.using_encoded(to_ascii_hex); + let signer = Self::eth_recover(ðereum_signature, &data, &statement) + .ok_or(Error::::InvalidEthereumSignature)?; + if let Some(s) = Signing::get(signer) { + ensure!(s.to_text() == &statement[..], Error::::InvalidStatement); + } + Self::process_claim(signer, dest)?; + } + + /// Attest to a statement, needed to finalize the claims process. + /// + /// WARNING: Insecure unless your chain includes `PrevalidateAttests` as a `SignedExtension`. + /// + /// Unsigned Validation: + /// A call to attest is deemed valid if the sender has a `Preclaim` registered + /// and provides a `statement` which is expected for the account. + /// + /// Parameters: + /// - `statement`: The identity of the statement which is being attested to in the signature. + /// + /// + /// Total Complexity: O(1) + /// ---------------------------- + /// Base Weight: 93.3 µs + /// DB Weight: + /// - Read: Preclaims, Signing, Claims, Total, Claims Vesting, Vesting Vesting, Balance Lock, Account + /// - Write: Vesting Vesting, Account, Balance Lock, Total, Claim, Claims Vesting, Signing, Preclaims + /// Validate PreValidateAttests: +8.631 µs + /// + #[weight = ( + T::DbWeight::get().reads_writes(8, 8) + 90_000_000 + 10_000_000, + DispatchClass::Normal, + Pays::No + )] + fn attest(origin, statement: Vec) { + let who = ensure_signed(origin)?; + let signer = Preclaims::::get(&who).ok_or(Error::::SenderHasNoClaim)?; + if let Some(s) = Signing::get(signer) { + ensure!(s.to_text() == &statement[..], Error::::InvalidStatement); + } + Self::process_claim(signer, who.clone())?; + Preclaims::::remove(&who); } } } @@ -270,9 +401,9 @@ fn to_ascii_hex(data: &[u8]) -> Vec { impl Module { // Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign. - fn ethereum_signable_message(what: &[u8]) -> Vec { + fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec { let prefix = T::Prefix::get(); - let mut l = prefix.len() + what.len(); + let mut l = prefix.len() + what.len() + extra.len(); let mut rev = Vec::new(); while l > 0 { rev.push(b'0' + (l % 10) as u8); @@ -282,17 +413,44 @@ impl Module { v.extend(rev.into_iter().rev()); v.extend_from_slice(&prefix[..]); v.extend_from_slice(what); + v.extend_from_slice(extra); v } // Attempts to recover the Ethereum address from a message signature signed by using // the Ethereum RPC's `personal_sign` and `eth_sign`. - fn eth_recover(s: &EcdsaSignature, what: &[u8]) -> Option { - let msg = keccak_256(&Self::ethereum_signable_message(what)); + fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option { + let msg = keccak_256(&Self::ethereum_signable_message(what, extra)); let mut res = EthereumAddress::default(); res.0.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]); Some(res) } + + fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> DispatchResult { + let balance_due = >::get(&signer) + .ok_or(Error::::SignerHasNoClaim)?; + + let new_total = Self::total().checked_sub(&balance_due).ok_or(Error::::PotUnderflow)?; + + // Check if this claim should have a vesting schedule. + if let Some(vs) = >::get(&signer) { + // If this fails, destination account already has a vesting schedule + // applied to it, and this claim should not be processed. + T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2) + .map_err(|_| Error::::DestinationVesting)?; + } + + CurrencyOf::::deposit_creating(&dest, balance_due); + >::put(new_total); + >::remove(&signer); + >::remove(&signer); + Signing::remove(&signer); + + // Let's deposit an event to let the outside world know this happened. + Self::deposit_event(RawEvent::Claimed(dest, signer, balance_due)); + + Ok(()) + } } impl sp_runtime::traits::ValidateUnsigned for Module { @@ -301,38 +459,112 @@ impl sp_runtime::traits::ValidateUnsigned for Module { fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity { const PRIORITY: u64 = 100; - match call { + let (maybe_signer, maybe_statement) = match call { // - // Base Weight: 370 µs - // DB Weight: 1 Read (Claims) + // Base Weight: 188.7 µs (includes the full logic of `validate_unsigned`) + // DB Weight: 2 Read (Claims, Signing) // Call::claim(account, ethereum_signature) => { let data = account.using_encoded(to_ascii_hex); - let maybe_signer = Self::eth_recover(ðereum_signature, &data); - let signer = if let Some(s) = maybe_signer { - s - } else { - return InvalidTransaction::Custom( - ValidityError::InvalidEthereumSignature.into(), - ).into(); - }; - - if !>::contains_key(&signer) { - return Err(InvalidTransaction::Custom( - ValidityError::SignerHasNoClaim.into(), - ).into()); - } + (Self::eth_recover(ðereum_signature, &data, &[][..]), None) + } + // + // Base Weight: 190.1 µs (includes the full logic of `validate_unsigned`) + // DB Weight: 2 Read (Claims, Signing) + // + Call::claim_attest(account, ethereum_signature, statement) => { + let data = account.using_encoded(to_ascii_hex); + (Self::eth_recover(ðereum_signature, &data, &statement), Some(statement.as_slice())) + } + _ => return Err(InvalidTransaction::Call.into()), + }; - Ok(ValidTransaction { - priority: PRIORITY, - requires: vec![], - provides: vec![("claims", signer).encode()], - longevity: TransactionLongevity::max_value(), - propagate: true, - }) + let signer = maybe_signer + .ok_or(InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()))?; + + let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()); + ensure!(>::contains_key(&signer), e); + + let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into()); + match Signing::get(signer) { + None => ensure!(maybe_statement.is_none(), e), + Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e), + } + + Ok(ValidTransaction { + priority: PRIORITY, + requires: vec![], + provides: vec![("claims", signer).encode()], + longevity: TransactionLongevity::max_value(), + propagate: true, + }) + } +} + +/// Validate `attest` calls prior to execution. Needed to avoid a DoS attack since they are +/// otherwise free to place on chain. +#[derive(Encode, Decode, Clone, Eq, PartialEq)] +pub struct PrevalidateAttests(sp_std::marker::PhantomData) where + ::Call: IsSubType, T>; + +impl Debug for PrevalidateAttests where + ::Call: IsSubType, T> +{ + #[cfg(feature = "std")] + fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { + write!(f, "PrevalidateAttests") + } + + #[cfg(not(feature = "std"))] + fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { + Ok(()) + } +} + +impl PrevalidateAttests where + ::Call: IsSubType, T> +{ + /// Create new `SignedExtension` to check runtime version. + pub fn new() -> Self { + Self(sp_std::marker::PhantomData) + } +} + +impl SignedExtension for PrevalidateAttests where + ::Call: IsSubType, T> +{ + type AccountId = T::AccountId; + type Call = ::Call; + type AdditionalSigned = (); + type Pre = (); + const IDENTIFIER: &'static str = "PrevalidateAttests"; + + fn additional_signed(&self) -> Result { + Ok(()) + } + + // + // Base Weight: 8.631 µs + // DB Weight: 2 Read (Preclaims, Signing) + // + fn validate( + &self, + who: &Self::AccountId, + call: &Self::Call, + _info: &DispatchInfoOf, + _len: usize, + ) -> TransactionValidity { + if let Some(local_call) = call.is_sub_type() { + if let Call::attest(attested_statement) = local_call { + let signer = Preclaims::::get(who) + .ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?; + if let Some(s) = Signing::get(signer) { + let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into()); + ensure!(&attested_statement[..] == s.to_text(), e); + } } - _ => Err(InvalidTransaction::Call.into()), } + Ok(ValidTransaction::default()) } } @@ -349,8 +581,8 @@ mod secp_utils { res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]); res } - pub fn sig(secret: &secp256k1::SecretKey, what: &[u8]) -> EcdsaSignature { - let msg = keccak_256(&>::ethereum_signable_message(&to_ascii_hex(what)[..])); + pub fn sig(secret: &secp256k1::SecretKey, what: &[u8], extra: &[u8]) -> EcdsaSignature { + let msg = keccak_256(&>::ethereum_signable_message(&to_ascii_hex(what)[..], extra)); let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), secret); let mut r = [0u8; 65]; r[0..64].copy_from_slice(&sig.serialize()[..]); @@ -372,13 +604,21 @@ mod tests { // or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup, Identity}, testing::Header}; use frame_support::{ - impl_outer_origin, assert_ok, assert_err, assert_noop, parameter_types + impl_outer_origin, impl_outer_dispatch, assert_ok, assert_err, assert_noop, parameter_types, + weights::{Pays, GetDispatchInfo}, }; use balances; + use super::Call as ClaimsCall; impl_outer_origin! { pub enum Origin for Test {} } + + impl_outer_dispatch! { + pub enum Call for Test where origin: Origin { + claims::Claims, + } + } // 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. @@ -392,7 +632,7 @@ mod tests { } impl system::Trait for Test { type Origin = Origin; - type Call = (); + type Call = Call; type Index = u64; type BlockNumber = u64; type Hash = H256; @@ -456,6 +696,15 @@ mod tests { fn bob() -> secp256k1::SecretKey { secp256k1::SecretKey::parse(&keccak_256(b"Bob")).unwrap() } + fn dave() -> secp256k1::SecretKey { + secp256k1::SecretKey::parse(&keccak_256(b"Dave")).unwrap() + } + fn eve() -> secp256k1::SecretKey { + secp256k1::SecretKey::parse(&keccak_256(b"Eve")).unwrap() + } + fn frank() -> secp256k1::SecretKey { + secp256k1::SecretKey::parse(&keccak_256(b"Frank")).unwrap() + } // This function basically just builds a genesis storage key/value store according to // our desired mockup. @@ -464,17 +713,29 @@ mod tests { // We use default for brevity, but you can configure as desired if needed. balances::GenesisConfig::::default().assimilate_storage(&mut t).unwrap(); GenesisConfig::{ - claims: vec![(eth(&alice()), 100)], + claims: vec![ + (eth(&alice()), 100, None, None), + (eth(&dave()), 200, None, Some(StatementKind::Default)), + (eth(&eve()), 300, Some(42), Some(StatementKind::Alternative)), + (eth(&frank()), 400, Some(43), None), + ], vesting: vec![(eth(&alice()), (50, 10, 1))], }.assimilate_storage(&mut t).unwrap(); t.into() } + fn total_claims() -> u64 { + 100 + 200 + 300 + 400 + } + #[test] fn basic_setup_works() { new_test_ext().execute_with(|| { - assert_eq!(Claims::total(), 100); + assert_eq!(Claims::total(), total_claims()); assert_eq!(Claims::claims(ð(&alice())), Some(100)); + assert_eq!(Claims::claims(ð(&dave())), Some(200)); + assert_eq!(Claims::claims(ð(&eve())), Some(300)); + assert_eq!(Claims::claims(ð(&frank())), Some(400)); assert_eq!(Claims::claims(&EthereumAddress::default()), None); assert_eq!(Claims::vesting(ð(&alice())), Some((50, 10, 1))); }); @@ -493,10 +754,113 @@ mod tests { fn claiming_works() { new_test_ext().execute_with(|| { assert_eq!(Balances::free_balance(42), 0); - assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode()))); + assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode(), &[][..]))); assert_eq!(Balances::free_balance(&42), 100); assert_eq!(Vesting::vesting_balance(&42), Some(50)); - assert_eq!(Claims::total(), 0); + assert_eq!(Claims::total(), total_claims() - 100); + }); + } + + #[test] + fn claiming_does_not_bypass_signing() { + new_test_ext().execute_with(|| { + assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode(), &[][..]))); + assert_noop!( + Claims::claim(Origin::NONE, 42, sig::(&dave(), &42u64.encode(), &[][..])), + Error::::InvalidStatement, + ); + assert_noop!( + Claims::claim(Origin::NONE, 42, sig::(&eve(), &42u64.encode(), &[][..])), + Error::::InvalidStatement, + ); + assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&frank(), &42u64.encode(), &[][..]))); + }); + } + + #[test] + fn attest_claiming_works() { + new_test_ext().execute_with(|| { + assert_eq!(Balances::free_balance(42), 0); + let s = sig::(&dave(), &42u64.encode(), StatementKind::Alternative.to_text()); + let r = Claims::claim_attest(Origin::NONE, 42, s.clone(), StatementKind::Alternative.to_text().to_vec()); + assert_noop!(r, Error::::InvalidStatement); + + let r = Claims::claim_attest(Origin::NONE, 42, s, StatementKind::Default.to_text().to_vec()); + assert_noop!(r, Error::::SignerHasNoClaim); + // ^^^ we use ecdsa_recover, so an invalid signature just results in a random signer id + // being recovered, which realistically will never have a claim. + + let s = sig::(&dave(), &42u64.encode(), StatementKind::Default.to_text()); + assert_ok!(Claims::claim_attest(Origin::NONE, 42, s, StatementKind::Default.to_text().to_vec())); + assert_eq!(Balances::free_balance(&42), 200); + assert_eq!(Claims::total(), total_claims() - 200); + + let s = sig::(&dave(), &42u64.encode(), StatementKind::Default.to_text()); + let r = Claims::claim_attest(Origin::NONE, 42, s, StatementKind::Default.to_text().to_vec()); + assert_noop!(r, Error::::SignerHasNoClaim); + }); + } + + #[test] + fn attesting_works() { + new_test_ext().execute_with(|| { + assert_eq!(Balances::free_balance(42), 0); + assert_noop!(Claims::attest(Origin::signed(69), StatementKind::Alternative.to_text().to_vec()), Error::::SenderHasNoClaim); + assert_noop!(Claims::attest(Origin::signed(42), StatementKind::Default.to_text().to_vec()), Error::::InvalidStatement); + assert_ok!(Claims::attest(Origin::signed(42), StatementKind::Alternative.to_text().to_vec())); + assert_eq!(Balances::free_balance(&42), 300); + assert_eq!(Claims::total(), total_claims() - 300); + }); + } + + #[test] + fn claim_cannot_clobber_preclaim() { + new_test_ext().execute_with(|| { + assert_eq!(Balances::free_balance(42), 0); + // Alice's claim is 100 + assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode(), &[][..]))); + assert_eq!(Balances::free_balance(&42), 100); + // Eve's claim is 300 through Account 42 + assert_ok!(Claims::attest(Origin::signed(42), StatementKind::Alternative.to_text().to_vec())); + assert_eq!(Balances::free_balance(&42), 100 + 300); + assert_eq!(Claims::total(), total_claims() - 400); + }); + } + + #[test] + fn valid_attest_transactions_are_free() { + new_test_ext().execute_with(|| { + let p = PrevalidateAttests::::new(); + let c = Call::Claims(ClaimsCall::attest(StatementKind::Alternative.to_text().to_vec())); + let di = c.get_dispatch_info(); + assert_eq!(di.pays_fee, Pays::No); + let r = p.validate(&42, &c, &di, 20); + assert_eq!(r, TransactionValidity::Ok(ValidTransaction::default())); + }); + } + + #[test] + fn invalid_attest_transactions_are_recognised() { + new_test_ext().execute_with(|| { + let p = PrevalidateAttests::::new(); + let c = Call::Claims(ClaimsCall::attest(StatementKind::Default.to_text().to_vec())); + let di = c.get_dispatch_info(); + let r = p.validate(&42, &c, &di, 20); + assert!(r.is_err()); + let c = Call::Claims(ClaimsCall::attest(StatementKind::Alternative.to_text().to_vec())); + let di = c.get_dispatch_info(); + let r = p.validate(&69, &c, &di, 20); + assert!(r.is_err()); + }); + } + + #[test] + fn cannot_bypass_attest_claiming() { + new_test_ext().execute_with(|| { + assert_eq!(Balances::free_balance(42), 0); + let s = sig::(&dave(), &42u64.encode(), &[]); + let r = Claims::claim(Origin::NONE, 42, s.clone()); + assert_noop!(r, Error::::InvalidStatement); }); } @@ -504,20 +868,20 @@ mod tests { fn add_claim_works() { new_test_ext().execute_with(|| { assert_noop!( - Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, None), + Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, None, None), sp_runtime::traits::BadOrigin, ); assert_eq!(Balances::free_balance(42), 0); assert_noop!( - Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode())), + Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode(), &[][..])), Error::::SignerHasNoClaim, ); - assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None)); - assert_eq!(Claims::total(), 300); - assert_ok!(Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode()))); + assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None, None)); + assert_eq!(Claims::total(), total_claims() + 200); + assert_ok!(Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode(), &[][..]))); assert_eq!(Balances::free_balance(&69), 200); assert_eq!(Vesting::vesting_balance(&69), None); - assert_eq!(Claims::total(), 100); + assert_eq!(Claims::total(), total_claims()); }); } @@ -525,27 +889,58 @@ mod tests { fn add_claim_with_vesting_works() { new_test_ext().execute_with(|| { assert_noop!( - Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, Some((50, 10, 1))), + Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, Some((50, 10, 1)), None), sp_runtime::traits::BadOrigin, ); assert_eq!(Balances::free_balance(42), 0); assert_noop!( - Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode())), + Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode(), &[][..])), Error::::SignerHasNoClaim ); - assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1)))); - assert_ok!(Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode()))); + assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1)), None)); + assert_ok!(Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode(), &[][..]))); assert_eq!(Balances::free_balance(&69), 200); assert_eq!(Vesting::vesting_balance(&69), Some(50)); }); } + #[test] + fn add_claim_with_statement_works() { + new_test_ext().execute_with(|| { + assert_noop!( + Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, None, Some(StatementKind::Default)), + sp_runtime::traits::BadOrigin, + ); + assert_eq!(Balances::free_balance(42), 0); + let signature = sig::(&bob(), &69u64.encode(), StatementKind::Default.to_text()); + assert_noop!( + Claims::claim_attest( + Origin::NONE, 69, signature.clone(), StatementKind::Default.to_text().to_vec() + ), + Error::::SignerHasNoClaim + ); + assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None, Some(StatementKind::Default))); + assert_noop!( + Claims::claim_attest( + Origin::NONE, 69, signature.clone(), vec![], + ), + Error::::SignerHasNoClaim + ); + assert_ok!( + Claims::claim_attest( + Origin::NONE, 69, signature.clone(), StatementKind::Default.to_text().to_vec() + ) + ); + assert_eq!(Balances::free_balance(&69), 200); + }); + } + #[test] fn origin_signed_claiming_fail() { new_test_ext().execute_with(|| { assert_eq!(Balances::free_balance(42), 0); assert_err!( - Claims::claim(Origin::signed(42), 42, sig::(&alice(), &42u64.encode())), + Claims::claim(Origin::signed(42), 42, sig::(&alice(), &42u64.encode(), &[][..])), sp_runtime::traits::BadOrigin, ); }); @@ -555,9 +950,9 @@ mod tests { fn double_claiming_doesnt_work() { new_test_ext().execute_with(|| { assert_eq!(Balances::free_balance(42), 0); - assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode()))); + assert_ok!(Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode(), &[][..]))); assert_noop!( - Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode())), + Claims::claim(Origin::NONE, 42, sig::(&alice(), &42u64.encode(), &[][..])), Error::::SignerHasNoClaim ); }); @@ -566,24 +961,19 @@ mod tests { #[test] fn claiming_while_vested_doesnt_work() { new_test_ext().execute_with(|| { - assert_eq!(Claims::total(), 100); // A user is already vested - assert_ok!(::VestingSchedule::add_vesting_schedule(&69, 1000, 100, 10)); - CurrencyOf::::make_free_balance_be(&69, 1000); - assert_eq!(Balances::free_balance(69), 1000); - assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1)))); + assert_ok!(::VestingSchedule::add_vesting_schedule(&69, total_claims(), 100, 10)); + CurrencyOf::::make_free_balance_be(&69, total_claims()); + assert_eq!(Balances::free_balance(69), total_claims()); + assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1)), None)); // New total - assert_eq!(Claims::total(), 300); + assert_eq!(Claims::total(), total_claims() + 200); // They should not be able to claim assert_noop!( - Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode())), + Claims::claim(Origin::NONE, 69, sig::(&bob(), &69u64.encode(), &[][..])), Error::::DestinationVesting ); - // Everything should be unchanged - assert_eq!(Claims::total(), 300); - assert_eq!(Balances::free_balance(69), 1000); - assert_eq!(Vesting::vesting_balance(&69), Some(1000)); }); } @@ -592,7 +982,7 @@ mod tests { new_test_ext().execute_with(|| { assert_eq!(Balances::free_balance(42), 0); assert_noop!( - Claims::claim(Origin::NONE, 42, sig::(&alice(), &69u64.encode())), + Claims::claim(Origin::NONE, 42, sig::(&alice(), &69u64.encode(), &[][..])), Error::::SignerHasNoClaim ); }); @@ -603,7 +993,7 @@ mod tests { new_test_ext().execute_with(|| { assert_eq!(Balances::free_balance(42), 0); assert_noop!( - Claims::claim(Origin::NONE, 42, sig::(&bob(), &69u64.encode())), + Claims::claim(Origin::NONE, 42, sig::(&bob(), &69u64.encode(), &[][..])), Error::::SignerHasNoClaim ); }); @@ -616,7 +1006,7 @@ mod tests { let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"]; let sig = EcdsaSignature(sig); let who = 42u64.using_encoded(to_ascii_hex); - let signer = Claims::eth_recover(&sig, &who).unwrap(); + let signer = Claims::eth_recover(&sig, &who, &[][..]).unwrap(); assert_eq!(signer.0, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]); }); } @@ -628,7 +1018,7 @@ mod tests { new_test_ext().execute_with(|| { assert_eq!( - >::validate_unsigned(source, &Call::claim(1, sig::(&alice(), &1u64.encode()))), + >::validate_unsigned(source, &ClaimsCall::claim(1, sig::(&alice(), &1u64.encode(), &[][..]))), Ok(ValidTransaction { priority: 100, requires: vec![], @@ -638,17 +1028,54 @@ mod tests { }) ); assert_eq!( - >::validate_unsigned(source, &Call::claim(0, EcdsaSignature([0; 65]))), + >::validate_unsigned(source, &ClaimsCall::claim(0, EcdsaSignature([0; 65]))), InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(), ); assert_eq!( - >::validate_unsigned(source, &Call::claim(1, sig::(&bob(), &1u64.encode()))), + >::validate_unsigned(source, &ClaimsCall::claim(1, sig::(&bob(), &1u64.encode(), &[][..]))), InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(), ); + let s = sig::(&dave(), &1u64.encode(), StatementKind::Default.to_text()); + let call = ClaimsCall::claim_attest(1, s, StatementKind::Default.to_text().to_vec()); assert_eq!( - >::validate_unsigned(source, &Call::claim(0, sig::(&bob(), &1u64.encode()))), + >::validate_unsigned(source, &call), + Ok(ValidTransaction { + priority: 100, + requires: vec![], + provides: vec![("claims", eth(&dave())).encode()], + longevity: TransactionLongevity::max_value(), + propagate: true, + }) + ); + assert_eq!( + >::validate_unsigned( + source, + &ClaimsCall::claim_attest(1, EcdsaSignature([0; 65]), + StatementKind::Default.to_text().to_vec()) + ), + InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(), + ); + + let s = sig::(&bob(), &1u64.encode(), StatementKind::Default.to_text()); + let call = ClaimsCall::claim_attest(1, s, StatementKind::Default.to_text().to_vec()); + assert_eq!( + >::validate_unsigned(source, &call), InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(), ); + + let s = sig::(&dave(), &1u64.encode(), StatementKind::Alternative.to_text()); + let call = ClaimsCall::claim_attest(1, s, StatementKind::Default.to_text().to_vec()); + assert_eq!( + >::validate_unsigned(source, &call), + InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(), + ); + + let s = sig::(&dave(), &1u64.encode(), StatementKind::Alternative.to_text()); + let call = ClaimsCall::claim_attest(1, s, StatementKind::Alternative.to_text().to_vec()); + assert_eq!( + >::validate_unsigned(source, &call), + InvalidTransaction::Custom(ValidityError::InvalidStatement.into()).into(), + ); }); } } @@ -673,14 +1100,31 @@ mod benchmarking { let secret_key = secp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap(); let eth_address = eth(&secret_key); let vesting = Some((100_000.into(), 1_000.into(), 100.into())); - super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting)?; + super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, None)?; + Ok(()) + } + + fn create_claim_attest(input: u32) -> DispatchResult { + let secret_key = secp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap(); + let eth_address = eth(&secret_key); + let vesting = Some((100_000.into(), 1_000.into(), 100.into())); + super::Module::::mint_claim( + RawOrigin::Root.into(), + eth_address, + VALUE.into(), + vesting, + Some(Default::default()) + )?; Ok(()) } benchmarks! { _ { - // Create claims in storage. - let c in 0 .. MAX_CLAIMS => create_claim::(c)?; + // Create claims in storage. Two are created at a time! + let c in 0 .. MAX_CLAIMS / 2 => { + create_claim::(c)?; + create_claim_attest::(u32::max_value() - c)?; + }; } // Benchmark `claim` for different users. @@ -690,8 +1134,8 @@ mod benchmarking { let eth_address = eth(&secret_key); let account: T::AccountId = account("user", u, SEED); let vesting = Some((100_000.into(), 1_000.into(), 100.into())); - let signature = sig::(&secret_key, &account.encode()); - super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting)?; + let signature = sig::(&secret_key, &account.encode(), &[][..]); + super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, None)?; assert_eq!(Claims::::get(eth_address), Some(VALUE.into())); }: _(RawOrigin::None, account, signature) verify { @@ -703,24 +1147,96 @@ mod benchmarking { let c in ...; let eth_address = account("eth_address", c, SEED); let vesting = Some((100_000.into(), 1_000.into(), 100.into())); - }: _(RawOrigin::Root, eth_address, VALUE.into(), vesting) + let statement = StatementKind::Default; + }: _(RawOrigin::Root, eth_address, VALUE.into(), vesting, Some(statement)) + verify { + assert_eq!(Claims::::get(eth_address), Some(VALUE.into())); + } + + // Benchmark `claim_attest` for different users. + claim_attest { + let u in 0 .. 1000; + let attest_u = u32::max_value() - u; + let secret_key = secp256k1::SecretKey::parse(&keccak_256(&attest_u.encode())).unwrap(); + let eth_address = eth(&secret_key); + let account: T::AccountId = account("user", u, SEED); + let vesting = Some((100_000.into(), 1_000.into(), 100.into())); + let statement = StatementKind::Default; + let signature = sig::(&secret_key, &account.encode(), statement.to_text()); + super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, Some(statement))?; + assert_eq!(Claims::::get(eth_address), Some(VALUE.into())); + }: _(RawOrigin::None, account, signature, statement.to_text().to_vec()) verify { + assert_eq!(Claims::::get(eth_address), None); + } + + // Benchmark `attest` for different users. + attest { + let u in 0 .. 1000; + let attest_u = u32::max_value() - u; + let secret_key = secp256k1::SecretKey::parse(&keccak_256(&attest_u.encode())).unwrap(); + let eth_address = eth(&secret_key); + let account: T::AccountId = account("user", u, SEED); + let vesting = Some((100_000.into(), 1_000.into(), 100.into())); + let statement = StatementKind::Default; + let signature = sig::(&secret_key, &account.encode(), statement.to_text()); + super::Module::::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, Some(statement))?; + Preclaims::::insert(&account, eth_address); assert_eq!(Claims::::get(eth_address), Some(VALUE.into())); + }: _(RawOrigin::Signed(account), statement.to_text().to_vec()) + verify { + assert_eq!(Claims::::get(eth_address), None); } - // Benchmark the time it takes to execute `validate_unsigned` - validate_unsigned { + // Benchmark the time it takes to execute `validate_unsigned` for `claim` + validate_unsigned_claim { let c in ...; // Crate signature let secret_key = secp256k1::SecretKey::parse(&keccak_256(&c.encode())).unwrap(); let account: T::AccountId = account("user", c, SEED); - let signature = sig::(&secret_key, &account.encode()); + let signature = sig::(&secret_key, &account.encode(), &[][..]); let call = Call::::claim(account, signature); let source = sp_runtime::transaction_validity::TransactionSource::External; }: { super::Module::::validate_unsigned(source, &call)? } + // Benchmark the time it takes to execute `validate_unsigned` for `claim_attest` + validate_unsigned_claim_attest { + let c in ...; + // Crate signature + let attest_c = u32::max_value() - c; + let secret_key = secp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap(); + let account: T::AccountId = account("user", c, SEED); + let signature = sig::(&secret_key, &account.encode(), StatementKind::Default.to_text()); + let call = Call::::claim_attest(account, signature, StatementKind::Default.to_text().to_vec()); + let source = sp_runtime::transaction_validity::TransactionSource::External; + }: { + super::Module::::validate_unsigned(source, &call)? + } + + validate_prevalidate_attests { + let c in ...; + let attest_c = u32::max_value() - c; + let secret_key = secp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap(); + let eth_address = eth(&secret_key); + let account: T::AccountId = account("user", c, SEED); + Preclaims::::insert(&account, eth_address); + let call = super::Call::attest(StatementKind::Default.to_text().to_vec()); + // We have to copy the validate statement here because of trait issues... :( + let validate = |who: &T::AccountId, call: &super::Call| -> DispatchResult { + if let Call::attest(attested_statement) = call { + let signer = Preclaims::::get(who).ok_or("signer has no claim")?; + if let Some(s) = Signing::get(signer) { + ensure!(&attested_statement[..] == s.to_text(), "invalid statement"); + } + } + Ok(()) + }; + }: { + validate(&account, &call)? + } + // Benchmark the time it takes to do `repeat` number of keccak256 hashes keccak256 { let i in 0 .. 10_000; @@ -737,11 +1253,12 @@ mod benchmarking { // Crate signature let secret_key = secp256k1::SecretKey::parse(&keccak_256(&i.encode())).unwrap(); let account: T::AccountId = account("user", i, SEED); - let signature = sig::(&secret_key, &account.encode()); + let signature = sig::(&secret_key, &account.encode(), &[][..]); let data = account.using_encoded(to_ascii_hex); + let extra = StatementKind::default().to_text(); }: { for _ in 0 .. i { - assert!(super::Module::::eth_recover(&signature, &data).is_some()); + assert!(super::Module::::eth_recover(&signature, &data, extra).is_some()); } } } @@ -757,7 +1274,11 @@ mod benchmarking { new_test_ext().execute_with(|| { assert_ok!(test_benchmark_claim::()); assert_ok!(test_benchmark_mint_claim::()); - assert_ok!(test_benchmark_validate_unsigned::()); + assert_ok!(test_benchmark_claim_attest::()); + assert_ok!(test_benchmark_attest::()); + assert_ok!(test_benchmark_validate_unsigned_claim::()); + assert_ok!(test_benchmark_validate_unsigned_claim_attest::()); + assert_ok!(test_benchmark_validate_prevalidate_attests::()); assert_ok!(test_benchmark_keccak256::()); assert_ok!(test_benchmark_eth_recover::()); }); diff --git a/runtime/kusama/Cargo.toml b/runtime/kusama/Cargo.toml index 92d0096ed6e7..3b23e86cd4f9 100644 --- a/runtime/kusama/Cargo.toml +++ b/runtime/kusama/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kusama-runtime" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" build = "build.rs" diff --git a/runtime/polkadot/Cargo.toml b/runtime/polkadot/Cargo.toml index 1c8f1dd00516..5d57e0e99c2b 100644 --- a/runtime/polkadot/Cargo.toml +++ b/runtime/polkadot/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-runtime" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" build = "build.rs" diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index b9c4ba994c1e..7c35b6a60c6f 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -612,6 +612,7 @@ impl system::offchain::CreateSignedTransaction for Runtime registrar::LimitParathreadCommits::::new(), parachains::ValidateDoubleVoteReports::::new(), grandpa::ValidateEquivocationReport::::new(), + claims::PrevalidateAttests::::new(), ); let raw_payload = SignedPayload::new(call, extra).map_err(|e| { debug::warn!("Unable to create signed payload: {:?}", e); @@ -771,6 +772,7 @@ pub type SignedExtra = ( registrar::LimitParathreadCommits, parachains::ValidateDoubleVoteReports, grandpa::ValidateEquivocationReport, + claims::PrevalidateAttests, ); /// Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; diff --git a/runtime/test-runtime/Cargo.toml b/runtime/test-runtime/Cargo.toml index 3aedca3fc67a..70b39ee68a66 100644 --- a/runtime/test-runtime/Cargo.toml +++ b/runtime/test-runtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-test-runtime" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" build = "build.rs" diff --git a/runtime/westend/Cargo.toml b/runtime/westend/Cargo.toml index 406f06a397b5..a9e9a6ed0db0 100644 --- a/runtime/westend/Cargo.toml +++ b/runtime/westend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "westend-runtime" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" build = "build.rs" diff --git a/service/Cargo.toml b/service/Cargo.toml index 6e3015048c93..c404cc125432 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-service" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/statement-table/Cargo.toml b/statement-table/Cargo.toml index 8e86c833451e..0fbc2860d48f 100644 --- a/statement-table/Cargo.toml +++ b/statement-table/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-statement-table" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018" diff --git a/validation/Cargo.toml b/validation/Cargo.toml index 51e23155eab1..dbe7af09652f 100644 --- a/validation/Cargo.toml +++ b/validation/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "polkadot-validation" -version = "0.7.33" +version = "0.7.34-dev" authors = ["Parity Technologies "] edition = "2018"