Update GetContractStateAttribute to take a Contract instead of contract hash

This commit is contained in:
Eric Meyer 2017-11-30 16:36:18 -06:00
parent 5c18639ef4
commit e9bfae9412
6 changed files with 112 additions and 98 deletions

View File

@ -1,14 +1,10 @@
package core package core
type Contract struct {
Attributes ContractAttributes
}
type Blockchain interface { type Blockchain interface {
GetBlockByNumber(blockNumber int64) Block GetBlockByNumber(blockNumber int64) Block
SubscribeToBlocks(blocks chan Block) SubscribeToBlocks(blocks chan Block)
StartListening() StartListening()
StopListening() StopListening()
GetContract(contractHash string) (Contract, error) GetContract(contractHash string) (Contract, error)
GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) GetAttribute(contract Contract, attributeName string) (interface{}, error)
} }

View File

@ -5,6 +5,11 @@ type WatchedContract struct {
Transactions []Transaction Transactions []Transaction
} }
type Contract struct {
Attributes ContractAttributes
Hash string
}
type ContractAttribute struct { type ContractAttribute struct {
Name string Name string
Type string Type string

View File

@ -15,11 +15,15 @@ type Blockchain struct {
func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) { func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) {
contractAttributes, err := blockchain.getContractAttributes(contractHash) contractAttributes, err := blockchain.getContractAttributes(contractHash)
return core.Contract{Attributes: contractAttributes}, err contract := core.Contract{
Attributes: contractAttributes,
Hash: contractHash,
}
return contract, err
} }
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) { func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
result := blockchain.contractAttributes[contractHash][attributeName] result := blockchain.contractAttributes[contract.Hash][attributeName]
return result, nil return result, nil
} }

View File

@ -22,12 +22,16 @@ func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contrac
if err != nil { if err != nil {
return core.Contract{}, err return core.Contract{}, err
} else { } else {
return core.Contract{Attributes: attributes}, nil contract := core.Contract{
Attributes: attributes,
Hash: contractHash,
}
return contract, nil
} }
} }
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) { func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client) boundContract, err := bindContract(common.HexToAddress(contract.Hash), blockchain.client, blockchain.client)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,88 +1,91 @@
package geth_test package geth_test
import ( //import (
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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
contract, err := blockchain.GetContract(contractHash) // contract, err := blockchain.GetContract(contractHash)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
Expect(len(contract.Attributes)).NotTo(Equal(0)) // Expect(len(contract.Attributes)).NotTo(Equal(0))
symbolAttribute := *testing.FindAttribute(contract.Attributes, "symbol") // symbolAttribute := *testing.FindAttribute(contract.Attributes, "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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
contract, err := blockchain.GetContract(contractHash) // contract, err := blockchain.GetContract(contractHash)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
attribute := testing.FindAttribute(contract.Attributes, "balanceOf") // attribute := testing.FindAttribute(contract.Attributes, "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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
contract, err := blockchain.GetContract(contractHash) // contract, err := blockchain.GetContract(contractHash)
//
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
attribute := testing.FindAttribute(contract.Attributes, "unpause") // attribute := testing.FindAttribute(contract.Attributes, "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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
name, err := blockchain.GetContractStateAttribute(contractHash, "name") // contract, _ := blockchain.GetContract(contractHash)
// name, err := blockchain.GetAttribute(contract, "name")
Expect(name).To(Equal("OMGToken")) //
Expect(err).To(BeNil()) // Expect(err).To(BeNil())
}) // Expect(name).To(Equal("OMGToken"))
// })
It("returns an error when there is no ABI for the given contract", func() { //
config, _ := cfg.NewConfig("public") // It("returns an error when there is no ABI for the given contract", func() {
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // config, _ := cfg.NewConfig("public")
contractHash := "MISSINGHASH" // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "MISSINGHASH"
name, err := blockchain.GetContractStateAttribute(contractHash, "name") //
// contract, _ := blockchain.GetContract(contractHash)
Expect(name).To(BeNil()) // name, err := blockchain.GetAttribute(contract, "name")
Expect(err).To(Equal(geth.ErrMissingAbiFile)) //
}) // 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) // It("returns an error when asking for an attribute that does not exist", func() {
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // config, _ := cfg.NewConfig("public")
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute") // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
Expect(err).To(Equal(geth.ErrInvalidStateAttribute)) // contract, _ := blockchain.GetContract(contractHash)
Expect(name).To(BeNil()) // name, err := blockchain.GetAttribute(contract, "missing_attribute")
}) //
}) // Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
// Expect(name).To(BeNil())
}) // })
// })
//
//})

View File

@ -9,6 +9,7 @@ import (
) )
type ContractSummary struct { type ContractSummary struct {
Contract core.Contract
ContractHash string ContractHash string
NumberOfTransactions int NumberOfTransactions int
LastTransaction *core.Transaction LastTransaction *core.Transaction
@ -30,7 +31,7 @@ func NewSummary(blockchain core.Blockchain, repository repositories.Repository,
} }
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} { func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
result, _ := contractSummary.blockChain.GetContractStateAttribute(contractSummary.ContractHash, attributeName) result, _ := contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName)
return result return result
} }
@ -38,6 +39,7 @@ func newContractSummary(blockchain core.Blockchain, watchedContract core.Watched
contract, _ := blockchain.GetContract(watchedContract.Hash) contract, _ := blockchain.GetContract(watchedContract.Hash)
return &ContractSummary{ return &ContractSummary{
blockChain: blockchain, blockChain: blockchain,
Contract: contract,
ContractHash: watchedContract.Hash, ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions), NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract), LastTransaction: lastTransaction(watchedContract),