Add log index to various events (#78)

* Add log_index field to flip kick

* Add log index to flop kick

* Add repo tests for Cat.file

* Add log_index to vat_flux

* Add log_index to vat_slip

* Add log_index to vat_toll

* Add log_index to vat_tune

* Add test to repos for updating checked headers
This commit is contained in:
Elizabeth
2018-10-23 16:33:04 -05:00
committed by GitHub
parent 28f8861f81
commit 5682ee988b
45 changed files with 292 additions and 84 deletions
+2
View File
@@ -50,6 +50,7 @@ func (FlopKickConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]
}
entity.Raw = ethLog
entity.TransactionIndex = ethLog.TxIndex
entity.LogIndex = ethLog.Index
results = append(results, entity)
}
return results, nil
@@ -71,6 +72,7 @@ func (FlopKickConverter) ToModels(entities []Entity) ([]Model, error) {
Gal: entity.Gal.String(),
End: time.Unix(endValue, 0),
TransactionIndex: entity.TransactionIndex,
LogIndex: entity.LogIndex,
Raw: rawLogJson,
}
results = append(results, model)
+1 -7
View File
@@ -34,13 +34,7 @@ var _ = Describe("FlopKick Converter", func() {
Expect(err).NotTo(HaveOccurred())
entity := entities[0]
Expect(entity.Id).To(Equal(test_data.FlopKickEntity.Id))
Expect(entity.Lot).To(Equal(test_data.FlopKickEntity.Lot))
Expect(entity.Bid).To(Equal(test_data.FlopKickEntity.Bid))
Expect(entity.Gal).To(Equal(test_data.FlopKickEntity.Gal))
Expect(entity.End).To(Equal(test_data.FlopKickEntity.End))
Expect(entity.TransactionIndex).To(Equal(test_data.FlopKickEntity.TransactionIndex))
Expect(entity.Raw).To(Equal(test_data.FlopKickEntity.Raw))
Expect(entity).To(Equal(test_data.FlopKickEntity))
})
It("returns an error if converting the log to an entity fails", func() {
+1
View File
@@ -28,5 +28,6 @@ type Entity struct {
Gal common.Address
End *big.Int
TransactionIndex uint
LogIndex uint
Raw types.Log
}
+1
View File
@@ -23,5 +23,6 @@ type Model struct {
Gal string
End time.Time
TransactionIndex uint `db:"tx_idx"`
LogIndex uint `db:"log_idx"`
Raw []byte `db:"raw_log"`
}
+3 -3
View File
@@ -40,9 +40,9 @@ func (r FlopKickRepository) Create(headerId int64, flopKicks []Model) error {
}
for _, flopKick := range flopKicks {
_, err = tx.Exec(
`INSERT into maker.flop_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)`,
headerId, flopKick.BidId, flopKick.Lot, flopKick.Bid, flopKick.Gal, flopKick.End, flopKick.TransactionIndex, flopKick.Raw,
`INSERT into maker.flop_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, log_idx, raw_log)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`,
headerId, flopKick.BidId, flopKick.Lot, flopKick.Bid, flopKick.Gal, flopKick.End, flopKick.TransactionIndex, flopKick.LogIndex, flopKick.Raw,
)
if err != nil {
tx.Rollback()
+36 -3
View File
@@ -50,12 +50,12 @@ var _ = Describe("FlopRepository", func() {
BeforeEach(func() {
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
})
It("creates FlopKick records", func() {
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
err = db.QueryRowx(`SELECT * FROM maker.flop_kick WHERE header_id = $1`, headerId).StructScan(&dbResult)
Expect(err).NotTo(HaveOccurred())
Expect(dbResult.HeaderId).To(Equal(headerId))
@@ -65,10 +65,26 @@ var _ = Describe("FlopRepository", func() {
Expect(dbResult.Gal).To(Equal(test_data.FlopKickModel.Gal))
Expect(dbResult.End.Equal(test_data.FlopKickModel.End)).To(BeTrue())
Expect(dbResult.TransactionIndex).To(Equal(test_data.FlopKickModel.TransactionIndex))
Expect(dbResult.LogIndex).To(Equal(test_data.FlopKickModel.LogIndex))
Expect(dbResult.Raw).To(MatchJSON(test_data.FlopKickModel.Raw))
})
It("marks headerId as checked for flop kick logs", func() {
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT flop_kick_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 = repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT flop_kick_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
@@ -76,12 +92,29 @@ var _ = Describe("FlopRepository", func() {
})
It("returns an error if inserting the flop_kick record fails", func() {
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("allows for multiple flop kick events in one transaction if they have different log indexes", func() {
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
newFlopKick := test_data.FlopKickModel
newFlopKick.LogIndex = newFlopKick.LogIndex + 1
err = repository.Create(headerId, []flop_kick.Model{newFlopKick})
Expect(err).NotTo(HaveOccurred())
})
It("deletes the flop_kick records if its corresponding header record is deleted", func() {
err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel})
Expect(err).NotTo(HaveOccurred())
var flopKickCount int
err = db.QueryRow(`SELECT count(*) FROM maker.flop_kick`).Scan(&flopKickCount)
Expect(err).NotTo(HaveOccurred())