Move WatchedContract to repositories

This commit is contained in:
Eric Meyer 2017-12-04 13:33:07 -06:00
parent 3af336a34a
commit 52e3266495
9 changed files with 38 additions and 34 deletions

View File

@ -4,7 +4,7 @@ import (
"flag" "flag"
"github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/repositories"
) )
func main() { func main() {
@ -13,5 +13,5 @@ func main() {
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
repository := cmd.LoadPostgres(config.Database) repository := cmd.LoadPostgres(config.Database)
repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash}) repository.CreateWatchedContract(repositories.WatchedContract{Hash: *contractHash})
} }

View File

@ -1,10 +1,5 @@
package core package core
type WatchedContract struct {
Hash string
Transactions []Transaction
}
type Contract struct { type Contract struct {
Attributes ContractAttributes Attributes ContractAttributes
Hash string Hash string
@ -20,6 +15,7 @@ type ContractAttributes []ContractAttribute
func (attributes ContractAttributes) Len() int { func (attributes ContractAttributes) Len() int {
return len(attributes) return len(attributes)
} }
func (attributes ContractAttributes) Swap(i, j int) { func (attributes ContractAttributes) Swap(i, j int) {
attributes[i], attributes[j] = attributes[j], attributes[i] attributes[i], attributes[j] = attributes[j], attributes[i]
} }

View File

@ -6,10 +6,10 @@ import (
type InMemory struct { type InMemory struct {
blocks map[int64]*core.Block blocks map[int64]*core.Block
watchedContracts map[string]*core.WatchedContract watchedContracts map[string]*WatchedContract
} }
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error { func (repository *InMemory) CreateWatchedContract(watchedContract WatchedContract) error {
repository.watchedContracts[watchedContract.Hash] = &watchedContract repository.watchedContracts[watchedContract.Hash] = &watchedContract
return nil return nil
} }
@ -19,7 +19,7 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
return present return present
} }
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract { func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
var transactions []core.Transaction var transactions []core.Transaction
if _, ok := repository.watchedContracts[contractHash]; !ok { if _, ok := repository.watchedContracts[contractHash]; !ok {
return nil return nil
@ -31,7 +31,7 @@ func (repository *InMemory) FindWatchedContract(contractHash string) *core.Watch
} }
} }
} }
return &core.WatchedContract{Hash: contractHash, Transactions: transactions} return &WatchedContract{Hash: contractHash, Transactions: transactions}
} }
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 { func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
@ -47,7 +47,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),
watchedContracts: make(map[string]*core.WatchedContract), watchedContracts: make(map[string]*WatchedContract),
} }
} }

View File

@ -31,7 +31,7 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
return Postgres{Db: db}, nil return Postgres{Db: db}, nil
} }
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error { func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
_, err := repository.Db.Exec( _, err := repository.Db.Exec(
`INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash) `INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
if err != nil { if err != nil {
@ -47,8 +47,8 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
return exists return exists
} }
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract { func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
var savedContracts []core.WatchedContract var savedContracts []WatchedContract
contractRows, _ := repository.Db.Query( contractRows, _ := repository.Db.Query(
`SELECT contract_hash FROM watched_contracts WHERE contract_hash=$1`, contractHash) `SELECT contract_hash FROM watched_contracts WHERE contract_hash=$1`, contractHash)
savedContracts = repository.loadContract(contractRows) savedContracts = repository.loadContract(contractRows)
@ -192,14 +192,14 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
return transactions return transactions
} }
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract { func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContract {
var savedContracts []core.WatchedContract var savedContracts []WatchedContract
for contractRows.Next() { for contractRows.Next() {
var savedContractHash string var savedContractHash string
contractRows.Scan(&savedContractHash) contractRows.Scan(&savedContractHash)
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash) transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash)
transactions := repository.loadTransactions(transactionRows) transactions := repository.loadTransactions(transactionRows)
savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions} savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions}
savedContracts = append(savedContracts, savedContract) savedContracts = append(savedContracts, savedContract)
} }
return savedContracts return savedContracts

View File

@ -8,7 +8,7 @@ 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
CreateWatchedContract(contract core.WatchedContract) error CreateWatchedContract(contract WatchedContract) error
IsWatchedContract(contractHash string) bool IsWatchedContract(contractHash string) bool
FindWatchedContract(contractHash string) *core.WatchedContract FindWatchedContract(contractHash string) *WatchedContract
} }

View File

@ -210,7 +210,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
Describe("Creating watched contracts", func() { Describe("Creating watched contracts", func() {
It("returns the watched contract when it exists", func() { It("returns the watched contract when it exists", func() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).NotTo(BeNil()) Expect(watchedContract).NotTo(BeNil())
@ -226,7 +226,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
}) })
It("returns empty array when no transactions 'To' a watched contract", func() { It("returns empty array when no transactions 'To' a watched contract", func() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil()) Expect(watchedContract).ToNot(BeNil())
Expect(watchedContract.Transactions).To(BeEmpty()) Expect(watchedContract.Transactions).To(BeEmpty())
@ -244,7 +244,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
} }
repository.CreateBlock(block) repository.CreateBlock(block)
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil()) Expect(watchedContract).ToNot(BeNil())
Expect(watchedContract.Transactions).To( Expect(watchedContract.Transactions).To(

View File

@ -0,0 +1,8 @@
package repositories
import "github.com/8thlight/vulcanizedb/pkg/core"
type WatchedContract struct {
Hash string
Transactions []core.Transaction
}

View File

@ -39,7 +39,7 @@ func (contractSummary ContractSummary) GetStateAttribute(attributeName string) i
return result return result
} }
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary { func newContractSummary(blockchain core.Blockchain, watchedContract repositories.WatchedContract, blockNumber *big.Int) ContractSummary {
contract, _ := blockchain.GetContract(watchedContract.Hash) contract, _ := blockchain.GetContract(watchedContract.Hash)
return ContractSummary{ return ContractSummary{
blockChain: blockchain, blockChain: blockchain,
@ -52,9 +52,9 @@ func newContractSummary(blockchain core.Blockchain, watchedContract core.Watched
} }
} }
func lastTransaction(contract core.WatchedContract) *core.Transaction { func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction {
if len(contract.Transactions) > 0 { if len(watchedContract.Transactions) > 0 {
return &contract.Transactions[0] return &watchedContract.Transactions[0]
} else { } else {
return nil return nil
} }

View File

@ -32,7 +32,7 @@ var _ = Describe("The watched contract summary", func() {
Context("when the given contract is being watched", func() { Context("when the given contract is being watched", func() {
It("returns the summary", func() { It("returns the summary", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
@ -44,7 +44,7 @@ var _ = Describe("The watched contract summary", func() {
It("includes the contract hash in the summary", func() { It("includes the contract hash in the summary", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
@ -55,7 +55,7 @@ var _ = Describe("The watched contract summary", func() {
It("sets the number of transactions", func() { It("sets the number of transactions", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
block := core.Block{ block := core.Block{
Transactions: []core.Transaction{ Transactions: []core.Transaction{
@ -73,7 +73,7 @@ var _ = Describe("The watched contract summary", func() {
It("sets the last transaction", func() { It("sets the last transaction", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
block := core.Block{ block := core.Block{
Transactions: []core.Transaction{ Transactions: []core.Transaction{
@ -91,7 +91,7 @@ var _ = Describe("The watched contract summary", func() {
It("gets contract state attribute for the contract from the blockchain", func() { It("gets contract state attribute for the contract from the blockchain", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar") blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
@ -104,7 +104,7 @@ var _ = Describe("The watched contract summary", func() {
It("gets contract state attribute for the contract from the blockchain at specific block height", func() { It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockNumber := big.NewInt(1000) blockNumber := big.NewInt(1000)
@ -119,7 +119,7 @@ var _ = Describe("The watched contract summary", func() {
It("gets attributes for the contract from the blockchain", func() { It("gets attributes for the contract from the blockchain", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar") blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")