@@ -11,6 +11,8 @@ import (
1111 "github.com/rs/zerolog"
1212 "github.com/stretchr/testify/mock"
1313 "github.com/stretchr/testify/suite"
14+ "google.golang.org/grpc/codes"
15+ "google.golang.org/grpc/status"
1416
1517 "github.com/onflow/flow/protobuf/go/flow/entities"
1618 "github.com/onflow/flow/protobuf/go/flow/execution"
@@ -35,7 +37,7 @@ import (
3537 "github.com/onflow/flow-go/module"
3638 "github.com/onflow/flow-go/module/counters"
3739 execmock "github.com/onflow/flow-go/module/execution/mock"
38- testutil "github.com/onflow/flow-go/module/executiondatasync/testutil"
40+ "github.com/onflow/flow-go/module/executiondatasync/testutil"
3941 "github.com/onflow/flow-go/module/metrics"
4042 syncmock "github.com/onflow/flow-go/module/state_synchronization/mock"
4143 protocol "github.com/onflow/flow-go/state/protocol/badger"
@@ -648,6 +650,33 @@ func (s *TransactionsFunctionalSuite) TestTransactionResultByIndex_ExecutionNode
648650 s .Require ().Equal (expectedResult , result )
649651}
650652
653+ func (s * TransactionsFunctionalSuite ) TestTransactionResultByIndex_ExecutionNode_Errors () {
654+ s .T ().Run ("failed to get events from EN" , func (t * testing.T ) {
655+ blockID := s .tf .Block .ID ()
656+ env := systemcontracts .SystemContractsForChain (s .g .ChainID ()).AsTemplateEnv ()
657+ pendingExecuteEventType := blueprints .PendingExecutionEventType (env )
658+
659+ eventsExpectedErr := status .Error (
660+ codes .Unavailable ,
661+ "there are no available nodes" ,
662+ )
663+ s .setupExecutionGetEventsRequestFailed (blockID , pendingExecuteEventType , eventsExpectedErr )
664+
665+ params := s .defaultExecutionNodeParams ()
666+ txBackend , err := NewTransactionsBackend (params )
667+ s .Require ().NoError (err )
668+
669+ expectedErr := fmt .Errorf ("failed to get process transactions events: rpc error: code = Unavailable desc = failed to retrieve result from any execution node: 1 error occurred:\n \t * %w\n \n " , eventsExpectedErr )
670+ index := uint32 (20 ) // case when user transactions is not within the guarantees
671+ result , err := txBackend .GetTransactionResultByIndex (context .Background (), blockID , index , entities .EventEncodingVersion_JSON_CDC_V0 )
672+ s .Require ().Error (err )
673+ s .Require ().Nil (result )
674+
675+ s .Require ().Equal (codes .Unavailable , status .Code (err ))
676+ s .Require ().Equal (expectedErr .Error (), err .Error ())
677+ })
678+ }
679+
651680func (s * TransactionsFunctionalSuite ) TestTransactionResultsByBlockID_ExecutionNode () {
652681 blockID := s .tf .Block .ID ()
653682
@@ -717,17 +746,19 @@ func (s *TransactionsFunctionalSuite) TestTransactionsByBlockID_ExecutionNode()
717746 }
718747
719748 nodeResponse := & execution.GetEventsForBlockIDsResponse {
720- Results : []* execution.GetEventsForBlockIDsResponse_Result {{
721- BlockId : blockID [:],
722- BlockHeight : block .Height ,
723- Events : events ,
724- }},
749+ Results : []* execution.GetEventsForBlockIDsResponse_Result {
750+ {
751+ BlockId : blockID [:],
752+ BlockHeight : block .Height ,
753+ Events : events ,
754+ },
755+ },
725756 EventEncodingVersion : entities .EventEncodingVersion_CCF_V0 ,
726757 }
727758
728759 s .execClient .
729760 On ("GetEventsForBlockIDs" , mock .Anything , expectedRequest ).
730- Return (nodeResponse , nil )
761+ Return (nodeResponse , nil ). Once ()
731762
732763 params := s .defaultExecutionNodeParams ()
733764 txBackend , err := NewTransactionsBackend (params )
@@ -738,37 +769,46 @@ func (s *TransactionsFunctionalSuite) TestTransactionsByBlockID_ExecutionNode()
738769 s .Require ().Equal (expectedTransactions , results )
739770}
740771
772+ func (s * TransactionsFunctionalSuite ) TestTransactionsByBlockID_ExecutionNode_Errors () {
773+ s .T ().Run ("failed to get events from EN" , func (t * testing.T ) {
774+ blockID := s .tf .Block .ID ()
775+ env := systemcontracts .SystemContractsForChain (s .g .ChainID ()).AsTemplateEnv ()
776+ pendingExecuteEventType := blueprints .PendingExecutionEventType (env )
777+
778+ eventsExpectedErr := status .Error (
779+ codes .Unavailable ,
780+ "there are no available nodes" ,
781+ )
782+ s .setupExecutionGetEventsRequestFailed (blockID , pendingExecuteEventType , eventsExpectedErr )
783+
784+ params := s .defaultExecutionNodeParams ()
785+ txBackend , err := NewTransactionsBackend (params )
786+ s .Require ().NoError (err )
787+
788+ expectedErr := fmt .Errorf ("failed to get process transactions events: rpc error: code = Unavailable desc = failed to retrieve result from any execution node: 1 error occurred:\n \t * %w\n \n " , eventsExpectedErr )
789+ results , err := txBackend .GetTransactionsByBlockID (context .Background (), blockID )
790+ s .Require ().Error (err )
791+ s .Require ().Nil (results )
792+
793+ s .Require ().Equal (codes .Unavailable , status .Code (err ))
794+ s .Require ().Equal (expectedErr .Error (), err .Error ())
795+ })
796+ }
797+
741798func (s * TransactionsFunctionalSuite ) TestScheduledTransactionsByBlockID_ExecutionNode () {
742799 block := s .tf .Block
743800 blockID := block .ID ()
744801
745802 env := systemcontracts .SystemContractsForChain (s .g .ChainID ()).AsTemplateEnv ()
746803 pendingExecuteEventType := blueprints .PendingExecutionEventType (env )
747804
748- expectedRequest := & execproto.GetEventsForBlockIDsRequest {
749- Type : string (pendingExecuteEventType ),
750- BlockIds : [][]byte {blockID [:]},
751- }
752-
753- events := make ([]* entities.Event , 0 )
805+ events := make ([]flow.Event , 0 )
754806 for _ , event := range s .tf .ExpectedEvents {
755807 if blueprints .IsPendingExecutionEvent (env , event ) {
756- events = append (events , convert . EventToMessage ( event ) )
808+ events = append (events , event )
757809 }
758810 }
759-
760- nodeResponse := & execution.GetEventsForBlockIDsResponse {
761- Results : []* execution.GetEventsForBlockIDsResponse_Result {{
762- BlockId : blockID [:],
763- BlockHeight : block .Height ,
764- Events : events ,
765- }},
766- EventEncodingVersion : entities .EventEncodingVersion_CCF_V0 ,
767- }
768-
769- s .execClient .
770- On ("GetEventsForBlockIDs" , mock .Anything , expectedRequest ).
771- Return (nodeResponse , nil )
811+ s .setupExecutionGetEventsRequest (blockID , pendingExecuteEventType , block .Height , events )
772812
773813 params := s .defaultExecutionNodeParams ()
774814 txBackend , err := NewTransactionsBackend (params )
@@ -785,3 +825,72 @@ func (s *TransactionsFunctionalSuite) TestScheduledTransactionsByBlockID_Executi
785825 break // call for the first scheduled transaction iterated
786826 }
787827}
828+
829+ func (s * TransactionsFunctionalSuite ) TestScheduledTransactionsByBlockID_ExecutionNode_Errors () {
830+ s .T ().Run ("failed to get events from EN" , func (t * testing.T ) {
831+ block := s .tf .Block
832+ blockID := block .ID ()
833+ env := systemcontracts .SystemContractsForChain (s .g .ChainID ()).AsTemplateEnv ()
834+ pendingExecuteEventType := blueprints .PendingExecutionEventType (env )
835+
836+ eventsExpectedErr := status .Error (
837+ codes .Unavailable ,
838+ "there are no available nodes" ,
839+ )
840+ s .setupExecutionGetEventsRequestFailed (blockID , pendingExecuteEventType , eventsExpectedErr )
841+
842+ params := s .defaultExecutionNodeParams ()
843+ txBackend , err := NewTransactionsBackend (params )
844+ s .Require ().NoError (err )
845+
846+ expectedErr := fmt .Errorf ("rpc error: code = Unavailable desc = failed to retrieve result from any execution node: 1 error occurred:\n \t * %w\n \n " , eventsExpectedErr )
847+ for _ , scheduledTxID := range s .tf .ExpectedScheduledTransactions {
848+ results , err := txBackend .GetScheduledTransaction (context .Background (), scheduledTxID )
849+ s .Require ().Error (err )
850+ s .Require ().Nil (results )
851+
852+ s .Require ().Equal (codes .Unavailable , status .Code (err ))
853+ s .Require ().Equal (expectedErr .Error (), err .Error ())
854+
855+ break // call for the first scheduled transaction iterated
856+ }
857+ })
858+ }
859+
860+ func (s * TransactionsFunctionalSuite ) setupExecutionGetEventsRequest (blockID flow.Identifier , eventType flow.EventType , blockHeight uint64 , events []flow.Event ) {
861+ eventMessages := make ([]* entities.Event , len (events ))
862+ for i , event := range events {
863+ eventMessages [i ] = convert .EventToMessage (event )
864+ }
865+
866+ request := & execproto.GetEventsForBlockIDsRequest {
867+ Type : string (eventType ),
868+ BlockIds : [][]byte {blockID [:]},
869+ }
870+ expectedResponse := & execproto.GetEventsForBlockIDsResponse {
871+ Results : []* execproto.GetEventsForBlockIDsResponse_Result {
872+ {
873+ BlockId : blockID [:],
874+ BlockHeight : blockHeight ,
875+ Events : eventMessages ,
876+ },
877+ },
878+ EventEncodingVersion : entities .EventEncodingVersion_CCF_V0 ,
879+ }
880+
881+ s .execClient .
882+ On ("GetEventsForBlockIDs" , mock .Anything , request ).
883+ Return (expectedResponse , nil ).
884+ Once ()
885+ }
886+
887+ func (s * TransactionsFunctionalSuite ) setupExecutionGetEventsRequestFailed (blockID flow.Identifier , eventType flow.EventType , expectedErr error ) {
888+ expectedRequest := & execproto.GetEventsForBlockIDsRequest {
889+ Type : string (eventType ),
890+ BlockIds : [][]byte {blockID [:]},
891+ }
892+
893+ s .execClient .
894+ On ("GetEventsForBlockIDs" , mock .Anything , expectedRequest ).
895+ Return (nil , expectedErr ).Once ()
896+ }
0 commit comments