Add log index to Vat init

This commit is contained in:
Rob Mulholand 2018-10-22 16:48:27 -05:00
parent 702f90367a
commit 4ac896c246
8 changed files with 44 additions and 16 deletions

View File

@ -2,7 +2,8 @@ CREATE TABLE maker.vat_init (
id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
ilk TEXT,
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)
);

View File

@ -868,6 +868,7 @@ CREATE TABLE maker.vat_init (
id integer NOT NULL,
header_id integer NOT NULL,
ilk text,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -2078,11 +2079,11 @@ ALTER TABLE ONLY maker.vat_heal
--
-- Name: vat_init vat_init_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: vat_init vat_init_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.vat_init
ADD CONSTRAINT vat_init_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT vat_init_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--

View File

@ -38,13 +38,14 @@ var EthVatInitLog = types.Log{
TxHash: common.HexToHash("0xe8f39fbb7fea3621f543868f19b1114e305aff6a063a30d32835ff1012526f91"),
TxIndex: 7,
BlockHash: common.HexToHash("0xe3dd2e05bd8b92833e20ed83e2171bbc06a9ec823232eca1730a807bd8f5edc0"),
Index: 0,
Index: 8,
Removed: false,
}
var rawVatInitLog, _ = json.Marshal(EthVatInitLog)
var VatInitModel = vat_init.VatInitModel{
Ilk: "fake ilk",
LogIndex: EthVatInitLog.Index,
TransactionIndex: EthVatInitLog.TxIndex,
Raw: rawVatInitLog,
}

View File

@ -37,6 +37,7 @@ func (VatInitConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
}
model := VatInitModel{
Ilk: ilk,
LogIndex: ethLog.Index,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}

View File

@ -37,7 +37,7 @@ var _ = Describe("Vat init converter", func() {
Expect(err).To(HaveOccurred())
})
It("converts a log to an model", func() {
It("converts a log to a model", func() {
models, err := converter.ToModels([]types.Log{test_data.EthVatInitLog})
Expect(err).NotTo(HaveOccurred())

View File

@ -16,6 +16,7 @@ package vat_init
type VatInitModel struct {
Ilk string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}

View File

@ -40,9 +40,9 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{})
log.Printf("VatInit model: %v", vatInit)
_, err = tx.Exec(
`INSERT INTO maker.vat_init (header_id, ilk, tx_idx, raw_log)
VALUES($1, $2, $3, $4)`,
headerID, vatInit.Ilk, vatInit.TransactionIndex, vatInit.Raw,
`INSERT INTO maker.vat_init (header_id, ilk, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5)`,
headerID, vatInit.Ilk, vatInit.LogIndex, vatInit.TransactionIndex, vatInit.Raw,
)
if err != nil {
tx.Rollback()
@ -54,7 +54,7 @@ func (repository VatInitRepository) Create(headerID int64, models []interface{})
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_init_checked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_heal_checked = $2`, headerID, true)
UPDATE SET vat_init_checked = $2`, headerID, true)
if err != nil {
tx.Rollback()
return err

View File

@ -52,21 +52,25 @@ var _ = Describe("Vat init repository", func() {
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a vat event", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
var dbVatInit vat_init.VatInitModel
err = db.Get(&dbVatInit, `SELECT ilk, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
err = db.Get(&dbVatInit, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbVatInit.Ilk).To(Equal(test_data.VatInitModel.Ilk))
Expect(dbVatInit.LogIndex).To(Equal(test_data.VatInitModel.LogIndex))
Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex))
Expect(dbVatInit.Raw).To(MatchJSON(test_data.VatInitModel.Raw))
})
It("does not duplicate vat events", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).To(HaveOccurred())
@ -74,16 +78,35 @@ var _ = Describe("Vat init repository", func() {
})
It("removes vat if corresponding header is deleted", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatInit vat_init.VatInitModel
err = db.Get(&dbVatInit, `SELECT ilk, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
err = db.Get(&dbVatInit, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("marks the header as checked for vat init logs", func() {
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_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 = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_init_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
@ -161,7 +184,7 @@ var _ = Describe("Vat init repository", func() {
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
})
It("only treats headers as checked if drip drip logs have been checked", func() {
It("only treats headers as checked if vat init logs have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())