Updated to use contracts derived Attributes

This commit is contained in:
Matt Krump
2017-11-28 13:43:08 -06:00
parent 60bef69113
commit aa3318451b
6 changed files with 64 additions and 10 deletions
+11 -2
View File
@@ -1,6 +1,8 @@
package fakes
import "github.com/8thlight/vulcanizedb/pkg/core"
import (
"github.com/8thlight/vulcanizedb/pkg/core"
)
type Blockchain struct {
blocks map[int64]core.Block
@@ -10,7 +12,14 @@ type Blockchain struct {
}
func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) {
panic("implement me")
var contractAttribute []core.ContractAttribute
attributes, ok := blockchain.contractAttributes[contractHash]
if ok {
for key, _ := range attributes {
contractAttribute = append(contractAttribute, core.ContractAttribute{Name: key, Type: "string"})
}
}
return contractAttribute, nil
}
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {
+3 -3
View File
@@ -14,6 +14,7 @@ func GenerateConsoleOutput(summary *ContractSummary) string {
attributesString(summary),
)
}
func template() string {
return `********************Contract Summary***********************
HASH: %v
@@ -36,10 +37,9 @@ func transactionToString(transaction *core.Transaction) string {
}
func attributesString(summary *ContractSummary) string {
attributes := []string{"name", "symbol"}
var formattedAttributes string
for _, attribute := range attributes {
formattedAttributes += formatAttribute(attribute, summary) + "\n" + " "
for _, attribute := range summary.Attributes {
formattedAttributes += formatAttribute(attribute.Name, summary) + "\n" + " "
}
return formattedAttributes
}
+3
View File
@@ -13,6 +13,7 @@ type ContractSummary struct {
NumberOfTransactions int
LastTransaction *core.Transaction
blockChain core.Blockchain
Attributes []core.ContractAttribute
}
var NewContractNotWatchedErr = func(contractHash string) error {
@@ -34,11 +35,13 @@ func (contractSummary ContractSummary) GetStateAttribute(attributeName string) s
}
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
attributes, _ := blockchain.GetContractAttributes(watchedContract.Hash)
return &ContractSummary{
blockChain: blockchain,
ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract),
Attributes: attributes,
}
}
+19 -1
View File
@@ -83,7 +83,7 @@ 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() {
It("gets contract state attribute for the contract from the blockchain", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
@@ -94,6 +94,24 @@ var _ bool = Describe("The watched contract summary", func() {
Expect(contractSummary.GetStateAttribute("foo")).To(Equal("bar"))
})
It("gets a attributes 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")
blockchain.SetContractStateAttribute("0x123", "baz", "bar")
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.Attributes).To(Equal(
[]core.ContractAttribute{
{Name: "foo", Type: "string"},
{Name: "baz", Type: "string"},
},
))
})
})
})