Add log index to Tend

This commit is contained in:
Rob Mulholand 2018-10-22 16:07:19 -05:00
parent e0253cce55
commit db52955669
7 changed files with 42 additions and 13 deletions

View File

@ -4,9 +4,10 @@ CREATE TABLE maker.tend (
bid_id NUMERIC NOT NULL UNIQUE,
lot NUMERIC,
bid NUMERIC,
guy VARCHAR,
guy TEXT,
tic 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)
);

View File

@ -685,8 +685,9 @@ CREATE TABLE maker.tend (
bid_id numeric NOT NULL,
lot numeric,
bid numeric,
guy character varying,
guy text,
tic numeric,
log_idx integer NOT NULL,
tx_idx integer NOT NULL,
raw_log jsonb
);
@ -1994,11 +1995,11 @@ ALTER TABLE ONLY maker.price_feeds
--
-- Name: tend tend_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- Name: tend tend_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
ALTER TABLE ONLY maker.tend
ADD CONSTRAINT tend_header_id_tx_idx_key UNIQUE (header_id, tx_idx);
ADD CONSTRAINT tend_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
--

View File

@ -44,6 +44,7 @@ func (TendConverter) ToModels(ethLogs []types.Log) (results []interface{}, err e
//TODO: it is likely that the tic value will need to be added to an emitted event,
//so this will need to be updated at that point
transactionIndex := ethLog.TxIndex
logIndex := ethLog.Index
rawJson, err := json.Marshal(ethLog)
if err != nil {
@ -57,6 +58,7 @@ func (TendConverter) ToModels(ethLogs []types.Log) (results []interface{}, err e
Bid: bidValue,
Guy: guy,
Tic: tic,
LogIndex: logIndex,
TransactionIndex: transactionIndex,
Raw: raw,
}

View File

@ -20,6 +20,7 @@ type TendModel struct {
Bid string
Guy string
Tic string
LogIndex uint `db:"log_idx"`
TransactionIndex uint `db:"tx_idx"`
Raw string `db:"raw_log"`
}

View File

@ -38,9 +38,9 @@ func (repository TendRepository) Create(headerId int64, models []interface{}) er
}
_, err = tx.Exec(
`INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)`,
headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.TransactionIndex, tend.Raw,
`INSERT into maker.tend (header_id, bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9)`,
headerId, tend.BidId, tend.Lot, tend.Bid, tend.Guy, tend.Tic, tend.LogIndex, tend.TransactionIndex, tend.Raw,
)
if err != nil {

View File

@ -50,19 +50,19 @@ var _ = Describe("TendRepository", func() {
BeforeEach(func() {
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
})
It("persists a tend record", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var count int
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
dbResult := tend.TendModel{}
err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId)
err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(dbResult.BidId).To(Equal(test_data.TendModel.BidId))
@ -70,11 +70,28 @@ var _ = Describe("TendRepository", func() {
Expect(dbResult.Bid).To(Equal(test_data.TendModel.Bid))
Expect(dbResult.Guy).To(Equal(test_data.TendModel.Guy))
Expect(dbResult.Tic).To(Equal(test_data.TendModel.Tic))
Expect(dbResult.LogIndex).To(Equal(test_data.TendModel.LogIndex))
Expect(dbResult.TransactionIndex).To(Equal(test_data.TendModel.TransactionIndex))
Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson))
})
It("marks header as checked", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_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 = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
@ -83,11 +100,17 @@ var _ = Describe("TendRepository", func() {
It("returns an error if inserting a tend record fails", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("deletes the tend record if its corresponding header record is deleted", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var count int
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
Expect(err).NotTo(HaveOccurred())

View File

@ -61,6 +61,7 @@ var TendModel = tend.TendModel{
Bid: tendBid,
Guy: tendGuy,
Tic: TendTic,
LogIndex: TendLogNote.Index,
TransactionIndex: TendLogNote.TxIndex,
Raw: string(RawLogNoteJson),
}