github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/block_store_test.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package storages
    20  
    21  import (
    22  	"crypto/sha256"
    23  	"testing"
    24  
    25  	"fmt"
    26  	"os"
    27  
    28  	"time"
    29  
    30  	"bytes"
    31  
    32  	"github.com/magiconair/properties/assert"
    33  	"github.com/sixexorg/magnetic-ring/common"
    34  	"github.com/sixexorg/magnetic-ring/core/mainchain/types"
    35  	"github.com/sixexorg/magnetic-ring/mock"
    36  )
    37  
    38  var (
    39  	testBlockStore   *BlockStore
    40  	testAccountStore *AccountStore
    41  )
    42  
    43  func TestMain(m *testing.M) {
    44  	var err error
    45  	testBlockDir := "test/block"
    46  	testBlockStore, err = NewBlockStore(testBlockDir, false)
    47  	if err != nil {
    48  		fmt.Fprintf(os.Stderr, "NewBlockStore error %s\n", err)
    49  		return
    50  	}
    51  	testStateDir := "test/account"
    52  	testAccountStore, err = NewAccoutStore(testStateDir, true)
    53  	if err != nil {
    54  		fmt.Fprintf(os.Stderr, "NewAccoutStore error %s\n", err)
    55  		return
    56  	}
    57  	m.Run()
    58  	err = testBlockStore.Close()
    59  	if err != nil {
    60  		fmt.Fprintf(os.Stderr, "testBlockStore.Close error %s\n", err)
    61  		return
    62  	}
    63  	err = os.RemoveAll("./test")
    64  	if err != nil {
    65  		fmt.Fprintf(os.Stderr, "os.RemoveAll error %s\n", err)
    66  		return
    67  	}
    68  }
    69  func TestVersion(t *testing.T) {
    70  	testBlockStore.NewBatch()
    71  	version := byte(1)
    72  	err := testBlockStore.SaveVersion(version)
    73  	if err != nil {
    74  		t.Errorf("SaveVersion error %s", err)
    75  		return
    76  	}
    77  	err = testBlockStore.CommitTo()
    78  	if err != nil {
    79  		t.Errorf("CommitTo error %s", err)
    80  		return
    81  	}
    82  	v, err := testBlockStore.GetVersion()
    83  	if err != nil {
    84  		t.Errorf("GetVersion error %s", err)
    85  		return
    86  	}
    87  	if version != v {
    88  		t.Errorf("TestVersion failed version %d != %d", v, version)
    89  		return
    90  	}
    91  }
    92  
    93  func TestCurrentBlock(t *testing.T) {
    94  	blockHash := common.Hash(sha256.Sum256([]byte("123456789")))
    95  	t.Logf("%x", blockHash)
    96  	blockHeight := uint64(1)
    97  	testBlockStore.NewBatch()
    98  	err := testBlockStore.SaveCurrentBlock(blockHeight, blockHash)
    99  	if err != nil {
   100  		t.Errorf("SaveCurrentBlock error %s", err)
   101  		return
   102  	}
   103  	err = testBlockStore.CommitTo()
   104  	if err != nil {
   105  		t.Errorf("CommitTo error %s", err)
   106  		return
   107  	}
   108  	hash, height, err := testBlockStore.GetCurrentBlock()
   109  	if hash != blockHash {
   110  		t.Errorf("TestCurrentBlock BlockHash %x != %x", hash, blockHash)
   111  		return
   112  	}
   113  	if height != blockHeight {
   114  		t.Errorf("TestCurrentBlock BlockHeight %x != %x", height, blockHeight)
   115  		return
   116  	}
   117  }
   118  
   119  func TestBlockHash(t *testing.T) {
   120  	blockHash := common.Hash(sha256.Sum256([]byte("123456789")))
   121  	blockHeight := uint64(1)
   122  	testBlockStore.NewBatch()
   123  	testBlockStore.SaveBlockHash(blockHeight, blockHash)
   124  	blockHash = common.Hash(sha256.Sum256([]byte("234567890")))
   125  	blockHeight = uint64(2)
   126  	testBlockStore.SaveBlockHash(blockHeight, blockHash)
   127  	err := testBlockStore.CommitTo()
   128  	if err != nil {
   129  		t.Errorf("CommitTo error %s", err)
   130  		return
   131  	}
   132  	hash, err := testBlockStore.GetBlockHash(blockHeight)
   133  	if err != nil {
   134  		t.Errorf("GetBlockHash error %s", err)
   135  		return
   136  	}
   137  	if hash != blockHash {
   138  		t.Errorf("TestBlockHash failed BlockHash %x != %x", hash, blockHash)
   139  		return
   140  	}
   141  }
   142  
   143  func TestSaveTransaction(t *testing.T) {
   144  	buff := bytes.NewBuffer(nil)
   145  	tx := mock.Mock_Tx_CreateLeague
   146  	err := tx.Serialize(buff)
   147  	if err != nil {
   148  		t.Fail()
   149  		t.Errorf("Serialize error %s", err)
   150  		return
   151  	}
   152  
   153  	txHash := mock.Mock_Tx_CreateLeague.Hash()
   154  
   155  	exist, err := testBlockStore.ContainTransaction(txHash)
   156  	if err != nil {
   157  		t.Errorf("ContainTransaction error %s", err)
   158  		return
   159  	}
   160  	if exist {
   161  		t.Errorf("TestSaveTransaction ContainTransaction should be false.")
   162  		return
   163  	}
   164  
   165  	testBlockStore.NewBatch()
   166  	err = testBlockStore.SaveTransaction(tx, 1)
   167  	if err != nil {
   168  		t.Errorf("SaveTransaction error %s", err)
   169  		return
   170  	}
   171  	err = testBlockStore.CommitTo()
   172  	if err != nil {
   173  		t.Errorf("CommitTo error %s", err)
   174  		return
   175  	}
   176  
   177  	tx1, height, err := testBlockStore.GetTransaction(txHash)
   178  	if err != nil {
   179  		t.Errorf("GetTransaction error %s", err)
   180  		return
   181  	}
   182  	if 1 != height {
   183  		t.Errorf("TestSaveTransaction failed BlockHeight %d != %d", height, 1)
   184  		return
   185  	}
   186  	if tx1.TxType != tx.TxType {
   187  		t.Errorf("TestSaveTransaction failed TxType %d != %d", tx1.TxType, tx.TxType)
   188  		return
   189  	}
   190  	tx1Hash := tx1.Hash()
   191  	if txHash != tx1Hash {
   192  		t.Errorf("TestSaveTransaction failed TxHash %x != %x", tx1Hash, txHash)
   193  		return
   194  	}
   195  
   196  	exist, err = testBlockStore.ContainTransaction(txHash)
   197  	if err != nil {
   198  		t.Errorf("ContainTransaction error %s", err)
   199  		return
   200  	}
   201  	if !exist {
   202  		t.Errorf("TestSaveTransaction ContainTransaction should be true.")
   203  		return
   204  	}
   205  }
   206  
   207  func TestHeaderIndexList(t *testing.T) {
   208  	testBlockStore.NewBatch()
   209  	startHeight := uint64(0)
   210  	size := uint64(100)
   211  	indexMap := make(map[uint64]common.Hash, size)
   212  	indexList := make([]common.Hash, 0)
   213  	for i := startHeight; i < size; i++ {
   214  		hash := common.Hash(sha256.Sum256([]byte(fmt.Sprintf("%v", i))))
   215  		indexMap[i] = hash
   216  		indexList = append(indexList, hash)
   217  	}
   218  	err := testBlockStore.SaveHeaderIndexList(startHeight, indexList)
   219  	if err != nil {
   220  		t.Errorf("SaveHeaderIndexList error %s", err)
   221  		return
   222  	}
   223  	startHeight = uint64(100)
   224  	indexMap = make(map[uint64]common.Hash, size)
   225  	for i := startHeight; i < size; i++ {
   226  		hash := common.Hash(sha256.Sum256([]byte(fmt.Sprintf("%v", i))))
   227  		indexMap[i] = hash
   228  		indexList = append(indexList, hash)
   229  	}
   230  	err = testBlockStore.CommitTo()
   231  	if err != nil {
   232  		t.Errorf("CommitTo error %s", err)
   233  		return
   234  	}
   235  
   236  	totalMap, err := testBlockStore.GetHeaderIndexList()
   237  	if err != nil {
   238  		t.Errorf("GetHeaderIndexList error %s", err)
   239  		return
   240  	}
   241  
   242  	for height, hash := range indexList {
   243  		h, ok := totalMap[uint64(height)]
   244  		if !ok {
   245  			t.Errorf("TestHeaderIndexList failed height:%d hash not exist", height)
   246  			return
   247  		}
   248  		if hash != h {
   249  			t.Errorf("TestHeaderIndexList failed height:%d hash %x != %x", height, hash, h)
   250  			return
   251  		}
   252  	}
   253  }
   254  
   255  func TestSaveHeader(t *testing.T) {
   256  	//bookkeeper := mock.Mock_Address_1
   257  	header := &types.Header{
   258  		Version:          123,
   259  		PrevBlockHash:    common.Hash{},
   260  		TxRoot:           common.Hash{},
   261  		Timestamp:        uint64(time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC).Unix()),
   262  		Height:           uint64(1),
   263  		ConsensusData:    123456789,
   264  		ConsensusPayload: []byte("123456789"),
   265  		//NextBookkeeper: bookkeeper,
   266  	}
   267  	block := &types.Block{
   268  		Header:       header,
   269  		Transactions: []*types.Transaction{},
   270  	}
   271  	blockHash := block.Hash()
   272  
   273  	testBlockStore.NewBatch()
   274  	err := testBlockStore.SaveHeader(block)
   275  	if err != nil {
   276  		t.Errorf("SaveHeader error %s", err)
   277  		return
   278  	}
   279  	err = testBlockStore.CommitTo()
   280  	if err != nil {
   281  		t.Errorf("CommitTo error %s", err)
   282  		return
   283  	}
   284  
   285  	h, err := testBlockStore.GetHeader(blockHash)
   286  	if err != nil {
   287  		t.Errorf("GetHeader error %s", err)
   288  		return
   289  	}
   290  
   291  	headerHash := h.Hash()
   292  	if blockHash != headerHash {
   293  		t.Errorf("TestSaveHeader failed HeaderHash \r\n %x \r\n %x", headerHash, blockHash)
   294  		return
   295  	}
   296  
   297  	if header.Height != h.Height {
   298  		t.Errorf("TestSaveHeader failed Height %d \r\n %d", h.Height, header.Height)
   299  		return
   300  	}
   301  	fmt.Println(string(h.ConsensusPayload))
   302  }
   303  
   304  func TestBlock(t *testing.T) {
   305  
   306  	header := &types.Header{
   307  		Version:        123,
   308  		PrevBlockHash:  common.Hash{},
   309  		TxRoot:         common.Hash{},
   310  		Timestamp:      uint64(time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC).Unix()),
   311  		Height:         2,
   312  		ConsensusData:  1234567890,
   313  		NextBookkeeper: mock.Mock_Address_1,
   314  	}
   315  	buff := bytes.NewBuffer(nil)
   316  	tx := mock.Mock_Tx_CreateLeague
   317  	tx2 := mock.Mock_Tx
   318  	err := tx.Serialize(buff)
   319  	block := &types.Block{
   320  		Header:       header,
   321  		Transactions: []*types.Transaction{tx, tx2},
   322  		Sigs: &types.SigData{
   323  			TimeoutSigs: [][]byte{{1, 2, 3}, {4, 5, 6}},
   324  			FailerSigs:  [][]byte{{1, 2, 3}, {4, 5, 6}},
   325  			ProcSigs:    [][]byte{{1, 2, 3}, {4, 5, 6}},
   326  		},
   327  	}
   328  	blockHash := block.Hash()
   329  	tx1Hash := tx.Hash()
   330  
   331  	testBlockStore.NewBatch()
   332  
   333  	err = testBlockStore.SaveBlock(block)
   334  	if err != nil {
   335  		t.Errorf("SaveHeader error %s", err)
   336  		return
   337  	}
   338  	err = testBlockStore.CommitTo()
   339  	if err != nil {
   340  		t.Errorf("CommitTo error %s", err)
   341  		return
   342  	}
   343  
   344  	b, err := testBlockStore.GetBlock(blockHash)
   345  	if err != nil {
   346  		t.Errorf("GetBlock error %s", err)
   347  		return
   348  	}
   349  
   350  	hash := b.Hash()
   351  	if hash != blockHash {
   352  		t.Errorf("TestBlock failed BlockHash %x != %x ", hash, blockHash)
   353  		return
   354  	}
   355  	exist, err := testBlockStore.ContainTransaction(tx1Hash)
   356  	if err != nil {
   357  		t.Errorf("ContainTransaction error %s", err)
   358  		return
   359  	}
   360  	if !exist {
   361  		t.Errorf("TestBlock failed transaction %x should exist", tx1Hash)
   362  		return
   363  	}
   364  
   365  	if len(block.Transactions) != len(b.Transactions) {
   366  		t.Errorf("TestBlock failed Transaction size %d != %d ", len(b.Transactions), len(block.Transactions))
   367  		return
   368  	}
   369  	if b.Transactions[0].Hash() != tx1Hash {
   370  		t.Errorf("TestBlock failed transaction1 hash %x != %x", b.Transactions[0].Hash(), tx1Hash)
   371  		return
   372  	}
   373  	assert.Equal(t, block.Sigs, b.Sigs)
   374  }
   375  
   376  func TestGetTx(t *testing.T) {
   377  	dbdir := "/Users/rennbon/magnetict/node4/magneticchain/block"
   378  	bkstore, err := NewBlockStore(dbdir, false)
   379  	if err != nil {
   380  		t.Error(err)
   381  		t.Fail()
   382  		return
   383  	}
   384  	for i := uint64(1); i < 20; i++ {
   385  		bkHash, err := bkstore.GetBlockHash(i)
   386  		if err != nil {
   387  			t.Error(err)
   388  			t.Fail()
   389  			return
   390  		}
   391  		bk, err := bkstore.GetBlock(bkHash)
   392  		if err != nil {
   393  			t.Error(err)
   394  			t.Fail()
   395  			return
   396  		}
   397  		fmt.Println(bk.Header.Height, bk.Transactions.Len())
   398  
   399  	}
   400  
   401  	/*	txHash, _ := common.StringToHash("yvt4vx3ge2sf1azggbf3vj9vaheebfrc2wf8ej33agznw4znxvyh====")
   402  
   403  		tx, height, err := bkstore.GetTransaction(txHash)
   404  		if err != nil {
   405  			t.Error(err)
   406  			t.Fail()
   407  			return
   408  		}
   409  		t.Log(tx, height)*/
   410  }