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
+1 -1
View File
@@ -1,5 +1,5 @@
package core
type Contract struct {
type WatchedContract struct {
Hash string
}
+7 -7
View File
@@ -5,17 +5,17 @@ import (
)
type InMemory struct {
blocks map[int64]*core.Block
contracts map[string]*core.Contract
blocks map[int64]*core.Block
watchedContracts map[string]*core.WatchedContract
}
func (repository *InMemory) CreateContract(contract core.Contract) error {
repository.contracts[contract.Hash] = &contract
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error {
repository.watchedContracts[watchedContract.Hash] = &watchedContract
return nil
}
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
_, present := repository.contracts[contractHash]
_, present := repository.watchedContracts[contractHash]
return present
}
@@ -31,8 +31,8 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
func NewInMemory() *InMemory {
return &InMemory{
blocks: make(map[int64]*core.Block),
contracts: make(map[string]*core.Contract),
blocks: make(map[int64]*core.Block),
watchedContracts: make(map[string]*core.WatchedContract),
}
}
+3 -3
View File
@@ -31,9 +31,9 @@ func NewPostgres(databaseConfig config.Database) Postgres {
return Postgres{Db: db}
}
func (repository Postgres) CreateContract(contract core.Contract) error {
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
_, 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 {
return ErrDBInsertFailed
}
@@ -43,7 +43,7 @@ func (repository Postgres) CreateContract(contract core.Contract) error {
func (repository Postgres) IsWatchedContract(contractHash string) bool {
var exists bool
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 {
log.Fatalf("error checking if row exists %v", err)
}
+1 -1
View File
@@ -8,6 +8,6 @@ type Repository interface {
FindBlockByNumber(blockNumber int64) *core.Block
MaxBlockNumber() int64
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
CreateContract(contract core.Contract) error
CreateWatchedContract(contract core.WatchedContract) error
IsWatchedContract(contractHash string) bool
}
+2 -2
View File
@@ -192,8 +192,8 @@ var _ = Describe("Repositories", func() {
Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5}))
})
It("Adds a contract to the contracts table", func() {
repository.CreateContract(core.Contract{Hash: "x123"})
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())