Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/trezor common #1838

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Feature/trezor common #1838

wants to merge 5 commits into from

Conversation

OBorce
Copy link
Contributor

@OBorce OBorce commented Nov 14, 2024

Move the simplified no-std types used by Trezor firmware to keep it in sync with the core types.
Add randomized tests to check the encodings match between the types.

@OBorce OBorce force-pushed the feature/trezor-common branch 2 times, most recently from c2c31bb to a99237c Compare November 14, 2024 09:19
Cargo.toml Outdated Show resolved Hide resolved
trezor-common/Cargo.toml Outdated Show resolved Hide resolved
@OBorce OBorce force-pushed the feature/trezor-common branch 2 times, most recently from 00f5576 to 44b17f9 Compare November 14, 2024 17:34
wallet/trezor-common/src/tests/mod.rs Outdated Show resolved Hide resolved
Comment on lines 30 to 40
impl From<chain::OutPointSourceId> for crate::OutPointSourceId {
fn from(value: chain::OutPointSourceId) -> Self {
match value {
chain::OutPointSourceId::Transaction(tx) => Self::Transaction(tx.to_hash().into()),
chain::OutPointSourceId::BlockReward(tx) => Self::BlockReward(tx.to_hash().into()),
}
}
}

impl From<chain::UtxoOutPoint> for crate::UtxoOutPoint {
fn from(value: chain::UtxoOutPoint) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I don't insist on this, I'd suggest to make the make_random... functions return a pair of values that are supposed to be encoded to the same bytes, and remove these From implementations (which look somewhat fragile to me).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the same, because you would still need to put the same random data in the right fields for both versions when generating them.

wallet/trezor-common/src/tests/mod.rs Outdated Show resolved Hide resolved
Comment on lines 374 to 385
#[derive(FromPrimitive)]
pub enum AccountCommandIndex {
MintTokens = 0,
UnmintTokens = 1,
LockTokenSupply = 2,
FreezeToken = 3,
UnfreezeToken = 4,
ChangeTokenAuthority = 5,
ConcludeOrder = 6,
FillOrder = 7,
ChangeTokenMetadataUri = 8,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this enum needed here? As I can see, it's only used in the trezor firmware repo in encode_token_account_command_input but only on the rust side. And its values are not tied to anything, they just have to be the same in rust and python. So probably it should go to messages-mintlayer.proto in the trezor repo, so that named constants can be used on the python side too.

Comment on lines 289 to 293
#[derive(FromPrimitive)]
pub enum OutPointSourceIdIndex {
Transaction = 0,
BlockReward = 1,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see, this is similar to AccountCommandIndex, but the difference is that there is already MintlayerUtxoType in the protobuf config which this enum should be consistent with. Remove it and use MintlayerUtxoType instead.

@OBorce OBorce force-pushed the feature/trezor-common branch 2 times, most recently from e9d69d2 to c57154a Compare December 12, 2024 09:14
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, EnumDiscriminants)]
#[strum_discriminants(name(OutputTimeLockIndex), derive(EnumIter, FromPrimitive))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see, OutputTimeLockIndex has nothing to do with OutputTimeLock, same as it was for AccountCommandIndex and OutPointSourceIdIndex. But it must be conmsistent with lock_type returned by get_lock in the trezor repo. So it makes sense to do the same for it as well - move it to rust code in the trezo repo, add same enum to the .proto file, write tests that ensures their consistency.

P.S. probably these extra protobuf enums should go to a separate file, because they have nothing to do with the protocol and only serve as a way to have the same constants in python and rust.

}

fn make_random_account_command(rng: &mut (impl Rng + CryptoRng)) -> chain::AccountCommand {
match crate::AccountCommandIndex::iter().choose(rng).unwrap() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, the idea was that you add Tag enum to chain::AccountCommand so that if it gets more variants, we get a compilation failure here. Same for other "XXXIndex" enums.

Also, I suggest calling them "XXXTag", because "index" is a bit confusing IMO. I.e. we also have those #[codec(index = n)] annotations on the "parent" enums, which may not be consecutive, and "XXXIndex" sounds like at has something to do with those indices. And "XXXTag" sounds like it's just a name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is hard to re-export the AccountCommandTag from the original as it would bring a dependency on that crate, and it is not nostd compliant. As for the handling of new variants it will be caught by the From implementations, they do a match on the original enums.

I replaced Index with Type as it sounds better to me, AccountCommandType than Tag?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is hard to re-export the AccountCommandTag from the original as it would bring a dependency on that crate, and it is not nostd compliant.

But it's for tests only, which can use std, right? I mean, you already use chain::DestinationTag, which also comes from a crate that is not nostd compliant.

As for the handling of new variants it will be caught by the From implementations, they do a match on the original enums.

Yeah, I guess it will work. But IMO it'd still be a bit nicer if the match was over Tag types (or whatever they will be called) that come with the type that is being generated. Otherwise it's not obvious that the test actually generates all possible "chain::AccountCommand"s

I replaced Index with Type as it sounds better to me, AccountCommandType than Tag?

Doesn't Type sound ambiguous?
In any case, we should probably use the same suffix for all such discriminant types. I.e. if "tag" is not good enough, chain::DestinationTag should be renamed too (I can do it myself or you can do it).
I'm not sure Type is a good alternative though. "Discriminant" might be better, but it's also longer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are used in the Trezor firmware repo. On the python side we have the enums from the proto files, but on the rust side we use this enums. I added tests to make sure both of the enums correspond to each other.

I guess we can use Tag for consistency and it being short.

@OBorce OBorce force-pushed the feature/trezor-common branch from e7610ee to 9fcfb4b Compare December 13, 2024 09:38
@OBorce OBorce force-pushed the feature/trezor-common branch 2 times, most recently from c9cebcc to d8de7c1 Compare December 13, 2024 09:55
@OBorce OBorce force-pushed the feature/trezor-common branch from d8de7c1 to ebc5349 Compare December 13, 2024 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants