7 "github.com/vapor/common"
8 "github.com/vapor/config"
9 "github.com/vapor/consensus"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
13 dbm "github.com/tendermint/tmlibs/db"
16 func TestLoadBlockIndex(t *testing.T) {
17 defer os.RemoveAll("temp")
18 testDB := dbm.NewDB("testdb", "leveldb", "temp")
19 store := NewStore(testDB)
20 config.CommonConfig = config.DefaultConfig()
21 config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
22 config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
23 for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners {
24 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
28 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
30 block := config.GenesisBlock()
31 txStatus := bc.NewTransactionStatus()
33 if err := store.SaveBlock(block, txStatus); err != nil {
37 for block.Height <= 128 {
38 preHash := block.Hash()
39 block.PreviousBlockHash = preHash
41 if err := store.SaveBlock(block, txStatus); err != nil {
45 if block.Height%32 != 0 {
49 for i := uint64(0); i < block.Height/32; i++ {
50 if err := store.SaveBlock(block, txStatus); err != nil {
56 if _, err := store.LoadBlockIndex(128); err != nil {
61 func TestLoadBlockIndexBestHeight(t *testing.T) {
63 blockBestHeight uint64
64 stateBestHeight uint64
80 defer os.RemoveAll("temp")
81 testDB := dbm.NewDB("testdb", "leveldb", "temp")
82 store := NewStore(testDB)
83 var savedBlocks []types.Block
84 config.CommonConfig = config.DefaultConfig()
85 config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
86 config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
87 for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners {
88 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
92 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
95 for _, c := range cases {
96 block := config.GenesisBlock()
97 txStatus := bc.NewTransactionStatus()
99 for i := uint64(0); i < c.blockBestHeight; i++ {
100 if err := store.SaveBlock(block, txStatus); err != nil {
104 savedBlocks = append(savedBlocks, *block)
105 block.PreviousBlockHash = block.Hash()
109 index, err := store.LoadBlockIndex(c.stateBestHeight)
114 for _, block := range savedBlocks {
115 blockHash := block.Hash()
116 if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) {
117 t.Errorf("Error in load block index")