github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/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/v2" 8 "github.com/stretchr/testify/require" 9 10 recovery "github.com/onflow/flow-go/consensus/recovery/protocol" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/module/metrics" 13 protocol "github.com/onflow/flow-go/state/protocol/badger" 14 "github.com/onflow/flow-go/state/protocol/util" 15 bstorage "github.com/onflow/flow-go/storage/badger" 16 "github.com/onflow/flow-go/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 protocolState, err := rootSnapshot.ProtocolState() 25 require.NoError(t, err) 26 rootProtocolStateID := protocolState.ID() 27 b0, err := rootSnapshot.Head() 28 require.NoError(t, err) 29 util.RunWithFullProtocolState(t, rootSnapshot, func(db *badger.DB, state *protocol.ParticipantState) { 30 b1 := unittest.BlockWithParentFixture(b0) 31 b1.SetPayload(unittest.PayloadFixture(unittest.WithProtocolStateID(rootProtocolStateID))) 32 33 err = state.Extend(context.Background(), b1) 34 require.NoError(t, err) 35 36 b2 := unittest.BlockWithParentProtocolState(b1) 37 38 err = state.Extend(context.Background(), b2) 39 require.NoError(t, err) 40 41 b3 := unittest.BlockWithParentProtocolState(b2) 42 43 err = state.Extend(context.Background(), b3) 44 require.NoError(t, err) 45 46 metrics := metrics.NewNoopCollector() 47 headers := bstorage.NewHeaders(metrics, db) 48 finalized, pending, err := recovery.FindLatest(state, headers) 49 require.NoError(t, err) 50 require.Equal(t, b0.ID(), finalized.ID(), "recover find latest returns inconsistent finalized block") 51 52 // b1,b2,b3 are unfinalized (pending) blocks 53 require.Equal(t, []*flow.Header{b1.Header, b2.Header, b3.Header}, pending) 54 }) 55 }