github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/types/proto3_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/golang/protobuf/proto" // nolint: staticcheck // still used by gogoproto
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/tendermint/tendermint/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  		LastBlockID: &proto3.BlockID{
    28  			Hash: []byte("some serious hashing"),
    29  			PartsHeader: &proto3.PartSetHeader{
    30  				Total: 8,
    31  				Hash:  []byte("some more serious hashing"),
    32  			},
    33  		},
    34  		LastCommitHash: []byte("commit hash"),
    35  		DataHash:       []byte("data hash"),
    36  		ValidatorsHash: []byte("validators hash"),
    37  	}
    38  	aminoHeader := Header{
    39  		ChainID: "cosmos",
    40  		Height:  150,
    41  		Time:    tm,
    42  		LastBlockID: BlockID{
    43  			Hash: []byte("some serious hashing"),
    44  			PartsHeader: PartSetHeader{
    45  				Total: 8,
    46  				Hash:  []byte("some more serious hashing"),
    47  			},
    48  		},
    49  		LastCommitHash: []byte("commit hash"),
    50  		DataHash:       []byte("data hash"),
    51  		ValidatorsHash: []byte("validators hash"),
    52  	}
    53  	ab, err := cdc.MarshalBinaryBare(aminoHeader)
    54  	assert.NoError(t, err, "unexpected error")
    55  
    56  	pb, err := proto.Marshal(&pbHeader)
    57  	assert.NoError(t, err, "unexpected error")
    58  	// This works:
    59  	assert.Equal(t, ab, pb, "encoding doesn't match")
    60  
    61  	emptyLastBlockPb := proto3.Header{
    62  		ChainID:        "cosmos",
    63  		Height:         150,
    64  		Time:           &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
    65  		LastCommitHash: []byte("commit hash"),
    66  		DataHash:       []byte("data hash"),
    67  		ValidatorsHash: []byte("validators hash"),
    68  	}
    69  	emptyLastBlockAm := Header{
    70  		ChainID:        "cosmos",
    71  		Height:         150,
    72  		Time:           tm,
    73  		LastCommitHash: []byte("commit hash"),
    74  		DataHash:       []byte("data hash"),
    75  		ValidatorsHash: []byte("validators hash"),
    76  	}
    77  
    78  	ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
    79  	assert.NoError(t, err, "unexpected error")
    80  
    81  	pb, err = proto.Marshal(&emptyLastBlockPb)
    82  	assert.NoError(t, err, "unexpected error")
    83  	// This works:
    84  	assert.Equal(t, ab, pb, "encoding doesn't match")
    85  
    86  	pb, err = proto.Marshal(&proto3.Header{})
    87  	assert.NoError(t, err, "unexpected error")
    88  	t.Log(pb)
    89  
    90  	// While in protobuf Header{} encodes to an empty byte slice it does not in amino:
    91  	ab, err = cdc.MarshalBinaryBare(Header{})
    92  	assert.NoError(t, err, "unexpected error")
    93  	t.Log(ab)
    94  
    95  	pb, err = proto.Marshal(&proto3.Timestamp{})
    96  	assert.NoError(t, err, "unexpected error")
    97  	t.Log(pb)
    98  
    99  	ab, err = cdc.MarshalBinaryBare(time.Time{})
   100  	assert.NoError(t, err, "unexpected error")
   101  	t.Log(ab)
   102  }