Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Add Pit file debt ceiling event
Browse files Browse the repository at this point in the history
  • Loading branch information
rmulhol committed Sep 5, 2018
1 parent eb2b845 commit d080038
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 6 deletions.
3 changes: 2 additions & 1 deletion db/migrations/1535667935_create_pit_file_table.down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DROP TABLE maker.pit_file_ilk;
DROP TABLE maker.pit_file_stability_fee;
DROP TABLE maker.pit_file_stability_fee;
DROP TABLE maker.pit_file_debt_ceiling;
12 changes: 11 additions & 1 deletion db/migrations/1535667935_create_pit_file_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ CREATE TABLE maker.pit_file_stability_fee (
tx_idx INTEGER NOT NULL,
raw_log JSONB,
UNIQUE (header_id, tx_idx)
)
);

CREATE TABLE maker.pit_file_debt_ceiling (
id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
what TEXT,
data NUMERIC,
tx_idx INTEGER NOT NULL,
raw_log JSONB,
UNIQUE (header_id, tx_idx)
);
65 changes: 65 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,40 @@ CREATE SEQUENCE maker.frob_id_seq
ALTER SEQUENCE maker.frob_id_seq OWNED BY maker.frob.id;


--
-- Name: pit_file_debt_ceiling; Type: TABLE; Schema: maker; Owner: -
--

CREATE TABLE maker.pit_file_debt_ceiling (
id integer NOT NULL,
header_id integer NOT NULL,
what text,
data numeric,
tx_idx integer NOT NULL,
raw_log jsonb
);


--
-- Name: pit_file_debt_ceiling_id_seq; Type: SEQUENCE; Schema: maker; Owner: -
--

CREATE SEQUENCE maker.pit_file_debt_ceiling_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: pit_file_debt_ceiling_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: -
--

ALTER SEQUENCE maker.pit_file_debt_ceiling_id_seq OWNED BY maker.pit_file_debt_ceiling.id;


--
-- Name: pit_file_ilk; Type: TABLE; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -689,6 +723,13 @@ ALTER TABLE ONLY maker.flip_kick ALTER COLUMN db_id SET DEFAULT nextval('maker.f
ALTER TABLE ONLY maker.frob ALTER COLUMN id SET DEFAULT nextval('maker.frob_id_seq'::regclass);


--
-- Name: pit_file_debt_ceiling id; Type: DEFAULT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.pit_file_debt_ceiling ALTER COLUMN id SET DEFAULT nextval('maker.pit_file_debt_ceiling_id_seq'::regclass);


--
-- Name: pit_file_ilk id; Type: DEFAULT; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -828,6 +869,22 @@ ALTER TABLE ONLY maker.frob
ADD CONSTRAINT frob_pkey PRIMARY KEY (id);


--
-- Name: pit_file_debt_ceiling pit_file_debt_ceiling_header_id_tx_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);


--
-- Name: pit_file_debt_ceiling pit_file_debt_ceiling_pkey; Type: CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.pit_file_debt_ceiling
ADD CONSTRAINT pit_file_debt_ceiling_pkey PRIMARY KEY (id);


--
-- Name: pit_file_ilk pit_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -1055,6 +1112,14 @@ ALTER TABLE ONLY maker.price_feeds
ADD CONSTRAINT headers_fk FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;


--
-- Name: pit_file_debt_ceiling pit_file_debt_ceiling_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.pit_file_debt_ceiling
ADD CONSTRAINT pit_file_debt_ceiling_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;


--
-- Name: pit_file_ilk pit_file_ilk_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--
Expand Down
29 changes: 29 additions & 0 deletions pkg/transformers/pit_file/debt_ceiling/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package debt_ceiling

import (
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
)

type Converter interface {
ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileDebtCeilingModel, error)
}

type PitFileDebtCeilingConverter struct{}

func (PitFileDebtCeilingConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileDebtCeilingModel, error) {
what := common.HexToAddress(ethLog.Topics[1].String()).String()
itemByteLength := 32
riskBytes := ethLog.Data[len(ethLog.Data)-itemByteLength:]
data := big.NewInt(0).SetBytes(riskBytes).String()

raw, err := json.Marshal(ethLog)
return PitFileDebtCeilingModel{
What: what,
Data: data,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}, err
}
25 changes: 25 additions & 0 deletions pkg/transformers/pit_file/debt_ceiling/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package debt_ceiling_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)

var _ = Describe("", func() {
It("converts a log to an model", func() {
converter := debt_ceiling.PitFileDebtCeilingConverter{}

model, err := converter.ToModel(test_data.PitAddress, shared.PitABI, test_data.EthPitFileDebtCeilingLog)

Expect(err).NotTo(HaveOccurred())
Expect(model.What).To(Equal(test_data.PitFileDebtCeilingModel.What))
Expect(model.Data).To(Equal(test_data.PitFileDebtCeilingModel.Data))
Expect(model.TransactionIndex).To(Equal(test_data.PitFileDebtCeilingModel.TransactionIndex))
Expect(model.Raw).To(Equal(test_data.PitFileDebtCeilingModel.Raw))
Expect(model).To(Equal(test_data.PitFileDebtCeilingModel))
})
})
8 changes: 8 additions & 0 deletions pkg/transformers/pit_file/debt_ceiling/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package debt_ceiling

type PitFileDebtCeilingModel struct {
What string
Data string
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}
48 changes: 48 additions & 0 deletions pkg/transformers/pit_file/debt_ceiling/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package debt_ceiling

import (
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)

type Repository interface {
Create(headerID int64, model PitFileDebtCeilingModel) error
MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error)
}

type PitFileDebtCeilingRepository struct {
db *postgres.DB
}

func NewPitFileDebtCeilingRepository(db *postgres.DB) PitFileDebtCeilingRepository {
return PitFileDebtCeilingRepository{
db: db,
}
}

func (repository PitFileDebtCeilingRepository) Create(headerID int64, model PitFileDebtCeilingModel) error {
_, err := repository.db.Exec(
`INSERT into maker.pit_file_debt_ceiling (header_id, what, data, tx_idx, raw_log)
VALUES($1, $2, $3::NUMERIC, $4, $5)`,
headerID, model.What, model.Data, model.TransactionIndex, model.Raw,
)
return err
}

func (repository PitFileDebtCeilingRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN maker.pit_file_debt_ceiling on headers.id = header_id
WHERE header_id ISNULL
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)

return result, err
}
130 changes: 130 additions & 0 deletions pkg/transformers/pit_file/debt_ceiling/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package debt_ceiling_test

import (
"database/sql"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config"
)

var _ = Describe("", func() {
Describe("Create", func() {
It("adds a pit file debt ceiling event", func() {
db := test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{})
Expect(err).NotTo(HaveOccurred())
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)

err = pitFileRepository.Create(headerID, 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)
Expect(err).NotTo(HaveOccurred())
Expect(dbPitFile.What).To(Equal(test_data.PitFileDebtCeilingModel.What))
Expect(dbPitFile.Data).To(Equal(test_data.PitFileDebtCeilingModel.Data))
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileDebtCeilingModel.TransactionIndex))
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileDebtCeilingModel.Raw))
})

It("does not duplicate pit file events", func() {
db := test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{})
Expect(err).NotTo(HaveOccurred())
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)
err = pitFileRepository.Create(headerID, test_data.PitFileDebtCeilingModel)
Expect(err).NotTo(HaveOccurred())

err = pitFileRepository.Create(headerID, test_data.PitFileDebtCeilingModel)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})

It("removes pit file if corresponding header is deleted", func() {
db := test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{})
Expect(err).NotTo(HaveOccurred())
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)
err = pitFileRepository.Create(headerID, 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)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
})

Describe("MissingHeaders", func() {
It("returns headers with no associated pit file event", func() {
db := test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository := repositories.NewHeaderRepository(db)
startingBlockNumber := int64(1)
pitFileBlockNumber := int64(2)
endingBlockNumber := int64(3)
blockNumbers := []int64{startingBlockNumber, pitFileBlockNumber, endingBlockNumber, endingBlockNumber + 1}
var headerIDs []int64
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n})
headerIDs = append(headerIDs, headerID)
Expect(err).NotTo(HaveOccurred())
}
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)
err := pitFileRepository.Create(headerIDs[1], test_data.PitFileDebtCeilingModel)
Expect(err).NotTo(HaveOccurred())

headers, err := pitFileRepository.MissingHeaders(startingBlockNumber, endingBlockNumber)

Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber)))
})

It("only returns headers associated with the current node", func() {
db := test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
blockNumbers := []int64{1, 2, 3}
headerRepository := repositories.NewHeaderRepository(db)
dbTwo := test_config.NewTestDB(core.Node{ID: "second"})
headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo)
var headerIDs []int64
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n})
Expect(err).NotTo(HaveOccurred())
headerIDs = append(headerIDs, headerID)
_, err = headerRepositoryTwo.CreateOrUpdateHeader(core.Header{BlockNumber: n})
Expect(err).NotTo(HaveOccurred())
}
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)
pitFileRepositoryTwo := debt_ceiling.NewPitFileDebtCeilingRepository(dbTwo)
err := pitFileRepository.Create(headerIDs[0], test_data.PitFileDebtCeilingModel)
Expect(err).NotTo(HaveOccurred())

nodeOneMissingHeaders, err := pitFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))

nodeTwoMissingHeaders, err := pitFileRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
})
})
Loading

0 comments on commit d080038

Please sign in to comment.