Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing for Eth Backing Module #172

Merged
merged 17 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
11 changes: 10 additions & 1 deletion Cargo.lock

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

17 changes: 15 additions & 2 deletions srml/eth-backing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
hex = { version = "0.4", default-features = false }
#hex = { version = "0.4", default-features = false }
serde = { version = "1.0.101", optional = true }

# github.com
Expand All @@ -28,12 +28,25 @@ sr-eth-primitives = { path = "../../core/sr-eth-primitives", default-features =

[dev-dependencies]
hex-literal = "0.2.1"
rustc-hex = "2.0"

balances = { package = "darwinia-balances", path = '../balances' }
kton = { package = "darwinia-kton", path = "../kton" }
staking = { package = "darwinia-staking", path = "../staking" }

authorship = { package = "srml-authorship", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
session = { package = "srml-session",git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
sr-staking-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
phragmen = { package = "substrate-phragmen", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
runtime_io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git"}


[features]
default = ["std"]
std = [
"codec/std",
"hex/std",
# "hex/std",
"serde/std",

"ethabi/std",
Expand Down
79 changes: 47 additions & 32 deletions srml/eth-backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//use codec::{Decode, Encode};
use ethabi::{Event as EthEvent, EventParam as EthEventParam, ParamType, RawLog};
use rstd::{borrow::ToOwned, convert::TryFrom, marker::PhantomData, result, vec}; // fmt::Debug
use sr_primitives::traits::{SaturatedConversion, Saturating};
use support::{decl_event, decl_module, decl_storage, ensure, traits::Currency, traits::OnUnbalanced}; // dispatch::Result,
use sr_primitives::traits::{CheckedSub, SaturatedConversion};
use support::{decl_event, decl_module, decl_storage, ensure, fail, traits::Currency, traits::OnUnbalanced}; // dispatch::Result,
use system::ensure_signed; // Convert,

//use sr_primitives::RuntimeDebug;
Expand All @@ -33,6 +33,11 @@ type KtonBalanceOf<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::Ac
type PositiveImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::PositiveImbalance;
//type NegativeImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;

#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

pub trait Trait: timestamp::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type EthRelay: VerifyEthReceipts;
Expand Down Expand Up @@ -68,16 +73,6 @@ decl_event! {
}
}

impl<T: Trait> Module<T> {
pub fn adjust_deposit_value() {
unimplemented!()
}

// fn _release(_dest: &T::AccountId, _value: RingBalanceOf<T>) -> Result {
// unimplemented!()
// }
}

decl_module! {
pub struct Module<T: Trait> for enum Call
where
Expand All @@ -100,9 +95,14 @@ decl_module! {
T::RingReward::on_unbalanced(redeemed_positive_imbalance_ring);

RingProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<RingLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_ring);
});

if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
}

// event KtonBurndropTokens(address indexed token, address indexed owner, uint amount, bytes data)
Expand All @@ -121,9 +121,14 @@ decl_module! {
T::KtonReward::on_unbalanced(redeemed_positive_imbalance_kton);

KtonProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<KtonLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_kton);
});

if let Some(new_kton_locked) = Self::kton_locked().checked_sub(&redeemed_kton) {
<KtonLocked<T>>::mutate(|l| {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
*l = new_kton_locked;
});
} else {
fail!("KTON Locked - NO SUFFICIENT BACKING ASSETS")
}
}

// https://github.com/evolutionlandorg/bank
Expand Down Expand Up @@ -185,7 +190,7 @@ decl_module! {
.ok_or("Convert to Int - FAILED")?;
let redeemed_amount = {
// TODO: div 10**18 and mul 10**9
let amount = result.params[2]
let amount = result.params[5]
.value
.clone()
.to_uint()
Expand All @@ -195,14 +200,14 @@ decl_module! {
Balance::try_from(amount)?
};
let darwinia_account = {
let raw_sub_key = result.params[3]
let raw_sub_key = result.params[6]
.value
.clone()
.to_bytes()
.ok_or("Convert to Bytes - FAILED")?;
let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;
// let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

T::DetermineAccountId::account_id_for(&decoded_sub_key)?
T::DetermineAccountId::account_id_for(&raw_sub_key)?
};
let redeemed_ring = <RingBalanceOf<T>>::saturated_from(redeemed_amount);
let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&darwinia_account, redeemed_ring)?;
Expand All @@ -221,9 +226,13 @@ decl_module! {
)?;

DepositProofVerified::insert((proof_record.header_hash, proof_record.index), proof_record);
<RingLocked<T>>::mutate(|l| {
*l = l.saturating_sub(redeemed_ring);
});
if let Some(new_ring_locked) = Self::ring_locked().checked_sub(&redeemed_ring) {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
<RingLocked<T>>::mutate(|l| {
*l = new_ring_locked;
});
} else {
fail!("RING Locked - NO SUFFICIENT BACKING ASSETS")
}
}
}
}
Expand Down Expand Up @@ -290,17 +299,17 @@ impl<T: Trait> Module<T> {
.clone()
.to_bytes()
.ok_or("Convert to Bytes - FAILED")?;
let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

T::DetermineAccountId::account_id_for(&decoded_sub_key)?
// let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

T::DetermineAccountId::account_id_for(&raw_sub_key)?
};

Ok((darwinia_account, redeemed_amount))
}
}

pub trait AccountIdFor<AccountId> {
// fn contract_address_for(code_hash: &CodeHash, data: &[u8], origin: &AccountId) -> AccountId;
fn account_id_for(decoded_sub_key: &[u8]) -> result::Result<AccountId, &'static str>;
}

Expand All @@ -311,12 +320,12 @@ where
T::AccountId: rstd::convert::From<[u8; 32]> + AsRef<[u8]>,
{
fn account_id_for(decoded_sub_key: &[u8]) -> result::Result<T::AccountId, &'static str> {
if decoded_sub_key.len() != 32 {
return Err("Address Length - MISMATCHED");
}
ensure!(decoded_sub_key.len() == 33, "Address Length - MISMATCHED");

ensure!(decoded_sub_key[0] == 42, "Pubkey Prefix - MISMATCHED");

let mut r = [0u8; 32];
r.copy_from_slice(&decoded_sub_key[..]);
r.copy_from_slice(&decoded_sub_key[1..]);

let darwinia_account = r.into();

Expand All @@ -325,3 +334,9 @@ where
Ok(darwinia_account)
}
}

impl<T: Trait> Module<T> {
pub fn adjust_deposit_value() {
unimplemented!()
}
}
Loading