From 60bef69113916770c2ef3ad25232d089372c8377 Mon Sep 17 00:00:00 2001 From: Eric Meyer Date: Mon, 27 Nov 2017 16:18:42 -0600 Subject: [PATCH] Start reading available attributes from ABI --- pkg/core/blockchain.go | 1 + pkg/core/watched_contract.go | 5 ++ pkg/fakes/blockchain.go | 4 ++ pkg/geth/abi_test.go | 5 +- pkg/geth/contract.go | 19 ++++++++ pkg/geth/contract_test.go | 55 +++++++++++++++++++++- pkg/geth/testing/contract_attributes.go | 14 ++++++ pkg/geth/testing/valid_abi.json | 1 + pkg/watched_contracts/console_presenter.go | 20 ++++++-- 9 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 pkg/geth/testing/contract_attributes.go create mode 100644 pkg/geth/testing/valid_abi.json diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index ced5c5da..c7ba3432 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -5,5 +5,6 @@ type Blockchain interface { SubscribeToBlocks(blocks chan Block) StartListening() StopListening() + GetContractAttributes(contractHash string) ([]ContractAttribute, error) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) } diff --git a/pkg/core/watched_contract.go b/pkg/core/watched_contract.go index 8547d20d..b3e50218 100644 --- a/pkg/core/watched_contract.go +++ b/pkg/core/watched_contract.go @@ -4,3 +4,8 @@ type WatchedContract struct { Hash string Transactions []Transaction } + +type ContractAttribute struct { + Name string + Type string +} diff --git a/pkg/fakes/blockchain.go b/pkg/fakes/blockchain.go index c45da542..c8b8ae8d 100644 --- a/pkg/fakes/blockchain.go +++ b/pkg/fakes/blockchain.go @@ -9,6 +9,10 @@ type Blockchain struct { WasToldToStop bool } +func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) { + panic("implement me") +} + func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) { result := new(string) *result = blockchain.contractAttributes[contractHash][attributeName] diff --git a/pkg/geth/abi_test.go b/pkg/geth/abi_test.go index bf89a165..d12ea9fc 100644 --- a/pkg/geth/abi_test.go +++ b/pkg/geth/abi_test.go @@ -13,8 +13,7 @@ import ( var _ = Describe("Reading ABI files", func() { It("loads a valid ABI file", func() { - contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" - path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", contractHash+".json") + path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json") contractAbi, err := geth.ParseAbiFile(path) @@ -23,7 +22,7 @@ var _ = Describe("Reading ABI files", func() { }) It("returns an error when the file does not exist", func() { - path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", "missing_file.json") + path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json") contractAbi, err := geth.ParseAbiFile(path) diff --git a/pkg/geth/contract.go b/pkg/geth/contract.go index 474354d2..b4b48d96 100644 --- a/pkg/geth/contract.go +++ b/pkg/geth/contract.go @@ -5,7 +5,10 @@ import ( "fmt" "path/filepath" + "sort" + "github.com/8thlight/vulcanizedb/pkg/config" + "github.com/8thlight/vulcanizedb/pkg/core" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" ) @@ -14,6 +17,22 @@ var ( ErrInvalidStateAttribute = errors.New("invalid state attribute") ) +func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) { + abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash)) + parsed, _ := ParseAbiFile(abiFilePath) + var contractAttributes []core.ContractAttribute + for _, abiElement := range parsed.Methods { + if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const && abiElement.Outputs[0].Type.String() == "string" { + attributeType := abiElement.Outputs[0].Type.String() + contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType}) + } + } + sort.Slice(contractAttributes, func(i, j int) bool { + return contractAttributes[i].Name < contractAttributes[j].Name + }) + return contractAttributes, nil +} + func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) { boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client) if err != nil { diff --git a/pkg/geth/contract_test.go b/pkg/geth/contract_test.go index f9eb3ff8..acd2792f 100644 --- a/pkg/geth/contract_test.go +++ b/pkg/geth/contract_test.go @@ -3,11 +3,64 @@ package geth_test //import ( // cfg "github.com/8thlight/vulcanizedb/pkg/config" // "github.com/8thlight/vulcanizedb/pkg/geth" +// "github.com/8thlight/vulcanizedb/pkg/geth/testing" // . "github.com/onsi/ginkgo" // . "github.com/onsi/gomega" //) // -//var _ = Describe("The Geth blockchain", func() { +//var _ = Describe("Reading contracts", func() { +// +// Describe("Reading the list of attributes", func() { +// It("returns a string attribute for a real contract", func() { +// config, _ := cfg.NewConfig("public") +// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) +// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" +// +// attributes, err := blockchain.GetContractAttributes(contractHash) +// +// Expect(err).To(BeNil()) +// Expect(len(attributes)).NotTo(Equal(0)) +// symbolAttribute := *testing.FindAttribute(attributes, "symbol") +// Expect(symbolAttribute.Name).To(Equal("symbol")) +// Expect(symbolAttribute.Type).To(Equal("string")) +// }) +// +// It("does not return an attribute that takes an input", func() { +// config, _ := cfg.NewConfig("public") +// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) +// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" +// +// attributes, err := blockchain.GetContractAttributes(contractHash) +// +// Expect(err).To(BeNil()) +// attribute := testing.FindAttribute(attributes, "balanceOf") +// Expect(attribute).To(BeNil()) +// }) +// +// It("does not return an attribute that is not constant", func() { +// config, _ := cfg.NewConfig("public") +// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) +// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" +// +// attributes, err := blockchain.GetContractAttributes(contractHash) +// +// Expect(err).To(BeNil()) +// attribute := testing.FindAttribute(attributes, "unpause") +// Expect(attribute).To(BeNil()) +// }) +// +// It("temporarily filters out non-string attributes", func() { +// config, _ := cfg.NewConfig("public") +// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) +// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" +// +// attributes, err := blockchain.GetContractAttributes(contractHash) +// +// Expect(err).To(BeNil()) +// attribute := testing.FindAttribute(attributes, "decimals") +// Expect(attribute).To(BeNil()) +// }) +// }) // // Describe("Getting a contract attribute", func() { // It("returns the correct attribute for a real contract", func() { diff --git a/pkg/geth/testing/contract_attributes.go b/pkg/geth/testing/contract_attributes.go new file mode 100644 index 00000000..6dbbb4c7 --- /dev/null +++ b/pkg/geth/testing/contract_attributes.go @@ -0,0 +1,14 @@ +package testing + +import ( + "github.com/8thlight/vulcanizedb/pkg/core" +) + +func FindAttribute(contractAttributes []core.ContractAttribute, attributeName string) *core.ContractAttribute { + for _, contractAttribute := range contractAttributes { + if contractAttribute.Name == attributeName { + return &contractAttribute + } + } + return nil +} diff --git a/pkg/geth/testing/valid_abi.json b/pkg/geth/testing/valid_abi.json new file mode 100644 index 00000000..3d80565a --- /dev/null +++ b/pkg/geth/testing/valid_abi.json @@ -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"}] diff --git a/pkg/watched_contracts/console_presenter.go b/pkg/watched_contracts/console_presenter.go index e053f1dd..e934a58c 100644 --- a/pkg/watched_contracts/console_presenter.go +++ b/pkg/watched_contracts/console_presenter.go @@ -9,19 +9,19 @@ import ( func GenerateConsoleOutput(summary *ContractSummary) string { return fmt.Sprintf(template(), summary.ContractHash, - summary.GetStateAttribute("name"), summary.NumberOfTransactions, transactionToString(summary.LastTransaction), + attributesString(summary), ) } - func template() string { return `********************Contract Summary*********************** HASH: %v - NAME: %s NUMBER OF TRANSACTIONS: %d LAST TRANSACTION: %s + ATTRIBUTES: + %s ` } @@ -34,3 +34,17 @@ func transactionToString(transaction *core.Transaction) string { From: %s`, transaction.Hash, transaction.To, transaction.From) } } + +func attributesString(summary *ContractSummary) string { + attributes := []string{"name", "symbol"} + var formattedAttributes string + for _, attribute := range attributes { + formattedAttributes += formatAttribute(attribute, summary) + "\n" + " " + } + return formattedAttributes +} + +func formatAttribute(attributeName string, summary *ContractSummary) string { + formattedAttribute := fmt.Sprintf("%s: %s", attributeName, summary.GetStateAttribute(attributeName)) + return formattedAttribute +}