github.com/koko1123/flow-go-1@v0.29.6/consensus/recovery/protocol/state_test.go (about)

     1  package protocol_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v3"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	recovery "github.com/koko1123/flow-go-1/consensus/recovery/protocol"
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  	"github.com/koko1123/flow-go-1/module/metrics"
    13  	protocol "github.com/koko1123/flow-go-1/state/protocol/badger"
    14  	"github.com/koko1123/flow-go-1/state/protocol/util"
    15  	bstorage "github.com/koko1123/flow-go-1/storage/badger"
    16  	"github.com/koko1123/flow-go-1/utils/unittest"
    17  )
    18  
    19  // as a consensus follower, when a block is received and saved,
    20  // if it's not finalized yet, this block should be returned by latest
    21  func TestSaveBlockAsReplica(t *testing.T) {
    22  	participants := unittest.IdentityListFixture(5, unittest.WithAllRoles())
    23  	rootSnapshot := unittest.RootSnapshotFixture(participants)
    24  	b0, err := rootSnapshot.Head()
    25  	require.NoError(t, err)
    26  	util.RunWithFullProtocolState(t, rootSnapshot, func(db *badger.DB, state *protocol.MutableState) {
    27  		b1 := unittest.BlockWithParentFixture(b0)
    28  		b1.SetPayload(flow.Payload{})
    29  
    30  		err = state.Extend(context.Background(), b1)
    31  		require.NoError(t, err)
    32  
    33  		b2 := unittest.BlockWithParentFixture(b1.Header)
    34  		b2.SetPayload(flow.Payload{})
    35  
    36  		err = state.Extend(context.Background(), b2)
    37  		require.NoError(t, err)
    38  
    39  		b3 := unittest.BlockWithParentFixture(b2.Header)
    40  		b3.SetPayload(flow.Payload{})
    41  
    42  		err = state.Extend(context.Background(), b3)
    43  		require.NoError(t, err)
    44  
    45  		metrics := metrics.NewNoopCollector()
    46  		headers := bstorage.NewHeaders(metrics, db)
    47  		finalized, pending, err := recovery.FindLatest(state, headers)
    48  		require.NoError(t, err)
    49  		require.Equal(t, b0.ID(), finalized.ID(), "recover find latest returns inconsistent finalized block")
    50  
    51  		// b1,b2,b3 are unfinalized (pending) blocks
    52  		require.Equal(t, []*flow.Header{b1.Header, b2.Header, b3.Header}, pending)
    53  	})
    54  }