github.com/vipernet-xyz/tm@v0.34.24/rpc/core/blocks_test.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	dbm "github.com/tendermint/tm-db"
    11  
    12  	abci "github.com/vipernet-xyz/tm/abci/types"
    13  	tmstate "github.com/vipernet-xyz/tm/proto/tendermint/state"
    14  	ctypes "github.com/vipernet-xyz/tm/rpc/core/types"
    15  	rpctypes "github.com/vipernet-xyz/tm/rpc/jsonrpc/types"
    16  	sm "github.com/vipernet-xyz/tm/state"
    17  	"github.com/vipernet-xyz/tm/types"
    18  )
    19  
    20  func TestBlockchainInfo(t *testing.T) {
    21  	cases := []struct {
    22  		min, max     int64
    23  		base, height int64
    24  		limit        int64
    25  		resultLength int64
    26  		wantErr      bool
    27  	}{
    28  
    29  		// min > max
    30  		{0, 0, 0, 0, 10, 0, true},  // min set to 1
    31  		{0, 1, 0, 0, 10, 0, true},  // max set to height (0)
    32  		{0, 0, 0, 1, 10, 1, false}, // max set to height (1)
    33  		{2, 0, 0, 1, 10, 0, true},  // max set to height (1)
    34  		{2, 1, 0, 5, 10, 0, true},
    35  
    36  		// negative
    37  		{1, 10, 0, 14, 10, 10, false}, // control
    38  		{-1, 10, 0, 14, 10, 0, true},
    39  		{1, -10, 0, 14, 10, 0, true},
    40  		{-9223372036854775808, -9223372036854775788, 0, 100, 20, 0, true},
    41  
    42  		// check base
    43  		{1, 1, 1, 1, 1, 1, false},
    44  		{2, 5, 3, 5, 5, 3, false},
    45  
    46  		// check limit and height
    47  		{1, 1, 0, 1, 10, 1, false},
    48  		{1, 1, 0, 5, 10, 1, false},
    49  		{2, 2, 0, 5, 10, 1, false},
    50  		{1, 2, 0, 5, 10, 2, false},
    51  		{1, 5, 0, 1, 10, 1, false},
    52  		{1, 5, 0, 10, 10, 5, false},
    53  		{1, 15, 0, 10, 10, 10, false},
    54  		{1, 15, 0, 15, 10, 10, false},
    55  		{1, 15, 0, 15, 20, 15, false},
    56  		{1, 20, 0, 15, 20, 15, false},
    57  		{1, 20, 0, 20, 20, 20, false},
    58  	}
    59  
    60  	for i, c := range cases {
    61  		caseString := fmt.Sprintf("test %d failed", i)
    62  		min, max, err := filterMinMax(c.base, c.height, c.min, c.max, c.limit)
    63  		if c.wantErr {
    64  			require.Error(t, err, caseString)
    65  		} else {
    66  			require.NoError(t, err, caseString)
    67  			require.Equal(t, 1+max-min, c.resultLength, caseString)
    68  		}
    69  	}
    70  }
    71  
    72  func TestBlockResults(t *testing.T) {
    73  	results := &tmstate.ABCIResponses{
    74  		DeliverTxs: []*abci.ResponseDeliverTx{
    75  			{Code: 0, Data: []byte{0x01}, Log: "ok"},
    76  			{Code: 0, Data: []byte{0x02}, Log: "ok"},
    77  			{Code: 1, Log: "not ok"},
    78  		},
    79  		EndBlock:   &abci.ResponseEndBlock{},
    80  		BeginBlock: &abci.ResponseBeginBlock{},
    81  	}
    82  
    83  	env = &Environment{}
    84  	env.StateStore = sm.NewStore(dbm.NewMemDB(), sm.StoreOptions{
    85  		DiscardABCIResponses: false,
    86  	})
    87  	err := env.StateStore.SaveABCIResponses(100, results)
    88  	require.NoError(t, err)
    89  	env.BlockStore = mockBlockStore{height: 100}
    90  
    91  	testCases := []struct {
    92  		height  int64
    93  		wantErr bool
    94  		wantRes *ctypes.ResultBlockResults
    95  	}{
    96  		{-1, true, nil},
    97  		{0, true, nil},
    98  		{101, true, nil},
    99  		{100, false, &ctypes.ResultBlockResults{
   100  			Height:                100,
   101  			TxsResults:            results.DeliverTxs,
   102  			BeginBlockEvents:      results.BeginBlock.Events,
   103  			EndBlockEvents:        results.EndBlock.Events,
   104  			ValidatorUpdates:      results.EndBlock.ValidatorUpdates,
   105  			ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates,
   106  		}},
   107  	}
   108  
   109  	for _, tc := range testCases {
   110  		res, err := BlockResults(&rpctypes.Context{}, &tc.height)
   111  		if tc.wantErr {
   112  			assert.Error(t, err)
   113  		} else {
   114  			assert.NoError(t, err)
   115  			assert.Equal(t, tc.wantRes, res)
   116  		}
   117  	}
   118  }
   119  
   120  type mockBlockStore struct {
   121  	height int64
   122  }
   123  
   124  func (mockBlockStore) Base() int64                                       { return 1 }
   125  func (store mockBlockStore) Height() int64                               { return store.height }
   126  func (store mockBlockStore) Size() int64                                 { return store.height }
   127  func (mockBlockStore) LoadBaseMeta() *types.BlockMeta                    { return nil }
   128  func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta       { return nil }
   129  func (mockBlockStore) LoadBlock(height int64) *types.Block               { return nil }
   130  func (mockBlockStore) LoadBlockByHash(hash []byte) *types.Block          { return nil }
   131  func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
   132  func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit        { return nil }
   133  func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit         { return nil }
   134  func (mockBlockStore) PruneBlocks(height int64) (uint64, error)          { return 0, nil }
   135  func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
   136  }