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"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
func main() {
@ -13,5 +13,5 @@ func main() {
flag.Parse()
config := cmd.LoadConfig(*environment)
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
type WatchedContract struct {
Hash string
Transactions []Transaction
}
type Contract struct {
Attributes ContractAttributes
Hash string
@ -20,6 +15,7 @@ type ContractAttributes []ContractAttribute
func (attributes ContractAttributes) Len() int {
return len(attributes)
}
func (attributes ContractAttributes) Swap(i, j int) {
attributes[i], attributes[j] = attributes[j], attributes[i]
}

View File

@ -6,10 +6,10 @@ import (
type InMemory struct {
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
return nil
}
@ -19,7 +19,7 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
return present
}
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract {
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
var transactions []core.Transaction
if _, ok := repository.watchedContracts[contractHash]; !ok {
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 {
@ -47,7 +47,7 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
func NewInMemory() *InMemory {
return &InMemory{
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
}
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
_, err := repository.Db.Exec(
`INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
if err != nil {
@ -47,8 +47,8 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
return exists
}
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract {
var savedContracts []core.WatchedContract
func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
var savedContracts []WatchedContract
contractRows, _ := repository.Db.Query(
`SELECT contract_hash FROM watched_contracts WHERE contract_hash=$1`, contractHash)
savedContracts = repository.loadContract(contractRows)
@ -192,14 +192,14 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
return transactions
}
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract {
var savedContracts []core.WatchedContract
func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContract {
var savedContracts []WatchedContract
for contractRows.Next() {
var savedContractHash string
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)
transactions := repository.loadTransactions(transactionRows)
savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions}
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions}
savedContracts = append(savedContracts, savedContract)
}
return savedContracts

View File

@ -8,7 +8,7 @@ type Repository interface {
FindBlockByNumber(blockNumber int64) *core.Block
MaxBlockNumber() int64
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
CreateWatchedContract(contract core.WatchedContract) error
CreateWatchedContract(contract WatchedContract) error
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() {
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")
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() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil())
Expect(watchedContract.Transactions).To(BeEmpty())
@ -244,7 +244,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
}
repository.CreateBlock(block)
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil())
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
}
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)
return ContractSummary{
blockChain: blockchain,
@ -52,9 +52,9 @@ func newContractSummary(blockchain core.Blockchain, watchedContract core.Watched
}
}
func lastTransaction(contract core.WatchedContract) *core.Transaction {
if len(contract.Transactions) > 0 {
return &contract.Transactions[0]
func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction {
if len(watchedContract.Transactions) > 0 {
return &watchedContract.Transactions[0]
} else {
return nil
}

View File

@ -32,7 +32,7 @@ var _ = Describe("The watched contract summary", func() {
Context("when the given contract is being watched", func() {
It("returns the summary", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
@ -44,7 +44,7 @@ var _ = Describe("The watched contract summary", func() {
It("includes the contract hash in the summary", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
@ -55,7 +55,7 @@ var _ = Describe("The watched contract summary", func() {
It("sets the number of transactions", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
block := core.Block{
Transactions: []core.Transaction{
@ -73,7 +73,7 @@ var _ = Describe("The watched contract summary", func() {
It("sets the last transaction", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
block := core.Block{
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() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
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() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
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() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
watchedContract := repositories.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")