Update Blockchain interface to use WatchedContract instead of Contract

This commit is contained in:
Eric Meyer 2017-12-04 16:35:41 -06:00
parent e432219e20
commit 3a2e7e0cc1
9 changed files with 86 additions and 100 deletions

View File

@ -11,13 +11,13 @@ import (
) )
type ContractSummary struct { type ContractSummary struct {
Contract core.Contract
ContractHash string ContractHash string
NumberOfTransactions int NumberOfTransactions int
LastTransaction *core.Transaction LastTransaction *core.Transaction
blockChain core.Blockchain blockChain core.Blockchain
Attributes core.ContractAttributes Attributes core.ContractAttributes
BlockNumber *big.Int BlockNumber *big.Int
WatchedContract core.WatchedContract
} }
var NewContractNotWatchedErr = func(contractHash string) error { var NewContractNotWatchedErr = func(contractHash string) error {
@ -35,20 +35,20 @@ func NewSummary(blockchain core.Blockchain, repository repositories.Repository,
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.Contract, attributeName, contractSummary.BlockNumber) result, _ = contractSummary.blockChain.GetAttribute(contractSummary.WatchedContract, attributeName, contractSummary.BlockNumber)
return result return result
} }
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary { func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary {
contract, _ := blockchain.GetContract(watchedContract.Hash) attributes, _ := blockchain.GetAttributes(watchedContract)
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: contract.Attributes, Attributes: attributes,
BlockNumber: blockNumber, BlockNumber: blockNumber,
WatchedContract: watchedContract,
} }
} }

View File

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

View File

@ -15,21 +15,12 @@ type Blockchain struct {
WasToldToStop bool WasToldToStop bool
} }
func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) { func (blockchain *Blockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) {
contractAttributes, err := blockchain.GetContractAttributes(contractHash)
contract := core.Contract{
Attributes: contractAttributes,
Hash: contractHash,
}
return contract, err
}
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[contract.Hash+"-1"][attributeName] result = blockchain.contractAttributes[watchedContract.Hash+"-1"][attributeName]
} else { } else {
result = blockchain.contractAttributes[contract.Hash+blockNumber.String()][attributeName] result = blockchain.contractAttributes[watchedContract.Hash+blockNumber.String()][attributeName]
} }
return result, nil return result, nil
} }
@ -84,9 +75,9 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blo
blockchain.contractAttributes[key][attributeName] = attributeValue blockchain.contractAttributes[key][attributeName] = attributeValue
} }
func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { func (blockchain *Blockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) {
var contractAttributes core.ContractAttributes var contractAttributes core.ContractAttributes
attributes, ok := blockchain.contractAttributes[contractHash+"-1"] attributes, ok := blockchain.contractAttributes[watchedContract.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

@ -18,6 +18,10 @@ func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
if err != nil { if err != nil {
return abi.ABI{}, ErrMissingAbiFile return abi.ABI{}, ErrMissingAbiFile
} }
return ParseAbi(abiString)
}
func ParseAbi(abiString string) (abi.ABI, error) {
parsedAbi, err := abi.JSON(strings.NewReader(abiString)) parsedAbi, err := abi.JSON(strings.NewReader(abiString))
if err != nil { if err != nil {
return abi.ABI{}, ErrInvalidAbiFile return abi.ABI{}, ErrInvalidAbiFile

View File

@ -13,7 +13,6 @@ import (
"github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -21,30 +20,8 @@ var (
ErrInvalidStateAttribute = errors.New("invalid state attribute") ErrInvalidStateAttribute = errors.New("invalid state attribute")
) )
func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contract, error) { func (blockchain *GethBlockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) {
attributes, err := blockchain.GetContractAttributes(contractHash) parsed, err := ParseAbi(watchedContract.Abi)
if err != nil {
return core.Contract{}, err
} else {
contract := core.Contract{
Attributes: attributes,
Hash: contractHash,
}
return contract, nil
}
}
func (blockchain *GethBlockchain) getParseAbi(contract core.Contract) (abi.ABI, error) {
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contract.Hash))
parsed, err := ParseAbiFile(abiFilePath)
if err != nil {
return abi.ABI{}, err
}
return parsed, nil
}
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
parsed, err := blockchain.getParseAbi(contract)
var result interface{} var result interface{}
if err != nil { if err != nil {
return result, err return result, err
@ -53,7 +30,7 @@ func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attribute
if err != nil { if err != nil {
return nil, ErrInvalidStateAttribute return nil, ErrInvalidStateAttribute
} }
output, err := callContract(contract, input, err, blockchain, blockNumber) output, err := callContract(watchedContract.Hash, input, blockchain, blockNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,13 +40,27 @@ func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attribute
} }
return result, nil return result, nil
} }
func callContract(contract core.Contract, input []byte, err error, blockchain *GethBlockchain, blockNumber *big.Int) ([]byte, error) {
to := common.HexToAddress(contract.Hash) func callContract(contractHash string, input []byte, blockchain *GethBlockchain, blockNumber *big.Int) ([]byte, error) {
to := common.HexToAddress(contractHash)
msg := ethereum.CallMsg{To: &to, Data: input} msg := ethereum.CallMsg{To: &to, Data: input}
return blockchain.client.CallContract(context.Background(), msg, blockNumber) return blockchain.client.CallContract(context.Background(), msg, blockNumber)
} }
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { func (blockchain *GethBlockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) {
parsed, _ := ParseAbi(watchedContract.Abi)
var contractAttributes core.ContractAttributes
for _, abiElement := range parsed.Methods {
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {
attributeType := abiElement.Outputs[0].Type.String()
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
}
}
sort.Sort(contractAttributes)
return contractAttributes, nil
}
func (blockchain *GethBlockchain) GetContractAttributesOld(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

View File

@ -16,14 +16,13 @@ var _ = Describe("Reading contracts", 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" watchedContract := testing.SampleWatchedContract()
contract, err := blockchain.GetContract(contractHash) contractAttributes, err := blockchain.GetAttributes(watchedContract)
//contractAttributes, _ := blockchain.GetContractAttributes(contractHash)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(len(contract.Attributes)).NotTo(Equal(0)) Expect(len(contractAttributes)).NotTo(Equal(0))
symbolAttribute := *testing.FindAttribute(contract.Attributes, "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"))
}) })
@ -31,24 +30,24 @@ var _ = Describe("Reading contracts", func() {
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" watchedContract := testing.SampleWatchedContract()
contract, err := blockchain.GetContract(contractHash) contractAttributes, err := blockchain.GetAttributes(watchedContract)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
attribute := testing.FindAttribute(contract.Attributes, "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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" watchedContract := testing.SampleWatchedContract()
contract, err := blockchain.GetContract(contractHash) contractAttributes, err := blockchain.GetAttributes(watchedContract)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
attribute := testing.FindAttribute(contract.Attributes, "unpause") attribute := testing.FindAttribute(contractAttributes, "unpause")
Expect(attribute).To(BeNil()) Expect(attribute).To(BeNil())
}) })
}) })
@ -57,10 +56,9 @@ var _ = Describe("Reading contracts", 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"
contract, _ := blockchain.GetContract(contractHash) watchedContract := testing.SampleWatchedContract()
name, err := blockchain.GetAttribute(contract, "name", nil) name, err := blockchain.GetAttribute(watchedContract, "name", nil)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(name).To(Equal("OMGToken")) Expect(name).To(Equal("OMGToken"))
@ -69,10 +67,9 @@ var _ = Describe("Reading contracts", 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" watchedContract := testing.SampleWatchedContract()
contract, _ := blockchain.GetContract(contractHash) 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"))
@ -81,34 +78,20 @@ var _ = Describe("Reading contracts", func() {
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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" watchedContract := testing.SampleWatchedContract()
contract, _ := blockchain.GetContract(contractHash) 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 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() { 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)
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" watchedContract := testing.SampleWatchedContract()
contract, _ := blockchain.GetContract(contractHash) 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

@ -1,14 +0,0 @@
package testing
import (
"github.com/8thlight/vulcanizedb/pkg/core"
)
func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute {
for _, contractAttribute := range contractAttributes {
if contractAttribute.Name == attributeName {
return &contractAttribute
}
}
return nil
}

View File

@ -0,0 +1,31 @@
package testing
import (
"path/filepath"
"github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth"
)
func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute {
for _, contractAttribute := range contractAttributes {
if contractAttribute.Name == attributeName {
return &contractAttribute
}
}
return nil
}
func SampleWatchedContract() core.WatchedContract {
return core.WatchedContract{
Abi: sampleAbiFileContents(),
Hash: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07",
}
}
func sampleAbiFileContents() string {
abiFilepath := filepath.Join(config.ProjectRoot(), "pkg", "geth", "testing", "sample_abi.json")
abiFileContents, _ := geth.ReadAbiFile(abiFilepath)
return abiFileContents
}

View File

@ -0,0 +1 @@
[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]