diff --git a/pkg/datastore/postgres/repositories/block_repository.go b/pkg/datastore/postgres/repositories/block_repository.go index c1c6cfbe..74018296 100644 --- a/pkg/datastore/postgres/repositories/block_repository.go +++ b/pkg/datastore/postgres/repositories/block_repository.go @@ -56,11 +56,11 @@ func (blockRepository BlockRepository) MissingBlockNumbers(startingBlockNumber i numbers := make([]int64, 0) blockRepository.database.Select(&numbers, `SELECT all_block_numbers - FROM ( - SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series - LEFT JOIN blocks - ON number = all_block_numbers - WHERE number ISNULL OR eth_node_fingerprint != $3`, + FROM ( + SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series + WHERE all_block_numbers NOT IN ( + SELECT number FROM blocks WHERE eth_node_fingerprint = $3 + ) `, startingBlockNumber, highestBlockNumber, nodeId) return numbers diff --git a/pkg/datastore/postgres/repositories/block_repository_test.go b/pkg/datastore/postgres/repositories/block_repository_test.go index 1b2c64fc..93ffe650 100644 --- a/pkg/datastore/postgres/repositories/block_repository_test.go +++ b/pkg/datastore/postgres/repositories/block_repository_test.go @@ -241,6 +241,23 @@ var _ = Describe("Saving blocks", func() { Expect(len(blockRepository.MissingBlockNumbers(1, 1, node.ID))).To(Equal(0)) }) + It("is empty if copies of block exist from both current node and another", func() { + blockRepository.CreateOrUpdateBlock(core.Block{Number: 0}) + blockRepository.CreateOrUpdateBlock(core.Block{Number: 1}) + nodeTwo := core.Node{ + GenesisBlock: "0x456", + NetworkID: 1, + } + dbTwo, err := postgres.NewDB(test_config.DBConfig, nodeTwo) + Expect(err).NotTo(HaveOccurred()) + repositoryTwo := repositories.NewBlockRepository(dbTwo) + repositoryTwo.CreateOrUpdateBlock(core.Block{Number: 0}) + + missing := blockRepository.MissingBlockNumbers(0, 1, node.ID) + + Expect(len(missing)).To(BeZero()) + }) + It("is the only missing block number", func() { blockRepository.CreateOrUpdateBlock(core.Block{Number: 2})