github.com/aergoio/aergo@v1.3.1/state/storage_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/aergoio/aergo/types"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  // func TestCacheBasic(t *testing.T) {
    11  // 	t.Log("StorageCache")
    12  // }
    13  
    14  func TestStorageBasic(t *testing.T) {
    15  	storage := newBufferedStorage(nil, nil)
    16  	v1 := types.GetHashID([]byte("v1"))
    17  	v2 := types.GetHashID([]byte("v2"))
    18  
    19  	storage.checkpoint(0)
    20  	storage.put(newValueEntry(v1, []byte{1})) // rev 1
    21  
    22  	storage.checkpoint(1)
    23  	storage.put(newValueEntry(v2, []byte{2})) // rev 3
    24  
    25  	storage.checkpoint(2)
    26  	storage.put(newValueEntry(v1, []byte{3})) // rev 5
    27  	storage.put(newValueEntry(v2, []byte{4})) // rev 6
    28  	storage.put(newValueEntry(v2, []byte{5})) // rev 7
    29  
    30  	storage.checkpoint(3)
    31  	storage.put(newValueEntry(v1, []byte{6})) // rev 9
    32  	storage.put(newValueEntry(v2, []byte{7})) // rev 10
    33  
    34  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2).Value())
    35  	assert.Equal(t, []byte{6}, storage.get(v1).Value())
    36  	assert.Equal(t, []byte{7}, storage.get(v2).Value())
    37  
    38  	storage.rollback(3)
    39  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2).Value())
    40  	assert.Equal(t, []byte{3}, storage.get(v1).Value())
    41  	assert.Equal(t, []byte{5}, storage.get(v2).Value())
    42  
    43  	storage.rollback(1)
    44  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2))
    45  	assert.Equal(t, []byte{1}, storage.get(v1).Value())
    46  	assert.Nil(t, storage.get(v2))
    47  
    48  	storage.rollback(0)
    49  	t.Log("v1", storage.get(v1), "v2", storage.get(v2))
    50  	assert.Nil(t, storage.get(v1))
    51  	assert.Nil(t, storage.get(v2))
    52  }
    53  
    54  func TestStorageDelete(t *testing.T) {
    55  	storage := newBufferedStorage(nil, nil)
    56  	v1 := types.GetHashID([]byte("v1"))
    57  	v2 := types.GetHashID([]byte("v2"))
    58  
    59  	storage.put(newValueEntry(v1, []byte{1}))
    60  	storage.put(newValueEntry(v2, []byte{2}))
    61  
    62  	storage.checkpoint(0)
    63  	storage.put(newValueEntry(v1, []byte{3}))
    64  	storage.put(newValueEntry(v2, []byte{4}))
    65  	storage.put(newValueEntry(v2, []byte{5}))
    66  
    67  	storage.checkpoint(1)
    68  	storage.put(newValueEntry(v1, []byte{6}))
    69  	storage.put(newValueEntry(v2, []byte{7}))
    70  	storage.put(newValueEntryDelete(v2))
    71  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2).Value())
    72  	assert.Equal(t, []byte{6}, storage.get(v1).Value())
    73  	assert.Equal(t, []byte{0}, storage.get(v2).Hash())
    74  	assert.Nil(t, storage.get(v2).Value())
    75  
    76  	storage.rollback(1)
    77  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2).Value())
    78  	assert.Equal(t, []byte{3}, storage.get(v1).Value())
    79  	assert.Equal(t, []byte{5}, storage.get(v2).Value())
    80  
    81  	storage.put(newValueEntryDelete(v2))
    82  	t.Log("v1", storage.get(v1).Value(), "v2", storage.get(v2).Value())
    83  	assert.Equal(t, []byte{3}, storage.get(v1).Value())
    84  	assert.Equal(t, []byte{0}, storage.get(v2).Hash())
    85  	assert.Nil(t, storage.get(v2).Value())
    86  }
    87  
    88  func TestStorageHasKey(t *testing.T) {
    89  	storage := newBufferedStorage(nil, nil)
    90  	v1 := types.GetHashID([]byte("v1"))
    91  
    92  	assert.False(t, storage.has(v1, false)) // check buffer only
    93  	assert.False(t, storage.has(v1, true))  // check buffer and trie
    94  
    95  	// put entry
    96  	storage.put(newValueEntry(v1, []byte{1}))
    97  	assert.True(t, storage.has(v1, false)) // buffer has key
    98  	assert.True(t, storage.has(v1, true))  // buffer has key
    99  
   100  	// update storage and reset buffer
   101  	err := storage.update()
   102  	assert.NoError(t, err, "failed to update storage")
   103  	err = storage.buffer.reset()
   104  	assert.NoError(t, err, "failed to reset buffer")
   105  	// after update and reset
   106  	assert.False(t, storage.has(v1, false)) // buffer doesn't have key
   107  	assert.True(t, storage.has(v1, true))   // buffer doesn't have, but trie has key
   108  
   109  	// delete entry
   110  	storage.put(newValueEntryDelete(v1))
   111  	assert.True(t, storage.has(v1, false)) // buffer has key
   112  	assert.True(t, storage.has(v1, true))  // buffer has key
   113  
   114  	// update storage and reset buffer
   115  	err = storage.update()
   116  	assert.NoError(t, err, "failed to update storage")
   117  	err = storage.buffer.reset()
   118  	assert.NoError(t, err, "failed to reset buffer")
   119  	// after update and reset
   120  	assert.False(t, storage.has(v1, false)) // buffer doesn't have key
   121  	assert.False(t, storage.has(v1, true))  // buffer and trie don't have key
   122  }