Allow for multiple Geth nodes (#128)

This commit is contained in:
Matt K 2018-01-10 15:54:36 -06:00 committed by GitHub
parent f41bf49b0e
commit a9bea4f492
12 changed files with 98 additions and 26 deletions

View File

@ -0,0 +1,3 @@
ALTER TABLE nodes
DROP COLUMN node_id,
DROP COLUMN client_name;

View File

@ -0,0 +1,3 @@
ALTER TABLE nodes
ADD COLUMN node_id VARCHAR(128),
ADD COLUMN client_name VARCHAR;

View File

@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE nodes
DROP CONSTRAINT node_uc;
ALTER TABLE nodes
ADD CONSTRAINT node_uc UNIQUE (genesis_block, network_id);
COMMIT;

View File

@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE nodes
DROP CONSTRAINT node_uc;
ALTER TABLE nodes
ADD CONSTRAINT node_uc UNIQUE (genesis_block, network_id, node_id);
COMMIT;

View File

@ -52,6 +52,8 @@ var _ = Describe("Reading from the Geth blockchain", func() {
Expect(node.GenesisBlock).To(Equal(devNetworkGenesisBlock)) Expect(node.GenesisBlock).To(Equal(devNetworkGenesisBlock))
Expect(node.NetworkId).To(Equal(devNetworkNodeId)) Expect(node.NetworkId).To(Equal(devNetworkNodeId))
Expect(len(node.Id)).To(Equal(128))
Expect(node.ClientName).To(ContainSubstring("Geth"))
close(done) close(done)
}, 15) }, 15)

View File

@ -3,4 +3,6 @@ package core
type Node struct { type Node struct {
GenesisBlock string GenesisBlock string
NetworkId float64 NetworkId float64
Id string
ClientName string
} }

View File

@ -50,7 +50,7 @@ func NewBlockchain() *Blockchain {
blocks: make(map[int64]core.Block), blocks: make(map[int64]core.Block),
logs: make(map[string][]core.Log), logs: make(map[string][]core.Log),
contractAttributes: make(map[string]map[string]string), contractAttributes: make(map[string]map[string]string),
node: core.Node{GenesisBlock: "GENESIS"}, node: core.Node{GenesisBlock: "GENESIS", NetworkId: 1, Id: "x123", ClientName: "Geth"},
} }
} }

View File

@ -3,6 +3,8 @@ package geth
import ( import (
"math/big" "math/big"
"strings"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth/node" "github.com/8thlight/vulcanizedb/pkg/geth/node"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
@ -25,11 +27,22 @@ func NewBlockchain(ipcPath string) *Blockchain {
blockchain := Blockchain{} blockchain := Blockchain{}
rpcClient, _ := rpc.Dial(ipcPath) rpcClient, _ := rpc.Dial(ipcPath)
client := ethclient.NewClient(rpcClient) client := ethclient.NewClient(rpcClient)
blockchain.node = node.Retrieve(rpcClient) blockchain.node = node.Info(rpcClient)
if infura := isInfuraNode(ipcPath); infura {
blockchain.node.Id = "infura"
blockchain.node.ClientName = "infura"
}
blockchain.client = client blockchain.client = client
return &blockchain return &blockchain
} }
func isInfuraNode(ipcPath string) bool {
if strings.Contains(ipcPath, "infura") {
return true
}
return false
}
func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) { func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) {
if endingBlockNumber == nil { if endingBlockNumber == nil {
endingBlockNumber = startingBlockNumber endingBlockNumber = startingBlockNumber

View File

@ -7,22 +7,43 @@ import (
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
func Retrieve(client *rpc.Client) core.Node { func Info(client *rpc.Client) core.Node {
node := core.Node{} node := core.Node{}
node.NetworkId = NetworkId(client)
var version string node.GenesisBlock = GenesisBlock(client)
client.CallContext(context.Background(), &version, "net_version") node.Id, node.ClientName = IdClientName(client)
node.NetworkId, _ = strconv.ParseFloat(version, 64)
var protocolVersion string
client.CallContext(context.Background(), &protocolVersion, "eth_protocolVersion")
var header *types.Header
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
node.GenesisBlock = header.Hash().Hex()
return node return node
} }
func IdClientName(client *rpc.Client) (string, string) {
var info p2p.NodeInfo
modules, _ := client.SupportedModules()
if _, ok := modules["admin"]; ok {
client.CallContext(context.Background(), &info, "admin_nodeInfo")
return info.ID, info.Name
}
return "", ""
}
func NetworkId(client *rpc.Client) float64 {
var version string
client.CallContext(context.Background(), &version, "net_version")
networkId, _ := strconv.ParseFloat(version, 64)
return networkId
}
func ProtocolVersion(client *rpc.Client) string {
var protocolVersion string
client.CallContext(context.Background(), &protocolVersion, "eth_protocolVersion")
return protocolVersion
}
func GenesisBlock(client *rpc.Client) string {
var header *types.Header
client.CallContext(context.Background(), &header, "eth_getBlockByNumber", "0x0", false)
return header.Hash().Hex()
}

View File

@ -111,13 +111,16 @@ func (repository Postgres) FindLogs(address string, blockNumber int64) []core.Lo
func (repository *Postgres) CreateNode(node *core.Node) error { func (repository *Postgres) CreateNode(node *core.Node) error {
var nodeId int64 var nodeId int64
err := repository.Db.QueryRow( err := repository.Db.QueryRow(
`INSERT INTO nodes (genesis_block, network_id) `INSERT INTO nodes (genesis_block, network_id, node_id, client_name)
VALUES ($1, $2) VALUES ($1, $2, $3, $4)
ON CONFLICT (genesis_block, network_id) ON CONFLICT (genesis_block, network_id, node_id)
DO UPDATE DO UPDATE
SET genesis_block = $1, network_id = $2 SET genesis_block = $1,
network_id = $2,
node_id = $3,
client_name = $4
RETURNING id`, RETURNING id`,
node.GenesisBlock, node.NetworkId).Scan(&nodeId) node.GenesisBlock, node.NetworkId, node.Id, node.ClientName).Scan(&nodeId)
if err != nil { if err != nil {
return ErrUnableToSetNode return ErrUnableToSetNode
} }

View File

@ -47,7 +47,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{}, Transactions: []core.Transaction{},
} }
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1} node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1, Id: "x123", ClientName: "geth"}
repository, _ := repositories.NewPostgres(cfg.Database, node) repository, _ := repositories.NewPostgres(cfg.Database, node)
err1 := repository.CreateOrUpdateBlock(badBlock) err1 := repository.CreateOrUpdateBlock(badBlock)
@ -60,7 +60,7 @@ var _ = Describe("Postgres repository", func() {
It("throws error when can't connect to the database", func() { It("throws error when can't connect to the database", func() {
invalidDatabase := config.Database{} invalidDatabase := config.Database{}
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1} node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1, Id: "x123", ClientName: "geth"}
_, err := repositories.NewPostgres(invalidDatabase, node) _, err := repositories.NewPostgres(invalidDatabase, node)
Expect(err).To(Equal(repositories.ErrDBConnectionFailed)) Expect(err).To(Equal(repositories.ErrDBConnectionFailed))
}) })
@ -68,7 +68,7 @@ var _ = Describe("Postgres repository", func() {
It("throws error when can't create node", func() { It("throws error when can't create node", func() {
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100)) badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
node := core.Node{GenesisBlock: badHash, NetworkId: 1} node := core.Node{GenesisBlock: badHash, NetworkId: 1, Id: "x123", ClientName: "geth"}
_, err := repositories.NewPostgres(cfg.Database, node) _, err := repositories.NewPostgres(cfg.Database, node)
Expect(err).To(Equal(repositories.ErrUnableToSetNode)) Expect(err).To(Equal(repositories.ErrUnableToSetNode))
}) })
@ -82,7 +82,7 @@ var _ = Describe("Postgres repository", func() {
TxHash: badTxHash, TxHash: badTxHash,
} }
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1} node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1, Id: "x123", ClientName: "geth"}
repository, _ := repositories.NewPostgres(cfg.Database, node) repository, _ := repositories.NewPostgres(cfg.Database, node)
err := repository.CreateLogs([]core.Log{badLog}) err := repository.CreateLogs([]core.Log{badLog})
@ -101,7 +101,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{badTransaction}, Transactions: []core.Transaction{badTransaction},
} }
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1} node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1, Id: "x123", ClientName: "geth"}
repository, _ := repositories.NewPostgres(cfg.Database, node) repository, _ := repositories.NewPostgres(cfg.Database, node)
err1 := repository.CreateOrUpdateBlock(block) err1 := repository.CreateOrUpdateBlock(block)

View File

@ -22,7 +22,12 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
var repository repositories.Repository var repository repositories.Repository
BeforeEach(func() { BeforeEach(func() {
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1} node := core.Node{
GenesisBlock: "GENESIS",
NetworkId: 1,
Id: "b6f90c0fdd8ec9607aed8ee45c69322e47b7063f0bfb7a29c8ecafab24d0a22d24dd2329b5ee6ed4125a03cb14e57fd584e67f9e53e6c631055cbbd82f080845",
ClientName: "Geth/v1.7.2-stable-1db4ecdc/darwin-amd64/go1.9",
}
repository = buildRepository(node) repository = buildRepository(node)
}) })
@ -48,6 +53,8 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
nodeTwo := core.Node{ nodeTwo := core.Node{
GenesisBlock: "0x456", GenesisBlock: "0x456",
NetworkId: 1, NetworkId: 1,
Id: "x123456",
ClientName: "Geth",
} }
repositoryTwo := buildRepository(nodeTwo) repositoryTwo := buildRepository(nodeTwo)