From 702f90367a59e1f2738097a17b8ff21ffce83ba7 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Mon, 22 Oct 2018 16:39:49 -0500 Subject: [PATCH] Add log index to Vat heal --- .../1538671228_create_vat_heal.up.sql | 9 +++++---- ...39809713_update_vat_heal_rad_type.down.sql | 1 - ...1539809713_update_vat_heal_rad_type.up.sql | 1 - db/schema.sql | 9 +++++---- pkg/transformers/test_data/vat_heal.go | 5 +++-- pkg/transformers/vat_heal/converter.go | 1 + pkg/transformers/vat_heal/converter_test.go | 7 ++----- pkg/transformers/vat_heal/model.go | 1 + pkg/transformers/vat_heal/repository.go | 6 +++--- pkg/transformers/vat_heal/repository_test.go | 20 +++++++++++++++++-- 10 files changed, 38 insertions(+), 22 deletions(-) delete mode 100644 db/migrations/1539809713_update_vat_heal_rad_type.down.sql delete mode 100644 db/migrations/1539809713_update_vat_heal_rad_type.up.sql diff --git a/db/migrations/1538671228_create_vat_heal.up.sql b/db/migrations/1538671228_create_vat_heal.up.sql index e052c9c7..fa1fc5a5 100644 --- a/db/migrations/1538671228_create_vat_heal.up.sql +++ b/db/migrations/1538671228_create_vat_heal.up.sql @@ -1,12 +1,13 @@ CREATE TABLE maker.vat_heal ( id SERIAL PRIMARY KEY, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, - urn varchar, - v varchar, - rad int, + urn TEXT, + v TEXT, + rad 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/migrations/1539809713_update_vat_heal_rad_type.down.sql b/db/migrations/1539809713_update_vat_heal_rad_type.down.sql deleted file mode 100644 index b581b5a6..00000000 --- a/db/migrations/1539809713_update_vat_heal_rad_type.down.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE maker.vat_heal ALTER COLUMN rad SET DATA TYPE int; \ No newline at end of file diff --git a/db/migrations/1539809713_update_vat_heal_rad_type.up.sql b/db/migrations/1539809713_update_vat_heal_rad_type.up.sql deleted file mode 100644 index aeade49d..00000000 --- a/db/migrations/1539809713_update_vat_heal_rad_type.up.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE maker.vat_heal ALTER COLUMN rad SET DATA TYPE numeric; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index 3467cdc0..c3c3a354 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -831,9 +831,10 @@ ALTER SEQUENCE maker.vat_grab_id_seq OWNED BY maker.vat_grab.id; CREATE TABLE maker.vat_heal ( id integer NOT NULL, header_id integer NOT NULL, - urn character varying, - v character varying, + urn text, + v text, rad numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -2061,11 +2062,11 @@ ALTER TABLE ONLY maker.vat_grab -- --- Name: vat_heal vat_heal_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: vat_heal vat_heal_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.vat_heal - ADD CONSTRAINT vat_heal_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT vat_heal_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/test_data/vat_heal.go b/pkg/transformers/test_data/vat_heal.go index 6c6bdce3..2ab1d0ac 100644 --- a/pkg/transformers/test_data/vat_heal.go +++ b/pkg/transformers/test_data/vat_heal.go @@ -35,7 +35,7 @@ var VatHealLog = types.Log{ TxHash: common.HexToHash("0x991b8079b1333024000dcaf2b00c24c5db0315e112a4ac4d912aa96a602e12b9"), TxIndex: 2, BlockHash: common.HexToHash("0x0c54bdadb149691a103cfa5ad57329bda1e0fe55f1ba1ce5dd49dc1ecfe03daa"), - Index: 0, + Index: 3, Removed: false, } @@ -44,6 +44,7 @@ var VatHealModel = vat_heal.VatHealModel{ Urn: "0x7d7bEe5fCfD8028cf7b00876C5b1421c800561A6", V: "0x7340e006f4135BA6970D43bf43d88DCAD4e7a8CA", Rad: "120", - TransactionIndex: 2, + LogIndex: VatHealLog.Index, + TransactionIndex: VatHealLog.TxIndex, Raw: rawHealLog, } diff --git a/pkg/transformers/vat_heal/converter.go b/pkg/transformers/vat_heal/converter.go index d00c9bae..8a30494f 100644 --- a/pkg/transformers/vat_heal/converter.go +++ b/pkg/transformers/vat_heal/converter.go @@ -52,6 +52,7 @@ func (VatHealConverter) ToModels(ethLogs []types.Log) ([]VatHealModel, error) { Urn: urn.String(), V: v.String(), Rad: radInt.String(), + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: rawLogJson, } diff --git a/pkg/transformers/vat_heal/converter_test.go b/pkg/transformers/vat_heal/converter_test.go index a32579ea..1bf347ad 100644 --- a/pkg/transformers/vat_heal/converter_test.go +++ b/pkg/transformers/vat_heal/converter_test.go @@ -30,11 +30,8 @@ var _ = Describe("VatHeal converter", func() { models, err := converter.ToModels([]types.Log{test_data.VatHealLog}) Expect(err).NotTo(HaveOccurred()) - Expect(models[0].Urn).To(Equal(test_data.VatHealModel.Urn)) - Expect(models[0].V).To(Equal(test_data.VatHealModel.V)) - Expect(models[0].Rad).To(Equal(test_data.VatHealModel.Rad)) - Expect(models[0].TransactionIndex).To(Equal(test_data.VatHealModel.TransactionIndex)) - Expect(models[0].Raw).To(Equal(test_data.VatHealModel.Raw)) + Expect(len(models)).To(Equal(1)) + Expect(models[0]).To(Equal(test_data.VatHealModel)) }) It("Returns an error there are missing topics", func() { diff --git a/pkg/transformers/vat_heal/model.go b/pkg/transformers/vat_heal/model.go index 0a309118..dce7e938 100644 --- a/pkg/transformers/vat_heal/model.go +++ b/pkg/transformers/vat_heal/model.go @@ -18,6 +18,7 @@ type VatHealModel struct { Urn string V string Rad string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/vat_heal/repository.go b/pkg/transformers/vat_heal/repository.go index cef55f43..18ee9318 100644 --- a/pkg/transformers/vat_heal/repository.go +++ b/pkg/transformers/vat_heal/repository.go @@ -40,9 +40,9 @@ func (repository VatHealRepository) Create(headerId int64, models []VatHealModel } for _, model := range models { - _, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, tx_idx, raw_log) - VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, - headerId, model.Urn, model.V, model.Rad, model.TransactionIndex, model.Raw) + _, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, + headerId, model.Urn, model.V, model.Rad, model.LogIndex, model.TransactionIndex, model.Raw) if err != nil { tx.Rollback() return err diff --git a/pkg/transformers/vat_heal/repository_test.go b/pkg/transformers/vat_heal/repository_test.go index 650773aa..b3f90c42 100644 --- a/pkg/transformers/vat_heal/repository_test.go +++ b/pkg/transformers/vat_heal/repository_test.go @@ -62,7 +62,7 @@ var _ = Describe("VatHeal Repository", func() { It("persists vat heal records", func() { anotherVatHeal := test_data.VatHealModel - anotherVatHeal.TransactionIndex = test_data.VatHealModel.TransactionIndex + 1 + anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1 err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, anotherVatHeal}) var dbResult []VatHealDBResult @@ -72,8 +72,9 @@ var _ = Describe("VatHeal Repository", func() { Expect(dbResult[0].Urn).To(Equal(test_data.VatHealModel.Urn)) Expect(dbResult[0].V).To(Equal(test_data.VatHealModel.V)) Expect(dbResult[0].Rad).To(Equal(test_data.VatHealModel.Rad)) + Expect(dbResult[0].LogIndex).To(Equal(test_data.VatHealModel.LogIndex)) + Expect(dbResult[1].LogIndex).To(Equal(test_data.VatHealModel.LogIndex + 1)) Expect(dbResult[0].TransactionIndex).To(Equal(test_data.VatHealModel.TransactionIndex)) - Expect(dbResult[1].TransactionIndex).To(Equal(test_data.VatHealModel.TransactionIndex + 1)) Expect(dbResult[0].Raw).To(MatchJSON(test_data.VatHealModel.Raw)) Expect(dbResult[0].HeaderId).To(Equal(headerId)) }) @@ -81,7 +82,9 @@ var _ = Describe("VatHeal Repository", func() { It("returns an error if the insertion fails", func() { err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred()) + err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) @@ -96,6 +99,19 @@ var _ = Describe("VatHeal Repository", func() { Expect(headerChecked).To(BeTrue()) }) + 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 = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT vat_heal_checked FROM public.checked_headers WHERE header_id = $1`, headerId) + Expect(err).NotTo(HaveOccurred()) + Expect(headerChecked).To(BeTrue()) + }) + It("removes vat heal if corresponding header is deleted", func() { err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred())