Add log index to Vat grab

This commit is contained in:
Rob Mulholand 2018-10-22 16:29:02 -05:00
parent 1e74c73591
commit 650bbf5874
7 changed files with 42 additions and 14 deletions

View File

@ -7,9 +7,10 @@ CREATE TABLE maker.vat_grab (
w TEXT,
dink NUMERIC,
dart 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)
);
ALTER TABLE public.checked_headers

View File

@ -798,6 +798,7 @@ CREATE TABLE maker.vat_grab (
w text,
dink numeric,
dart numeric,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -2044,11 +2045,11 @@ ALTER TABLE ONLY maker.vat_fold
--
-- Name: vat_grab vat_grab_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: vat_grab vat_grab_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.vat_grab
ADD CONSTRAINT vat_grab_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT vat_grab_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--

View File

@ -22,7 +22,7 @@ var EthVatGrabLog = types.Log{
TxHash: common.HexToHash("0x7cb84c750ce4985f7811abf641d52ffcb35306d943081475226484cf1470c6fa"),
TxIndex: 4,
BlockHash: common.HexToHash("0xf5a367d560e14c4658ef85e4877e08b5560a4773b69b39f6b8025910b666fade"),
Index: 0,
Index: 5,
Removed: false,
}
@ -34,6 +34,7 @@ var VatGrabModel = vat_grab.VatGrabModel{
W: "0x3728e9777B2a0a611ee0F89e00E01044ce4736d1",
Dink: "115792089237316195423570985008687907853269984665640564039455584007913129639936",
Dart: "115792089237316195423570985008687907853269984665640564039441803007913129639936",
LogIndex: EthVatGrabLog.Index,
TransactionIndex: EthVatGrabLog.TxIndex,
Raw: rawVatGrabLog,
}

View File

@ -44,6 +44,7 @@ func (VatGrabConverter) ToModels(ethLogs []types.Log) ([]VatGrabModel, error) {
W: w.String(),
Dink: dink,
Dart: dart,
LogIndex: ethLog.Index,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}

View File

@ -7,6 +7,7 @@ type VatGrabModel struct {
W string
Dink string
Dart string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}

View File

@ -28,9 +28,9 @@ func (repository VatGrabRepository) Create(headerID int64, models []VatGrabModel
}
for _, model := range models {
_, err = tx.Exec(
`INSERT into maker.vat_grab (header_id, ilk, urn, v, w, dink, dart, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9)`,
headerID, model.Ilk, model.Urn, model.V, model.W, model.Dink, model.Dart, model.TransactionIndex, model.Raw,
`INSERT into maker.vat_grab (header_id, ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6::NUMERIC, $7::NUMERIC, $8, $9, $10)`,
headerID, model.Ilk, model.Urn, model.V, model.W, model.Dink, model.Dart, model.LogIndex, model.TransactionIndex, model.Raw,
)
if err != nil {
tx.Rollback()

View File

@ -37,14 +37,14 @@ var _ = Describe("Vat grab repository", func() {
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a vat grab event", func() {
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
var dbVatGrab vat_grab.VatGrabModel
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbVatGrab.Ilk).To(Equal(test_data.VatGrabModel.Ilk))
Expect(dbVatGrab.Urn).To(Equal(test_data.VatGrabModel.Urn))
@ -52,30 +52,53 @@ var _ = Describe("Vat grab repository", func() {
Expect(dbVatGrab.W).To(Equal(test_data.VatGrabModel.W))
Expect(dbVatGrab.Dink).To(Equal(test_data.VatGrabModel.Dink))
Expect(dbVatGrab.Dart).To(Equal(test_data.VatGrabModel.Dart))
Expect(dbVatGrab.LogIndex).To(Equal(test_data.VatGrabModel.LogIndex))
Expect(dbVatGrab.TransactionIndex).To(Equal(test_data.VatGrabModel.TransactionIndex))
Expect(dbVatGrab.Raw).To(MatchJSON(test_data.VatGrabModel.Raw))
})
It("marks header as checked for logs", func() {
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate pit file vat_grab events", func() {
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 = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat grab events", func() {
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes pit file vat_grab if corresponding header is deleted", func() {
It("removes vat grab if corresponding header is deleted", func() {
err = vatGrabRepository.Create(headerID, []vat_grab.VatGrabModel{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatGrab vat_grab.VatGrabModel
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})