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