github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/chainmgr/storage_test.go (about)

     1  package chainmgr
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/davecgh/go-spew/spew"
     9  
    10  	dbm "github.com/bytom/bytom/database/leveldb"
    11  	"github.com/bytom/bytom/protocol/bc/types"
    12  )
    13  
    14  func TestReadWriteBlocks(t *testing.T) {
    15  	tmp, err := ioutil.TempDir(".", "")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer os.RemoveAll(tmp)
    20  
    21  	testDB := dbm.NewDB("testdb", "leveldb", tmp)
    22  	defer testDB.Close()
    23  
    24  	s := newStorage(testDB)
    25  
    26  	cases := []struct {
    27  		storageRAMLimit int
    28  		blocks          []*types.Block
    29  		peerID          string
    30  		isRAM           bool
    31  	}{
    32  		{
    33  			storageRAMLimit: 800 * 1024 * 1024,
    34  			blocks:          mockBlocks(nil, 500),
    35  			peerID:          "testPeer",
    36  			isRAM:           true,
    37  		},
    38  		{
    39  			storageRAMLimit: 1,
    40  			blocks:          mockBlocks(nil, 500),
    41  			peerID:          "testPeer",
    42  			isRAM:           false,
    43  		},
    44  	}
    45  
    46  	for index, c := range cases {
    47  		maxByteOfStorageRAM = c.storageRAMLimit
    48  		s.writeBlocks(c.peerID, c.blocks)
    49  
    50  		for i := 0; i < len(c.blocks); i++ {
    51  			blockStorage, err := s.readBlock(uint64(i))
    52  			if err != nil {
    53  				t.Fatal(err)
    54  			}
    55  
    56  			if blockStorage.isRAM != c.isRAM {
    57  				t.Fatalf("case %d: TestReadWriteBlocks block %d isRAM: got %t want %t", index, i, blockStorage.isRAM, c.isRAM)
    58  			}
    59  
    60  			if blockStorage.block.Hash() != c.blocks[i].Hash() {
    61  				t.Fatalf("case %d: TestReadWriteBlocks block %d: got %s want %s", index, i, spew.Sdump(blockStorage.block), spew.Sdump(c.blocks[i]))
    62  			}
    63  		}
    64  	}
    65  }
    66  
    67  func TestDeleteBlock(t *testing.T) {
    68  	tmp, err := ioutil.TempDir(".", "")
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	defer os.RemoveAll(tmp)
    73  
    74  	testDB := dbm.NewDB("testdb", "leveldb", tmp)
    75  	defer testDB.Close()
    76  
    77  	maxByteOfStorageRAM = 1024
    78  	blocks := mockBlocks(nil, 500)
    79  	s := newStorage(testDB)
    80  	for i, block := range blocks {
    81  		if err := s.writeBlocks("testPeer", []*types.Block{block}); err != nil {
    82  			t.Fatal(err)
    83  		}
    84  
    85  		blockStorage, err := s.readBlock(block.Height)
    86  		if err != nil {
    87  			t.Fatal(err)
    88  		}
    89  
    90  		if !blockStorage.isRAM {
    91  			t.Fatalf("TestReadWriteBlocks block %d isRAM: got %t want %t", i, blockStorage.isRAM, true)
    92  		}
    93  
    94  		s.deleteBlock(block.Height)
    95  	}
    96  
    97  }
    98  
    99  func TestLevelDBStorageReadWrite(t *testing.T) {
   100  	tmp, err := ioutil.TempDir(".", "")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	defer os.RemoveAll(tmp)
   105  
   106  	testDB := dbm.NewDB("testdb", "leveldb", tmp)
   107  	defer testDB.Close()
   108  
   109  	blocks := mockBlocks(nil, 16)
   110  	s := newDBStore(testDB)
   111  
   112  	for i, block := range blocks {
   113  		err := s.writeBlock(block)
   114  		if err != nil {
   115  			t.Fatal(err)
   116  		}
   117  
   118  		gotBlock, err := s.readBlock(block.Height)
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  
   123  		if gotBlock.Hash() != block.Hash() {
   124  			t.Fatalf("TestLevelDBStorageReadWrite block %d: got %s want %s", i, spew.Sdump(gotBlock), spew.Sdump(block))
   125  		}
   126  
   127  		s.clearData()
   128  		_, err = s.readBlock(block.Height)
   129  		if err == nil {
   130  			t.Fatalf("TestLevelDBStorageReadWrite clear data err block %d", i)
   131  		}
   132  	}
   133  }