From 4ac896c2460ce63c4b5d7ffd8cf44f579229b66b Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Mon, 22 Oct 2018 16:48:27 -0500 Subject: [PATCH] Add log index to Vat init --- .../1536267596_create_vat_init_table.up.sql | 5 +-- db/schema.sql | 5 +-- pkg/transformers/test_data/vat_init.go | 3 +- pkg/transformers/vat_init/converter.go | 1 + pkg/transformers/vat_init/converter_test.go | 2 +- pkg/transformers/vat_init/model.go | 1 + pkg/transformers/vat_init/repository.go | 8 ++--- pkg/transformers/vat_init/repository_test.go | 35 +++++++++++++++---- 8 files changed, 44 insertions(+), 16 deletions(-) diff --git a/db/migrations/1536267596_create_vat_init_table.up.sql b/db/migrations/1536267596_create_vat_init_table.up.sql index f48ed523..0d670d9e 100644 --- a/db/migrations/1536267596_create_vat_init_table.up.sql +++ b/db/migrations/1536267596_create_vat_init_table.up.sql @@ -2,7 +2,8 @@ CREATE TABLE maker.vat_init ( id SERIAL PRIMARY KEY, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, ilk TEXT, - tx_idx INTEGER NOT NUll, + 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) ); \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index c3c3a354..7c5d67fe 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -868,6 +868,7 @@ CREATE TABLE maker.vat_init ( id integer NOT NULL, header_id integer NOT NULL, ilk text, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -2078,11 +2079,11 @@ ALTER TABLE ONLY maker.vat_heal -- --- Name: vat_init vat_init_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: vat_init vat_init_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.vat_init - ADD CONSTRAINT vat_init_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT vat_init_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/test_data/vat_init.go b/pkg/transformers/test_data/vat_init.go index acb95086..4c67ea35 100644 --- a/pkg/transformers/test_data/vat_init.go +++ b/pkg/transformers/test_data/vat_init.go @@ -38,13 +38,14 @@ var EthVatInitLog = types.Log{ TxHash: common.HexToHash("0xe8f39fbb7fea3621f543868f19b1114e305aff6a063a30d32835ff1012526f91"), TxIndex: 7, BlockHash: common.HexToHash("0xe3dd2e05bd8b92833e20ed83e2171bbc06a9ec823232eca1730a807bd8f5edc0"), - Index: 0, + Index: 8, Removed: false, } var rawVatInitLog, _ = json.Marshal(EthVatInitLog) var VatInitModel = vat_init.VatInitModel{ Ilk: "fake ilk", + LogIndex: EthVatInitLog.Index, TransactionIndex: EthVatInitLog.TxIndex, Raw: rawVatInitLog, } diff --git a/pkg/transformers/vat_init/converter.go b/pkg/transformers/vat_init/converter.go index 88f40fdf..b4f7b337 100644 --- a/pkg/transformers/vat_init/converter.go +++ b/pkg/transformers/vat_init/converter.go @@ -37,6 +37,7 @@ func (VatInitConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { } model := VatInitModel{ Ilk: ilk, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/vat_init/converter_test.go b/pkg/transformers/vat_init/converter_test.go index f7e158bc..35ef0f9c 100644 --- a/pkg/transformers/vat_init/converter_test.go +++ b/pkg/transformers/vat_init/converter_test.go @@ -37,7 +37,7 @@ var _ = Describe("Vat init converter", func() { Expect(err).To(HaveOccurred()) }) - It("converts a log to an model", func() { + It("converts a log to a model", func() { models, err := converter.ToModels([]types.Log{test_data.EthVatInitLog}) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/vat_init/model.go b/pkg/transformers/vat_init/model.go index 8528af13..81376ace 100644 --- a/pkg/transformers/vat_init/model.go +++ b/pkg/transformers/vat_init/model.go @@ -16,6 +16,7 @@ package vat_init type VatInitModel struct { Ilk string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/vat_init/repository.go b/pkg/transformers/vat_init/repository.go index b581b8b1..3d405929 100644 --- a/pkg/transformers/vat_init/repository.go +++ b/pkg/transformers/vat_init/repository.go @@ -40,9 +40,9 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{}) log.Printf("VatInit model: %v", vatInit) _, err = tx.Exec( - `INSERT INTO maker.vat_init (header_id, ilk, tx_idx, raw_log) - VALUES($1, $2, $3, $4)`, - headerID, vatInit.Ilk, vatInit.TransactionIndex, vatInit.Raw, + `INSERT INTO maker.vat_init (header_id, ilk, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5)`, + headerID, vatInit.Ilk, vatInit.LogIndex, vatInit.TransactionIndex, vatInit.Raw, ) if err != nil { tx.Rollback() @@ -54,7 +54,7 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{}) _, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked) VALUES($1, $2) ON CONFLICT (header_id) DO - UPDATE SET vat_heal_checked = $2`, headerID, true) + UPDATE SET vat_init_checked = $2`, headerID, true) if err != nil { tx.Rollback() return err diff --git a/pkg/transformers/vat_init/repository_test.go b/pkg/transformers/vat_init/repository_test.go index 28d1f29d..8f0a8fa8 100644 --- a/pkg/transformers/vat_init/repository_test.go +++ b/pkg/transformers/vat_init/repository_test.go @@ -52,21 +52,25 @@ var _ = Describe("Vat init repository", func() { BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - - err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) - Expect(err).NotTo(HaveOccurred()) }) It("adds a vat event", func() { + err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) + + Expect(err).NotTo(HaveOccurred()) var dbVatInit vat_init.VatInitModel - err = db.Get(&dbVatInit, `SELECT ilk, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID) + err = db.Get(&dbVatInit, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbVatInit.Ilk).To(Equal(test_data.VatInitModel.Ilk)) + Expect(dbVatInit.LogIndex).To(Equal(test_data.VatInitModel.LogIndex)) Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex)) Expect(dbVatInit.Raw).To(MatchJSON(test_data.VatInitModel.Raw)) }) It("does not duplicate vat events", func() { + err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) + Expect(err).NotTo(HaveOccurred()) + err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) Expect(err).To(HaveOccurred()) @@ -74,16 +78,35 @@ var _ = Describe("Vat init repository", func() { }) It("removes vat if corresponding header is deleted", func() { + err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) + Expect(err).NotTo(HaveOccurred()) + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) var dbVatInit vat_init.VatInitModel - err = db.Get(&dbVatInit, `SELECT ilk, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID) + err = db.Get(&dbVatInit, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) It("marks the header as checked for vat init logs", func() { + err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID) + Expect(err).NotTo(HaveOccurred()) + 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 = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) @@ -161,7 +184,7 @@ var _ = Describe("Vat init repository", func() { Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) }) - It("only treats headers as checked if drip drip logs have been checked", func() { + It("only treats headers as checked if vat init logs have been checked", func() { _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) Expect(err).NotTo(HaveOccurred())