github.com/soomindae/tendermint@v0.0.5-0.20210528140126-84a0c70c8162/light/store/db/db_test.go (about)

     1  package db
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	dbm "github.com/soomindae/tm-db"
    12  
    13  	"github.com/soomindae/tendermint/crypto"
    14  	"github.com/soomindae/tendermint/crypto/tmhash"
    15  	tmrand "github.com/soomindae/tendermint/libs/rand"
    16  	tmversion "github.com/soomindae/tendermint/proto/tendermint/version"
    17  	"github.com/soomindae/tendermint/types"
    18  	"github.com/soomindae/tendermint/version"
    19  )
    20  
    21  func TestLast_FirstLightBlockHeight(t *testing.T) {
    22  	dbStore := New(dbm.NewMemDB(), "TestLast_FirstLightBlockHeight")
    23  
    24  	// Empty store
    25  	height, err := dbStore.LastLightBlockHeight()
    26  	require.NoError(t, err)
    27  	assert.EqualValues(t, -1, height)
    28  
    29  	height, err = dbStore.FirstLightBlockHeight()
    30  	require.NoError(t, err)
    31  	assert.EqualValues(t, -1, height)
    32  
    33  	// 1 key
    34  	err = dbStore.SaveLightBlock(randLightBlock(int64(1)))
    35  	require.NoError(t, err)
    36  
    37  	height, err = dbStore.LastLightBlockHeight()
    38  	require.NoError(t, err)
    39  	assert.EqualValues(t, 1, height)
    40  
    41  	height, err = dbStore.FirstLightBlockHeight()
    42  	require.NoError(t, err)
    43  	assert.EqualValues(t, 1, height)
    44  }
    45  
    46  func Test_SaveLightBlock(t *testing.T) {
    47  	dbStore := New(dbm.NewMemDB(), "Test_SaveLightBlockAndValidatorSet")
    48  
    49  	// Empty store
    50  	h, err := dbStore.LightBlock(1)
    51  	require.Error(t, err)
    52  	assert.Nil(t, h)
    53  
    54  	// 1 key
    55  	err = dbStore.SaveLightBlock(randLightBlock(1))
    56  	require.NoError(t, err)
    57  
    58  	size := dbStore.Size()
    59  	assert.Equal(t, uint16(1), size)
    60  	t.Log(size)
    61  
    62  	h, err = dbStore.LightBlock(1)
    63  	require.NoError(t, err)
    64  	assert.NotNil(t, h)
    65  
    66  	// Empty store
    67  	err = dbStore.DeleteLightBlock(1)
    68  	require.NoError(t, err)
    69  
    70  	h, err = dbStore.LightBlock(1)
    71  	require.Error(t, err)
    72  	assert.Nil(t, h)
    73  
    74  }
    75  
    76  func Test_LightBlockBefore(t *testing.T) {
    77  	dbStore := New(dbm.NewMemDB(), "Test_LightBlockBefore")
    78  
    79  	assert.Panics(t, func() {
    80  		_, _ = dbStore.LightBlockBefore(0)
    81  		_, _ = dbStore.LightBlockBefore(100)
    82  	})
    83  
    84  	err := dbStore.SaveLightBlock(randLightBlock(int64(2)))
    85  	require.NoError(t, err)
    86  
    87  	h, err := dbStore.LightBlockBefore(3)
    88  	require.NoError(t, err)
    89  	if assert.NotNil(t, h) {
    90  		assert.EqualValues(t, 2, h.Height)
    91  	}
    92  }
    93  
    94  func Test_Prune(t *testing.T) {
    95  	dbStore := New(dbm.NewMemDB(), "Test_Prune")
    96  
    97  	// Empty store
    98  	assert.EqualValues(t, 0, dbStore.Size())
    99  	err := dbStore.Prune(0)
   100  	require.NoError(t, err)
   101  
   102  	// One header
   103  	err = dbStore.SaveLightBlock(randLightBlock(2))
   104  	require.NoError(t, err)
   105  
   106  	assert.EqualValues(t, 1, dbStore.Size())
   107  
   108  	err = dbStore.Prune(1)
   109  	require.NoError(t, err)
   110  	assert.EqualValues(t, 1, dbStore.Size())
   111  
   112  	err = dbStore.Prune(0)
   113  	require.NoError(t, err)
   114  	assert.EqualValues(t, 0, dbStore.Size())
   115  
   116  	// Multiple headers
   117  	for i := 1; i <= 10; i++ {
   118  		err = dbStore.SaveLightBlock(randLightBlock(int64(i)))
   119  		require.NoError(t, err)
   120  	}
   121  
   122  	err = dbStore.Prune(11)
   123  	require.NoError(t, err)
   124  	assert.EqualValues(t, 10, dbStore.Size())
   125  
   126  	err = dbStore.Prune(7)
   127  	require.NoError(t, err)
   128  	assert.EqualValues(t, 7, dbStore.Size())
   129  }
   130  
   131  func Test_Concurrency(t *testing.T) {
   132  	dbStore := New(dbm.NewMemDB(), "Test_Prune")
   133  
   134  	var wg sync.WaitGroup
   135  	for i := 1; i <= 100; i++ {
   136  		wg.Add(1)
   137  		go func(i int64) {
   138  			defer wg.Done()
   139  
   140  			err := dbStore.SaveLightBlock(randLightBlock(i))
   141  			require.NoError(t, err)
   142  
   143  			_, err = dbStore.LightBlock(i)
   144  			if err != nil {
   145  				t.Log(err)
   146  			}
   147  
   148  			_, err = dbStore.LastLightBlockHeight()
   149  			if err != nil {
   150  				t.Log(err)
   151  			}
   152  			_, err = dbStore.FirstLightBlockHeight()
   153  			if err != nil {
   154  				t.Log(err)
   155  			}
   156  
   157  			err = dbStore.Prune(2)
   158  			if err != nil {
   159  				t.Log(err)
   160  			}
   161  			_ = dbStore.Size()
   162  
   163  			err = dbStore.DeleteLightBlock(1)
   164  			if err != nil {
   165  				t.Log(err)
   166  			}
   167  		}(int64(i))
   168  	}
   169  
   170  	wg.Wait()
   171  }
   172  
   173  func randLightBlock(height int64) *types.LightBlock {
   174  	vals, _ := types.RandValidatorSet(2, 1)
   175  	return &types.LightBlock{
   176  		SignedHeader: &types.SignedHeader{
   177  			Header: &types.Header{
   178  				Version:            tmversion.Consensus{Block: version.BlockProtocol, App: 0},
   179  				ChainID:            tmrand.Str(12),
   180  				Height:             height,
   181  				Time:               time.Now(),
   182  				LastBlockID:        types.BlockID{},
   183  				LastCommitHash:     crypto.CRandBytes(tmhash.Size),
   184  				DataHash:           crypto.CRandBytes(tmhash.Size),
   185  				ValidatorsHash:     crypto.CRandBytes(tmhash.Size),
   186  				NextValidatorsHash: crypto.CRandBytes(tmhash.Size),
   187  				ConsensusHash:      crypto.CRandBytes(tmhash.Size),
   188  				AppHash:            crypto.CRandBytes(tmhash.Size),
   189  				LastResultsHash:    crypto.CRandBytes(tmhash.Size),
   190  				EvidenceHash:       crypto.CRandBytes(tmhash.Size),
   191  				ProposerAddress:    crypto.CRandBytes(crypto.AddressSize),
   192  			},
   193  			Commit: &types.Commit{},
   194  		},
   195  		ValidatorSet: vals,
   196  	}
   197  }