github.com/koko1123/flow-go-1@v0.29.6/engine/protocol/api_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	protocol "github.com/koko1123/flow-go-1/state/protocol/mock"
    12  	"github.com/koko1123/flow-go-1/storage"
    13  	storagemock "github.com/koko1123/flow-go-1/storage/mock"
    14  	"github.com/koko1123/flow-go-1/utils/unittest"
    15  
    16  	"google.golang.org/grpc/codes"
    17  	"google.golang.org/grpc/status"
    18  )
    19  
    20  var CodesNotFoundErr = status.Errorf(codes.NotFound, "not found")
    21  var StorageNotFoundErr = status.Errorf(codes.NotFound, "not found: %v", storage.ErrNotFound)
    22  var InternalErr = status.Errorf(codes.Internal, "internal")
    23  var OutputInternalErr = status.Errorf(codes.Internal, "failed to find: %v", status.Errorf(codes.Internal, "internal"))
    24  
    25  type Suite struct {
    26  	suite.Suite
    27  
    28  	state            *protocol.State
    29  	blocks           *storagemock.Blocks
    30  	headers          *storagemock.Headers
    31  	executionResults *storagemock.ExecutionResults
    32  	snapshot         *protocol.Snapshot
    33  }
    34  
    35  func TestHandler(t *testing.T) {
    36  	suite.Run(t, new(Suite))
    37  }
    38  
    39  func (suite *Suite) SetupTest() {
    40  	rand.Seed(time.Now().UnixNano())
    41  	suite.snapshot = new(protocol.Snapshot)
    42  
    43  	suite.state = new(protocol.State)
    44  	suite.blocks = new(storagemock.Blocks)
    45  	suite.headers = new(storagemock.Headers)
    46  	suite.executionResults = new(storagemock.ExecutionResults)
    47  }
    48  
    49  func (suite *Suite) TestGetLatestFinalizedBlock_Success() {
    50  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
    51  
    52  	// setup the mocks
    53  	block := unittest.BlockFixture()
    54  	header := block.Header
    55  
    56  	suite.snapshot.
    57  		On("Head").
    58  		Return(header, nil).
    59  		Once()
    60  
    61  	suite.blocks.
    62  		On("ByID", header.ID()).
    63  		Return(&block, nil).
    64  		Once()
    65  
    66  	backend := New(suite.state, suite.blocks, suite.headers, nil)
    67  
    68  	// query the handler for the latest finalized block
    69  	responseBlock, err := backend.GetLatestBlock(context.Background(), false)
    70  	suite.checkResponse(responseBlock, err)
    71  
    72  	// make sure we got the latest block
    73  	suite.Require().Equal(block, *responseBlock)
    74  
    75  	suite.assertAllExpectations()
    76  }
    77  
    78  func (suite *Suite) TestGetLatestSealedBlock_Success() {
    79  	suite.state.On("Sealed").Return(suite.snapshot, nil).Maybe()
    80  
    81  	// setup the mocks
    82  	block := unittest.BlockFixture()
    83  	header := block.Header
    84  
    85  	suite.snapshot.
    86  		On("Head").
    87  		Return(header, nil).
    88  		Once()
    89  
    90  	suite.blocks.
    91  		On("ByID", header.ID()).
    92  		Return(&block, nil).
    93  		Once()
    94  
    95  	backend := New(suite.state, suite.blocks, suite.headers, nil)
    96  
    97  	// query the handler for the latest finalized block
    98  	responseBlock, err := backend.GetLatestBlock(context.Background(), true)
    99  	suite.checkResponse(responseBlock, err)
   100  
   101  	// make sure we got the latest block
   102  	suite.Require().Equal(block, *responseBlock)
   103  
   104  	suite.assertAllExpectations()
   105  }
   106  
   107  func (suite *Suite) TestGetLatestBlock_StorageNotFoundFailure() {
   108  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   109  
   110  	// setup the mocks
   111  	block := unittest.BlockFixture()
   112  	header := block.Header
   113  
   114  	suite.snapshot.
   115  		On("Head").
   116  		Return(header, storage.ErrNotFound).
   117  		Once()
   118  
   119  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   120  
   121  	// query the handler for the latest finalized block
   122  	_, err := backend.GetLatestBlock(context.Background(), false)
   123  	suite.Require().Error(err)
   124  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   125  
   126  	suite.assertAllExpectations()
   127  }
   128  
   129  func (suite *Suite) TestGetLatestBlock_CodesNotFoundFailure() {
   130  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   131  
   132  	// setup the mocks
   133  	block := unittest.BlockFixture()
   134  	header := block.Header
   135  
   136  	suite.snapshot.
   137  		On("Head").
   138  		Return(header, CodesNotFoundErr).
   139  		Once()
   140  
   141  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   142  
   143  	// query the handler for the latest finalized block
   144  	_, err := backend.GetLatestBlock(context.Background(), false)
   145  	suite.Require().Error(err)
   146  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   147  
   148  	suite.assertAllExpectations()
   149  }
   150  
   151  func (suite *Suite) TestGetLatestBlock_InternalFailure() {
   152  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   153  
   154  	// setup the mocks
   155  	block := unittest.BlockFixture()
   156  	header := block.Header
   157  
   158  	suite.snapshot.
   159  		On("Head").
   160  		Return(header, InternalErr).
   161  		Once()
   162  
   163  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   164  
   165  	// query the handler for the latest finalized block
   166  	_, err := backend.GetLatestBlock(context.Background(), false)
   167  	suite.Require().Error(err)
   168  	suite.Require().ErrorIs(err, OutputInternalErr)
   169  
   170  	suite.assertAllExpectations()
   171  }
   172  
   173  func (suite *Suite) TestGetBlockById_Success() {
   174  	// setup the mocks
   175  	block := unittest.BlockFixture()
   176  
   177  	suite.blocks.
   178  		On("ByID", block.ID()).
   179  		Return(&block, nil).
   180  		Once()
   181  
   182  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   183  
   184  	// query the handler for the latest sealed block
   185  	responseBlock, err := backend.GetBlockByID(context.Background(), block.ID())
   186  	suite.checkResponse(responseBlock, err)
   187  
   188  	// make sure we got the latest block
   189  	suite.Require().Equal(block.ID(), responseBlock.ID())
   190  
   191  	suite.assertAllExpectations()
   192  }
   193  
   194  func (suite *Suite) TestGetBlockById_StorageNotFoundFailure() {
   195  	// setup the mocks
   196  	block := unittest.BlockFixture()
   197  
   198  	suite.blocks.
   199  		On("ByID", block.ID()).
   200  		Return(&block, storage.ErrNotFound).
   201  		Once()
   202  
   203  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   204  
   205  	// query the handler for the latest block
   206  	_, err := backend.GetBlockByID(context.Background(), block.ID())
   207  	suite.Require().Error(err)
   208  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   209  
   210  	suite.assertAllExpectations()
   211  }
   212  
   213  func (suite *Suite) TestGetBlockById_CodesNotFoundFailure() {
   214  	// setup the mocks
   215  	block := unittest.BlockFixture()
   216  
   217  	suite.blocks.
   218  		On("ByID", block.ID()).
   219  		Return(&block, CodesNotFoundErr).
   220  		Once()
   221  
   222  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   223  
   224  	// query the handler for the latest block
   225  	_, err := backend.GetBlockByID(context.Background(), block.ID())
   226  	suite.Require().Error(err)
   227  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   228  
   229  	suite.assertAllExpectations()
   230  }
   231  
   232  func (suite *Suite) TestGetBlockById_InternalFailure() {
   233  	// setup the mocks
   234  	block := unittest.BlockFixture()
   235  
   236  	suite.blocks.
   237  		On("ByID", block.ID()).
   238  		Return(&block, InternalErr).
   239  		Once()
   240  
   241  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   242  
   243  	// query the handler for the latest block
   244  	_, err := backend.GetBlockByID(context.Background(), block.ID())
   245  	suite.Require().Error(err)
   246  	suite.Require().ErrorIs(err, OutputInternalErr)
   247  
   248  	suite.assertAllExpectations()
   249  }
   250  
   251  func (suite *Suite) TestGetBlockByHeight_Success() {
   252  	// setup the mocks
   253  	block := unittest.BlockFixture()
   254  	height := block.Header.Height
   255  
   256  	suite.blocks.
   257  		On("ByHeight", height).
   258  		Return(&block, nil).
   259  		Once()
   260  
   261  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   262  
   263  	// query the handler for the latest block
   264  	responseBlock, err := backend.GetBlockByHeight(context.Background(), height)
   265  	suite.checkResponse(responseBlock, err)
   266  
   267  	// make sure we got the latest block
   268  	suite.Require().Equal(block.ID(), responseBlock.ID())
   269  
   270  	suite.assertAllExpectations()
   271  }
   272  
   273  func (suite *Suite) TestGetBlockByHeight_StorageNotFoundFailure() {
   274  	// setup the mocks
   275  	block := unittest.BlockFixture()
   276  	height := block.Header.Height
   277  
   278  	suite.blocks.
   279  		On("ByHeight", height).
   280  		Return(&block, StorageNotFoundErr).
   281  		Once()
   282  
   283  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   284  
   285  	// query the handler for the latest block
   286  	_, err := backend.GetBlockByHeight(context.Background(), height)
   287  	suite.Require().Error(err)
   288  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   289  
   290  	suite.assertAllExpectations()
   291  }
   292  
   293  func (suite *Suite) TestGetBlockByHeight_CodesNotFoundFailure() {
   294  	// setup the mocks
   295  	block := unittest.BlockFixture()
   296  	height := block.Header.Height
   297  
   298  	suite.blocks.
   299  		On("ByHeight", height).
   300  		Return(&block, CodesNotFoundErr).
   301  		Once()
   302  
   303  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   304  
   305  	// query the handler for the latest block
   306  	_, err := backend.GetBlockByHeight(context.Background(), height)
   307  	suite.Require().Error(err)
   308  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   309  
   310  	suite.assertAllExpectations()
   311  }
   312  
   313  func (suite *Suite) TestGetBlockByHeight_InternalFailure() {
   314  	// setup the mocks
   315  	block := unittest.BlockFixture()
   316  	height := block.Header.Height
   317  
   318  	suite.blocks.
   319  		On("ByHeight", height).
   320  		Return(&block, InternalErr).
   321  		Once()
   322  
   323  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   324  
   325  	// query the handler for the latest block
   326  	_, err := backend.GetBlockByHeight(context.Background(), height)
   327  	suite.Require().Error(err)
   328  	suite.Require().ErrorIs(err, OutputInternalErr)
   329  
   330  	suite.assertAllExpectations()
   331  }
   332  
   333  func (suite *Suite) TestGetLatestFinalizedBlockHeader_Success() {
   334  	// setup the mocks
   335  	blockHeader := unittest.BlockHeaderFixture()
   336  
   337  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   338  	suite.snapshot.On("Head").Return(blockHeader, nil).Once()
   339  
   340  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   341  
   342  	// query the handler for the latest finalized block
   343  	responseBlockHeader, err := backend.GetLatestBlockHeader(context.Background(), false)
   344  	suite.checkResponse(responseBlockHeader, err)
   345  
   346  	// make sure we got the latest finalized block
   347  	suite.Require().Equal(blockHeader.ID(), responseBlockHeader.ID())
   348  	suite.Require().Equal(blockHeader.Height, responseBlockHeader.Height)
   349  	suite.Require().Equal(blockHeader.ParentID, responseBlockHeader.ParentID)
   350  
   351  	suite.assertAllExpectations()
   352  }
   353  
   354  func (suite *Suite) TestGetLatestSealedBlockHeader_Success() {
   355  	// setup the mocks
   356  	blockHeader := unittest.BlockHeaderFixture()
   357  
   358  	suite.state.On("Sealed").Return(suite.snapshot, nil).Maybe()
   359  	suite.snapshot.On("Head").Return(blockHeader, nil).Once()
   360  
   361  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   362  
   363  	// query the handler for the latest sealed block
   364  	responseBlockHeader, err := backend.GetLatestBlockHeader(context.Background(), true)
   365  	suite.checkResponse(responseBlockHeader, err)
   366  
   367  	// make sure we got the latest sealed block
   368  	suite.Require().Equal(blockHeader.ID(), responseBlockHeader.ID())
   369  	suite.Require().Equal(blockHeader.Height, responseBlockHeader.Height)
   370  	suite.Require().Equal(blockHeader.ParentID, responseBlockHeader.ParentID)
   371  
   372  	suite.assertAllExpectations()
   373  }
   374  
   375  func (suite *Suite) TestGetLatestBlockHeader_StorageNotFoundFailure() {
   376  	// setup the mocks
   377  	blockHeader := unittest.BlockHeaderFixture()
   378  
   379  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   380  	suite.snapshot.On("Head").Return(blockHeader, storage.ErrNotFound).Once()
   381  
   382  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   383  
   384  	// query the handler for the latest block
   385  	_, err := backend.GetLatestBlockHeader(context.Background(), false)
   386  	suite.Require().Error(err)
   387  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   388  
   389  	suite.assertAllExpectations()
   390  }
   391  
   392  func (suite *Suite) TestGetLatestBlockHeader_CodesNotFoundFailure() {
   393  	// setup the mocks
   394  	blockHeader := unittest.BlockHeaderFixture()
   395  
   396  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   397  	suite.snapshot.On("Head").Return(blockHeader, CodesNotFoundErr).Once()
   398  
   399  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   400  
   401  	// query the handler for the latest block
   402  	_, err := backend.GetLatestBlockHeader(context.Background(), false)
   403  	suite.Require().Error(err)
   404  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   405  
   406  	suite.assertAllExpectations()
   407  }
   408  
   409  func (suite *Suite) TestGetLatestBlockHeader_InternalFailure() {
   410  	// setup the mocks
   411  	blockHeader := unittest.BlockHeaderFixture()
   412  
   413  	suite.state.On("Final").Return(suite.snapshot, nil).Maybe()
   414  	suite.snapshot.On("Head").Return(blockHeader, InternalErr).Once()
   415  
   416  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   417  
   418  	// query the handler for the latest block
   419  	_, err := backend.GetLatestBlockHeader(context.Background(), false)
   420  	suite.Require().Error(err)
   421  	suite.Require().ErrorIs(err, OutputInternalErr)
   422  
   423  	suite.assertAllExpectations()
   424  }
   425  
   426  func (suite *Suite) TestGetBlockHeaderByID_Success() {
   427  	// setup the mocks
   428  	block := unittest.BlockFixture()
   429  	header := block.Header
   430  
   431  	suite.headers.
   432  		On("ByBlockID", block.ID()).
   433  		Return(header, nil).
   434  		Once()
   435  
   436  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   437  
   438  	// query the handler for the latest block
   439  	responseBlockHeader, err := backend.GetBlockHeaderByID(context.Background(), block.ID())
   440  
   441  	suite.checkResponse(responseBlockHeader, err)
   442  
   443  	// make sure we got the latest block
   444  	suite.Require().Equal(header.ID(), responseBlockHeader.ID())
   445  	suite.Require().Equal(header.Height, responseBlockHeader.Height)
   446  	suite.Require().Equal(header.ParentID, responseBlockHeader.ParentID)
   447  
   448  	suite.assertAllExpectations()
   449  }
   450  
   451  func (suite *Suite) TestGetBlockHeaderByID_StorageNotFoundFailure() {
   452  	// setup the mocks
   453  	block := unittest.BlockFixture()
   454  	header := block.Header
   455  
   456  	suite.headers.
   457  		On("ByBlockID", block.ID()).
   458  		Return(header, StorageNotFoundErr).
   459  		Once()
   460  
   461  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   462  
   463  	// query the handler for the block header
   464  	_, err := backend.GetBlockHeaderByID(context.Background(), block.ID())
   465  	suite.Require().Error(err)
   466  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   467  
   468  	suite.assertAllExpectations()
   469  }
   470  
   471  func (suite *Suite) TestGetBlockHeaderByID_CodesNotFoundFailure() {
   472  	// setup the mocks
   473  	block := unittest.BlockFixture()
   474  	header := block.Header
   475  
   476  	suite.headers.
   477  		On("ByBlockID", block.ID()).
   478  		Return(header, CodesNotFoundErr).
   479  		Once()
   480  
   481  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   482  
   483  	// query the handler for the block header
   484  	_, err := backend.GetBlockHeaderByID(context.Background(), block.ID())
   485  	suite.Require().Error(err)
   486  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   487  
   488  	suite.assertAllExpectations()
   489  }
   490  
   491  func (suite *Suite) TestGetBlockHeaderByID_InternalFailure() {
   492  	// setup the mocks
   493  	block := unittest.BlockFixture()
   494  	header := block.Header
   495  
   496  	suite.headers.
   497  		On("ByBlockID", block.ID()).
   498  		Return(header, InternalErr).
   499  		Once()
   500  
   501  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   502  
   503  	// query the handler for the block header
   504  	_, err := backend.GetBlockHeaderByID(context.Background(), block.ID())
   505  	suite.Require().Error(err)
   506  	suite.Require().ErrorIs(err, OutputInternalErr)
   507  
   508  	suite.assertAllExpectations()
   509  }
   510  
   511  func (suite *Suite) TestGetBlockHeaderByHeight_Success() {
   512  	// setup the mocks
   513  	blockHeader := unittest.BlockHeaderFixture()
   514  	headerHeight := blockHeader.Height
   515  
   516  	suite.headers.
   517  		On("ByHeight", headerHeight).
   518  		Return(blockHeader, nil).
   519  		Once()
   520  
   521  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   522  
   523  	// query the handler for the block header
   524  	responseBlockHeader, err := backend.GetBlockHeaderByHeight(context.Background(), headerHeight)
   525  
   526  	suite.checkResponse(responseBlockHeader, err)
   527  
   528  	// make sure we got the block header
   529  	suite.Require().Equal(blockHeader.Height, responseBlockHeader.Height)
   530  	suite.Require().Equal(blockHeader.ParentID, responseBlockHeader.ParentID)
   531  
   532  	suite.assertAllExpectations()
   533  }
   534  
   535  func (suite *Suite) TestGetBlockHeaderByHeight_StorageNotFoundFailure() {
   536  	// setup the mocks
   537  	blockHeader := unittest.BlockHeaderFixture()
   538  	headerHeight := blockHeader.Height
   539  
   540  	suite.headers.
   541  		On("ByHeight", headerHeight).
   542  		Return(blockHeader, StorageNotFoundErr).
   543  		Once()
   544  
   545  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   546  
   547  	// query the handler for the block header
   548  	_, err := backend.GetBlockHeaderByHeight(context.Background(), headerHeight)
   549  	suite.Require().Error(err)
   550  	suite.Require().ErrorIs(err, StorageNotFoundErr)
   551  
   552  	suite.assertAllExpectations()
   553  }
   554  
   555  func (suite *Suite) TestGetBlockHeaderByHeight_CodesNotFoundFailure() {
   556  	// setup the mocks
   557  	blockHeader := unittest.BlockHeaderFixture()
   558  	headerHeight := blockHeader.Height
   559  
   560  	suite.headers.
   561  		On("ByHeight", headerHeight).
   562  		Return(blockHeader, CodesNotFoundErr).
   563  		Once()
   564  
   565  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   566  
   567  	// query the handler for the block header
   568  	_, err := backend.GetBlockHeaderByHeight(context.Background(), headerHeight)
   569  	suite.Require().Error(err)
   570  	suite.Require().ErrorIs(err, CodesNotFoundErr)
   571  
   572  	suite.assertAllExpectations()
   573  }
   574  
   575  func (suite *Suite) TestGetBlockHeaderByHeight_InternalFailure() {
   576  	// setup the mocks
   577  	blockHeader := unittest.BlockHeaderFixture()
   578  	headerHeight := blockHeader.Height
   579  
   580  	suite.headers.
   581  		On("ByHeight", headerHeight).
   582  		Return(blockHeader, InternalErr).
   583  		Once()
   584  
   585  	backend := New(suite.state, suite.blocks, suite.headers, nil)
   586  
   587  	// query the handler for the block header
   588  	_, err := backend.GetBlockHeaderByHeight(context.Background(), headerHeight)
   589  	suite.Require().Error(err)
   590  	suite.Require().ErrorIs(err, OutputInternalErr)
   591  
   592  	suite.assertAllExpectations()
   593  }
   594  
   595  func (suite *Suite) checkResponse(resp interface{}, err error) {
   596  	suite.Require().NoError(err)
   597  	suite.Require().NotNil(resp)
   598  }
   599  
   600  func (suite *Suite) assertAllExpectations() {
   601  	suite.snapshot.AssertExpectations(suite.T())
   602  	suite.state.AssertExpectations(suite.T())
   603  	suite.blocks.AssertExpectations(suite.T())
   604  	suite.headers.AssertExpectations(suite.T())
   605  	suite.executionResults.AssertExpectations(suite.T())
   606  }