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 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
5 changes: 5 additions & 0 deletions .maintain/config/ropsten_pangolin.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ ethereum:
topics:
# set authorities topic
- 0x91d6d149c7e5354d1c671fe15a5a3332c47a38e15e8ac0339b24af3c1090690f
backing:
address: "0xd5FC8F2eB94fE6AAdeE91c561818e1fF4ea2C041"
topics:
- "0x0c403c4583ff520bad94bf49975b3547a573f7157070022cf8c9a023498d4d11"
- "0xf70fbddcb43e433da621898f5f2628b0a644a77a4389ac2580c5b1de06382fe2"
# (optional) the person who will relay darwinia data to ethereum
# Disable by removing or commenting
relayer:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

15 changes: 14 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 @@ -274,4 +274,17 @@ impl Darwinia {
.await?
.unwrap_or(false))
}

/// Check if should issuing sync
pub async fn verified_issuing(
&self,
block_hash: web3::types::H256,
tx_index: u64,
) -> Result<bool> {
Ok(self
.subxt
.verified_issuing_proof((block_hash.to_fixed_bytes(), tx_index), None)
.await?
.unwrap_or(false))
}
}
67 changes: 67 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::{RedeemErc20, RedeemErc20CallExt, RegisterErc20, RegisterErc20CallExt},
relay::{
Affirm, AffirmCallExt, ConfirmedBlockNumbersStoreExt, EthereumRelay,
PendingRelayHeaderParcelsStoreExt, SetConfirmedParcel,
Expand Down Expand Up @@ -309,4 +310,70 @@ impl Ethereum2Darwinia {
Some(real) => voting_state.contains(real),
}
}

/// register erc20
pub async fn register_erc20(
&self,
account: &Account,
proof: EthereumReceiptProofThing,
) -> Result<H256> {
match &account.0.real {
Some(real) => {
let call = RegisterErc20 {
_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_erc20(&account.0.signer, proof)
.await?),
}
}

/// redeem erc20
pub async fn redeem_erc20(
&self,
account: &Account,
proof: EthereumReceiptProofThing,
) -> Result<H256> {
match &account.0.real {
Some(real) => {
let call = RedeemErc20 {
_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
.redeem_erc20(&account.0.signer, proof)
.await?),
}
}
}
12 changes: 6 additions & 6 deletions primitives/src/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ macro_rules! hex {
let mut s = String::new();
for i in $bytes {
s.push_str(&format!("{:02x}", i));
}
s
}};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These format looks weird

s
}};
}

/// Convert hex string to `Vec<u8>` or `[u8; n]`
Expand All @@ -29,21 +29,21 @@ macro_rules! bytes {
let mut h = $hex;
if h.starts_with("0x") {
h = &h[2..];
}
}

(0..h.len())
.step_by(2)
.map(|i| u8::from_str_radix(&h[i..i + 2], 16))
.collect::<Result<Vec<u8>, _>>()
.unwrap_or_default()
}};
}};

// Convert hex to [u8; $bits]
($hex:expr, $bits:expr) => {{
let mut hash = [0_u8; $bits];
hash.copy_from_slice(&bytes!($hex));
hash
}};
}};
}

/// Implement serde for big array
Expand Down
4 changes: 4 additions & 0 deletions primitives/src/chain/ethereum/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub enum RedeemFor {
Deposit,
/// Redeem for set authorities
SetAuthorities,
/// Redeem for register erc20 token
RegisterErc20Token,
/// Redeem for erc20 token
RedeemErc20Token,
}

impl Default for RedeemFor {
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/frame/ethereum/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use crate::chain::ethereum::{EthereumReceiptProofThing, RedeemFor};
use codec::{Decode, Encode};
use core::marker::PhantomData;
use substrate_subxt_proc_macro::{module, Call, Event, Store};
use substrate_subxt::system::System;
use substrate_subxt::balances::Balances;
use substrate_subxt::system::System;
use substrate_subxt_proc_macro::{module, Call, Event, Store};

/// Ethereum Relay Pallet
#[module]
Expand Down
43 changes: 43 additions & 0 deletions primitives/src/frame/ethereum/issuing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Darwinia Ethereum Issuing
use crate::chain::ethereum::EthereumReceiptProofThing;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use substrate_subxt::{balances::Balances, system::System};
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 erc20 token
#[derive(Clone, Debug, PartialEq, Call, Encode)]
pub struct RegisterErc20<T: EthereumIssuing> {
/// Runtime marker
pub _runtime: PhantomData<T>,
/// Ethereum Receipt Proof
pub proof: EthereumReceiptProofThing,
}

/// Submit redeem erc20 token
#[derive(Clone, Debug, PartialEq, Call, Encode)]
pub struct RedeemErc20<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;
5 changes: 1 addition & 4 deletions primitives/src/frame/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Frame Proxy
use codec::{Decode, Encode};
use core::marker::PhantomData;
use substrate_subxt::{
system::System,
Encoded,
};
use substrate_subxt::{system::System, Encoded};
use substrate_subxt_proc_macro::{module, Call};

/// The subset of the `frame_proxy::Trait` that a client must implement.
Expand Down
5 changes: 1 addition & 4 deletions primitives/src/frame/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::weights::Weight;
use substrate_subxt::{
system::System,
Encoded,
};
use substrate_subxt::{system::System, Encoded};
use substrate_subxt_proc_macro::{module, Call, Store};

/// The subset of the `frame_sudo::Trait` that a client must implement.
Expand Down
5 changes: 1 addition & 4 deletions primitives/src/frame/technical_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

use codec::{Decode, Encode};
use core::marker::PhantomData;
use substrate_subxt::{
system::System,
Encoded,
};
use substrate_subxt::{system::System, Encoded};
use substrate_subxt_proc_macro::{module, Call, Store};

/// The subset of the `frame_council::Trait` that a client must implement.
Expand Down
13 changes: 7 additions & 6 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ pub mod runtime;
#[cfg(test)]
mod tests {
use super::runtime::DarwiniaRuntime;
use substrate_subxt::{
ClientBuilder,
};
use substrate_subxt::ClientBuilder;
// use super::frame::ethereum::relay::PendingRelayHeaderParcelsStoreExt;
use super::frame::technical_committee::MembersStoreExt;

Expand All @@ -60,7 +58,8 @@ mod tests {
.set_url("ws://100.64.200.3:10000")
.skip_type_sizes_check()
.build()
.await.unwrap();
.await
.unwrap();

println!("-----------");
let block_number = 1;
Expand All @@ -78,7 +77,8 @@ mod tests {
.set_url("ws://100.64.200.3:10000")
.skip_type_sizes_check()
.build()
.await.unwrap();
.await
.unwrap();

let members = client.members(None).await.unwrap();
for member in members {
Expand All @@ -92,7 +92,8 @@ mod tests {
.set_url("wss://pangolin-rpc.darwinia.network")
.skip_type_sizes_check()
.build()
.await.unwrap();
.await
.unwrap();

let members = client.members(None).await.unwrap();
for member in members {
Expand Down
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 @@ -144,6 +147,10 @@ impl EthereumBacking for DarwiniaRuntime {
type EthereumTransactionIndex = u64;
}

impl EthereumIssuing for DarwiniaRuntime {
type EthereumTransactionIndex = u64;
}

impl Proxy for DarwiniaRuntime {
type ProxyType = ProxyType;
}
Expand Down
16 changes: 12 additions & 4 deletions src/cmd/info_d2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ impl fmt::Display for TxProofWithMMRProof {
}

/// Get Darwinia to Ethereum Info
pub async fn exec(network: String, txblock: u64, mmrblock: u64, signblock: u64) -> Result<()> {
pub async fn exec(
network: String,
txblock: u64,
mmrblock: u64,
signblock: u64,
istoken: bool,
) -> Result<()> {
std::env::set_var("RUST_LOG", "info,darwinia_bridger");
env_logger::init();

Expand All @@ -72,9 +78,11 @@ pub async fn exec(network: String, txblock: u64, mmrblock: u64, signblock: u64)
.await?;
let event_proof = darwinia
.get_event_proof(
array_bytes::hex2bytes(
"f8860dda3d08046cf2706b92bf7202eaae7a79191c90e76297e0895605b8b457",
)
array_bytes::hex2bytes(if istoken {
"e66f3de22eed97c730152f373193b5a0485b407d88f37d5fd6a2c59e5a696691"
} else {
"f8860dda3d08046cf2706b92bf7202eaae7a79191c90e76297e0895605b8b457"
})
.unwrap(),
header.hash(),
)
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ enum Opt {
/// sign block number
#[structopt(short, long)]
signblock: u64,
/// is token or ring/kton
#[structopt(short, long)]
istoken: bool,
},
/// Sign MMR root
SignMMRRoot {
Expand Down Expand Up @@ -157,7 +160,8 @@ pub async fn exec() -> Result<()> {
txblock,
mmrblock,
signblock,
} => info_d2e::exec(network, txblock, mmrblock, signblock).await?,
istoken,
} => info_d2e::exec(network, txblock, mmrblock, signblock, istoken).await?,
Opt::SignMMRRoot { network, mmrblock } => sign_mmr_root::exec(network, mmrblock).await?,
Opt::AffirmForce { block } => {
affirm_force::exec(block).await?;
Expand Down
Loading