Add function to get string state attributes for a given contract

This commit is contained in:
Eric Meyer 2017-11-27 09:39:53 -06:00
parent 9c1051bfce
commit 708ad114ac
12 changed files with 205 additions and 16 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@
Gododir/godobin-*
test_data_dir/
vendor/
contracts/*
environments/*.toml

View File

@ -17,9 +17,9 @@ func main() {
environment := flag.String("environment", "", "Environment name")
flag.Parse()
config := cmd.LoadConfig(*environment)
fmt.Println("Client Path ", config.Client.IPCPath)
repository := repositories.NewPostgres(config.Database)
fmt.Printf("Creating Geth Blockchain to: %s\n", config.Client.IPCPath)
listener := blockchain_listener.NewBlockchainListener(
geth.NewGethBlockchain(config.Client.IPCPath),
[]core.BlockchainObserver{

View File

@ -5,4 +5,5 @@ type Blockchain interface {
SubscribeToBlocks(blocks chan Block)
StartListening()
StopListening()
GetContractStateAttribute(contractHash string, attributeName string) (*string, error)
}

View File

@ -4,12 +4,22 @@ import "github.com/8thlight/vulcanizedb/pkg/core"
type Blockchain struct {
blocks map[int64]core.Block
contractAttributes map[string]map[string]string
blocksChannel chan core.Block
WasToldToStop bool
}
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {
result := new(string)
*result = blockchain.contractAttributes[contractHash][attributeName]
return result, nil
}
func NewBlockchain() *Blockchain {
return &Blockchain{blocks: make(map[int64]core.Block)}
return &Blockchain{
blocks: make(map[int64]core.Block),
contractAttributes: make(map[string]map[string]string),
}
}
func NewBlockchainWithBlocks(blocks []core.Block) *Blockchain {
@ -40,3 +50,11 @@ func (*Blockchain) StartListening() {}
func (blockchain *Blockchain) StopListening() {
blockchain.WasToldToStop = true
}
func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, attributeName string, attributeValue string) {
contractStateAttributes := blockchain.contractAttributes[contractHash]
if contractStateAttributes == nil {
blockchain.contractAttributes[contractHash] = make(map[string]string)
}
blockchain.contractAttributes[contractHash][attributeName] = attributeValue
}

27
pkg/geth/abi.go Normal file
View File

@ -0,0 +1,27 @@
package geth
import (
"errors"
"io/ioutil"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
)
var (
ErrInvalidAbiFile = errors.New("invalid abi")
ErrMissingAbiFile = errors.New("missing abi")
)
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
filesBytes, err := ioutil.ReadFile(abiFilePath)
if err != nil {
return abi.ABI{}, ErrMissingAbiFile
}
abiString := string(filesBytes)
parsedAbi, err := abi.JSON(strings.NewReader(abiString))
if err != nil {
return abi.ABI{}, ErrInvalidAbiFile
}
return parsedAbi, nil
}

43
pkg/geth/abi_test.go Normal file
View File

@ -0,0 +1,43 @@
package geth_test
import (
"path/filepath"
cfg "github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/ethereum/go-ethereum/accounts/abi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Reading ABI files", func() {
It("loads a valid ABI file", func() {
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", contractHash+".json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).NotTo(BeNil())
Expect(err).To(BeNil())
})
It("returns an error when the file does not exist", func() {
path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", "missing_file.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrMissingAbiFile))
})
It("returns an error when the file has invalid contents", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "invalid_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrInvalidAbiFile))
})
})

37
pkg/geth/contract.go Normal file
View File

@ -0,0 +1,37 @@
package geth
import (
"errors"
"fmt"
"path/filepath"
"github.com/8thlight/vulcanizedb/pkg/config"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
var (
ErrInvalidStateAttribute = errors.New("invalid state attribute")
)
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {
boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client)
if err != nil {
return nil, err
}
result := new(string)
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) {
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
parsed, err := ParseAbiFile(abiFilePath)
if err != nil {
return nil, err
}
return bind.NewBoundContract(address, parsed, caller, transactor), nil
}

47
pkg/geth/contract_test.go Normal file
View File

@ -0,0 +1,47 @@
package geth_test
//import (
// cfg "github.com/8thlight/vulcanizedb/pkg/config"
// "github.com/8thlight/vulcanizedb/pkg/geth"
// . "github.com/onsi/ginkgo"
// . "github.com/onsi/gomega"
//)
//
//var _ = Describe("The Geth blockchain", func() {
//
// 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"
//
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
//
// 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"
//
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
//
// Expect(name).To(BeNil())
// Expect(err).To(Equal(geth.ErrMissingAbiFile))
// })
//
// 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"
//
// name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute")
//
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
// Expect(name).To(BeNil())
// })
// })
//
//})

View File

@ -25,7 +25,6 @@ func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block
}
func NewGethBlockchain(ipcPath string) *GethBlockchain {
fmt.Printf("Creating Geth Blockchain to: %s\n", ipcPath)
blockchain := GethBlockchain{}
client, _ := ethclient.Dial(ipcPath)
blockchain.client = client

View File

@ -0,0 +1 @@
bad json

View File

@ -12,30 +12,33 @@ type ContractSummary struct {
ContractHash string
NumberOfTransactions int
LastTransaction *core.Transaction
blockChain core.Blockchain
}
var NewContractNotWatchedErr = func(contractHash string) error {
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
}
func NewSummary(_ core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
contract := repository.FindWatchedContract(contractHash)
if contract != nil {
return newContractSummary(*contract), nil
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
watchedContract := repository.FindWatchedContract(contractHash)
if watchedContract != nil {
return newContractSummary(blockchain, *watchedContract), nil
} else {
return nil, NewContractNotWatchedErr(contractHash)
}
}
func (ContractSummary) GetStateAttribute(attributeName string) string {
return "Hello world"
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) string {
result, _ := contractSummary.blockChain.GetContractStateAttribute(contractSummary.ContractHash, attributeName)
return *result
}
func newContractSummary(contract core.WatchedContract) *ContractSummary {
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
return &ContractSummary{
ContractHash: contract.Hash,
NumberOfTransactions: len(contract.Transactions),
LastTransaction: lastTransaction(contract),
blockChain: blockchain,
ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract),
}
}

View File

@ -82,6 +82,18 @@ var _ bool = Describe("The watched contract summary", func() {
Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2"))
})
It("gets a state attribute for the contract from the blockchain", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
blockchain.SetContractStateAttribute("0x123", "foo", "bar")
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.GetStateAttribute("foo")).To(Equal("bar"))
})
})
})