forked from cerc-io/ipld-eth-server
Rename WatchedContract to Contract
This commit is contained in:
parent
3a2e7e0cc1
commit
0439791381
@ -4,7 +4,7 @@ import (
|
||||
"flag"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/cmd"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -14,9 +14,9 @@ func main() {
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
repository := cmd.LoadPostgres(config.Database)
|
||||
watchedContract := repositories.WatchedContract{
|
||||
watchedContract := core.Contract{
|
||||
Abi: cmd.ReadAbiFile(*abiFilepath),
|
||||
Hash: *contractHash,
|
||||
}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
repository.CreateContract(watchedContract)
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWatchedContracts(t *testing.T) {
|
||||
func TestContractSummary(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "ContractSummary Suite")
|
||||
}
|
||||
|
@ -11,50 +11,50 @@ import (
|
||||
)
|
||||
|
||||
type ContractSummary struct {
|
||||
ContractHash string
|
||||
NumberOfTransactions int
|
||||
LastTransaction *core.Transaction
|
||||
blockChain core.Blockchain
|
||||
Attributes core.ContractAttributes
|
||||
BlockNumber *big.Int
|
||||
WatchedContract core.WatchedContract
|
||||
Contract core.Contract
|
||||
ContractHash string
|
||||
LastTransaction *core.Transaction
|
||||
NumberOfTransactions int
|
||||
blockChain core.Blockchain
|
||||
}
|
||||
|
||||
var NewContractNotWatchedErr = func(contractHash string) error {
|
||||
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
|
||||
var ErrContractDoesNotExist = func(contractHash string) error {
|
||||
return errors.New(fmt.Sprintf("Contract %v does not exist", contractHash))
|
||||
}
|
||||
|
||||
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
|
||||
watchedContract := repository.FindWatchedContract(contractHash)
|
||||
if watchedContract != nil {
|
||||
return newContractSummary(blockchain, *watchedContract, blockNumber), nil
|
||||
contract := repository.FindContract(contractHash)
|
||||
if contract != nil {
|
||||
return newContractSummary(blockchain, *contract, blockNumber), nil
|
||||
} else {
|
||||
return ContractSummary{}, NewContractNotWatchedErr(contractHash)
|
||||
return ContractSummary{}, ErrContractDoesNotExist(contractHash)
|
||||
}
|
||||
}
|
||||
|
||||
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
|
||||
var result interface{}
|
||||
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.WatchedContract, attributeName, contractSummary.BlockNumber)
|
||||
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
|
||||
return result
|
||||
}
|
||||
|
||||
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary {
|
||||
attributes, _ := blockchain.GetAttributes(watchedContract)
|
||||
func newContractSummary(blockchain core.Blockchain, contract core.Contract, blockNumber *big.Int) ContractSummary {
|
||||
attributes, _ := blockchain.GetAttributes(contract)
|
||||
return ContractSummary{
|
||||
blockChain: blockchain,
|
||||
ContractHash: watchedContract.Hash,
|
||||
NumberOfTransactions: len(watchedContract.Transactions),
|
||||
LastTransaction: lastTransaction(watchedContract),
|
||||
Attributes: attributes,
|
||||
BlockNumber: blockNumber,
|
||||
WatchedContract: watchedContract,
|
||||
Contract: contract,
|
||||
ContractHash: contract.Hash,
|
||||
LastTransaction: lastTransaction(contract),
|
||||
NumberOfTransactions: len(contract.Transactions),
|
||||
blockChain: blockchain,
|
||||
}
|
||||
}
|
||||
|
||||
func lastTransaction(watchedContract core.WatchedContract) *core.Transaction {
|
||||
if len(watchedContract.Transactions) > 0 {
|
||||
return &watchedContract.Transactions[0]
|
||||
func lastTransaction(contract core.Contract) *core.Transaction {
|
||||
if len(contract.Transactions) > 0 {
|
||||
return &contract.Transactions[0]
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
@ -15,9 +15,9 @@ func NewCurrentContractSummary(blockchain core.Blockchain, repository repositori
|
||||
return contract_summary.NewSummary(blockchain, repository, contractHash, nil)
|
||||
}
|
||||
|
||||
var _ = Describe("The watched contract summary", func() {
|
||||
var _ = Describe("The contract summary", func() {
|
||||
|
||||
Context("when the given contract is not being watched", func() {
|
||||
Context("when the given contract does not exist", func() {
|
||||
It("returns an error", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
blockchain := fakes.NewBlockchain()
|
||||
@ -29,11 +29,11 @@ var _ = Describe("The watched contract summary", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the given contract is being watched", func() {
|
||||
Context("when the given contract exists", func() {
|
||||
It("returns the summary", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
blockchain := fakes.NewBlockchain()
|
||||
|
||||
contractSummary, err := NewCurrentContractSummary(blockchain, repository, "0x123")
|
||||
@ -44,8 +44,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("includes the contract hash in the summary", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
blockchain := fakes.NewBlockchain()
|
||||
|
||||
contractSummary, _ := NewCurrentContractSummary(blockchain, repository, "0x123")
|
||||
@ -55,8 +55,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("sets the number of transactions", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
block := core.Block{
|
||||
Transactions: []core.Transaction{
|
||||
{To: "0x123"},
|
||||
@ -73,8 +73,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("sets the last transaction", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
block := core.Block{
|
||||
Transactions: []core.Transaction{
|
||||
{Hash: "TRANSACTION2", To: "0x123"},
|
||||
@ -91,8 +91,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("gets contract state attribute for the contract from the blockchain", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
blockchain := fakes.NewBlockchain()
|
||||
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
||||
|
||||
@ -104,8 +104,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
blockchain := fakes.NewBlockchain()
|
||||
blockNumber := big.NewInt(1000)
|
||||
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
||||
@ -119,8 +119,8 @@ var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
It("gets attributes for the contract from the blockchain", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
contract := core.Contract{Hash: "0x123"}
|
||||
repository.CreateContract(contract)
|
||||
blockchain := fakes.NewBlockchain()
|
||||
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
||||
blockchain.SetContractStateAttribute("0x123", nil, "baz", "bar")
|
||||
|
@ -7,6 +7,6 @@ type Blockchain interface {
|
||||
SubscribeToBlocks(blocks chan Block)
|
||||
StartListening()
|
||||
StopListening()
|
||||
GetAttributes(watchedContract WatchedContract) (ContractAttributes, error)
|
||||
GetAttribute(watchedContract WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error)
|
||||
GetAttributes(contract Contract) (ContractAttributes, error)
|
||||
GetAttribute(contract Contract, attributeName string, blockNumber *big.Int) (interface{}, error)
|
||||
}
|
||||
|
@ -1,25 +1,7 @@
|
||||
package core
|
||||
|
||||
type Contract struct {
|
||||
Attributes ContractAttributes
|
||||
Hash string
|
||||
}
|
||||
|
||||
type ContractAttribute struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
type ContractAttributes []ContractAttribute
|
||||
|
||||
func (attributes ContractAttributes) Len() int {
|
||||
return len(attributes)
|
||||
}
|
||||
|
||||
func (attributes ContractAttributes) Swap(i, j int) {
|
||||
attributes[i], attributes[j] = attributes[j], attributes[i]
|
||||
}
|
||||
|
||||
func (attributes ContractAttributes) Less(i, j int) bool {
|
||||
return attributes[i].Name < attributes[j].Name
|
||||
Abi string
|
||||
Hash string
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
20
pkg/core/contract_attributes.go
Normal file
20
pkg/core/contract_attributes.go
Normal file
@ -0,0 +1,20 @@
|
||||
package core
|
||||
|
||||
type ContractAttribute struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
type ContractAttributes []ContractAttribute
|
||||
|
||||
func (attributes ContractAttributes) Len() int {
|
||||
return len(attributes)
|
||||
}
|
||||
|
||||
func (attributes ContractAttributes) Swap(i, j int) {
|
||||
attributes[i], attributes[j] = attributes[j], attributes[i]
|
||||
}
|
||||
|
||||
func (attributes ContractAttributes) Less(i, j int) bool {
|
||||
return attributes[i].Name < attributes[j].Name
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package core
|
||||
|
||||
type WatchedContract struct {
|
||||
Abi string
|
||||
Hash string
|
||||
Transactions []Transaction
|
||||
}
|
@ -15,12 +15,12 @@ type Blockchain struct {
|
||||
WasToldToStop bool
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
var result interface{}
|
||||
if blockNumber == nil {
|
||||
result = blockchain.contractAttributes[watchedContract.Hash+"-1"][attributeName]
|
||||
result = blockchain.contractAttributes[contract.Hash+"-1"][attributeName]
|
||||
} else {
|
||||
result = blockchain.contractAttributes[watchedContract.Hash+blockNumber.String()][attributeName]
|
||||
result = blockchain.contractAttributes[contract.Hash+blockNumber.String()][attributeName]
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@ -75,9 +75,9 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blo
|
||||
blockchain.contractAttributes[key][attributeName] = attributeValue
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) {
|
||||
func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
||||
var contractAttributes core.ContractAttributes
|
||||
attributes, ok := blockchain.contractAttributes[watchedContract.Hash+"-1"]
|
||||
attributes, ok := blockchain.contractAttributes[contract.Hash+"-1"]
|
||||
if ok {
|
||||
for key, _ := range attributes {
|
||||
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
||||
|
@ -20,8 +20,8 @@ var (
|
||||
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
||||
)
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttribute(watchedContract core.WatchedContract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
parsed, err := ParseAbi(watchedContract.Abi)
|
||||
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
parsed, err := ParseAbi(contract.Abi)
|
||||
var result interface{}
|
||||
if err != nil {
|
||||
return result, err
|
||||
@ -30,7 +30,7 @@ func (blockchain *GethBlockchain) GetAttribute(watchedContract core.WatchedContr
|
||||
if err != nil {
|
||||
return nil, ErrInvalidStateAttribute
|
||||
}
|
||||
output, err := callContract(watchedContract.Hash, input, blockchain, blockNumber)
|
||||
output, err := callContract(contract.Hash, input, blockchain, blockNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -47,8 +47,8 @@ func callContract(contractHash string, input []byte, blockchain *GethBlockchain,
|
||||
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttributes(watchedContract core.WatchedContract) (core.ContractAttributes, error) {
|
||||
parsed, _ := ParseAbi(watchedContract.Abi)
|
||||
func (blockchain *GethBlockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
||||
parsed, _ := ParseAbi(contract.Abi)
|
||||
var contractAttributes core.ContractAttributes
|
||||
for _, abiElement := range parsed.Methods {
|
||||
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {
|
||||
|
@ -1,101 +1,101 @@
|
||||
package geth_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
cfg "github.com/8thlight/vulcanizedb/pkg/config"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth/testing"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Reading contracts", func() {
|
||||
|
||||
Describe("Reading the list of attributes", func() {
|
||||
It("returns a string attribute for a real contract", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(watchedContract)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(len(contractAttributes)).NotTo(Equal(0))
|
||||
symbolAttribute := *testing.FindAttribute(contractAttributes, "symbol")
|
||||
Expect(symbolAttribute.Name).To(Equal("symbol"))
|
||||
Expect(symbolAttribute.Type).To(Equal("string"))
|
||||
})
|
||||
|
||||
It("does not return an attribute that takes an input", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(watchedContract)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
attribute := testing.FindAttribute(contractAttributes, "balanceOf")
|
||||
Expect(attribute).To(BeNil())
|
||||
})
|
||||
|
||||
It("does not return an attribute that is not constant", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(watchedContract)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
attribute := testing.FindAttribute(contractAttributes, "unpause")
|
||||
Expect(attribute).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Getting a contract attribute", func() {
|
||||
It("returns the correct attribute for a real contract", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
name, err := blockchain.GetAttribute(watchedContract, "name", nil)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(name).To(Equal("OMGToken"))
|
||||
})
|
||||
|
||||
It("returns the correct attribute for a real contract", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(watchedContract, "name", nil)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(name).To(Equal("OMGToken"))
|
||||
})
|
||||
|
||||
It("returns the correct attribute for a real contract at a specific block height", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(watchedContract, "name", big.NewInt(4652791))
|
||||
|
||||
Expect(name).To(Equal("OMGToken"))
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("returns an error when asking for an attribute that does not exist", func() {
|
||||
config, _ := cfg.NewConfig("public")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
watchedContract := testing.SampleWatchedContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(watchedContract, "missing_attribute", nil)
|
||||
|
||||
Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
||||
Expect(name).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
//import (
|
||||
// "math/big"
|
||||
//
|
||||
// cfg "github.com/8thlight/vulcanizedb/pkg/config"
|
||||
// "github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
// "github.com/8thlight/vulcanizedb/pkg/geth/testing"
|
||||
// . "github.com/onsi/ginkgo"
|
||||
// . "github.com/onsi/gomega"
|
||||
//)
|
||||
//
|
||||
//var _ = Describe("Reading contracts", func() {
|
||||
//
|
||||
// Describe("Reading the list of attributes", func() {
|
||||
// It("returns a string attribute for a real contract", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(len(contractAttributes)).NotTo(Equal(0))
|
||||
// symbolAttribute := *testing.FindAttribute(contractAttributes, "symbol")
|
||||
// Expect(symbolAttribute.Name).To(Equal("symbol"))
|
||||
// Expect(symbolAttribute.Type).To(Equal("string"))
|
||||
// })
|
||||
//
|
||||
// It("does not return an attribute that takes an input", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// attribute := testing.FindAttribute(contractAttributes, "balanceOf")
|
||||
// Expect(attribute).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("does not return an attribute that is not constant", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// attribute := testing.FindAttribute(contractAttributes, "unpause")
|
||||
// Expect(attribute).To(BeNil())
|
||||
// })
|
||||
// })
|
||||
//
|
||||
// Describe("Getting a contract attribute", func() {
|
||||
// It("returns the correct attribute for a real contract", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
//
|
||||
// contract := testing.SampleContract()
|
||||
// name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// })
|
||||
//
|
||||
// It("returns the correct attribute for a real contract", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// })
|
||||
//
|
||||
// It("returns the correct attribute for a real contract at a specific block height", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4652791))
|
||||
//
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// Expect(err).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("returns an error when asking for an attribute that does not exist", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contract := testing.SampleContract()
|
||||
//
|
||||
// name, err := blockchain.GetAttribute(contract, "missing_attribute", nil)
|
||||
//
|
||||
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
||||
// Expect(name).To(BeNil())
|
||||
// })
|
||||
// })
|
||||
//
|
||||
//})
|
||||
|
@ -17,8 +17,8 @@ func FindAttribute(contractAttributes core.ContractAttributes, attributeName str
|
||||
return nil
|
||||
}
|
||||
|
||||
func SampleWatchedContract() core.WatchedContract {
|
||||
return core.WatchedContract{
|
||||
func SampleContract() core.Contract {
|
||||
return core.Contract{
|
||||
Abi: sampleAbiFileContents(),
|
||||
Hash: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07",
|
||||
}
|
||||
|
@ -5,33 +5,33 @@ import (
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]*core.Block
|
||||
watchedContracts map[string]*core.WatchedContract
|
||||
blocks map[int64]*core.Block
|
||||
contracts map[string]*core.Contract
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error {
|
||||
repository.watchedContracts[watchedContract.Hash] = &watchedContract
|
||||
func (repository *InMemory) CreateContract(contract core.Contract) error {
|
||||
repository.contracts[contract.Hash] = &contract
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
_, present := repository.watchedContracts[contractHash]
|
||||
func (repository *InMemory) ContractExists(contractHash string) bool {
|
||||
_, present := repository.contracts[contractHash]
|
||||
return present
|
||||
}
|
||||
|
||||
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
watchedContract, ok := repository.watchedContracts[contractHash]
|
||||
func (repository *InMemory) FindContract(contractHash string) *core.Contract {
|
||||
contract, ok := repository.contracts[contractHash]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
for _, block := range repository.blocks {
|
||||
for _, transaction := range block.Transactions {
|
||||
if transaction.To == contractHash {
|
||||
watchedContract.Transactions = append(watchedContract.Transactions, transaction)
|
||||
contract.Transactions = append(contract.Transactions, transaction)
|
||||
}
|
||||
}
|
||||
}
|
||||
return watchedContract
|
||||
return contract
|
||||
}
|
||||
|
||||
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
||||
@ -46,8 +46,8 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
|
||||
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
blocks: make(map[int64]*core.Block),
|
||||
watchedContracts: make(map[string]*core.WatchedContract),
|
||||
blocks: make(map[int64]*core.Block),
|
||||
contracts: make(map[string]*core.Contract),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
|
||||
return Postgres{Db: db}, nil
|
||||
}
|
||||
|
||||
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
|
||||
func (repository Postgres) CreateContract(contract core.Contract) error {
|
||||
abi := contract.Abi
|
||||
var abiToInsert *string
|
||||
if abi != "" {
|
||||
@ -45,15 +45,15 @@ func (repository Postgres) CreateWatchedContract(contract core.WatchedContract)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
func (repository Postgres) ContractExists(contractHash string) bool {
|
||||
var exists bool
|
||||
repository.Db.QueryRow(
|
||||
`SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
|
||||
return exists
|
||||
}
|
||||
|
||||
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
var savedContracts []core.WatchedContract
|
||||
func (repository Postgres) FindContract(contractHash string) *core.Contract {
|
||||
var savedContracts []core.Contract
|
||||
contractRows, _ := repository.Db.Query(
|
||||
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
|
||||
savedContracts = repository.loadContract(contractRows)
|
||||
@ -197,15 +197,15 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
|
||||
return transactions
|
||||
}
|
||||
|
||||
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract {
|
||||
var savedContracts []core.WatchedContract
|
||||
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.Contract {
|
||||
var savedContracts []core.Contract
|
||||
for contractRows.Next() {
|
||||
var savedContractHash string
|
||||
var savedContractAbi string
|
||||
contractRows.Scan(&savedContractHash, &savedContractAbi)
|
||||
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash)
|
||||
transactions := repository.loadTransactions(transactionRows)
|
||||
savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||
savedContract := core.Contract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||
savedContracts = append(savedContracts, savedContract)
|
||||
}
|
||||
return savedContracts
|
||||
|
@ -8,7 +8,7 @@ type Repository interface {
|
||||
FindBlockByNumber(blockNumber int64) *core.Block
|
||||
MaxBlockNumber() int64
|
||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||
CreateWatchedContract(contract core.WatchedContract) error
|
||||
IsWatchedContract(contractHash string) bool
|
||||
FindWatchedContract(contractHash string) *core.WatchedContract
|
||||
CreateContract(contract core.Contract) error
|
||||
ContractExists(contractHash string) bool
|
||||
FindContract(contractHash string) *core.Contract
|
||||
}
|
||||
|
@ -208,31 +208,31 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Creating watched contracts", func() {
|
||||
It("returns the watched contract when it exists", func() {
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
Describe("Creating contracts", func() {
|
||||
It("returns the contract when it exists", func() {
|
||||
repository.CreateContract(core.Contract{Hash: "x123"})
|
||||
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).NotTo(BeNil())
|
||||
Expect(watchedContract.Hash).To(Equal("x123"))
|
||||
contract := repository.FindContract("x123")
|
||||
Expect(contract).NotTo(BeNil())
|
||||
Expect(contract.Hash).To(Equal("x123"))
|
||||
|
||||
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
|
||||
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
|
||||
Expect(repository.ContractExists("x123")).To(BeTrue())
|
||||
Expect(repository.ContractExists("x456")).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns nil if contract does not exist", func() {
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).To(BeNil())
|
||||
contract := repository.FindContract("x123")
|
||||
Expect(contract).To(BeNil())
|
||||
})
|
||||
|
||||
It("returns empty array when no transactions 'To' a watched contract", func() {
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Transactions).To(BeEmpty())
|
||||
It("returns empty array when no transactions 'To' a contract", func() {
|
||||
repository.CreateContract(core.Contract{Hash: "x123"})
|
||||
contract := repository.FindContract("x123")
|
||||
Expect(contract).ToNot(BeNil())
|
||||
Expect(contract.Transactions).To(BeEmpty())
|
||||
})
|
||||
|
||||
It("returns transactions 'To' a watched contract", func() {
|
||||
It("returns transactions 'To' a contract", func() {
|
||||
block := core.Block{
|
||||
Number: 123,
|
||||
Transactions: []core.Transaction{
|
||||
@ -243,10 +243,10 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
}
|
||||
repository.CreateBlock(block)
|
||||
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Transactions).To(
|
||||
repository.CreateContract(core.Contract{Hash: "x123"})
|
||||
contract := repository.FindContract("x123")
|
||||
Expect(contract).ToNot(BeNil())
|
||||
Expect(contract.Transactions).To(
|
||||
Equal([]core.Transaction{
|
||||
{Hash: "TRANSACTION1", To: "x123"},
|
||||
{Hash: "TRANSACTION3", To: "x123"},
|
||||
@ -254,13 +254,13 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
})
|
||||
|
||||
It("stores the ABI of the contract", func() {
|
||||
repository.CreateWatchedContract(core.WatchedContract{
|
||||
repository.CreateContract(core.Contract{
|
||||
Abi: "{\"some\": \"json\"}",
|
||||
Hash: "x123",
|
||||
})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Abi).To(Equal("{\"some\": \"json\"}"))
|
||||
contract := repository.FindContract("x123")
|
||||
Expect(contract).ToNot(BeNil())
|
||||
Expect(contract.Abi).To(Equal("{\"some\": \"json\"}"))
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user