github.com/okex/exchain@v1.8.0/libs/tendermint/types/results_test.go (about)

     1  package types
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/tendermint/go-amino"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	abci "github.com/okex/exchain/libs/tendermint/abci/types"
    13  )
    14  
    15  func TestABCIResults(t *testing.T) {
    16  	a := ABCIResult{Code: 0, Data: nil}
    17  	b := ABCIResult{Code: 0, Data: []byte{}}
    18  	c := ABCIResult{Code: 0, Data: []byte("one")}
    19  	d := ABCIResult{Code: 14, Data: nil}
    20  	e := ABCIResult{Code: 14, Data: []byte("foo")}
    21  	f := ABCIResult{Code: 14, Data: []byte("bar")}
    22  
    23  	// Nil and []byte{} should produce the same bytes
    24  	require.Equal(t, a.Bytes(), a.Bytes())
    25  	require.Equal(t, b.Bytes(), b.Bytes())
    26  	require.Equal(t, a.Bytes(), b.Bytes())
    27  
    28  	// a and b should be the same, don't go in results.
    29  	results := ABCIResults{a, c, d, e, f}
    30  
    31  	// Make sure each result serializes differently
    32  	var last []byte
    33  	assert.Equal(t, last, a.Bytes()) // first one is empty
    34  	for i, res := range results[1:] {
    35  		bz := res.Bytes()
    36  		assert.NotEqual(t, last, bz, "%d", i)
    37  		last = bz
    38  	}
    39  
    40  	// Make sure that we can get a root hash from results and verify proofs.
    41  	root := results.Hash()
    42  	assert.NotEmpty(t, root)
    43  
    44  	for i, res := range results {
    45  		proof := results.ProveResult(i)
    46  		valid := proof.Verify(root, res.Bytes())
    47  		assert.NoError(t, valid, "%d", i)
    48  	}
    49  }
    50  
    51  func TestABCIResultsBytes(t *testing.T) {
    52  	results := NewResults([]*abci.ResponseDeliverTx{
    53  		{Code: 0, Data: []byte{}},
    54  		{Code: 0, Data: []byte("one")},
    55  		{Code: 14, Data: nil},
    56  		{Code: 14, Data: []byte("foo")},
    57  		{Code: 14, Data: []byte("bar")},
    58  	})
    59  	assert.NotNil(t, results.Bytes())
    60  }
    61  
    62  func TestABCIResultAmino(t *testing.T) {
    63  	testCases := []ABCIResult{
    64  		{},
    65  		{Code: 100, Data: []byte("this is data")},
    66  		{Code: math.MaxUint32, Data: []byte{}},
    67  		{Code: 0, Data: []byte{}},
    68  		{Code: 0, Data: []byte("one")},
    69  		{Code: 14, Data: nil},
    70  		{Code: 14, Data: []byte("foo")},
    71  		{Code: 14, Data: []byte("bar")},
    72  	}
    73  	cdc := amino.NewCodec()
    74  	for _, res := range testCases {
    75  		expectData, err := cdc.MarshalBinaryBare(res)
    76  		require.NoError(t, err)
    77  
    78  		actualData, err := res.MarshalToAmino(cdc)
    79  		require.NoError(t, err)
    80  
    81  		require.Equal(t, expectData, actualData)
    82  		require.Equal(t, len(expectData), res.AminoSize())
    83  
    84  		bytesData := cdcEncode(res)
    85  		require.Equal(t, bytesData, actualData)
    86  	}
    87  }
    88  
    89  func BenchmarkABCIResultBytes(b *testing.B) {
    90  	res := ABCIResult{12345, []byte("some data")}
    91  	b.ResetTimer()
    92  	b.Run("cdcEncode", func(b *testing.B) {
    93  		b.ReportAllocs()
    94  		for i := 0; i < b.N; i++ {
    95  			_ = cdcEncode(res)
    96  		}
    97  	})
    98  	b.Run("marshaller", func(b *testing.B) {
    99  		b.ReportAllocs()
   100  		for i := 0; i < b.N; i++ {
   101  			_, _ = res.MarshalToAmino(cdc)
   102  		}
   103  	})
   104  }