Remove unused contract methods

This commit is contained in:
Matt Krump 2018-03-12 14:09:20 -05:00 committed by Matt K
parent 6c5bc673c0
commit 5a2bb04670
10 changed files with 5 additions and 456 deletions

View File

@ -14,84 +14,7 @@ import (
var _ = Describe("Reading contracts", func() {
Describe("Reading the list of attributes", func() {
It("returns a string attribute for a real contract", func() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.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() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.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() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.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() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.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() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.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() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.IPCPath)
contract := testing.SampleContract()
name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4701536))
Expect(name).To(Equal("OMGToken"))
Expect(err).To(BeNil())
})
It("returns an error when asking for an attribute that does not exist", func() {
blockchain := geth.NewBlockchain(test_config.InfuraClient.IPCPath)
contract := testing.SampleContract()
name, err := blockchain.GetAttribute(contract, "missing_attribute", nil)
Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
Expect(name).To(BeNil())
})
It("retrieves the event log for a specific block and contract", func() {
expectedLogZero := core.Log{
BlockNumber: 4703824,

View File

@ -1,59 +0,0 @@
package contract_summary
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
func GenerateConsoleOutput(summary ContractSummary) string {
return fmt.Sprintf(template(),
summary.ContractHash,
summary.NumberOfTransactions,
transactionToString(summary.LastTransaction),
attributesString(summary),
)
}
func template() string {
return `********************Contract Summary***********************
HASH: %v
NUMBER OF TRANSACTIONS: %d
LAST TRANSACTION:
%s
ATTRIBUTES:
%s
`
}
func transactionToString(transaction *core.Transaction) string {
if transaction == nil {
return "NONE"
}
return fmt.Sprintf(`Hash: %s
To: %s
From: %s`, transaction.Hash, transaction.To, transaction.From)
}
func attributesString(summary ContractSummary) string {
var formattedAttributes string
for _, attribute := range summary.Attributes {
formattedAttributes += formatAttribute(attribute.Name, summary) + "\n" + " "
}
return formattedAttributes
}
func formatAttribute(attributeName string, summary ContractSummary) string {
var stringResult string
result := summary.GetStateAttribute(attributeName)
switch t := result.(type) {
case common.Address:
ca := result.(common.Address)
stringResult = fmt.Sprintf("%s: %v", attributeName, ca.Hex())
default:
_ = t
stringResult = fmt.Sprintf("%s: %v", attributeName, result)
}
return stringResult
}

View File

@ -1,13 +0,0 @@
package contract_summary_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestContractSummary(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ContractSummary Suite")
}

View File

@ -1,52 +0,0 @@
package contract_summary
import (
"math/big"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
)
type ContractSummary struct {
Attributes core.ContractAttributes
BlockNumber *big.Int
Contract core.Contract
ContractHash string
LastTransaction *core.Transaction
NumberOfTransactions int
blockChain core.Blockchain
}
func NewSummary(blockchain core.Blockchain, contractRepository datastore.ContractRepository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
contract, err := contractRepository.GetContract(contractHash)
if err != nil {
return ContractSummary{}, err
}
return newContractSummary(blockchain, contract, blockNumber), nil
}
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
var result interface{}
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
return result
}
func newContractSummary(blockchain core.Blockchain, contract core.Contract, blockNumber *big.Int) ContractSummary {
attributes, _ := blockchain.GetAttributes(contract)
return ContractSummary{
Attributes: attributes,
BlockNumber: blockNumber,
Contract: contract,
ContractHash: contract.Hash,
LastTransaction: lastTransaction(contract),
NumberOfTransactions: len(contract.Transactions),
blockChain: blockchain,
}
}
func lastTransaction(contract core.Contract) *core.Transaction {
if len(contract.Transactions) > 0 {
return &contract.Transactions[0]
}
return nil
}

View File

@ -1,149 +0,0 @@
package contract_summary_test
import (
"math/big"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/contract_summary"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
)
func NewCurrentContractSummary(blockchain core.Blockchain, contractRepository datastore.ContractRepository, contractHash string) (contract_summary.ContractSummary, error) {
return contract_summary.NewSummary(blockchain, contractRepository, contractHash, nil)
}
var _ = Describe("The contract summary", func() {
var inMemoryDB *inmemory.InMemory
var contractRepostiory *inmemory.ContractRepostiory
BeforeEach(func() {
inMemoryDB = inmemory.NewInMemory()
contractRepostiory = &inmemory.ContractRepostiory{InMemory: inMemoryDB}
})
Context("when the given contract does not exist", func() {
It("returns an error", func() {
blockchain := fakes.NewBlockchain()
contractSummary, err := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary).To(Equal(contract_summary.ContractSummary{}))
Expect(err).NotTo(BeNil())
})
})
Context("when the given contract exists", func() {
It("returns the summary", func() {
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
blockchain := fakes.NewBlockchain()
contractSummary, err := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary).NotTo(Equal(contract_summary.ContractSummary{}))
Expect(err).To(BeNil())
})
It("includes the contract hash in the summary", func() {
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
blockchain := fakes.NewBlockchain()
contractSummary, _ := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary.ContractHash).To(Equal("0x123"))
})
It("sets the number of transactions", func() {
blocks := &inmemory.BlockRepository{InMemory: inMemoryDB}
blockchain := fakes.NewBlockchain()
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
block := core.Block{
Transactions: []core.Transaction{
{To: "0x123"},
{To: "0x123"},
},
}
blocks.CreateOrUpdateBlock(block)
contractSummary, _ := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary.NumberOfTransactions).To(Equal(2))
})
It("sets the last transaction", func() {
blocks := &inmemory.BlockRepository{InMemory: inMemoryDB}
blockchain := fakes.NewBlockchain()
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
block := core.Block{
Transactions: []core.Transaction{
{Hash: "TRANSACTION2", To: "0x123"},
{Hash: "TRANSACTION1", To: "0x123"},
},
}
blocks.CreateOrUpdateBlock(block)
contractSummary, _ := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2"))
})
It("gets contract state attribute for the contract from the blockchain", func() {
blockchain := fakes.NewBlockchain()
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
contractSummary, _ := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
attribute := contractSummary.GetStateAttribute("foo")
Expect(attribute).To(Equal("bar"))
})
It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
blockchain := fakes.NewBlockchain()
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
blockNumber := big.NewInt(1000)
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
blockchain.SetContractStateAttribute("0x123", blockNumber, "foo", "baz")
contractSummary, _ := contract_summary.NewSummary(blockchain, contractRepostiory, "0x123", blockNumber)
attribute := contractSummary.GetStateAttribute("foo")
Expect(attribute).To(Equal("baz"))
})
It("gets attributes for the contract from the blockchain", func() {
blockchain := fakes.NewBlockchain()
contract := core.Contract{Hash: "0x123"}
contractRepostiory.CreateContract(contract)
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
blockchain.SetContractStateAttribute("0x123", nil, "baz", "bar")
contractSummary, _ := NewCurrentContractSummary(blockchain, contractRepostiory, "0x123")
Expect(contractSummary.Attributes).To(Equal(
core.ContractAttributes{
{Name: "baz", Type: "string"},
{Name: "foo", Type: "string"},
},
))
})
})
})

View File

@ -3,8 +3,7 @@ package core
import "math/big"
type Blockchain interface {
GetAttribute(contract Contract, attributeName string, blockNumber *big.Int) (interface{}, error)
GetAttributes(contract Contract) (ContractAttributes, error)
ContractDataFetcher
GetBlockByNumber(blockNumber int64) Block
GetLogs(contract Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]Log, error)
LastBlock() *big.Int

View File

@ -1,20 +0,0 @@
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
}

View File

@ -1,8 +1,6 @@
package fakes
import (
"sort"
"math/big"
"github.com/vulcanize/vulcanizedb/pkg/core"
@ -18,6 +16,10 @@ type Blockchain struct {
ContractReturnValue []byte
}
func (blockchain *Blockchain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
panic("implement me")
}
func (blockchain *Blockchain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
return blockchain.ContractReturnValue, nil
}
@ -40,16 +42,6 @@ func (blockchain *Blockchain) Node() core.Node {
return blockchain.node
}
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
var result interface{}
if blockNumber == nil {
result = blockchain.contractAttributes[contract.Hash+"-1"][attributeName]
} else {
result = blockchain.contractAttributes[contract.Hash+blockNumber.String()][attributeName]
}
return result, nil
}
func NewBlockchain() *Blockchain {
return &Blockchain{
blocks: make(map[int64]core.Block),
@ -77,29 +69,3 @@ func (blockchain *Blockchain) AddBlock(block core.Block) {
blockchain.blocks[block.Number] = block
blockchain.blocksChannel <- block
}
func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blockNumber *big.Int, attributeName string, attributeValue string) {
var key string
if blockNumber == nil {
key = contractHash + "-1"
} else {
key = contractHash + blockNumber.String()
}
contractStateAttributes := blockchain.contractAttributes[key]
if contractStateAttributes == nil {
blockchain.contractAttributes[key] = make(map[string]string)
}
blockchain.contractAttributes[key][attributeName] = attributeValue
}
func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
var contractAttributes core.ContractAttributes
attributes, ok := blockchain.contractAttributes[contract.Hash+"-1"]
if ok {
for key := range attributes {
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
}
}
sort.Sort(contractAttributes)
return contractAttributes, nil
}

View File

@ -3,41 +3,17 @@ package geth
import (
"errors"
"sort"
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
var (
ErrInvalidStateAttribute = errors.New("invalid state attribute")
)
func (blockchain *Blockchain) 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
}
input, err := parsed.Pack(attributeName)
if err != nil {
return nil, ErrInvalidStateAttribute
}
output, err := blockchain.callContract(contract.Hash, input, blockNumber)
if err != nil {
return nil, err
}
err = parsed.Unpack(&result, attributeName, output)
if err != nil {
return nil, err
}
return result, nil
}
func (blockchain *Blockchain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
parsed, err := ParseAbi(abiJSON)
if err != nil {
@ -59,16 +35,3 @@ func (blockchain *Blockchain) callContract(contractHash string, input []byte, bl
msg := ethereum.CallMsg{To: &to, Data: input}
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
}
func (blockchain *Blockchain) 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 {
attributeType := abiElement.Outputs[0].Type.String()
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: abiElement.Name, Type: attributeType})
}
}
sort.Sort(contractAttributes)
return contractAttributes, nil
}

View File

@ -8,15 +8,6 @@ import (
"github.com/vulcanize/vulcanizedb/test_config"
)
func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute {
for _, contractAttribute := range contractAttributes {
if contractAttribute.Name == attributeName {
return &contractAttribute
}
}
return nil
}
func SampleContract() core.Contract {
return core.Contract{
Abi: sampleAbiFileContents(),