Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #141 from vulcanize/contract-watcher-duplicate-ins…
Browse files Browse the repository at this point in the history
…erts

Do nothing on duplicate log insert in contract watcher
  • Loading branch information
rmulhol committed Sep 23, 2019
2 parents dc30099 + 37e2673 commit a596966
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/contract_watcher/shared/repository/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (r *eventRepository) persistHeaderSyncLogs(logs []types.Log, eventInfo type
for i := 0; i < el; i++ {
pgStr = pgStr + fmt.Sprintf(", $%d", i+6)
}
pgStr = pgStr + ")"
pgStr = pgStr + ") ON CONFLICT DO NOTHING"

// Add this query to the transaction
_, err = tx.Exec(pgStr, data...)
Expand Down
32 changes: 24 additions & 8 deletions pkg/contract_watcher/shared/repository/event_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,13 @@ var _ = Describe("Repository", func() {
err = hr.AddCheckColumn(event.Name + "_" + con.Address)
Expect(err).ToNot(HaveOccurred())

// Try and fail to persist the same log twice in a single call
err = dataStore.PersistLogs([]types.Log{logs[0], logs[0]}, event, con.Address, con.Name)
Expect(err).To(HaveOccurred())

// Successfuly persist the two unique logs
// Successfully persist the two unique logs
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
Expect(err).ToNot(HaveOccurred())

// Try and fail to persist the same logs again in separate call
err = dataStore.PersistLogs([]types.Log{*log}, event, con.Address, con.Name)
Expect(err).To(HaveOccurred())
// Try to insert the same logs again
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
Expect(err).ToNot(HaveOccurred())

// Show that no new logs were entered
var count int
Expand All @@ -339,6 +335,26 @@ var _ = Describe("Repository", func() {
Expect(count).To(Equal(2))
})

It("inserts additional log if only some are duplicate", func() {
hr := lr.NewHeaderRepository(db)
err = hr.AddCheckColumn(event.Name + "_" + con.Address)
Expect(err).ToNot(HaveOccurred())

// Successfully persist first log
err = dataStore.PersistLogs([]types.Log{logs[0]}, event, con.Address, con.Name)
Expect(err).ToNot(HaveOccurred())

// Successfully persist second log even though first already persisted
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
Expect(err).ToNot(HaveOccurred())

// Show that both logs were entered
var count int
err = db.Get(&count, fmt.Sprintf("SELECT COUNT(*) FROM header_%s.transfer_event", constants.TusdContractAddress))
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(2))
})

It("Fails if the persisted event does not have a corresponding eventID column in the checked_headers table", func() {
err = dataStore.PersistLogs(logs, event, con.Address, con.Name)
Expect(err).To(HaveOccurred())
Expand Down

0 comments on commit a596966

Please sign in to comment.