Merge pull request #83 from 8thlight/refactor-two

Refactor Blockchain interface
This commit is contained in:
Matt K 2017-11-30 16:49:29 -06:00 committed by GitHub
commit 7a11d3c50f
6 changed files with 74 additions and 42 deletions

View File

@ -5,6 +5,6 @@ type Blockchain interface {
SubscribeToBlocks(blocks chan Block) SubscribeToBlocks(blocks chan Block)
StartListening() StartListening()
StopListening() StopListening()
GetContractAttributes(contractHash string) (ContractAttributes, 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

@ -13,20 +13,17 @@ type Blockchain struct {
WasToldToStop bool WasToldToStop bool
} }
func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) {
var contractAttributes core.ContractAttributes contractAttributes, err := blockchain.getContractAttributes(contractHash)
attributes, ok := blockchain.contractAttributes[contractHash] contract := core.Contract{
if ok { Attributes: contractAttributes,
for key, _ := range attributes { Hash: contractHash,
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
}
} }
sort.Sort(contractAttributes) return contract, err
return contractAttributes, nil
} }
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
} }
@ -73,3 +70,15 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, att
} }
blockchain.contractAttributes[contractHash][attributeName] = attributeValue blockchain.contractAttributes[contractHash][attributeName] = attributeValue
} }
func (blockchain *Blockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
var contractAttributes core.ContractAttributes
attributes, ok := blockchain.contractAttributes[contractHash]
if ok {
for key, _ := range attributes {
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
}
}
sort.Sort(contractAttributes)
return contractAttributes, nil
}

View File

@ -17,7 +17,33 @@ var (
ErrInvalidStateAttribute = errors.New("invalid state attribute") ErrInvalidStateAttribute = errors.New("invalid state attribute")
) )
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contract, error) {
attributes, err := blockchain.getContractAttributes(contractHash)
if err != nil {
return core.Contract{}, err
} else {
contract := core.Contract{
Attributes: attributes,
Hash: contractHash,
}
return contract, nil
}
}
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
boundContract, err := bindContract(common.HexToAddress(contract.Hash), blockchain.client, blockchain.client)
if err != nil {
return nil, err
}
var result interface{}
err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
if err != nil {
return nil, ErrInvalidStateAttribute
}
return result, nil
}
func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash)) abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
parsed, _ := ParseAbiFile(abiFilePath) parsed, _ := ParseAbiFile(abiFilePath)
var contractAttributes core.ContractAttributes var contractAttributes core.ContractAttributes
@ -31,19 +57,6 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co
return contractAttributes, nil return contractAttributes, nil
} }
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client)
if err != nil {
return nil, err
}
var result interface{}
err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
if err != nil {
return nil, ErrInvalidStateAttribute
}
return result, nil
}
func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) { func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex())) abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
parsed, err := ParseAbiFile(abiFilePath) parsed, err := ParseAbiFile(abiFilePath)

View File

@ -16,11 +16,11 @@ package geth_test
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
// //
// attributes, err := blockchain.GetContractAttributes(contractHash) // contract, err := blockchain.GetContract(contractHash)
// //
// Expect(err).To(BeNil()) // Expect(err).To(BeNil())
// Expect(len(attributes)).NotTo(Equal(0)) // Expect(len(contract.Attributes)).NotTo(Equal(0))
// symbolAttribute := *testing.FindAttribute(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"))
// }) // })
@ -30,10 +30,10 @@ package geth_test
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
// //
// attributes, err := blockchain.GetContractAttributes(contractHash) // contract, err := blockchain.GetContract(contractHash)
// //
// Expect(err).To(BeNil()) // Expect(err).To(BeNil())
// attribute := testing.FindAttribute(attributes, "balanceOf") // attribute := testing.FindAttribute(contract.Attributes, "balanceOf")
// Expect(attribute).To(BeNil()) // Expect(attribute).To(BeNil())
// }) // })
// //
@ -42,10 +42,10 @@ package geth_test
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
// //
// attributes, err := blockchain.GetContractAttributes(contractHash) // contract, err := blockchain.GetContract(contractHash)
// //
// Expect(err).To(BeNil()) // Expect(err).To(BeNil())
// attribute := testing.FindAttribute(attributes, "unpause") // attribute := testing.FindAttribute(contract.Attributes, "unpause")
// Expect(attribute).To(BeNil()) // Expect(attribute).To(BeNil())
// }) // })
// }) // })
@ -56,10 +56,11 @@ package geth_test
// 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() { // It("returns an error when there is no ABI for the given contract", func() {
@ -67,10 +68,11 @@ package geth_test
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "MISSINGHASH" // contractHash := "MISSINGHASH"
// //
// name, err := blockchain.GetContractStateAttribute(contractHash, "name") // contract, _ := blockchain.GetContract(contractHash)
// name, err := blockchain.GetAttribute(contract, "name")
// //
// Expect(name).To(BeNil())
// 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() { // It("returns an error when asking for an attribute that does not exist", func() {
@ -78,7 +80,8 @@ package geth_test
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) // blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" // contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
// //
// name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute") // contract, _ := blockchain.GetContract(contractHash)
// name, err := blockchain.GetAttribute(contract, "missing_attribute")
// //
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute)) // Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
// Expect(name).To(BeNil()) // 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,18 +31,19 @@ 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
} }
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary { func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
attributes, _ := blockchain.GetContractAttributes(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),
Attributes: attributes, Attributes: contract.Attributes,
} }
} }