github.com/turingchain2020/turingchain@v1.1.21/blockchain/reindex_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 blockchain_test
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  	"time"
    11  
    12  	dbm "github.com/turingchain2020/turingchain/common/db"
    13  	"github.com/turingchain2020/turingchain/common/version"
    14  	"github.com/turingchain2020/turingchain/types"
    15  	"github.com/turingchain2020/turingchain/util"
    16  	"github.com/turingchain2020/turingchain/util/testnode"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  var (
    21  	//区块0产生的kv对数量
    22  	kvCount = 25
    23  )
    24  
    25  func TestReindex(t *testing.T) {
    26  	cfg := testnode.GetDefaultConfig()
    27  	mock33 := testnode.NewWithConfig(cfg, nil)
    28  	//发送交易
    29  	chain := mock33.GetBlockChain()
    30  	db := chain.GetDB()
    31  	kvs := getAllKeys(db)
    32  	assert.Equal(t, len(kvs), kvCount)
    33  	defer mock33.Close()
    34  	txs := util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
    35  	for i := 0; i < len(txs); i++ {
    36  		reply, err := mock33.GetAPI().SendTx(txs[i])
    37  		assert.Nil(t, err)
    38  		assert.Equal(t, reply.IsOk, true)
    39  	}
    40  	mock33.WaitHeight(1)
    41  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
    42  	for i := 0; i < len(txs); i++ {
    43  		reply, err := mock33.GetAPI().SendTx(txs[i])
    44  		assert.Nil(t, err)
    45  		assert.Equal(t, reply.IsOk, true)
    46  	}
    47  	mock33.WaitHeight(2)
    48  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 1)
    49  	for i := 0; i < len(txs); i++ {
    50  		reply, err := mock33.GetAPI().SendTx(txs[i])
    51  		assert.Nil(t, err)
    52  		assert.Equal(t, reply.IsOk, true)
    53  	}
    54  	mock33.WaitHeight(3)
    55  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 2)
    56  	for i := 0; i < len(txs); i++ {
    57  		reply, err := mock33.GetAPI().SendTx(txs[i])
    58  		assert.Nil(t, err)
    59  		assert.Equal(t, reply.IsOk, true)
    60  	}
    61  	mock33.WaitHeight(4)
    62  	time.Sleep(time.Second)
    63  	kvs1 := getAllKeys(db)
    64  	version.SetLocalDBVersion("10000.0.0")
    65  	chain.UpgradeChain()
    66  	kvs2 := getAllKeys(db)
    67  	assert.Equal(t, kvs1, kvs2)
    68  }
    69  
    70  func getAllKeys(db dbm.IteratorDB) (kvs []*types.KeyValue) {
    71  	it := db.Iterator(nil, types.EmptyValue, false)
    72  	defer it.Close()
    73  	for it.Rewind(); it.Valid(); it.Next() {
    74  		key := copyBytes(it.Key())
    75  		val := it.ValueCopy()
    76  		//meta 信息是唯一不同的地方
    77  		if string(key) == "LocalDBMeta" {
    78  			continue
    79  		}
    80  		if bytes.HasPrefix(key, []byte("TotalFee")) {
    81  			//println("--", string(key)[0:4], common.ToHex(key))
    82  			totalFee := &types.TotalFee{}
    83  			types.Decode(val, totalFee)
    84  			//println("val", totalFee.String())
    85  		}
    86  		kvs = append(kvs, &types.KeyValue{Key: key, Value: val})
    87  	}
    88  	return kvs
    89  }
    90  
    91  func copyBytes(keys []byte) []byte {
    92  	data := make([]byte, len(keys))
    93  	copy(data, keys)
    94  	return data
    95  }
    96  
    97  func TestUpgradeChain(t *testing.T) {
    98  	cfg := testnode.GetDefaultConfig()
    99  	cfg.GetModuleConfig().Store.LocalDBVersion = "0.0.0"
   100  	mock33 := testnode.NewWithConfig(cfg, nil)
   101  	//发送交易
   102  	chain := mock33.GetBlockChain()
   103  	db := chain.GetDB()
   104  	kvs := getAllKeys(db)
   105  	assert.Equal(t, len(kvs), kvCount)
   106  	defer mock33.Close()
   107  	txs := util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
   108  	for i := 0; i < len(txs); i++ {
   109  		reply, err := mock33.GetAPI().SendTx(txs[i])
   110  		assert.Nil(t, err)
   111  		assert.Equal(t, reply.IsOk, true)
   112  	}
   113  	mock33.WaitHeight(1)
   114  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
   115  	for i := 0; i < len(txs); i++ {
   116  		reply, err := mock33.GetAPI().SendTx(txs[i])
   117  		assert.Nil(t, err)
   118  		assert.Equal(t, reply.IsOk, true)
   119  	}
   120  	mock33.WaitHeight(2)
   121  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 1)
   122  	for i := 0; i < len(txs); i++ {
   123  		reply, err := mock33.GetAPI().SendTx(txs[i])
   124  		assert.Nil(t, err)
   125  		assert.Equal(t, reply.IsOk, true)
   126  	}
   127  	mock33.WaitHeight(3)
   128  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 2)
   129  	for i := 0; i < len(txs); i++ {
   130  		reply, err := mock33.GetAPI().SendTx(txs[i])
   131  		assert.Nil(t, err)
   132  		assert.Equal(t, reply.IsOk, true)
   133  	}
   134  	mock33.WaitHeight(4)
   135  	time.Sleep(time.Second)
   136  
   137  	version.SetLocalDBVersion("1.0.0")
   138  	chain.UpgradeChain()
   139  	ver, err := getUpgradeMeta(db)
   140  	assert.Nil(t, err)
   141  	assert.Equal(t, ver.GetVersion(), "1.0.0")
   142  
   143  	version.SetLocalDBVersion("2.0.0")
   144  	chain.UpgradeChain()
   145  
   146  	ver, err = getUpgradeMeta(db)
   147  	assert.Nil(t, err)
   148  	assert.Equal(t, ver.GetVersion(), "2.0.0")
   149  }
   150  
   151  //GetUpgradeMeta 获取blockchain的数据库版本号
   152  func getUpgradeMeta(db dbm.DB) (*types.UpgradeMeta, error) {
   153  	ver := types.UpgradeMeta{}
   154  	version, err := db.Get(version.LocalDBMeta)
   155  	if err != nil && err != dbm.ErrNotFoundInDb {
   156  		return nil, err
   157  	}
   158  	if len(version) == 0 {
   159  		return &types.UpgradeMeta{Version: "0.0.0"}, nil
   160  	}
   161  	err = types.Decode(version, &ver)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  	return &ver, nil
   166  }