github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/filedao/filedao_legacy_test.go (about)

     1  // Copyright (c) 2020 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package filedao
     7  
     8  import (
     9  	"context"
    10  	"crypto/sha256"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/iotexproject/go-pkgs/hash"
    15  	"github.com/pkg/errors"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/iotexproject/iotex-core/blockchain/block"
    19  	"github.com/iotexproject/iotex-core/blockchain/genesis"
    20  	"github.com/iotexproject/iotex-core/db"
    21  	"github.com/iotexproject/iotex-core/testutil"
    22  )
    23  
    24  func TestFileDAOLegacy_PutBlock(t *testing.T) {
    25  	var (
    26  		normalHeaderSize, compressHeaderSize int
    27  	)
    28  	testFdInterface := func(cfg db.Config, t *testing.T) {
    29  		r := require.New(t)
    30  
    31  		testutil.CleanupPath(cfg.DbPath)
    32  		deser := block.NewDeserializer(_defaultEVMNetworkID)
    33  		fdLegacy, err := newFileDAOLegacy(cfg, deser)
    34  		r.NoError(err)
    35  		fd, ok := fdLegacy.(*fileDAOLegacy)
    36  		r.True(ok)
    37  		ctx := context.Background()
    38  		r.NoError(fd.Start(ctx))
    39  		defer func() {
    40  			r.NoError(fd.Stop(ctx))
    41  		}()
    42  
    43  		blk := createTestingBlock(block.NewTestingBuilder(), 1, hash.ZeroHash256)
    44  		r.NoError(fd.PutBlock(ctx, blk))
    45  		kv, _, err := fd.getTopDB(blk.Height())
    46  		r.NoError(err)
    47  		h := blk.HashBlock()
    48  		header, err := kv.Get(_blockHeaderNS, h[:])
    49  		r.NoError(err)
    50  		if cfg.CompressLegacy {
    51  			compressHeaderSize = len(header)
    52  		} else {
    53  			normalHeaderSize = len(header)
    54  		}
    55  
    56  		// verify API for genesis block
    57  		h, err = fd.GetBlockHash(0)
    58  		r.NoError(err)
    59  		r.Equal(block.GenesisHash(), h)
    60  		height, err := fd.GetBlockHeight(h)
    61  		r.NoError(err)
    62  		r.Zero(height)
    63  		blk, err = fd.GetBlockByHeight(0)
    64  		r.NoError(err)
    65  		r.Equal(block.GenesisBlock(), blk)
    66  	}
    67  
    68  	r := require.New(t)
    69  	testPath, err := testutil.PathOfTempFile("filedao-legacy")
    70  	r.NoError(err)
    71  	defer func() {
    72  		testutil.CleanupPath(testPath)
    73  	}()
    74  
    75  	cfg := db.DefaultConfig
    76  	cfg.DbPath = testPath
    77  	genesis.SetGenesisTimestamp(genesis.Default.Timestamp)
    78  	block.LoadGenesisHash(&genesis.Default)
    79  	for _, compress := range []bool{false, true} {
    80  		cfg.CompressLegacy = compress
    81  		t.Run("test fileDAOLegacy interface", func(t *testing.T) {
    82  			testFdInterface(cfg, t)
    83  		})
    84  	}
    85  
    86  	// compare header length
    87  	r.True(normalHeaderSize > compressHeaderSize)
    88  }
    89  
    90  func TestFileDAOLegacy_DeleteTipBlock(t *testing.T) {
    91  	r := require.New(t)
    92  
    93  	cfg := db.DefaultConfig
    94  	cfg.DbPath = "./filedao_legacy.db"
    95  	cfg.CompressLegacy = true // enable compress
    96  
    97  	deser := block.NewDeserializer(_defaultEVMNetworkID)
    98  	fd, err := newFileDAOLegacy(cfg, deser)
    99  	r.NoError(err)
   100  	legacy := fd.(*fileDAOLegacy)
   101  
   102  	ctx := context.Background()
   103  	r.NoError(fd.Start(ctx))
   104  
   105  	err = legacy.DeleteTipBlock()
   106  	r.Equal("cannot delete genesis block", err.Error())
   107  
   108  	height, err := legacy.Height()
   109  	r.NoError(err)
   110  
   111  	builder := block.NewTestingBuilder()
   112  	h := hash.ZeroHash256
   113  	blk := createTestingBlock(builder, height+1, h)
   114  	r.NoError(fd.PutBlock(ctx, blk))
   115  
   116  	r.NoError(legacy.DeleteTipBlock())
   117  	_, err = fd.GetBlock(h)
   118  	r.Equal(db.ErrNotExist, errors.Cause(err))
   119  	_, err = legacy.GetBlockByHeight(height + 1)
   120  	r.Equal(db.ErrNotExist, errors.Cause(err))
   121  	_, err = fd.GetReceipts(1)
   122  	r.Equal(db.ErrNotExist, errors.Cause(err))
   123  
   124  	r.NoError(fd.Stop(ctx))
   125  	os.RemoveAll(cfg.DbPath)
   126  }
   127  
   128  func TestFileDAOLegacy_getBlockValue(t *testing.T) {
   129  	r := require.New(t)
   130  
   131  	cfg := db.DefaultConfig
   132  	cfg.DbPath = "./filedao_legacy.db"
   133  
   134  	deser := block.NewDeserializer(_defaultEVMNetworkID)
   135  	fd, err := newFileDAOLegacy(cfg, deser)
   136  	r.NoError(err)
   137  	legacy := fd.(*fileDAOLegacy)
   138  
   139  	ctx := context.Background()
   140  	r.NoError(legacy.Start(ctx))
   141  
   142  	// getBlockValue when block is not exist
   143  	_, err = legacy.getBlockValue(_blockHeaderNS, hash.ZeroHash256)
   144  	r.Equal(db.ErrNotExist, errors.Cause(err))
   145  
   146  	// getBlockValue when block is exist
   147  	builder := block.NewTestingBuilder()
   148  	blk := createTestingBlock(builder, 1, sha256.Sum256([]byte("1")))
   149  	r.NoError(legacy.PutBlock(ctx, blk))
   150  
   151  	value, err := legacy.getBlockValue(_blockHeaderNS, blk.HashBlock())
   152  	r.NoError(err)
   153  	header, err := blk.Header.Serialize()
   154  	r.NoError(err)
   155  	r.Equal(value, header)
   156  
   157  	// getBlockValue when NS is not exist
   158  	_, err = legacy.getBlockValue(_blockHeaderNS+"_error_case", blk.HashBlock())
   159  	r.Error(db.ErrNotExist, errors.Cause(err))
   160  
   161  	r.NoError(legacy.Stop(ctx))
   162  	os.RemoveAll(cfg.DbPath)
   163  }