2017-11-13 15:58:36 +00:00
|
|
|
package watched_contracts_test
|
|
|
|
|
|
|
|
import (
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-11-13 15:58:36 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
2017-11-27 15:21:21 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/fakes"
|
2017-11-13 15:58:36 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/watched_contracts"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
func NewCurrentContractSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string) (watched_contracts.ContractSummary, error) {
|
|
|
|
return watched_contracts.NewSummary(blockchain, repository, contractHash, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("The watched contract summary", func() {
|
2017-11-13 15:58:36 +00:00
|
|
|
|
|
|
|
Context("when the given contract is not being watched", func() {
|
|
|
|
It("returns an error", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-11-27 15:21:21 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, err := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 16:17:13 +00:00
|
|
|
Expect(contractSummary).To(Equal(watched_contracts.ContractSummary{}))
|
2017-11-13 15:58:36 +00:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when the given contract is being watched", func() {
|
|
|
|
It("returns the summary", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-13 15:58:36 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
2017-11-27 15:21:21 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, err := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 16:17:13 +00:00
|
|
|
Expect(contractSummary).NotTo(Equal(watched_contracts.ContractSummary{}))
|
2017-11-13 15:58:36 +00:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("includes the contract hash in the summary", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-13 15:58:36 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
2017-11-27 15:21:21 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-13 15:58:36 +00:00
|
|
|
|
|
|
|
Expect(contractSummary.ContractHash).To(Equal("0x123"))
|
|
|
|
})
|
2017-11-14 15:14:34 +00:00
|
|
|
|
|
|
|
It("sets the number of transactions", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-14 15:14:34 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
|
|
|
block := core.Block{
|
|
|
|
Transactions: []core.Transaction{
|
|
|
|
{To: "0x123"},
|
|
|
|
{To: "0x123"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
repository.CreateBlock(block)
|
2017-11-27 15:21:21 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-11-14 15:14:34 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-14 15:14:34 +00:00
|
|
|
|
|
|
|
Expect(contractSummary.NumberOfTransactions).To(Equal(2))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets the last transaction", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-14 15:14:34 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
|
|
|
block := core.Block{
|
|
|
|
Transactions: []core.Transaction{
|
|
|
|
{Hash: "TRANSACTION2", To: "0x123"},
|
|
|
|
{Hash: "TRANSACTION1", To: "0x123"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
repository.CreateBlock(block)
|
2017-11-27 15:21:21 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-11-14 15:14:34 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-14 15:14:34 +00:00
|
|
|
|
|
|
|
Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2"))
|
|
|
|
})
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-11-28 16:45:21 +00:00
|
|
|
It("gets contract state attribute for the contract from the blockchain", func() {
|
2017-11-27 15:39:53 +00:00
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-27 15:39:53 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-12-04 18:54:33 +00:00
|
|
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-29 15:31:23 +00:00
|
|
|
attribute := contractSummary.GetStateAttribute("foo")
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-11-29 15:31:23 +00:00
|
|
|
Expect(attribute).To(Equal("bar"))
|
2017-11-27 15:39:53 +00:00
|
|
|
})
|
2017-11-28 16:45:21 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-12-04 18:54:33 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
|
|
|
blockchain := fakes.NewBlockchain()
|
|
|
|
blockNumber := big.NewInt(1000)
|
|
|
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
|
|
|
blockchain.SetContractStateAttribute("0x123", blockNumber, "foo", "baz")
|
|
|
|
|
|
|
|
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123", blockNumber)
|
|
|
|
attribute := contractSummary.GetStateAttribute("foo")
|
|
|
|
|
|
|
|
Expect(attribute).To(Equal("baz"))
|
|
|
|
})
|
|
|
|
|
2017-11-28 20:40:05 +00:00
|
|
|
It("gets attributes for the contract from the blockchain", func() {
|
2017-11-28 16:45:21 +00:00
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
2017-11-28 16:45:21 +00:00
|
|
|
repository.CreateWatchedContract(watchedContract)
|
|
|
|
blockchain := fakes.NewBlockchain()
|
2017-12-04 18:54:33 +00:00
|
|
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
|
|
|
blockchain.SetContractStateAttribute("0x123", nil, "baz", "bar")
|
2017-11-28 16:45:21 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
2017-11-28 16:45:21 +00:00
|
|
|
|
|
|
|
Expect(contractSummary.Attributes).To(Equal(
|
2017-11-28 20:03:21 +00:00
|
|
|
core.ContractAttributes{
|
2017-11-28 16:45:21 +00:00
|
|
|
{Name: "baz", Type: "string"},
|
2017-11-28 20:03:21 +00:00
|
|
|
{Name: "foo", Type: "string"},
|
2017-11-28 16:45:21 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
})
|
2017-11-13 15:58:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|