Skip to content

Commit

Permalink
vote supports customized enc/dec
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi Zhou committed Mar 4, 2022
1 parent 547eacc commit 4b69999
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/types/chamber_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (p *Proposal) ValidateBasic() error {
}

// block will be append to end of stream
type ProposalRaw struct {
type proposalRaw struct {
Height uint64
Round uint32
POLRound uint32
Expand All @@ -136,7 +136,7 @@ type ProposalRaw struct {
}

func (p *Proposal) EncodeRLP(w io.Writer) error {
if err := rlp.Encode(w, ProposalRaw{
if err := rlp.Encode(w, proposalRaw{
Height: uint64(p.Height),
Round: uint32(p.Round),
POLRound: uint32(p.POLRound),
Expand All @@ -151,7 +151,7 @@ func (p *Proposal) EncodeRLP(w io.Writer) error {
}

func (p *Proposal) DecodeRLP(s *rlp.Stream) error {
var pr ProposalRaw
var pr proposalRaw
if err := s.Decode(&pr); err != nil {
return err
}
Expand Down
43 changes: 43 additions & 0 deletions core/types/chamber_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"errors"
"fmt"
"io"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
Expand Down Expand Up @@ -153,3 +154,45 @@ func (vote *Vote) ValidateBasic() error {

return nil
}

type voteRaw struct {
Type uint32
Height uint64
Round uint32
BlockID common.Hash
Timestamp uint64
ValidatorAddress common.Address
ValidatorIndex uint32
Signature []byte
}

func (v *Vote) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &voteRaw{
Type: uint32(v.Type),
Height: uint64(v.Height),
Round: uint32(v.Round),
BlockID: v.BlockID,
Timestamp: uint64(v.TimestampMs),
ValidatorAddress: v.ValidatorAddress,
ValidatorIndex: uint32(v.ValidatorIndex),
Signature: v.Signature,
})
}

func (v *Vote) DecodeRLP(s *rlp.Stream) error {
var vr voteRaw
if err := s.Decode(&vr); err != nil {
return err
}

v.Type = SignedMsgType(vr.Type)
v.Height = vr.Height
v.Round = int32(vr.Round)
v.BlockID = vr.BlockID
v.TimestampMs = vr.Timestamp
v.ValidatorAddress = vr.ValidatorAddress
v.ValidatorIndex = int32(vr.ValidatorIndex)
v.Signature = vr.Signature

return nil
}

0 comments on commit 4b69999

Please sign in to comment.