Skip to content

Commit

Permalink
Move helper traits to new pkg plasm-support (#268)
Browse files Browse the repository at this point in the history
* initial

* downgrade log package

* fix formatter

* plasm version updates to 1.7.0
  • Loading branch information
Maar-io authored Feb 8, 2021
1 parent f8400c6 commit 432d568
Show file tree
Hide file tree
Showing 11 changed files with 912 additions and 835 deletions.
1,602 changes: 822 additions & 780 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"frame/dapps-staking",
"frame/plasm-rewards",
"frame/plasm-lockdrop",
"frame/plasm-support",
"frame/plasm-validator",
"frame/ovm",
"frame/ovm/rpc/runtime-api",
Expand Down
6 changes: 4 additions & 2 deletions frame/dapps-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pallet-contracts = { version = "2.0.0", default_features = false }
pallet-staking = { version = "2.0.0", default_features = false }
sp-staking = { version = "2.0.0", default_features = false }

pallet-contract-operator = { path = "../operator", version = "1.4.0", default_features = false }
pallet-plasm-rewards = { path = "../plasm-rewards", version = "1.4.0", default_features = false }
pallet-contract-operator = { path = "../operator", version = "1.7.0", default_features = false }
pallet-plasm-rewards = { path = "../plasm-rewards", version = "1.7.0", default_features = false }
pallet-plasm-support = { path = "../plasm-support", version = "1.7.0", default_features = false }

[dev-dependencies]
wabt = "0.10.0"
Expand Down Expand Up @@ -57,5 +58,6 @@ std = [
"pallet-staking/std",
"pallet-contract-operator/std",
"pallet-plasm-rewards/std",
"pallet-plasm-support/std",
"sp-staking/std",
]
2 changes: 1 addition & 1 deletion frame/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use frame_support::{
IterableStorageDoubleMap, StorageMap, StorageValue,
};
use frame_system::{self as system, ensure_signed};
use pallet_contract_operator::ContractFinder;
use pallet_plasm_rewards::{
traits::{ComputeEraWithParam, EraFinder, ForDappsEraRewardFinder, HistoryDepthFinder},
EraIndex, Releases,
};
use pallet_plasm_support::ContractFinder;
pub use pallet_staking::{Forcing, RewardDestination};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, CheckedSub, Saturating, StaticLookup, Zero},
Expand Down
2 changes: 2 additions & 0 deletions frame/operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ frame-support = { version = "2.0.0", default_features = false }
pallet-timestamp = { version = "2.0.0", default_features = false }
frame-system = { version = "2.0.0", default_features = false }
pallet-contracts = { version = "2.0.0", default_features = false }
pallet-plasm-support = { path = "../plasm-support", version = "1.7.0", default_features = false }

[dev-dependencies]
wabt = "0.10.0"
Expand All @@ -43,4 +44,5 @@ std = [
"pallet-timestamp/std",
"frame-system/std",
"pallet-contracts/std",
"pallet-plasm-support/std",
]
50 changes: 2 additions & 48 deletions frame/operator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use frame_support::dispatch::UnfilteredDispatchable;
use frame_support::{decl_event, decl_module, decl_storage, Parameter};
use frame_system::{ensure_signed, RawOrigin};
use pallet_contracts::{BalanceOf, CodeHash, ContractAddressFor, Gas};
use sp_runtime::{
traits::{MaybeDisplay, MaybeSerialize, Member},
DispatchError,
};
use sp_runtime::traits::{MaybeDisplay, MaybeSerialize, Member};
use sp_std::prelude::*;

pub mod parameters;
Expand All @@ -16,50 +13,7 @@ mod tests;

use crate::parameters::Verifiable;

pub trait ContractFinder<AccountId, Parameter> {
fn is_exists_contract(contract_id: &AccountId) -> bool;
fn operator(contract_id: &AccountId) -> Option<AccountId>;
fn parameters(contract_id: &AccountId) -> Option<Parameter>;
}

pub trait OperatorFinder<AccountId: Parameter> {
fn contracts(operator_id: &AccountId) -> Vec<AccountId>;
}

pub trait TransferOperator<AccountId: Parameter>: OperatorFinder<AccountId> {
/// Changes an operator for identified contracts with verify.
fn transfer_operator(
current_operator: AccountId,
contracts: Vec<AccountId>,
new_operator: AccountId,
) -> Result<(), DispatchError> {
Self::verify_transfer_operator(&current_operator, &contracts)?;
Self::force_transfer_operator(current_operator, contracts, new_operator);
Ok(())
}

fn verify_transfer_operator(
current_operator: &AccountId,
contracts: &Vec<AccountId>,
) -> Result<(), DispatchError> {
let operate_contracts = Self::contracts(current_operator);

// check the actually operate the contract.
if !contracts.iter().all(|c| operate_contracts.contains(c)) {
Err(DispatchError::Other(
"The sender don't operate the contracts address.",
))?
}
Ok(())
}

/// Force Changes an operator for identified contracts without verify.
fn force_transfer_operator(
current_operator: AccountId,
contracts: Vec<AccountId>,
new_operator: AccountId,
);
}
use pallet_plasm_support::{ContractFinder, OperatorFinder, TransferOperator};

/// The module's configuration trait.
pub trait Trait: pallet_contracts::Trait {
Expand Down
26 changes: 26 additions & 0 deletions frame/plasm-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "pallet-plasm-support"
version = "1.7.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://docs.plasmnet.io/"
repository = "https://github.com/staketechnologies/Plasm/"
description = "FRAME support pallet for Plasm traits"

[dependencies]
sp-runtime = { version = "2.0.0", default_features = false }
frame-support = { version = "2.0.0", default_features = false }
sp-std = { version = "2.0.0", default_features = false }


[dev-dependencies]


[features]
default = ["std"]
std = [
"sp-runtime/std",
"frame-support/std",
"sp-std/std",
]
50 changes: 50 additions & 0 deletions frame/plasm-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::Parameter;
use sp_runtime::DispatchError;
use sp_std::prelude::*;

pub trait ContractFinder<AccountId, Parameter> {
fn is_exists_contract(contract_id: &AccountId) -> bool;
fn operator(contract_id: &AccountId) -> Option<AccountId>;
fn parameters(contract_id: &AccountId) -> Option<Parameter>;
}

pub trait OperatorFinder<AccountId: Parameter> {
fn contracts(operator_id: &AccountId) -> Vec<AccountId>;
}

pub trait TransferOperator<AccountId: Parameter>: OperatorFinder<AccountId> {
/// Changes an operator for identified contracts with verify.
fn transfer_operator(
current_operator: AccountId,
contracts: Vec<AccountId>,
new_operator: AccountId,
) -> Result<(), DispatchError> {
Self::verify_transfer_operator(&current_operator, &contracts)?;
Self::force_transfer_operator(current_operator, contracts, new_operator);
Ok(())
}

fn verify_transfer_operator(
current_operator: &AccountId,
contracts: &Vec<AccountId>,
) -> Result<(), DispatchError> {
let operate_contracts = Self::contracts(current_operator);

// check the actually operate the contract.
if !contracts.iter().all(|c| operate_contracts.contains(c)) {
Err(DispatchError::Other(
"The sender don't operate the contracts address.",
))?
}
Ok(())
}

/// Force Changes an operator for identified contracts without verify.
fn force_transfer_operator(
current_operator: AccountId,
contracts: Vec<AccountId>,
new_operator: AccountId,
);
}
2 changes: 1 addition & 1 deletion frame/plasm-validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ frame-support = { version = "2.0.0", default_features = false }
frame-system = { version = "2.0.0", default_features = false }
sp-staking = { version = "2.0.0", default_features = false }

pallet-plasm-rewards = { path = "../plasm-rewards", version = "1.4.0", default_features = false }
pallet-plasm-rewards = { path = "../plasm-rewards", version = "1.7.0", default_features = false }

[dev-dependencies]
hex-literal = "0.2.1"
Expand Down
4 changes: 2 additions & 2 deletions frame/trading/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ frame-support = { version = "2.0.0", default_features = false }
pallet-timestamp = { version = "2.0.0", default_features = false }
frame-system = { version = "2.0.0", default_features = false }

pallet-contract-operator = { path = "../operator", version = "1.4.0", default_features = false }
pallet-plasm-support = { path = "../plasm-support", version = "1.7.0", default_features = false }

[features]
default = ["std"]
Expand All @@ -35,5 +35,5 @@ std = [
"frame-support/std",
"pallet-timestamp/std",
"frame-system/std",
"pallet-contract-operator/std",
"pallet-plasm-support/std",
]
2 changes: 1 addition & 1 deletion frame/trading/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_support::{
traits::{Currency, ExistenceRequirement, LockIdentifier, LockableCurrency, WithdrawReasons},
};
use frame_system::ensure_signed;
use pallet_contract_operator::{OperatorFinder, TransferOperator};
use pallet_plasm_support::{OperatorFinder, TransferOperator};
use sp_std::prelude::*;

#[cfg(feature = "std")]
Expand Down

0 comments on commit 432d568

Please sign in to comment.