Added sort method to ContractAttributes array

This commit is contained in:
Matt Krump 2017-11-28 14:03:21 -06:00
parent aa3318451b
commit 1bae6db483
7 changed files with 27 additions and 14 deletions

View File

@ -5,6 +5,6 @@ type Blockchain interface {
SubscribeToBlocks(blocks chan Block) SubscribeToBlocks(blocks chan Block)
StartListening() StartListening()
StopListening() StopListening()
GetContractAttributes(contractHash string) ([]ContractAttribute, error) GetContractAttributes(contractHash string) (ContractAttributes, error)
GetContractStateAttribute(contractHash string, attributeName string) (*string, error) GetContractStateAttribute(contractHash string, attributeName string) (*string, error)
} }

View File

@ -9,3 +9,16 @@ type ContractAttribute struct {
Name string Name string
Type 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
}

View File

@ -2,6 +2,7 @@ package fakes
import ( import (
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"sort"
) )
type Blockchain struct { type Blockchain struct {
@ -11,15 +12,16 @@ type Blockchain struct {
WasToldToStop bool WasToldToStop bool
} }
func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) { func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
var contractAttribute []core.ContractAttribute var contractAttributes core.ContractAttributes
attributes, ok := blockchain.contractAttributes[contractHash] attributes, ok := blockchain.contractAttributes[contractHash]
if ok { if ok {
for key, _ := range attributes { 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) { func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {

View File

@ -17,19 +17,17 @@ var (
ErrInvalidStateAttribute = errors.New("invalid state attribute") 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)) abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
parsed, _ := ParseAbiFile(abiFilePath) parsed, _ := ParseAbiFile(abiFilePath)
var contractAttributes []core.ContractAttribute var contractAttributes core.ContractAttributes
for _, abiElement := range parsed.Methods { for _, abiElement := range parsed.Methods {
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const && abiElement.Outputs[0].Type.String() == "string" { if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const && abiElement.Outputs[0].Type.String() == "string" {
attributeType := abiElement.Outputs[0].Type.String() attributeType := abiElement.Outputs[0].Type.String()
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType}) contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
} }
} }
sort.Slice(contractAttributes, func(i, j int) bool { sort.Sort(contractAttributes)
return contractAttributes[i].Name < contractAttributes[j].Name
})
return contractAttributes, nil return contractAttributes, nil
} }

View File

@ -4,7 +4,7 @@ import (
"github.com/8thlight/vulcanizedb/pkg/core" "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 { for _, contractAttribute := range contractAttributes {
if contractAttribute.Name == attributeName { if contractAttribute.Name == attributeName {
return &contractAttribute return &contractAttribute

View File

@ -13,7 +13,7 @@ type ContractSummary struct {
NumberOfTransactions int NumberOfTransactions int
LastTransaction *core.Transaction LastTransaction *core.Transaction
blockChain core.Blockchain blockChain core.Blockchain
Attributes []core.ContractAttribute Attributes core.ContractAttributes
} }
var NewContractNotWatchedErr = func(contractHash string) error { var NewContractNotWatchedErr = func(contractHash string) error {

View File

@ -106,9 +106,9 @@ var _ bool = Describe("The watched contract summary", func() {
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123") contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.Attributes).To(Equal( Expect(contractSummary.Attributes).To(Equal(
[]core.ContractAttribute{ core.ContractAttributes{
{Name: "foo", Type: "string"},
{Name: "baz", Type: "string"}, {Name: "baz", Type: "string"},
{Name: "foo", Type: "string"},
}, },
)) ))
}) })