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

chg: implement cometBFT changes and adapt tests accordingly #8

Merged
merged 15 commits into from
Mar 15, 2024

Conversation

marcello33
Copy link
Collaborator

This PR reflects the changes of cometBFT (peppermint rebase) into cosmos-sdk.

crypto/codec/cmt.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@sergio-mena sergio-mena left a comment

Choose a reason for hiding this comment

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

🙏 LGTM

option (amino.name) = "tendermint/PrivKeySecp256k1";
option (amino.message_encoding) = "key_field";

bytes key = 1;
}

// PrivKey defines a comet uncompressed secp256k1 private key.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we have (un)compressed private keys 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah but porting the changes from here https://github.com/0xPolygon/cometbft/blob/develop/crypto/secp256k1/secp256k1.go#L22 where we explicitly call PrivKeyName = "comet/PrivKeySecp256k1Uncompressed"
What do you suggest as a comment?

Copy link
Member

@Raneet10 Raneet10 Mar 6, 2024

Choose a reason for hiding this comment

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

I think Uncompressed was added as a suffix to have consistency with the naming; I don't think calling it that means there's actually an uncompressed priv key.
Again, it's just a nitpick. As a comment we might just say PrivKey defines a comet secp256k1 private key. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

WDYS @sergio-mena ?

Copy link
Collaborator Author

@marcello33 marcello33 Mar 6, 2024

Choose a reason for hiding this comment

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

Yeah for sure it's not an "uncompressed private key", I kept there for reference to the naming.
But I see it can create confusion. Addressed here

Copy link
Collaborator

@sergio-mena sergio-mena Mar 13, 2024

Choose a reason for hiding this comment

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

Sorry for delay coming here. In the PR (cometbft/cometbft#2534) I'm working on to upstream these changes to (vanilla) CometBFT I'm not using "uncompressed" because I felt in a similar way to what motivated this thread: I renamed it to cometbft/PrivKeySecp256k1Eth.
Let me know if you'd like me to open two PRs (one here and one on peppermint) to keep the naming consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, please.
Thanks @sergio-mena

simapp/go.mod Outdated Show resolved Hide resolved
simapp/go.mod Outdated Show resolved Hide resolved
tests/go.mod Outdated Show resolved Hide resolved
@marcello33
Copy link
Collaborator Author

marcello33 commented Mar 7, 2024

some tests are failing under crypto/keys
Will fix them

UPDATE: FIXED.
Only waiting for updates on this (armor string for pubKeys)

)

func (privKey *PrivKeyOld) Bytes() []byte {
Copy link
Collaborator

@sergio-mena sergio-mena Mar 13, 2024

Choose a reason for hiding this comment

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

I performed an ad-hoc diff-of-diffs of this directory comparing it to the analogous code from 0xPolygon/cometbft#2.
I think it would ease maintenance if these functions resembled the ones here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that was my direction as well, but I tried to keep the diffs with upstream as small as possible.
For example, I did not change the order of the methods in crypto/keys/secp256k1/secp256k1.go.
Also, some functions are left to crypto/keys/secp256k1/secp256k1_nocgo.go where the original methods of the type (privKey *PrivKey) were defined (e.g. Sign and VerifySignature)

func (privKey *PrivKeyOld) Type() string {
return keyType
}
func (privKey *PrivKeyOld) Sign(msg []byte) ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

PrivKey doesn't have method Sign. Do we need it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is there, but not in the same file. I kept as it was originally for comos-sdk, meaning crypto/keys/secp256k1/secp256k1_nocgo.go, see here


// Address returns an ethereum style addresses
func (pubKey *PubKeyOld) Address() crypto.Address {
return pubKey.Address()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this method calling itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, forgot to push this 🤦
Here it is, thanks

Comment on lines 271 to 322
// Address returns an ethereum style addresses
func (pubKey *PubKeyOld) Address() crypto.Address {
return pubKey.Address()
}

// Bytes returns the pubkey byte format.
func (pubKey *PubKeyOld) Bytes() []byte {
return pubKey.Key
}

func (pubKey *PubKeyOld) String() string {
return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey.Key)
}

func (pubKey *PubKeyOld) Type() string {
return keyType
}

func (pubKey *PubKeyOld) Equals(other cryptotypes.PubKey) bool {
return pubKey.Type() == other.Type() && bytes.Equal(pubKey.Bytes(), other.Bytes())
}

func (pubKey *PubKeyOld) VerifySignature(msg []byte, sigStr []byte) bool {
return pubKey.VerifySignature(msg, sigStr)
}

// MarshalAmino overrides Amino binary marshaling.
func (pubKey PubKeyOld) MarshalAmino() ([]byte, error) {
return pubKey.Key, nil
}

// UnmarshalAmino overrides Amino binary marshaling.
func (pubKey *PubKeyOld) UnmarshalAmino(bz []byte) error {
if len(bz) != PubKeySize {
return errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size")
}
pubKey.Key = bz

return nil
}

// MarshalAminoJSON overrides Amino JSON marshaling.
func (pubKey PubKeyOld) MarshalAminoJSON() ([]byte, error) {
// When we marshal to Amino JSON, we don't marshal the "key" field itself,
// just its contents (i.e. the key bytes).
return pubKey.MarshalAmino()
}

// UnmarshalAminoJSON overrides Amino JSON marshaling.
func (pubKey *PubKeyOld) UnmarshalAminoJSON(bz []byte) error {
return pubKey.UnmarshalAmino(bz)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly to the comment above, it might be simpler to maintain if this code resembled this one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

}

func (pubKey *PubKeyOld) VerifySignature(msg []byte, sigStr []byte) bool {
return pubKey.VerifySignature(msg, sigStr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have the impression this method is calling itself

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, forgot to push this 🤦
Here it is, thanks

x.Key = value.Bytes()
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PubKeyOld"))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PubKeyOld"))
}
panic(fmt.Errorf("message cosmos.crypto.secp256k1.PubKeyOld does not contain field %s", fd.FullName()))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
x.Key = value.Bytes()
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PubKey"))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PubKey"))
}
panic(fmt.Errorf("message cosmos.crypto.secp256k1.PubKey does not contain field %s", fd.FullName()))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
x.Key = value.Bytes()
default:
if fd.IsExtension() {
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PubKey"))
panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.secp256k1.PrivKeyOld"))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
panic(fmt.Errorf("message cosmos.crypto.secp256k1.PubKey does not contain field %s", fd.FullName()))
panic(fmt.Errorf("message cosmos.crypto.secp256k1.PrivKeyOld does not contain field %s", fd.FullName()))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
func (pubKey *PubKey) Address() crypto.Address {
if len(pubKey.Key) != PubKeySize {
panic("length of pubkey is incorrect")
panic(fmt.Sprintf("length of pubkey is incorrect %d != %d", len(pubKey.Key), PubKeySize))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@@ -164,7 +164,7 @@
// in SDK except in a tendermint validator context.
func (pubKey *PubKey) Address() crypto.Address {
if len(pubKey.Key) != PubKeySize {
panic("pubkey is incorrect size")
panic(fmt.Sprintf("length of pubkey is incorrect %d != %d", len(pubKey.Key), PubKeySize))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
// Address returns an ethereum style addresses
func (pubKey *PubKeyOld) Address() crypto.Address {
if len(pubKey.Key) != PubKeySize {
panic(fmt.Sprintf("length of pubkey is incorrect %d != %d", len(pubKey.Key), PubKeySize))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
@marcello33 marcello33 merged commit b48bfdf into mardizzone/POS-1956-auth Mar 15, 2024
28 of 46 checks passed
@marcello33
Copy link
Collaborator Author

FYI @Raneet10 @sergio-mena

This PR has been merged to mardizzone/POS-1956-auth branch, so you can find the changes in this PR too https://github.com/0xPolygon/cosmos-sdk/pull/3/.

This has been done because, after solving the conflicts from devel for mardizzone/POS-1956-auth, due to the changes in this PR #6 the tests in auth and crypto packages were failing again.

Feel free to keep commenting on this PR, especially for open discussions, and I'll make sure to port the changes upstream.

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