github.com/Finschia/finschia-sdk@v0.48.1/baseapp/abci_test.go (about)

     1  package baseapp
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	abci "github.com/tendermint/tendermint/abci/types"
     9  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    10  	dbm "github.com/tendermint/tm-db"
    11  
    12  	ocabci "github.com/Finschia/ostracon/abci/types"
    13  
    14  	sdk "github.com/Finschia/finschia-sdk/types"
    15  )
    16  
    17  func TestGetBlockRentionHeight(t *testing.T) {
    18  	logger := defaultLogger()
    19  	db := dbm.NewMemDB()
    20  	name := t.Name()
    21  
    22  	testCases := map[string]struct {
    23  		bapp         *BaseApp
    24  		maxAgeBlocks int64
    25  		commitHeight int64
    26  		expected     int64
    27  	}{
    28  		"defaults": {
    29  			bapp:         NewBaseApp(name, logger, db, nil),
    30  			maxAgeBlocks: 0,
    31  			commitHeight: 499000,
    32  			expected:     0,
    33  		},
    34  		"pruning unbonding time only": {
    35  			bapp:         NewBaseApp(name, logger, db, nil, SetMinRetainBlocks(1)),
    36  			maxAgeBlocks: 362880,
    37  			commitHeight: 499000,
    38  			expected:     136120,
    39  		},
    40  		"pruning iavl snapshot only": {
    41  			bapp: NewBaseApp(
    42  				name, logger, db, nil,
    43  				SetPruning(sdk.PruningOptions{KeepEvery: 10000}),
    44  				SetMinRetainBlocks(1),
    45  			),
    46  			maxAgeBlocks: 0,
    47  			commitHeight: 499000,
    48  			expected:     490000,
    49  		},
    50  		"pruning state sync snapshot only": {
    51  			bapp: NewBaseApp(
    52  				name, logger, db, nil,
    53  				SetSnapshotInterval(50000),
    54  				SetSnapshotKeepRecent(3),
    55  				SetMinRetainBlocks(1),
    56  			),
    57  			maxAgeBlocks: 0,
    58  			commitHeight: 499000,
    59  			expected:     349000,
    60  		},
    61  		"pruning min retention only": {
    62  			bapp: NewBaseApp(
    63  				name, logger, db, nil,
    64  				SetMinRetainBlocks(400000),
    65  			),
    66  			maxAgeBlocks: 0,
    67  			commitHeight: 499000,
    68  			expected:     99000,
    69  		},
    70  		"pruning all conditions": {
    71  			bapp: NewBaseApp(
    72  				name, logger, db, nil,
    73  				SetPruning(sdk.PruningOptions{KeepEvery: 10000}),
    74  				SetMinRetainBlocks(400000),
    75  				SetSnapshotInterval(50000), SetSnapshotKeepRecent(3),
    76  			),
    77  			maxAgeBlocks: 362880,
    78  			commitHeight: 499000,
    79  			expected:     99000,
    80  		},
    81  		"no pruning due to no persisted state": {
    82  			bapp: NewBaseApp(
    83  				name, logger, db, nil,
    84  				SetPruning(sdk.PruningOptions{KeepEvery: 10000}),
    85  				SetMinRetainBlocks(400000),
    86  				SetSnapshotInterval(50000), SetSnapshotKeepRecent(3),
    87  			),
    88  			maxAgeBlocks: 362880,
    89  			commitHeight: 10000,
    90  			expected:     0,
    91  		},
    92  		"disable pruning": {
    93  			bapp: NewBaseApp(
    94  				name, logger, db, nil,
    95  				SetPruning(sdk.PruningOptions{KeepEvery: 10000}),
    96  				SetMinRetainBlocks(0),
    97  				SetSnapshotInterval(50000), SetSnapshotKeepRecent(3),
    98  			),
    99  			maxAgeBlocks: 362880,
   100  			commitHeight: 499000,
   101  			expected:     0,
   102  		},
   103  		"iavl disable fast node": {
   104  			bapp: NewBaseApp(
   105  				name, logger, db, nil,
   106  				SetIAVLDisableFastNode(true),
   107  			),
   108  			maxAgeBlocks: 0,
   109  			commitHeight: 499000,
   110  			expected:     0,
   111  		},
   112  	}
   113  
   114  	for name, tc := range testCases {
   115  		tc := tc
   116  
   117  		tc.bapp.SetParamStore(&paramStore{db: dbm.NewMemDB()})
   118  		tc.bapp.InitChain(abci.RequestInitChain{
   119  			ConsensusParams: &abci.ConsensusParams{
   120  				Evidence: &tmproto.EvidenceParams{
   121  					MaxAgeNumBlocks: tc.maxAgeBlocks,
   122  				},
   123  			},
   124  		})
   125  
   126  		t.Run(name, func(t *testing.T) {
   127  			require.Equal(t, tc.expected, tc.bapp.GetBlockRetentionHeight(tc.commitHeight))
   128  		})
   129  	}
   130  }
   131  
   132  // Test and ensure that invalid block heights always cause errors.
   133  // See issues:
   134  // - https://github.com/cosmos/cosmos-sdk/issues/11220
   135  // - https://github.com/cosmos/cosmos-sdk/issues/7662
   136  func TestBaseAppCreateQueryContext(t *testing.T) {
   137  	t.Parallel()
   138  
   139  	logger := defaultLogger()
   140  	db := dbm.NewMemDB()
   141  	name := t.Name()
   142  	app := NewBaseApp(name, logger, db, nil)
   143  	app.init()
   144  
   145  	app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: 1}})
   146  	app.Commit()
   147  
   148  	app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: 2}})
   149  	app.Commit()
   150  
   151  	testCases := []struct {
   152  		name   string
   153  		height int64
   154  		prove  bool
   155  		expErr bool
   156  	}{
   157  		{"valid height", 2, true, false},
   158  		{"future height", 10, true, true},
   159  		{"negative height, prove=true", -1, true, true},
   160  		{"negative height, prove=false", -1, false, true},
   161  	}
   162  
   163  	for _, tc := range testCases {
   164  		t.Run(tc.name, func(t *testing.T) {
   165  			_, err := app.createQueryContext(tc.height, tc.prove)
   166  			if tc.expErr {
   167  				require.Error(t, err)
   168  			} else {
   169  				require.NoError(t, err)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  // Test and ensure that consensus params has been updated.
   176  // See:
   177  // - https://github.com/Finschia/finschia-sdk/pull/673
   178  func TestBaseAppBeginBlockConsensusParams(t *testing.T) {
   179  	t.Parallel()
   180  
   181  	logger := defaultLogger()
   182  	db := dbm.NewMemDB()
   183  	name := t.Name()
   184  	app := NewBaseApp(name, logger, db, nil)
   185  	app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
   186  	app.InitChain(abci.RequestInitChain{
   187  		ConsensusParams: &abci.ConsensusParams{
   188  			Block: &abci.BlockParams{
   189  				MaxGas: -1,
   190  			},
   191  		},
   192  	})
   193  	app.init()
   194  
   195  	// set block params
   196  	app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: 1}})
   197  	ctx := app.deliverState.ctx
   198  	maxGas := int64(123456789)
   199  	app.paramStore.Set(ctx, ParamStoreKeyBlockParams,
   200  		&abci.BlockParams{
   201  			MaxGas: maxGas,
   202  		})
   203  	app.Commit()
   204  
   205  	// confirm consensus params updated into the context
   206  	app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: 2}})
   207  	newCtx := app.getContextForTx(app.checkState, []byte{})
   208  	require.Equal(t, maxGas, newCtx.ConsensusParams().Block.MaxGas)
   209  }
   210  
   211  type paramStore struct {
   212  	db *dbm.MemDB
   213  }
   214  
   215  func (ps *paramStore) Set(_ sdk.Context, key []byte, value interface{}) {
   216  	bz, err := json.Marshal(value)
   217  	if err != nil {
   218  		panic(err)
   219  	}
   220  
   221  	ps.db.Set(key, bz)
   222  }
   223  
   224  func (ps *paramStore) Has(_ sdk.Context, key []byte) bool {
   225  	ok, err := ps.db.Has(key)
   226  	if err != nil {
   227  		panic(err)
   228  	}
   229  
   230  	return ok
   231  }
   232  
   233  func (ps *paramStore) Get(_ sdk.Context, key []byte, ptr interface{}) {
   234  	bz, err := ps.db.Get(key)
   235  	if err != nil {
   236  		panic(err)
   237  	}
   238  
   239  	if len(bz) == 0 {
   240  		return
   241  	}
   242  
   243  	if err := json.Unmarshal(bz, ptr); err != nil {
   244  		panic(err)
   245  	}
   246  }