github.com/amazechain/amc@v0.1.3/modules/rawdb/accessors_chain_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"github.com/amazechain/amc/common/types"
    21  	"github.com/holiman/uint256"
    22  	"github.com/ledgerwatch/erigon-lib/kv/memdb"
    23  	"testing"
    24  )
    25  
    26  // Tests block total difficulty storage and retrieval operations.
    27  func TestTdStorage(t *testing.T) {
    28  	_, tx := memdb.NewTestTx(t)
    29  
    30  	// Create a test TD to move around the database and make sure it's really new
    31  	hash, td := types.Hash{}, uint256.NewInt(314)
    32  	entry, err := ReadTd(tx, hash, 0)
    33  	if err != nil {
    34  		t.Fatalf("ReadTd failed: %v", err)
    35  	}
    36  	if entry != nil {
    37  		t.Fatalf("Non existent TD returned: %v", entry)
    38  	}
    39  	// Write and verify the TD in the database
    40  	err = WriteTd(tx, hash, 0, td)
    41  	if err != nil {
    42  		t.Fatalf("WriteTd failed: %v", err)
    43  	}
    44  	entry, err = ReadTd(tx, hash, 0)
    45  	if err != nil {
    46  		t.Fatalf("ReadTd failed: %v", err)
    47  	}
    48  	if entry == nil {
    49  		t.Fatalf("Stored TD not found")
    50  	} else if entry.Cmp(td) != 0 {
    51  		t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
    52  	}
    53  	// Delete the TD and verify the execution
    54  	err = TruncateTd(tx, 0)
    55  	if err != nil {
    56  		t.Fatalf("DeleteTd failed: %v", err)
    57  	}
    58  	entry, err = ReadTd(tx, hash, 0)
    59  	if err != nil {
    60  		t.Fatalf("ReadTd failed: %v", err)
    61  	}
    62  	if entry != nil {
    63  		t.Fatalf("Deleted TD returned: %v", entry)
    64  	}
    65  
    66  	gHash, gTd := types.Hash{}, uint256.NewInt(0)
    67  	if err := WriteTd(tx, gHash, 100, gTd); nil != err {
    68  		t.Fatalf("WriteTd failed: %v", err)
    69  	}
    70  
    71  	entryE, errE := ReadTd(tx, gHash, 101)
    72  	if nil != errE {
    73  		t.Fatalf("ReadTd failed: %v", err)
    74  	}
    75  
    76  	if entryE != nil {
    77  		t.Fatal("ReadTd returned nil")
    78  	}
    79  
    80  	entryg, errg := ReadTd(tx, gHash, 100)
    81  	if nil != errg {
    82  		t.Fatalf("ReadTd failed: %v", err)
    83  	}
    84  
    85  	if entryg.Cmp(gTd) != 0 {
    86  		t.Fatal("ReadTd returned nil")
    87  	}
    88  }