proto: add proto files for ibc unblock (#4853)

## Description

these proto files are meant to help unblock ibc in their quest of migrating the ibc module to proto.

Closes: #XXX
This commit is contained in:
Marko
2020-05-25 17:52:34 +02:00
committed by GitHub
parent 970cbbad6d
commit e03b61abd2
53 changed files with 6116 additions and 1006 deletions

View File

@@ -9,6 +9,7 @@ import (
tmmath "github.com/tendermint/tendermint/libs/math"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmprotobits "github.com/tendermint/tendermint/proto/libs/bits"
)
// BitArray is a thread-safe implementation of a bit array.
@@ -418,3 +419,28 @@ func (bA *BitArray) UnmarshalJSON(bz []byte) error {
*bA = *bA2 //nolint:govet
return nil
}
// ToProto converts BitArray to protobuf
func (bA *BitArray) ToProto() *tmprotobits.BitArray {
if bA == nil || len(bA.Elems) == 0 {
return nil
}
return &tmprotobits.BitArray{
Bits: int64(bA.Bits),
Elems: bA.Elems,
}
}
// FromProto sets a protobuf BitArray to the given pointer.
func (bA *BitArray) FromProto(protoBitArray *tmprotobits.BitArray) {
if protoBitArray == nil {
bA = nil
return
}
bA.Bits = int(protoBitArray.Bits)
if len(protoBitArray.Elems) > 0 {
bA.Elems = protoBitArray.Elems
}
}

View File

@@ -265,3 +265,26 @@ func TestJSONMarshalUnmarshal(t *testing.T) {
})
}
}
func TestBitArrayProtoBuf(t *testing.T) {
testCases := []struct {
msg string
bA1 *BitArray
expPass bool
}{
{"success empty", &BitArray{}, true},
{"success", NewBitArray(1), true},
{"success", NewBitArray(2), true},
{"negative", NewBitArray(-1), false},
}
for _, tc := range testCases {
protoBA := tc.bA1.ToProto()
ba := new(BitArray)
ba.FromProto(protoBA)
if tc.expPass {
require.Equal(t, tc.bA1, ba, tc.msg)
} else {
require.NotEqual(t, tc.bA1, ba, tc.msg)
}
}
}