这是我的测试:
assert_noop!(
TemplateModule::commit_vote(
Origin::signed(5),
count - 1,
0,
"2-Votinghash".as_bytes().to_vec()
),
Error::<Test>::AlreadyCommitUsed
);当我添加另一个错误时,它失败了,因为:
thread 'tests::commit_vote' panicked at 'assertion failed: `(left == right)`
left: `Err(DispatchError::Module { index: 1, error: 7, message: Some("AlreadyCommitUsed") })`,
right: `Err(DispatchError::Module { index: 1, error: 4, message: Some("DepartmentNotAssociated") })`', pallets/template/src/tests.rs:106:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace其中,当我添加AlreadyCommitUsed作为错误时,它给出了以下奇怪的不等式:
thread 'tests::commit_vote' panicked at 'assertion failed: `(left == right)`
left: `[218, 23, 247, 200, 228, 182, 117, 227, 150, 32, 248, 47, 209, 182, 207, 246, 198, 19, 20, 74, 185, 125, 35, 94, 74, 161, 128, 199, 114, 155, 238, 228]`,
right: `[128, 69, 23, 167, 50, 250, 133, 59, 10, 41, 100, 95, 141, 187, 236, 154, 67, 174, 251, 17, 219, 45, 146, 141, 204, 77, 245, 38, 95, 131, 122, 48]`', pallets/template/src/tests.rs:106:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace这是我的commit_vote函数:
#[weight = 10_000 + T::DbWeight::get().reads_writes(3,3)]
pub fn commit_vote(origin, departmentid:u128, voting_cycle:u128, vote_commit:Vec<u8>) -> dispatch::DispatchResult {
let who = ensure_signed(origin.clone())?;
Self::check_citizen_associated_department(who.clone(), departmentid)?;
let status = VoteStatus::get((departmentid, voting_cycle, vote_commit.clone()));
match status {
Some(value) => {
if value == true {
Err(Error::<T>::AlreadyCommitUsed.into())
} else {
Err(Error::<T>::VoteRevealed.into())
}
}
None => {
Self::add_vote(departmentid, voting_cycle, vote_commit)?;
Ok(())
}
}
}这是我的helper函数:
// Helper functions
impl<T: Config> Module<T> {
fn check_citizen_associated_department(
who: T::AccountId,
departmentid: u128,
) -> dispatch::DispatchResult {
let approved_peer_dep = PeerDepartments::<T>::get(&who);
match approved_peer_dep.binary_search(&departmentid) {
Ok(_) => {
Self::deposit_event(RawEvent::PeerDepartment(departmentid, who));
Ok(())
}
Err(_) => Err(Error::<T>::DepartmentNotAssociated.into()),
}
}
}是的,错误是由于helper函数造成的,当check_citizen_associated_department的deposit_event被移除时,错误就会消失。不确定写helper函数的方式是否正确。
发布于 2021-04-07 22:38:04
此错误:
thread 'tests::commit_vote' panicked at 'assertion failed: `(left == right)`
left: `[218, 23, 247, 200, 228, 182, 117, 227, 150, 32, 248, 47, 209, 182, 207, 246, 198, 19, 20, 74, 185, 125, 35, 94, 74, 161, 128, 199, 114, 155, 238, 228]`,
right: `[128, 69, 23, 167, 50, 250, 133, 59, 10, 41, 100, 95, 141, 187, 236, 154, 67, 174, 251, 17, 219, 45, 146, 141, 204, 77, 245, 38, 95, 131, 122, 48]`', pallets/template/src/tests.rs:106:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace告诉您您的外部操作导致了错误,但您还更改了运行库的状态。这违反了你如何使用底层的规则。
这就是assert_noop!宏的用途:
https://crates.parity.io/frame_support/macro.assert_noop.html
有一个策略规定,所有extrinsics必须是“先检查,最后写入”,就像在中一样,当您实际着手修改运行时状态时,应该不会发生错误。
问题似乎是您在check_citizen_associated_department中发出了一个事件,这会修改状态,但在match status中可能会出错。
您将需要修复您的逻辑,以便只有当您知道其他一切都会成功时才会发出事件。
https://stackoverflow.com/questions/66984081
复制相似问题