Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex committed Jan 26, 2024
1 parent f170e40 commit 4f33fc5
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions pallets/dkg/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
//
// You should have received a copy of the GNU General Public License
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.
use crate::{mock::*, types::FeeInfo, Error, FeeInfo as FeeInfoStorage};
use crate::{mock::*, types::FeeInfo, Error, Event, FeeInfo as FeeInfoStorage};
use frame_support::{assert_noop, assert_ok};
use parity_scale_codec::Encode;
use sp_core::{crypto::ByteArray, ecdsa, keccak_256, sr25519};
use sp_io::crypto::{ecdsa_generate, ecdsa_sign_prehashed, sr25519_generate, sr25519_sign};
use tangle_primitives::jobs::{
DKGTSSKeySubmissionResult, DKGTSSSignatureResult, DigitalSignatureType, JobResult,
DKGTSSKeyRotationResult, DKGTSSKeySubmissionResult, DKGTSSSignatureResult,
DigitalSignatureType, JobResult,
};

fn mock_pub_key_ecdsa() -> ecdsa::Public {
Expand Down Expand Up @@ -298,3 +299,49 @@ fn dkg_signature_verifcation_works_schnorr() {
assert_ok!(DKG::verify(JobResult::DKGPhaseTwo(job_to_verify)));
});
}

#[test]
fn dkg_key_rotation_works() {
new_test_ext().execute_with(|| {
// setup key/signature
let curr_key = mock_pub_key_ecdsa();
let new_key = mock_pub_key_ecdsa();
let invalid_key = mock_pub_key_ecdsa();
let signature = mock_signature_ecdsa(invalid_key, new_key);

let job_to_verify = DKGTSSKeyRotationResult {
signature_type: DigitalSignatureType::Ecdsa,
signature,
key: curr_key.to_raw_vec(),
new_key: new_key.to_raw_vec(),
phase_one_id: 1,
new_phase_one_id: 2,
};

// should fail for invalid keys
assert_noop!(
DKG::verify(JobResult::DKGPhaseFour(job_to_verify)),
Error::<Runtime>::SigningKeyMismatch
);

let signature = mock_signature_ecdsa(curr_key, new_key);

let job_to_verify = DKGTSSKeyRotationResult {
signature_type: DigitalSignatureType::Ecdsa,
signature: signature.clone(),
key: curr_key.to_raw_vec(),
new_key: new_key.to_raw_vec(),
phase_one_id: 1,
new_phase_one_id: 2,
};
// should work with correct params
assert_ok!(DKG::verify(JobResult::DKGPhaseFour(job_to_verify)));
// should emit KeyRotated event
assert!(System::events().iter().any(|r| r.event ==
RuntimeEvent::DKG(Event::KeyRotated {
from_job_id: 1,
to_job_id: 2,
signature: signature.clone()
})));
});
}

0 comments on commit 4f33fc5

Please sign in to comment.