Skip to content

Commit

Permalink
Improve Substrate pallet logs (paritytech#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent 5cdd0c6 commit 51b4d3e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
8 changes: 6 additions & 2 deletions bridges/modules/substrate/src/fork_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ where
}

// Try and import into storage
let res = verifier.import_header(header.clone()).map_err(TestError::Import);
let res = verifier
.import_header(header.hash(), header.clone())
.map_err(TestError::Import);
assert_eq!(
res, *expected_result,
"Expected {:?} while importing header ({}, {}), got {:?}",
Expand Down Expand Up @@ -427,7 +429,9 @@ where
header.digest = change_log(*delay);
}

let res = verifier.import_header(header.clone()).map_err(TestError::Import);
let res = verifier
.import_header(header.hash(), header.clone())
.map_err(TestError::Import);
assert_eq!(
res, *expected_result,
"Expected {:?} while importing header ({}, {}), got {:?}",
Expand Down
21 changes: 16 additions & 5 deletions bridges/modules/substrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,21 @@ decl_module! {
) -> DispatchResult {
ensure_operational::<T>()?;
let _ = ensure_signed(origin)?;
frame_support::debug::trace!("Got header {:?}", header);
let hash = header.hash();
frame_support::debug::trace!("Going to import header {:?}: {:?}", hash, header);

let mut verifier = verifier::Verifier {
storage: PalletStorage::<T>::new(),
};

let _ = verifier
.import_header(header)
.map_err(|_| <Error<T>>::InvalidHeader)?;
.import_header(hash, header)
.map_err(|e| {
frame_support::debug::error!("Failed to import header {:?}: {:?}", hash, e);
<Error<T>>::InvalidHeader
})?;

frame_support::debug::trace!("Successfully imported header: {:?}", hash);

Ok(())
}
Expand All @@ -199,15 +205,20 @@ decl_module! {
) -> DispatchResult {
ensure_operational::<T>()?;
let _ = ensure_signed(origin)?;
frame_support::debug::trace!("Got header hash {:?}", hash);
frame_support::debug::trace!("Going to finalize header: {:?}", hash);

let mut verifier = verifier::Verifier {
storage: PalletStorage::<T>::new(),
};

let _ = verifier
.import_finality_proof(hash, finality_proof.into())
.map_err(|_| <Error<T>>::UnfinalizedHeader)?;
.map_err(|e| {
frame_support::debug::error!("Failed to finalize header {:?}: {:?}", hash, e);
<Error<T>>::UnfinalizedHeader
})?;

frame_support::debug::trace!("Successfully finalized header: {:?}", hash);

Ok(())
}
Expand Down
28 changes: 15 additions & 13 deletions bridges/modules/substrate/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ where
///
/// Will perform some basic checks to make sure that this header doesn't break any assumptions
/// such as being on a different finalized fork.
pub fn import_header(&mut self, header: H) -> Result<(), ImportError> {
let hash = header.hash();
pub fn import_header(&mut self, hash: H::Hash, header: H) -> Result<(), ImportError> {
let best_finalized = self.storage.best_finalized_header();

if header.number() <= best_finalized.number() {
Expand Down Expand Up @@ -424,7 +423,7 @@ mod tests {

let header = test_header(1);
let mut verifier = Verifier { storage };
assert_err!(verifier.import_header(header), ImportError::OldHeader);
assert_err!(verifier.import_header(header.hash(), header), ImportError::OldHeader);
})
}

Expand All @@ -440,7 +439,10 @@ mod tests {
let header = TestHeader::new_from_number(2);

let mut verifier = Verifier { storage };
assert_err!(verifier.import_header(header), ImportError::MissingParent);
assert_err!(
verifier.import_header(header.hash(), header),
ImportError::MissingParent
);
})
}

Expand All @@ -460,7 +462,7 @@ mod tests {
<ImportedHeaders<TestRuntime>>::insert(header.hash(), &imported_header);

let mut verifier = Verifier { storage };
assert_err!(verifier.import_header(header), ImportError::OldHeader);
assert_err!(verifier.import_header(header.hash(), header), ImportError::OldHeader);
})
}

Expand All @@ -484,7 +486,7 @@ mod tests {
let mut verifier = Verifier {
storage: storage.clone(),
};
assert_ok!(verifier.import_header(header.clone()));
assert_ok!(verifier.import_header(header.hash(), header.clone()));

let stored_header = storage
.header_by_hash(header.hash())
Expand Down Expand Up @@ -514,8 +516,8 @@ mod tests {
};

// It should be fine to import both
assert_ok!(verifier.import_header(header_on_fork1.clone()));
assert_ok!(verifier.import_header(header_on_fork2.clone()));
assert_ok!(verifier.import_header(header_on_fork1.hash(), header_on_fork1.clone()));
assert_ok!(verifier.import_header(header_on_fork2.hash(), header_on_fork2.clone()));

// We should have two headers marked as being the best since they're
// both at the same height
Expand Down Expand Up @@ -559,7 +561,7 @@ mod tests {
let mut verifier = Verifier {
storage: storage.clone(),
};
assert_ok!(verifier.import_header(better_header.clone()));
assert_ok!(verifier.import_header(better_header.hash(), better_header.clone()));

// Since `better_header` is the only one at height = 2 we should only have
// a single "best header" now.
Expand Down Expand Up @@ -668,7 +670,7 @@ mod tests {
let mut verifier = Verifier { storage };

assert_eq!(
verifier.import_header(header).unwrap_err(),
verifier.import_header(header.hash(), header).unwrap_err(),
ImportError::InvalidAuthoritySet
);
})
Expand Down Expand Up @@ -697,7 +699,7 @@ mod tests {
storage: storage.clone(),
};

assert_ok!(verifier.import_header(header.clone()));
assert_ok!(verifier.import_header(header.hash(), header.clone()));
assert_ok!(verifier.import_finality_proof(header.hash(), justification.into()));
assert_eq!(storage.best_finalized_header().header, header);
})
Expand All @@ -724,7 +726,7 @@ mod tests {
let mut verifier = Verifier {
storage: storage.clone(),
};
assert!(verifier.import_header(header.clone()).is_ok());
assert!(verifier.import_header(header.hash(), header.clone()).is_ok());
assert!(verifier
.import_finality_proof(header.hash(), justification.into())
.is_ok());
Expand Down Expand Up @@ -776,7 +778,7 @@ mod tests {
storage: storage.clone(),
};

assert_ok!(verifier.import_header(header.clone()));
assert_ok!(verifier.import_header(header.hash(), header.clone()));
assert_eq!(storage.missing_justifications().len(), 1);
assert_eq!(storage.missing_justifications()[0].hash, header.hash());

Expand Down

0 comments on commit 51b4d3e

Please sign in to comment.