Update NewSummary to take the blockchain

This commit is contained in:
Eric Meyer 2017-11-27 09:21:21 -06:00
parent 90edd15ead
commit 9c1051bfce
4 changed files with 21 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/repositories"
"github.com/8thlight/vulcanizedb/pkg/watched_contracts"
)
@ -18,8 +19,9 @@ func main() {
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
repository := repositories.NewPostgres(config.Database)
contractSummary, err := watched_contracts.NewSummary(repository, *contractHash)
contractSummary, err := watched_contracts.NewSummary(blockchain, repository, *contractHash)
if err != nil {
log.Fatalln(err)
}

View File

@ -9,6 +9,7 @@ import (
func GenerateConsoleOutput(summary *ContractSummary) string {
return fmt.Sprintf(template(),
summary.ContractHash,
summary.GetStateAttribute("name"),
summary.NumberOfTransactions,
transactionToString(summary.LastTransaction),
)
@ -17,6 +18,7 @@ func GenerateConsoleOutput(summary *ContractSummary) string {
func template() string {
return `********************Contract Summary***********************
HASH: %v
NAME: %s
NUMBER OF TRANSACTIONS: %d
LAST TRANSACTION:
%s

View File

@ -18,7 +18,7 @@ var NewContractNotWatchedErr = func(contractHash string) error {
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
}
func NewSummary(repository repositories.Repository, contractHash string) (*ContractSummary, error) {
func NewSummary(_ core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
contract := repository.FindWatchedContract(contractHash)
if contract != nil {
return newContractSummary(*contract), nil
@ -27,6 +27,10 @@ func NewSummary(repository repositories.Repository, contractHash string) (*Contr
}
}
func (ContractSummary) GetStateAttribute(attributeName string) string {
return "Hello world"
}
func newContractSummary(contract core.WatchedContract) *ContractSummary {
return &ContractSummary{
ContractHash: contract.Hash,

View File

@ -2,6 +2,7 @@ package watched_contracts_test
import (
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/fakes"
"github.com/8thlight/vulcanizedb/pkg/repositories"
"github.com/8thlight/vulcanizedb/pkg/watched_contracts"
. "github.com/onsi/ginkgo"
@ -13,8 +14,9 @@ var _ bool = Describe("The watched contract summary", func() {
Context("when the given contract is not being watched", func() {
It("returns an error", func() {
repository := repositories.NewInMemory()
blockchain := fakes.NewBlockchain()
contractSummary, err := watched_contracts.NewSummary(repository, "123")
contractSummary, err := watched_contracts.NewSummary(blockchain, repository, "123")
Expect(contractSummary).To(BeNil())
Expect(err).NotTo(BeNil())
@ -26,8 +28,9 @@ var _ bool = Describe("The watched contract summary", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
contractSummary, err := watched_contracts.NewSummary(repository, "0x123")
contractSummary, err := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary).NotTo(BeNil())
Expect(err).To(BeNil())
@ -37,8 +40,9 @@ var _ bool = Describe("The watched contract summary", func() {
repository := repositories.NewInMemory()
watchedContract := core.WatchedContract{Hash: "0x123"}
repository.CreateWatchedContract(watchedContract)
blockchain := fakes.NewBlockchain()
contractSummary, _ := watched_contracts.NewSummary(repository, "0x123")
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.ContractHash).To(Equal("0x123"))
})
@ -54,8 +58,9 @@ var _ bool = Describe("The watched contract summary", func() {
},
}
repository.CreateBlock(block)
blockchain := fakes.NewBlockchain()
contractSummary, _ := watched_contracts.NewSummary(repository, "0x123")
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.NumberOfTransactions).To(Equal(2))
})
@ -71,8 +76,9 @@ var _ bool = Describe("The watched contract summary", func() {
},
}
repository.CreateBlock(block)
blockchain := fakes.NewBlockchain()
contractSummary, _ := watched_contracts.NewSummary(repository, "0x123")
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2"))
})