Merge pull request #69 from 8thlight/watch_contract

* Add contracts table / start building out watch contracts
This commit is contained in:
Matt K 2017-11-13 10:55:55 -06:00 committed by GitHub
commit f0f086e48d
11 changed files with 130 additions and 7 deletions

View File

@ -36,6 +36,16 @@ func tasks(p *do.Project) {
do.M{"environment": environment, "startingNumber": startingNumber, "$in": "cmd/populate_blocks"})
})
p.Task("watchContract", nil, func(context *do.Context) {
environment := parseEnvironment(context)
contractHash := context.Args.MayString("", "contract-hash", "c")
if contractHash == "" {
log.Fatalln("--contract-hash required")
}
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}}`,
do.M{"environment": environment, "contractHash": contractHash, "$in": "cmd/subscribe_contract"})
})
p.Task("migrate", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := cmd.LoadConfig(environment)

View File

@ -0,0 +1,18 @@
package main
import (
"flag"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
flag.Parse()
config := cmd.LoadConfig(*environment)
repository := repositories.NewPostgres(config.Database)
repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash})
}

View File

@ -0,0 +1 @@
DROP TABLE watched_contracts

View File

@ -0,0 +1,5 @@
CREATE TABLE watched_contracts
(
contract_id SERIAL PRIMARY KEY,
contract_hash VARCHAR(66)
)

View File

@ -3,7 +3,7 @@
--
-- Dumped from database version 10.0
-- Dumped by pg_dump version 10.0
-- Dumped by pg_dump version 10.1
SET statement_timeout = 0;
SET lock_timeout = 0;
@ -118,6 +118,35 @@ CREATE SEQUENCE transactions_id_seq
ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
--
-- Name: watched_contracts; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE watched_contracts (
contract_id integer NOT NULL,
contract_hash character varying(66)
);
--
-- Name: watched_contracts_contract_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE watched_contracts_contract_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: watched_contracts_contract_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE watched_contracts_contract_id_seq OWNED BY watched_contracts.contract_id;
--
-- Name: blocks id; Type: DEFAULT; Schema: public; Owner: -
--
@ -132,6 +161,13 @@ ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::reg
ALTER TABLE ONLY transactions ALTER COLUMN id SET DEFAULT nextval('transactions_id_seq'::regclass);
--
-- Name: watched_contracts contract_id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY watched_contracts ALTER COLUMN contract_id SET DEFAULT nextval('watched_contracts_contract_id_seq'::regclass);
--
-- Name: blocks blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -156,6 +192,14 @@ ALTER TABLE ONLY transactions
ADD CONSTRAINT transactions_pkey PRIMARY KEY (id);
--
-- Name: watched_contracts watched_contracts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY watched_contracts
ADD CONSTRAINT watched_contracts_pkey PRIMARY KEY (contract_id);
--
-- Name: block_number_index; Type: INDEX; Schema: public; Owner: -
--

5
pkg/core/contract.go Normal file
View File

@ -0,0 +1,5 @@
package core
type WatchedContract struct {
Hash string
}

View File

@ -6,6 +6,17 @@ import (
type InMemory struct {
blocks map[int64]*core.Block
watchedContracts map[string]*core.WatchedContract
}
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error {
repository.watchedContracts[watchedContract.Hash] = &watchedContract
return nil
}
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
_, present := repository.watchedContracts[contractHash]
return present
}
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
@ -21,6 +32,7 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
func NewInMemory() *InMemory {
return &InMemory{
blocks: make(map[int64]*core.Block),
watchedContracts: make(map[string]*core.WatchedContract),
}
}

View File

@ -18,6 +18,10 @@ type Postgres struct {
Db *sqlx.DB
}
var (
ErrDBInsertFailed = errors.New("postgres: insert failed")
)
func NewPostgres(databaseConfig config.Database) Postgres {
connectString := config.DbConnectionString(databaseConfig)
db, err := sqlx.Connect("postgres", connectString)
@ -27,9 +31,24 @@ func NewPostgres(databaseConfig config.Database) Postgres {
return Postgres{Db: db}
}
var (
ErrDBInsertFailed = errors.New("postgres: insert failed")
)
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
_, err := repository.Db.Exec(
`INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
if err != nil {
return ErrDBInsertFailed
}
return nil
}
func (repository Postgres) IsWatchedContract(contractHash string) bool {
var exists bool
err := repository.Db.QueryRow(
`SELECT exists(select 1 from watched_contracts where contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
log.Fatalf("error checking if row exists %v", err)
}
return exists
}
func (repository Postgres) MaxBlockNumber() int64 {
var highestBlockNumber int64

View File

@ -8,4 +8,6 @@ type Repository interface {
FindBlockByNumber(blockNumber int64) *core.Block
MaxBlockNumber() int64
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
CreateWatchedContract(contract core.WatchedContract) error
IsWatchedContract(contractHash string) bool
}

View File

@ -195,6 +195,13 @@ var _ = Describe("Repositories", func() {
Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5}))
})
It("Adds a contract to the watched_contracts table", func() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
})
})
Describe("The max block numbers", func() {

View File

@ -2,7 +2,7 @@
if [ $# -eq 1 ]
then
migrate create -dir ./migrations -ext sql $1
migrate create -dir ./db/migrations -ext sql $1
else
echo "**An Error Occurred**"
echo "Usage: ./scripts/create_migration <migration-name>"