Browse Source

Updated Marshal and unmarshal methods to make compatible with protobuf (#2918)

* upadtes in grpc Marshal and unmarshal

* update comments
pull/2922/head
nagarajmanjunath 6 years ago
committed by Ethan Buchman
parent
commit
bef39f3346
5 changed files with 134 additions and 0 deletions
  1. +28
    -0
      consensus/types/peer_round_state.go
  2. +28
    -0
      consensus/types/round_state.go
  3. +28
    -0
      p2p/node_info.go
  4. +22
    -0
      types/block.go
  5. +28
    -0
      types/block_meta.go

+ 28
- 0
consensus/types/peer_round_state.go View File

@ -55,3 +55,31 @@ func (prs PeerRoundState) StringIndented(indent string) string {
indent, prs.CatchupCommit, prs.CatchupCommitRound, indent, prs.CatchupCommit, prs.CatchupCommitRound,
indent) 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
- 0
consensus/types/round_state.go View File

@ -201,3 +201,31 @@ func (rs *RoundState) StringShort() string {
return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`, return fmt.Sprintf(`RoundState{H:%v R:%v S:%v ST:%v}`,
rs.Height, rs.Round, rs.Step, rs.StartTime) 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
- 0
p2p/node_info.go View File

@ -230,3 +230,31 @@ func (info DefaultNodeInfo) NetAddress() *NetAddress {
} }
return netAddr 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
- 0
types/block.go View File

@ -275,6 +275,28 @@ func (b *Block) StringShort() string {
return fmt.Sprintf("Block#%v", b.Hash()) 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. // MaxDataBytes returns the maximum size of block's data.


+ 28
- 0
types/block_meta.go View File

@ -13,3 +13,31 @@ func NewBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
Header: block.Header, 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)
}

Loading…
Cancel
Save