forked from cerc-io/ipld-eth-server
Merge pull request #83 from 8thlight/refactor-two
Refactor Blockchain interface
This commit is contained in:
commit
7a11d3c50f
@ -5,6 +5,6 @@ type Blockchain interface {
|
||||
SubscribeToBlocks(blocks chan Block)
|
||||
StartListening()
|
||||
StopListening()
|
||||
GetContractAttributes(contractHash string) (ContractAttributes, error)
|
||||
GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error)
|
||||
GetContract(contractHash string) (Contract, error)
|
||||
GetAttribute(contract Contract, attributeName string) (interface{}, error)
|
||||
}
|
||||
|
@ -5,6 +5,11 @@ type WatchedContract struct {
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
type Contract struct {
|
||||
Attributes ContractAttributes
|
||||
Hash string
|
||||
}
|
||||
|
||||
type ContractAttribute struct {
|
||||
Name string
|
||||
Type string
|
||||
|
@ -13,20 +13,17 @@ type Blockchain struct {
|
||||
WasToldToStop bool
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
var contractAttributes core.ContractAttributes
|
||||
attributes, ok := blockchain.contractAttributes[contractHash]
|
||||
if ok {
|
||||
for key, _ := range attributes {
|
||||
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
||||
}
|
||||
func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) {
|
||||
contractAttributes, err := blockchain.getContractAttributes(contractHash)
|
||||
contract := core.Contract{
|
||||
Attributes: contractAttributes,
|
||||
Hash: contractHash,
|
||||
}
|
||||
sort.Sort(contractAttributes)
|
||||
return contractAttributes, nil
|
||||
return contract, err
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
||||
result := blockchain.contractAttributes[contractHash][attributeName]
|
||||
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
|
||||
result := blockchain.contractAttributes[contract.Hash][attributeName]
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -73,3 +70,15 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, att
|
||||
}
|
||||
blockchain.contractAttributes[contractHash][attributeName] = attributeValue
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
var contractAttributes core.ContractAttributes
|
||||
attributes, ok := blockchain.contractAttributes[contractHash]
|
||||
if ok {
|
||||
for key, _ := range attributes {
|
||||
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
||||
}
|
||||
}
|
||||
sort.Sort(contractAttributes)
|
||||
return contractAttributes, nil
|
||||
}
|
||||
|
@ -17,7 +17,33 @@ var (
|
||||
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
||||
)
|
||||
|
||||
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contract, error) {
|
||||
attributes, err := blockchain.getContractAttributes(contractHash)
|
||||
if err != nil {
|
||||
return core.Contract{}, err
|
||||
} else {
|
||||
contract := core.Contract{
|
||||
Attributes: attributes,
|
||||
Hash: contractHash,
|
||||
}
|
||||
return contract, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
|
||||
boundContract, err := bindContract(common.HexToAddress(contract.Hash), blockchain.client, blockchain.client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result interface{}
|
||||
err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidStateAttribute
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
|
||||
parsed, _ := ParseAbiFile(abiFilePath)
|
||||
var contractAttributes core.ContractAttributes
|
||||
@ -31,19 +57,6 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co
|
||||
return contractAttributes, nil
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
||||
boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result interface{}
|
||||
err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidStateAttribute
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {
|
||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
|
||||
parsed, err := ParseAbiFile(abiFilePath)
|
||||
|
@ -16,11 +16,11 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
||||
// contract, err := blockchain.GetContract(contractHash)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(len(attributes)).NotTo(Equal(0))
|
||||
// symbolAttribute := *testing.FindAttribute(attributes, "symbol")
|
||||
// Expect(len(contract.Attributes)).NotTo(Equal(0))
|
||||
// symbolAttribute := *testing.FindAttribute(contract.Attributes, "symbol")
|
||||
// Expect(symbolAttribute.Name).To(Equal("symbol"))
|
||||
// Expect(symbolAttribute.Type).To(Equal("string"))
|
||||
// })
|
||||
@ -30,10 +30,10 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
||||
// contract, err := blockchain.GetContract(contractHash)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// attribute := testing.FindAttribute(attributes, "balanceOf")
|
||||
// attribute := testing.FindAttribute(contract.Attributes, "balanceOf")
|
||||
// Expect(attribute).To(BeNil())
|
||||
// })
|
||||
//
|
||||
@ -42,10 +42,10 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
||||
// contract, err := blockchain.GetContract(contractHash)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// attribute := testing.FindAttribute(attributes, "unpause")
|
||||
// attribute := testing.FindAttribute(contract.Attributes, "unpause")
|
||||
// Expect(attribute).To(BeNil())
|
||||
// })
|
||||
// })
|
||||
@ -56,10 +56,11 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name")
|
||||
//
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// })
|
||||
//
|
||||
// It("returns an error when there is no ABI for the given contract", func() {
|
||||
@ -67,10 +68,11 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "MISSINGHASH"
|
||||
//
|
||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name")
|
||||
//
|
||||
// Expect(name).To(BeNil())
|
||||
// Expect(err).To(Equal(geth.ErrMissingAbiFile))
|
||||
// Expect(name).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("returns an error when asking for an attribute that does not exist", func() {
|
||||
@ -78,7 +80,8 @@ package geth_test
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute")
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "missing_attribute")
|
||||
//
|
||||
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
||||
// Expect(name).To(BeNil())
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type ContractSummary struct {
|
||||
Contract core.Contract
|
||||
ContractHash string
|
||||
NumberOfTransactions int
|
||||
LastTransaction *core.Transaction
|
||||
@ -30,18 +31,19 @@ func NewSummary(blockchain core.Blockchain, repository repositories.Repository,
|
||||
}
|
||||
|
||||
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
|
||||
result, _ := contractSummary.blockChain.GetContractStateAttribute(contractSummary.ContractHash, attributeName)
|
||||
result, _ := contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName)
|
||||
return result
|
||||
}
|
||||
|
||||
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
|
||||
attributes, _ := blockchain.GetContractAttributes(watchedContract.Hash)
|
||||
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
||||
return &ContractSummary{
|
||||
blockChain: blockchain,
|
||||
Contract: contract,
|
||||
ContractHash: watchedContract.Hash,
|
||||
NumberOfTransactions: len(watchedContract.Transactions),
|
||||
LastTransaction: lastTransaction(watchedContract),
|
||||
Attributes: attributes,
|
||||
Attributes: contract.Attributes,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user