github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/rpc/core/blocks_test.go (about)

     1  package core
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	dbm "github.com/tendermint/tm-db"
    12  
    13  	abci "github.com/ari-anchor/sei-tendermint/abci/types"
    14  	sm "github.com/ari-anchor/sei-tendermint/internal/state"
    15  	"github.com/ari-anchor/sei-tendermint/internal/state/mocks"
    16  	"github.com/ari-anchor/sei-tendermint/rpc/coretypes"
    17  )
    18  
    19  func TestBlockchainInfo(t *testing.T) {
    20  	cases := []struct {
    21  		min, max     int64
    22  		base, height int64
    23  		limit        int64
    24  		resultLength int64
    25  		wantErr      bool
    26  	}{
    27  
    28  		// min > max
    29  		{0, 0, 0, 0, 10, 0, true},  // min set to 1
    30  		{0, 1, 0, 0, 10, 0, true},  // max set to height (0)
    31  		{0, 0, 0, 1, 10, 1, false}, // max set to height (1)
    32  		{2, 0, 0, 1, 10, 0, true},  // max set to height (1)
    33  		{2, 1, 0, 5, 10, 0, true},
    34  
    35  		// negative
    36  		{1, 10, 0, 14, 10, 10, false}, // control
    37  		{-1, 10, 0, 14, 10, 0, true},
    38  		{1, -10, 0, 14, 10, 0, true},
    39  		{-9223372036854775808, -9223372036854775788, 0, 100, 20, 0, true},
    40  
    41  		// check base
    42  		{1, 1, 1, 1, 1, 1, false},
    43  		{2, 5, 3, 5, 5, 3, false},
    44  
    45  		// check limit and height
    46  		{1, 1, 0, 1, 10, 1, false},
    47  		{1, 1, 0, 5, 10, 1, false},
    48  		{2, 2, 0, 5, 10, 1, false},
    49  		{1, 2, 0, 5, 10, 2, false},
    50  		{1, 5, 0, 1, 10, 1, false},
    51  		{1, 5, 0, 10, 10, 5, false},
    52  		{1, 15, 0, 10, 10, 10, false},
    53  		{1, 15, 0, 15, 10, 10, false},
    54  		{1, 15, 0, 15, 20, 15, false},
    55  		{1, 20, 0, 15, 20, 15, false},
    56  		{1, 20, 0, 20, 20, 20, false},
    57  	}
    58  
    59  	for i, c := range cases {
    60  		caseString := fmt.Sprintf("test %d failed", i)
    61  		min, max, err := filterMinMax(c.base, c.height, c.min, c.max, c.limit)
    62  		if c.wantErr {
    63  			require.Error(t, err, caseString)
    64  		} else {
    65  			require.NoError(t, err, caseString)
    66  			require.Equal(t, 1+max-min, c.resultLength, caseString)
    67  		}
    68  	}
    69  }
    70  
    71  func TestBlockResults(t *testing.T) {
    72  	results := &abci.ResponseFinalizeBlock{
    73  		TxResults: []*abci.ExecTxResult{
    74  			{Code: 0, Data: []byte{0x01}, Log: "ok", GasUsed: 10},
    75  			{Code: 0, Data: []byte{0x02}, Log: "ok", GasUsed: 5},
    76  			{Code: 1, Log: "not ok", GasUsed: 0},
    77  		},
    78  	}
    79  
    80  	env := &Environment{}
    81  	env.StateStore = sm.NewStore(dbm.NewMemDB())
    82  	err := env.StateStore.SaveFinalizeBlockResponses(100, results)
    83  	require.NoError(t, err)
    84  	mockstore := &mocks.BlockStore{}
    85  	mockstore.On("Height").Return(int64(100))
    86  	mockstore.On("Base").Return(int64(1))
    87  	env.BlockStore = mockstore
    88  
    89  	testCases := []struct {
    90  		height  int64
    91  		wantErr bool
    92  		wantRes *coretypes.ResultBlockResults
    93  	}{
    94  		{-1, true, nil},
    95  		{0, true, nil},
    96  		{101, true, nil},
    97  		{100, false, &coretypes.ResultBlockResults{
    98  			Height:                100,
    99  			TxsResults:            results.TxResults,
   100  			TotalGasUsed:          15,
   101  			FinalizeBlockEvents:   results.Events,
   102  			ValidatorUpdates:      results.ValidatorUpdates,
   103  			ConsensusParamUpdates: results.ConsensusParamUpdates,
   104  		}},
   105  	}
   106  
   107  	ctx := context.Background()
   108  	for _, tc := range testCases {
   109  		res, err := env.BlockResults(ctx, &coretypes.RequestBlockInfo{
   110  			Height: (*coretypes.Int64)(&tc.height),
   111  		})
   112  		if tc.wantErr {
   113  			assert.Error(t, err)
   114  		} else {
   115  			assert.NoError(t, err)
   116  			assert.Equal(t, tc.wantRes, res)
   117  		}
   118  	}
   119  }