From 495f39abb8df5bdc823ab15c3330e96a0db1d78c Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 19 Oct 2018 11:15:01 -0500 Subject: [PATCH] Add log index to Bite --- db/migrations/1534295713_create_bite_table.up.sql | 3 ++- db/schema.sql | 5 +++-- pkg/transformers/bite/converter.go | 3 +++ pkg/transformers/bite/entity.go | 1 + pkg/transformers/bite/model.go | 1 + pkg/transformers/bite/repository.go | 6 +++--- pkg/transformers/bite/repository_test.go | 5 +++-- pkg/transformers/test_data/bite.go | 6 ++++-- 8 files changed, 20 insertions(+), 10 deletions(-) diff --git a/db/migrations/1534295713_create_bite_table.up.sql b/db/migrations/1534295713_create_bite_table.up.sql index e4045eac..8ee7e670 100644 --- a/db/migrations/1534295713_create_bite_table.up.sql +++ b/db/migrations/1534295713_create_bite_table.up.sql @@ -9,6 +9,7 @@ CREATE TABLE maker.bite ( tab NUMERIC, nflip NUMERIC, tx_idx INTEGER NOT NUll, + log_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 14426a85..b079c817 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -72,6 +72,7 @@ CREATE TABLE maker.bite ( tab numeric, nflip numeric, tx_idx integer NOT NULL, + log_idx integer NOT NULL, raw_log jsonb ); @@ -1707,11 +1708,11 @@ ALTER TABLE ONLY public.watched_contracts ALTER COLUMN contract_id SET DEFAULT n -- --- Name: bite bite_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: bite bite_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- ALTER TABLE ONLY maker.bite - ADD CONSTRAINT bite_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + ADD CONSTRAINT bite_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx); -- diff --git a/pkg/transformers/bite/converter.go b/pkg/transformers/bite/converter.go index 6df68c83..3dd3f4b0 100644 --- a/pkg/transformers/bite/converter.go +++ b/pkg/transformers/bite/converter.go @@ -51,6 +51,7 @@ func (BiteConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Bite } entity.Raw = ethLog + entity.LogIndex = ethLog.Index entity.TransactionIndex = ethLog.TxIndex entities = append(entities, *entity) @@ -69,6 +70,7 @@ func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, err iArt := entity.IArt tab := entity.Tab flip := entity.Flip + logIdx := entity.LogIndex txIdx := entity.TransactionIndex rawLogJson, err := json.Marshal(entity.Raw) rawLogString := string(rawLogJson) @@ -84,6 +86,7 @@ func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, err IArt: shared.BigIntToString(iArt), Tab: shared.BigIntToString(tab), NFlip: shared.BigIntToString(flip), + LogIndex: logIdx, TransactionIndex: txIdx, Raw: rawLogString, } diff --git a/pkg/transformers/bite/entity.go b/pkg/transformers/bite/entity.go index f20a6ff7..9270eebb 100644 --- a/pkg/transformers/bite/entity.go +++ b/pkg/transformers/bite/entity.go @@ -27,6 +27,7 @@ type BiteEntity struct { Tab *big.Int Flip *big.Int IArt *big.Int + LogIndex uint TransactionIndex uint Raw types.Log } diff --git a/pkg/transformers/bite/model.go b/pkg/transformers/bite/model.go index 34100857..b7b3890c 100644 --- a/pkg/transformers/bite/model.go +++ b/pkg/transformers/bite/model.go @@ -22,6 +22,7 @@ type BiteModel struct { IArt string Tab string NFlip string + LogIndex uint `db:"log_idx"` TransactionIndex uint `db:"tx_idx"` Raw string `db:"raw_log"` } diff --git a/pkg/transformers/bite/repository.go b/pkg/transformers/bite/repository.go index 6b9ce952..ddb5a4c8 100644 --- a/pkg/transformers/bite/repository.go +++ b/pkg/transformers/bite/repository.go @@ -40,9 +40,9 @@ func (repository BiteRepository) Create(headerID int64, models []BiteModel) erro } for _, model := range models { _, err := tx.Exec( - `INSERT into maker.bite (header_id, ilk, urn, ink, art, iart, tab, nflip, tx_idx, raw_log) - VALUES($1, $2, $3, $4::NUMERIC, $5::NUMERIC, $6::NUMERIC, $7::NUMERIC, $8::NUMERIC, $9, $10)`, - headerID, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.NFlip, model.TransactionIndex, model.Raw, + `INSERT into maker.bite (header_id, ilk, urn, ink, art, iart, tab, nflip, log_idx, tx_idx, raw_log) + VALUES($1, $2, $3, $4::NUMERIC, $5::NUMERIC, $6::NUMERIC, $7::NUMERIC, $8::NUMERIC, $9, $10, $11)`, + headerID, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.NFlip, model.LogIndex, model.TransactionIndex, model.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/bite/repository_test.go b/pkg/transformers/bite/repository_test.go index 57beb7f4..d6958ea4 100644 --- a/pkg/transformers/bite/repository_test.go +++ b/pkg/transformers/bite/repository_test.go @@ -58,7 +58,7 @@ var _ = Describe("Bite repository", func() { It("persists a bite record", func() { var dbBite bite.BiteModel - err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) + err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, log_idx, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) Expect(dbBite.Ilk).To(Equal(test_data.BiteModel.Ilk)) Expect(dbBite.Urn).To(Equal(test_data.BiteModel.Urn)) @@ -67,6 +67,7 @@ var _ = Describe("Bite repository", func() { Expect(dbBite.Tab).To(Equal(test_data.BiteModel.Tab)) Expect(dbBite.NFlip).To(Equal(test_data.BiteModel.NFlip)) Expect(dbBite.IArt).To(Equal(test_data.BiteModel.IArt)) + Expect(dbBite.LogIndex).To(Equal(test_data.BiteModel.LogIndex)) Expect(dbBite.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex)) Expect(dbBite.Raw).To(MatchJSON(test_data.BiteModel.Raw)) }) @@ -90,7 +91,7 @@ var _ = Describe("Bite repository", func() { Expect(err).NotTo(HaveOccurred()) var dbBite bite.BiteModel - err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) + err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, log_idx, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) diff --git a/pkg/transformers/test_data/bite.go b/pkg/transformers/test_data/bite.go index 9b2da2e6..f65e24b3 100644 --- a/pkg/transformers/test_data/bite.go +++ b/pkg/transformers/test_data/bite.go @@ -57,7 +57,7 @@ var EthBiteLog = types.Log{ TxHash: common.HexToHash(TemporaryBiteTransaction), TxIndex: 111, BlockHash: TemporaryBiteBlockHash, - Index: 0, + Index: 7, Removed: false, } @@ -69,6 +69,7 @@ var BiteEntity = bite.BiteEntity{ Tab: biteTab, Flip: biteFlip, IArt: biteIArt, + LogIndex: EthBiteLog.Index, TransactionIndex: EthBiteLog.TxIndex, Raw: EthBiteLog, } @@ -78,9 +79,10 @@ var BiteModel = bite.BiteModel{ Urn: biteLadString, Ink: biteInk.String(), Art: biteArt.String(), + IArt: biteIArt.String(), Tab: biteTab.String(), NFlip: biteFlip.String(), - IArt: biteIArt.String(), + LogIndex: EthBiteLog.Index, TransactionIndex: EthBiteLog.TxIndex, Raw: biteRawString, }