From 5aa0bcd6ce7d949d4a2d24db3ccd4bc4ba515d9c Mon Sep 17 00:00:00 2001 From: Eric Meyer Date: Mon, 4 Dec 2017 15:31:39 -0600 Subject: [PATCH] Move WatchedContract back to core --- pkg/core/blockchain.go | 1 + pkg/core/watched_contract.go | 7 + pkg/fakes/blockchain.go | 4 +- pkg/geth/contract.go | 4 +- pkg/geth/contract_test.go | 231 +++++++++++++------------- pkg/repositories/in_memory.go | 8 +- pkg/repositories/postgres.go | 12 +- pkg/repositories/repository.go | 4 +- pkg/repositories/testing/helpers.go | 8 +- pkg/repositories/watched_contract.go | 9 - pkg/watched_contracts/summary.go | 4 +- pkg/watched_contracts/summary_test.go | 14 +- 12 files changed, 153 insertions(+), 153 deletions(-) create mode 100644 pkg/core/watched_contract.go delete mode 100644 pkg/repositories/watched_contract.go diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index a80af9e8..1e9d84dd 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -8,5 +8,6 @@ type Blockchain interface { StartListening() StopListening() GetContract(contractHash string) (Contract, error) + GetContractAttributes(contractHash string) (ContractAttributes, error) GetAttribute(contract Contract, attributeName string, blockNumber *big.Int) (interface{}, error) } diff --git a/pkg/core/watched_contract.go b/pkg/core/watched_contract.go new file mode 100644 index 00000000..9ccc7b54 --- /dev/null +++ b/pkg/core/watched_contract.go @@ -0,0 +1,7 @@ +package core + +type WatchedContract struct { + Abi string + Hash string + Transactions []Transaction +} diff --git a/pkg/fakes/blockchain.go b/pkg/fakes/blockchain.go index 48ec5070..aed35794 100644 --- a/pkg/fakes/blockchain.go +++ b/pkg/fakes/blockchain.go @@ -16,7 +16,7 @@ type Blockchain struct { } func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) { - contractAttributes, err := blockchain.getContractAttributes(contractHash) + contractAttributes, err := blockchain.GetContractAttributes(contractHash) contract := core.Contract{ Attributes: contractAttributes, Hash: contractHash, @@ -84,7 +84,7 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blo blockchain.contractAttributes[key][attributeName] = attributeValue } -func (blockchain *Blockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) { +func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { var contractAttributes core.ContractAttributes attributes, ok := blockchain.contractAttributes[contractHash+"-1"] if ok { diff --git a/pkg/geth/contract.go b/pkg/geth/contract.go index c1b08aa8..a5932da7 100644 --- a/pkg/geth/contract.go +++ b/pkg/geth/contract.go @@ -22,7 +22,7 @@ var ( ) func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contract, error) { - attributes, err := blockchain.getContractAttributes(contractHash) + attributes, err := blockchain.GetContractAttributes(contractHash) if err != nil { return core.Contract{}, err } else { @@ -69,7 +69,7 @@ func callContract(contract core.Contract, input []byte, err error, blockchain *G return blockchain.client.CallContract(context.Background(), msg, blockNumber) } -func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) { +func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash)) parsed, _ := ParseAbiFile(abiFilePath) var contractAttributes core.ContractAttributes diff --git a/pkg/geth/contract_test.go b/pkg/geth/contract_test.go index 3f4a6637..a4ea1125 100644 --- a/pkg/geth/contract_test.go +++ b/pkg/geth/contract_test.go @@ -1,117 +1,118 @@ package geth_test -//import ( -// "math/big" -// -// cfg "github.com/8thlight/vulcanizedb/pkg/config" -// "github.com/8thlight/vulcanizedb/pkg/geth" -// "github.com/8thlight/vulcanizedb/pkg/geth/testing" -// . "github.com/onsi/ginkgo" -// . "github.com/onsi/gomega" -//) -// -//var _ = Describe("Reading contracts", func() { -// -// Describe("Reading the list of attributes", func() { -// It("returns a string attribute for a real contract", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, err := blockchain.GetContract(contractHash) -// -// Expect(err).To(BeNil()) -// Expect(len(contract.Attributes)).NotTo(Equal(0)) -// symbolAttribute := *testing.FindAttribute(contract.Attributes, "symbol") -// Expect(symbolAttribute.Name).To(Equal("symbol")) -// Expect(symbolAttribute.Type).To(Equal("string")) -// }) -// -// It("does not return an attribute that takes an input", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, err := blockchain.GetContract(contractHash) -// -// Expect(err).To(BeNil()) -// attribute := testing.FindAttribute(contract.Attributes, "balanceOf") -// Expect(attribute).To(BeNil()) -// }) -// -// It("does not return an attribute that is not constant", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, err := blockchain.GetContract(contractHash) -// -// Expect(err).To(BeNil()) -// attribute := testing.FindAttribute(contract.Attributes, "unpause") -// Expect(attribute).To(BeNil()) -// }) -// }) -// -// Describe("Getting a contract attribute", func() { -// It("returns the correct attribute for a real contract", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, _ := blockchain.GetContract(contractHash) -// name, err := blockchain.GetAttribute(contract, "name", nil) -// -// Expect(err).To(BeNil()) -// Expect(name).To(Equal("OMGToken")) -// }) -// -// It("returns the correct attribute for a real contract", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, _ := blockchain.GetContract(contractHash) -// name, err := blockchain.GetAttribute(contract, "name", nil) -// -// Expect(err).To(BeNil()) -// Expect(name).To(Equal("OMGToken")) -// }) -// -// It("returns the correct attribute for a real contract at a specific block height", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, _ := blockchain.GetContract(contractHash) -// name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4652791)) -// -// Expect(name).To(Equal("OMGToken")) -// Expect(err).To(BeNil()) -// }) -// -// It("returns an error when there is no ABI for the given contract", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "MISSINGHASH" -// -// contract, _ := blockchain.GetContract(contractHash) -// name, err := blockchain.GetAttribute(contract, "name", nil) -// -// Expect(err).To(Equal(geth.ErrMissingAbiFile)) -// Expect(name).To(BeNil()) -// }) -// -// It("returns an error when asking for an attribute that does not exist", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// contract, _ := blockchain.GetContract(contractHash) -// name, err := blockchain.GetAttribute(contract, "missing_attribute", nil) -// -// Expect(err).To(Equal(geth.ErrInvalidStateAttribute)) -// Expect(name).To(BeNil()) -// }) -// }) -// -//}) +import ( + "math/big" + + cfg "github.com/8thlight/vulcanizedb/pkg/config" + "github.com/8thlight/vulcanizedb/pkg/geth" + "github.com/8thlight/vulcanizedb/pkg/geth/testing" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Reading contracts", func() { + + Describe("Reading the list of attributes", func() { + It("returns a string attribute for a real contract", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, err := blockchain.GetContract(contractHash) + //contractAttributes, _ := blockchain.GetContractAttributes(contractHash) + + Expect(err).To(BeNil()) + Expect(len(contract.Attributes)).NotTo(Equal(0)) + symbolAttribute := *testing.FindAttribute(contract.Attributes, "symbol") + Expect(symbolAttribute.Name).To(Equal("symbol")) + Expect(symbolAttribute.Type).To(Equal("string")) + }) + + It("does not return an attribute that takes an input", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, err := blockchain.GetContract(contractHash) + + Expect(err).To(BeNil()) + attribute := testing.FindAttribute(contract.Attributes, "balanceOf") + Expect(attribute).To(BeNil()) + }) + + It("does not return an attribute that is not constant", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, err := blockchain.GetContract(contractHash) + + Expect(err).To(BeNil()) + attribute := testing.FindAttribute(contract.Attributes, "unpause") + Expect(attribute).To(BeNil()) + }) + }) + + Describe("Getting a contract attribute", func() { + It("returns the correct attribute for a real contract", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, _ := blockchain.GetContract(contractHash) + name, err := blockchain.GetAttribute(contract, "name", nil) + + Expect(err).To(BeNil()) + Expect(name).To(Equal("OMGToken")) + }) + + It("returns the correct attribute for a real contract", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, _ := blockchain.GetContract(contractHash) + name, err := blockchain.GetAttribute(contract, "name", nil) + + Expect(err).To(BeNil()) + Expect(name).To(Equal("OMGToken")) + }) + + It("returns the correct attribute for a real contract at a specific block height", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, _ := blockchain.GetContract(contractHash) + name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4652791)) + + Expect(name).To(Equal("OMGToken")) + Expect(err).To(BeNil()) + }) + + It("returns an error when there is no ABI for the given contract", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "MISSINGHASH" + + contract, _ := blockchain.GetContract(contractHash) + name, err := blockchain.GetAttribute(contract, "name", nil) + + Expect(err).To(Equal(geth.ErrMissingAbiFile)) + Expect(name).To(BeNil()) + }) + + It("returns an error when asking for an attribute that does not exist", func() { + config, _ := cfg.NewConfig("public") + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" + + contract, _ := blockchain.GetContract(contractHash) + name, err := blockchain.GetAttribute(contract, "missing_attribute", nil) + + Expect(err).To(Equal(geth.ErrInvalidStateAttribute)) + Expect(name).To(BeNil()) + }) + }) + +}) diff --git a/pkg/repositories/in_memory.go b/pkg/repositories/in_memory.go index dfd975d7..eee1548c 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]*WatchedContract + watchedContracts map[string]*core.WatchedContract } -func (repository *InMemory) CreateWatchedContract(watchedContract WatchedContract) error { +func (repository *InMemory) CreateWatchedContract(watchedContract core.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) *WatchedContract { +func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract { watchedContract, ok := repository.watchedContracts[contractHash] if !ok { return nil @@ -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]*WatchedContract), + watchedContracts: make(map[string]*core.WatchedContract), } } diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index e36b7fa1..cbf7acb9 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 WatchedContract) error { +func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error { abi := contract.Abi var abiToInsert *string if abi != "" { @@ -52,8 +52,8 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool { return exists } -func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract { - var savedContracts []WatchedContract +func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract { + var savedContracts []core.WatchedContract contractRows, _ := repository.Db.Query( `SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash) savedContracts = repository.loadContract(contractRows) @@ -197,15 +197,15 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr return transactions } -func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContract { - var savedContracts []WatchedContract +func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract { + var savedContracts []core.WatchedContract for contractRows.Next() { var savedContractHash string var savedContractAbi string contractRows.Scan(&savedContractHash, &savedContractAbi) 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 := WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi} + savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi} savedContracts = append(savedContracts, savedContract) } return savedContracts diff --git a/pkg/repositories/repository.go b/pkg/repositories/repository.go index 93581f3b..bc246b2b 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 WatchedContract) error + CreateWatchedContract(contract core.WatchedContract) error IsWatchedContract(contractHash string) bool - FindWatchedContract(contractHash string) *WatchedContract + FindWatchedContract(contractHash string) *core.WatchedContract } diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index 766a37be..d579ad17 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(repositories.WatchedContract{Hash: "x123"}) + repository.CreateWatchedContract(core.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(repositories.WatchedContract{Hash: "x123"}) + repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) watchedContract := repository.FindWatchedContract("x123") Expect(watchedContract).ToNot(BeNil()) Expect(watchedContract.Transactions).To(BeEmpty()) @@ -243,7 +243,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) { } repository.CreateBlock(block) - repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"}) + repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) watchedContract := repository.FindWatchedContract("x123") Expect(watchedContract).ToNot(BeNil()) Expect(watchedContract.Transactions).To( @@ -254,7 +254,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) { }) It("stores the ABI of the contract", func() { - repository.CreateWatchedContract(repositories.WatchedContract{ + repository.CreateWatchedContract(core.WatchedContract{ Abi: "{\"some\": \"json\"}", Hash: "x123", }) diff --git a/pkg/repositories/watched_contract.go b/pkg/repositories/watched_contract.go deleted file mode 100644 index 14daaa62..00000000 --- a/pkg/repositories/watched_contract.go +++ /dev/null @@ -1,9 +0,0 @@ -package repositories - -import "github.com/8thlight/vulcanizedb/pkg/core" - -type WatchedContract struct { - Abi string - Hash string - Transactions []core.Transaction -} diff --git a/pkg/watched_contracts/summary.go b/pkg/watched_contracts/summary.go index beae1b08..98ec4352 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 repositories.WatchedContract, blockNumber *big.Int) ContractSummary { +func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary { contract, _ := blockchain.GetContract(watchedContract.Hash) return ContractSummary{ blockChain: blockchain, @@ -52,7 +52,7 @@ func newContractSummary(blockchain core.Blockchain, watchedContract repositories } } -func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction { +func lastTransaction(watchedContract core.WatchedContract) *core.Transaction { if len(watchedContract.Transactions) > 0 { return &watchedContract.Transactions[0] } else { diff --git a/pkg/watched_contracts/summary_test.go b/pkg/watched_contracts/summary_test.go index af8d4542..8308ad03 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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.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 := repositories.WatchedContract{Hash: "0x123"} + watchedContract := core.WatchedContract{Hash: "0x123"} repository.CreateWatchedContract(watchedContract) blockchain := fakes.NewBlockchain() blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")