-
Notifications
You must be signed in to change notification settings - Fork 129
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 (pkg/scale): Integrate scale into grandpa library #1694
Changes from all commits
59374ec
6761109
aee80f6
5a59a19
c4dcbfc
246e771
37fab4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,14 +18,12 @@ package grandpa | |||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"io" | ||||||
"math/big" | ||||||
|
||||||
"github.com/ChainSafe/gossamer/dot/network" | ||||||
"github.com/ChainSafe/gossamer/dot/types" | ||||||
"github.com/ChainSafe/gossamer/lib/common" | ||||||
"github.com/ChainSafe/gossamer/lib/crypto/ed25519" | ||||||
"github.com/ChainSafe/gossamer/lib/scale" | ||||||
"github.com/ChainSafe/gossamer/pkg/scale" | ||||||
) | ||||||
|
||||||
// GrandpaMessage is implemented by all GRANDPA network messages | ||||||
|
@@ -46,15 +44,15 @@ var ( | |||||
// FullVote represents a vote with additional information about the state | ||||||
// this is encoded and signed and the signature is included in SignedMessage | ||||||
type FullVote struct { | ||||||
Stage subround | ||||||
Stage Subround | ||||||
Vote *Vote | ||||||
Round uint64 | ||||||
SetID uint64 | ||||||
} | ||||||
|
||||||
// SignedMessage represents a block hash and number signed by an authority | ||||||
type SignedMessage struct { | ||||||
Stage subround // 0 for pre-vote, 1 for pre-commit, 2 for primary proposal | ||||||
Stage Subround // 0 for pre-vote, 1 for pre-commit, 2 for primary proposal | ||||||
Hash common.Hash | ||||||
Number uint32 | ||||||
Signature [64]byte // ed25519.SignatureLength | ||||||
|
@@ -66,37 +64,6 @@ func (m *SignedMessage) String() string { | |||||
return fmt.Sprintf("stage=%s hash=%s number=%d authorityID=%s", m.Stage, m.Hash, m.Number, m.AuthorityID) | ||||||
} | ||||||
|
||||||
// Decode SCALE decodes the data into a SignedMessage | ||||||
func (m *SignedMessage) Decode(r io.Reader) (err error) { | ||||||
m.Stage, err = subround(0).Decode(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
vote, err := new(Vote).Decode(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
m.Hash = vote.Hash | ||||||
m.Number = vote.Number | ||||||
|
||||||
sig, err := common.Read64Bytes(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
copy(m.Signature[:], sig[:]) | ||||||
|
||||||
id, err := common.Read32Bytes(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
copy(m.AuthorityID[:], id[:]) | ||||||
return nil | ||||||
} | ||||||
|
||||||
// VoteMessage represents a network-level vote message | ||||||
// https://github.com/paritytech/substrate/blob/master/client/finality-grandpa/src/communication/gossip.rs#L336 | ||||||
type VoteMessage struct { | ||||||
|
@@ -105,31 +72,17 @@ type VoteMessage struct { | |||||
Message *SignedMessage | ||||||
} | ||||||
|
||||||
// Decode SCALE decodes the data into a VoteMessage | ||||||
func (v *VoteMessage) Decode(r io.Reader) (err error) { | ||||||
v.Round, err = common.ReadUint64(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
v.SetID, err = common.ReadUint64(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
v.Message = new(SignedMessage) | ||||||
err = v.Message.Decode(r) | ||||||
return err | ||||||
} | ||||||
|
||||||
// Type returns voteType | ||||||
func (v *VoteMessage) Type() byte { | ||||||
return voteType | ||||||
} | ||||||
|
||||||
// Index Returns VDT index | ||||||
func (v *VoteMessage) Index() uint { return 0 } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Update all the other receiver functions too. |
||||||
|
||||||
// ToConsensusMessage converts the VoteMessage into a network-level consensus message | ||||||
func (v *VoteMessage) ToConsensusMessage() (*ConsensusMessage, error) { | ||||||
enc, err := scale.Encode(v) | ||||||
enc, err := scale.Marshal(v) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -147,9 +100,12 @@ type NeighbourMessage struct { | |||||
Number uint32 | ||||||
} | ||||||
|
||||||
// Index Returns VDT index | ||||||
func (m *NeighbourMessage) Index() uint { return 2 } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// ToConsensusMessage converts the NeighbourMessage into a network-level consensus message | ||||||
func (m *NeighbourMessage) ToConsensusMessage() (*network.ConsensusMessage, error) { | ||||||
enc, err := scale.Encode(m) | ||||||
enc, err := scale.Marshal(m) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -170,29 +126,6 @@ type AuthData struct { | |||||
AuthorityID ed25519.PublicKeyBytes | ||||||
} | ||||||
|
||||||
// Encode SCALE encodes the AuthData | ||||||
func (d *AuthData) Encode() ([]byte, error) { | ||||||
return append(d.Signature[:], d.AuthorityID[:]...), nil | ||||||
} | ||||||
|
||||||
// Decode SCALE decodes the data into an AuthData | ||||||
func (d *AuthData) Decode(r io.Reader) error { | ||||||
sig, err := common.Read64Bytes(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
copy(d.Signature[:], sig[:]) | ||||||
|
||||||
id, err := common.Read32Bytes(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
copy(d.AuthorityID[:], id[:]) | ||||||
return nil | ||||||
} | ||||||
|
||||||
// CommitMessage represents a network finalisation message | ||||||
type CommitMessage struct { | ||||||
Round uint64 | ||||||
|
@@ -202,57 +135,8 @@ type CommitMessage struct { | |||||
AuthData []*AuthData | ||||||
} | ||||||
|
||||||
// Decode SCALE decodes the data into a CommitMessage | ||||||
func (f *CommitMessage) Decode(r io.Reader) (err error) { | ||||||
f.Round, err = common.ReadUint64(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
f.SetID, err = common.ReadUint64(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
f.Vote, err = new(Vote).Decode(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
sd := &scale.Decoder{Reader: r} | ||||||
numPrecommits, err := sd.Decode(new(big.Int)) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
f.Precommits = make([]*Vote, numPrecommits.(*big.Int).Int64()) | ||||||
for i := range f.Precommits { | ||||||
f.Precommits[i], err = new(Vote).Decode(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
numAuthData, err := sd.Decode(new(big.Int)) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if numAuthData.(*big.Int).Cmp(numPrecommits.(*big.Int)) != 0 { | ||||||
return ErrPrecommitSignatureMismatch | ||||||
} | ||||||
|
||||||
f.AuthData = make([]*AuthData, numAuthData.(*big.Int).Int64()) | ||||||
for i := range f.AuthData { | ||||||
f.AuthData[i] = new(AuthData) | ||||||
err = f.AuthData[i].Decode(r) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
// Index Returns VDT index | ||||||
func (f *CommitMessage) Index() uint { return 1 } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// Type returns commitType | ||||||
func (f *CommitMessage) Type() byte { | ||||||
|
@@ -261,7 +145,7 @@ func (f *CommitMessage) Type() byte { | |||||
|
||||||
// ToConsensusMessage converts the CommitMessage into a network-level consensus message | ||||||
func (f *CommitMessage) ToConsensusMessage() (*ConsensusMessage, error) { | ||||||
enc, err := scale.Encode(f) | ||||||
enc, err := scale.Marshal(f) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -323,6 +207,9 @@ type catchUpRequest struct { | |||||
SetID uint64 | ||||||
} | ||||||
|
||||||
// Index Returns VDT index | ||||||
func (r *catchUpRequest) Index() uint { return 3 } | ||||||
|
||||||
func newCatchUpRequest(round, setID uint64) *catchUpRequest { | ||||||
return &catchUpRequest{ | ||||||
Round: round, | ||||||
|
@@ -337,7 +224,7 @@ func (r *catchUpRequest) Type() byte { | |||||
|
||||||
// ToConsensusMessage converts the catchUpRequest into a network-level consensus message | ||||||
func (r *catchUpRequest) ToConsensusMessage() (*ConsensusMessage, error) { | ||||||
enc, err := scale.Encode(r) | ||||||
enc, err := scale.Marshal(r) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -356,6 +243,9 @@ type catchUpResponse struct { | |||||
Number uint32 | ||||||
} | ||||||
|
||||||
// Index Returns VDT index | ||||||
func (r *catchUpResponse) Index() uint { return 4 } | ||||||
|
||||||
func (s *Service) newCatchUpResponse(round, setID uint64) (*catchUpResponse, error) { | ||||||
header, err := s.blockState.GetFinalisedHeader(round, setID) | ||||||
if err != nil { | ||||||
|
@@ -389,7 +279,7 @@ func (r *catchUpResponse) Type() byte { | |||||
|
||||||
// ToConsensusMessage converts the catchUpResponse into a network-level consensus message | ||||||
func (r *catchUpResponse) ToConsensusMessage() (*ConsensusMessage, error) { | ||||||
enc, err := scale.Encode(r) | ||||||
enc, err := scale.Marshal(r) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: create a local var for the justification on another line