Handle events

- Adds interfaces for developers to build handlers that update data in
response to log events
- Resolves #29
This commit is contained in:
Matt Krump
2018-03-05 10:01:50 -06:00
parent ed907535e3
commit 06f78e0083
163 changed files with 586 additions and 22397 deletions
+7 -8
View File
@@ -25,20 +25,19 @@ type Response struct {
Result string
}
type EtherScanApi struct {
type EtherScanAPI struct {
client *http.Client
url string
}
func NewEtherScanClient(url string) *EtherScanApi {
return &EtherScanApi{
func NewEtherScanClient(url string) *EtherScanAPI {
return &EtherScanAPI{
client: &http.Client{Timeout: 10 * time.Second},
url: url,
}
}
func GenUrl(network string) string {
func GenURL(network string) string {
switch network {
case "ropsten":
return "https://ropsten.etherscan.io"
@@ -52,7 +51,7 @@ func GenUrl(network string) string {
}
//https://api.etherscan.io/api?module=contract&action=getabi&address=%s
func (e *EtherScanApi) GetAbi(contractHash string) (string, error) {
func (e *EtherScanAPI) GetAbi(contractHash string) (string, error) {
target := new(Response)
request := fmt.Sprintf("%s/api?module=contract&action=getabi&address=%s", e.url, contractHash)
r, err := e.client.Get(request)
@@ -60,8 +59,8 @@ func (e *EtherScanApi) GetAbi(contractHash string) (string, error) {
return "", ErrApiRequestFailed
}
defer r.Body.Close()
json.NewDecoder(r.Body).Decode(&target)
return target.Result, nil
err = json.NewDecoder(r.Body).Decode(&target)
return target.Result, err
}
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
+16 -19
View File
@@ -1,20 +1,16 @@
package geth_test
import (
"path/filepath"
"net/http"
"fmt"
"log"
"github.com/ethereum/go-ethereum/accounts/abi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
cfg "github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/test_config"
)
var _ = Describe("ABI files", func() {
@@ -22,7 +18,7 @@ var _ = Describe("ABI files", func() {
Describe("Reading ABI files", func() {
It("loads a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
path := test_config.ABIFilePath + "valid_abi.json"
contractAbi, err := geth.ParseAbiFile(path)
@@ -31,7 +27,7 @@ var _ = Describe("ABI files", func() {
})
It("reads the contents of a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
path := test_config.ABIFilePath + "valid_abi.json"
contractAbi, err := geth.ReadAbiFile(path)
@@ -40,7 +36,7 @@ var _ = Describe("ABI files", func() {
})
It("returns an error when the file does not exist", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
path := test_config.ABIFilePath + "missing_abi.json"
contractAbi, err := geth.ParseAbiFile(path)
@@ -49,7 +45,7 @@ var _ = Describe("ABI files", func() {
})
It("returns an error when the file has invalid contents", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "invalid_abi.json")
path := test_config.ABIFilePath + "invalid_abi.json"
contractAbi, err := geth.ParseAbiFile(path)
@@ -61,19 +57,20 @@ var _ = Describe("ABI files", func() {
var (
server *ghttp.Server
client *geth.EtherScanApi
client *geth.EtherScanAPI
abiString string
err error
)
BeforeEach(func() {
server = ghttp.NewServer()
client = geth.NewEtherScanClient(server.URL())
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "sample_abi.json")
abiString, err := geth.ReadAbiFile(path)
path := test_config.ABIFilePath + "sample_abi.json"
abiString, err = geth.ReadAbiFile(path)
Expect(err).NotTo(HaveOccurred())
_, err = geth.ParseAbi(abiString)
if err != nil {
log.Fatalln("Could not parse ABI")
}
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
@@ -104,14 +101,14 @@ var _ = Describe("ABI files", func() {
Describe("Generating etherscan endpoints based on network", func() {
It("should return the main endpoint as the default", func() {
url := geth.GenUrl("")
url := geth.GenURL("")
Expect(url).To(Equal("https://api.etherscan.io"))
})
It("generates various test network endpoint if test network is supplied", func() {
ropstenUrl := geth.GenUrl("ropsten")
rinkebyUrl := geth.GenUrl("rinkeby")
kovanUrl := geth.GenUrl("kovan")
ropstenUrl := geth.GenURL("ropsten")
rinkebyUrl := geth.GenURL("rinkeby")
kovanUrl := geth.GenURL("kovan")
Expect(ropstenUrl).To(Equal("https://ropsten.etherscan.io"))
Expect(kovanUrl).To(Equal("https://kovan.etherscan.io"))
+5 -6
View File
@@ -12,12 +12,12 @@ import (
"golang.org/x/net/context"
)
type GethClient interface {
type Client interface {
TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
}
func ToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
func ToCoreBlock(gethBlock *types.Block, client Client) core.Block {
transactions := convertTransactionsToCore(gethBlock, client)
coreBlock := core.Block{
Difficulty: gethBlock.Difficulty().Int64(),
@@ -39,7 +39,7 @@ func ToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
return coreBlock
}
func convertTransactionsToCore(gethBlock *types.Block, client GethClient) []core.Transaction {
func convertTransactionsToCore(gethBlock *types.Block, client Client) []core.Transaction {
transactions := make([]core.Transaction, 0)
for i, gethTransaction := range gethBlock.Transactions() {
from, err := client.TransactionSender(context.Background(), gethTransaction, gethBlock.Hash(), uint(i))
@@ -56,7 +56,7 @@ func convertTransactionsToCore(gethBlock *types.Block, client GethClient) []core
return transactions
}
func appendReceiptToTransaction(client GethClient, transaction core.Transaction) (core.Transaction, error) {
func appendReceiptToTransaction(client Client, transaction core.Transaction) (core.Transaction, error) {
gethReceipt, err := client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
if err != nil {
log.Println(err)
@@ -84,7 +84,6 @@ func transToCoreTrans(transaction *types.Transaction, from *common.Address) core
func addressToHex(to *common.Address) string {
if to == nil {
return ""
} else {
return to.Hex()
}
return to.Hex()
}
+2 -1
View File
@@ -29,12 +29,13 @@ func NewBlockchain(ipcPath string) *Blockchain {
blockchain := Blockchain{}
rpcClient, err := rpc.Dial(ipcPath)
if err != nil {
log.Println("Unable to connect to node")
log.Fatal(err)
}
client := ethclient.NewClient(rpcClient)
blockchain.node = node.Info(rpcClient)
if infura := isInfuraNode(ipcPath); infura {
blockchain.node.Id = "infura"
blockchain.node.ID = "infura"
blockchain.node.ClientName = "infura"
}
blockchain.client = client
+18 -2
View File
@@ -27,7 +27,7 @@ func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName
if err != nil {
return nil, ErrInvalidStateAttribute
}
output, err := callContract(contract.Hash, input, blockchain, blockNumber)
output, err := blockchain.callContract(contract.Hash, input, blockNumber)
if err != nil {
return nil, err
}
@@ -38,7 +38,23 @@ func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName
return result, nil
}
func callContract(contractHash string, input []byte, blockchain *Blockchain, blockNumber *big.Int) ([]byte, error) {
func (blockchain *Blockchain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
parsed, err := ParseAbi(abiJSON)
if err != nil {
return err
}
input, err := parsed.Pack(method, methodArg)
if err != nil {
return err
}
output, err := blockchain.callContract(address, input, big.NewInt(blockNumber))
if err != nil {
return err
}
return parsed.Unpack(result, method, output)
}
func (blockchain *Blockchain) callContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
to := common.HexToAddress(contractHash)
msg := ethereum.CallMsg{To: &to, Data: input}
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
+4 -4
View File
@@ -13,13 +13,13 @@ import (
func Info(client *rpc.Client) core.Node {
node := core.Node{}
node.NetworkId = NetworkId(client)
node.NetworkID = NetworkID(client)
node.GenesisBlock = GenesisBlock(client)
node.Id, node.ClientName = IdClientName(client)
node.ID, node.ClientName = IDClientName(client)
return node
}
func IdClientName(client *rpc.Client) (string, string) {
func IDClientName(client *rpc.Client) (string, string) {
var info p2p.NodeInfo
modules, _ := client.SupportedModules()
if _, ok := modules["admin"]; ok {
@@ -29,7 +29,7 @@ func IdClientName(client *rpc.Client) (string, string) {
return "", ""
}
func NetworkId(client *rpc.Client) float64 {
func NetworkID(client *rpc.Client) float64 {
var version string
client.CallContext(context.Background(), &version, "net_version")
networkId, _ := strconv.ParseFloat(version, 64)
+6 -4
View File
@@ -1,11 +1,11 @@
package testing
import (
"path/filepath"
"log"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/test_config"
)
func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute {
@@ -25,7 +25,9 @@ func SampleContract() core.Contract {
}
func sampleAbiFileContents() string {
abiFilepath := filepath.Join(config.ProjectRoot(), "pkg", "geth", "testing", "sample_abi.json")
abiFileContents, _ := geth.ReadAbiFile(abiFilepath)
abiFileContents, err := geth.ReadAbiFile(test_config.ABIFilePath + "sample_abi.json")
if err != nil {
log.Fatal(err)
}
return abiFileContents
}