Add log index to Frob

This commit is contained in:
Rob Mulholand 2018-10-22 14:37:59 -05:00
parent b3d00fe3c5
commit a0a86871b5
10 changed files with 48 additions and 28 deletions

View File

@ -1,14 +1,15 @@
CREATE TABLE maker.frob ( CREATE TABLE maker.frob (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
ilk bytea, ilk TEXT,
urn bytea, urn TEXT,
dink NUMERIC, dink NUMERIC,
dart NUMERIC, dart NUMERIC,
ink NUMERIC, ink NUMERIC,
art NUMERIC, art NUMERIC,
iart NUMERIC, iart NUMERIC,
log_idx INTEGER NOT NUll,
tx_idx INTEGER NOT NUll, tx_idx INTEGER NOT NUll,
raw_log JSONB, raw_log JSONB,
UNIQUE (header_id, tx_idx) UNIQUE (header_id, tx_idx, log_idx)
); );

View File

@ -1,7 +0,0 @@
ALTER TABLE maker.bite
ALTER COLUMN ilk SET DATA TYPE bytea,
ALTER COLUMN urn SET DATA TYPE bytea;
ALTER TABLE maker.frob
ALTER COLUMN ilk SET DATA TYPE bytea,
ALTER COLUMN urn SET DATA TYPE bytea;

View File

@ -1,7 +0,0 @@
ALTER TABLE maker.bite
ALTER COLUMN ilk SET DATA TYPE text,
ALTER COLUMN urn SET DATA TYPE text;
ALTER TABLE maker.frob
ALTER COLUMN ilk SET DATA TYPE text,
ALTER COLUMN urn SET DATA TYPE text;

View File

@ -507,6 +507,7 @@ CREATE TABLE maker.frob (
ink numeric, ink numeric,
art numeric, art numeric,
iart numeric, iart numeric,
log_idx integer NOT NULL,
tx_idx integer NOT NULL, tx_idx integer NOT NULL,
raw_log jsonb raw_log jsonb
); );
@ -1912,11 +1913,11 @@ ALTER TABLE ONLY maker.flop_kick
-- --
-- Name: frob frob_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- Name: frob frob_header_id_tx_idx_log_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
-- --
ALTER TABLE ONLY maker.frob ALTER TABLE ONLY maker.frob
ADD CONSTRAINT frob_header_id_tx_idx_key UNIQUE (header_id, tx_idx); ADD CONSTRAINT frob_header_id_tx_idx_log_idx_key UNIQUE (header_id, tx_idx, log_idx);
-- --

View File

@ -42,6 +42,10 @@ func (FrobConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Frob
} }
contract := bind.NewBoundContract(address, abi, nil, nil, nil) contract := bind.NewBoundContract(address, abi, nil, nil, nil)
err = contract.UnpackLog(&entity, "Frob", ethLog) err = contract.UnpackLog(&entity, "Frob", ethLog)
if err != nil {
return entities, err
}
entity.LogIndex = ethLog.Index
entity.TransactionIndex = ethLog.TxIndex entity.TransactionIndex = ethLog.TxIndex
entity.Raw = ethLog entity.Raw = ethLog
entities = append(entities, entity) entities = append(entities, entity)
@ -65,6 +69,7 @@ func (FrobConverter) ToModels(entities []FrobEntity) ([]FrobModel, error) {
Dink: entity.Dink.String(), Dink: entity.Dink.String(),
Dart: entity.Dart.String(), Dart: entity.Dart.String(),
IArt: entity.IArt.String(), IArt: entity.IArt.String(),
LogIndex: entity.LogIndex,
TransactionIndex: entity.TransactionIndex, TransactionIndex: entity.TransactionIndex,
Raw: rawLog, Raw: rawLog,
} }

View File

@ -28,6 +28,7 @@ type FrobEntity struct {
Dink *big.Int Dink *big.Int
Dart *big.Int Dart *big.Int
IArt *big.Int IArt *big.Int
LogIndex uint
TransactionIndex uint TransactionIndex uint
Raw types.Log Raw types.Log
} }

View File

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

View File

@ -39,9 +39,9 @@ func (repository FrobRepository) Create(headerID int64, models []FrobModel) erro
return err return err
} }
for _, model := range models { for _, model := range models {
_, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, tx_idx) _, err = tx.Exec(`INSERT INTO maker.frob (header_id, art, dart, dink, iart, ilk, ink, urn, raw_log, log_idx, tx_idx)
VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10)`, VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5::NUMERIC, $6, $7::NUMERIC, $8, $9, $10, $11)`,
headerID, model.Art, model.Dart, model.Dink, model.IArt, model.Ilk, model.Ink, model.Urn, model.Raw, model.TransactionIndex) headerID, model.Art, model.Dart, model.Dink, model.IArt, model.Ilk, model.Ink, model.Urn, model.Raw, model.LogIndex, model.TransactionIndex)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err

View File

@ -51,14 +51,14 @@ var _ = Describe("Frob repository", func() {
BeforeEach(func() { BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
}) })
It("adds a frob", func() { It("adds a frob", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
var dbFrob frob.FrobModel var dbFrob frob.FrobModel
err = db.Get(&dbFrob, `SELECT art, dart, dink, iart, ilk, ink, urn, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) err = db.Get(&dbFrob, `SELECT art, dart, dink, iart, ilk, ink, urn, log_idx, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbFrob.Ilk).To(Equal(test_data.FrobModel.Ilk)) Expect(dbFrob.Ilk).To(Equal(test_data.FrobModel.Ilk))
Expect(dbFrob.Urn).To(Equal(test_data.FrobModel.Urn)) Expect(dbFrob.Urn).To(Equal(test_data.FrobModel.Urn))
@ -67,11 +67,28 @@ var _ = Describe("Frob repository", func() {
Expect(dbFrob.Dink).To(Equal(test_data.FrobModel.Dink)) Expect(dbFrob.Dink).To(Equal(test_data.FrobModel.Dink))
Expect(dbFrob.Dart).To(Equal(test_data.FrobModel.Dart)) Expect(dbFrob.Dart).To(Equal(test_data.FrobModel.Dart))
Expect(dbFrob.IArt).To(Equal(test_data.FrobModel.IArt)) Expect(dbFrob.IArt).To(Equal(test_data.FrobModel.IArt))
Expect(dbFrob.LogIndex).To(Equal(test_data.FrobModel.LogIndex))
Expect(dbFrob.TransactionIndex).To(Equal(test_data.FrobModel.TransactionIndex)) Expect(dbFrob.TransactionIndex).To(Equal(test_data.FrobModel.TransactionIndex))
Expect(dbFrob.Raw).To(MatchJSON(test_data.FrobModel.Raw)) Expect(dbFrob.Raw).To(MatchJSON(test_data.FrobModel.Raw))
}) })
It("marks header as checked for logs", func() { It("marks header as checked for logs", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT frob_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 = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool var headerChecked bool
err = db.Get(&headerChecked, `SELECT frob_checked FROM public.checked_headers WHERE header_id = $1`, headerID) err = db.Get(&headerChecked, `SELECT frob_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -79,6 +96,9 @@ var _ = Describe("Frob repository", func() {
}) })
It("does not duplicate frob events", func() { It("does not duplicate frob events", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel}) err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
@ -86,11 +106,14 @@ var _ = Describe("Frob repository", func() {
}) })
It("removes frob if corresponding header is deleted", func() { It("removes frob if corresponding header is deleted", func() {
err = frobRepository.Create(headerID, []frob.FrobModel{test_data.FrobModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbFrob frob.FrobModel var dbFrob frob.FrobModel
err = db.Get(&dbFrob, `SELECT art, iart, ilk, ink, urn, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID) err = db.Get(&dbFrob, `SELECT art, iart, ilk, ink, urn, log_idx, tx_idx, raw_log FROM maker.frob WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows)) Expect(err).To(MatchError(sql.ErrNoRows))
}) })

View File

@ -57,7 +57,7 @@ var EthFrobLog = types.Log{
TxHash: common.HexToHash(TemporaryFrobTransaction), TxHash: common.HexToHash(TemporaryFrobTransaction),
TxIndex: 123, TxIndex: 123,
BlockHash: TemporaryFrobBlockHash, BlockHash: TemporaryFrobBlockHash,
Index: 0, Index: 7,
Removed: false, Removed: false,
} }
@ -69,6 +69,7 @@ var FrobEntity = frob.FrobEntity{
Dink: dink, Dink: dink,
Dart: dart, Dart: dart,
IArt: iArt, IArt: iArt,
LogIndex: EthFrobLog.Index,
TransactionIndex: EthFrobLog.TxIndex, TransactionIndex: EthFrobLog.TxIndex,
Raw: EthFrobLog, Raw: EthFrobLog,
} }
@ -82,6 +83,7 @@ var FrobModel = frob.FrobModel{
Dink: dink.String(), Dink: dink.String(),
Dart: dart.String(), Dart: dart.String(),
IArt: iArt.String(), IArt: iArt.String(),
LogIndex: EthFrobLog.Index,
TransactionIndex: EthFrobLog.TxIndex, TransactionIndex: EthFrobLog.TxIndex,
Raw: rawFrobLog, Raw: rawFrobLog,
} }