github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/collection/pusher/engine_test.go (about)

     1  package pusher_test
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/rs/zerolog"
     8  	"github.com/stretchr/testify/mock"
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	"github.com/onflow/flow-go/engine/collection/pusher"
    12  	"github.com/onflow/flow-go/model/flow"
    13  	"github.com/onflow/flow-go/model/flow/filter"
    14  	"github.com/onflow/flow-go/model/messages"
    15  	"github.com/onflow/flow-go/module/metrics"
    16  	module "github.com/onflow/flow-go/module/mock"
    17  	"github.com/onflow/flow-go/network/channels"
    18  	"github.com/onflow/flow-go/network/mocknetwork"
    19  	protocol "github.com/onflow/flow-go/state/protocol/mock"
    20  	storage "github.com/onflow/flow-go/storage/mock"
    21  	"github.com/onflow/flow-go/utils/unittest"
    22  )
    23  
    24  type Suite struct {
    25  	suite.Suite
    26  
    27  	identities   flow.IdentityList
    28  	state        *protocol.State
    29  	snapshot     *protocol.Snapshot
    30  	conduit      *mocknetwork.Conduit
    31  	me           *module.Local
    32  	collections  *storage.Collections
    33  	transactions *storage.Transactions
    34  
    35  	engine *pusher.Engine
    36  }
    37  
    38  func (suite *Suite) SetupTest() {
    39  	var err error
    40  
    41  	// add some dummy identities so we have one of each role
    42  	suite.identities = unittest.IdentityListFixture(5, unittest.WithAllRoles())
    43  	me := suite.identities.Filter(filter.HasRole[flow.Identity](flow.RoleCollection))[0]
    44  
    45  	suite.state = new(protocol.State)
    46  	suite.snapshot = new(protocol.Snapshot)
    47  	suite.snapshot.On("Identities", mock.Anything).Return(func(filter flow.IdentityFilter[flow.Identity]) flow.IdentityList {
    48  		return suite.identities.Filter(filter)
    49  	}, func(filter flow.IdentityFilter[flow.Identity]) error {
    50  		return nil
    51  	})
    52  	suite.state.On("Final").Return(suite.snapshot)
    53  
    54  	metrics := metrics.NewNoopCollector()
    55  
    56  	net := new(mocknetwork.Network)
    57  	suite.conduit = new(mocknetwork.Conduit)
    58  	net.On("Register", mock.Anything, mock.Anything).Return(suite.conduit, nil)
    59  
    60  	suite.me = new(module.Local)
    61  	suite.me.On("NodeID").Return(me.NodeID)
    62  
    63  	suite.collections = new(storage.Collections)
    64  	suite.transactions = new(storage.Transactions)
    65  
    66  	suite.engine, err = pusher.New(
    67  		zerolog.New(io.Discard),
    68  		net,
    69  		suite.state,
    70  		metrics,
    71  		metrics,
    72  		suite.me,
    73  		suite.collections,
    74  		suite.transactions,
    75  	)
    76  	suite.Require().Nil(err)
    77  }
    78  
    79  func TestPusherEngine(t *testing.T) {
    80  	suite.Run(t, new(Suite))
    81  }
    82  
    83  // should be able to submit collection guarantees to consensus nodes
    84  func (suite *Suite) TestSubmitCollectionGuarantee() {
    85  
    86  	guarantee := unittest.CollectionGuaranteeFixture()
    87  
    88  	// should submit the collection to consensus nodes
    89  	consensus := suite.identities.Filter(filter.HasRole[flow.Identity](flow.RoleConsensus))
    90  	suite.conduit.On("Publish", guarantee, consensus[0].NodeID).Return(nil)
    91  
    92  	msg := &messages.SubmitCollectionGuarantee{
    93  		Guarantee: *guarantee,
    94  	}
    95  	err := suite.engine.ProcessLocal(msg)
    96  	suite.Require().Nil(err)
    97  
    98  	suite.conduit.AssertExpectations(suite.T())
    99  }
   100  
   101  // should be able to submit collection guarantees to consensus nodes
   102  func (suite *Suite) TestSubmitCollectionGuaranteeNonLocal() {
   103  
   104  	guarantee := unittest.CollectionGuaranteeFixture()
   105  
   106  	// send from a non-allowed role
   107  	sender := suite.identities.Filter(filter.HasRole[flow.Identity](flow.RoleVerification))[0]
   108  
   109  	msg := &messages.SubmitCollectionGuarantee{
   110  		Guarantee: *guarantee,
   111  	}
   112  	err := suite.engine.Process(channels.PushGuarantees, sender.NodeID, msg)
   113  	suite.Require().Error(err)
   114  
   115  	suite.conduit.AssertNumberOfCalls(suite.T(), "Multicast", 0)
   116  }