Move WatchedContract back to core

This commit is contained in:
Eric Meyer 2017-12-04 15:31:39 -06:00
parent e4a05858f8
commit 5aa0bcd6ce
12 changed files with 153 additions and 153 deletions

View File

@ -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)
}

View File

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

View File

@ -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 {

View File

@ -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

View File

@ -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())
})
})
})

View File

@ -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),
}
}

View File

@ -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

View File

@ -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
}

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(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",
})

View File

@ -1,9 +0,0 @@
package repositories
import "github.com/8thlight/vulcanizedb/pkg/core"
type WatchedContract struct {
Abi string
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 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 {

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 := 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")