Skip to content

Commit

Permalink
Merge pull request #938 from crustio/parachain/fix-bridge-transfer
Browse files Browse the repository at this point in the history
Fix bridge_transfer transaction from mainnet to crustpara not showing up in SubScan, together with other minor improvements.
  • Loading branch information
wuhaixian1984 authored Mar 5, 2024
2 parents 783b58e + 615a434 commit 7c010e4
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 64 deletions.
2 changes: 1 addition & 1 deletion crust-collator-state-2008
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x00000000000000000000000000000000000000000000000000000000000000000064a6841f4eaa8a1fc77fc43dcd2721aedbbb24fb4cf9d59c183c9b02fe84a28703170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
0x00000000000000000000000000000000000000000000000000000000000000000065ee6e4a72e5288aa476a38f9e27e0cba172d27f7344db3f14beb112ba926de203170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
2 changes: 1 addition & 1 deletion crust-collator-state-2012
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x000000000000000000000000000000000000000000000000000000000000000000a859850cdbb8db78e33da9931e6e53a30a240db81d27a8142607009dc956210703170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
0x0000000000000000000000000000000000000000000000000000000000000000004b1e5219eb9eb1f668da45a4ef5a05194128399870dfec2ad7ccab574501a8f803170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
2 changes: 1 addition & 1 deletion crust-collator-state-staging
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x0000000000000000000000000000000000000000000000000000000000000000005005999bc7f6a4fbc2c30b616c55e63dd90c8a60b211243404ac5a67f3e97ee103170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
0x0000000000000000000000000000000000000000000000000000000000000000008a89bbca833bf32652211efc644a62f156da7fe5b12f8b59821d52c36b7ad7f503170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c11131400
2 changes: 1 addition & 1 deletion crust-collator-wasm-2008

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crust-collator-wasm-2012

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crust-collator-wasm-staging

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions crust-collator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch =
pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }

[features]
enable_sudo = []
31 changes: 26 additions & 5 deletions crust-collator/pallets/bridge-transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ mod types {

#[frame_support::pallet]
pub mod pallet {
use crate::types::{
BalanceOf,
};
use crate::types::BalanceOf;
use frame_support::{
pallet_prelude::*,
traits::{
Expand All @@ -40,18 +38,23 @@ pub mod pallet {
},
};
use frame_system::pallet_prelude::*;
use frame_support::PalletId;
use frame_support::sp_runtime::traits::Saturating;
use frame_support::sp_runtime::SaturatedConversion;
use sp_core::U256;
use sp_std::convert::TryInto;
use sp_std::vec::Vec;
use sp_runtime::traits::StaticLookup;
use sp_runtime::traits::AccountIdConversion;
use cstrml_bridge as bridge;
use crate::weights::WeightInfo;

#[pallet::pallet]
pub struct Pallet<T>(_);

/// The bridge-transfer's pallet id
pub const PALLET_ID: PalletId = PalletId(*b"brdgtxfr");

/// Tracks current relayer set
#[pallet::storage]
#[pallet::getter(fn bridge_fee)]
Expand Down Expand Up @@ -175,7 +178,7 @@ pub mod pallet {
min_fee
};
ensure!(amount > fee, Error::<T>::LessThanFee);
T::Currency::withdraw(&source, amount.into(), WithdrawReasons::all(), AllowDeath)?;
let _ = T::Currency::withdraw(&source, amount.into(), WithdrawReasons::all(), AllowDeath)?;

<bridge::Pallet<T>>::transfer_fungible(dest_id, T::BridgeTokenId::get(), recipient, U256::from(amount.saturating_sub(fee).saturated_into::<u128>()))
}
Expand All @@ -192,9 +195,27 @@ pub mod pallet {
// 1. Check bridge limit
ensure!(Self::bridge_limit() >= amount, Error::<T>::ExceedBridgeLimit);
BridgeLimit::<T>::mutate(|l| *l = l.saturating_sub(amount));
<T as Config>::Currency::deposit_creating(&to, amount.into());

// Fix SubScan display issue
// Cause: SubScan 'Transfer' tab only display transactions which emits Event::Transfer event
// But Currency::deposit_creating only emits Event::Deposit event
// Solution: deposit to the pallet account first, then transfer to the target account
let pot = Self::account_id();

// Amount should be larger then ED, otherwise the newly created pot account would still have 0 balance
let _ = <T as Config>::Currency::deposit_creating(&pot, amount.into());

<T as Config>::Currency::transfer(&pot, &to, amount.into(), AllowDeath)?;

Ok(())
}

}

impl<T: Config> Pallet<T> {
/// Provides an AccountId for the pallet to use as a temporary deposit/withdrawal account.
pub fn account_id() -> T::AccountId {
PALLET_ID.into_account_truncating()
}
}
}
23 changes: 13 additions & 10 deletions crust-collator/res/2008.json

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions crust-collator/res/2012.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions crust-collator/res/staging.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crust-collator/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ std = [
"bridge-transfer/std",
"substrate-wasm-builder"
]
enable_sudo = []
16 changes: 10 additions & 6 deletions crust-collator/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,12 @@ impl pallet_proxy::Config for Runtime {
type AnnouncementDepositFactor = AnnouncementDepositFactor;
}

// impl pallet_sudo::Config for Runtime {
// type RuntimeCall = RuntimeCall;
// type RuntimeEvent = RuntimeEvent;
// type WeightInfo = ();
// }
#[cfg(feature = "enable_sudo")]
impl pallet_sudo::Config for Runtime {
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * RuntimeBlockWeights::get().max_block;
Expand Down Expand Up @@ -1563,7 +1564,10 @@ construct_runtime! {
System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
// Sudo: pallet_sudo::{Pallet, Call, Storage, Config<T>, Event<T>},

#[cfg(feature = "enable_sudo")]
Sudo: pallet_sudo::{Pallet, Call, Storage, Config<T>, Event<T>},

ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event<T>, ValidateUnsigned, Config<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
ParachainInfo: parachain_info::{Pallet, Storage, Config<T>, Call},
Expand Down
26 changes: 19 additions & 7 deletions crust-collator/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use serde::{Deserialize, Serialize};
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
use sp_core::crypto::UncheckedInto;
use parachain_runtime::{AuraId};
use parachain_runtime::{Balance, CENTS};
use parachain_runtime::{AuraId, Balance, CENTS};

const SHADOW_ED: Balance = 10 * CENTS;

Expand Down Expand Up @@ -87,7 +86,8 @@ pub fn get_chain_spec(id: ParaId) -> ChainSpec {
ChainType::Local,
move || {
testnet_genesis(
hex!["f8da9f475ca917478d54d8971c8838ab109a8e4bb85566e753deceec1044ef45"].into(),
//hex!["f8da9f475ca917478d54d8971c8838ab109a8e4bb85566e753deceec1044ef45"].into(),
get_account_id_from_seed::<sr25519::Public>("Alice"),
vec![(
hex!("0a38d76ecfbd4b13077669bb9c9ebaaf0847723426f809d20a67c62f2bebc75a").into(),
hex!("0a38d76ecfbd4b13077669bb9c9ebaaf0847723426f809d20a67c62f2bebc75a").unchecked_into()
Expand All @@ -110,10 +110,17 @@ pub fn get_chain_spec(id: ParaId) -> ChainSpec {
hex!["0a38d76ecfbd4b13077669bb9c9ebaaf0847723426f809d20a67c62f2bebc75a"].into(),
hex!["7e5040d49782960b2a15e7cb4106f730ca7a997a28facff6e2978aeda32fc348"].into(),
hex!["869f4e66b0b16f6de5f3cc217b99ada20f766a7d26868dda3020d29e9e80e97c"].into(),
hex!["7a6a226782a4cf5712f914bbf3cc64304f3c9af58b82f1dd2a4f09c48278ae65"].into()
hex!["7a6a226782a4cf5712f914bbf3cc64304f3c9af58b82f1dd2a4f09c48278ae65"].into(),
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
],
id,
vec![],
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
],
)
},
vec![],
Expand Down Expand Up @@ -225,16 +232,21 @@ fn testnet_genesis(
aura_ext: Default::default(),
parachain_system: Default::default(),
council: parachain_runtime::CouncilConfig {
members: council_accounts,
members: council_accounts.clone(),
phantom: Default::default(),
},
technical_committee: parachain_runtime::TechnicalCommitteeConfig {
members: vec![],
members: if let Some(first_elem) = council_accounts.first() { vec![(*first_elem).clone()] } else { vec![] },
phantom: Default::default(),
},
technical_membership: Default::default(),
treasury: Default::default(),
democracy: Default::default(),
phragmen_election: Default::default(),
#[cfg(feature = "enable_sudo")]
sudo: parachain_runtime::SudoConfig {
// Assign network admin rights.
key: Some(_root_key),
},
}
}
33 changes: 21 additions & 12 deletions scripts/build_spec_wasm_and_state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@
./target/release/crust-collator build-spec --raw --disable-default-bootnode --chain 2012 > ./crust-collator/res/2012.json
./target/release/crust-collator build-spec --raw --disable-default-bootnode --chain staging > ./crust-collator/res/staging.json

sed -i "" "s/\"protocolId\": null/\"protocolId\": \"2008\"/g" ./crust-collator/res/2008.json
sed -i "" "s/\"name\": \"Local Testnet\"/\"name\": \"2008\"/g" ./crust-collator/res/2008.json
sed -i "" "s/\"id\": \"local_testnet\"/\"id\": \"2008\"/g" ./crust-collator/res/2008.json
sed -i "" "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2008\"},/g" ./crust-collator/res/2008.json
# sed command is slightly different between MacOS and Linux
if [ "$(uname)" = "Darwin" ]; then
# MacOS - sed in MacOS need an additional argument to specify the extension
SED_CMD="sed -i ''"
else
# Linux
SED_CMD="sed -i"
fi

sed -i "" "s/\"protocolId\": null/\"protocolId\": \"2012\"/g" ./crust-collator/res/2012.json
sed -i "" "s/\"name\": \"Local Testnet\"/\"name\": \"2012\"/g" ./crust-collator/res/2012.json
sed -i "" "s/\"id\": \"local_testnet\"/\"id\": \"2012\"/g" ./crust-collator/res/2012.json
sed -i "" "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2012\"},/g" ./crust-collator/res/2012.json
$SED_CMD "s/\"protocolId\": null/\"protocolId\": \"2008\"/g" ./crust-collator/res/2008.json
$SED_CMD "s/\"name\": \"Local Testnet\"/\"name\": \"2008\"/g" ./crust-collator/res/2008.json
$SED_CMD "s/\"id\": \"local_testnet\"/\"id\": \"2008\"/g" ./crust-collator/res/2008.json
$SED_CMD "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2008\"},/g" ./crust-collator/res/2008.json

sed -i "" "s/\"protocolId\": null/\"protocolId\": \"2012\"/g" ./crust-collator/res/staging.json
sed -i "" "s/\"name\": \"Local Testnet\"/\"name\": \"2012\"/g" ./crust-collator/res/staging.json
sed -i "" "s/\"id\": \"local_testnet\"/\"id\": \"2012\"/g" ./crust-collator/res/staging.json
sed -i "" "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2012\"},/g" ./crust-collator/res/staging.json
$SED_CMD "s/\"protocolId\": null/\"protocolId\": \"2012\"/g" ./crust-collator/res/2012.json
$SED_CMD "s/\"name\": \"Local Testnet\"/\"name\": \"2012\"/g" ./crust-collator/res/2012.json
$SED_CMD "s/\"id\": \"local_testnet\"/\"id\": \"2012\"/g" ./crust-collator/res/2012.json
$SED_CMD "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2012\"},/g" ./crust-collator/res/2012.json

$SED_CMD "s/\"protocolId\": null/\"protocolId\": \"2012\"/g" ./crust-collator/res/staging.json
$SED_CMD "s/\"name\": \"Local Testnet\"/\"name\": \"2012\"/g" ./crust-collator/res/staging.json
$SED_CMD "s/\"id\": \"local_testnet\"/\"id\": \"2012\"/g" ./crust-collator/res/staging.json
$SED_CMD "s/\"properties\": null,/\"properties\": {\"ss58Format\": 42, \"tokenDecimals\": 12, \"tokenSymbol\": \"2012\"},/g" ./crust-collator/res/staging.json

./target/release/crust-collator export-genesis-state --chain ./crust-collator/res/2012.json > crust-collator-state-2012
./target/release/crust-collator export-genesis-state --chain ./crust-collator/res/2008.json > crust-collator-state-2008
Expand Down

0 comments on commit 7c010e4

Please sign in to comment.