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
11 changed files with 130 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
package core
type WatchedContract struct {
Hash string
}
+14 -2
View File
@@ -5,7 +5,18 @@ import (
)
type InMemory struct {
blocks map[int64]*core.Block
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 {
@@ -20,7 +31,8 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
func NewInMemory() *InMemory {
return &InMemory{
blocks: make(map[int64]*core.Block),
blocks: make(map[int64]*core.Block),
watchedContracts: make(map[string]*core.WatchedContract),
}
}
+22 -3
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
+2
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
}
+7
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() {