forked from cerc-io/ipld-eth-server
Add contracts table / start building out watch contracts
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package core
|
||||
|
||||
type Contract struct {
|
||||
Hash string
|
||||
}
|
||||
@@ -5,7 +5,18 @@ import (
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]*core.Block
|
||||
blocks map[int64]*core.Block
|
||||
contracts map[string]*core.Contract
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateContract(contract core.Contract) error {
|
||||
repository.contracts[contract.Hash] = &contract
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
_, present := repository.contracts[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),
|
||||
contracts: make(map[string]*core.Contract),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) CreateContract(contract core.Contract) error {
|
||||
_, err := repository.Db.Exec(
|
||||
`INSERT INTO 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 contracts where contract_hash=$1) FROM 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
|
||||
|
||||
@@ -8,4 +8,6 @@ type Repository interface {
|
||||
FindBlockByNumber(blockNumber int64) *core.Block
|
||||
MaxBlockNumber() int64
|
||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||
CreateContract(contract core.Contract) error
|
||||
IsWatchedContract(contractHash string) bool
|
||||
}
|
||||
|
||||
@@ -192,6 +192,13 @@ 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"})
|
||||
|
||||
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
|
||||
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("The max block numbers", func() {
|
||||
|
||||
Reference in New Issue
Block a user