diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index c7ba3432..0036036d 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -5,6 +5,6 @@ type Blockchain interface { SubscribeToBlocks(blocks chan Block) StartListening() StopListening() - GetContractAttributes(contractHash string) ([]ContractAttribute, error) + GetContractAttributes(contractHash string) (ContractAttributes, error) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) } diff --git a/pkg/core/watched_contract.go b/pkg/core/watched_contract.go index b3e50218..ec8176dd 100644 --- a/pkg/core/watched_contract.go +++ b/pkg/core/watched_contract.go @@ -9,3 +9,16 @@ type ContractAttribute struct { Name string Type string } + +type ContractAttributes []ContractAttribute + +func (s ContractAttributes) Len() int { + return len(s) +} +func (s ContractAttributes) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func (s ContractAttributes) Less(i, j int) bool { + return s[i].Name < s[j].Name +} \ No newline at end of file diff --git a/pkg/fakes/blockchain.go b/pkg/fakes/blockchain.go index 6b840ab2..966f5112 100644 --- a/pkg/fakes/blockchain.go +++ b/pkg/fakes/blockchain.go @@ -2,6 +2,7 @@ package fakes import ( "github.com/8thlight/vulcanizedb/pkg/core" + "sort" ) type Blockchain struct { @@ -11,15 +12,16 @@ type Blockchain struct { WasToldToStop bool } -func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) { - var contractAttribute []core.ContractAttribute +func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { + var contractAttributes core.ContractAttributes attributes, ok := blockchain.contractAttributes[contractHash] if ok { for key, _ := range attributes { - contractAttribute = append(contractAttribute, core.ContractAttribute{Name: key, Type: "string"}) + contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"}) } } - return contractAttribute, nil + sort.Sort(contractAttributes) + return contractAttributes, nil } func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) { diff --git a/pkg/geth/contract.go b/pkg/geth/contract.go index b4b48d96..de65de3e 100644 --- a/pkg/geth/contract.go +++ b/pkg/geth/contract.go @@ -17,19 +17,17 @@ var ( ErrInvalidStateAttribute = errors.New("invalid state attribute") ) -func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) { +func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) { abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash)) parsed, _ := ParseAbiFile(abiFilePath) - var contractAttributes []core.ContractAttribute + var contractAttributes core.ContractAttributes 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 - }) + sort.Sort(contractAttributes) return contractAttributes, nil } diff --git a/pkg/geth/testing/contract_attributes.go b/pkg/geth/testing/contract_attributes.go index 6dbbb4c7..a6cd7de3 100644 --- a/pkg/geth/testing/contract_attributes.go +++ b/pkg/geth/testing/contract_attributes.go @@ -4,7 +4,7 @@ import ( "github.com/8thlight/vulcanizedb/pkg/core" ) -func FindAttribute(contractAttributes []core.ContractAttribute, attributeName string) *core.ContractAttribute { +func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute { for _, contractAttribute := range contractAttributes { if contractAttribute.Name == attributeName { return &contractAttribute diff --git a/pkg/watched_contracts/summary.go b/pkg/watched_contracts/summary.go index e5cafddd..3e9183a6 100644 --- a/pkg/watched_contracts/summary.go +++ b/pkg/watched_contracts/summary.go @@ -13,7 +13,7 @@ type ContractSummary struct { NumberOfTransactions int LastTransaction *core.Transaction blockChain core.Blockchain - Attributes []core.ContractAttribute + Attributes core.ContractAttributes } var NewContractNotWatchedErr = func(contractHash string) error { diff --git a/pkg/watched_contracts/summary_test.go b/pkg/watched_contracts/summary_test.go index 37576ba2..ee5b6d46 100644 --- a/pkg/watched_contracts/summary_test.go +++ b/pkg/watched_contracts/summary_test.go @@ -106,9 +106,9 @@ var _ bool = Describe("The watched contract summary", func() { contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123") Expect(contractSummary.Attributes).To(Equal( - []core.ContractAttribute{ - {Name: "foo", Type: "string"}, + core.ContractAttributes{ {Name: "baz", Type: "string"}, + {Name: "foo", Type: "string"}, }, )) })