Add function to get string state attributes for a given contract
This commit is contained in:
parent
9c1051bfce
commit
708ad114ac
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,5 @@
|
|||||||
Gododir/godobin-*
|
Gododir/godobin-*
|
||||||
test_data_dir/
|
test_data_dir/
|
||||||
vendor/
|
vendor/
|
||||||
|
contracts/*
|
||||||
environments/*.toml
|
environments/*.toml
|
||||||
|
@ -17,9 +17,9 @@ func main() {
|
|||||||
environment := flag.String("environment", "", "Environment name")
|
environment := flag.String("environment", "", "Environment name")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config := cmd.LoadConfig(*environment)
|
config := cmd.LoadConfig(*environment)
|
||||||
fmt.Println("Client Path ", config.Client.IPCPath)
|
|
||||||
|
|
||||||
repository := repositories.NewPostgres(config.Database)
|
repository := repositories.NewPostgres(config.Database)
|
||||||
|
fmt.Printf("Creating Geth Blockchain to: %s\n", config.Client.IPCPath)
|
||||||
listener := blockchain_listener.NewBlockchainListener(
|
listener := blockchain_listener.NewBlockchainListener(
|
||||||
geth.NewGethBlockchain(config.Client.IPCPath),
|
geth.NewGethBlockchain(config.Client.IPCPath),
|
||||||
[]core.BlockchainObserver{
|
[]core.BlockchainObserver{
|
||||||
|
@ -5,4 +5,5 @@ type Blockchain interface {
|
|||||||
SubscribeToBlocks(blocks chan Block)
|
SubscribeToBlocks(blocks chan Block)
|
||||||
StartListening()
|
StartListening()
|
||||||
StopListening()
|
StopListening()
|
||||||
|
GetContractStateAttribute(contractHash string, attributeName string) (*string, error)
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,22 @@ import "github.com/8thlight/vulcanizedb/pkg/core"
|
|||||||
|
|
||||||
type Blockchain struct {
|
type Blockchain struct {
|
||||||
blocks map[int64]core.Block
|
blocks map[int64]core.Block
|
||||||
|
contractAttributes map[string]map[string]string
|
||||||
blocksChannel chan core.Block
|
blocksChannel chan core.Block
|
||||||
WasToldToStop bool
|
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 {
|
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 {
|
func NewBlockchainWithBlocks(blocks []core.Block) *Blockchain {
|
||||||
@ -40,3 +50,11 @@ func (*Blockchain) StartListening() {}
|
|||||||
func (blockchain *Blockchain) StopListening() {
|
func (blockchain *Blockchain) StopListening() {
|
||||||
blockchain.WasToldToStop = true
|
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
27
pkg/geth/abi.go
Normal 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
43
pkg/geth/abi_test.go
Normal 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
37
pkg/geth/contract.go
Normal 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
47
pkg/geth/contract_test.go
Normal 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())
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
//})
|
@ -25,7 +25,6 @@ func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
||||||
fmt.Printf("Creating Geth Blockchain to: %s\n", ipcPath)
|
|
||||||
blockchain := GethBlockchain{}
|
blockchain := GethBlockchain{}
|
||||||
client, _ := ethclient.Dial(ipcPath)
|
client, _ := ethclient.Dial(ipcPath)
|
||||||
blockchain.client = client
|
blockchain.client = client
|
||||||
|
1
pkg/geth/testing/invalid_abi.json
Normal file
1
pkg/geth/testing/invalid_abi.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
bad json
|
@ -12,30 +12,33 @@ type ContractSummary struct {
|
|||||||
ContractHash string
|
ContractHash string
|
||||||
NumberOfTransactions int
|
NumberOfTransactions int
|
||||||
LastTransaction *core.Transaction
|
LastTransaction *core.Transaction
|
||||||
|
blockChain core.Blockchain
|
||||||
}
|
}
|
||||||
|
|
||||||
var NewContractNotWatchedErr = func(contractHash string) error {
|
var NewContractNotWatchedErr = func(contractHash string) error {
|
||||||
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
|
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSummary(_ core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
|
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
|
||||||
contract := repository.FindWatchedContract(contractHash)
|
watchedContract := repository.FindWatchedContract(contractHash)
|
||||||
if contract != nil {
|
if watchedContract != nil {
|
||||||
return newContractSummary(*contract), nil
|
return newContractSummary(blockchain, *watchedContract), nil
|
||||||
} else {
|
} else {
|
||||||
return nil, NewContractNotWatchedErr(contractHash)
|
return nil, NewContractNotWatchedErr(contractHash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ContractSummary) GetStateAttribute(attributeName string) string {
|
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) string {
|
||||||
return "Hello world"
|
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{
|
return &ContractSummary{
|
||||||
ContractHash: contract.Hash,
|
blockChain: blockchain,
|
||||||
NumberOfTransactions: len(contract.Transactions),
|
ContractHash: watchedContract.Hash,
|
||||||
LastTransaction: lastTransaction(contract),
|
NumberOfTransactions: len(watchedContract.Transactions),
|
||||||
|
LastTransaction: lastTransaction(watchedContract),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,6 +82,18 @@ var _ bool = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2"))
|
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"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user