github.com/turingchain2020/turingchain@v1.1.21/blockchain/rollback_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  	"testing"
     9  	"time"
    10  
    11  	"fmt"
    12  
    13  	"strings"
    14  	"sync"
    15  
    16  	"github.com/turingchain2020/turingchain/types"
    17  	"github.com/turingchain2020/turingchain/util"
    18  	"github.com/turingchain2020/turingchain/util/testnode"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  var once sync.Once
    24  
    25  func TestRollbackblock(t *testing.T) {
    26  	once.Do(func() {
    27  		types.RegFork("store-kvmvccmavl", func(cfg *types.TuringchainConfig) {
    28  			cfg.RegisterDappFork("store-kvmvccmavl", "ForkKvmvccmavl", 20*10000)
    29  		})
    30  	})
    31  	str := types.GetDefaultCfgstring()
    32  	new := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1)
    33  	cfg := types.NewTuringchainConfig(new)
    34  	mfg := cfg.GetModuleConfig()
    35  	mfg.BlockChain.RollbackBlock = 0
    36  	mock33 := testnode.NewWithConfig(cfg, nil)
    37  	chain := mock33.GetBlockChain()
    38  	chain.Rollbackblock()
    39  	db := chain.GetDB()
    40  	kvs := getAllKeys(db)
    41  	assert.Equal(t, len(kvs), kvCount)
    42  	defer mock33.Close()
    43  
    44  	//发送交易
    45  	testMockSendTx(t, mock33)
    46  
    47  	chain.Rollbackblock()
    48  }
    49  
    50  func TestNeedRollback(t *testing.T) {
    51  	once.Do(func() {
    52  		types.RegFork("store-kvmvccmavl", func(cfg *types.TuringchainConfig) {
    53  			cfg.RegisterDappFork("store-kvmvccmavl", "ForkKvmvccmavl", 20*10000)
    54  		})
    55  	})
    56  
    57  	str := types.GetDefaultCfgstring()
    58  	new := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1)
    59  	cfg := types.NewTuringchainConfig(new)
    60  	mock33 := testnode.NewWithConfig(cfg, nil)
    61  	chain := mock33.GetBlockChain()
    62  
    63  	curHeight := int64(5)
    64  	rollHeight := int64(5)
    65  	ok := chain.NeedRollback(curHeight, rollHeight)
    66  	require.Equal(t, false, ok)
    67  
    68  	curHeight = int64(5)
    69  	rollHeight = int64(6)
    70  	ok = chain.NeedRollback(curHeight, rollHeight)
    71  	require.Equal(t, false, ok)
    72  
    73  	curHeight = int64(10 * 10000)
    74  	rollHeight = int64(5 * 10000)
    75  	ok = chain.NeedRollback(curHeight, rollHeight)
    76  	require.Equal(t, true, ok)
    77  
    78  	curHeight = int64(20*10000 + 1)
    79  	rollHeight = int64(20 * 10000)
    80  	ok = chain.NeedRollback(curHeight, rollHeight)
    81  	require.Equal(t, true, ok)
    82  
    83  	curHeight = int64(22 * 10000)
    84  	rollHeight = int64(20 * 10000)
    85  	ok = chain.NeedRollback(curHeight, rollHeight)
    86  	require.Equal(t, false, ok)
    87  
    88  	curHeight = int64(22 * 10000)
    89  	rollHeight = int64(20*10000 + 1)
    90  	ok = chain.NeedRollback(curHeight, rollHeight)
    91  	require.Equal(t, true, ok)
    92  
    93  }
    94  
    95  func TestRollback(t *testing.T) {
    96  	cfg := testnode.GetDefaultConfig()
    97  	mfg := cfg.GetModuleConfig()
    98  	mfg.BlockChain.RollbackBlock = 2
    99  	mock33 := testnode.NewWithConfig(cfg, nil)
   100  	chain := mock33.GetBlockChain()
   101  	db := chain.GetDB()
   102  	kvs := getAllKeys(db)
   103  	assert.Equal(t, len(kvs), kvCount)
   104  	defer mock33.Close()
   105  
   106  	//发送交易
   107  	testMockSendTx(t, mock33)
   108  
   109  	chain.Rollback()
   110  	require.Equal(t, int64(2), chain.GetBlockHeight())
   111  }
   112  
   113  func TestRollbackSave(t *testing.T) {
   114  	cfg := testnode.GetDefaultConfig()
   115  	mfg := cfg.GetModuleConfig()
   116  	mfg.BlockChain.RollbackBlock = 2
   117  	mfg.BlockChain.RollbackSave = true
   118  	mock33 := testnode.NewWithConfig(cfg, nil)
   119  	chain := mock33.GetBlockChain()
   120  	db := chain.GetDB()
   121  	kvs := getAllKeys(db)
   122  	assert.Equal(t, len(kvs), kvCount)
   123  	defer mock33.Close()
   124  
   125  	//发送交易
   126  	testMockSendTx(t, mock33)
   127  
   128  	height := chain.GetBlockHeight()
   129  	chain.Rollback()
   130  
   131  	// check
   132  	require.Equal(t, int64(2), chain.GetBlockHeight())
   133  	for i := height; i > 2; i-- {
   134  		key := []byte(fmt.Sprintf("TB:%012d", i))
   135  		_, err := chain.GetDB().Get(key)
   136  		assert.NoError(t, err)
   137  	}
   138  	value, err := chain.GetDB().Get([]byte("LTB:"))
   139  	assert.NoError(t, err)
   140  	assert.NotNil(t, value)
   141  	h := &types.Int64{}
   142  	err = types.Decode(value, h)
   143  	assert.NoError(t, err)
   144  	assert.Equal(t, height, h.Data)
   145  }
   146  
   147  func TestRollbackPara(t *testing.T) {
   148  	cfg := testnode.GetDefaultConfig()
   149  	mfg := cfg.GetModuleConfig()
   150  	mfg.BlockChain.RollbackBlock = 2
   151  	mfg.BlockChain.IsParaChain = true
   152  	mock33 := testnode.NewWithConfig(cfg, nil)
   153  	chain := mock33.GetBlockChain()
   154  	defer mock33.Close()
   155  
   156  	//发送交易
   157  	testMockSendTx(t, mock33)
   158  
   159  	chain.Rollback()
   160  	require.Equal(t, int64(2), chain.GetBlockHeight())
   161  }
   162  
   163  func testMockSendTx(t *testing.T, mock33 *testnode.TuringchainMock) {
   164  	cfg := mock33.GetClient().GetConfig()
   165  	txs := util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
   166  	for i := 0; i < len(txs); i++ {
   167  		reply, err := mock33.GetAPI().SendTx(txs[i])
   168  		assert.Nil(t, err)
   169  		assert.Equal(t, reply.IsOk, true)
   170  	}
   171  	mock33.WaitHeight(1)
   172  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10)
   173  	for i := 0; i < len(txs); i++ {
   174  		reply, err := mock33.GetAPI().SendTx(txs[i])
   175  		assert.Nil(t, err)
   176  		assert.Equal(t, reply.IsOk, true)
   177  	}
   178  	mock33.WaitHeight(2)
   179  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 1)
   180  	for i := 0; i < len(txs); i++ {
   181  		reply, err := mock33.GetAPI().SendTx(txs[i])
   182  		assert.Nil(t, err)
   183  		assert.Equal(t, reply.IsOk, true)
   184  	}
   185  	mock33.WaitHeight(3)
   186  	txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 2)
   187  	for i := 0; i < len(txs); i++ {
   188  		reply, err := mock33.GetAPI().SendTx(txs[i])
   189  		assert.Nil(t, err)
   190  		assert.Equal(t, reply.IsOk, true)
   191  	}
   192  	mock33.WaitHeight(4)
   193  	time.Sleep(time.Second)
   194  }