github.com/turingchain2020/turingchain@v1.1.21/blockchain/restore_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 "github.com/turingchain2020/turingchain/common/address" 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 func TestNeedReExec(t *testing.T) { 21 defer func() { 22 r := recover() 23 if r == "not support upgrade store to greater than 2.0.0" { 24 assert.Equal(t, r, "not support upgrade store to greater than 2.0.0") 25 } else if r == "not support degrade the program" { 26 assert.Equal(t, r, "not support degrade the program") 27 } 28 }() 29 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 30 mock33 := testnode.NewWithConfig(cfg, nil) 31 32 //发送交易 33 chain := mock33.GetBlockChain() 34 // 相当于数据库中的版本 35 testcase := []*types.UpgradeMeta{ 36 {Starting: true}, 37 {Starting: false, Version: "1.0.0"}, 38 {Starting: false, Version: "1.0.0"}, 39 {Starting: false, Version: "2.0.0"}, 40 {Starting: false, Version: "2.0.0"}, 41 } 42 // 程序中的版本 43 vsCase := []string{ 44 "1.0.0", 45 "1.0.0", 46 "2.0.0", 47 "3.0.0", 48 "1.0.0", 49 } 50 result := []bool{ 51 true, 52 false, 53 true, 54 false, 55 false, 56 } 57 for i, c := range testcase { 58 version.SetStoreDBVersion(vsCase[i]) 59 res := chain.NeedReExec(c) 60 assert.Equal(t, res, result[i]) 61 } 62 } 63 64 func GetAddrTxsCount(db dbm.DB, addr string) (int64, error) { 65 count := types.Int64{} 66 TxsCount, err := db.Get(types.CalcAddrTxsCountKey(addr)) 67 if err != nil && err != types.ErrNotFound { 68 return 0, err 69 } 70 if len(TxsCount) == 0 { 71 return 0, nil 72 } 73 err = types.Decode(TxsCount, &count) 74 if err != nil { 75 return 0, err 76 } 77 return count.Data, nil 78 } 79 80 func TestUpgradeStore(t *testing.T) { 81 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 82 cfg.GetModuleConfig().BlockChain.EnableReExecLocal = true 83 mock33 := testnode.NewWithConfig(cfg, nil) 84 //发送交易 85 chain := mock33.GetBlockChain() 86 db := chain.GetDB() 87 kvs := getAllKeys(db) 88 assert.Equal(t, len(kvs), kvCount) 89 defer mock33.Close() 90 txs := util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10) 91 for i := 0; i < len(txs); i++ { 92 reply, err := mock33.GetAPI().SendTx(txs[i]) 93 assert.Nil(t, err) 94 assert.Equal(t, reply.IsOk, true) 95 } 96 mock33.WaitHeight(1) 97 txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 10) 98 for i := 0; i < len(txs); i++ { 99 reply, err := mock33.GetAPI().SendTx(txs[i]) 100 assert.Nil(t, err) 101 assert.Equal(t, reply.IsOk, true) 102 } 103 mock33.WaitHeight(2) 104 txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 1) 105 for i := 0; i < len(txs); i++ { 106 reply, err := mock33.GetAPI().SendTx(txs[i]) 107 assert.Nil(t, err) 108 assert.Equal(t, reply.IsOk, true) 109 } 110 mock33.WaitHeight(3) 111 txs = util.GenNoneTxs(cfg, mock33.GetGenesisKey(), 2) 112 for i := 0; i < len(txs); i++ { 113 reply, err := mock33.GetAPI().SendTx(txs[i]) 114 assert.Nil(t, err) 115 assert.Equal(t, reply.IsOk, true) 116 } 117 mock33.WaitHeight(4) 118 time.Sleep(time.Second) 119 addr := address.PubKeyToAddress(mock33.GetGenesisKey().PubKey().Bytes()).String() 120 count1, err := GetAddrTxsCount(db, addr) 121 assert.NoError(t, err) 122 version.SetStoreDBVersion("2.0.0") 123 chain.UpgradeStore() 124 count2, err := GetAddrTxsCount(db, addr) 125 assert.NoError(t, err) 126 assert.Equal(t, count1, count2) 127 }