Add tests for pkg/geth/blockchain

- inject dependencies instead of initializing them in the constructor
This commit is contained in:
Rob Mulholand
2018-07-18 16:34:13 -05:00
parent 1355271011
commit 63434f6bc9
20 changed files with 538 additions and 106 deletions
+2 -2
View File
@@ -82,8 +82,8 @@ func coldImport() {
// init cold importer deps
blockRepository := repositories.NewBlockRepository(&pgDB)
receiptRepository := repositories.ReceiptRepository{DB: &pgDB}
transactionconverter := cold_db.NewColdDbTransactionConverter()
blockConverter := vulcCommon.NewBlockConverter(transactionconverter)
transactionConverter := cold_db.NewColdDbTransactionConverter()
blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
// init and execute cold importer
coldImporter := cold_import.NewColdImporter(ethDB, blockRepository, receiptRepository, blockConverter)
+17 -3
View File
@@ -15,11 +15,16 @@
package cmd
import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/libraries/shared"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"log"
"time"
)
@@ -49,14 +54,23 @@ Expects an ethereum node to be running and requires a .toml config file:
func watchERC20s() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
blockchain := geth.NewBlockChain(ipc)
db, err := postgres.NewDB(databaseConfig, blockchain.Node())
rpcClient, err := rpc.Dial(ipc)
if err != nil {
log.Fatal(err)
}
ethClient := ethclient.NewClient(rpcClient)
client := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{ContextCaller: rpcClient, IPCPath: ipc}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(client, node, transactionConverter)
db, err := postgres.NewDB(databaseConfig, blockChain.Node())
if err != nil {
log.Fatal("Failed to initialize database.")
}
watcher := shared.Watcher{
DB: *db,
Blockchain: blockchain,
Blockchain: blockChain,
}
watcher.AddTransformers(every_block.TransformerInitializers())
+15 -1
View File
@@ -15,11 +15,16 @@
package cmd
import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"github.com/vulcanize/vulcanizedb/pkg/history"
"github.com/vulcanize/vulcanizedb/utils"
"log"
@@ -63,7 +68,16 @@ func backFillAllHeaders(blockchain core.Blockchain, headerRepository datastore.H
func lightSync() {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
blockChain := geth.NewBlockChain(ipc)
rpcClient, err := rpc.Dial(ipc)
if err != nil {
log.Fatal(err)
}
ethClient := ethclient.NewClient(rpcClient)
client := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{ContextCaller: rpcClient, IPCPath: ipc}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(client, node, transactionConverter)
lastBlock := blockChain.LastBlock().Int64()
if lastBlock == 0 {
+20 -6
View File
@@ -21,11 +21,16 @@ import (
"log"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"github.com/vulcanize/vulcanizedb/pkg/history"
"github.com/vulcanize/vulcanizedb/utils"
)
@@ -72,9 +77,18 @@ func backFillAllBlocks(blockchain core.Blockchain, blockRepository datastore.Blo
func sync() {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
blockchain := geth.NewBlockChain(ipc)
rpcClient, err := rpc.Dial(ipc)
if err != nil {
log.Fatal(err)
}
ethClient := ethclient.NewClient(rpcClient)
client := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{ContextCaller: rpcClient, IPCPath: ipc}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(client, node, transactionConverter)
lastBlock := blockchain.LastBlock().Int64()
lastBlock := blockChain.LastBlock().Int64()
if lastBlock == 0 {
log.Fatal("geth initial: state sync not finished")
}
@@ -82,11 +96,11 @@ func sync() {
log.Fatal("starting block number > current block number")
}
db := utils.LoadPostgres(databaseConfig, blockchain.Node())
db := utils.LoadPostgres(databaseConfig, blockChain.Node())
blockRepository := repositories.NewBlockRepository(&db)
validator := history.NewBlockValidator(blockchain, blockRepository, validationWindow)
validator := history.NewBlockValidator(blockChain, blockRepository, validationWindow)
missingBlocksPopulated := make(chan int)
go backFillAllBlocks(blockchain, blockRepository, missingBlocksPopulated, startingBlockNumber)
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
for {
select {
@@ -94,7 +108,7 @@ func sync() {
window := validator.ValidateBlocks()
window.Log(os.Stdout)
case <-missingBlocksPopulated:
go backFillAllBlocks(blockchain, blockRepository, missingBlocksPopulated, startingBlockNumber)
go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
}
}
}