2017-12-04 21:36:11 +00:00
|
|
|
package contract_summary_test
|
2017-11-13 15:58:36 +00:00
|
|
|
|
|
|
|
import (
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-12-04 21:36:11 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/contract_summary"
|
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/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2017-12-04 21:36:11 +00:00
|
|
|
func NewCurrentContractSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string) (contract_summary.ContractSummary, error) {
|
|
|
|
return contract_summary.NewSummary(blockchain, repository, contractHash, nil)
|
2017-12-04 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
var _ = Describe("The contract summary", func() {
|
2017-11-13 15:58:36 +00:00
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
Context("when the given contract does not exist", func() {
|
2017-11-13 15:58:36 +00:00
|
|
|
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 21:36:11 +00:00
|
|
|
Expect(contractSummary).To(Equal(contract_summary.ContractSummary{}))
|
2017-11-13 15:58:36 +00:00
|
|
|
Expect(err).NotTo(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
Context("when the given contract exists", func() {
|
2017-11-13 15:58:36 +00:00
|
|
|
It("returns the summary", func() {
|
|
|
|
repository := repositories.NewInMemory()
|
2017-12-04 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
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 21:36:11 +00:00
|
|
|
Expect(contractSummary).NotTo(Equal(contract_summary.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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
2017-11-14 15:14:34 +00:00
|
|
|
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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
2017-11-14 15:14:34 +00:00
|
|
|
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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
2017-11-27 15:39:53 +00:00
|
|
|
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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
2017-12-04 18:54:33 +00:00
|
|
|
blockchain := fakes.NewBlockchain()
|
|
|
|
blockNumber := big.NewInt(1000)
|
|
|
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
|
|
|
blockchain.SetContractStateAttribute("0x123", blockNumber, "foo", "baz")
|
|
|
|
|
2017-12-04 21:36:11 +00:00
|
|
|
contractSummary, _ := contract_summary.NewSummary(blockchain, repository, "0x123", blockNumber)
|
2017-12-04 18:54:33 +00:00
|
|
|
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 22:54:35 +00:00
|
|
|
contract := core.Contract{Hash: "0x123"}
|
|
|
|
repository.CreateContract(contract)
|
2017-11-28 16:45:21 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
})
|