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

Commit

Permalink
Add Drip drip transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
rmulhol committed Sep 13, 2018
1 parent 560305f commit f5595be
Show file tree
Hide file tree
Showing 17 changed files with 830 additions and 2 deletions.
1 change: 1 addition & 0 deletions db/migrations/1536710319_create_drip_drip_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE maker.drip_drip;
8 changes: 8 additions & 0 deletions db/migrations/1536710319_create_drip_drip_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE maker.drip_drip (
id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
ilk TEXT,
tx_idx INTEGER NOT NUll,
raw_log JSONB,
UNIQUE (header_id, tx_idx)
);
64 changes: 64 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,39 @@ CREATE SEQUENCE maker.dent_db_id_seq
ALTER SEQUENCE maker.dent_db_id_seq OWNED BY maker.dent.db_id;


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

CREATE TABLE maker.drip_drip (
id integer NOT NULL,
header_id integer NOT NULL,
ilk text,
tx_idx integer NOT NULL,
raw_log jsonb
);


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

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


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

ALTER SEQUENCE maker.drip_drip_id_seq OWNED BY maker.drip_drip.id;


--
-- Name: drip_file_ilk; Type: TABLE; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -931,6 +964,13 @@ ALTER TABLE ONLY maker.deal ALTER COLUMN id SET DEFAULT nextval('maker.deal_id_s
ALTER TABLE ONLY maker.dent ALTER COLUMN db_id SET DEFAULT nextval('maker.dent_db_id_seq'::regclass);


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

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


--
-- Name: drip_file_ilk id; Type: DEFAULT; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -1119,6 +1159,22 @@ ALTER TABLE ONLY maker.dent
ADD CONSTRAINT dent_pkey PRIMARY KEY (db_id);


--
-- Name: drip_drip drip_drip_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.drip_drip
ADD CONSTRAINT drip_drip_header_id_tx_idx_key UNIQUE (header_id, tx_idx);


--
-- Name: drip_drip drip_drip_pkey; Type: CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.drip_drip
ADD CONSTRAINT drip_drip_pkey PRIMARY KEY (id);


--
-- Name: drip_file_ilk drip_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--
Expand Down Expand Up @@ -1450,6 +1506,14 @@ ALTER TABLE ONLY maker.dent
ADD CONSTRAINT dent_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;


--
-- Name: drip_drip drip_drip_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.drip_drip
ADD CONSTRAINT drip_drip_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: -
--
Expand Down
25 changes: 25 additions & 0 deletions pkg/transformers/drip_drip/config.go
Original file line number Diff line number Diff line change
@@ -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_drip

import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"

var DripDripConfig = shared.TransformerConfig{
ContractAddress: shared.DripContractAddress,
ContractAbi: shared.DripABI,
Topics: []string{shared.DripDripSignature},
StartingBlockNumber: 0,
EndingBlockNumber: 100,
}
49 changes: 49 additions & 0 deletions pkg/transformers/drip_drip/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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_drip

import (
"bytes"
"encoding/json"
"errors"
"github.com/ethereum/go-ethereum/core/types"
)

type Converter interface {
ToModel(ethLog types.Log) (DripDripModel, error)
}

type DripDripConverter struct{}

func (DripDripConverter) ToModel(ethLog types.Log) (DripDripModel, error) {
err := verifyLog(ethLog)
if err != nil {
return DripDripModel{}, err
}
ilk := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00"))
raw, err := json.Marshal(ethLog)
return DripDripModel{
Ilk: ilk,
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}, err
}

func verifyLog(log types.Log) error {
if len(log.Topics) < 3 {
return errors.New("log missing topics")
}
return nil
}
44 changes: 44 additions & 0 deletions pkg/transformers/drip_drip/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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_drip_test

import (
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vulcanize/vulcanizedb/pkg/transformers/drip_drip"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)

var _ = Describe("Drip drip converter", func() {
It("returns err if log is missing topics", func() {
converter := drip_drip.DripDripConverter{}
badLog := types.Log{}

_, err := converter.ToModel(badLog)

Expect(err).To(HaveOccurred())
})

It("converts a log to an model", func() {
converter := drip_drip.DripDripConverter{}

model, err := converter.ToModel(test_data.EthDripDripLog)

Expect(err).NotTo(HaveOccurred())
Expect(model).To(Equal(test_data.DripDripModel))
})
})
27 changes: 27 additions & 0 deletions pkg/transformers/drip_drip/drip_drip_suite_test.go
Original file line number Diff line number Diff line change
@@ -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 drip_drip_test

import (
"testing"

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

func TestDripDrip(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "DripDrip Suite")
}
21 changes: 21 additions & 0 deletions pkg/transformers/drip_drip/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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_drip

type DripDripModel struct {
Ilk string
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}
59 changes: 59 additions & 0 deletions pkg/transformers/drip_drip/repository.go
Original file line number Diff line number Diff line change
@@ -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 drip_drip

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

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

type DripDripRepository struct {
db *postgres.DB
}

func NewDripDripRepository(db *postgres.DB) DripDripRepository {
return DripDripRepository{db: db}
}

func (repository DripDripRepository) Create(headerID int64, model DripDripModel) error {
_, err := repository.db.Exec(
`INSERT into maker.drip_drip (header_id, ilk, tx_idx, raw_log)
VALUES($1, $2, $3, $4)`,
headerID, model.Ilk, model.TransactionIndex, model.Raw,
)
return err
}

func (repository DripDripRepository) 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_drip 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
}
Loading

0 comments on commit f5595be

Please sign in to comment.