Add log index to Bite

This commit is contained in:
Rob Mulholand 2018-10-19 11:15:01 -05:00
parent ea3de51792
commit 495f39abb8
8 changed files with 20 additions and 10 deletions

View File

@ -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)
)

View File

@ -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);
--

View File

@ -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,
}

View File

@ -27,6 +27,7 @@ type BiteEntity struct {
Tab *big.Int
Flip *big.Int
IArt *big.Int
LogIndex uint
TransactionIndex uint
Raw types.Log
}

View File

@ -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"`
}

View File

@ -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()

View File

@ -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))
})

View File

@ -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,
}