diff --git a/internal/evmreader/claim.go b/internal/evmreader/claim.go index 027aa31c5..05e20ba8c 100644 --- a/internal/evmreader/claim.go +++ b/internal/evmreader/claim.go @@ -79,9 +79,8 @@ func (r *EvmReader) readAndUpdateClaims( appAddresses := appToAddresses(apps) // All these apps shares the same IConsensus - if len(apps) == 0 { - continue - } + // If there is a key on indexApps, there is at least one + // application in the referred application slice consensusContract := apps[0].consensusContract // Retrieve Claim Acceptance Events from blockchain diff --git a/internal/evmreader/evmreader_test.go b/internal/evmreader/evmreader_test.go index b8d1dc3ca..36900a579 100644 --- a/internal/evmreader/evmreader_test.go +++ b/internal/evmreader/evmreader_test.go @@ -453,6 +453,95 @@ func newMockRepository() *MockRepository { } +func (s *EvmReaderSuite) TestIndexApps() { + + s.Run("Ok", func() { + apps := []application{ + {Application: Application{LastProcessedBlock: 23}}, + {Application: Application{LastProcessedBlock: 22}}, + {Application: Application{LastProcessedBlock: 21}}, + {Application: Application{LastProcessedBlock: 23}}, + } + + keyByProcessedBlock := func(a application) uint64 { + return a.LastProcessedBlock + } + + indexApps := indexApps(keyByProcessedBlock, apps) + + s.Require().Equal(3, len(indexApps)) + apps, ok := indexApps[23] + s.Require().True(ok) + s.Require().Equal(2, len(apps)) + }) + + s.Run("whenIndexAppsArrayEmpty", func() { + apps := []application{} + + keyByProcessedBlock := func(a application) uint64 { + return a.LastProcessedBlock + } + + indexApps := indexApps(keyByProcessedBlock, apps) + + s.Require().Equal(0, len(indexApps)) + }) + + s.Run("whenIndexAppsArray", func() { + apps := []application{} + + keyByProcessedBlock := func(a application) uint64 { + return a.LastProcessedBlock + } + + indexApps := indexApps(keyByProcessedBlock, apps) + + s.Require().Equal(0, len(indexApps)) + }) + + s.Run("whenIndexByEmptyKey", func() { + apps := []application{ + {Application: Application{LastProcessedBlock: 23}}, + {Application: Application{LastProcessedBlock: 22}}, + {Application: Application{LastProcessedBlock: 21}}, + {Application: Application{LastProcessedBlock: 23}}, + } + + keyByIConsensus := func(a application) ConsensusContract { + return a.consensusContract + } + + indexApps := indexApps(keyByIConsensus, apps) + + s.Require().Equal(1, len(indexApps)) + apps, ok := indexApps[nil] + s.Require().True(ok) + s.Require().Equal(4, len(apps)) + }) + + s.Run("whenUsesWrongKey", func() { + apps := []application{ + {Application: Application{LastProcessedBlock: 23}}, + {Application: Application{LastProcessedBlock: 22}}, + {Application: Application{LastProcessedBlock: 21}}, + {Application: Application{LastProcessedBlock: 23}}, + } + + keyByProcessedBlock := func(a application) uint64 { + return a.LastProcessedBlock + } + + indexApps := indexApps(keyByProcessedBlock, apps) + + s.Require().Equal(3, len(indexApps)) + apps, ok := indexApps[0] + s.Require().False(ok) + s.Require().Nil(apps) + + }) + +} + func (m *MockRepository) Unset(methodName string) { for _, call := range m.ExpectedCalls { if call.Method == methodName {