Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
register token to darwinia
  • Loading branch information
xiaoch05 committed Mar 9, 2021
commit a4775991b37df2eff40f6e88dacf68b5c452e383
11 changes: 10 additions & 1 deletion darwinia/src/darwinia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use substrate_subxt::{

use primitives::{
//todo move to e2d
frame::ethereum::backing::VerifiedProofStoreExt,
frame::ethereum::{backing::VerifiedProofStoreExt, issuing::VerifiedIssuingProofStoreExt},
runtime::DarwiniaRuntime,
};

Expand Down Expand Up @@ -263,4 +263,13 @@ impl Darwinia {
.await?
.unwrap_or(false))
}

/// Check if should issuing sync
pub async fn verified_issuing(&self, block_hash: H256, tx_index: u64) -> Result<bool> {
Ok(self
.subxt
.verified_issuing_proof((block_hash.to_fixed_bytes(), tx_index), None)
.await?
.unwrap_or(false))
}
}
34 changes: 34 additions & 0 deletions darwinia/src/from_ethereum/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use primitives::{
ethereum::{
backing::{Redeem, RedeemCallExt},
game::{AffirmationsStoreExt, EthereumRelayerGame},
issuing::{RegisterOrIssuingErc20, RegisterOrIssuingErc20CallExt},
relay::{
Affirm, AffirmCallExt, ConfirmedBlockNumbersStoreExt, EthereumRelay,
PendingRelayHeaderParcelsStoreExt, SetConfirmedParcel,
Expand Down Expand Up @@ -286,4 +287,37 @@ impl Ethereum2Darwinia {
Some(real) => voting_state.contains(real),
}
}

/// register_or_issuing_erc20
pub async fn register_or_issuing_erc20(
&self,
account: &Account,
proof: EthereumReceiptProofThing,
) -> Result<H256> {
match &account.0.real {
Some(real) => {
let call = RegisterOrIssuingErc20 {
_runtime: PhantomData::default(),
proof,
};

let ex = self.darwinia.subxt.encode(call).unwrap();
Ok(self
.darwinia
.subxt
.proxy(
&account.0.signer,
real.clone(),
Some(ProxyType::EthereumBridge),
&ex,
)
.await?)
}
None => Ok(self
.darwinia
.subxt
.register_or_issuing_erc20(&account.0.signer, proof)
.await?),
}
}
}
2 changes: 2 additions & 0 deletions primitives/src/chain/ethereum/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum RedeemFor {
Deposit,
/// Redeem for set authorities
SetAuthorities,
/// Redeem for register token
RegisterToken,
}

impl Default for RedeemFor {
Expand Down
37 changes: 37 additions & 0 deletions primitives/src/frame/ethereum/issuing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Darwinia Ethereum Issuing
use crate::chain::ethereum::EthereumReceiptProofThing;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use substrate_subxt::{
balances::{Balances, BalancesEventsDecoder},
system::{System, SystemEventsDecoder},
};
use substrate_subxt_proc_macro::{module, Call, Store};

/// Ethereum Issuing Pallet
#[module]
pub trait EthereumIssuing: System + Balances {
/// Ethereum transaction index
type EthereumTransactionIndex: 'static + Encode + Decode + Send + Default + Clone + Sync;
}

// Call

/// Submit register or lock call
#[derive(Clone, Debug, PartialEq, Call, Encode)]
pub struct RegisterOrIssuingErc20<T: EthereumIssuing> {
/// Runtime marker
pub _runtime: PhantomData<T>,
/// Ethereum Receipt Proof
pub proof: EthereumReceiptProofThing,
}

/// verified proof Storage
#[derive(Clone, Debug, Eq, PartialEq, Store, Decode, Encode)]
pub struct VerifiedIssuingProof<T: EthereumIssuing> {
#[store(returns = Option<bool>)]
/// Receipt tx hash
pub map: ([u8; 32], u64),
/// Runtime marker
pub _runtime: PhantomData<T>,
}
1 change: 1 addition & 0 deletions primitives/src/frame/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

pub mod backing;
pub mod game;
pub mod issuing;
pub mod relay;
9 changes: 8 additions & 1 deletion primitives/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::{
},
frame::{
bridge::relay_authorities::EthereumRelayAuthorities,
ethereum::{backing::EthereumBacking, game::EthereumRelayerGame, relay::EthereumRelay},
ethereum::{
backing::EthereumBacking, game::EthereumRelayerGame, issuing::EthereumIssuing,
relay::EthereumRelay,
},
proxy::Proxy,
sudo::Sudo,
technical_committee::TechnicalCommittee,
Expand Down Expand Up @@ -106,6 +109,10 @@ impl EthereumBacking for DarwiniaRuntime {
type EthereumTransactionIndex = u64;
}

impl EthereumIssuing for DarwiniaRuntime {
type EthereumTransactionIndex = u64;
}

impl Proxy for DarwiniaRuntime {
type ProxyType = ProxyType;
}
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct EthereumContract {
pub issuing: EthereumContractTuple,
/// Relay Contract
pub relay: EthereumContractTuple,
/// Backing Contract
pub backing: EthereumContractTuple,
}

/// Ethereum Relayer
Expand Down
34 changes: 28 additions & 6 deletions src/service/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ pub struct ContractAddress {
pub bank: H256,
/// relay
pub relay: H256,
/// register
pub register: H256,
}

/// Ethereum transaction service
///
/// This service can check and scan darwinia txs in Ethereum
pub struct EthereumService {
contracts: ContractAddress,
filters: [FilterBuilder; 3],
filters: [FilterBuilder; 4],
web3: Web3<Http>,
darwinia: Darwinia,
scan_from: u64,
Expand Down Expand Up @@ -146,7 +148,7 @@ impl EthereumService {
async fn do_scan(
web3: Web3<Http>,
contracts: ContractAddress,
filters: [FilterBuilder; 3],
filters: [FilterBuilder; 4],
from: u64,
to: u64,
) -> BridgerResult<Vec<EthereumTransaction>> {
Expand Down Expand Up @@ -195,6 +197,15 @@ impl EthereumService {
block,
index,
}
} else if l.topics.contains(&contracts.register) {
EthereumTransaction {
tx_hash: EthereumTransactionHash::RegisterToken(
l.transaction_hash.unwrap_or_default(),
),
block_hash: l.block_hash.unwrap_or_default(),
block,
index,
}
} else {
EthereumTransaction {
tx_hash: EthereumTransactionHash::Deposit(
Expand All @@ -217,7 +228,7 @@ impl EthereumService {
darwinia: Darwinia,
web3: Web3<Http>,
contracts: ContractAddress,
filters: [FilterBuilder; 3],
filters: [FilterBuilder; 4],
scan_from: u64,
relay_service: Recipient<MsgBlockNumber>,
redeem_service: Recipient<MsgEthereumTransaction>,
Expand Down Expand Up @@ -256,7 +267,9 @@ impl EthereumService {
}

for tx in &txs {
if darwinia.verified(tx.block_hash, tx.index).await? {
if darwinia.verified(tx.block_hash, tx.index).await?
|| darwinia.verified_issuing(tx.block_hash, tx.index).await?
{
trace!(
" This ethereum tx {:?} has already been redeemed.",
tx.enclosed_hash()
Expand Down Expand Up @@ -290,19 +303,23 @@ impl EthereumService {
let ring_topics = contract.ring.topics.clone().unwrap();
let kton_topics = contract.kton.topics.clone().unwrap();
let relay_topics = contract.relay.topics.clone().unwrap();
let backing_topics = contract.backing.topics.clone().unwrap();
ContractAddress {
bank: H256::from_slice(&bytes!(bank_topics[0].as_str())),
kton: H256::from_slice(&bytes!(kton_topics[0].as_str())),
ring: H256::from_slice(&bytes!(ring_topics[0].as_str())),
relay: H256::from_slice(&bytes!(relay_topics[0].as_str())),
register: H256::from_slice(&bytes!(backing_topics[0].as_str())),
//lock: H256::from_slice(&bytes!(backing_topics[1].as_str())),
}
}

/// Parse log filter from config
pub fn parse_filter(config: &Settings) -> [FilterBuilder; 3] {
pub fn parse_filter(config: &Settings) -> [FilterBuilder; 4] {
let filters = [
&config.ethereum.contract.bank,
&config.ethereum.contract.issuing,
&config.ethereum.contract.backing,
&config.ethereum.contract.relay,
]
.iter()
Expand All @@ -320,7 +337,12 @@ impl EthereumService {
.topics(Some(topics), None, None, None)
})
.collect::<Vec<FilterBuilder>>();
[filters[0].clone(), filters[1].clone(), filters[2].clone()]
[
filters[0].clone(),
filters[1].clone(),
filters[2].clone(),
filters[3].clone(),
]
}

/// get_latest_block_number
Expand Down
13 changes: 13 additions & 0 deletions src/service/extrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ impl ExtrinsicsService {
}
}
}
RedeemFor::RegisterToken => {
if let Some(ethereum2darwinia) = &ethereum2darwinia {
if let Some(relayer) = &ethereum2darwinia_relayer {
let ex_hash = ethereum2darwinia
.register_or_issuing_erc20(&relayer, proof)
.await?;
info!(
"register or issuing token tx {:?} with extrinsic {:?}",
ethereum_tx.tx_hash, ex_hash
);
}
}
}
_ => {
if let Some(ethereum2darwinia) = &ethereum2darwinia {
if let Some(relayer) = &ethereum2darwinia_relayer {
Expand Down
4 changes: 4 additions & 0 deletions src/service/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum EthereumTransactionHash {
Token(H256),
/// SetAuthoritiesEvent
SetAuthorities(H256),
/// RegisterToken
RegisterToken(H256),
}

/// Reedeemable Ethereum transaction
Expand All @@ -43,6 +45,7 @@ impl EthereumTransaction {
EthereumTransactionHash::Token(h) => h,
EthereumTransactionHash::Deposit(h) => h,
EthereumTransactionHash::SetAuthorities(h) => h,
EthereumTransactionHash::RegisterToken(h) => h,
}
}
}
Expand Down Expand Up @@ -188,6 +191,7 @@ impl RedeemService {
EthereumTransactionHash::Deposit(_) => RedeemFor::Deposit,
EthereumTransactionHash::Token(_) => RedeemFor::Token,
EthereumTransactionHash::SetAuthorities(_) => RedeemFor::SetAuthorities,
EthereumTransactionHash::RegisterToken(_) => RedeemFor::RegisterToken,
};

let ex = Extrinsic::Redeem(redeem_for, proof, tx);
Expand Down