diff --git a/db/migrations/1535667935_create_pit_file_table.down.sql b/db/migrations/1535667935_create_pit_file_tables.down.sql similarity index 100% rename from db/migrations/1535667935_create_pit_file_table.down.sql rename to db/migrations/1535667935_create_pit_file_tables.down.sql diff --git a/db/migrations/1535667935_create_pit_file_table.up.sql b/db/migrations/1535667935_create_pit_file_tables.up.sql similarity index 100% rename from db/migrations/1535667935_create_pit_file_table.up.sql rename to db/migrations/1535667935_create_pit_file_tables.up.sql diff --git a/db/migrations/1536603967_create_drip_file_tables.down.sql b/db/migrations/1536603967_create_drip_file_tables.down.sql new file mode 100644 index 00000000..6b2c745b --- /dev/null +++ b/db/migrations/1536603967_create_drip_file_tables.down.sql @@ -0,0 +1,3 @@ +DROP TABLE maker.drip_file_ilk; +DROP TABLE maker.drip_file_repo; +DROP TABLE maker.drip_file_vow; \ No newline at end of file diff --git a/db/migrations/1536603967_create_drip_file_tables.up.sql b/db/migrations/1536603967_create_drip_file_tables.up.sql new file mode 100644 index 00000000..c51efed4 --- /dev/null +++ b/db/migrations/1536603967_create_drip_file_tables.up.sql @@ -0,0 +1,30 @@ +CREATE TABLE maker.drip_file_ilk ( + id SERIAL PRIMARY KEY, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + ilk TEXT, + vow TEXT, + tax NUMERIC, + tx_idx INTEGER NOT NUll, + raw_log JSONB, + UNIQUE (header_id, tx_idx) +); + +CREATE TABLE maker.drip_file_repo ( + 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) +); + +CREATE TABLE maker.drip_file_vow ( + id SERIAL PRIMARY KEY, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + what TEXT, + data TEXT, + tx_idx INTEGER NOT NULL, + raw_log JSONB, + UNIQUE (header_id, tx_idx) +); \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index b64f43f1..1148ff3c 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -116,6 +116,109 @@ CREATE SEQUENCE maker.dent_db_id_seq ALTER SEQUENCE maker.dent_db_id_seq OWNED BY maker.dent.db_id; +-- +-- Name: drip_file_ilk; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.drip_file_ilk ( + id integer NOT NULL, + header_id integer NOT NULL, + ilk text, + vow text, + tax numeric, + tx_idx integer NOT NULL, + raw_log jsonb +); + + +-- +-- Name: drip_file_ilk_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.drip_file_ilk_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: drip_file_ilk_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.drip_file_ilk_id_seq OWNED BY maker.drip_file_ilk.id; + + +-- +-- Name: drip_file_repo; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.drip_file_repo ( + id integer NOT NULL, + header_id integer NOT NULL, + what text, + data numeric, + tx_idx integer NOT NULL, + raw_log jsonb +); + + +-- +-- Name: drip_file_repo_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.drip_file_repo_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: drip_file_repo_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.drip_file_repo_id_seq OWNED BY maker.drip_file_repo.id; + + +-- +-- Name: drip_file_vow; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.drip_file_vow ( + id integer NOT NULL, + header_id integer NOT NULL, + what text, + data text, + tx_idx integer NOT NULL, + raw_log jsonb +); + + +-- +-- Name: drip_file_vow_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.drip_file_vow_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: drip_file_vow_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.drip_file_vow_id_seq OWNED BY maker.drip_file_vow.id; + + -- -- Name: flip_kick; Type: TABLE; Schema: maker; Owner: - -- @@ -786,6 +889,27 @@ ALTER TABLE ONLY maker.bite ALTER COLUMN id SET DEFAULT nextval('maker.bite_id_s ALTER TABLE ONLY maker.dent ALTER COLUMN db_id SET DEFAULT nextval('maker.dent_db_id_seq'::regclass); +-- +-- Name: drip_file_ilk id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_ilk ALTER COLUMN id SET DEFAULT nextval('maker.drip_file_ilk_id_seq'::regclass); + + +-- +-- Name: drip_file_repo id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_repo ALTER COLUMN id SET DEFAULT nextval('maker.drip_file_repo_id_seq'::regclass); + + +-- +-- Name: drip_file_vow id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_vow ALTER COLUMN id SET DEFAULT nextval('maker.drip_file_vow_id_seq'::regclass); + + -- -- Name: flip_kick db_id; Type: DEFAULT; Schema: maker; Owner: - -- @@ -937,6 +1061,54 @@ ALTER TABLE ONLY maker.dent ADD CONSTRAINT dent_pkey PRIMARY KEY (db_id); +-- +-- Name: drip_file_ilk drip_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_ilk + ADD CONSTRAINT drip_file_ilk_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + + +-- +-- Name: drip_file_ilk drip_file_ilk_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_ilk + ADD CONSTRAINT drip_file_ilk_pkey PRIMARY KEY (id); + + +-- +-- Name: drip_file_repo drip_file_repo_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_repo + ADD CONSTRAINT drip_file_repo_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + + +-- +-- Name: drip_file_repo drip_file_repo_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_repo + ADD CONSTRAINT drip_file_repo_pkey PRIMARY KEY (id); + + +-- +-- Name: drip_file_vow drip_file_vow_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_vow + ADD CONSTRAINT drip_file_vow_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + + +-- +-- Name: drip_file_vow drip_file_vow_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_vow + ADD CONSTRAINT drip_file_vow_pkey PRIMARY KEY (id); + + -- -- Name: flip_kick flip_kick_id_key; Type: CONSTRAINT; Schema: maker; Owner: - -- @@ -1212,6 +1384,30 @@ ALTER TABLE ONLY maker.dent ADD CONSTRAINT dent_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; +-- +-- Name: drip_file_ilk drip_file_ilk_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_ilk + ADD CONSTRAINT drip_file_ilk_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; + + +-- +-- Name: drip_file_repo drip_file_repo_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_repo + ADD CONSTRAINT drip_file_repo_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; + + +-- +-- Name: drip_file_vow drip_file_vow_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.drip_file_vow + ADD CONSTRAINT drip_file_vow_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; + + -- -- Name: flip_kick flip_kick_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - -- diff --git a/pkg/transformers/drip_file/config.go b/pkg/transformers/drip_file/config.go new file mode 100644 index 00000000..299ff25d --- /dev/null +++ b/pkg/transformers/drip_file/config.go @@ -0,0 +1,25 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package drip_file + +import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + +var DripFileConfig = shared.TransformerConfig{ + ContractAddress: shared.DripContractAddress, + ContractAbi: shared.DripABI, + Topics: []string{shared.DripFileIlkSignature, shared.DripFileRepoSignature}, + StartingBlockNumber: 0, + EndingBlockNumber: 100, +} diff --git a/pkg/transformers/drip_file/ilk/converter.go b/pkg/transformers/drip_file/ilk/converter.go new file mode 100644 index 00000000..7b03f958 --- /dev/null +++ b/pkg/transformers/drip_file/ilk/converter.go @@ -0,0 +1,60 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +import ( + "bytes" + "encoding/json" + "math/big" + + "errors" + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +type Converter interface { + ToModel(ethLog types.Log) (DripFileIlkModel, error) +} + +type DripFileIlkConverter struct{} + +func (DripFileIlkConverter) ToModel(ethLog types.Log) (DripFileIlkModel, error) { + err := verifyLog(ethLog) + if err != nil { + return DripFileIlkModel{}, err + } + ilk := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) + vow := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00")) + taxBytes := ethLog.Data[len(ethLog.Data)-shared.DataItemLength:] + tax := big.NewInt(0).SetBytes(taxBytes).String() + raw, err := json.Marshal(ethLog) + return DripFileIlkModel{ + Ilk: ilk, + Vow: vow, + Tax: tax, + TransactionIndex: ethLog.TxIndex, + Raw: raw, + }, err +} + +func verifyLog(log types.Log) error { + if len(log.Topics) < 4 { + return errors.New("log missing topics") + } + if len(log.Data) < shared.DataItemLength { + return errors.New("log missing data") + } + return nil +} diff --git a/pkg/transformers/drip_file/ilk/converter_test.go b/pkg/transformers/drip_file/ilk/converter_test.go new file mode 100644 index 00000000..fbbeb574 --- /dev/null +++ b/pkg/transformers/drip_file/ilk/converter_test.go @@ -0,0 +1,60 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +var _ = Describe("Drip file ilk converter", func() { + It("returns err if log missing topics", func() { + converter := ilk.DripFileIlkConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}}, + Data: []byte{1, 1, 1, 1, 1}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("returns err if log missing data", func() { + converter := ilk.DripFileIlkConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}, {}, {}, {}}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + + }) + + It("converts a log to a model", func() { + converter := ilk.DripFileIlkConverter{} + + model, err := converter.ToModel(test_data.EthDripFileIlkLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.DripFileIlkModel)) + }) +}) diff --git a/pkg/transformers/drip_file/ilk/ilk_suite_test.go b/pkg/transformers/drip_file/ilk/ilk_suite_test.go new file mode 100644 index 00000000..0cd56e44 --- /dev/null +++ b/pkg/transformers/drip_file/ilk/ilk_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestIlk(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Ilk Suite") +} diff --git a/pkg/transformers/drip_file/ilk/model.go b/pkg/transformers/drip_file/ilk/model.go new file mode 100644 index 00000000..7573692f --- /dev/null +++ b/pkg/transformers/drip_file/ilk/model.go @@ -0,0 +1,23 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +type DripFileIlkModel struct { + Ilk string + Vow string + Tax string + TransactionIndex uint `db:"tx_idx"` + Raw []byte `db:"raw_log"` +} diff --git a/pkg/transformers/drip_file/ilk/repository.go b/pkg/transformers/drip_file/ilk/repository.go new file mode 100644 index 00000000..a0b3581d --- /dev/null +++ b/pkg/transformers/drip_file/ilk/repository.go @@ -0,0 +1,59 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +) + +type Repository interface { + Create(headerID int64, model DripFileIlkModel) error + MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) +} + +type DripFileIlkRepository struct { + db *postgres.DB +} + +func NewDripFileIlkRepository(db *postgres.DB) DripFileIlkRepository { + return DripFileIlkRepository{db: db} +} + +func (repository DripFileIlkRepository) Create(headerID int64, model DripFileIlkModel) error { + _, err := repository.db.Exec( + `INSERT into maker.drip_file_ilk (header_id, ilk, vow, tax, tx_idx, raw_log) + VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, + headerID, model.Ilk, model.Vow, model.Tax, model.TransactionIndex, model.Raw, + ) + return err +} + +func (repository DripFileIlkRepository) 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.drip_file_ilk 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 +} diff --git a/pkg/transformers/drip_file/ilk/repository_test.go b/pkg/transformers/drip_file/ilk/repository_test.go new file mode 100644 index 00000000..595572e6 --- /dev/null +++ b/pkg/transformers/drip_file/ilk/repository_test.go @@ -0,0 +1,145 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk_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/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/test_config" +) + +var _ = Describe("Drip file ilk repository", func() { + Describe("Create", func() { + It("adds a drip file ilk 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()) + dripFileIlkRepository := ilk.NewDripFileIlkRepository(db) + + err = dripFileIlkRepository.Create(headerID, test_data.DripFileIlkModel) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileIlk ilk.DripFileIlkModel + err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) + Expect(err).NotTo(HaveOccurred()) + Expect(dbDripFileIlk.Ilk).To(Equal(test_data.DripFileIlkModel.Ilk)) + Expect(dbDripFileIlk.Vow).To(Equal(test_data.DripFileIlkModel.Vow)) + Expect(dbDripFileIlk.Tax).To(Equal(test_data.DripFileIlkModel.Tax)) + Expect(dbDripFileIlk.TransactionIndex).To(Equal(test_data.DripFileIlkModel.TransactionIndex)) + Expect(dbDripFileIlk.Raw).To(MatchJSON(test_data.DripFileIlkModel.Raw)) + }) + + It("does not duplicate drip 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()) + dripFileIlkRepository := ilk.NewDripFileIlkRepository(db) + err = dripFileIlkRepository.Create(headerID, test_data.DripFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + err = dripFileIlkRepository.Create(headerID, test_data.DripFileIlkModel) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) + }) + + It("removes drip 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()) + dripFileIlkRepository := ilk.NewDripFileIlkRepository(db) + err = dripFileIlkRepository.Create(headerID, test_data.DripFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileIlk ilk.DripFileIlkModel + err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(sql.ErrNoRows)) + }) + }) + + Describe("MissingHeaders", func() { + It("returns headers with no associated drip file event", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + dripFileIlkBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, dripFileIlkBlockNumber, 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()) + } + dripFileIlkRepository := ilk.NewDripFileIlkRepository(db) + err := dripFileIlkRepository.Create(headerIDs[1], test_data.DripFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + headers, err := dripFileIlkRepository.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()) + } + dripFileIlkRepository := ilk.NewDripFileIlkRepository(db) + dripFileIlkRepositoryTwo := ilk.NewDripFileIlkRepository(dbTwo) + err := dripFileIlkRepository.Create(headerIDs[0], test_data.DripFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + nodeOneMissingHeaders, err := dripFileIlkRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) + + nodeTwoMissingHeaders, err := dripFileIlkRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) + }) + }) +}) diff --git a/pkg/transformers/drip_file/ilk/transformer.go b/pkg/transformers/drip_file/ilk/transformer.go new file mode 100644 index 00000000..e8b7219f --- /dev/null +++ b/pkg/transformers/drip_file/ilk/transformer.go @@ -0,0 +1,71 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +type DripFileIlkTransformer struct { + Config shared.TransformerConfig + Converter Converter + Fetcher shared.LogFetcher + Repository Repository +} + +type DripFileIlkTransformerInitializer struct { + Config shared.TransformerConfig +} + +func (initializer DripFileIlkTransformerInitializer) NewDripFileIlkTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { + converter := DripFileIlkConverter{} + fetcher := shared.NewFetcher(blockChain) + repository := NewDripFileIlkRepository(db) + return DripFileIlkTransformer{ + Config: initializer.Config, + Converter: converter, + Fetcher: fetcher, + Repository: repository, + } +} + +func (transformer DripFileIlkTransformer) Execute() error { + missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) + if err != nil { + return err + } + for _, header := range missingHeaders { + topics := [][]common.Hash{{common.HexToHash(shared.DripFileIlkSignature)}} + matchingLogs, err := transformer.Fetcher.FetchLogs(drip_file.DripFileConfig.ContractAddress, topics, header.BlockNumber) + if err != nil { + return err + } + for _, log := range matchingLogs { + model, err := transformer.Converter.ToModel(log) + if err != nil { + return err + } + err = transformer.Repository.Create(header.Id, model) + if err != nil { + return err + } + } + } + return nil +} diff --git a/pkg/transformers/drip_file/ilk/transformer_test.go b/pkg/transformers/drip_file/ilk/transformer_test.go new file mode 100644 index 00000000..a3e12b55 --- /dev/null +++ b/pkg/transformers/drip_file/ilk/transformer_test.go @@ -0,0 +1,174 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk_test + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" + ilk_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/drip_file/ilk" +) + +var _ = Describe("Drip file ilk transformer", func() { + It("gets missing headers for block numbers specified in config", func() { + repository := &ilk_mocks.MockDripFileIlkRepository{} + transformer := ilk.DripFileIlkTransformer{ + Config: drip_file.DripFileConfig, + Fetcher: &mocks.MockLogFetcher{}, + Converter: &ilk_mocks.MockDripFileIlkConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedStartingBlockNumber).To(Equal(drip_file.DripFileConfig.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(drip_file.DripFileConfig.EndingBlockNumber)) + }) + + It("returns error if repository returns error for missing headers", func() { + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeadersErr(fakes.FakeError) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: &mocks.MockLogFetcher{}, + Converter: &ilk_mocks.MockDripFileIlkConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("fetches logs for missing headers", func() { + fetcher := &mocks.MockLogFetcher{} + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: &ilk_mocks.MockDripFileIlkConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) + Expect(fetcher.FetchedContractAddress).To(Equal(drip_file.DripFileConfig.ContractAddress)) + Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.DripFileIlkSignature)}})) + }) + + It("returns error if fetcher returns error", func() { + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetcherError(fakes.FakeError) + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: &ilk_mocks.MockDripFileIlkConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("converts matching logs", func() { + converter := &ilk_mocks.MockDripFileIlkConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileIlkLog}) + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(converter.PassedLog).To(Equal(test_data.EthDripFileIlkLog)) + }) + + It("returns error if converter returns error", func() { + converter := &ilk_mocks.MockDripFileIlkConverter{} + converter.SetConverterError(fakes.FakeError) + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileIlkLog}) + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("persists drip file model", func() { + converter := &ilk_mocks.MockDripFileIlkConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileIlkLog}) + repository := &ilk_mocks.MockDripFileIlkRepository{} + fakeHeader := core.Header{BlockNumber: 1, Id: 2} + repository.SetMissingHeaders([]core.Header{fakeHeader}) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) + Expect(repository.PassedModel).To(Equal(test_data.DripFileIlkModel)) + }) + + It("returns error if repository returns error for create", func() { + converter := &ilk_mocks.MockDripFileIlkConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileIlkLog}) + repository := &ilk_mocks.MockDripFileIlkRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetCreateError(fakes.FakeError) + transformer := ilk.DripFileIlkTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) +}) diff --git a/pkg/transformers/drip_file/repo/converter.go b/pkg/transformers/drip_file/repo/converter.go new file mode 100644 index 00000000..d525b246 --- /dev/null +++ b/pkg/transformers/drip_file/repo/converter.go @@ -0,0 +1,52 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "bytes" + "encoding/json" + "errors" + "github.com/ethereum/go-ethereum/core/types" + "math/big" +) + +type Converter interface { + ToModel(ethLog types.Log) (DripFileRepoModel, error) +} + +type DripFileRepoConverter struct{} + +func (DripFileRepoConverter) ToModel(ethLog types.Log) (DripFileRepoModel, error) { + err := verifyLog(ethLog) + if err != nil { + return DripFileRepoModel{}, err + } + what := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) + data := big.NewInt(0).SetBytes(ethLog.Topics[3].Bytes()).String() + raw, err := json.Marshal(ethLog) + return DripFileRepoModel{ + What: what, + Data: data, + TransactionIndex: ethLog.TxIndex, + Raw: raw, + }, err +} + +func verifyLog(log types.Log) error { + if len(log.Topics) < 4 { + return errors.New("log missing topics") + } + return nil +} diff --git a/pkg/transformers/drip_file/repo/converter_test.go b/pkg/transformers/drip_file/repo/converter_test.go new file mode 100644 index 00000000..eda236d6 --- /dev/null +++ b/pkg/transformers/drip_file/repo/converter_test.go @@ -0,0 +1,48 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +var _ = Describe("Drip file repo converter", func() { + It("returns err if log missing topics", func() { + converter := repo.DripFileRepoConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}}, + Data: []byte{1, 1, 1, 1, 1}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("converts a log to a model", func() { + converter := repo.DripFileRepoConverter{} + + model, err := converter.ToModel(test_data.EthDripFileRepoLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.DripFileRepoModel)) + }) +}) diff --git a/pkg/transformers/drip_file/repo/model.go b/pkg/transformers/drip_file/repo/model.go new file mode 100644 index 00000000..34260055 --- /dev/null +++ b/pkg/transformers/drip_file/repo/model.go @@ -0,0 +1,22 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +type DripFileRepoModel struct { + What string + Data string + TransactionIndex uint `db:"tx_idx"` + Raw []byte `db:"raw_log"` +} diff --git a/pkg/transformers/drip_file/repo/repo_suite_test.go b/pkg/transformers/drip_file/repo/repo_suite_test.go new file mode 100644 index 00000000..ab12c4e0 --- /dev/null +++ b/pkg/transformers/drip_file/repo/repo_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestRepo(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Repo Suite") +} diff --git a/pkg/transformers/drip_file/repo/repository.go b/pkg/transformers/drip_file/repo/repository.go new file mode 100644 index 00000000..1ab629ad --- /dev/null +++ b/pkg/transformers/drip_file/repo/repository.go @@ -0,0 +1,59 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +) + +type Repository interface { + Create(headerID int64, model DripFileRepoModel) error + MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) +} + +type DripFileRepoRepository struct { + db *postgres.DB +} + +func NewDripFileRepoRepository(db *postgres.DB) DripFileRepoRepository { + return DripFileRepoRepository{db: db} +} + +func (repository DripFileRepoRepository) Create(headerID int64, model DripFileRepoModel) error { + _, err := repository.db.Exec( + `INSERT into maker.drip_file_repo (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 DripFileRepoRepository) 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.drip_file_repo 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 +} diff --git a/pkg/transformers/drip_file/repo/repository_test.go b/pkg/transformers/drip_file/repo/repository_test.go new file mode 100644 index 00000000..138d918a --- /dev/null +++ b/pkg/transformers/drip_file/repo/repository_test.go @@ -0,0 +1,142 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo_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/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/test_config" +) + +var _ = Describe("", func() { + Describe("Create", func() { + It("adds a drip file repo 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()) + dripFileRepoRepository := repo.NewDripFileRepoRepository(db) + + err = dripFileRepoRepository.Create(headerID, test_data.DripFileRepoModel) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileRepo repo.DripFileRepoModel + err = db.Get(&dbDripFileRepo, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) + Expect(err).NotTo(HaveOccurred()) + Expect(dbDripFileRepo.What).To(Equal(test_data.DripFileRepoModel.What)) + Expect(dbDripFileRepo.Data).To(Equal(test_data.DripFileRepoModel.Data)) + Expect(dbDripFileRepo.TransactionIndex).To(Equal(test_data.DripFileRepoModel.TransactionIndex)) + Expect(dbDripFileRepo.Raw).To(MatchJSON(test_data.DripFileRepoModel.Raw)) + }) + + It("does not duplicate drip 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()) + dripFileRepoRepository := repo.NewDripFileRepoRepository(db) + err = dripFileRepoRepository.Create(headerID, test_data.DripFileRepoModel) + Expect(err).NotTo(HaveOccurred()) + + err = dripFileRepoRepository.Create(headerID, test_data.DripFileRepoModel) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) + }) + + It("removes drip 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()) + dripFileRepoRepository := repo.NewDripFileRepoRepository(db) + err = dripFileRepoRepository.Create(headerID, test_data.DripFileRepoModel) + Expect(err).NotTo(HaveOccurred()) + + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileRepo repo.DripFileRepoModel + err = db.Get(&dbDripFileRepo, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(sql.ErrNoRows)) + }) + }) + + Describe("MissingHeaders", func() { + It("returns headers with no associated drip file event", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + dripFileRepoBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, dripFileRepoBlockNumber, 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()) + } + dripFileRepoRepository := repo.NewDripFileRepoRepository(db) + err := dripFileRepoRepository.Create(headerIDs[1], test_data.DripFileRepoModel) + Expect(err).NotTo(HaveOccurred()) + + headers, err := dripFileRepoRepository.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()) + } + dripFileRepoRepository := repo.NewDripFileRepoRepository(db) + dripFileRepoRepositoryTwo := repo.NewDripFileRepoRepository(dbTwo) + err := dripFileRepoRepository.Create(headerIDs[0], test_data.DripFileRepoModel) + Expect(err).NotTo(HaveOccurred()) + + nodeOneMissingHeaders, err := dripFileRepoRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) + + nodeTwoMissingHeaders, err := dripFileRepoRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) + }) + }) +}) diff --git a/pkg/transformers/drip_file/repo/transformer.go b/pkg/transformers/drip_file/repo/transformer.go new file mode 100644 index 00000000..aa74c670 --- /dev/null +++ b/pkg/transformers/drip_file/repo/transformer.go @@ -0,0 +1,71 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +type DripFileRepoTransformer struct { + Config shared.TransformerConfig + Converter Converter + Fetcher shared.LogFetcher + Repository Repository +} + +type DripFileRepoTransformerInitializer struct { + Config shared.TransformerConfig +} + +func (initializer DripFileRepoTransformerInitializer) NewDripFileRepoTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { + converter := DripFileRepoConverter{} + fetcher := shared.NewFetcher(blockChain) + repository := NewDripFileRepoRepository(db) + return DripFileRepoTransformer{ + Config: initializer.Config, + Converter: converter, + Fetcher: fetcher, + Repository: repository, + } +} + +func (transformer DripFileRepoTransformer) Execute() error { + missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) + if err != nil { + return err + } + for _, header := range missingHeaders { + topics := [][]common.Hash{{common.HexToHash(shared.DripFileRepoSignature)}} + matchingLogs, err := transformer.Fetcher.FetchLogs(drip_file.DripFileConfig.ContractAddress, topics, header.BlockNumber) + if err != nil { + return err + } + for _, log := range matchingLogs { + model, err := transformer.Converter.ToModel(log) + if err != nil { + return err + } + err = transformer.Repository.Create(header.Id, model) + if err != nil { + return err + } + } + } + return nil +} diff --git a/pkg/transformers/drip_file/repo/transformer_test.go b/pkg/transformers/drip_file/repo/transformer_test.go new file mode 100644 index 00000000..cc95feef --- /dev/null +++ b/pkg/transformers/drip_file/repo/transformer_test.go @@ -0,0 +1,175 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo_test + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" + repo_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/drip_file/repo" +) + +var _ = Describe("Drip file repo transformer", func() { + It("gets missing headers for block numbers specified in config", func() { + repository := &repo_mocks.MockDripFileRepoRepository{} + transformer := repo.DripFileRepoTransformer{ + Config: drip_file.DripFileConfig, + Fetcher: &mocks.MockLogFetcher{}, + Converter: &repo_mocks.MockDripFileRepoConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedStartingBlockNumber).To(Equal(drip_file.DripFileConfig.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(drip_file.DripFileConfig.EndingBlockNumber)) + }) + + It("returns error if repository returns error for missing headers", func() { + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeadersErr(fakes.FakeError) + transformer := repo.DripFileRepoTransformer{ + Fetcher: &mocks.MockLogFetcher{}, + Converter: &repo_mocks.MockDripFileRepoConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("fetches logs for missing headers", func() { + fetcher := &mocks.MockLogFetcher{} + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: &repo_mocks.MockDripFileRepoConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) + Expect(fetcher.FetchedContractAddress).To(Equal(drip_file.DripFileConfig.ContractAddress)) + Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.DripFileRepoSignature)}})) + }) + + It("returns error if fetcher returns error", func() { + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetcherError(fakes.FakeError) + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: &repo_mocks.MockDripFileRepoConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("converts matching logs", func() { + converter := &repo_mocks.MockDripFileRepoConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileRepoLog}) + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(converter.PassedLog).To(Equal(test_data.EthDripFileRepoLog)) + }) + + It("returns error if converter returns error", func() { + converter := &repo_mocks.MockDripFileRepoConverter{} + converter.SetConverterError(fakes.FakeError) + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileRepoLog}) + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("persists drip file model", func() { + converter := &repo_mocks.MockDripFileRepoConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileRepoLog}) + repository := &repo_mocks.MockDripFileRepoRepository{} + fakeHeader := core.Header{BlockNumber: 1, Id: 2} + repository.SetMissingHeaders([]core.Header{fakeHeader}) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) + Expect(repository.PassedModel).To(Equal(test_data.DripFileRepoModel)) + }) + + It("returns error if repository returns error for create", func() { + converter := &repo_mocks.MockDripFileRepoConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileRepoLog}) + repository := &repo_mocks.MockDripFileRepoRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetCreateError(fakes.FakeError) + transformer := repo.DripFileRepoTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) +}) diff --git a/pkg/transformers/drip_file/vow/converter.go b/pkg/transformers/drip_file/vow/converter.go new file mode 100644 index 00000000..93d1200b --- /dev/null +++ b/pkg/transformers/drip_file/vow/converter.go @@ -0,0 +1,51 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +import ( + "bytes" + "encoding/json" + "errors" + "github.com/ethereum/go-ethereum/core/types" +) + +type Converter interface { + ToModel(ethLog types.Log) (DripFileVowModel, error) +} + +type DripFileVowConverter struct{} + +func (DripFileVowConverter) ToModel(ethLog types.Log) (DripFileVowModel, error) { + err := verifyLog(ethLog) + if err != nil { + return DripFileVowModel{}, err + } + what := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) + data := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00")) + raw, err := json.Marshal(ethLog) + return DripFileVowModel{ + What: what, + Data: data, + TransactionIndex: ethLog.TxIndex, + Raw: raw, + }, err +} + +func verifyLog(log types.Log) error { + if len(log.Topics) < 4 { + return errors.New("log missing topics") + } + return nil +} diff --git a/pkg/transformers/drip_file/vow/converter_test.go b/pkg/transformers/drip_file/vow/converter_test.go new file mode 100644 index 00000000..6c86ff82 --- /dev/null +++ b/pkg/transformers/drip_file/vow/converter_test.go @@ -0,0 +1,48 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +var _ = Describe("Drip file repo converter", func() { + It("returns err if log missing topics", func() { + converter := vow.DripFileVowConverter{} + badLog := types.Log{ + Topics: []common.Hash{{}}, + Data: []byte{1, 1, 1, 1, 1}, + } + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("converts a log to a model", func() { + converter := vow.DripFileVowConverter{} + + model, err := converter.ToModel(test_data.EthDripFileVowLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.DripFileVowModel)) + }) +}) diff --git a/pkg/transformers/drip_file/vow/model.go b/pkg/transformers/drip_file/vow/model.go new file mode 100644 index 00000000..42614648 --- /dev/null +++ b/pkg/transformers/drip_file/vow/model.go @@ -0,0 +1,22 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +type DripFileVowModel struct { + What string + Data string + TransactionIndex uint `db:"tx_idx"` + Raw []byte `db:"raw_log"` +} diff --git a/pkg/transformers/drip_file/vow/repository.go b/pkg/transformers/drip_file/vow/repository.go new file mode 100644 index 00000000..d41c4a53 --- /dev/null +++ b/pkg/transformers/drip_file/vow/repository.go @@ -0,0 +1,59 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +) + +type Repository interface { + Create(headerID int64, model DripFileVowModel) error + MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) +} + +type DripFileVowRepository struct { + db *postgres.DB +} + +func NewDripFileVowRepository(db *postgres.DB) DripFileVowRepository { + return DripFileVowRepository{db: db} +} + +func (repository DripFileVowRepository) Create(headerID int64, model DripFileVowModel) error { + _, err := repository.db.Exec( + `INSERT into maker.drip_file_vow (header_id, what, data, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5)`, + headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + ) + return err +} + +func (repository DripFileVowRepository) 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.drip_file_vow 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 +} diff --git a/pkg/transformers/drip_file/vow/repository_test.go b/pkg/transformers/drip_file/vow/repository_test.go new file mode 100644 index 00000000..d9ca1a9a --- /dev/null +++ b/pkg/transformers/drip_file/vow/repository_test.go @@ -0,0 +1,142 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow_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/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/test_config" +) + +var _ = Describe("", func() { + Describe("Create", func() { + It("adds a drip file vow 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()) + dripFileVowRepository := vow.NewDripFileVowRepository(db) + + err = dripFileVowRepository.Create(headerID, test_data.DripFileVowModel) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileVow vow.DripFileVowModel + err = db.Get(&dbDripFileVow, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) + Expect(err).NotTo(HaveOccurred()) + Expect(dbDripFileVow.What).To(Equal(test_data.DripFileVowModel.What)) + Expect(dbDripFileVow.Data).To(Equal(test_data.DripFileVowModel.Data)) + Expect(dbDripFileVow.TransactionIndex).To(Equal(test_data.DripFileVowModel.TransactionIndex)) + Expect(dbDripFileVow.Raw).To(MatchJSON(test_data.DripFileVowModel.Raw)) + }) + + It("does not duplicate drip 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()) + dripFileVowRepository := vow.NewDripFileVowRepository(db) + err = dripFileVowRepository.Create(headerID, test_data.DripFileVowModel) + Expect(err).NotTo(HaveOccurred()) + + err = dripFileVowRepository.Create(headerID, test_data.DripFileVowModel) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) + }) + + It("removes drip 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()) + dripFileVowRepository := vow.NewDripFileVowRepository(db) + err = dripFileVowRepository.Create(headerID, test_data.DripFileVowModel) + Expect(err).NotTo(HaveOccurred()) + + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + + Expect(err).NotTo(HaveOccurred()) + var dbDripFileVow vow.DripFileVowModel + err = db.Get(&dbDripFileVow, `SELECT what, data, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(sql.ErrNoRows)) + }) + }) + + Describe("MissingHeaders", func() { + It("returns headers with no associated drip file event", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + dripFileVowBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, dripFileVowBlockNumber, 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()) + } + dripFileVowRepository := vow.NewDripFileVowRepository(db) + err := dripFileVowRepository.Create(headerIDs[1], test_data.DripFileVowModel) + Expect(err).NotTo(HaveOccurred()) + + headers, err := dripFileVowRepository.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()) + } + dripFileVowRepository := vow.NewDripFileVowRepository(db) + dripFileVowRepositoryTwo := vow.NewDripFileVowRepository(dbTwo) + err := dripFileVowRepository.Create(headerIDs[0], test_data.DripFileVowModel) + Expect(err).NotTo(HaveOccurred()) + + nodeOneMissingHeaders, err := dripFileVowRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) + + nodeTwoMissingHeaders, err := dripFileVowRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) + }) + }) +}) diff --git a/pkg/transformers/drip_file/vow/transformer.go b/pkg/transformers/drip_file/vow/transformer.go new file mode 100644 index 00000000..e447b483 --- /dev/null +++ b/pkg/transformers/drip_file/vow/transformer.go @@ -0,0 +1,53 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +type DripFileVowTransformer struct { + Config shared.TransformerConfig + Converter Converter + Fetcher shared.LogFetcher + Repository Repository +} + +func (transformer DripFileVowTransformer) Execute() error { + missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) + if err != nil { + return err + } + for _, header := range missingHeaders { + topics := [][]common.Hash{{common.HexToHash(shared.DripFileVowSignature)}} + matchingLogs, err := transformer.Fetcher.FetchLogs(drip_file.DripFileConfig.ContractAddress, topics, header.BlockNumber) + if err != nil { + return err + } + for _, log := range matchingLogs { + model, err := transformer.Converter.ToModel(log) + if err != nil { + return err + } + err = transformer.Repository.Create(header.Id, model) + if err != nil { + return err + } + } + } + return nil +} diff --git a/pkg/transformers/drip_file/vow/transformer_test.go b/pkg/transformers/drip_file/vow/transformer_test.go new file mode 100644 index 00000000..9e82a6fa --- /dev/null +++ b/pkg/transformers/drip_file/vow/transformer_test.go @@ -0,0 +1,175 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow_test + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" + vow_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/drip_file/vow" +) + +var _ = Describe("Drip file vow transformer", func() { + It("gets missing headers for block numbers specified in config", func() { + repository := &vow_mocks.MockDripFileVowRepository{} + transformer := vow.DripFileVowTransformer{ + Config: drip_file.DripFileConfig, + Fetcher: &mocks.MockLogFetcher{}, + Converter: &vow_mocks.MockDripFileVowConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedStartingBlockNumber).To(Equal(drip_file.DripFileConfig.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(drip_file.DripFileConfig.EndingBlockNumber)) + }) + + It("returns error if repository returns error for missing headers", func() { + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeadersErr(fakes.FakeError) + transformer := vow.DripFileVowTransformer{ + Fetcher: &mocks.MockLogFetcher{}, + Converter: &vow_mocks.MockDripFileVowConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("fetches logs for missing headers", func() { + fetcher := &mocks.MockLogFetcher{} + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: &vow_mocks.MockDripFileVowConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) + Expect(fetcher.FetchedContractAddress).To(Equal(drip_file.DripFileConfig.ContractAddress)) + Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.DripFileVowSignature)}})) + }) + + It("returns error if fetcher returns error", func() { + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetcherError(fakes.FakeError) + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: &vow_mocks.MockDripFileVowConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("converts matching logs", func() { + converter := &vow_mocks.MockDripFileVowConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileVowLog}) + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(converter.PassedLog).To(Equal(test_data.EthDripFileVowLog)) + }) + + It("returns error if converter returns error", func() { + converter := &vow_mocks.MockDripFileVowConverter{} + converter.SetConverterError(fakes.FakeError) + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileVowLog}) + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("persists drip file model", func() { + converter := &vow_mocks.MockDripFileVowConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileVowLog}) + repository := &vow_mocks.MockDripFileVowRepository{} + fakeHeader := core.Header{BlockNumber: 1, Id: 2} + repository.SetMissingHeaders([]core.Header{fakeHeader}) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) + Expect(repository.PassedModel).To(Equal(test_data.DripFileVowModel)) + }) + + It("returns error if repository returns error for create", func() { + converter := &vow_mocks.MockDripFileVowConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthDripFileVowLog}) + repository := &vow_mocks.MockDripFileVowRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetCreateError(fakes.FakeError) + transformer := vow.DripFileVowTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) +}) diff --git a/pkg/transformers/drip_file/vow/vow_suite_test.go b/pkg/transformers/drip_file/vow/vow_suite_test.go new file mode 100644 index 00000000..57ca31e9 --- /dev/null +++ b/pkg/transformers/drip_file/vow/vow_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestVow(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Vow Suite") +} diff --git a/pkg/transformers/pit_file/ilk/converter.go b/pkg/transformers/pit_file/ilk/converter.go index 9bee67e8..c0ab6dde 100644 --- a/pkg/transformers/pit_file/ilk/converter.go +++ b/pkg/transformers/pit_file/ilk/converter.go @@ -17,9 +17,9 @@ package ilk import ( "bytes" "encoding/json" + "errors" "math/big" - "errors" "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) diff --git a/pkg/transformers/pit_file/ilk/converter_test.go b/pkg/transformers/pit_file/ilk/converter_test.go index 38e5ffe0..867fbc37 100644 --- a/pkg/transformers/pit_file/ilk/converter_test.go +++ b/pkg/transformers/pit_file/ilk/converter_test.go @@ -15,11 +15,11 @@ package ilk_test import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) diff --git a/pkg/transformers/pit_file/stability_fee/converter.go b/pkg/transformers/pit_file/stability_fee/converter.go index d78d6b2a..c5aa9bf5 100644 --- a/pkg/transformers/pit_file/stability_fee/converter.go +++ b/pkg/transformers/pit_file/stability_fee/converter.go @@ -17,8 +17,8 @@ package stability_fee import ( "bytes" "encoding/json" - "errors" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) diff --git a/pkg/transformers/pit_file/stability_fee/converter_test.go b/pkg/transformers/pit_file/stability_fee/converter_test.go index f91cf927..451af664 100644 --- a/pkg/transformers/pit_file/stability_fee/converter_test.go +++ b/pkg/transformers/pit_file/stability_fee/converter_test.go @@ -15,10 +15,10 @@ package stability_fee_test import ( + "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) diff --git a/pkg/transformers/shared/constants.go b/pkg/transformers/shared/constants.go index 1cb63992..803a7136 100644 --- a/pkg/transformers/shared/constants.go +++ b/pkg/transformers/shared/constants.go @@ -18,6 +18,7 @@ var ( DataItemLength = 32 CatABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"vat\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"vow\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"flips\",\"outputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"lad\",\"type\":\"bytes32\"},{\"name\":\"ink\",\"type\":\"uint256\"},{\"name\":\"tab\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"nflip\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"live\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"wards\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"name\":\"ilks\",\"outputs\":[{\"name\":\"flip\",\"type\":\"address\"},{\"name\":\"chop\",\"type\":\"uint256\"},{\"name\":\"lump\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"pit\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"vat_\",\"type\":\"address\"},{\"name\":\"pit_\",\"type\":\"address\"},{\"name\":\"vow_\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"ilk\",\"type\":\"bytes32\"},{\"indexed\":true,\"name\":\"lad\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"ink\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"art\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"tab\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"flip\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"iArt\",\"type\":\"uint256\"}],\"name\":\"Bite\",\"type\":\"event\"},{\"anonymous\":true,\"inputs\":[{\"indexed\":true,\"name\":\"sig\",\"type\":\"bytes4\"},{\"indexed\":true,\"name\":\"guy\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"foo\",\"type\":\"bytes32\"},{\"indexed\":true,\"name\":\"bar\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"wad\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"fax\",\"type\":\"bytes\"}],\"name\":\"LogNote\",\"type\":\"event\"},{\"constant\":false,\"inputs\":[{\"name\":\"guy\",\"type\":\"address\"}],\"name\":\"rely\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"guy\",\"type\":\"address\"}],\"name\":\"deny\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"what\",\"type\":\"bytes32\"},{\"name\":\"data\",\"type\":\"uint256\"}],\"name\":\"file\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"what\",\"type\":\"bytes32\"},{\"name\":\"flip\",\"type\":\"address\"}],\"name\":\"file\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"lad\",\"type\":\"bytes32\"}],\"name\":\"bite\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"uint256\"},{\"name\":\"wad\",\"type\":\"uint256\"}],\"name\":\"flip\",\"outputs\":[{\"name\":\"id\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" + DripABI = `[{"constant":true,"inputs":[],"name":"vat","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x36569e77"},{"constant":true,"inputs":[],"name":"repo","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x56ff3122"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"wards","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xbf353dbb"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"ilks","outputs":[{"name":"vow","type":"bytes32"},{"name":"tax","type":"uint256"},{"name":"rho","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xd9638d36"},{"inputs":[{"name":"vat_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor","signature":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event","signature":"0x644843f351d3fba4abcd60109eaff9f54bac8fb8ccf0bab941009c21df21cf31"},{"constant":false,"inputs":[{"name":"guy","type":"address"}],"name":"rely","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x65fae35e"},{"constant":false,"inputs":[{"name":"guy","type":"address"}],"name":"deny","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x9c52a7f1"},{"constant":true,"inputs":[],"name":"era","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x143e55e0"},{"constant":false,"inputs":[{"name":"ilk","type":"bytes32"},{"name":"vow","type":"bytes32"},{"name":"tax","type":"uint256"}],"name":"file","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x1a0b287e"},{"constant":false,"inputs":[{"name":"what","type":"bytes32"},{"name":"data","type":"uint256"}],"name":"file","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x29ae8114"},{"constant":false,"inputs":[{"name":"ilk","type":"bytes32"}],"name":"drip","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x44e2a5a8"}]` FlipperABI = `[{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"bids","outputs":[{"name":"bid","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"guy","type":"address"},{"name":"tic","type":"uint48"},{"name":"end","type":"uint48"},{"name":"urn","type":"bytes32"},{"name":"gal","type":"address"},{"name":"tab","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x4423c5f1"},{"constant":true,"inputs":[],"name":"ttl","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x4e8b1dd5"},{"constant":true,"inputs":[],"name":"gem","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x7bd2bea7"},{"constant":true,"inputs":[],"name":"beg","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x7d780d82"},{"constant":true,"inputs":[],"name":"tau","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xcfc4af55"},{"constant":true,"inputs":[],"name":"kicks","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xcfdd3302"},{"constant":true,"inputs":[],"name":"dai","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xf4b9fa75"},{"inputs":[{"name":"dai_","type":"address"},{"name":"gem_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor","signature":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":false,"name":"lot","type":"uint256"},{"indexed":false,"name":"bid","type":"uint256"},{"indexed":false,"name":"gal","type":"address"},{"indexed":false,"name":"end","type":"uint48"},{"indexed":true,"name":"urn","type":"bytes32"},{"indexed":false,"name":"tab","type":"uint256"}],"name":"Kick","type":"event","signature":"0xbac86238bdba81d21995024470425ecb370078fa62b7271b90cf28cbd1e3e87e"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event","signature":"0x644843f351d3fba4abcd60109eaff9f54bac8fb8ccf0bab941009c21df21cf31"},{"constant":true,"inputs":[],"name":"era","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x143e55e0"},{"constant":false,"inputs":[{"name":"urn","type":"bytes32"},{"name":"gal","type":"address"},{"name":"tab","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"kick","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xeae19d9e"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"tick","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xfc7b6aee"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"tend","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x4b43ed12"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"dent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x5ff3a382"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"deal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xc959c42b"}]` MedianizerABI = `[{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"","type":"bytes32"}],"name":"poke","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"poke","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"compute","outputs":[{"name":"","type":"bytes32"},{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"wat","type":"address"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wat","type":"address"}],"name":"unset","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"indexes","outputs":[{"name":"","type":"bytes12"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"next","outputs":[{"name":"","type":"bytes12"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"read","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"peek","outputs":[{"name":"","type":"bytes32"},{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes12"}],"name":"values","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"min_","type":"uint96"}],"name":"setMin","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"void","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"pos","type":"bytes12"},{"name":"wat","type":"address"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"pos","type":"bytes12"}],"name":"unset","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"next_","type":"bytes12"}],"name":"setNext","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"min","outputs":[{"name":"","type":"uint96"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"val","type":"bytes32"}],"name":"LogValue","type":"event"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}]]` PitABI = `[{"constant":true,"inputs":[],"name":"vat","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x36569e77"},{"constant":true,"inputs":[],"name":"live","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x957aa58c"},{"constant":true,"inputs":[],"name":"drip","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x9f678cca"},{"constant":true,"inputs":[],"name":"Line","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xbabe8a3f"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"wards","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xbf353dbb"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"ilks","outputs":[{"name":"spot","type":"uint256"},{"name":"line","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xd9638d36"},{"inputs":[{"name":"vat_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor","signature":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"ilk","type":"bytes32"},{"indexed":true,"name":"urn","type":"bytes32"},{"indexed":false,"name":"ink","type":"uint256"},{"indexed":false,"name":"art","type":"uint256"},{"indexed":false,"name":"dink","type":"int256"},{"indexed":false,"name":"dart","type":"int256"},{"indexed":false,"name":"iArt","type":"uint256"}],"name":"Frob","type":"event","signature":"0xb2afa28318bcc689926b52835d844de174ef8de97e982a85c0199d584920791b"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event","signature":"0x644843f351d3fba4abcd60109eaff9f54bac8fb8ccf0bab941009c21df21cf31"},{"constant":false,"inputs":[{"name":"guy","type":"address"}],"name":"rely","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x65fae35e"},{"constant":false,"inputs":[{"name":"guy","type":"address"}],"name":"deny","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x9c52a7f1"},{"constant":false,"inputs":[{"name":"ilk","type":"bytes32"},{"name":"what","type":"bytes32"},{"name":"data","type":"uint256"}],"name":"file","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x1a0b287e"},{"constant":false,"inputs":[{"name":"what","type":"bytes32"},{"name":"data","type":"uint256"}],"name":"file","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x29ae8114"},{"constant":false,"inputs":[{"name":"what","type":"bytes32"},{"name":"data","type":"address"}],"name":"file","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xd4e8be83"},{"constant":false,"inputs":[{"name":"ilk","type":"bytes32"},{"name":"dink","type":"int256"},{"name":"dart","type":"int256"}],"name":"frob","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x5a984ded"}]` @@ -25,6 +26,7 @@ var ( // temporary addresses from local Ganache deployment CatContractAddress = "0xe0f0fa6982c59d8aa4ae0134bfe048327bd788cacf758b643ca41f055ffce76c" + DripContractAddress = "0xD4ae7420FCF54ab7cd27ab9c24DCC876eF6b64C3" FlipperContractAddress = "0x6b59c42097e2fff7cad96cb08ceefd601081ad9c" PepContractAddress = "0x99041F808D598B782D5a3e498681C2452A31da08" PipContractAddress = "0x729D19f657BD0614b4985Cf1D82531c67569197B" @@ -32,13 +34,16 @@ var ( RepContractAddress = "0xF5f94b7F9De14D43112e713835BCef2d55b76c1C" VatContractAddress = "0x239E6f0AB02713f1F8AA90ebeDeD9FC66Dc96CD6" - biteMethod = GetSolidityMethodSignature(CatABI, "Bite") - dentMethod = GetSolidityMethodSignature(FlipperABI, "dent") - flipKickMethod = GetSolidityMethodSignature(FlipperABI, "Kick") - frobMethod = GetSolidityMethodSignature(PitABI, "Frob") - //TODO: get these pit file method signatures directly from the ABI - pitFileDebtCeilingMethod = "file(bytes32,uint256)" - pitFileIlkMethod = "file(bytes32,bytes32,uint256)" + //TODO: get pit and drip file method signatures directly from the ABI + biteMethod = GetSolidityMethodSignature(CatABI, "Bite") + dentMethod = GetSolidityMethodSignature(FlipperABI, "dent") + dripFileIlkMethod = "file(bytes32,bytes32,uint256)" + dripFileRepoMethod = GetSolidityMethodSignature(DripABI, "file") + dripFileVowMethod = "file(bytes32,bytes32)" + flipKickMethod = GetSolidityMethodSignature(FlipperABI, "Kick") + frobMethod = GetSolidityMethodSignature(PitABI, "Frob") + pitFileDebtCeilingMethod = "file(bytes32,uint256)" + pitFileIlkMethod = "file(bytes32,bytes32,uint256)" pitFileStabilityFeeMethod = GetSolidityMethodSignature(PitABI, "file") tendMethod = GetSolidityMethodSignature(FlipperABI, "tend") logValueMethod = GetSolidityMethodSignature(MedianizerABI, "LogValue") @@ -46,6 +51,9 @@ var ( BiteSignature = GetEventSignature(biteMethod) DentFunctionSignature = GetLogNoteSignature(dentMethod) + DripFileIlkSignature = GetLogNoteSignature(dripFileIlkMethod) + DripFileRepoSignature = GetLogNoteSignature(dripFileRepoMethod) + DripFileVowSignature = GetLogNoteSignature(dripFileVowMethod) FlipKickSignature = GetEventSignature(flipKickMethod) FrobSignature = GetEventSignature(frobMethod) LogValueSignature = GetEventSignature(logValueMethod) diff --git a/pkg/transformers/shared/event_signature_generator.go b/pkg/transformers/shared/event_signature_generator.go index 4d54541d..bb73d84e 100644 --- a/pkg/transformers/shared/event_signature_generator.go +++ b/pkg/transformers/shared/event_signature_generator.go @@ -15,11 +15,13 @@ package shared import ( - "github.com/ethereum/go-ethereum/crypto" - "github.com/vulcanize/vulcanizedb/pkg/geth" - "github.com/ethereum/go-ethereum/accounts/abi" "fmt" "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/crypto" + + "github.com/vulcanize/vulcanizedb/pkg/geth" ) func GetEventSignature(solidityMethodSignature string) string { @@ -53,4 +55,4 @@ func getEventSignature(event abi.Event) string { } return fmt.Sprintf("%v(%v)", event.Name, strings.Join(types, ",")) -} \ No newline at end of file +} diff --git a/pkg/transformers/tend/converter.go b/pkg/transformers/tend/converter.go index 7be49f53..f27dfef9 100644 --- a/pkg/transformers/tend/converter.go +++ b/pkg/transformers/tend/converter.go @@ -21,14 +21,13 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - ) type Converter interface { Convert(contractAddress string, contractAbi string, ethLog types.Log) (TendModel, error) } -type TendConverter struct {} +type TendConverter struct{} func NewTendConverter() TendConverter { return TendConverter{} diff --git a/pkg/transformers/test_data/drip_file.go b/pkg/transformers/test_data/drip_file.go new file mode 100644 index 00000000..d434eea0 --- /dev/null +++ b/pkg/transformers/test_data/drip_file.go @@ -0,0 +1,103 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test_data + +import ( + "encoding/json" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/types" + ilk2 "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "math/big" +) + +var EthDripFileIlkLog = types.Log{ + Address: common.HexToAddress(shared.DripContractAddress), + Topics: []common.Hash{ + common.HexToHash("0x1a0b287e00000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x00000000000000000000000064d922894153be9eef7b7218dc565d1d0ce2a092"), + common.HexToHash("0x66616b6520696c6b000000000000000000000000000000000000000000000000"), + common.HexToHash("0x66616b6520766f77000000000000000000000000000000000000000000000000"), + }, + Data: hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000641a0b287e66616b6520696c6b00000000000000000000000000000000000000000000000066616b6520766f77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b"), + BlockNumber: 35, + TxHash: common.HexToHash("0xa1c31b7e6389470902237161263558615e60b40f2e63060b2f4aeafe92d57e5f"), + TxIndex: 12, + BlockHash: common.HexToHash("0x0188f3ee3cc05aa72457fa328e6a461de31e4cbd429fc37f9a52da4e9773c0b4"), + Index: 0, + Removed: false, +} + +var rawDripFileIlkLog, _ = json.Marshal(EthDripFileIlkLog) +var DripFileIlkModel = ilk2.DripFileIlkModel{ + Ilk: "fake ilk", + Vow: "fake vow", + Tax: big.NewInt(123).String(), + TransactionIndex: EthDripFileIlkLog.TxIndex, + Raw: rawDripFileIlkLog, +} + +var EthDripFileRepoLog = types.Log{ + Address: common.HexToAddress(shared.DripContractAddress), + Topics: []common.Hash{ + common.HexToHash("0x29ae811400000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x00000000000000000000000064d922894153be9eef7b7218dc565d1d0ce2a092"), + common.HexToHash("0x66616b6520776861740000000000000000000000000000000000000000000000"), + common.HexToHash("0x000000000000000000000000000000000000000000000000000000000000007b"), + }, + Data: hexutil.MustDecode("0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004429ae811466616b6520776861740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007b"), + BlockNumber: 36, + TxHash: common.HexToHash("0xeeaa16de1d91c239b66773e8c2116a26cfeaaf5d962b31466c9bf047a5caa20f"), + TxIndex: 13, + BlockHash: common.HexToHash("0x89de4145ea8e34dfd9db9a7ea34f5be6f1f402e812fd389acca342513b353288"), + Index: 0, + Removed: false, +} + +var rawDripFileRepoLog, _ = json.Marshal(EthDripFileRepoLog) +var DripFileRepoModel = repo.DripFileRepoModel{ + What: "fake what", + Data: big.NewInt(123).String(), + TransactionIndex: EthDripFileRepoLog.TxIndex, + Raw: rawDripFileRepoLog, +} + +var EthDripFileVowLog = types.Log{ + Address: common.HexToAddress(shared.DripContractAddress), + Topics: []common.Hash{ + common.HexToHash("0xe9b674b900000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x00000000000000000000000064d922894153be9eef7b7218dc565d1d0ce2a092"), + common.HexToHash("0x66616b6520776861740000000000000000000000000000000000000000000000"), + common.HexToHash("0x66616b6520646174610000000000000000000000000000000000000000000000"), + }, + Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000044e9b674b966616b652077686174000000000000000000000000000000000000000000000066616b6520646174610000000000000000000000000000000000000000000000"), + BlockNumber: 51, + TxHash: common.HexToHash("0x586e26b71b41fcd6905044dbe8f0cca300517542278f74a9b925c4f800fed85c"), + TxIndex: 14, + BlockHash: common.HexToHash("0xbec69b1e93503679c9c006819477b86fe16aaff3a418da1e916c431b68be5522"), + Index: 0, + Removed: false, +} + +var rawDripFileVowLog, _ = json.Marshal(EthDripFileVowLog) +var DripFileVowModel = vow.DripFileVowModel{ + What: "fake what", + Data: "fake data", + TransactionIndex: EthDripFileVowLog.TxIndex, + Raw: rawDripFileVowLog, +} diff --git a/pkg/transformers/test_data/mocks/drip_file/ilk/converter.go b/pkg/transformers/test_data/mocks/drip_file/ilk/converter.go new file mode 100644 index 00000000..13d8a566 --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/ilk/converter.go @@ -0,0 +1,35 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +type MockDripFileIlkConverter struct { + converterErr error + PassedLog types.Log +} + +func (converter *MockDripFileIlkConverter) ToModel(ethLog types.Log) (ilk.DripFileIlkModel, error) { + converter.PassedLog = ethLog + return test_data.DripFileIlkModel, converter.converterErr +} + +func (converter *MockDripFileIlkConverter) SetConverterError(e error) { + converter.converterErr = e +} diff --git a/pkg/transformers/test_data/mocks/drip_file/ilk/repository.go b/pkg/transformers/test_data/mocks/drip_file/ilk/repository.go new file mode 100644 index 00000000..e08a2e58 --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/ilk/repository.go @@ -0,0 +1,54 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ilk + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" +) + +type MockDripFileIlkRepository struct { + createErr error + missingHeaders []core.Header + missingHeadersErr error + PassedStartingBlockNumber int64 + PassedEndingBlockNumber int64 + PassedHeaderID int64 + PassedModel ilk.DripFileIlkModel +} + +func (repository *MockDripFileIlkRepository) Create(headerID int64, model ilk.DripFileIlkModel) error { + repository.PassedHeaderID = headerID + repository.PassedModel = model + return repository.createErr +} + +func (repository *MockDripFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + repository.PassedStartingBlockNumber = startingBlockNumber + repository.PassedEndingBlockNumber = endingBlockNumber + return repository.missingHeaders, repository.missingHeadersErr +} + +func (repository *MockDripFileIlkRepository) SetMissingHeadersErr(e error) { + repository.missingHeadersErr = e +} + +func (repository *MockDripFileIlkRepository) SetMissingHeaders(headers []core.Header) { + repository.missingHeaders = headers +} + +func (repository *MockDripFileIlkRepository) SetCreateError(e error) { + repository.createErr = e +} diff --git a/pkg/transformers/test_data/mocks/drip_file/repo/converter.go b/pkg/transformers/test_data/mocks/drip_file/repo/converter.go new file mode 100644 index 00000000..2f9e37f5 --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/repo/converter.go @@ -0,0 +1,35 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +type MockDripFileRepoConverter struct { + converterErr error + PassedLog types.Log +} + +func (converter *MockDripFileRepoConverter) ToModel(ethLog types.Log) (repo.DripFileRepoModel, error) { + converter.PassedLog = ethLog + return test_data.DripFileRepoModel, converter.converterErr +} + +func (converter *MockDripFileRepoConverter) SetConverterError(e error) { + converter.converterErr = e +} diff --git a/pkg/transformers/test_data/mocks/drip_file/repo/repository.go b/pkg/transformers/test_data/mocks/drip_file/repo/repository.go new file mode 100644 index 00000000..dcf532b6 --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/repo/repository.go @@ -0,0 +1,54 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repo + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" +) + +type MockDripFileRepoRepository struct { + createErr error + missingHeaders []core.Header + missingHeadersErr error + PassedStartingBlockNumber int64 + PassedEndingBlockNumber int64 + PassedHeaderID int64 + PassedModel repo.DripFileRepoModel +} + +func (repository *MockDripFileRepoRepository) Create(headerID int64, model repo.DripFileRepoModel) error { + repository.PassedHeaderID = headerID + repository.PassedModel = model + return repository.createErr +} + +func (repository *MockDripFileRepoRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + repository.PassedStartingBlockNumber = startingBlockNumber + repository.PassedEndingBlockNumber = endingBlockNumber + return repository.missingHeaders, repository.missingHeadersErr +} + +func (repository *MockDripFileRepoRepository) SetMissingHeadersErr(e error) { + repository.missingHeadersErr = e +} + +func (repository *MockDripFileRepoRepository) SetMissingHeaders(headers []core.Header) { + repository.missingHeaders = headers +} + +func (repository *MockDripFileRepoRepository) SetCreateError(e error) { + repository.createErr = e +} diff --git a/pkg/transformers/test_data/mocks/drip_file/vow/converter.go b/pkg/transformers/test_data/mocks/drip_file/vow/converter.go new file mode 100644 index 00000000..95400dd1 --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/vow/converter.go @@ -0,0 +1,35 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +type MockDripFileVowConverter struct { + converterErr error + PassedLog types.Log +} + +func (converter *MockDripFileVowConverter) ToModel(ethLog types.Log) (vow.DripFileVowModel, error) { + converter.PassedLog = ethLog + return test_data.DripFileVowModel, converter.converterErr +} + +func (converter *MockDripFileVowConverter) SetConverterError(e error) { + converter.converterErr = e +} diff --git a/pkg/transformers/test_data/mocks/drip_file/vow/repository.go b/pkg/transformers/test_data/mocks/drip_file/vow/repository.go new file mode 100644 index 00000000..69404a7d --- /dev/null +++ b/pkg/transformers/test_data/mocks/drip_file/vow/repository.go @@ -0,0 +1,54 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vow + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" +) + +type MockDripFileVowRepository struct { + createErr error + missingHeaders []core.Header + missingHeadersErr error + PassedStartingBlockNumber int64 + PassedEndingBlockNumber int64 + PassedHeaderID int64 + PassedModel vow.DripFileVowModel +} + +func (repository *MockDripFileVowRepository) Create(headerID int64, model vow.DripFileVowModel) error { + repository.PassedHeaderID = headerID + repository.PassedModel = model + return repository.createErr +} + +func (repository *MockDripFileVowRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + repository.PassedStartingBlockNumber = startingBlockNumber + repository.PassedEndingBlockNumber = endingBlockNumber + return repository.missingHeaders, repository.missingHeadersErr +} + +func (repository *MockDripFileVowRepository) SetMissingHeadersErr(e error) { + repository.missingHeadersErr = e +} + +func (repository *MockDripFileVowRepository) SetMissingHeaders(headers []core.Header) { + repository.missingHeaders = headers +} + +func (repository *MockDripFileVowRepository) SetCreateError(e error) { + repository.createErr = e +} diff --git a/pkg/transformers/test_data/mocks/price_feeds/converter.go b/pkg/transformers/test_data/mocks/price_feeds/converter.go new file mode 100644 index 00000000..ef5115ca --- /dev/null +++ b/pkg/transformers/test_data/mocks/price_feeds/converter.go @@ -0,0 +1,37 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package price_feeds + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +type MockPriceFeedConverter struct { + converterErr error + PassedLog types.Log + PassedHeaderID int64 +} + +func (converter *MockPriceFeedConverter) ToModel(log types.Log, headerID int64) (price_feeds.PriceFeedModel, error) { + converter.PassedLog = log + converter.PassedHeaderID = headerID + return test_data.PriceFeedModel, converter.converterErr +} + +func (converter *MockPriceFeedConverter) SetConverterErr(e error) { + converter.converterErr = e +} diff --git a/pkg/transformers/test_data/price_feed.go b/pkg/transformers/test_data/price_feed.go new file mode 100644 index 00000000..b6d83377 --- /dev/null +++ b/pkg/transformers/test_data/price_feed.go @@ -0,0 +1,50 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test_data + +import ( + "encoding/json" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +var ( + medianizerAddress = common.HexToAddress("0x99041f808d598b782d5a3e498681c2452a31da08") + blockNumber = uint64(6147230) + txIndex = uint(119) +) + +// https://etherscan.io/tx/0xa51a50a2adbfba4e2ab3d72dfd67a21c769f1bc8d2b180663a15500a56cde58f +var EthPriceFeedLog = types.Log{ + Address: medianizerAddress, + Topics: []common.Hash{common.HexToHash(shared.LogValueSignature)}, + Data: common.FromHex("00000000000000000000000000000000000000000000001486f658319fb0c100"), + BlockNumber: blockNumber, + TxHash: common.HexToHash("0xa51a50a2adbfba4e2ab3d72dfd67a21c769f1bc8d2b180663a15500a56cde58f"), + TxIndex: txIndex, + BlockHash: common.HexToHash("0x27ecebbf69eefa3bb3cf65f472322a80ff4946653a50a2171dc605f49829467d"), + Index: 0, + Removed: false, +} + +var rawPriceFeedLog, _ = json.Marshal(EthPriceFeedLog) +var PriceFeedModel = price_feeds.PriceFeedModel{ + BlockNumber: blockNumber, + MedianizerAddress: EthPriceFeedLog.Address[:], + UsdValue: "378.6599388897", + TransactionIndex: EthPriceFeedLog.TxIndex, +} diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index 6ef0888f..d65d843b 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -17,6 +17,9 @@ package transformers import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/bite" "github.com/vulcanize/vulcanizedb/pkg/transformers/dent" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file" + ilk2 "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" "github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/frob" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" @@ -34,6 +37,9 @@ func TransformerInitializers() []shared.TransformerInitializer { dentTransformerInitializer := dent.DentTransformerInitializer{Config: dent.DentConfig} flipKickTransformerInitializer := flip_kick.FlipKickTransformerInitializer{Config: flip_kick.FlipKickConfig} frobTransformerInitializer := frob.FrobTransformerInitializer{Config: frob.FrobConfig} + dripFileConfig := drip_file.DripFileConfig + dripFileIlkTransformerInitializer := ilk2.DripFileIlkTransformerInitializer{Config: dripFileConfig} + dripFileRepoTransformerInitializer := repo.DripFileRepoTransformerInitializer{Config: dripFileConfig} pitFileConfig := pit_file.PitFileConfig pitFileDebtCeilingTransformerInitializer := debt_ceiling.PitFileDebtCeilingTransformerInitializer{Config: pitFileConfig} pitFileIlkTransformerInitializer := ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig} @@ -46,6 +52,8 @@ func TransformerInitializers() []shared.TransformerInitializer { return []shared.TransformerInitializer{ biteTransformerInitializer.NewBiteTransformer, dentTransformerInitializer.NewDentTransformer, + dripFileIlkTransformerInitializer.NewDripFileIlkTransformer, + dripFileRepoTransformerInitializer.NewDripFileRepoTransformer, flipKickTransformerInitializer.NewFlipKickTransformer, frobTransformerInitializer.NewFrobTransformer, pitFileDebtCeilingTransformerInitializer.NewPitFileDebtCeilingTransformer, diff --git a/test_config/test_config.go b/test_config/test_config.go index 5e14d6e5..80af9fe4 100644 --- a/test_config/test_config.go +++ b/test_config/test_config.go @@ -77,6 +77,11 @@ func CleanTestDB(db *postgres.DB) { db.MustExec("DELETE FROM headers") db.MustExec("DELETE FROM log_filters") db.MustExec("DELETE FROM logs") + db.MustExec("DELETE FROM maker.bite") + db.MustExec("DELETE FROM maker.drip_file_ilk") + db.MustExec("DELETE FROM maker.drip_file_repo") + db.MustExec("DELETE FROM maker.drip_file_vow") + db.MustExec("DELETE FROM maker.pit_file_debt_ceiling") db.MustExec("DELETE FROM maker.pit_file_ilk") db.MustExec("DELETE FROM maker.pit_file_stability_fee") db.MustExec("DELETE FROM receipts")