From d87fb14c6bcca53b5e5f5114c97fad80a4d77e75 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 19 Oct 2018 15:30:07 -0500 Subject: [PATCH] Add log index to Drip file events --- .../1536603967_create_drip_file_tables.up.sql | 23 +++++++++++-------- db/schema.sql | 15 +++++++----- pkg/transformers/drip_file/ilk/converter.go | 1 + pkg/transformers/drip_file/ilk/model.go | 1 + pkg/transformers/drip_file/ilk/repository.go | 6 ++--- .../drip_file/ilk/repository_test.go | 5 ++-- pkg/transformers/drip_file/repo/converter.go | 1 + pkg/transformers/drip_file/repo/model.go | 1 + pkg/transformers/drip_file/repo/repository.go | 6 ++--- .../drip_file/repo/repository_test.go | 5 ++-- pkg/transformers/drip_file/vow/converter.go | 1 + pkg/transformers/drip_file/vow/model.go | 1 + pkg/transformers/drip_file/vow/repository.go | 6 ++--- .../drip_file/vow/repository_test.go | 5 ++-- pkg/transformers/test_data/drip_file.go | 9 +++++--- 15 files changed, 52 insertions(+), 34 deletions(-) diff --git a/db/migrations/1536603967_create_drip_file_tables.up.sql b/db/migrations/1536603967_create_drip_file_tables.up.sql index c51efed4..e6da2764 100644 --- a/db/migrations/1536603967_create_drip_file_tables.up.sql +++ b/db/migrations/1536603967_create_drip_file_tables.up.sql @@ -1,12 +1,13 @@ CREATE TABLE maker.drip_file_ilk ( - id SERIAL PRIMARY KEY, - header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, - ilk TEXT, - vow TEXT, - tax NUMERIC, - tx_idx INTEGER NOT NUll, - raw_log JSONB, - UNIQUE (header_id, tx_idx) + id SERIAL PRIMARY KEY, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + ilk TEXT, + vow TEXT, + tax NUMERIC, + log_idx INTEGER NOT NUll, + tx_idx INTEGER NOT NUll, + raw_log JSONB, + UNIQUE (header_id, tx_idx, log_idx) ); CREATE TABLE maker.drip_file_repo ( @@ -14,9 +15,10 @@ CREATE TABLE maker.drip_file_repo ( header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, what TEXT, data 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) ); CREATE TABLE maker.drip_file_vow ( @@ -24,7 +26,8 @@ CREATE TABLE maker.drip_file_vow ( header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, what TEXT, data TEXT, + 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 c075cc33..008cbcd1 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -318,6 +318,7 @@ CREATE TABLE maker.drip_file_ilk ( ilk text, vow text, tax numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -352,6 +353,7 @@ CREATE TABLE maker.drip_file_repo ( header_id integer NOT NULL, what text, data numeric, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -386,6 +388,7 @@ CREATE TABLE maker.drip_file_vow ( header_id integer NOT NULL, what text, data text, + log_idx integer NOT NULL, tx_idx integer NOT NULL, raw_log jsonb ); @@ -1823,11 +1826,11 @@ ALTER TABLE ONLY maker.drip_drip -- --- Name: drip_file_ilk drip_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: drip_file_ilk drip_file_ilk_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.drip_file_ilk - ADD CONSTRAINT drip_file_ilk_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT drip_file_ilk_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- @@ -1839,11 +1842,11 @@ ALTER TABLE ONLY maker.drip_file_ilk -- --- Name: drip_file_repo drip_file_repo_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: drip_file_repo drip_file_repo_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.drip_file_repo - ADD CONSTRAINT drip_file_repo_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT drip_file_repo_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- @@ -1855,11 +1858,11 @@ ALTER TABLE ONLY maker.drip_file_repo -- --- Name: drip_file_vow drip_file_vow_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: drip_file_vow drip_file_vow_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.drip_file_vow - ADD CONSTRAINT drip_file_vow_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT drip_file_vow_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/drip_file/ilk/converter.go b/pkg/transformers/drip_file/ilk/converter.go index 7455cfdf..7620324e 100644 --- a/pkg/transformers/drip_file/ilk/converter.go +++ b/pkg/transformers/drip_file/ilk/converter.go @@ -49,6 +49,7 @@ func (DripFileIlkConverter) ToModels(ethLogs []types.Log) ([]DripFileIlkModel, e Ilk: ilk, Vow: vow, Tax: tax, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/drip_file/ilk/model.go b/pkg/transformers/drip_file/ilk/model.go index 7573692f..3e4ce8e6 100644 --- a/pkg/transformers/drip_file/ilk/model.go +++ b/pkg/transformers/drip_file/ilk/model.go @@ -18,6 +18,7 @@ type DripFileIlkModel struct { Ilk string Vow string Tax string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/drip_file/ilk/repository.go b/pkg/transformers/drip_file/ilk/repository.go index 9ab100f0..d9bad4fe 100644 --- a/pkg/transformers/drip_file/ilk/repository.go +++ b/pkg/transformers/drip_file/ilk/repository.go @@ -40,9 +40,9 @@ func (repository DripFileIlkRepository) Create(headerID int64, models []DripFile } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.drip_file_ilk (header_id, ilk, vow, tax, tx_idx, raw_log) - VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, - headerID, model.Ilk, model.Vow, model.Tax, model.TransactionIndex, model.Raw, + `INSERT into maker.drip_file_ilk (header_id, ilk, vow, tax, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, + headerID, model.Ilk, model.Vow, model.Tax, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/drip_file/ilk/repository_test.go b/pkg/transformers/drip_file/ilk/repository_test.go index fee2064b..359fc518 100644 --- a/pkg/transformers/drip_file/ilk/repository_test.go +++ b/pkg/transformers/drip_file/ilk/repository_test.go @@ -58,11 +58,12 @@ var _ = Describe("Drip file ilk repository", func() { It("adds a drip file ilk event", func() { var dbDripFileIlk ilk.DripFileIlkModel - err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, log_idx, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbDripFileIlk.Ilk).To(Equal(test_data.DripFileIlkModel.Ilk)) Expect(dbDripFileIlk.Vow).To(Equal(test_data.DripFileIlkModel.Vow)) Expect(dbDripFileIlk.Tax).To(Equal(test_data.DripFileIlkModel.Tax)) + Expect(dbDripFileIlk.LogIndex).To(Equal(test_data.DripFileIlkModel.LogIndex)) Expect(dbDripFileIlk.TransactionIndex).To(Equal(test_data.DripFileIlkModel.TransactionIndex)) Expect(dbDripFileIlk.Raw).To(MatchJSON(test_data.DripFileIlkModel.Raw)) }) @@ -87,7 +88,7 @@ var _ = Describe("Drip file ilk repository", func() { Expect(err).NotTo(HaveOccurred()) var dbDripFileIlk ilk.DripFileIlkModel - err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, log_idx, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) diff --git a/pkg/transformers/drip_file/repo/converter.go b/pkg/transformers/drip_file/repo/converter.go index 37590dcf..8dfb29c6 100644 --- a/pkg/transformers/drip_file/repo/converter.go +++ b/pkg/transformers/drip_file/repo/converter.go @@ -44,6 +44,7 @@ func (DripFileRepoConverter) ToModels(ethLogs []types.Log) ([]DripFileRepoModel, model := DripFileRepoModel{ What: what, Data: data, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/drip_file/repo/model.go b/pkg/transformers/drip_file/repo/model.go index 34260055..476617d6 100644 --- a/pkg/transformers/drip_file/repo/model.go +++ b/pkg/transformers/drip_file/repo/model.go @@ -17,6 +17,7 @@ package repo type DripFileRepoModel struct { What string Data string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/drip_file/repo/repository.go b/pkg/transformers/drip_file/repo/repository.go index 82e9d7c8..3ea04d13 100644 --- a/pkg/transformers/drip_file/repo/repository.go +++ b/pkg/transformers/drip_file/repo/repository.go @@ -40,9 +40,9 @@ func (repository DripFileRepoRepository) Create(headerID int64, models []DripFil } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.drip_file_repo (header_id, what, data, tx_idx, raw_log) - VALUES($1, $2, $3::NUMERIC, $4, $5)`, - headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + `INSERT into maker.drip_file_repo (header_id, what, data, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3::NUMERIC, $4, $5, $6)`, + headerID, model.What, model.Data, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/drip_file/repo/repository_test.go b/pkg/transformers/drip_file/repo/repository_test.go index b95c0499..9973c0ac 100644 --- a/pkg/transformers/drip_file/repo/repository_test.go +++ b/pkg/transformers/drip_file/repo/repository_test.go @@ -58,10 +58,11 @@ var _ = Describe("Drip file repo repository", func() { It("adds a drip file repo event", func() { var dbDripFileRepo repo.DripFileRepoModel - err = db.Get(&dbDripFileRepo, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileRepo, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbDripFileRepo.What).To(Equal(test_data.DripFileRepoModel.What)) Expect(dbDripFileRepo.Data).To(Equal(test_data.DripFileRepoModel.Data)) + Expect(dbDripFileRepo.LogIndex).To(Equal(test_data.DripFileRepoModel.LogIndex)) Expect(dbDripFileRepo.TransactionIndex).To(Equal(test_data.DripFileRepoModel.TransactionIndex)) Expect(dbDripFileRepo.Raw).To(MatchJSON(test_data.DripFileRepoModel.Raw)) }) @@ -85,7 +86,7 @@ var _ = Describe("Drip file repo repository", func() { Expect(err).NotTo(HaveOccurred()) var dbDripFileRepo repo.DripFileRepoModel - err = db.Get(&dbDripFileRepo, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileRepo, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) diff --git a/pkg/transformers/drip_file/vow/converter.go b/pkg/transformers/drip_file/vow/converter.go index 082f6c88..c299d6ed 100644 --- a/pkg/transformers/drip_file/vow/converter.go +++ b/pkg/transformers/drip_file/vow/converter.go @@ -44,6 +44,7 @@ func (DripFileVowConverter) ToModels(ethLogs []types.Log) ([]DripFileVowModel, e model := DripFileVowModel{ What: what, Data: data, + LogIndex: ethLog.Index, TransactionIndex: ethLog.TxIndex, Raw: raw, } diff --git a/pkg/transformers/drip_file/vow/model.go b/pkg/transformers/drip_file/vow/model.go index 42614648..f95cace1 100644 --- a/pkg/transformers/drip_file/vow/model.go +++ b/pkg/transformers/drip_file/vow/model.go @@ -17,6 +17,7 @@ package vow type DripFileVowModel struct { What string Data string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/drip_file/vow/repository.go b/pkg/transformers/drip_file/vow/repository.go index 25ba0919..bba93b65 100644 --- a/pkg/transformers/drip_file/vow/repository.go +++ b/pkg/transformers/drip_file/vow/repository.go @@ -40,9 +40,9 @@ func (repository DripFileVowRepository) Create(headerID int64, models []DripFile } for _, model := range models { _, err = tx.Exec( - `INSERT into maker.drip_file_vow (header_id, what, data, tx_idx, raw_log) - VALUES($1, $2, $3, $4, $5)`, - headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + `INSERT into maker.drip_file_vow (header_id, what, data, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5, $6)`, + headerID, model.What, model.Data, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/drip_file/vow/repository_test.go b/pkg/transformers/drip_file/vow/repository_test.go index 740a2506..96fde281 100644 --- a/pkg/transformers/drip_file/vow/repository_test.go +++ b/pkg/transformers/drip_file/vow/repository_test.go @@ -58,10 +58,11 @@ var _ = Describe("Drip file vow repository", func() { It("adds a drip file vow event", func() { var dbDripFileVow vow.DripFileVowModel - err = db.Get(&dbDripFileVow, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileVow, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbDripFileVow.What).To(Equal(test_data.DripFileVowModel.What)) Expect(dbDripFileVow.Data).To(Equal(test_data.DripFileVowModel.Data)) + Expect(dbDripFileVow.LogIndex).To(Equal(test_data.DripFileVowModel.LogIndex)) Expect(dbDripFileVow.TransactionIndex).To(Equal(test_data.DripFileVowModel.TransactionIndex)) Expect(dbDripFileVow.Raw).To(MatchJSON(test_data.DripFileVowModel.Raw)) }) @@ -85,7 +86,7 @@ var _ = Describe("Drip file vow repository", func() { Expect(err).NotTo(HaveOccurred()) var dbDripFileVow vow.DripFileVowModel - err = db.Get(&dbDripFileVow, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) + err = db.Get(&dbDripFileVow, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) diff --git a/pkg/transformers/test_data/drip_file.go b/pkg/transformers/test_data/drip_file.go index f429bd2b..915dbdcf 100644 --- a/pkg/transformers/test_data/drip_file.go +++ b/pkg/transformers/test_data/drip_file.go @@ -39,7 +39,7 @@ var EthDripFileIlkLog = types.Log{ TxHash: common.HexToHash("0xa1c31b7e6389470902237161263558615e60b40f2e63060b2f4aeafe92d57e5f"), TxIndex: 12, BlockHash: common.HexToHash("0x0188f3ee3cc05aa72457fa328e6a461de31e4cbd429fc37f9a52da4e9773c0b4"), - Index: 0, + Index: 15, Removed: false, } @@ -48,6 +48,7 @@ var DripFileIlkModel = ilk2.DripFileIlkModel{ Ilk: "fake ilk", Vow: "fake vow", Tax: big.NewInt(123).String(), + LogIndex: EthDripFileIlkLog.Index, TransactionIndex: EthDripFileIlkLog.TxIndex, Raw: rawDripFileIlkLog, } @@ -65,7 +66,7 @@ var EthDripFileRepoLog = types.Log{ TxHash: common.HexToHash("0xeeaa16de1d91c239b66773e8c2116a26cfeaaf5d962b31466c9bf047a5caa20f"), TxIndex: 13, BlockHash: common.HexToHash("0x89de4145ea8e34dfd9db9a7ea34f5be6f1f402e812fd389acca342513b353288"), - Index: 0, + Index: 16, Removed: false, } @@ -73,6 +74,7 @@ var rawDripFileRepoLog, _ = json.Marshal(EthDripFileRepoLog) var DripFileRepoModel = repo.DripFileRepoModel{ What: "fake what", Data: big.NewInt(123).String(), + LogIndex: EthDripFileRepoLog.Index, TransactionIndex: EthDripFileRepoLog.TxIndex, Raw: rawDripFileRepoLog, } @@ -90,7 +92,7 @@ var EthDripFileVowLog = types.Log{ TxHash: common.HexToHash("0x586e26b71b41fcd6905044dbe8f0cca300517542278f74a9b925c4f800fed85c"), TxIndex: 14, BlockHash: common.HexToHash("0xbec69b1e93503679c9c006819477b86fe16aaff3a418da1e916c431b68be5522"), - Index: 0, + Index: 17, Removed: false, } @@ -98,6 +100,7 @@ var rawDripFileVowLog, _ = json.Marshal(EthDripFileVowLog) var DripFileVowModel = vow.DripFileVowModel{ What: "vow", Data: "0x3728e9777B2a0a611ee0F89e00E01044ce4736d1", + LogIndex: EthDripFileVowLog.Index, TransactionIndex: EthDripFileVowLog.TxIndex, Raw: rawDripFileVowLog, }