github.com/evdatsion/aphelion-dpos-bft@v0.32.1/types/proto3_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/evdatsion/aphelion-dpos-bft/types/proto3"
    11  )
    12  
    13  func TestProto3Compatibility(t *testing.T) {
    14  	tm, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
    15  	assert.NoError(t, err)
    16  	// add some nanos, otherwise protobuf will skip over this while amino (still) won't!
    17  	tm = tm.Add(50000 * time.Nanosecond)
    18  	seconds := tm.Unix()
    19  	nanos := int32(tm.Nanosecond())
    20  	t.Log("seconds", seconds)
    21  	t.Log("nanos", nanos)
    22  
    23  	pbHeader := proto3.Header{
    24  		ChainID: "cosmos",
    25  		Height:  150,
    26  		Time:    &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
    27  		NumTxs:  7,
    28  		LastBlockID: &proto3.BlockID{
    29  			Hash: []byte("some serious hashing"),
    30  			PartsHeader: &proto3.PartSetHeader{
    31  				Total: 8,
    32  				Hash:  []byte("some more serious hashing"),
    33  			},
    34  		},
    35  		TotalTxs:       100,
    36  		LastCommitHash: []byte("commit hash"),
    37  		DataHash:       []byte("data hash"),
    38  		ValidatorsHash: []byte("validators hash"),
    39  	}
    40  	aminoHeader := Header{
    41  		ChainID: "cosmos",
    42  		Height:  150,
    43  		Time:    tm,
    44  		NumTxs:  7,
    45  		LastBlockID: BlockID{
    46  			Hash: []byte("some serious hashing"),
    47  			PartsHeader: PartSetHeader{
    48  				Total: 8,
    49  				Hash:  []byte("some more serious hashing"),
    50  			},
    51  		},
    52  		TotalTxs:       100,
    53  		LastCommitHash: []byte("commit hash"),
    54  		DataHash:       []byte("data hash"),
    55  		ValidatorsHash: []byte("validators hash"),
    56  	}
    57  	ab, err := cdc.MarshalBinaryBare(aminoHeader)
    58  	assert.NoError(t, err, "unexpected error")
    59  
    60  	pb, err := proto.Marshal(&pbHeader)
    61  	assert.NoError(t, err, "unexpected error")
    62  	// This works:
    63  	assert.Equal(t, ab, pb, "encoding doesn't match")
    64  
    65  	emptyLastBlockPb := proto3.Header{
    66  		ChainID:        "cosmos",
    67  		Height:         150,
    68  		Time:           &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
    69  		NumTxs:         7,
    70  		TotalTxs:       100,
    71  		LastCommitHash: []byte("commit hash"),
    72  		DataHash:       []byte("data hash"),
    73  		ValidatorsHash: []byte("validators hash"),
    74  	}
    75  	emptyLastBlockAm := Header{
    76  		ChainID:        "cosmos",
    77  		Height:         150,
    78  		Time:           tm,
    79  		NumTxs:         7,
    80  		TotalTxs:       100,
    81  		LastCommitHash: []byte("commit hash"),
    82  		DataHash:       []byte("data hash"),
    83  		ValidatorsHash: []byte("validators hash"),
    84  	}
    85  
    86  	ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
    87  	assert.NoError(t, err, "unexpected error")
    88  
    89  	pb, err = proto.Marshal(&emptyLastBlockPb)
    90  	assert.NoError(t, err, "unexpected error")
    91  	// This works:
    92  	assert.Equal(t, ab, pb, "encoding doesn't match")
    93  
    94  	pb, err = proto.Marshal(&proto3.Header{})
    95  	assert.NoError(t, err, "unexpected error")
    96  	t.Log(pb)
    97  
    98  	// While in protobuf Header{} encodes to an empty byte slice it does not in amino:
    99  	ab, err = cdc.MarshalBinaryBare(Header{})
   100  	assert.NoError(t, err, "unexpected error")
   101  	t.Log(ab)
   102  
   103  	pb, err = proto.Marshal(&proto3.Timestamp{})
   104  	assert.NoError(t, err, "unexpected error")
   105  	t.Log(pb)
   106  
   107  	ab, err = cdc.MarshalBinaryBare(time.Time{})
   108  	assert.NoError(t, err, "unexpected error")
   109  	t.Log(ab)
   110  }