From a0a86871b57b3de020f96fd6cf2b81051dd0eff6 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Mon, 22 Oct 2018 14:37:59 -0500 Subject: [PATCH] Add log index to Frob --- .../1533844125_create_frob_table.up.sql | 7 ++-- ...026_update_frob_bite_urn_ilk_type.down.sql | 7 ---- ...70026_update_frob_bite_urn_ilk_type.up.sql | 7 ---- db/schema.sql | 5 +-- pkg/transformers/frob/converter.go | 5 +++ pkg/transformers/frob/entity.go | 1 + pkg/transformers/frob/model.go | 1 + pkg/transformers/frob/repository.go | 6 ++-- pkg/transformers/frob/repository_test.go | 33 ++++++++++++++++--- pkg/transformers/test_data/frob.go | 4 ++- 10 files changed, 48 insertions(+), 28 deletions(-) delete mode 100644 db/migrations/1538670026_update_frob_bite_urn_ilk_type.down.sql delete mode 100644 db/migrations/1538670026_update_frob_bite_urn_ilk_type.up.sql diff --git a/db/migrations/1533844125_create_frob_table.up.sql b/db/migrations/1533844125_create_frob_table.up.sql index 78a0fdb0..13be1690 100644 --- a/db/migrations/1533844125_create_frob_table.up.sql +++ b/db/migrations/1533844125_create_frob_table.up.sql @@ -1,14 +1,15 @@ CREATE TABLE maker.frob ( id SERIAL PRIMARY KEY, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, - ilk bytea, - urn bytea, + ilk TEXT, + urn TEXT, dink NUMERIC, dart NUMERIC, ink NUMERIC, art NUMERIC, iart 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) ); \ No newline at end of file diff --git a/db/migrations/1538670026_update_frob_bite_urn_ilk_type.down.sql b/db/migrations/1538670026_update_frob_bite_urn_ilk_type.down.sql deleted file mode 100644 index e134b3f3..00000000 --- a/db/migrations/1538670026_update_frob_bite_urn_ilk_type.down.sql +++ /dev/null @@ -1,7 +0,0 @@ -ALTER TABLE maker.bite - ALTER COLUMN ilk SET DATA TYPE bytea, - ALTER COLUMN urn SET DATA TYPE bytea; - -ALTER TABLE maker.frob - ALTER COLUMN ilk SET DATA TYPE bytea, - ALTER COLUMN urn SET DATA TYPE bytea; diff --git a/db/migrations/1538670026_update_frob_bite_urn_ilk_type.up.sql b/db/migrations/1538670026_update_frob_bite_urn_ilk_type.up.sql deleted file mode 100644 index 730241d9..00000000 --- a/db/migrations/1538670026_update_frob_bite_urn_ilk_type.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -ALTER TABLE maker.bite - ALTER COLUMN ilk SET DATA TYPE text, - ALTER COLUMN urn SET DATA TYPE text; - -ALTER TABLE maker.frob - ALTER COLUMN ilk SET DATA TYPE text, - ALTER COLUMN urn SET DATA TYPE text; diff --git a/db/schema.sql b/db/schema.sql index df584fc5..90f1de40 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -507,6 +507,7 @@ CREATE TABLE maker.frob ( ink numeric, art numeric, iart numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -1912,11 +1913,11 @@ ALTER TABLE ONLY maker.flop_kick -- --- Name: frob frob_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: frob frob_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.frob - ADD CONSTRAINT frob_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT frob_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/frob/converter.go b/pkg/transformers/frob/converter.go index 9f22bd25..34bb1ea3 100644 --- a/pkg/transformers/frob/converter.go +++ b/pkg/transformers/frob/converter.go @@ -42,6 +42,10 @@ func (FrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Frob } contract := bind.NewBoundContract(address, abi, nil, nil, nil) err = contract.UnpackLog(&entity, "Frob", ethLog) + if err != nil { + return entities, err + } + entity.LogIndex = ethLog.Index entity.TransactionIndex = ethLog.TxIndex entity.Raw = ethLog entities = append(entities, entity) @@ -65,6 +69,7 @@ func (FrobConverter) ToModels(entities []FrobEntity) ([]FrobModel, error) { Dink: entity.Dink.String(), Dart: entity.Dart.String(), IArt: entity.IArt.String(), + LogIndex: entity.LogIndex, TransactionIndex: entity.TransactionIndex, Raw: rawLog, } diff --git a/pkg/transformers/frob/entity.go b/pkg/transformers/frob/entity.go index c150219e..d33eb49d 100644 --- a/pkg/transformers/frob/entity.go +++ b/pkg/transformers/frob/entity.go @@ -28,6 +28,7 @@ type FrobEntity struct { Dink *big.Int Dart *big.Int IArt *big.Int + LogIndex uint TransactionIndex uint Raw types.Log } diff --git a/pkg/transformers/frob/model.go b/pkg/transformers/frob/model.go index bbb31f37..eef52833 100644 --- a/pkg/transformers/frob/model.go +++ b/pkg/transformers/frob/model.go @@ -22,6 +22,7 @@ type FrobModel struct { Dink string Dart string IArt string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/frob/repository.go b/pkg/transformers/frob/repository.go index e4e1dfb7..ab7de620 100644 --- a/pkg/transformers/frob/repository.go +++ b/pkg/transformers/frob/repository.go @@ -39,9 +39,9 @@ func (repository FrobRepository) Create(headerID int64, models []FrobModel) erro return err } for _, model := range models { - _, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, tx_idx) - VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10)`, - headerID, model.Art, model.Dart, model.Dink, model.IArt, model.Ilk, model.Ink, model.Urn, model.Raw, model.TransactionIndex) + _, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx) + VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`, + headerID, model.Art, model.Dart, model.Dink, model.IArt, model.Ilk, model.Ink, model.Urn, model.Raw, model.LogIndex, model.TransactionIndex) if err != nil { tx.Rollback() return err diff --git a/pkg/transformers/frob/repository_test.go b/pkg/transformers/frob/repository_test.go index 1bc96071..dc14e824 100644 --- a/pkg/transformers/frob/repository_test.go +++ b/pkg/transformers/frob/repository_test.go @@ -51,14 +51,14 @@ var _ = Describe("Frob repository", func() { BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - - err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) - Expect(err).NotTo(HaveOccurred()) }) It("adds a frob", func() { + err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) + + Expect(err).NotTo(HaveOccurred()) var dbFrob frob.FrobModel - err = db.Get(&dbFrob, `SELECT art, dart, dink, iart, ilk, ink, urn, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) + err = db.Get(&dbFrob, `SELECT art, dart, dink, iart, ilk, ink, urn, log_idx, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbFrob.Ilk).To(Equal(test_data.FrobModel.Ilk)) Expect(dbFrob.Urn).To(Equal(test_data.FrobModel.Urn)) @@ -67,11 +67,28 @@ var _ = Describe("Frob repository", func() { Expect(dbFrob.Dink).To(Equal(test_data.FrobModel.Dink)) Expect(dbFrob.Dart).To(Equal(test_data.FrobModel.Dart)) Expect(dbFrob.IArt).To(Equal(test_data.FrobModel.IArt)) + Expect(dbFrob.LogIndex).To(Equal(test_data.FrobModel.LogIndex)) Expect(dbFrob.TransactionIndex).To(Equal(test_data.FrobModel.TransactionIndex)) Expect(dbFrob.Raw).To(MatchJSON(test_data.FrobModel.Raw)) }) It("marks header as checked for logs", func() { + err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT frob_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 = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT frob_checked FROM public.checked_headers WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) @@ -79,6 +96,9 @@ var _ = Describe("Frob repository", func() { }) It("does not duplicate frob events", func() { + err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) + Expect(err).NotTo(HaveOccurred()) + err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) Expect(err).To(HaveOccurred()) @@ -86,11 +106,14 @@ var _ = Describe("Frob repository", func() { }) It("removes frob if corresponding header is deleted", func() { + err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) + Expect(err).NotTo(HaveOccurred()) + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) var dbFrob frob.FrobModel - err = db.Get(&dbFrob, `SELECT art, iart, ilk, ink, urn, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) + err = db.Get(&dbFrob, `SELECT art, iart, ilk, ink, urn, log_idx, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) diff --git a/pkg/transformers/test_data/frob.go b/pkg/transformers/test_data/frob.go index 7ed5cd1c..69d828d5 100644 --- a/pkg/transformers/test_data/frob.go +++ b/pkg/transformers/test_data/frob.go @@ -57,7 +57,7 @@ var EthFrobLog = types.Log{ TxHash: common.HexToHash(TemporaryFrobTransaction), TxIndex: 123, BlockHash: TemporaryFrobBlockHash, - Index: 0, + Index: 7, Removed: false, } @@ -69,6 +69,7 @@ var FrobEntity = frob.FrobEntity{ Dink: dink, Dart: dart, IArt: iArt, + LogIndex: EthFrobLog.Index, TransactionIndex: EthFrobLog.TxIndex, Raw: EthFrobLog, } @@ -82,6 +83,7 @@ var FrobModel = frob.FrobModel{ Dink: dink.String(), Dart: dart.String(), IArt: iArt.String(), + LogIndex: EthFrobLog.Index, TransactionIndex: EthFrobLog.TxIndex, Raw: rawFrobLog, }