Skip to content

Commit 6857795

Browse files
committed
add tests for enum types against Trezor proto enums
1 parent 38d7371 commit 6857795

4 files changed

Lines changed: 186 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

trezor-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ strum = { version = "0.26", default-features = false, features = ["derive"] }
1717
test-utils = { path = "../test-utils" }
1818
common = { path = "../common" }
1919
crypto = { path = "../crypto/" }
20+
trezor-client = { git = "https://github.com/mintlayer/mintlayer-trezor-firmware", branch = "feature/mintlayer-pk", features = ["bitcoin", "mintlayer"] }
2021

2122
rstest.workspace = true

trezor-common/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub enum OutputValue {
7777
}
7878

7979
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, EnumDiscriminants)]
80-
#[strum_discriminants(name(OutputTimeLockIndex), derive(EnumIter, FromPrimitive))]
80+
#[strum_discriminants(name(OutputTimeLockTag), derive(EnumIter, FromPrimitive))]
8181
pub enum OutputTimeLock {
8282
#[codec(index = 0)]
8383
UntilHeight(#[codec(compact)] u64),
@@ -154,7 +154,7 @@ pub enum IsTokenFreezable {
154154
}
155155

156156
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, EnumDiscriminants)]
157-
#[strum_discriminants(name(TokenTotalSupplyIndex), derive(EnumIter, FromPrimitive))]
157+
#[strum_discriminants(name(TokenTotalSupplyTag), derive(EnumIter, FromPrimitive))]
158158
pub enum TokenTotalSupply {
159159
#[codec(index = 0)]
160160
Fixed(Amount), // fixed to a certain amount
@@ -267,7 +267,7 @@ pub struct H256(pub [u8; 32]);
267267
pub struct HtlcSecretHash(pub [u8; 20]);
268268

269269
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Ord, PartialOrd, EnumDiscriminants)]
270-
#[strum_discriminants(name(OutPointSourceIdIndex), derive(EnumIter, FromPrimitive))]
270+
#[strum_discriminants(name(OutPointSourceIdTag), derive(EnumIter, FromPrimitive))]
271271
pub enum OutPointSourceId {
272272
#[codec(index = 0)]
273273
Transaction(H256),
@@ -323,7 +323,7 @@ type OrderId = H256;
323323
type TokenId = H256;
324324

325325
#[derive(Encode, EnumDiscriminants)]
326-
#[strum_discriminants(name(AccountCommandIndex), derive(EnumIter, FromPrimitive))]
326+
#[strum_discriminants(name(AccountCommandTag), derive(EnumIter, FromPrimitive))]
327327
pub enum AccountCommand {
328328
// Create certain amount of tokens and add them to circulating supply
329329
#[codec(index = 0)]

trezor-common/src/tests/mod.rs

Lines changed: 180 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -335,43 +335,43 @@ fn make_random_bytes(rng: &mut (impl Rng + CryptoRng)) -> Vec<u8> {
335335
}
336336

337337
fn make_random_account_command(rng: &mut (impl Rng + CryptoRng)) -> chain::AccountCommand {
338-
match crate::AccountCommandIndex::iter().choose(rng).unwrap() {
339-
crate::AccountCommandIndex::MintTokens => chain::AccountCommand::MintTokens(
338+
match crate::AccountCommandTag::iter().choose(rng).unwrap() {
339+
crate::AccountCommandTag::MintTokens => chain::AccountCommand::MintTokens(
340340
primitives::H256(rng.gen()).into(),
341341
primitives::Amount::from_atoms(rng.gen()),
342342
),
343-
crate::AccountCommandIndex::UnmintTokens => {
343+
crate::AccountCommandTag::UnmintTokens => {
344344
chain::AccountCommand::UnmintTokens(primitives::H256(rng.gen()).into())
345345
}
346-
crate::AccountCommandIndex::LockTokenSupply => {
346+
crate::AccountCommandTag::LockTokenSupply => {
347347
chain::AccountCommand::LockTokenSupply(primitives::H256(rng.gen()).into())
348348
}
349-
crate::AccountCommandIndex::FreezeToken => chain::AccountCommand::FreezeToken(
349+
crate::AccountCommandTag::FreezeToken => chain::AccountCommand::FreezeToken(
350350
primitives::H256(rng.gen()).into(),
351351
if rng.gen::<bool>() {
352352
chain::tokens::IsTokenUnfreezable::Yes
353353
} else {
354354
chain::tokens::IsTokenUnfreezable::No
355355
},
356356
),
357-
crate::AccountCommandIndex::UnfreezeToken => {
357+
crate::AccountCommandTag::UnfreezeToken => {
358358
chain::AccountCommand::UnfreezeToken(primitives::H256(rng.gen()).into())
359359
}
360-
crate::AccountCommandIndex::ChangeTokenAuthority => {
360+
crate::AccountCommandTag::ChangeTokenAuthority => {
361361
chain::AccountCommand::ChangeTokenAuthority(
362362
primitives::H256(rng.gen()).into(),
363363
make_random_destination(rng),
364364
)
365365
}
366-
crate::AccountCommandIndex::ConcludeOrder => {
366+
crate::AccountCommandTag::ConcludeOrder => {
367367
chain::AccountCommand::ConcludeOrder(primitives::H256(rng.gen()).into())
368368
}
369-
crate::AccountCommandIndex::FillOrder => chain::AccountCommand::FillOrder(
369+
crate::AccountCommandTag::FillOrder => chain::AccountCommand::FillOrder(
370370
primitives::H256(rng.gen()).into(),
371371
primitives::Amount::from_atoms(rng.gen()),
372372
make_random_destination(rng),
373373
),
374-
crate::AccountCommandIndex::ChangeTokenMetadataUri => {
374+
crate::AccountCommandTag::ChangeTokenMetadataUri => {
375375
chain::AccountCommand::ChangeTokenMetadataUri(
376376
primitives::H256(rng.gen()).into(),
377377
make_random_bytes(rng),
@@ -412,17 +412,17 @@ fn make_random_value(rng: &mut (impl Rng + CryptoRng)) -> chain::output_value::O
412412
}
413413

414414
fn make_random_lock(rng: &mut (impl Rng + CryptoRng)) -> chain::timelock::OutputTimeLock {
415-
match crate::OutputTimeLockIndex::iter().choose(rng).unwrap() {
416-
crate::OutputTimeLockIndex::UntilHeight => {
415+
match crate::OutputTimeLockTag::iter().choose(rng).unwrap() {
416+
crate::OutputTimeLockTag::UntilHeight => {
417417
chain::timelock::OutputTimeLock::UntilHeight(primitives::BlockHeight::new(rng.gen()))
418418
}
419-
crate::OutputTimeLockIndex::UntilTime => chain::timelock::OutputTimeLock::UntilTime(
419+
crate::OutputTimeLockTag::UntilTime => chain::timelock::OutputTimeLock::UntilTime(
420420
chain::block::timestamp::BlockTimestamp::from_int_seconds(rng.gen()),
421421
),
422-
crate::OutputTimeLockIndex::ForSeconds => {
422+
crate::OutputTimeLockTag::ForSeconds => {
423423
chain::timelock::OutputTimeLock::ForSeconds(rng.gen())
424424
}
425-
crate::OutputTimeLockIndex::ForBlockCount => {
425+
crate::OutputTimeLockTag::ForBlockCount => {
426426
chain::timelock::OutputTimeLock::ForBlockCount(rng.gen())
427427
}
428428
}
@@ -431,10 +431,10 @@ fn make_random_lock(rng: &mut (impl Rng + CryptoRng)) -> chain::timelock::Output
431431
fn make_random_token_total_supply(
432432
rng: &mut (impl Rng + CryptoRng),
433433
) -> chain::tokens::TokenTotalSupply {
434-
match crate::TokenTotalSupplyIndex::iter().choose(rng).unwrap() {
435-
crate::TokenTotalSupplyIndex::Unlimited => chain::tokens::TokenTotalSupply::Unlimited,
436-
crate::TokenTotalSupplyIndex::Lockable => chain::tokens::TokenTotalSupply::Lockable,
437-
crate::TokenTotalSupplyIndex::Fixed => {
434+
match crate::TokenTotalSupplyTag::iter().choose(rng).unwrap() {
435+
crate::TokenTotalSupplyTag::Unlimited => chain::tokens::TokenTotalSupply::Unlimited,
436+
crate::TokenTotalSupplyTag::Lockable => chain::tokens::TokenTotalSupply::Lockable,
437+
crate::TokenTotalSupplyTag::Fixed => {
438438
chain::tokens::TokenTotalSupply::Fixed(primitives::Amount::from_atoms(rng.gen()))
439439
}
440440
}
@@ -582,3 +582,164 @@ fn check_output_encodings(#[case] seed: Seed) {
582582
assert_eq!(decoded_simple_out, out);
583583
}
584584
}
585+
586+
#[rstest]
587+
fn check_total_supply() {
588+
use num_traits::FromPrimitive;
589+
590+
for x in crate::TokenTotalSupplyTag::iter() {
591+
match x {
592+
crate::TokenTotalSupplyTag::Fixed => {
593+
assert_eq!(
594+
crate::TokenTotalSupplyTag::from_u32(
595+
trezor_client::protos::MintlayerTokenTotalSupplyType::FIXED as u32
596+
),
597+
Some(x)
598+
);
599+
}
600+
crate::TokenTotalSupplyTag::Lockable => {
601+
assert_eq!(
602+
crate::TokenTotalSupplyTag::from_u32(
603+
trezor_client::protos::MintlayerTokenTotalSupplyType::LOCKABLE as u32
604+
),
605+
Some(x)
606+
);
607+
}
608+
crate::TokenTotalSupplyTag::Unlimited => {
609+
assert_eq!(
610+
crate::TokenTotalSupplyTag::from_u32(
611+
trezor_client::protos::MintlayerTokenTotalSupplyType::UNLIMITED as u32
612+
),
613+
Some(x)
614+
);
615+
}
616+
}
617+
}
618+
}
619+
620+
#[rstest]
621+
fn check_account_command_tag() {
622+
use num_traits::FromPrimitive;
623+
624+
for x in crate::AccountCommandTag::iter() {
625+
match x {
626+
crate::AccountCommandTag::MintTokens => {
627+
assert_eq!(
628+
crate::AccountCommandTag::from_u32(
629+
trezor_client::protos::MintlayerAccountCommandType::MINT_TOKENS as u32
630+
),
631+
Some(x)
632+
);
633+
}
634+
crate::AccountCommandTag::UnmintTokens => {
635+
assert_eq!(
636+
crate::AccountCommandTag::from_u32(
637+
trezor_client::protos::MintlayerAccountCommandType::UNMINT_TOKENS as u32
638+
),
639+
Some(x)
640+
);
641+
}
642+
crate::AccountCommandTag::FreezeToken => {
643+
assert_eq!(
644+
crate::AccountCommandTag::from_u32(
645+
trezor_client::protos::MintlayerAccountCommandType::FREEZE_TOKEN as u32
646+
),
647+
Some(x)
648+
);
649+
}
650+
crate::AccountCommandTag::UnfreezeToken => {
651+
assert_eq!(
652+
crate::AccountCommandTag::from_u32(
653+
trezor_client::protos::MintlayerAccountCommandType::UNFREEZE_TOKEN as u32
654+
),
655+
Some(x)
656+
);
657+
}
658+
crate::AccountCommandTag::FillOrder => {
659+
assert_eq!(
660+
crate::AccountCommandTag::from_u32(
661+
trezor_client::protos::MintlayerAccountCommandType::FILL_ORDER as u32
662+
),
663+
Some(x)
664+
);
665+
}
666+
crate::AccountCommandTag::ConcludeOrder => {
667+
assert_eq!(
668+
crate::AccountCommandTag::from_u32(
669+
trezor_client::protos::MintlayerAccountCommandType::CONCLUDE_ORDER as u32
670+
),
671+
Some(x)
672+
);
673+
}
674+
crate::AccountCommandTag::ChangeTokenAuthority => {
675+
assert_eq!(
676+
crate::AccountCommandTag::from_u32(
677+
trezor_client::protos::MintlayerAccountCommandType::CHANGE_TOKEN_AUTHORITY
678+
as u32
679+
),
680+
Some(x)
681+
);
682+
}
683+
crate::AccountCommandTag::ChangeTokenMetadataUri => {
684+
assert_eq!(
685+
crate::AccountCommandTag::from_u32(
686+
trezor_client::protos::MintlayerAccountCommandType::CHANGE_TOKEN_METADATA_URI
687+
as u32
688+
),
689+
Some(x)
690+
);
691+
}
692+
crate::AccountCommandTag::LockTokenSupply => {
693+
assert_eq!(
694+
crate::AccountCommandTag::from_u32(
695+
trezor_client::protos::MintlayerAccountCommandType::LOCK_TOKEN_SUPPLY
696+
as u32
697+
),
698+
Some(x)
699+
);
700+
}
701+
}
702+
}
703+
}
704+
705+
#[rstest]
706+
fn check_output_timelock_type() {
707+
use num_traits::FromPrimitive;
708+
709+
for x in crate::OutputTimeLockTag::iter() {
710+
match x {
711+
crate::OutputTimeLockTag::UntilTime => {
712+
assert_eq!(
713+
crate::OutputTimeLockTag::from_u32(
714+
trezor_client::protos::MintlayerOutputTimeLockType::UNTIL_TIME as u32
715+
),
716+
Some(x)
717+
);
718+
}
719+
crate::OutputTimeLockTag::UntilHeight => {
720+
assert_eq!(
721+
crate::OutputTimeLockTag::from_u32(
722+
trezor_client::protos::MintlayerOutputTimeLockType::UNTIL_HEIGHT as u32
723+
),
724+
Some(x)
725+
);
726+
}
727+
crate::OutputTimeLockTag::ForSeconds => {
728+
assert_eq!(
729+
crate::OutputTimeLockTag::from_u32(
730+
trezor_client::protos::MintlayerOutputTimeLockType::FOR_SECONDS as u32
731+
),
732+
Some(x)
733+
);
734+
}
735+
crate::OutputTimeLockTag::ForBlockCount => {
736+
assert_eq!(
737+
crate::OutputTimeLockTag::from_u32(
738+
trezor_client::protos::MintlayerOutputTimeLockType::FOR_BLOCK_COUNT as u32
739+
),
740+
Some(x)
741+
);
742+
}
743+
}
744+
}
745+
}

0 commit comments

Comments
 (0)