Add log index to Pit file events

This commit is contained in:
Rob Mulholand 2018-10-22 14:06:06 -05:00
parent f10bddf353
commit b3d00fe3c5
15 changed files with 122 additions and 37 deletions

View File

@ -4,9 +4,10 @@ CREATE TABLE maker.pit_file_ilk (
ilk TEXT,
what TEXT,
data NUMERIC,
tx_idx INTEGER NOT NUll,
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.pit_file_stability_fee (
@ -14,9 +15,10 @@ CREATE TABLE maker.pit_file_stability_fee (
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)
);
CREATE TABLE maker.pit_file_debt_ceiling (
@ -24,7 +26,8 @@ CREATE TABLE maker.pit_file_debt_ceiling (
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)
);

View File

@ -541,6 +541,7 @@ CREATE TABLE maker.pit_file_debt_ceiling (
header_id integer NOT NULL,
what text,
data numeric,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -576,6 +577,7 @@ CREATE TABLE maker.pit_file_ilk (
ilk text,
what text,
data numeric,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -610,6 +612,7 @@ CREATE TABLE maker.pit_file_stability_fee (
header_id integer NOT NULL,
what text,
data text,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -1925,11 +1928,11 @@ ALTER TABLE ONLY maker.frob
--
-- Name: pit_file_debt_ceiling pit_file_debt_ceiling_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: pit_file_debt_ceiling pit_file_debt_ceiling_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.pit_file_debt_ceiling
ADD CONSTRAINT pit_file_debt_ceiling_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT pit_file_debt_ceiling_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--
@ -1941,11 +1944,11 @@ ALTER TABLE ONLY maker.pit_file_debt_ceiling
--
-- Name: pit_file_ilk pit_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: pit_file_ilk pit_file_ilk_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.pit_file_ilk
ADD CONSTRAINT pit_file_ilk_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT pit_file_ilk_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--
@ -1957,11 +1960,11 @@ ALTER TABLE ONLY maker.pit_file_ilk
--
-- Name: pit_file_stability_fee pit_file_stability_fee_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: pit_file_stability_fee pit_file_stability_fee_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.pit_file_stability_fee
ADD CONSTRAINT pit_file_stability_fee_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT pit_file_stability_fee_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--

View File

@ -43,6 +43,7 @@ func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]interface{},
model := PitFileDebtCeilingModel{
What: what,
Data: data,
LogIndex: ethLog.Index,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}

View File

@ -17,6 +17,7 @@ package debt_ceiling
type PitFileDebtCeilingModel struct {
What string
Data string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}

View File

@ -38,9 +38,9 @@ func (repository PitFileDebtCeilingRepository) Create(headerID int64, models []i
}
_, err = tx.Exec(
`INSERT into maker.pit_file_debt_ceiling (header_id, what, data, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5)`,
headerID, pitFileDC.What, pitFileDC.Data, pitFileDC.TransactionIndex, pitFileDC.Raw,
`INSERT into maker.pit_file_debt_ceiling (header_id, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5, $6)`,
headerID, pitFileDC.What, pitFileDC.Data, pitFileDC.LogIndex, pitFileDC.TransactionIndex, pitFileDC.Raw,
)
if err != nil {

View File

@ -53,22 +53,39 @@ var _ = Describe("Pit file debt ceiling repository", func() {
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a pit file debt ceiling event", func() {
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
var dbPitFile debt_ceiling.PitFileDebtCeilingModel
err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_debt_ceiling WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_debt_ceiling WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbPitFile.What).To(Equal(test_data.PitFileDebtCeilingModel.What))
Expect(dbPitFile.Data).To(Equal(test_data.PitFileDebtCeilingModel.Data))
Expect(dbPitFile.LogIndex).To(Equal(test_data.PitFileDebtCeilingModel.LogIndex))
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileDebtCeilingModel.TransactionIndex))
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileDebtCeilingModel.Raw))
})
It("marks header as checked for logs", func() {
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_debt_ceiling_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 = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_debt_ceiling_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
@ -76,6 +93,9 @@ var _ = Describe("Pit file debt ceiling repository", func() {
})
It("does not duplicate pit file events", func() {
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).To(HaveOccurred())
@ -83,17 +103,21 @@ var _ = Describe("Pit file debt ceiling repository", func() {
})
It("removes pit file if corresponding header is deleted", func() {
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbPitFile debt_ceiling.PitFileDebtCeilingModel
err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_debt_ceiling WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_debt_ceiling WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("Returns an error if model is of wrong type", func() {
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})

View File

@ -46,6 +46,7 @@ func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error)
Ilk: ilk,
What: what,
Data: risk,
LogIndex: ethLog.Index,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}

View File

@ -18,6 +18,7 @@ type PitFileIlkModel struct {
Ilk string
What string
Data string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}

View File

@ -38,9 +38,9 @@ func (repository PitFileIlkRepository) Create(headerID int64, models []interface
}
_, err = tx.Exec(
`INSERT into maker.pit_file_ilk (header_id, ilk, what, data, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`,
headerID, pitFileIlk.Ilk, pitFileIlk.What, pitFileIlk.Data, pitFileIlk.TransactionIndex, pitFileIlk.Raw,
`INSERT into maker.pit_file_ilk (header_id, ilk, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`,
headerID, pitFileIlk.Ilk, pitFileIlk.What, pitFileIlk.Data, pitFileIlk.LogIndex, pitFileIlk.TransactionIndex, pitFileIlk.Raw,
)
if err != nil {
tx.Rollback()

View File

@ -53,23 +53,40 @@ var _ = Describe("Pit file ilk repository", func() {
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a pit file ilk event", func() {
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
var dbPitFile ilk.PitFileIlkModel
err = db.Get(&dbPitFile, `SELECT ilk, what, data, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT ilk, what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbPitFile.Ilk).To(Equal(test_data.PitFileIlkModel.Ilk))
Expect(dbPitFile.What).To(Equal(test_data.PitFileIlkModel.What))
Expect(dbPitFile.Data).To(Equal(test_data.PitFileIlkModel.Data))
Expect(dbPitFile.LogIndex).To(Equal(test_data.PitFileIlkModel.LogIndex))
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileIlkModel.TransactionIndex))
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileIlkModel.Raw))
})
It("marks header as checked for logs", func() {
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_ilk_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 = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_ilk_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
@ -77,6 +94,9 @@ var _ = Describe("Pit file ilk repository", func() {
})
It("does not duplicate pit file ilk events", func() {
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).To(HaveOccurred())
@ -84,11 +104,14 @@ var _ = Describe("Pit file ilk repository", func() {
})
It("removes pit file ilk if corresponding header is deleted", func() {
err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbPitFile ilk.PitFileIlkModel
err = db.Get(&dbPitFile, `SELECT ilk, what, data, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT ilk, what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})

View File

@ -42,6 +42,7 @@ func (PitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]interface{}
model := PitFileStabilityFeeModel{
What: what,
Data: data,
LogIndex: ethLog.Index,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}

View File

@ -17,6 +17,7 @@ package stability_fee
type PitFileStabilityFeeModel struct {
What string
Data string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}

View File

@ -38,9 +38,9 @@ func (repository PitFileStabilityFeeRepository) Create(headerID int64, models []
}
_, err = tx.Exec(
`INSERT into maker.pit_file_stability_fee (header_id, what, data, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5)`,
headerID, pitFileSF.What, pitFileSF.Data, pitFileSF.TransactionIndex, pitFileSF.Raw,
`INSERT into maker.pit_file_stability_fee (header_id, what, data, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6)`,
headerID, pitFileSF.What, pitFileSF.Data, pitFileSF.LogIndex, pitFileSF.TransactionIndex, pitFileSF.Raw,
)
if err != nil {

View File

@ -53,22 +53,39 @@ var _ = Describe("Pit file stability fee repository", func() {
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a pit file stability fee event", func() {
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
var dbPitFile stability_fee.PitFileStabilityFeeModel
err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbPitFile.What).To(Equal(test_data.PitFileStabilityFeeModel.What))
Expect(dbPitFile.Data).To(Equal(test_data.PitFileStabilityFeeModel.Data))
Expect(dbPitFile.LogIndex).To(Equal(test_data.PitFileStabilityFeeModel.LogIndex))
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileStabilityFeeModel.TransactionIndex))
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileStabilityFeeModel.Raw))
})
It("marks header as checked for logs", func() {
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_stability_fee_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 = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT pit_file_stability_fee_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
@ -76,6 +93,9 @@ var _ = Describe("Pit file stability fee repository", func() {
})
It("does not duplicate pit file events", func() {
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).To(HaveOccurred())
@ -83,11 +103,14 @@ var _ = Describe("Pit file stability fee repository", func() {
})
It("removes pit file if corresponding header is deleted", func() {
err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbPitFile stability_fee.PitFileStabilityFeeModel
err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID)
err = db.Get(&dbPitFile, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})

View File

@ -41,7 +41,7 @@ var EthPitFileDebtCeilingLog = types.Log{
TxHash: common.HexToHash("0xd744878a0b6655e3ba729e1019f56b563b4a16750196b8ad6104f3977db43f42"),
TxIndex: 333,
BlockHash: common.HexToHash("0xa54d9d99c315bea3dda7256a36e51773ed009a01c0859295c5382d4b83d7eeb9"),
Index: 0,
Index: 15,
Removed: false,
}
@ -49,6 +49,7 @@ var rawPitFileDebtCeilingLog, _ = json.Marshal(EthPitFileDebtCeilingLog)
var PitFileDebtCeilingModel = debt_ceiling.PitFileDebtCeilingModel{
What: "Line",
Data: big.NewInt(123456).String(),
LogIndex: EthPitFileDebtCeilingLog.Index,
TransactionIndex: EthPitFileDebtCeilingLog.TxIndex,
Raw: rawPitFileDebtCeilingLog,
}
@ -66,7 +67,7 @@ var EthPitFileIlkLog = types.Log{
TxHash: common.HexToHash("0x1ba8125f60fa045c85b35df3983bee37db8627fbc32e3442a5cf17c85bb83f09"),
TxIndex: 111,
BlockHash: common.HexToHash("0x6dc284247c524b22b10a75ef1c9d1709a509208d04c15fa2b675a293db637d21"),
Index: 0,
Index: 14,
Removed: false,
}
@ -75,6 +76,7 @@ var PitFileIlkModel = ilk2.PitFileIlkModel{
Ilk: "fake ilk",
What: "spot",
Data: big.NewInt(123).String(),
LogIndex: EthPitFileIlkLog.Index,
TransactionIndex: EthPitFileIlkLog.TxIndex,
Raw: rawPitFileIlkLog,
}
@ -92,7 +94,7 @@ var EthPitFileStabilityFeeLog = types.Log{
TxHash: common.HexToHash("0x78cdc62316ccf8e31515d09745cc724f557569f01a557d0d09b1066bf7079fd2"),
TxIndex: 222,
BlockHash: common.HexToHash("0xe3d8e458421533170871b4033f978a3793ef10b7e33a9328a13c09e2fd90208d"),
Index: 0,
Index: 13,
Removed: false,
}
@ -100,6 +102,7 @@ var rawPitFileStabilityFeeLog, _ = json.Marshal(EthPitFileStabilityFeeLog)
var PitFileStabilityFeeModel = stability_fee.PitFileStabilityFeeModel{
What: "drip",
Data: "0x8E84a1e068d77059Cbe263C43AD0cDc130863313",
LogIndex: EthPitFileStabilityFeeLog.Index,
TransactionIndex: EthPitFileStabilityFeeLog.TxIndex,
Raw: rawPitFileStabilityFeeLog,
}