mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
Support both polkadot and kusama runtimes (#704)
* Allow both polkadot and kusama runtimes * Allow both polkadot and kusama runtimes * Make `collator` build * Removed kusama runtime * Introduced common runtime * Updated for latest substrate * Updated CI targets * Updated CI version check * Removed unused dependency * Pulled latests substrate * Pulled latest substrate * Fixed version * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * NEW_HEADS_IDENTIFIER moved to primitives * Updated CI check script * Fixed script * Set epoch duration for polkadot * ci: check_runtime for both runtimes Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: gabriel klawitter <gabreal@users.noreply.github.com>
This commit is contained in:
committed by
Gavin Wood
parent
9a9bbd1c2d
commit
a00d74d825
@@ -0,0 +1,189 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot 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.
|
||||
|
||||
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A module for tracking all attestations that fell on a given candidate receipt.
|
||||
//!
|
||||
//! In the future, it is planned that this module will handle dispute resolution
|
||||
//! as well.
|
||||
|
||||
use rstd::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
use frame_support::{decl_storage, decl_module, ensure, dispatch::DispatchResult, traits::Get};
|
||||
|
||||
use primitives::{Hash, parachain::{AttestedCandidate, CandidateReceipt, Id as ParaId}};
|
||||
use sp_runtime::RuntimeDebug;
|
||||
use sp_staking::SessionIndex;
|
||||
|
||||
use inherents::{ProvideInherent, InherentData, MakeFatalError, InherentIdentifier};
|
||||
use system::ensure_none;
|
||||
|
||||
/// Parachain blocks included in a recent relay-chain block.
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct IncludedBlocks<T: Trait> {
|
||||
/// The actual relay chain block number where blocks were included.
|
||||
pub actual_number: T::BlockNumber,
|
||||
/// The session index at this block.
|
||||
pub session: SessionIndex,
|
||||
/// The randomness seed at this block.
|
||||
pub random_seed: [u8; 32],
|
||||
/// All parachain IDs active at this block.
|
||||
pub active_parachains: Vec<ParaId>,
|
||||
/// Hashes of the parachain candidates included at this block.
|
||||
pub para_blocks: Vec<Hash>,
|
||||
}
|
||||
|
||||
/// Attestations kept over time on a parachain block.
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct BlockAttestations<T: Trait> {
|
||||
receipt: CandidateReceipt,
|
||||
valid: Vec<T::AccountId>, // stash account ID of voter.
|
||||
invalid: Vec<T::AccountId>, // stash account ID of voter.
|
||||
}
|
||||
|
||||
/// Additional attestations on a parachain block, after it was included.
|
||||
#[derive(Encode, Decode, Clone, PartialEq, RuntimeDebug)]
|
||||
pub struct MoreAttestations;
|
||||
|
||||
/// Something which processes rewards for received attestations.
|
||||
pub trait RewardAttestation {
|
||||
/// Reward immediate attestations on parachain blocks. The argument is an iterable of
|
||||
/// validator indices of the attesting validators.
|
||||
fn reward_immediate(validator_indices: impl IntoIterator<Item=u32>);
|
||||
}
|
||||
|
||||
impl RewardAttestation for () {
|
||||
fn reward_immediate(validator_indices: impl IntoIterator<Item=u32>) {
|
||||
// ensure side-effecting iterators do work.
|
||||
for _ in validator_indices {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: staking::Trait> RewardAttestation for staking::Module<T> {
|
||||
fn reward_immediate(validator_indices: impl IntoIterator<Item=u32>) {
|
||||
// The number of points to reward for a validity statement.
|
||||
// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#payment-details
|
||||
const STAKING_REWARD_POINTS: u32 = 20;
|
||||
|
||||
Self::reward_by_indices(validator_indices.into_iter().map(|i| (i, STAKING_REWARD_POINTS)))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trait: session::Trait {
|
||||
/// How many blocks ago we're willing to accept attestations for.
|
||||
type AttestationPeriod: Get<Self::BlockNumber>;
|
||||
|
||||
/// Get a list of the validators' underlying identities.
|
||||
type ValidatorIdentities: Get<Vec<Self::AccountId>>;
|
||||
|
||||
/// Hook for rewarding validators upon attesting.
|
||||
type RewardAttestation: RewardAttestation;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as Attestations {
|
||||
/// A mapping from modular block number (n % AttestationPeriod)
|
||||
/// to session index and the list of candidate hashes.
|
||||
pub RecentParaBlocks: map T::BlockNumber => Option<IncludedBlocks<T>>;
|
||||
|
||||
/// Attestations on a recent parachain block.
|
||||
pub ParaBlockAttestations: double_map T::BlockNumber, blake2_128(Hash) => Option<BlockAttestations<T>>;
|
||||
|
||||
// Did we already have more attestations included in this block?
|
||||
DidUpdate: bool;
|
||||
}
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
/// Parachain-attestations module.
|
||||
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::Origin {
|
||||
/// Provide candidate receipts for parachains, in ascending order by id.
|
||||
fn more_attestations(origin, _more: MoreAttestations) -> DispatchResult {
|
||||
ensure_none(origin)?;
|
||||
ensure!(!<DidUpdate>::exists(), "More attestations can be added only once in a block.");
|
||||
<DidUpdate>::put(true);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_finalize(_n: T::BlockNumber) {
|
||||
<DidUpdate>::kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> Module<T> {
|
||||
/// Update recent candidates to contain the already-checked parachain candidates.
|
||||
pub(crate) fn note_included(heads: &[AttestedCandidate], para_blocks: IncludedBlocks<T>) {
|
||||
let attestation_period = T::AttestationPeriod::get();
|
||||
let mod_num = para_blocks.actual_number % attestation_period;
|
||||
|
||||
// clear old entry that was in this place.
|
||||
if let Some(old_entry) = <RecentParaBlocks<T>>::take(&mod_num) {
|
||||
<ParaBlockAttestations<T>>::remove_prefix(&old_entry.actual_number);
|
||||
}
|
||||
|
||||
let validators = T::ValidatorIdentities::get();
|
||||
|
||||
// make new entry.
|
||||
for (head, hash) in heads.iter().zip(¶_blocks.para_blocks) {
|
||||
let mut valid = Vec::new();
|
||||
let invalid = Vec::new();
|
||||
|
||||
{
|
||||
let attesting_indices = head.validator_indices
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, bit)| *bit)
|
||||
.inspect(|&(auth_index, _)| {
|
||||
if let Some(stash_id) = validators.get(auth_index) {
|
||||
valid.push(stash_id.clone());
|
||||
}
|
||||
})
|
||||
.map(|(i, _)| i as u32);
|
||||
|
||||
T::RewardAttestation::reward_immediate(attesting_indices);
|
||||
}
|
||||
|
||||
let summary = BlockAttestations {
|
||||
receipt: head.candidate().clone(),
|
||||
valid,
|
||||
invalid,
|
||||
};
|
||||
|
||||
<ParaBlockAttestations<T>>::insert(¶_blocks.actual_number, hash, &summary);
|
||||
}
|
||||
|
||||
<RecentParaBlocks<T>>::insert(&mod_num, ¶_blocks);
|
||||
}
|
||||
}
|
||||
|
||||
/// An identifier for inherent data that provides after-the-fact attestations
|
||||
/// on already included parachain blocks.
|
||||
pub const MORE_ATTESTATIONS_IDENTIFIER: InherentIdentifier = *b"par-atts";
|
||||
|
||||
pub type InherentType = MoreAttestations;
|
||||
|
||||
impl<T: Trait> ProvideInherent for Module<T> {
|
||||
type Call = Call<T>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = MORE_ATTESTATIONS_IDENTIFIER;
|
||||
|
||||
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
|
||||
data.get_data::<InherentType>(&MORE_ATTESTATIONS_IDENTIFIER)
|
||||
.ok()
|
||||
.and_then(|x| x.map(Call::more_attestations))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,522 @@
|
||||
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// 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 sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover};
|
||||
use frame_support::{decl_event, decl_storage, decl_module};
|
||||
use frame_support::weights::SimpleDispatchInfo;
|
||||
use frame_support::traits::{Currency, Get, VestingCurrency};
|
||||
use system::{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::{
|
||||
RuntimeDebug, transaction_validity::{
|
||||
TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction
|
||||
},
|
||||
};
|
||||
use primitives::ValidityError;
|
||||
use system;
|
||||
|
||||
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
|
||||
|
||||
/// Configuration trait.
|
||||
pub trait Trait: system::Trait {
|
||||
/// The overarching event type.
|
||||
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
||||
type Currency: Currency<Self::AccountId>
|
||||
+ VestingCurrency<Self::AccountId, Moment=Self::BlockNumber>;
|
||||
type Prefix: Get<&'static [u8]>;
|
||||
}
|
||||
|
||||
/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
|
||||
///
|
||||
/// This gets serialized to the 0x-prefixed hex representation.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Default, RuntimeDebug)]
|
||||
pub struct EthereumAddress([u8; 20]);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for EthereumAddress {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
|
||||
serializer.serialize_str(&format!("0x{}", hex))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> Deserialize<'de> for EthereumAddress {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
|
||||
let base_string = String::deserialize(deserializer)?;
|
||||
let offset = if base_string.starts_with("0x") { 2 } else { 0 };
|
||||
let s = &base_string[offset..];
|
||||
if s.len() != 40 {
|
||||
Err(serde::de::Error::custom("Bad length of Ethereum address (should be 42 including '0x')"))?;
|
||||
}
|
||||
let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
|
||||
.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
|
||||
let mut r = Self::default();
|
||||
r.0.copy_from_slice(&raw);
|
||||
Ok(r)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode, Clone)]
|
||||
pub struct EcdsaSignature(pub [u8; 65]);
|
||||
|
||||
impl PartialEq for EcdsaSignature {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
&self.0[..] == &other.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl rstd::fmt::Debug for EcdsaSignature {
|
||||
fn fmt(&self, f: &mut rstd::fmt::Formatter<'_>) -> rstd::fmt::Result {
|
||||
write!(f, "EcdsaSignature({:?})", &self.0[..])
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event<T> where
|
||||
Balance = BalanceOf<T>,
|
||||
AccountId = <T as system::Trait>::AccountId
|
||||
{
|
||||
/// Someone claimed some DOTs.
|
||||
Claimed(AccountId, EthereumAddress, Balance),
|
||||
}
|
||||
);
|
||||
|
||||
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<BalanceOf<T>>;
|
||||
Total get(total) build(|config: &GenesisConfig<T>| {
|
||||
config.claims.iter().fold(Zero::zero(), |acc: BalanceOf<T>, &(_, n)| acc + n)
|
||||
}): BalanceOf<T>;
|
||||
/// Vesting schedule for a claim.
|
||||
/// First balance is the total amount that should be held for vesting.
|
||||
/// Second balance is how much should be unlocked per block.
|
||||
/// The block number is when the vesting should start.
|
||||
Vesting get(vesting) config(): map EthereumAddress => Option<(BalanceOf<T>, BalanceOf<T>, T::BlockNumber)>;
|
||||
}
|
||||
add_extra_genesis {
|
||||
config(claims): Vec<(EthereumAddress, BalanceOf<T>)>;
|
||||
}
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
/// The Prefix that is used in signed Ethereum messages for this network
|
||||
const Prefix: &[u8] = T::Prefix::get();
|
||||
|
||||
/// Deposit one of this module's events by using the default implementation.
|
||||
fn deposit_event() = default;
|
||||
|
||||
/// Make a claim.
|
||||
#[weight = SimpleDispatchInfo::FixedNormal(1_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)
|
||||
.ok_or("Invalid Ethereum signature")?;
|
||||
|
||||
let balance_due = <Claims<T>>::get(&signer)
|
||||
.ok_or("Ethereum address has no claim")?;
|
||||
|
||||
// Check if this claim should have a vesting schedule.
|
||||
if let Some(vs) = <Vesting<T>>::get(&signer) {
|
||||
// If this fails, destination account already has a vesting schedule
|
||||
// applied to it, and this claim should not be processed.
|
||||
T::Currency::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)?;
|
||||
}
|
||||
|
||||
<Claims<T>>::remove(&signer);
|
||||
<Vesting<T>>::remove(&signer);
|
||||
|
||||
<Total<T>>::mutate(|t| if *t < balance_due {
|
||||
panic!("Logic error: Pot less than the total of claims!")
|
||||
} else {
|
||||
*t -= balance_due
|
||||
});
|
||||
|
||||
T::Currency::deposit_creating(&dest, balance_due);
|
||||
|
||||
// Let's deposit an event to let the outside world know this happened.
|
||||
Self::deposit_event(RawEvent::Claimed(dest, signer, balance_due));
|
||||
}
|
||||
|
||||
/// Add a new claim, if you are root.
|
||||
#[weight = SimpleDispatchInfo::FreeOperational]
|
||||
fn mint_claim(origin,
|
||||
who: EthereumAddress,
|
||||
value: BalanceOf<T>,
|
||||
vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, T::BlockNumber)>,
|
||||
) {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<Total<T>>::mutate(|t| *t += value);
|
||||
<Claims<T>>::insert(who, value);
|
||||
if let Some(vs) = vesting_schedule {
|
||||
<Vesting<T>>::insert(who, vs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
|
||||
fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
|
||||
let mut r = Vec::with_capacity(data.len() * 2);
|
||||
let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
|
||||
for &b in data.iter() {
|
||||
push_nibble(b / 16);
|
||||
push_nibble(b % 16);
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
impl<T: Trait> Module<T> {
|
||||
// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
|
||||
fn ethereum_signable_message(what: &[u8]) -> Vec<u8> {
|
||||
let prefix = T::Prefix::get();
|
||||
let mut l = prefix.len() + what.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(what);
|
||||
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<EthereumAddress> {
|
||||
let msg = keccak_256(&Self::ethereum_signable_message(what));
|
||||
let mut res = EthereumAddress::default();
|
||||
res.0.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)] // Allow `ValidateUnsigned`
|
||||
impl<T: Trait> sp_runtime::traits::ValidateUnsigned for Module<T> {
|
||||
type Call = Call<T>;
|
||||
|
||||
fn validate_unsigned(call: &Self::Call) -> TransactionValidity {
|
||||
const PRIORITY: u64 = 100;
|
||||
|
||||
match call {
|
||||
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 !<Claims<T>>::exists(&signer) {
|
||||
return Err(InvalidTransaction::Custom(
|
||||
ValidityError::SignerHasNoClaim.into(),
|
||||
).into());
|
||||
}
|
||||
|
||||
Ok(ValidTransaction {
|
||||
priority: PRIORITY,
|
||||
requires: vec![],
|
||||
provides: vec![("claims", signer).encode()],
|
||||
longevity: TransactionLongevity::max_value(),
|
||||
propagate: true,
|
||||
})
|
||||
}
|
||||
_ => Err(InvalidTransaction::Call.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use secp256k1;
|
||||
use tiny_keccak::keccak256;
|
||||
use hex_literal::hex;
|
||||
use super::*;
|
||||
|
||||
use sp_core::H256;
|
||||
use codec::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 required.
|
||||
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
|
||||
use balances;
|
||||
use frame_support::{impl_outer_origin, assert_ok, assert_err, assert_noop, parameter_types};
|
||||
|
||||
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;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u32 = 250;
|
||||
pub const MaximumBlockWeight: u32 = 4 * 1024 * 1024;
|
||||
pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
}
|
||||
impl system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Call = ();
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<u64>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
type ModuleToIndex = ();
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 0;
|
||||
pub const TransferFee: u64 = 0;
|
||||
pub const CreationFee: u64 = 0;
|
||||
}
|
||||
|
||||
impl balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type OnFreeBalanceZero = ();
|
||||
type OnNewAccount = ();
|
||||
type Event = ();
|
||||
type DustRemoval = ();
|
||||
type TransferPayment = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type TransferFee = TransferFee;
|
||||
type CreationFee = CreationFee;
|
||||
}
|
||||
|
||||
parameter_types!{
|
||||
pub const Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:";
|
||||
}
|
||||
|
||||
impl Trait for Test {
|
||||
type Event = ();
|
||||
type Currency = Balances;
|
||||
type Prefix = Prefix;
|
||||
}
|
||||
type Balances = balances::Module<Test>;
|
||||
type Claims = Module<Test>;
|
||||
|
||||
fn alice() -> secp256k1::SecretKey {
|
||||
secp256k1::SecretKey::parse(&keccak256(b"Alice")).unwrap()
|
||||
}
|
||||
fn bob() -> secp256k1::SecretKey {
|
||||
secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap()
|
||||
}
|
||||
fn public(secret: &secp256k1::SecretKey) -> secp256k1::PublicKey {
|
||||
secp256k1::PublicKey::from_secret_key(secret)
|
||||
}
|
||||
fn eth(secret: &secp256k1::SecretKey) -> EthereumAddress {
|
||||
let mut res = EthereumAddress::default();
|
||||
res.0.copy_from_slice(&keccak256(&public(secret).serialize()[1..65])[12..]);
|
||||
res
|
||||
}
|
||||
fn sig(secret: &secp256k1::SecretKey, what: &[u8]) -> EcdsaSignature {
|
||||
let msg = keccak256(&Claims::ethereum_signable_message(&to_ascii_hex(what)[..]));
|
||||
let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), secret);
|
||||
let mut r = [0u8; 65];
|
||||
r[0..64].copy_from_slice(&sig.serialize()[..]);
|
||||
r[64] = recovery_id.serialize();
|
||||
EcdsaSignature(r)
|
||||
}
|
||||
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
// our desired mockup.
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
// We use default for brevity, but you can configure as desired if needed.
|
||||
balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::<Test>{
|
||||
claims: vec![(eth(&alice()), 100)],
|
||||
vesting: vec![(eth(&alice()), (50, 10, 1))],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_setup_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Claims::total(), 100);
|
||||
assert_eq!(Claims::claims(ð(&alice())), Some(100));
|
||||
assert_eq!(Claims::claims(&EthereumAddress::default()), None);
|
||||
assert_eq!(Claims::vesting(ð(&alice())), Some((50, 10, 1)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serde_works() {
|
||||
let x = EthereumAddress(hex!["0123456789abcdef0123456789abcdef01234567"]);
|
||||
let y = serde_json::to_string(&x).unwrap();
|
||||
assert_eq!(y, "\"0x0123456789abcdef0123456789abcdef01234567\"");
|
||||
let z: EthereumAddress = serde_json::from_str(&y).unwrap();
|
||||
assert_eq!(x, z);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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_eq!(Balances::free_balance(&42), 100);
|
||||
assert_eq!(Balances::vesting_balance(&42), 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_claim_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, None),
|
||||
sp_runtime::traits::BadOrigin,
|
||||
);
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
|
||||
"Ethereum address has no claim"
|
||||
);
|
||||
assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None));
|
||||
assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
|
||||
assert_eq!(Balances::free_balance(&69), 200);
|
||||
assert_eq!(Balances::vesting_balance(&69), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
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))),
|
||||
sp_runtime::traits::BadOrigin,
|
||||
);
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(
|
||||
Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
|
||||
"Ethereum address has no claim"
|
||||
);
|
||||
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_eq!(Balances::free_balance(&69), 200);
|
||||
assert_eq!(Balances::vesting_balance(&69), 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[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())),
|
||||
sp_runtime::traits::BadOrigin,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
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_noop!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())), "Ethereum address has no claim");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_sender_sig_doesnt_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(Claims::claim(Origin::NONE, 42, sig(&alice(), &69u64.encode())), "Ethereum address has no claim");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_claimant_doesnt_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&42), 0);
|
||||
assert_noop!(Claims::claim(Origin::NONE, 42, sig(&bob(), &69u64.encode())), "Ethereum address has no claim");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_eth_sig_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// "Pay RUSTs to the TEST account:2a00000000000000"
|
||||
let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"];
|
||||
let sig = EcdsaSignature(sig);
|
||||
let who = 42u64.using_encoded(to_ascii_hex);
|
||||
let signer = Claims::eth_recover(&sig, &who).unwrap();
|
||||
assert_eq!(signer.0, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_unsigned_works() {
|
||||
#![allow(deprecated)] // Allow `ValidateUnsigned`
|
||||
use sp_runtime::traits::ValidateUnsigned;
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(
|
||||
<Module<Test>>::validate_unsigned(&Call::claim(1, sig(&alice(), &1u64.encode()))),
|
||||
Ok(ValidTransaction {
|
||||
priority: 100,
|
||||
requires: vec![],
|
||||
provides: vec![("claims", eth(&alice())).encode()],
|
||||
longevity: TransactionLongevity::max_value(),
|
||||
propagate: true,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
<Module<Test>>::validate_unsigned(&Call::claim(0, EcdsaSignature([0; 65]))),
|
||||
InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
|
||||
);
|
||||
assert_eq!(
|
||||
<Module<Test>>::validate_unsigned(&Call::claim(1, sig(&bob(), &1u64.encode()))),
|
||||
InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
|
||||
);
|
||||
assert_eq!(
|
||||
<Module<Test>>::validate_unsigned(&Call::claim(0, sig(&bob(), &1u64.encode()))),
|
||||
InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,149 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot 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.
|
||||
|
||||
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Auxillary struct/enums for polkadot runtime.
|
||||
|
||||
use primitives::Balance;
|
||||
use sp_runtime::traits::{Convert, Saturating};
|
||||
use sp_runtime::{Fixed64, Perbill};
|
||||
use frame_support::weights::Weight;
|
||||
use frame_support::traits::{OnUnbalanced, Imbalance, Currency, Get};
|
||||
use crate::{MaximumBlockWeight, NegativeImbalance};
|
||||
|
||||
/// Logic for the author to get a portion of fees.
|
||||
pub struct ToAuthor<R>(rstd::marker::PhantomData<R>);
|
||||
|
||||
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
|
||||
where
|
||||
R: balances::Trait + authorship::Trait,
|
||||
<R as system::Trait>::AccountId: From<primitives::AccountId>,
|
||||
<R as system::Trait>::AccountId: Into<primitives::AccountId>,
|
||||
<R as system::Trait>::Event: From<balances::RawEvent<
|
||||
<R as system::Trait>::AccountId,
|
||||
<R as balances::Trait>::Balance,
|
||||
balances::DefaultInstance>
|
||||
>,
|
||||
{
|
||||
fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
|
||||
let numeric_amount = amount.peek();
|
||||
let author = <authorship::Module<R>>::author();
|
||||
<balances::Module<R>>::resolve_creating(&<authorship::Module<R>>::author(), amount);
|
||||
<system::Module<R>>::deposit_event(balances::RawEvent::Deposit(author, numeric_amount));
|
||||
}
|
||||
}
|
||||
|
||||
/// Converter for currencies to votes.
|
||||
pub struct CurrencyToVoteHandler<R>(rstd::marker::PhantomData<R>);
|
||||
|
||||
impl<R> CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn factor() -> u128 {
|
||||
let issuance: u128 = <balances::Module<R>>::total_issuance().into();
|
||||
(issuance / u64::max_value() as u128).max(1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> Convert<u128, u64> for CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn convert(x: u128) -> u64 { (x / Self::factor()) as u64 }
|
||||
}
|
||||
|
||||
impl<R> Convert<u128, u128> for CurrencyToVoteHandler<R>
|
||||
where
|
||||
R: balances::Trait,
|
||||
R::Balance: Into<u128>,
|
||||
{
|
||||
fn convert(x: u128) -> u128 { x * Self::factor() }
|
||||
}
|
||||
|
||||
/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
|
||||
/// node's balance type.
|
||||
///
|
||||
/// This should typically create a mapping between the following ranges:
|
||||
/// - [0, system::MaximumBlockWeight]
|
||||
/// - [Balance::min, Balance::max]
|
||||
///
|
||||
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
|
||||
/// - Setting it to `0` will essentially disable the weight fee.
|
||||
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
|
||||
pub struct WeightToFee;
|
||||
impl Convert<Weight, Balance> for WeightToFee {
|
||||
fn convert(x: Weight) -> Balance {
|
||||
// in Polkadot a weight of 10_000 (smallest non-zero weight) to be mapped to 10^7 units of
|
||||
// fees (1/10 CENT), hence:
|
||||
Balance::from(x).saturating_mul(1_000)
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the given multiplier based on the following formula
|
||||
///
|
||||
/// diff = (target_weight - previous_block_weight)
|
||||
/// v = 0.00004
|
||||
/// next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
|
||||
///
|
||||
/// Where `target_weight` must be given as the `Get` implementation of the `T` generic type.
|
||||
/// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#relay-chain-transaction-fees
|
||||
pub struct TargetedFeeAdjustment<T, R>(rstd::marker::PhantomData<(T, R)>);
|
||||
|
||||
impl<T: Get<Perbill>, R: system::Trait> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T, R> {
|
||||
fn convert(multiplier: Fixed64) -> Fixed64 {
|
||||
let block_weight = <system::Module<R>>::all_extrinsics_weight();
|
||||
let max_weight = MaximumBlockWeight::get();
|
||||
let target_weight = (T::get() * max_weight) as u128;
|
||||
let block_weight = block_weight as u128;
|
||||
|
||||
// determines if the first_term is positive
|
||||
let positive = block_weight >= target_weight;
|
||||
let diff_abs = block_weight.max(target_weight) - block_weight.min(target_weight);
|
||||
// diff is within u32, safe.
|
||||
let diff = Fixed64::from_rational(diff_abs as i64, max_weight as u64);
|
||||
let diff_squared = diff.saturating_mul(diff);
|
||||
|
||||
// 0.00004 = 4/100_000 = 40_000/10^9
|
||||
let v = Fixed64::from_rational(4, 100_000);
|
||||
// 0.00004^2 = 16/10^10 ~= 2/10^9. Taking the future /2 into account, then it is just 1
|
||||
// parts from a billionth.
|
||||
let v_squared_2 = Fixed64::from_rational(1, 1_000_000_000);
|
||||
|
||||
let first_term = v.saturating_mul(diff);
|
||||
// It is very unlikely that this will exist (in our poor perbill estimate) but we are giving
|
||||
// it a shot.
|
||||
let second_term = v_squared_2.saturating_mul(diff_squared);
|
||||
|
||||
if positive {
|
||||
// Note: this is merely bounded by how big the multiplier and the inner value can go,
|
||||
// not by any economical reasoning.
|
||||
let excess = first_term.saturating_add(second_term);
|
||||
multiplier.saturating_add(excess)
|
||||
} else {
|
||||
// Proof: first_term > second_term. Safe subtraction.
|
||||
let negative = first_term - second_term;
|
||||
multiplier.saturating_sub(negative)
|
||||
// despite the fact that apply_to saturates weight (final fee cannot go below 0)
|
||||
// it is crucially important to stop here and don't further reduce the weight fee
|
||||
// multiplier. While at -1, it means that the network is so un-congested that all
|
||||
// transactions have no weight fee. We stop here and only increase if the network
|
||||
// became more busy.
|
||||
.max(Fixed64::from_rational(-1, 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot 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.
|
||||
|
||||
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Common runtime code for Polkadot and Kusama.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub mod attestations;
|
||||
pub mod claims;
|
||||
pub mod parachains;
|
||||
pub mod slot_range;
|
||||
pub mod registrar;
|
||||
pub mod slots;
|
||||
pub mod crowdfund;
|
||||
pub mod impls;
|
||||
|
||||
use primitives::BlockNumber;
|
||||
use sp_runtime::Perbill;
|
||||
use frame_support::{
|
||||
parameter_types, traits::Currency,
|
||||
weights::Weight,
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use staking::StakerStatus;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
pub use sp_runtime::BuildStorage;
|
||||
pub use timestamp::Call as TimestampCall;
|
||||
pub use balances::Call as BalancesCall;
|
||||
pub use attestations::{Call as AttestationsCall, MORE_ATTESTATIONS_IDENTIFIER};
|
||||
pub use parachains::Call as ParachainsCall;
|
||||
|
||||
/// Implementations of some helper traits passed into runtime modules as associated types.
|
||||
pub use impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor, WeightToFee};
|
||||
|
||||
pub type NegativeImbalance<T> = <balances::Module<T> as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;
|
||||
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: BlockNumber = 250;
|
||||
pub const MaximumBlockWeight: Weight = 1_000_000_000;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,155 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot 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.
|
||||
|
||||
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! The SlotRange struct which succinctly handles the ten values that
|
||||
//! represent all sub ranges between 0 and 3 inclusive.
|
||||
|
||||
use rstd::{result, ops::Add, convert::{TryFrom, TryInto}};
|
||||
use sp_runtime::traits::CheckedSub;
|
||||
use codec::{Encode, Decode};
|
||||
|
||||
/// Total number of possible sub ranges of slots.
|
||||
pub const SLOT_RANGE_COUNT: usize = 10;
|
||||
|
||||
/// A compactly represented sub-range from the series (0, 1, 2, 3).
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
|
||||
#[repr(u8)]
|
||||
pub enum SlotRange {
|
||||
/// Sub range from index 0 to index 0 inclusive.
|
||||
ZeroZero = 0,
|
||||
/// Sub range from index 0 to index 1 inclusive.
|
||||
ZeroOne = 1,
|
||||
/// Sub range from index 0 to index 2 inclusive.
|
||||
ZeroTwo = 2,
|
||||
/// Sub range from index 0 to index 3 inclusive.
|
||||
ZeroThree = 3,
|
||||
/// Sub range from index 1 to index 1 inclusive.
|
||||
OneOne = 4,
|
||||
/// Sub range from index 1 to index 2 inclusive.
|
||||
OneTwo = 5,
|
||||
/// Sub range from index 1 to index 3 inclusive.
|
||||
OneThree = 6,
|
||||
/// Sub range from index 2 to index 2 inclusive.
|
||||
TwoTwo = 7,
|
||||
/// Sub range from index 2 to index 3 inclusive.
|
||||
TwoThree = 8,
|
||||
/// Sub range from index 3 to index 3 inclusive.
|
||||
ThreeThree = 9, // == SLOT_RANGE_COUNT - 1
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::fmt::Debug for SlotRange {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let p = self.as_pair();
|
||||
write!(fmt, "[{}..{}]", p.0, p.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl SlotRange {
|
||||
pub fn new_bounded<
|
||||
Index: Add<Output=Index> + CheckedSub + Copy + Ord + From<u32> + TryInto<u32>
|
||||
>(
|
||||
initial: Index,
|
||||
first: Index,
|
||||
last: Index
|
||||
) -> result::Result<Self, &'static str> {
|
||||
if first > last || first < initial || last > initial + 3.into() {
|
||||
return Err("Invalid range for this auction")
|
||||
}
|
||||
let count: u32 = last.checked_sub(&first)
|
||||
.ok_or("range ends before it begins")?
|
||||
.try_into()
|
||||
.map_err(|_| "range too big")?;
|
||||
let first: u32 = first.checked_sub(&initial)
|
||||
.ok_or("range begins too early")?
|
||||
.try_into()
|
||||
.map_err(|_| "start too far")?;
|
||||
match first {
|
||||
0 => match count {
|
||||
0 => Some(SlotRange::ZeroZero),
|
||||
1 => Some(SlotRange::ZeroOne),
|
||||
2 => Some(SlotRange::ZeroTwo),
|
||||
3 => Some(SlotRange::ZeroThree),
|
||||
_ => None,
|
||||
},
|
||||
1 => match count {
|
||||
0 => Some(SlotRange::OneOne),
|
||||
1 => Some(SlotRange::OneTwo),
|
||||
2 => Some(SlotRange::OneThree),
|
||||
_ => None
|
||||
},
|
||||
2 => match count { 0 => Some(SlotRange::TwoTwo), 1 => Some(SlotRange::TwoThree), _ => None },
|
||||
3 => match count { 0 => Some(SlotRange::ThreeThree), _ => None },
|
||||
_ => return Err("range begins too late"),
|
||||
}.ok_or("range ends too late")
|
||||
}
|
||||
|
||||
pub fn as_pair(&self) -> (u8, u8) {
|
||||
match self {
|
||||
SlotRange::ZeroZero => (0, 0),
|
||||
SlotRange::ZeroOne => (0, 1),
|
||||
SlotRange::ZeroTwo => (0, 2),
|
||||
SlotRange::ZeroThree => (0, 3),
|
||||
SlotRange::OneOne => (1, 1),
|
||||
SlotRange::OneTwo => (1, 2),
|
||||
SlotRange::OneThree => (1, 3),
|
||||
SlotRange::TwoTwo => (2, 2),
|
||||
SlotRange::TwoThree => (2, 3),
|
||||
SlotRange::ThreeThree => (3, 3),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn intersects(&self, other: SlotRange) -> bool {
|
||||
let a = self.as_pair();
|
||||
let b = other.as_pair();
|
||||
b.0 <= a.1 && a.0 <= b.1
|
||||
// == !(b.0 > a.1 || a.0 > b.1)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
SlotRange::ZeroZero => 1,
|
||||
SlotRange::ZeroOne => 2,
|
||||
SlotRange::ZeroTwo => 3,
|
||||
SlotRange::ZeroThree => 4,
|
||||
SlotRange::OneOne => 1,
|
||||
SlotRange::OneTwo => 2,
|
||||
SlotRange::OneThree => 3,
|
||||
SlotRange::TwoTwo => 1,
|
||||
SlotRange::TwoThree => 2,
|
||||
SlotRange::ThreeThree => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<usize> for SlotRange {
|
||||
type Error = ();
|
||||
fn try_from(x: usize) -> Result<SlotRange, ()> {
|
||||
Ok(match x {
|
||||
0 => SlotRange::ZeroZero,
|
||||
1 => SlotRange::ZeroOne,
|
||||
2 => SlotRange::ZeroTwo,
|
||||
3 => SlotRange::ZeroThree,
|
||||
4 => SlotRange::OneOne,
|
||||
5 => SlotRange::OneTwo,
|
||||
6 => SlotRange::OneThree,
|
||||
7 => SlotRange::TwoTwo,
|
||||
8 => SlotRange::TwoThree,
|
||||
9 => SlotRange::ThreeThree,
|
||||
_ => return Err(()),
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user