diff --git a/cmd/subscribe_contract/main.go b/cmd/subscribe_contract/main.go index 04fa4552..7d6dcf10 100644 --- a/cmd/subscribe_contract/main.go +++ b/cmd/subscribe_contract/main.go @@ -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}) } diff --git a/pkg/core/watched_contract.go b/pkg/core/contract.go similarity index 86% rename from pkg/core/watched_contract.go rename to pkg/core/contract.go index 41bb9b1f..1686732a 100644 --- a/pkg/core/watched_contract.go +++ b/pkg/core/contract.go @@ -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] } diff --git a/pkg/repositories/in_memory.go b/pkg/repositories/in_memory.go index a86df78f..93d77fb2 100644 --- a/pkg/repositories/in_memory.go +++ b/pkg/repositories/in_memory.go @@ -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), } } diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index 7d0d4d36..5c5b67b2 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -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 diff --git a/pkg/repositories/repository.go b/pkg/repositories/repository.go index bc246b2b..93581f3b 100644 --- a/pkg/repositories/repository.go +++ b/pkg/repositories/repository.go @@ -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 } diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index 199f6374..4b66877b 100644 --- a/pkg/repositories/testing/helpers.go +++ b/pkg/repositories/testing/helpers.go @@ -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( diff --git a/pkg/repositories/watched_contract.go b/pkg/repositories/watched_contract.go new file mode 100644 index 00000000..9d56544b --- /dev/null +++ b/pkg/repositories/watched_contract.go @@ -0,0 +1,8 @@ +package repositories + +import "github.com/8thlight/vulcanizedb/pkg/core" + +type WatchedContract struct { + Hash string + Transactions []core.Transaction +} diff --git a/pkg/watched_contracts/summary.go b/pkg/watched_contracts/summary.go index 71bf112d..beae1b08 100644 --- a/pkg/watched_contracts/summary.go +++ b/pkg/watched_contracts/summary.go @@ -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 } diff --git a/pkg/watched_contracts/summary_test.go b/pkg/watched_contracts/summary_test.go index 8308ad03..af8d4542 100644 --- a/pkg/watched_contracts/summary_test.go +++ b/pkg/watched_contracts/summary_test.go @@ -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")