Skip to content

Commit

Permalink
Updated Marshal and unmarshal methods to make compatible with protobuf (
Browse files Browse the repository at this point in the history
#2918)

* upadtes in grpc Marshal and unmarshal

* update comments
  • Loading branch information
vlasfama authored and ebuchman committed Nov 27, 2018
1 parent 94e63be commit bef39f3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
28 changes: 28 additions & 0 deletions consensus/types/peer_round_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,31 @@ func (prs PeerRoundState) StringIndented(indent string) string {
indent, prs.CatchupCommit, prs.CatchupCommitRound,
indent)
}

//-----------------------------------------------------------
// These methods are for Protobuf Compatibility

// Size returns the size of the amino encoding, in bytes.
func (ps *PeerRoundState) Size() int {
bs, _ := ps.Marshal()
return len(bs)
}

// Marshal returns the amino encoding.
func (ps *PeerRoundState) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(ps)
}

// MarshalTo calls Marshal and copies to the given buffer.
func (ps *PeerRoundState) MarshalTo(data []byte) (int, error) {
bs, err := ps.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}

// Unmarshal deserializes from amino encoded form.
func (ps *PeerRoundState) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, ps)
}
28 changes: 28 additions & 0 deletions consensus/types/round_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,31 @@ func (rs *RoundState) StringShort() string {
return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
rs.Height, rs.Round, rs.Step, rs.StartTime)
}

//-----------------------------------------------------------
// These methods are for Protobuf Compatibility

// Size returns the size of the amino encoding, in bytes.
func (rs *RoundStateSimple) Size() int {
bs, _ := rs.Marshal()
return len(bs)
}

// Marshal returns the amino encoding.
func (rs *RoundStateSimple) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(rs)
}

// MarshalTo calls Marshal and copies to the given buffer.
func (rs *RoundStateSimple) MarshalTo(data []byte) (int, error) {
bs, err := rs.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}

// Unmarshal deserializes from amino encoded form.
func (rs *RoundStateSimple) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, rs)
}
28 changes: 28 additions & 0 deletions p2p/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,31 @@ func (info DefaultNodeInfo) NetAddress() *NetAddress {
}
return netAddr
}

//-----------------------------------------------------------
// These methods are for Protobuf Compatibility

// Size returns the size of the amino encoding, in bytes.
func (info *DefaultNodeInfo) Size() int {
bs, _ := info.Marshal()
return len(bs)
}

// Marshal returns the amino encoding.
func (info *DefaultNodeInfo) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(info)
}

// MarshalTo calls Marshal and copies to the given buffer.
func (info *DefaultNodeInfo) MarshalTo(data []byte) (int, error) {
bs, err := info.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}

// Unmarshal deserializes from amino encoded form.
func (info *DefaultNodeInfo) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, info)
}
22 changes: 22 additions & 0 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ func (b *Block) StringShort() string {
return fmt.Sprintf("Block#%v", b.Hash())
}

//-----------------------------------------------------------
// These methods are for Protobuf Compatibility

// Marshal returns the amino encoding.
func (b *Block) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(b)
}

// MarshalTo calls Marshal and copies to the given buffer.
func (b *Block) MarshalTo(data []byte) (int, error) {
bs, err := b.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}

// Unmarshal deserializes from amino encoded form.
func (b *Block) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, b)
}

//-----------------------------------------------------------------------------

// MaxDataBytes returns the maximum size of block's data.
Expand Down
28 changes: 28 additions & 0 deletions types/block_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,31 @@ func NewBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
Header: block.Header,
}
}

//-----------------------------------------------------------
// These methods are for Protobuf Compatibility

// Size returns the size of the amino encoding, in bytes.
func (bm *BlockMeta) Size() int {
bs, _ := bm.Marshal()
return len(bs)
}

// Marshal returns the amino encoding.
func (bm *BlockMeta) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(bm)
}

// MarshalTo calls Marshal and copies to the given buffer.
func (bm *BlockMeta) MarshalTo(data []byte) (int, error) {
bs, err := bm.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}

// Unmarshal deserializes from amino encoded form.
func (bm *BlockMeta) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, bm)
}

0 comments on commit bef39f3

Please sign in to comment.