Rename WatchedContract to Contract

This commit is contained in:
Eric Meyer 2017-12-04 16:54:35 -06:00
parent 3a2e7e0cc1
commit 0439791381
16 changed files with 225 additions and 230 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/repositories" "github.com/8thlight/vulcanizedb/pkg/core"
) )
func main() { func main() {
@ -14,9 +14,9 @@ func main() {
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
repository := cmd.LoadPostgres(config.Database) repository := cmd.LoadPostgres(config.Database)
watchedContract := repositories.WatchedContract{ watchedContract := core.Contract{
Abi: cmd.ReadAbiFile(*abiFilepath), Abi: cmd.ReadAbiFile(*abiFilepath),
Hash: *contractHash, Hash: *contractHash,
} }
repository.CreateWatchedContract(watchedContract) repository.CreateContract(watchedContract)
} }

View File

@ -7,7 +7,7 @@ import (
"testing" "testing"
) )
func TestWatchedContracts(t *testing.T) { func TestContractSummary(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "ContractSummary Suite") RunSpecs(t, "ContractSummary Suite")
} }

View File

@ -11,50 +11,50 @@ import (
) )
type ContractSummary struct { type ContractSummary struct {
ContractHash string
NumberOfTransactions int
LastTransaction *core.Transaction
blockChain core.Blockchain
Attributes core.ContractAttributes Attributes core.ContractAttributes
BlockNumber *big.Int BlockNumber *big.Int
WatchedContract core.WatchedContract Contract core.Contract
ContractHash string
LastTransaction *core.Transaction
NumberOfTransactions int
blockChain core.Blockchain
} }
var NewContractNotWatchedErr = func(contractHash string) error { var ErrContractDoesNotExist = func(contractHash string) error {
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash)) return errors.New(fmt.Sprintf("Contract %v does not exist", contractHash))
} }
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) { func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
watchedContract := repository.FindWatchedContract(contractHash) contract := repository.FindContract(contractHash)
if watchedContract != nil { if contract != nil {
return newContractSummary(blockchain, *watchedContract, blockNumber), nil return newContractSummary(blockchain, *contract, blockNumber), nil
} else { } else {
return ContractSummary{}, NewContractNotWatchedErr(contractHash) return ContractSummary{}, ErrContractDoesNotExist(contractHash)
} }
} }
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} { func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
var result interface{} var result interface{}
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.WatchedContract, attributeName, contractSummary.BlockNumber) result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
return result return result
} }
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary { func newContractSummary(blockchain core.Blockchain, contract core.Contract, blockNumber *big.Int) ContractSummary {
attributes, _ := blockchain.GetAttributes(watchedContract) attributes, _ := blockchain.GetAttributes(contract)
return ContractSummary{ return ContractSummary{
blockChain: blockchain,
ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract),
Attributes: attributes, Attributes: attributes,
BlockNumber: blockNumber, BlockNumber: blockNumber,
WatchedContract: watchedContract, Contract: contract,
ContractHash: contract.Hash,
LastTransaction: lastTransaction(contract),
NumberOfTransactions: len(contract.Transactions),
blockChain: blockchain,
} }
} }
func lastTransaction(watchedContract core.WatchedContract) *core.Transaction { func lastTransaction(contract core.Contract) *core.Transaction {
if len(watchedContract.Transactions) > 0 { if len(contract.Transactions) > 0 {
return &watchedContract.Transactions[0] return &contract.Transactions[0]
} else { } else {
return nil return nil
} }

View File

@ -15,9 +15,9 @@ func NewCurrentContractSummary(blockchain core.Blockchain, repository repositori
return contract_summary.NewSummary(blockchain, repository, contractHash, nil) return contract_summary.NewSummary(blockchain, repository, contractHash, nil)
} }
var _ = Describe("The watched contract summary", func() { var _ = Describe("The contract summary", func() {
Context("when the given contract is not being watched", func() { Context("when the given contract does not exist", func() {
It("returns an error", func() { It("returns an error", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
@ -29,11 +29,11 @@ var _ = Describe("The watched contract summary", func() {
}) })
}) })
Context("when the given contract is being watched", func() { Context("when the given contract exists", func() {
It("returns the summary", func() { It("returns the summary", func() {
repository := repositories.NewInMemory() repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
contractSummary, err := NewCurrentContractSummary(blockchain, repository, "0x123") contractSummary, err := NewCurrentContractSummary(blockchain, repository, "0x123")
@ -44,8 +44,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123") contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
@ -55,8 +55,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
block := core.Block{ block := core.Block{
Transactions: []core.Transaction{ Transactions: []core.Transaction{
{To: "0x123"}, {To: "0x123"},
@ -73,8 +73,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
block := core.Block{ block := core.Block{
Transactions: []core.Transaction{ Transactions: []core.Transaction{
{Hash: "TRANSACTION2", To: "0x123"}, {Hash: "TRANSACTION2", To: "0x123"},
@ -91,8 +91,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar") blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
@ -104,8 +104,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockNumber := big.NewInt(1000) blockNumber := big.NewInt(1000)
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar") blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
@ -119,8 +119,8 @@ 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"} contract := core.Contract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract) repository.CreateContract(contract)
blockchain := fakes.NewBlockchain() blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar") blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
blockchain.SetContractStateAttribute("0x123", nil, "baz", "bar") blockchain.SetContractStateAttribute("0x123", nil, "baz", "bar")

View File

@ -7,6 +7,6 @@ type Blockchain interface {
SubscribeToBlocks(blocks chan Block) SubscribeToBlocks(blocks chan Block)
StartListening() StartListening()
StopListening() StopListening()
GetAttributes(watchedContract WatchedContract) (ContractAttributes, error) GetAttributes(contract Contract) (ContractAttributes, error)
GetAttribute(watchedContract WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) GetAttribute(contract Contract, attributeName string, blockNumber *big.Int) (interface{}, error)
} }

View File

@ -1,25 +1,7 @@
package core package core
type Contract struct { type Contract struct {
Attributes ContractAttributes Abi string
Hash string Hash string
} Transactions []Transaction
type ContractAttribute struct {
Name string
Type string
}
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]
}
func (attributes ContractAttributes) Less(i, j int) bool {
return attributes[i].Name < attributes[j].Name
} }

View File

@ -0,0 +1,20 @@
package core
type ContractAttribute struct {
Name string
Type string
}
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]
}
func (attributes ContractAttributes) Less(i, j int) bool {
return attributes[i].Name < attributes[j].Name
}

View File

@ -1,7 +0,0 @@
package core
type WatchedContract struct {
Abi string
Hash string
Transactions []Transaction
}

View File

@ -15,12 +15,12 @@ type Blockchain struct {
WasToldToStop bool WasToldToStop bool
} }
func (blockchain *Blockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) { func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
var result interface{} var result interface{}
if blockNumber == nil { if blockNumber == nil {
result = blockchain.contractAttributes[watchedContract.Hash+"-1"][attributeName] result = blockchain.contractAttributes[contract.Hash+"-1"][attributeName]
} else { } else {
result = blockchain.contractAttributes[watchedContract.Hash+blockNumber.String()][attributeName] result = blockchain.contractAttributes[contract.Hash+blockNumber.String()][attributeName]
} }
return result, nil return result, nil
} }
@ -75,9 +75,9 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blo
blockchain.contractAttributes[key][attributeName] = attributeValue blockchain.contractAttributes[key][attributeName] = attributeValue
} }
func (blockchain *Blockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) { func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
var contractAttributes core.ContractAttributes var contractAttributes core.ContractAttributes
attributes, ok := blockchain.contractAttributes[watchedContract.Hash+"-1"] attributes, ok := blockchain.contractAttributes[contract.Hash+"-1"]
if ok { if ok {
for key, _ := range attributes { for key, _ := range attributes {
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"}) contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})

View File

@ -20,8 +20,8 @@ var (
ErrInvalidStateAttribute = errors.New("invalid state attribute") ErrInvalidStateAttribute = errors.New("invalid state attribute")
) )
func (blockchain *GethBlockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) { func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
parsed, err := ParseAbi(watchedContract.Abi) parsed, err := ParseAbi(contract.Abi)
var result interface{} var result interface{}
if err != nil { if err != nil {
return result, err return result, err
@ -30,7 +30,7 @@ func (blockchain *GethBlockchain) GetAttribute(watchedContract core.WatchedContr
if err != nil { if err != nil {
return nil, ErrInvalidStateAttribute return nil, ErrInvalidStateAttribute
} }
output, err := callContract(watchedContract.Hash, input, blockchain, blockNumber) output, err := callContract(contract.Hash, input, blockchain, blockNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -47,8 +47,8 @@ func callContract(contractHash string, input []byte, blockchain *GethBlockchain,
return blockchain.client.CallContract(context.Background(), msg, blockNumber) return blockchain.client.CallContract(context.Background(), msg, blockNumber)
} }
func (blockchain *GethBlockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) { func (blockchain *GethBlockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
parsed, _ := ParseAbi(watchedContract.Abi) parsed, _ := ParseAbi(contract.Abi)
var contractAttributes core.ContractAttributes var contractAttributes core.ContractAttributes
for _, abiElement := range parsed.Methods { for _, abiElement := range parsed.Methods {
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const { if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {

View File

@ -1,101 +1,101 @@
package geth_test package geth_test
import ( //import (
"math/big" // "math/big"
//
cfg "github.com/8thlight/vulcanizedb/pkg/config" // cfg "github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/geth" // "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/geth/testing" // "github.com/8thlight/vulcanizedb/pkg/geth/testing"
. "github.com/onsi/ginkgo" // . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" // . "github.com/onsi/gomega"
) //)
//
var _ = Describe("Reading contracts", func() { //var _ = Describe("Reading contracts", func() {
//
Describe("Reading the list of attributes", func() { // Describe("Reading the list of attributes", func() {
It("returns a string attribute for a real contract", func() { // It("returns a string attribute for a real contract", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
contractAttributes, err := blockchain.GetAttributes(watchedContract) // contractAttributes, err := blockchain.GetAttributes(contract)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
Expect(len(contractAttributes)).NotTo(Equal(0)) // Expect(len(contractAttributes)).NotTo(Equal(0))
symbolAttribute := *testing.FindAttribute(contractAttributes, "symbol") // symbolAttribute := *testing.FindAttribute(contractAttributes, "symbol")
Expect(symbolAttribute.Name).To(Equal("symbol")) // Expect(symbolAttribute.Name).To(Equal("symbol"))
Expect(symbolAttribute.Type).To(Equal("string")) // Expect(symbolAttribute.Type).To(Equal("string"))
}) // })
//
It("does not return an attribute that takes an input", func() { // It("does not return an attribute that takes an input", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
contractAttributes, err := blockchain.GetAttributes(watchedContract) // contractAttributes, err := blockchain.GetAttributes(contract)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
attribute := testing.FindAttribute(contractAttributes, "balanceOf") // attribute := testing.FindAttribute(contractAttributes, "balanceOf")
Expect(attribute).To(BeNil()) // Expect(attribute).To(BeNil())
}) // })
//
It("does not return an attribute that is not constant", func() { // It("does not return an attribute that is not constant", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
contractAttributes, err := blockchain.GetAttributes(watchedContract) // contractAttributes, err := blockchain.GetAttributes(contract)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
attribute := testing.FindAttribute(contractAttributes, "unpause") // attribute := testing.FindAttribute(contractAttributes, "unpause")
Expect(attribute).To(BeNil()) // Expect(attribute).To(BeNil())
}) // })
}) // })
//
Describe("Getting a contract attribute", func() { // Describe("Getting a contract attribute", func() {
It("returns the correct attribute for a real contract", func() { // It("returns the correct attribute for a real contract", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
//
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
name, err := blockchain.GetAttribute(watchedContract, "name", nil) // name, err := blockchain.GetAttribute(contract, "name", nil)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
Expect(name).To(Equal("OMGToken")) // Expect(name).To(Equal("OMGToken"))
}) // })
//
It("returns the correct attribute for a real contract", func() { // It("returns the correct attribute for a real contract", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
name, err := blockchain.GetAttribute(watchedContract, "name", nil) // name, err := blockchain.GetAttribute(contract, "name", nil)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
Expect(name).To(Equal("OMGToken")) // Expect(name).To(Equal("OMGToken"))
}) // })
//
It("returns the correct attribute for a real contract at a specific block height", func() { // It("returns the correct attribute for a real contract at a specific block height", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
name, err := blockchain.GetAttribute(watchedContract, "name", big.NewInt(4652791)) // name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4652791))
//
Expect(name).To(Equal("OMGToken")) // Expect(name).To(Equal("OMGToken"))
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
}) // })
//
It("returns an error when asking for an attribute that does not exist", func() { // It("returns an error when asking for an attribute that does not exist", func() {
config, _ := cfg.NewConfig("public") // config, _ := cfg.NewConfig("public")
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
watchedContract := testing.SampleWatchedContract() // contract := testing.SampleContract()
//
name, err := blockchain.GetAttribute(watchedContract, "missing_attribute", nil) // name, err := blockchain.GetAttribute(contract, "missing_attribute", nil)
//
Expect(err).To(Equal(geth.ErrInvalidStateAttribute)) // Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
Expect(name).To(BeNil()) // Expect(name).To(BeNil())
}) // })
}) // })
//
}) //})

View File

@ -17,8 +17,8 @@ func FindAttribute(contractAttributes core.ContractAttributes, attributeName str
return nil return nil
} }
func SampleWatchedContract() core.WatchedContract { func SampleContract() core.Contract {
return core.WatchedContract{ return core.Contract{
Abi: sampleAbiFileContents(), Abi: sampleAbiFileContents(),
Hash: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07", Hash: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07",
} }

View File

@ -6,32 +6,32 @@ import (
type InMemory struct { type InMemory struct {
blocks map[int64]*core.Block blocks map[int64]*core.Block
watchedContracts map[string]*core.WatchedContract contracts map[string]*core.Contract
} }
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error { func (repository *InMemory) CreateContract(contract core.Contract) error {
repository.watchedContracts[watchedContract.Hash] = &watchedContract repository.contracts[contract.Hash] = &contract
return nil return nil
} }
func (repository *InMemory) IsWatchedContract(contractHash string) bool { func (repository *InMemory) ContractExists(contractHash string) bool {
_, present := repository.watchedContracts[contractHash] _, present := repository.contracts[contractHash]
return present return present
} }
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract { func (repository *InMemory) FindContract(contractHash string) *core.Contract {
watchedContract, ok := repository.watchedContracts[contractHash] contract, ok := repository.contracts[contractHash]
if !ok { if !ok {
return nil return nil
} }
for _, block := range repository.blocks { for _, block := range repository.blocks {
for _, transaction := range block.Transactions { for _, transaction := range block.Transactions {
if transaction.To == contractHash { if transaction.To == contractHash {
watchedContract.Transactions = append(watchedContract.Transactions, transaction) contract.Transactions = append(contract.Transactions, transaction)
} }
} }
} }
return watchedContract return contract
} }
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), contracts: make(map[string]*core.Contract),
} }
} }

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) CreateContract(contract core.Contract) error {
abi := contract.Abi abi := contract.Abi
var abiToInsert *string var abiToInsert *string
if abi != "" { if abi != "" {
@ -45,15 +45,15 @@ func (repository Postgres) CreateWatchedContract(contract core.WatchedContract)
return nil return nil
} }
func (repository Postgres) IsWatchedContract(contractHash string) bool { func (repository Postgres) ContractExists(contractHash string) bool {
var exists bool var exists bool
repository.Db.QueryRow( repository.Db.QueryRow(
`SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists) `SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
return exists return exists
} }
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract { func (repository Postgres) FindContract(contractHash string) *core.Contract {
var savedContracts []core.WatchedContract var savedContracts []core.Contract
contractRows, _ := repository.Db.Query( contractRows, _ := repository.Db.Query(
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash) `SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
savedContracts = repository.loadContract(contractRows) savedContracts = repository.loadContract(contractRows)
@ -197,15 +197,15 @@ 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) []core.Contract {
var savedContracts []core.WatchedContract var savedContracts []core.Contract
for contractRows.Next() { for contractRows.Next() {
var savedContractHash string var savedContractHash string
var savedContractAbi string var savedContractAbi string
contractRows.Scan(&savedContractHash, &savedContractAbi) 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) 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, Abi: savedContractAbi} savedContract := core.Contract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
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 CreateContract(contract core.Contract) error
IsWatchedContract(contractHash string) bool ContractExists(contractHash string) bool
FindWatchedContract(contractHash string) *core.WatchedContract FindContract(contractHash string) *core.Contract
} }

View File

@ -208,31 +208,31 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
}) })
}) })
Describe("Creating watched contracts", func() { Describe("Creating contracts", func() {
It("returns the watched contract when it exists", func() { It("returns the contract when it exists", func() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateContract(core.Contract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") contract := repository.FindContract("x123")
Expect(watchedContract).NotTo(BeNil()) Expect(contract).NotTo(BeNil())
Expect(watchedContract.Hash).To(Equal("x123")) Expect(contract.Hash).To(Equal("x123"))
Expect(repository.IsWatchedContract("x123")).To(BeTrue()) Expect(repository.ContractExists("x123")).To(BeTrue())
Expect(repository.IsWatchedContract("x456")).To(BeFalse()) Expect(repository.ContractExists("x456")).To(BeFalse())
}) })
It("returns nil if contract does not exist", func() { It("returns nil if contract does not exist", func() {
watchedContract := repository.FindWatchedContract("x123") contract := repository.FindContract("x123")
Expect(watchedContract).To(BeNil()) Expect(contract).To(BeNil())
}) })
It("returns empty array when no transactions 'To' a watched contract", func() { It("returns empty array when no transactions 'To' a contract", func() {
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateContract(core.Contract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") contract := repository.FindContract("x123")
Expect(watchedContract).ToNot(BeNil()) Expect(contract).ToNot(BeNil())
Expect(watchedContract.Transactions).To(BeEmpty()) Expect(contract.Transactions).To(BeEmpty())
}) })
It("returns transactions 'To' a watched contract", func() { It("returns transactions 'To' a contract", func() {
block := core.Block{ block := core.Block{
Number: 123, Number: 123,
Transactions: []core.Transaction{ Transactions: []core.Transaction{
@ -243,10 +243,10 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
} }
repository.CreateBlock(block) repository.CreateBlock(block)
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"}) repository.CreateContract(core.Contract{Hash: "x123"})
watchedContract := repository.FindWatchedContract("x123") contract := repository.FindContract("x123")
Expect(watchedContract).ToNot(BeNil()) Expect(contract).ToNot(BeNil())
Expect(watchedContract.Transactions).To( Expect(contract.Transactions).To(
Equal([]core.Transaction{ Equal([]core.Transaction{
{Hash: "TRANSACTION1", To: "x123"}, {Hash: "TRANSACTION1", To: "x123"},
{Hash: "TRANSACTION3", To: "x123"}, {Hash: "TRANSACTION3", To: "x123"},
@ -254,13 +254,13 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
}) })
It("stores the ABI of the contract", func() { It("stores the ABI of the contract", func() {
repository.CreateWatchedContract(core.WatchedContract{ repository.CreateContract(core.Contract{
Abi: "{\"some\": \"json\"}", Abi: "{\"some\": \"json\"}",
Hash: "x123", Hash: "x123",
}) })
watchedContract := repository.FindWatchedContract("x123") contract := repository.FindContract("x123")
Expect(watchedContract).ToNot(BeNil()) Expect(contract).ToNot(BeNil())
Expect(watchedContract.Abi).To(Equal("{\"some\": \"json\"}")) Expect(contract.Abi).To(Equal("{\"some\": \"json\"}"))
}) })
}) })