Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/node/runtime/pangolin/polkadot-compatible-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
"parent_mmr_root": "Hash"
},
"__[primitives.relay]__": {},
"OpCode": "[u8; 4; OpCode]",
"Term": "u32",
"RelayHeaderId": "EthereumBlockNumber",
"RelayHeaderParcel": "EthereumRelayHeaderParcel",
Expand Down
10 changes: 10 additions & 0 deletions bin/node/runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ use constants::*;
use darwinia_balances_rpc_runtime_api::RuntimeDispatchInfo as BalancesRuntimeDispatchInfo;
use darwinia_evm::Runner;
use darwinia_header_mmr_rpc_runtime_api::RuntimeDispatchInfo as HeaderMMRRuntimeDispatchInfo;
use darwinia_relay_primitives::relay_authorities::OpCode;
use darwinia_staking::EraIndex;
use darwinia_staking_rpc_runtime_api::RuntimeDispatchInfo as StakingRuntimeDispatchInfo;
use drml_primitives::*;
Expand Down Expand Up @@ -978,6 +979,8 @@ impl darwinia_crab_backing::Trait for Runtime {
parameter_types! {
pub const EthereumBackingModuleId: ModuleId = ModuleId(*b"da/ethbk");
pub const EthereumBackingFeeModuleId: ModuleId = ModuleId(*b"da/ethfe");
pub const RingLockLimit: Balance = 10_000_000 * COIN;
pub const KtonLockLimit: Balance = 1000 * COIN;
pub const AdvancedFee: Balance = 50 * COIN;
pub const SyncReward: Balance = 1000 * COIN;
}
Expand All @@ -990,6 +993,8 @@ impl darwinia_ethereum_backing::Trait for Runtime {
type OnDepositRedeem = Staking;
type RingCurrency = Ring;
type KtonCurrency = Kton;
type RingLockLimit = RingLockLimit;
type KtonLockLimit = KtonLockLimit;
type AdvancedFee = AdvancedFee;
type SyncReward = SyncReward;
type EcdsaAuthorities = EthereumRelayAuthorities;
Expand Down Expand Up @@ -1085,6 +1090,10 @@ parameter_types! {
pub const EthereumRelayAuthoritiesLockId: LockIdentifier = *b"ethrauth";
pub const EthereumRelayAuthoritiesTermDuration: BlockNumber = 30 * DAYS;
pub const MaxCandidates: usize = 7;
pub const OpCodes: (OpCode, OpCode) = (
[71, 159, 189, 249],
[180, 188, 244, 151]
);
pub const SignThreshold: Perbill = Perbill::from_percent(60);
pub const SubmitDuration: BlockNumber = 100;
}
Expand All @@ -1099,6 +1108,7 @@ impl darwinia_relay_authorities::Trait<EthereumRelayAuthoritiesInstance> for Run
type ResetOrigin = ApproveOrigin;
type DarwiniaMMR = HeaderMMR;
type Sign = EthereumBacking;
type OpCodes = OpCodes;
type SignThreshold = SignThreshold;
type SubmitDuration = SubmitDuration;
type WeightInfo = ();
Expand Down
5 changes: 5 additions & 0 deletions bin/node/runtime/pangolin/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"parent_mmr_root": "Hash"
},
"__[primitives.relay]__": {},
"OpCode": "[u8; 4; OpCode]",
"Term": "u32",
"RelayHeaderId": "EthereumBlockNumber",
"RelayHeaderParcel": "EthereumRelayHeaderParcel",
Expand All @@ -226,6 +227,10 @@
"stake": "Balance",
"term": "BlockNumber"
},
"ScheduledAuthoritiesChange": {
"next_authorities": "Vec<RelayAuthorityT>",
"deadline": "BlockNumber"
},
"MMRRoot": "Hash",
"__[pangolin.runtime]__": {
"ProxyType": {
Expand Down
30 changes: 17 additions & 13 deletions frame/bridge/ethereum/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,21 @@ use types::*;
pub trait Trait: frame_system::Trait {
/// The ethereum backing module id, used for deriving its sovereign account ID.
type ModuleId: Get<ModuleId>;

type FeeModuleId: Get<ModuleId>;

type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

type RedeemAccountId: From<[u8; 32]> + Into<Self::AccountId>;

type EthereumRelay: EthereumReceipt<Self::AccountId, RingBalance<Self>>;

type OnDepositRedeem: OnDepositRedeem<Self::AccountId, RingBalance<Self>>;

type RingCurrency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;

type KtonCurrency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;

type RingLockLimit: Get<RingBalance<Self>>;
type KtonLockLimit: Get<KtonBalance<Self>>;
type AdvancedFee: Get<RingBalance<Self>>;

type SyncReward: Get<RingBalance<Self>>;

type EcdsaAuthorities: RelayAuthorityProtocol<Self::BlockNumber, Signer = EthereumAddress>;

/// Weight information for the extrinsics in this pallet.
Expand Down Expand Up @@ -150,8 +146,8 @@ decl_error! {
AddressCF,
/// Asset - ALREADY REDEEMED
AssetAR,
/// Authorities Set - ALREADY SYNCED
AuthoritiesSetAR,
/// Authorities Change - ALREADY SYNCED
AuthoritiesChangeAR,
/// EthereumReceipt Proof - INVALID
ReceiptProofInv,
/// Eth Log - PARSING FAILED
Expand All @@ -167,6 +163,10 @@ decl_error! {
// FeeIns,
/// Redeem - DISABLED
RedeemDis,
/// Ring Lock - LIMITED
RingLockLim,
/// Kton Lock - LIMITED
KtonLockLim,
}
}

Expand Down Expand Up @@ -274,6 +274,8 @@ decl_module! {
if !ring_value.is_zero() {
let ring_to_lock = ring_value.min(T::RingCurrency::usable_balance(&user));

ensure!(ring_to_lock < T::RingLockLimit::get(), <Error<T>>::RingLockLim);

T::RingCurrency::transfer(&user, &Self::account_id(), ring_to_lock, KeepAlive)?;

let raw_event = RawEvent::LockRing(
Expand All @@ -293,6 +295,8 @@ decl_module! {
if !kton_value.is_zero() {
let kton_to_lock = kton_value.min(T::KtonCurrency::usable_balance(&user));

ensure!(kton_to_lock < T::KtonLockLimit::get(), <Error<T>>::KtonLockLim);

T::KtonCurrency::transfer(&user, &Self::account_id(), kton_to_lock, KeepAlive)?;

let raw_event = RawEvent::LockKton(
Expand All @@ -311,23 +315,23 @@ decl_module! {
}

if locked {
T::EcdsaAuthorities::new_mmr_to_sign((
T::EcdsaAuthorities::schedule_mmr_root((
<frame_system::Module<T>>::block_number().saturated_into()
/ 10 * 10 + 10
).saturated_into());
}
}

#[weight = 10_000_000]
fn sync_authorities_set(origin, proof: EthereumReceiptProofThing<T>) {
fn sync_authorities_change(origin, proof: EthereumReceiptProofThing<T>) {
let bridger = ensure_signed(origin)?;
let tx_index = T::EthereumRelay::gen_receipt_index(&proof);

ensure!(!VerifiedProof::contains_key(tx_index), <Error<T>>::AuthoritiesSetAR);
ensure!(!VerifiedProof::contains_key(tx_index), <Error<T>>::AuthoritiesChangeAR);

let (term, authorities, beneficiary) = Self::parse_authorities_set_proof(&proof)?;

T::EcdsaAuthorities::check_sync_result(term, authorities)?;
T::EcdsaAuthorities::check_authorities_change_to_sync(term, authorities)?;

let fee_account = Self::fee_account_id();
let sync_reward = T::SyncReward::get().min(
Expand All @@ -344,7 +348,7 @@ decl_module! {
)?;
}

T::EcdsaAuthorities::finish_authorities_change();
T::EcdsaAuthorities::sync_authorities_change();

VerifiedProof::insert(tx_index, true);
}
Expand Down
10 changes: 7 additions & 3 deletions frame/bridge/ethereum/backing/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ macro_rules! decl_tests {
impl RelayAuthorityProtocol<BlockNumber> for EcdsaAuthorities {
type Signer = EthereumAddress;

fn new_mmr_to_sign(_: BlockNumber) {}
fn schedule_mmr_root(_: BlockNumber) {}

fn check_sync_result(_: Term, _: Vec<Self::Signer>) -> DispatchResult {
fn check_authorities_change_to_sync(_: Term, _: Vec<Self::Signer>) -> DispatchResult {
Ok(())
}

fn finish_authorities_change() {}
fn sync_authorities_change() {}
}
parameter_types! {
pub const EthereumBackingModuleId: ModuleId = ModuleId(*b"da/backi");
pub const EthereumBackingFeeModuleId: ModuleId = ModuleId(*b"da/ethfe");
pub const RingLockLimit: Balance = 1000;
pub const KtonLockLimit: Balance = 1000;
pub const AdvancedFee: Balance = 1;
}
impl Trait for Test {
Expand All @@ -84,6 +86,8 @@ macro_rules! decl_tests {
type OnDepositRedeem = Staking;
type RingCurrency = Ring;
type KtonCurrency = Kton;
type RingLockLimit = RingLockLimit;
type KtonLockLimit = KtonLockLimit;
type AdvancedFee = AdvancedFee;
type SyncReward = ();
type EcdsaAuthorities = EcdsaAuthorities;
Expand Down
35 changes: 32 additions & 3 deletions frame/bridge/ethereum/backing/src/test_with_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,35 @@ fn lock_should_work() {
});
}

#[test]
fn lock_limit_should_work() {
ExtBuilder::default().build().execute_with(|| {
let account = Default::default();
let lock_balance = 10_000;
let _ = Ring::deposit_creating(&account, lock_balance);
let _ = Kton::deposit_creating(&account, lock_balance);

assert_err!(
EthereumBacking::lock(
Origin::signed(account.clone()),
lock_balance,
0,
Default::default()
),
<Error<Test>>::RingLockLim
);
assert_err!(
EthereumBacking::lock(
Origin::signed(account.clone()),
0,
lock_balance,
Default::default()
),
<Error<Test>>::KtonLockLim
);
});
}

#[test]
fn verify_signature_should_work() {
assert!(EthereumBacking::verify_signature(
Expand All @@ -456,17 +485,17 @@ fn verify_new_authorities() {
EthereumRelay::confirm_relay_header_parcel_with_reason(relay_header_parcel, vec![]);

assert_ok!(
EthereumBacking::sync_authorities_set(
EthereumBacking::sync_authorities_change(
Origin::signed(Default::default()),
test_receipt_proof_thing.clone(),
)
);
assert_err!(
EthereumBacking::sync_authorities_set(
EthereumBacking::sync_authorities_change(
Origin::signed(Default::default()),
test_receipt_proof_thing,
),
<Error<Test>>::AuthoritiesSetAR
<Error<Test>>::AuthoritiesChangeAR
);
});
}
2 changes: 2 additions & 0 deletions frame/bridge/relay-authorities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ sp-runtime = { default-features = false, git = "https://github.com/darwinia-n
sp-std = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", branch = "common-library" }

[dev-dependencies]
# crates
array-bytes = { version = "0.3.0" }
# darwinia
darwinia-balances = { path = "../../balances" }
darwinia-support = { path = "../../support" }
Expand Down
Loading