github.com/MetalBlockchain/metalgo@v1.11.9/vms/proposervm/state/chain_state_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package state
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/database"
    12  	"github.com/MetalBlockchain/metalgo/database/memdb"
    13  	"github.com/MetalBlockchain/metalgo/ids"
    14  )
    15  
    16  func testChainState(a *require.Assertions, cs ChainState) {
    17  	lastAccepted := ids.GenerateTestID()
    18  
    19  	_, err := cs.GetLastAccepted()
    20  	a.Equal(database.ErrNotFound, err)
    21  
    22  	err = cs.SetLastAccepted(lastAccepted)
    23  	a.NoError(err)
    24  
    25  	err = cs.SetLastAccepted(lastAccepted)
    26  	a.NoError(err)
    27  
    28  	fetchedLastAccepted, err := cs.GetLastAccepted()
    29  	a.NoError(err)
    30  	a.Equal(lastAccepted, fetchedLastAccepted)
    31  
    32  	fetchedLastAccepted, err = cs.GetLastAccepted()
    33  	a.NoError(err)
    34  	a.Equal(lastAccepted, fetchedLastAccepted)
    35  
    36  	err = cs.DeleteLastAccepted()
    37  	a.NoError(err)
    38  
    39  	_, err = cs.GetLastAccepted()
    40  	a.Equal(database.ErrNotFound, err)
    41  }
    42  
    43  func TestChainState(t *testing.T) {
    44  	a := require.New(t)
    45  
    46  	db := memdb.New()
    47  	cs := NewChainState(db)
    48  
    49  	testChainState(a, cs)
    50  }