Skip to content

Commit

Permalink
Format rust codes (paritytech#920)
Browse files Browse the repository at this point in the history
* Format rust codes

* Explicit rust nightly version
  • Loading branch information
yrong committed Aug 7, 2023
1 parent aa1d4b6 commit 7e0e166
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 56 deletions.
6 changes: 3 additions & 3 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e

# checks that the cumulus submodule is in sync with our substrate dependencies
check_duplicate_versions() {
local duplicates=`grep -Ei "source = \"git.*$1" cumulus/Cargo.lock parachain/Cargo.lock | sed 's/\.git//g' | sort -u`
local version_count=`echo "$duplicates" | awk -F':' '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | sort -u | wc -l | xargs`
local duplicates=$(grep -Ei "source = \"git.*$1" cumulus/Cargo.lock parachain/Cargo.lock | sed 's/\.git//g' | sort -u)
local version_count=$(echo "$duplicates" | awk -F':' '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | sort -u | wc -l | xargs)
if [ "$version_count" != "1" ]; then
echo Duplicate $1 versions detected
echo "$duplicates"
Expand All @@ -23,7 +23,7 @@ chronic typos .
(cd relayer && chronic mage lint && chronic go fmt ./...)

# format rust
(cd parachain && chronic cargo +nightly fmt)
(cd parachain && chronic cargo +nightly-"$SNOWBRIDGE_RUST_NIGHTLY" fmt)

check_duplicate_versions substrate
check_duplicate_versions polkadot
5 changes: 4 additions & 1 deletion parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ pub mod pallet {
#[transactional]
/// Used for pallet initialization and light client resetting. Needs to be called by
/// the root origin.
pub fn force_checkpoint(origin: OriginFor<T>, update: Box<CheckpointUpdate>) -> DispatchResult {
pub fn force_checkpoint(
origin: OriginFor<T>,
update: Box<CheckpointUpdate>,
) -> DispatchResult {
ensure_root(origin)?;
Self::process_checkpoint_update(&update)?;
Ok(())
Expand Down
65 changes: 52 additions & 13 deletions parachain/pallets/ethereum-beacon-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,10 @@ fn submit_update_in_current_period() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update.clone())));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(update.clone())
));
let block_root: H256 = update.finalized_header.hash_tree_root().unwrap();
assert!(<FinalizedBeaconState<Test>>::contains_key(block_root));
});
Expand Down Expand Up @@ -459,7 +462,10 @@ fn submit_update_in_next_period() {
RuntimeOrigin::signed(1),
Box::new(sync_committee_update.clone())
));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update.clone())));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(update.clone())
));
let block_root: H256 = update.finalized_header.clone().hash_tree_root().unwrap();
assert!(<FinalizedBeaconState<Test>>::contains_key(block_root));
});
Expand Down Expand Up @@ -558,9 +564,15 @@ fn submit_update_with_sync_committee_in_next_period() {
new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert!(!<NextSyncCommittee<Test>>::exists());
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update.clone())));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(update.clone())
));
assert!(<NextSyncCommittee<Test>>::exists());
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(next_update.clone())));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(next_update.clone())
));
let last_finalized_state =
FinalizedBeaconState::<Test>::get(LatestFinalizedBlockRoot::<Test>::get()).unwrap();
let last_synced_period = compute_period(last_finalized_state.slot);
Expand Down Expand Up @@ -614,7 +626,10 @@ fn submit_update_execution_headers_too_far_behind() {
let far_ahead_finalized_header_slot = finalized_header_update.finalized_header.slot +
(EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH * 2) as u64;
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_ok!(EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Box::new(execution_header_update)
Expand Down Expand Up @@ -706,7 +721,10 @@ fn submit_execution_header_update() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_ok!(EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Box::new(execution_header_update.clone())
Expand All @@ -728,7 +746,10 @@ fn submit_execution_header_update_invalid_ancestry_proof() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_err!(
EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Expand All @@ -748,7 +769,10 @@ fn submit_execution_header_update_invalid_execution_header_proof() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_err!(
EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Expand All @@ -770,7 +794,10 @@ fn submit_execution_header_update_that_skips_block() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_ok!(EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Box::new(execution_header_update.clone())
Expand All @@ -797,7 +824,10 @@ fn submit_execution_header_update_that_is_also_finalized_header_which_is_not_sto

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));
assert_err!(
EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Expand All @@ -818,7 +848,10 @@ fn submit_execution_header_update_that_is_also_finalized_header_which_is_stored_

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));

let block_root: H256 = execution_header_update.header.hash_tree_root().unwrap();

Expand Down Expand Up @@ -849,15 +882,21 @@ fn submit_execution_header_not_finalized() {

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_header_update)));
assert_ok!(EthereumBeaconClient::submit(
RuntimeOrigin::signed(1),
Box::new(finalized_header_update)
));

<FinalizedBeaconState<Test>>::mutate(<LatestFinalizedBlockRoot<Test>>::get(), |x| {
let prev = x.unwrap();
*x = Some(CompactBeaconState { slot: update.header.slot - 1, ..prev });
});

assert_err!(
EthereumBeaconClient::submit_execution_header(RuntimeOrigin::signed(1), Box::new(update)),
EthereumBeaconClient::submit_execution_header(
RuntimeOrigin::signed(1),
Box::new(update)
),
Error::<Test>::HeaderNotFinalized
);
});
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/outbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub struct PreparedMessage {
}

/// Convert message into an ABI-encoded form for delivery to the InboundQueue contract on Ethereum
impl From<PreparedMessage> for Token {
impl From<PreparedMessage> for Token {
fn from(x: PreparedMessage) -> Token {
Token::Tuple(vec![
Token::Uint(u32::from(x.origin).into()),
Expand Down
22 changes: 11 additions & 11 deletions parachain/primitives/beacon/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ fn apply_merkle_proof(proof: &[Vec<u8>]) -> Option<(H256, Vec<u8>)> {
};
let item_to_prove: mpt::ShortNode = rlp::decode(first_bytes).ok()?;

let final_hash: Option<[u8; 32]> =
iter.try_fold(keccak_256(first_bytes), |acc, x| {
let node: Box<dyn mpt::Node> = x.as_slice().try_into().ok()?;
if (*node).contains_hash(acc.into()) {
return Some(keccak_256(x))
}
None
});
let final_hash: Option<[u8; 32]> = iter.try_fold(keccak_256(first_bytes), |acc, x| {
let node: Box<dyn mpt::Node> = x.as_slice().try_into().ok()?;
if (*node).contains_hash(acc.into()) {
return Some(keccak_256(x))
}
None
});

final_hash.map(|hash| (hash.into(), item_to_prove.value))
}
Expand All @@ -44,7 +43,8 @@ mod tests {

#[test]
fn test_verify_receipt_proof() {
let root: H256 = hex!("fd5e397a84884641f53c496804f24b5276cbb8c5c9cfc2342246be8e3ce5ad02").into();
let root: H256 =
hex!("fd5e397a84884641f53c496804f24b5276cbb8c5c9cfc2342246be8e3ce5ad02").into();

// Valid proof
let proof_receipt5 = vec!(
Expand Down Expand Up @@ -80,7 +80,8 @@ mod tests {

#[test]
fn test_verify_receipt_proof_with_intermediate_short_node() {
let root: H256 = hex!("d128e3a57142d2bf15bc0cbcac7ad54f40750d571b5c3097e425882c10c9ba66").into();
let root: H256 =
hex!("d128e3a57142d2bf15bc0cbcac7ad54f40750d571b5c3097e425882c10c9ba66").into();

let proof_receipt263 = vec![
hex!("f90131a00d3cb8d3f57ac1c0e12918a2ebe0cafed8c273577b9dd73e7ed1079b403ef494a0678b9835b834f8a287c0dd33a8fca9146e456ca688555ed4ec1361a2180b778da0fe42da181a46677a043b3d9d4b8bb05a6a17b7b5c010c17e7c1d31cfb7c4f911a0c89f0e2c53241cdb578e1f2b4caf6ba36e00500bdc57fecd66b84a6a58394c19a086c3c1fae5a0575940b5d38e111c469d07883106c26856f3ef608469a2081f13a06c5992ff00aab6226a70a032fd2f571ba22f797321f45e2daa73020d638d21b0a050861e9503ef68728f6c90a44f7fe1bceb2a9bdab6957bbe7136166bd849561ea006aa6eaca8a07e57176e9aa41e6a09edfb7678d1a112404e0ec779d7e567e82ea0bb0b430d303ba21b0af11c487b8a218bd75db54c98940b3f11bad8ff47cad3ef8080808080808080").to_vec(),
Expand All @@ -92,5 +93,4 @@ mod tests {
];
assert!(verify_receipt_proof(root, &proof_receipt263).is_some());
}

}
13 changes: 8 additions & 5 deletions parachain/primitives/core/src/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ impl AgentExecuteCommand {
/// ABI-encode the sub-command
pub fn abi_encode(&self) -> Vec<u8> {
match self {
AgentExecuteCommand::TransferToken { token, recipient, amount } =>
ethabi::encode(&[Token::Uint(self.index().into()),
Token::Bytes(ethabi::encode(&[Token::Address(*token),
Token::Address(*recipient),
Token::Uint(U256::from(*amount))]))]),
AgentExecuteCommand::TransferToken { token, recipient, amount } => ethabi::encode(&[
Token::Uint(self.index().into()),
Token::Bytes(ethabi::encode(&[
Token::Address(*token),
Token::Address(*recipient),
Token::Uint(U256::from(*amount)),
])),
]),
}
}
}
15 changes: 7 additions & 8 deletions parachain/primitives/ethereum/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ impl Header {
};
let item_to_prove: mpt::ShortNode = rlp::decode(first_bytes).ok()?;

let final_hash: Option<[u8; 32]> =
iter.try_fold(keccak_256(first_bytes), |acc, x| {
let node: Box<dyn mpt::Node> = x.as_slice().try_into().ok()?;
if (*node).contains_hash(acc.into()) {
return Some(keccak_256(x))
}
None
});
let final_hash: Option<[u8; 32]> = iter.try_fold(keccak_256(first_bytes), |acc, x| {
let node: Box<dyn mpt::Node> = x.as_slice().try_into().ok()?;
if (*node).contains_hash(acc.into()) {
return Some(keccak_256(x))
}
None
});

final_hash.map(|hash| (hash.into(), item_to_prove.value))
}
Expand Down
20 changes: 10 additions & 10 deletions parachain/primitives/router/src/inbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use xcm_executor::traits::ConvertLocation;
const MINIMUM_DEPOSIT: u128 = 1;

/// Messages from Ethereum are versioned. This is because in future,
/// we may want to evolve the protocol so that the ethereum side sends XCM messages directly. Instead
/// having BridgeHub transcode the messages into XCM.
/// we may want to evolve the protocol so that the ethereum side sends XCM messages directly.
/// Instead having BridgeHub transcode the messages into XCM.
#[derive(Clone, Encode, Decode, RuntimeDebug)]
pub enum VersionedMessage {
V1(MessageV1),
Expand All @@ -40,7 +40,7 @@ pub enum Command {
/// The address of the ERC20 token to be bridged over to AssetHub
token: H160,
/// The stable ID of the `ForeignAssets::create` extrinsic
create_call_index: [u8; 2]
create_call_index: [u8; 2],
},
/// Send a token to AssetHub or another parachain
SendToken {
Expand All @@ -51,7 +51,7 @@ pub enum Command {
/// The destination for the transfer
destination: Destination,
/// Amount to transfer
amount: u128
amount: u128,
},
}

Expand All @@ -60,13 +60,13 @@ pub enum Command {
pub enum Destination {
/// The funds will be deposited into account `id` on AssetHub
AccountId32 { id: [u8; 32] },
/// The funds will deposited into the sovereign account of destination parachain `para_id` on AssetHub,
/// Account `id` on the destination parachain will receive the funds via a reserve-backed transfer.
/// See https://github.com/paritytech/xcm-format#depositreserveasset
/// The funds will deposited into the sovereign account of destination parachain `para_id` on
/// AssetHub, Account `id` on the destination parachain will receive the funds via a
/// reserve-backed transfer. See https://github.com/paritytech/xcm-format#depositreserveasset
ForeignAccountId32 { para_id: u32, id: [u8; 32] },
/// The funds will deposited into the sovereign account of destination parachain `para_id` on AssetHub,
/// Account `id` on the destination parachain will receive the funds via a reserve-backed transfer.
/// See https://github.com/paritytech/xcm-format#depositreserveasset
/// The funds will deposited into the sovereign account of destination parachain `para_id` on
/// AssetHub, Account `id` on the destination parachain will receive the funds via a
/// reserve-backed transfer. See https://github.com/paritytech/xcm-format#depositreserveasset
ForeignAccountId20 { para_id: u32, id: [u8; 20] },
}

Expand Down
7 changes: 3 additions & 4 deletions parachain/primitives/router/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ where

log::info!(target: "xcm::ethereum_blob_exporter", "message validated: location = {local_sub_location:?}, agent_id = '{agent_id:?}'");

// TODO (SNO-581): Make sure we charge fees for message delivery. Currently this is set to zero.
// TODO (SNO-581): Make sure we charge fees for message delivery. Currently this is set to
// zero.
Ok((ticket.encode(), MultiAssets::default()))
}

Expand Down Expand Up @@ -227,9 +228,7 @@ impl<'a, Call> XcmConverter<'a, Call> {
Ok(execution_fee)
}

fn native_tokens_unlock_message(
&mut self,
) -> Result<AgentExecuteCommand, XcmConverterError> {
fn native_tokens_unlock_message(&mut self) -> Result<AgentExecuteCommand, XcmConverterError> {
use XcmConverterError::*;
let (assets, beneficiary) = if let WithdrawAsset(reserved_assets) = self.next()? {
if reserved_assets.len() == 0 {
Expand Down

0 comments on commit 7e0e166

Please sign in to comment.