github.com/turingchain2020/turingchain@v1.1.21/executor/db_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package executor
     6  
     7  import (
     8  	"testing"
     9  
    10  	"strings"
    11  
    12  	dbm "github.com/turingchain2020/turingchain/common/db"
    13  	"github.com/turingchain2020/turingchain/queue"
    14  	"github.com/turingchain2020/turingchain/store"
    15  	"github.com/turingchain2020/turingchain/types"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func newStateDbForTest(height int64, cfg *types.TuringchainConfig) dbm.KV {
    20  	q := queue.New("channel")
    21  	q.SetConfig(cfg)
    22  	return NewStateDB(q.Client(), nil, nil, &StateDBOption{Height: height})
    23  }
    24  func TestStateDBGet(t *testing.T) {
    25  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
    26  	db := newStateDbForTest(0, cfg)
    27  	testDBGet(t, db)
    28  }
    29  
    30  func testDBGet(t *testing.T, db dbm.KV) {
    31  	err := db.Set([]byte("k1"), []byte("v1"))
    32  	assert.Nil(t, err)
    33  	v, err := db.Get([]byte("k1"))
    34  	assert.Nil(t, err)
    35  	assert.Equal(t, v, []byte("v1"))
    36  
    37  	err = db.Set([]byte("k1"), []byte("v11"))
    38  	assert.Nil(t, err)
    39  	v, err = db.Get([]byte("k1"))
    40  	assert.Nil(t, err)
    41  	assert.Equal(t, v, []byte("v11"))
    42  
    43  	stateDb := db.(*StateDB)
    44  	vs, err := stateDb.BatchGet([][]byte{[]byte("k1")})
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, [][]byte{[]byte("v11")}, vs)
    47  }
    48  
    49  func TestStateDBTxGetOld(t *testing.T) {
    50  	str := types.GetDefaultCfgstring()
    51  	new := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1)
    52  	cfg := types.NewTuringchainConfig(new)
    53  
    54  	q := queue.New("channel")
    55  	q.SetConfig(cfg)
    56  	// store
    57  	s := store.New(cfg)
    58  	s.SetQueueClient(q.Client())
    59  	// exec
    60  	db := NewStateDB(q.Client(), nil, nil, &StateDBOption{Height: cfg.GetFork("ForkExecRollback") - 1})
    61  	defer func() {
    62  		s.Close()
    63  		q.Close()
    64  	}()
    65  
    66  	db.Begin()
    67  	err := db.Set([]byte("k1"), []byte("v1"))
    68  	assert.Nil(t, err)
    69  	v, err := db.Get([]byte("k1"))
    70  	assert.Nil(t, err)
    71  	assert.Equal(t, v, []byte("v1"))
    72  
    73  	db.Rollback()
    74  	v, err = db.Get([]byte("k1"))
    75  	assert.Equal(t, err, types.ErrNotFound)
    76  	assert.Equal(t, v, []byte(nil))
    77  
    78  	db.Begin()
    79  	err = db.Set([]byte("k1"), []byte("v1"))
    80  	assert.Nil(t, err)
    81  	v, err = db.Get([]byte("k1"))
    82  	assert.Nil(t, err)
    83  	assert.Equal(t, v, []byte("v1"))
    84  
    85  	db.Commit()
    86  	v, err = db.Get([]byte("k1"))
    87  	assert.Nil(t, err)
    88  	assert.Equal(t, v, []byte("v1"))
    89  
    90  	err = db.Set([]byte("k1"), []byte("v11"))
    91  	assert.Nil(t, err)
    92  
    93  	db.Begin()
    94  	v, err = db.Get([]byte("k1"))
    95  	assert.Nil(t, err)
    96  	//fork 之前有bug,这里读到了脏数据
    97  	assert.Equal(t, v, []byte("v1"))
    98  
    99  	db.Begin()
   100  	db.Rollback()
   101  	db.Commit()
   102  }
   103  
   104  func testTxGet(t *testing.T, db dbm.KV) {
   105  	//新版本
   106  	db.Begin()
   107  	err := db.Set([]byte("k1"), []byte("v1"))
   108  	assert.Nil(t, err)
   109  	v, err := db.Get([]byte("k1"))
   110  	assert.Nil(t, err)
   111  	assert.Equal(t, v, []byte("v1"))
   112  
   113  	db.Commit()
   114  	v, err = db.Get([]byte("k1"))
   115  	assert.Nil(t, err)
   116  	assert.Equal(t, v, []byte("v1"))
   117  
   118  	//在非transaction中set,直接set成功,不能rollback
   119  	err = db.Set([]byte("k1"), []byte("v11"))
   120  	assert.Nil(t, err)
   121  
   122  	db.Begin()
   123  	v, err = db.Get([]byte("k1"))
   124  	assert.Nil(t, err)
   125  	assert.Equal(t, v, []byte("v11"))
   126  
   127  	err = db.Set([]byte("k1"), []byte("v12"))
   128  	assert.Nil(t, err)
   129  	v, err = db.Get([]byte("k1"))
   130  	assert.Nil(t, err)
   131  	assert.Equal(t, v, []byte("v12"))
   132  
   133  	db.Rollback()
   134  	v, err = db.Get([]byte("k1"))
   135  	assert.Nil(t, err)
   136  	assert.Equal(t, v, []byte("v11"))
   137  }
   138  
   139  func TestStateDBTxGet(t *testing.T) {
   140  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
   141  	db := newStateDbForTest(cfg.GetFork("ForkExecRollback"), cfg)
   142  	testTxGet(t, db)
   143  }