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

chore(lib/grandpa): integrate scale into grandpa #1811

Merged
merged 242 commits into from
Sep 30, 2021

Conversation

jimjbrettj
Copy link
Contributor

Changes

  • Remove the old scale library from grandpa
  • Remove as much custom serialization logic as possible from grandpa

Tests

go test ./dot/types -v
go test ./dot/state -v -short
go test ./lib/grandpa -v -short

Issues

Primary Reviewer

dot/state/grandpa_test.go Outdated Show resolved Hide resolved
Comment on lines 120 to 123
func (v *GrandpaVoter) Encode() ([]byte, error) {
enc := []byte{}
b := v.Key.Encode()
enc = append(enc, b...)
Copy link
Contributor

Choose a reason for hiding this comment

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

does the new SCALE package work with custom encodings? for example, if scale.Marshal(*GrandpaVoter) is called, will this function be called by SCALE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it does not. I also do not believe the GrandpaVoter is scale encoded because of the ed25519.PublicKey (Don't think the length is the leading byte of the encoded pubkey)

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, it's always a []byte of length 32. unfortunately the ed25519 has it just as []byte instead of [32]byte so we can't directly SCALE encode it. maybe as a next step for SCALE we can add custom type encodings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think that could be a good idea

Copy link
Contributor

Choose a reason for hiding this comment

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

this method should just be

return scale.Marshal(v)

if you change ed25519.PublicKey to [32]byte

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 we could just delete it all together and just directly marshal/unmarshal. Would also allow us to get rid of the custom encode and decode functions for GrandpaVoters

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 will look into this now actually. Could be a nice change to include in this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

if this is a huge change maybe do it in another PR, since I think the crypto/ed25519 golang lib we're using has some nice functions that we currently use (but maybe it's not a huge change)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah good call, I will do this in a separate PR

lib/grandpa/grandpa.go Outdated Show resolved Hide resolved
lib/grandpa/message.go Outdated Show resolved Hide resolved

if err != nil {
return nil, err
return nil, fmt.Errorf("message type not recognised")
Copy link
Contributor

Choose a reason for hiding this comment

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

why not ErrInvalidMessageType?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will change

lib/grandpa/network.go Outdated Show resolved Hide resolved
Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

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

looks good!! basically just a question about the decodeMessage function, I think everything else is fine

"fmt"
"io"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/scale"
scale2 "github.com/ChainSafe/gossamer/pkg/scale"
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need both scale packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well right now encoding/decoding for the grandpaVoter is using custom logic (since its not scale encoded I do not believe) and its uses the old scale to encode/decode. Dot/types is the integration I will finish after this one is merged so I was planning to fix it then but if you want it in this pr I can do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

As long as we have an issue for this that's fine.

primaryProposal subround = 2
Prevote Subround
Precommit Subround = 1
PrimaryProposal Subround = 2
Copy link
Contributor

Choose a reason for hiding this comment

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

why are these public now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So scale can access them when decoding/encoding. I believe this is necessary but I can change if I am wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

The values don't need to be public, only the attribute names if they are being used as an attribute in a struct.

Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

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

looks good, would be nice to either add SCALE custom encoding or change our crypto/ed25519/PublicKey type to be [32]byte so that it can be used with new SCALE, maybe wanna open issues for these?

@timwu20
Copy link
Contributor

timwu20 commented Sep 29, 2021

looks good, would be nice to either add SCALE custom encoding or change our crypto/ed25519/PublicKey type to be [32]byte so that it can be used with new SCALE, maybe wanna open issues for these?

I prefer the latter. If it was a [32]byte that would encode/decode easily.

v.ID = id
return nil
}

// Encode will encode the GrandpaVoter
func (v *GrandpaVoter) Encode() ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you update all the receiver functions.

Suggested change
func (v *GrandpaVoter) Encode() ([]byte, error) {
func (gv *GrandpaVoter) Encode() ([]byte, error) {

func justificationToCompact(just []*SignedVote) ([]*Vote, []*AuthData) {
precommits := make([]*Vote, len(just))
authData := make([]*AuthData, len(just))
func justificationToCompact(just []SignedVote) ([]Vote, []AuthData) {
Copy link
Member

Choose a reason for hiding this comment

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

this function name does not correspond to the action IMO, I guess splitJustification or fromJustificationToVoteAndAuth

Copy link
Contributor Author

@jimjbrettj jimjbrettj Sep 29, 2021

Choose a reason for hiding this comment

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

That makes sense to me. @noot or @timwu20 do you have thoughts on this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method should be a receiver function of a new type called type SignedVotes []SignedVote.

Copy link
Contributor

Choose a reason for hiding this comment

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

I named it this way to match substrate's naming, so I think it's ok to leave as is but up to your judgement

@noot noot merged commit 33059ad into development Sep 30, 2021
@noot noot deleted the jimmy/grandpaIntegration branch September 30, 2021 09:25
@github-actions
Copy link

github-actions bot commented Dec 3, 2021

🎉 This PR is included in version 0.6.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

timwu20 pushed a commit to timwu20/gossamer that referenced this pull request Dec 6, 2021
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.

4 participants