github.com/turingchain2020/turingchain@v1.1.21/system/dapp/util_test.go (about)

     1  package dapp
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/turingchain2020/turingchain/types"
     7  	"github.com/turingchain2020/turingchain/util"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestKVCreator(t *testing.T) {
    12  	dir, ldb, kvdb := util.CreateTestDB()
    13  	defer util.CloseTestDB(dir, ldb)
    14  	creator := NewKVCreator(kvdb, []byte("prefix-"), nil)
    15  	creator.AddKVOnly([]byte("a"), []byte("b"))
    16  	_, err := kvdb.Get([]byte("prefix-a"))
    17  	assert.Equal(t, err, types.ErrNotFound)
    18  	creator.Add([]byte("a"), []byte("b"))
    19  	value, err := kvdb.Get([]byte("prefix-a"))
    20  	assert.Equal(t, err, nil)
    21  	assert.Equal(t, value, []byte("b"))
    22  
    23  	creator = NewKVCreator(kvdb, []byte("prefix-"), []byte("rollback"))
    24  	creator.Add([]byte("a"), []byte("b"))
    25  	creator.Add([]byte("a1"), []byte("b1"))
    26  	creator.AddNoPrefix([]byte("np"), []byte("np-value"))
    27  	creator.AddList([]*types.KeyValue{
    28  		{Key: []byte("l1"), Value: []byte("vl1")},
    29  		{Key: []byte("l2"), Value: []byte("vl2")},
    30  	})
    31  	creator.AddListNoPrefix([]*types.KeyValue{
    32  		{Key: []byte("l1"), Value: []byte("vl1")},
    33  		{Key: []byte("l2"), Value: []byte("vl2")},
    34  	})
    35  	creator.Add([]byte("c1"), nil)
    36  	creator.Add([]byte("l2"), nil)
    37  	creator.AddRollbackKV()
    38  	assert.Equal(t, 9, len(creator.KVList()))
    39  	util.SaveKVList(ldb, creator.KVList())
    40  	kvs, err := creator.GetRollbackKVList()
    41  	assert.Nil(t, err)
    42  	assert.Equal(t, 8, len(kvs))
    43  	assert.Equal(t, []byte("b"), kvs[7].Value)
    44  	assert.Equal(t, []byte(nil), kvs[6].Value)
    45  	assert.Equal(t, []byte(nil), kvs[5].Value)
    46  	assert.Equal(t, []byte(nil), kvs[4].Value)
    47  	assert.Equal(t, []byte(nil), kvs[3].Value)
    48  	assert.Equal(t, []byte(nil), kvs[2].Value)
    49  	assert.Equal(t, []byte(nil), kvs[1].Value)
    50  	assert.Equal(t, []byte("vl2"), kvs[0].Value)
    51  	//current: a = b
    52  	//set data:
    53  	//a -> b (a -> b)
    54  	//a1 -> b1 (a1 -> nil)
    55  	//l1 -> vl1 (l1 -> nil)
    56  	//l2 -> vl2 (l2->nil)
    57  	//c1 -> nil (ignore)
    58  	//l2 -> nil (l2 -> vl2)
    59  	//rollback 的过程实际上是 set 的逆过程,就像时间倒流一样
    60  	//save rollback kvs
    61  	_, err = creator.Get([]byte("np"))
    62  	assert.Equal(t, types.ErrNotFound, err)
    63  	v, _ := creator.GetNoPrefix([]byte("np"))
    64  	assert.Equal(t, []byte("np-value"), v)
    65  	util.SaveKVList(ldb, kvs)
    66  	v, _ = kvdb.Get([]byte("prefix-a"))
    67  	assert.Equal(t, []byte("b"), v)
    68  	v, _ = creator.Get([]byte("a"))
    69  	assert.Equal(t, []byte("b"), v)
    70  	_, err = creator.Get([]byte("a1"))
    71  	assert.Equal(t, types.ErrNotFound, err)
    72  	_, err = creator.Get([]byte("l1"))
    73  	assert.Equal(t, types.ErrNotFound, err)
    74  	_, err = creator.Get([]byte("l2"))
    75  	assert.Equal(t, types.ErrNotFound, err)
    76  	_, err = creator.Get([]byte("c1"))
    77  	assert.Equal(t, types.ErrNotFound, err)
    78  
    79  	creator = NewKVCreator(kvdb, []byte("prefix-"), nil)
    80  	creator.AddKVListOnly([]*types.KeyValue{{Key: []byte("k"), Value: []byte("v")}})
    81  	creator.DelRollbackKV()
    82  	creator.AddToLogs(nil)
    83  
    84  	creator = NewKVCreator(kvdb, []byte("prefix-"), []byte("emptyrollback"))
    85  	creator.AddListNoPrefix(nil)
    86  	creator.AddRollbackKV()
    87  	assert.Zero(t, len(creator.kvs))
    88  	kvs, err = creator.GetRollbackKVList()
    89  	assert.Zero(t, len(kvs))
    90  	assert.Nil(t, err)
    91  	creator.DelRollbackKV()
    92  	assert.Zero(t, len(creator.kvs))
    93  }
    94  
    95  func TestHeightIndexStr(t *testing.T) {
    96  	assert.Equal(t, "000000000000100001", HeightIndexStr(1, 1))
    97  }