From 650bbf58744763187cc5c2e4dc12f500e2d2f9f9 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Mon, 22 Oct 2018 16:29:02 -0500 Subject: [PATCH] Add log index to Vat grab --- .../1539033620_create_vat_grab.up.sql | 3 +- db/schema.sql | 5 ++- pkg/transformers/test_data/vat_grab.go | 3 +- pkg/transformers/vat_grab/converter.go | 1 + pkg/transformers/vat_grab/model.go | 1 + pkg/transformers/vat_grab/repository.go | 6 +-- pkg/transformers/vat_grab/repository_test.go | 37 +++++++++++++++---- 7 files changed, 42 insertions(+), 14 deletions(-) diff --git a/db/migrations/1539033620_create_vat_grab.up.sql b/db/migrations/1539033620_create_vat_grab.up.sql index 60b66d87..85aa8d50 100644 --- a/db/migrations/1539033620_create_vat_grab.up.sql +++ b/db/migrations/1539033620_create_vat_grab.up.sql @@ -7,9 +7,10 @@ CREATE TABLE maker.vat_grab ( w TEXT, dink NUMERIC, dart NUMERIC, + log_idx INTEGER NOT NULL, tx_idx INTEGER NOT NULL, raw_log JSONB, - UNIQUE (header_id, tx_idx) + UNIQUE (header_id, tx_idx, log_idx) ); ALTER TABLE public.checked_headers diff --git a/db/schema.sql b/db/schema.sql index 95109c5b..3467cdc0 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -798,6 +798,7 @@ CREATE TABLE maker.vat_grab ( w text, dink numeric, dart numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -2044,11 +2045,11 @@ ALTER TABLE ONLY maker.vat_fold -- --- Name: vat_grab vat_grab_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: vat_grab vat_grab_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.vat_grab - ADD CONSTRAINT vat_grab_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT vat_grab_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/test_data/vat_grab.go b/pkg/transformers/test_data/vat_grab.go index dd3307a4..f0b39222 100644 --- a/pkg/transformers/test_data/vat_grab.go +++ b/pkg/transformers/test_data/vat_grab.go @@ -22,7 +22,7 @@ var EthVatGrabLog = types.Log{ TxHash: common.HexToHash("0x7cb84c750ce4985f7811abf641d52ffcb35306d943081475226484cf1470c6fa"), TxIndex: 4, BlockHash: common.HexToHash("0xf5a367d560e14c4658ef85e4877e08b5560a4773b69b39f6b8025910b666fade"), - Index: 0, + Index: 5, Removed: false, } @@ -34,6 +34,7 @@ var VatGrabModel = vat_grab.VatGrabModel{ W: "0x3728e9777B2a0a611ee0F89e00E01044ce4736d1", Dink: "115792089237316195423570985008687907853269984665640564039455584007913129639936", Dart: "115792089237316195423570985008687907853269984665640564039441803007913129639936", + LogIndex: EthVatGrabLog.Index, TransactionIndex: EthVatGrabLog.TxIndex, Raw: rawVatGrabLog, } diff --git a/pkg/transformers/vat_grab/converter.go b/pkg/transformers/vat_grab/converter.go index 67d46e41..892e109b 100644 --- a/pkg/transformers/vat_grab/converter.go +++ b/pkg/transformers/vat_grab/converter.go @@ -44,6 +44,7 @@ func (VatGrabConverter) ToModels(ethLogs []types.Log) ([]VatGrabModel, error) { W: w.String(), Dink: dink, Dart: dart, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/vat_grab/model.go b/pkg/transformers/vat_grab/model.go index dc19883f..a8a8aa20 100644 --- a/pkg/transformers/vat_grab/model.go +++ b/pkg/transformers/vat_grab/model.go @@ -7,6 +7,7 @@ type VatGrabModel struct { W string Dink string Dart string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/vat_grab/repository.go b/pkg/transformers/vat_grab/repository.go index 86f61117..baffbbbf 100644 --- a/pkg/transformers/vat_grab/repository.go +++ b/pkg/transformers/vat_grab/repository.go @@ -28,9 +28,9 @@ func (repository VatGrabRepository) Create(headerID int64, models []VatGrabModel } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.vat_grab (header_id, ilk, urn, v, w, dink, dart, tx_idx, raw_log) - VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9)`, - headerID, model.Ilk, model.Urn, model.V, model.W, model.Dink, model.Dart, model.TransactionIndex, model.Raw, + `INSERT into maker.vat_grab (header_id, ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9, $10)`, + headerID, model.Ilk, model.Urn, model.V, model.W, model.Dink, model.Dart, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/vat_grab/repository_test.go b/pkg/transformers/vat_grab/repository_test.go index 948ed0c6..92746181 100644 --- a/pkg/transformers/vat_grab/repository_test.go +++ b/pkg/transformers/vat_grab/repository_test.go @@ -37,14 +37,14 @@ var _ = Describe("Vat grab repository", func() { BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - - err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) - Expect(err).NotTo(HaveOccurred()) }) It("adds a vat grab event", func() { + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) + + Expect(err).NotTo(HaveOccurred()) var dbVatGrab vat_grab.VatGrabModel - err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID) + err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbVatGrab.Ilk).To(Equal(test_data.VatGrabModel.Ilk)) Expect(dbVatGrab.Urn).To(Equal(test_data.VatGrabModel.Urn)) @@ -52,30 +52,53 @@ var _ = Describe("Vat grab repository", func() { Expect(dbVatGrab.W).To(Equal(test_data.VatGrabModel.W)) Expect(dbVatGrab.Dink).To(Equal(test_data.VatGrabModel.Dink)) Expect(dbVatGrab.Dart).To(Equal(test_data.VatGrabModel.Dart)) + Expect(dbVatGrab.LogIndex).To(Equal(test_data.VatGrabModel.LogIndex)) Expect(dbVatGrab.TransactionIndex).To(Equal(test_data.VatGrabModel.TransactionIndex)) Expect(dbVatGrab.Raw).To(MatchJSON(test_data.VatGrabModel.Raw)) }) It("marks header as checked for logs", func() { + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT vat_grab_checked FROM public.checked_headers WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(headerChecked).To(BeTrue()) }) - It("does not duplicate pit file vat_grab events", func() { + It("updates the header to checked if checked headers row already exists", func() { + _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) + Expect(err).NotTo(HaveOccurred()) + + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT vat_grab_checked FROM public.checked_headers WHERE header_id = $1`, headerID) + Expect(err).NotTo(HaveOccurred()) + Expect(headerChecked).To(BeTrue()) + }) + + It("does not duplicate vat grab events", func() { + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) + Expect(err).NotTo(HaveOccurred()) + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) - It("removes pit file vat_grab if corresponding header is deleted", func() { + It("removes vat grab if corresponding header is deleted", func() { + err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel}) + Expect(err).NotTo(HaveOccurred()) + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) var dbVatGrab vat_grab.VatGrabModel - err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID) + err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) })