Convert Bite event field types to numeric

- Enables arithmetic operations when constructing views
- Also removes application-level references to DB ID
This commit is contained in:
Rob Mulholand 2018-10-16 14:51:26 -05:00
parent d3df685c46
commit d0226dbce5
11 changed files with 23 additions and 53 deletions

View File

@ -1,13 +1,13 @@
CREATE TABLE maker.bite ( CREATE TABLE maker.bite (
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,
lad bytea, urn TEXT,
ink VARCHAR, ink NUMERIC,
art VARCHAR, art NUMERIC,
iArt VARCHAR, iArt NUMERIC,
tab NUMERIC, tab NUMERIC,
flip VARCHAR, nflip NUMERIC,
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)

View File

@ -1 +0,0 @@
ALTER TABLE maker.bite RENAME COLUMN urn TO lad;

View File

@ -1 +0,0 @@
ALTER TABLE maker.bite RENAME COLUMN lad TO urn;

View File

@ -66,11 +66,11 @@ CREATE TABLE maker.bite (
header_id integer NOT NULL, header_id integer NOT NULL,
ilk text, ilk text,
urn text, urn text,
ink character varying, ink numeric,
art character varying, art numeric,
iart character varying, iart numeric,
tab numeric, tab numeric,
flip character varying, nflip numeric,
tx_idx integer NOT NULL, tx_idx integer NOT NULL,
raw_log jsonb raw_log jsonb
); );

View File

@ -61,7 +61,6 @@ func (BiteConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Bite
func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, error) { func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, error) {
var models []BiteModel var models []BiteModel
for _, entity := range entities { for _, entity := range entities {
id := entity.Id
ilk := common.BytesToAddress(entity.Ilk[:common.AddressLength]).String() ilk := common.BytesToAddress(entity.Ilk[:common.AddressLength]).String()
urn := common.BytesToAddress(entity.Urn[:common.AddressLength]).String() urn := common.BytesToAddress(entity.Urn[:common.AddressLength]).String()
ink := entity.Ink ink := entity.Ink
@ -77,14 +76,13 @@ func (converter BiteConverter) ToModels(entities []BiteEntity) ([]BiteModel, err
} }
model := BiteModel{ model := BiteModel{
Id: shared.BigIntToString(id),
Ilk: ilk, Ilk: ilk,
Urn: urn, Urn: urn,
Ink: shared.BigIntToString(ink), Ink: shared.BigIntToString(ink),
Art: shared.BigIntToString(art), Art: shared.BigIntToString(art),
IArt: shared.BigIntToString(iArt), IArt: shared.BigIntToString(iArt),
Tab: shared.BigIntToString(tab), Tab: shared.BigIntToString(tab),
Flip: shared.BigIntToString(flip), NFlip: shared.BigIntToString(flip),
TransactionIndex: txIdx, TransactionIndex: txIdx,
Raw: rawLogString, Raw: rawLogString,
} }

View File

@ -18,8 +18,6 @@ package bite_test
import ( import (
"encoding/json" "encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite" "github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
@ -60,10 +58,6 @@ var _ = Describe("Bite Converter", func() {
Describe("ToModel", func() { Describe("ToModel", func() {
var emptyEntity = bite.BiteEntity{} var emptyEntity = bite.BiteEntity{}
BeforeEach(func() {
emptyEntity.Id = big.NewInt(1)
})
It("converts an Entity to a Model", func() { It("converts an Entity to a Model", func() {
models, err := converter.ToModels([]bite.BiteEntity{test_data.BiteEntity}) models, err := converter.ToModels([]bite.BiteEntity{test_data.BiteEntity})
@ -71,21 +65,19 @@ var _ = Describe("Bite Converter", func() {
Expect(len(models)).To(Equal(1)) Expect(len(models)).To(Equal(1))
model := models[0] model := models[0]
Expect(model).To(Equal(test_data.BiteModel)) Expect(model).To(Equal(test_data.BiteModel))
Expect(model.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex))
}) })
It("handles nil values", func() { It("handles nil values", func() {
emptyLog, err := json.Marshal(types.Log{}) emptyLog, err := json.Marshal(types.Log{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
expectedModel := bite.BiteModel{ expectedModel := bite.BiteModel{
Id: "1",
Ilk: "0x0000000000000000000000000000000000000000", Ilk: "0x0000000000000000000000000000000000000000",
Urn: "0x0000000000000000000000000000000000000000", Urn: "0x0000000000000000000000000000000000000000",
Ink: "", Ink: "",
Art: "", Art: "",
IArt: "", IArt: "",
Tab: "", Tab: "",
Flip: "", NFlip: "",
TransactionIndex: 0, TransactionIndex: 0,
Raw: string(emptyLog), Raw: string(emptyLog),
} }

View File

@ -20,7 +20,6 @@ import (
) )
type BiteEntity struct { type BiteEntity struct {
Id *big.Int
Ilk [32]byte Ilk [32]byte
Urn [32]byte Urn [32]byte
Ink *big.Int Ink *big.Int

View File

@ -15,14 +15,13 @@
package bite package bite
type BiteModel struct { type BiteModel struct {
Id string
Ilk string Ilk string
Urn string Urn string
Ink string Ink string
Art string Art string
IArt string IArt string
Tab string Tab string
Flip string NFlip string
TransactionIndex uint `db:"tx_idx"` TransactionIndex uint `db:"tx_idx"`
Raw string `db:"raw_log"` Raw string `db:"raw_log"`
} }

View File

@ -40,9 +40,9 @@ func (repository BiteRepository) Create(headerID int64, models []BiteModel) erro
} }
for _, model := range models { for _, model := range models {
_, err := tx.Exec( _, err := tx.Exec(
`INSERT into maker.bite (header_id, id, ilk, urn, ink, art, iart, tab, flip, tx_idx, raw_log) `INSERT into maker.bite (header_id, ilk, urn, ink, art, iart, tab, nflip, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`, VALUES($1, $2, $3, $4::NUMERIC, $5::NUMERIC, $6::NUMERIC, $7::NUMERIC, $8::NUMERIC, $9, $10)`,
headerID, model.Id, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.Flip, model.TransactionIndex, model.Raw, headerID, model.Ilk, model.Urn, model.Ink, model.Art, model.IArt, model.Tab, model.NFlip, model.TransactionIndex, model.Raw,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()

View File

@ -58,14 +58,15 @@ var _ = Describe("Bite repository", func() {
It("persists a bite record", func() { It("persists a bite record", func() {
var dbBite bite.BiteModel var dbBite bite.BiteModel
err = db.Get(&dbBite, `SELECT id, ilk, urn, ink, art, tab, flip, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbBite.Id).To(Equal(test_data.BiteModel.Id))
Expect(dbBite.Ilk).To(Equal(test_data.BiteModel.Ilk)) Expect(dbBite.Ilk).To(Equal(test_data.BiteModel.Ilk))
Expect(dbBite.Urn).To(Equal(test_data.BiteModel.Urn)) Expect(dbBite.Urn).To(Equal(test_data.BiteModel.Urn))
Expect(dbBite.Ink).To(Equal(test_data.BiteModel.Ink))
Expect(dbBite.Art).To(Equal(test_data.BiteModel.Art)) Expect(dbBite.Art).To(Equal(test_data.BiteModel.Art))
Expect(dbBite.Tab).To(Equal(test_data.BiteModel.Tab)) Expect(dbBite.Tab).To(Equal(test_data.BiteModel.Tab))
Expect(dbBite.Flip).To(Equal(test_data.BiteModel.Flip)) Expect(dbBite.NFlip).To(Equal(test_data.BiteModel.NFlip))
Expect(dbBite.IArt).To(Equal(test_data.BiteModel.IArt))
Expect(dbBite.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex)) Expect(dbBite.TransactionIndex).To(Equal(test_data.BiteModel.TransactionIndex))
Expect(dbBite.Raw).To(MatchJSON(test_data.BiteModel.Raw)) Expect(dbBite.Raw).To(MatchJSON(test_data.BiteModel.Raw))
}) })
@ -78,20 +79,7 @@ var _ = Describe("Bite repository", func() {
}) })
It("does not duplicate bite events", func() { It("does not duplicate bite events", func() {
var anotherBiteModel = bite.BiteModel{ err = biteRepository.Create(headerID, []bite.BiteModel{test_data.BiteModel})
Id: "11",
Ilk: test_data.BiteModel.Ilk,
Urn: test_data.BiteModel.Urn,
Ink: test_data.BiteModel.Ink,
Art: test_data.BiteModel.Art,
Tab: test_data.BiteModel.Tab,
Flip: test_data.BiteModel.Flip,
IArt: test_data.BiteModel.IArt,
TransactionIndex: test_data.BiteModel.TransactionIndex,
Raw: test_data.BiteModel.Raw,
}
err = biteRepository.Create(headerID, []bite.BiteModel{anotherBiteModel})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
@ -102,7 +90,7 @@ var _ = Describe("Bite repository", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbBite bite.BiteModel var dbBite bite.BiteModel
err = db.Get(&dbBite, `SELECT id, ilk, urn, ink, art, tab, flip, tx_idx, raw_log FROM maker.bite WHERE header_id = $1`, headerID) err = db.Get(&dbBite, `SELECT ilk, urn, ink, art, tab, nflip, iart, tx_idx, raw_log FROM maker.bite 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

@ -22,7 +22,6 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/bite" "github.com/vulcanize/vulcanizedb/pkg/transformers/bite"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"math/big" "math/big"
"strconv"
) )
var ( var (
@ -44,7 +43,6 @@ var (
biteLad = [32]byte{102, 97, 107, 101, 32, 108, 97, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} biteLad = [32]byte{102, 97, 107, 101, 32, 108, 97, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
biteIlkString = "0x66616b6520696c6B000000000000000000000000" biteIlkString = "0x66616b6520696c6B000000000000000000000000"
biteLadString = "0x66616B65206c6164000000000000000000000000" biteLadString = "0x66616B65206c6164000000000000000000000000"
biteId = int64(1)
) )
var EthBiteLog = types.Log{ var EthBiteLog = types.Log{
@ -64,7 +62,6 @@ var EthBiteLog = types.Log{
} }
var BiteEntity = bite.BiteEntity{ var BiteEntity = bite.BiteEntity{
Id: big.NewInt(biteId),
Ilk: biteIlk, Ilk: biteIlk,
Urn: biteLad, Urn: biteLad,
Ink: biteInk, Ink: biteInk,
@ -77,13 +74,12 @@ var BiteEntity = bite.BiteEntity{
} }
var BiteModel = bite.BiteModel{ var BiteModel = bite.BiteModel{
Id: strconv.FormatInt(biteId, 10),
Ilk: biteIlkString, Ilk: biteIlkString,
Urn: biteLadString, Urn: biteLadString,
Ink: biteInk.String(), Ink: biteInk.String(),
Art: biteArt.String(), Art: biteArt.String(),
Tab: biteTab.String(), Tab: biteTab.String(),
Flip: biteFlip.String(), NFlip: biteFlip.String(),
IArt: biteIArt.String(), IArt: biteIArt.String(),
TransactionIndex: EthBiteLog.TxIndex, TransactionIndex: EthBiteLog.TxIndex,
Raw: biteRawString, Raw: biteRawString,