Update contract naming per Eric PR review

This commit is contained in:
Matt Krump 2017-11-13 10:11:27 -06:00
parent 30fadffb14
commit 4ad1d531a8
10 changed files with 66 additions and 63 deletions

View File

@ -36,7 +36,7 @@ func tasks(p *do.Project) {
do.M{"environment": environment, "startingNumber": startingNumber, "$in": "cmd/populate_blocks"}) do.M{"environment": environment, "startingNumber": startingNumber, "$in": "cmd/populate_blocks"})
}) })
p.Task("subscribeToContract", nil, func(context *do.Context) { p.Task("watchContract", nil, func(context *do.Context) {
environment := parseEnvironment(context) environment := parseEnvironment(context)
contractHash := context.Args.MayString("", "contract-hash", "c") contractHash := context.Args.MayString("", "contract-hash", "c")
if contractHash == "" { if contractHash == "" {

View File

@ -14,5 +14,5 @@ func main() {
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
repository := repositories.NewPostgres(config.Database) repository := repositories.NewPostgres(config.Database)
repository.CreateContract(core.Contract{Hash: *contractHash}) repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash})
} }

View File

@ -1 +1 @@
DROP TABLE contracts DROP TABLE watched_contracts

View File

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

View File

@ -3,7 +3,7 @@
-- --
-- Dumped from database version 10.0 -- 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 statement_timeout = 0;
SET lock_timeout = 0; SET lock_timeout = 0;
@ -58,6 +58,7 @@ CREATE TABLE blocks (
-- --
CREATE SEQUENCE blocks_id_seq CREATE SEQUENCE blocks_id_seq
AS integer
START WITH 1 START WITH 1
INCREMENT BY 1 INCREMENT BY 1
NO MINVALUE NO MINVALUE
@ -72,35 +73,6 @@ CREATE SEQUENCE blocks_id_seq
ALTER SEQUENCE blocks_id_seq OWNED BY blocks.id; ALTER SEQUENCE blocks_id_seq OWNED BY blocks.id;
--
-- Name: contracts; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE contracts (
contract_id integer NOT NULL,
contract_hash character varying(66)
);
--
-- Name: contracts_contract_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE contracts_contract_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: contracts_contract_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE contracts_contract_id_seq OWNED BY contracts.contract_id;
-- --
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: - -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
-- --
@ -132,6 +104,7 @@ CREATE TABLE transactions (
-- --
CREATE SEQUENCE transactions_id_seq CREATE SEQUENCE transactions_id_seq
AS integer
START WITH 1 START WITH 1
INCREMENT BY 1 INCREMENT BY 1
NO MINVALUE NO MINVALUE
@ -146,6 +119,36 @@ CREATE SEQUENCE transactions_id_seq
ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id; 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
AS integer
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: - -- Name: blocks id; Type: DEFAULT; Schema: public; Owner: -
-- --
@ -153,13 +156,6 @@ ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::regclass); ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::regclass);
--
-- Name: contracts contract_id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY contracts ALTER COLUMN contract_id SET DEFAULT nextval('contracts_contract_id_seq'::regclass);
-- --
-- Name: transactions id; Type: DEFAULT; Schema: public; Owner: - -- Name: transactions id; Type: DEFAULT; Schema: public; Owner: -
-- --
@ -167,6 +163,13 @@ ALTER TABLE ONLY contracts ALTER COLUMN contract_id SET DEFAULT nextval('contrac
ALTER TABLE ONLY transactions ALTER COLUMN id SET DEFAULT nextval('transactions_id_seq'::regclass); 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: - -- Name: blocks blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- --
@ -175,14 +178,6 @@ ALTER TABLE ONLY blocks
ADD CONSTRAINT blocks_pkey PRIMARY KEY (id); ADD CONSTRAINT blocks_pkey PRIMARY KEY (id);
--
-- Name: contracts contracts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY contracts
ADD CONSTRAINT contracts_pkey PRIMARY KEY (contract_id);
-- --
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- --
@ -199,6 +194,14 @@ ALTER TABLE ONLY transactions
ADD CONSTRAINT transactions_pkey PRIMARY KEY (id); 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: - -- Name: block_number_index; Type: INDEX; Schema: public; Owner: -
-- --

View File

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

View File

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

View File

@ -31,9 +31,9 @@ func NewPostgres(databaseConfig config.Database) Postgres {
return Postgres{Db: db} return Postgres{Db: db}
} }
func (repository Postgres) CreateContract(contract core.Contract) error { func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
_, err := repository.Db.Exec( _, err := repository.Db.Exec(
`INSERT INTO contracts (contract_hash) VALUES ($1)`, contract.Hash) `INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
if err != nil { if err != nil {
return ErrDBInsertFailed return ErrDBInsertFailed
} }
@ -43,7 +43,7 @@ func (repository Postgres) CreateContract(contract core.Contract) error {
func (repository Postgres) IsWatchedContract(contractHash string) bool { func (repository Postgres) IsWatchedContract(contractHash string) bool {
var exists bool var exists bool
err := repository.Db.QueryRow( err := repository.Db.QueryRow(
`SELECT exists(select 1 from contracts where contract_hash=$1) FROM contracts`, contractHash).Scan(&exists) `SELECT exists(select 1 from watched_contracts where contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Fatalf("error checking if row exists %v", err) log.Fatalf("error checking if row exists %v", err)
} }

View File

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

View File

@ -192,8 +192,8 @@ var _ = Describe("Repositories", func() {
Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5})) Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5}))
}) })
It("Adds a contract to the contracts table", func() { It("Adds a contract to the watched_contracts table", func() {
repository.CreateContract(core.Contract{Hash: "x123"}) repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
Expect(repository.IsWatchedContract("x123")).To(BeTrue()) Expect(repository.IsWatchedContract("x123")).To(BeTrue())
Expect(repository.IsWatchedContract("x456")).To(BeFalse()) Expect(repository.IsWatchedContract("x456")).To(BeFalse())