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 15:59:40 -05:00
parent 1355271011
commit 63434f6bc9
20 changed files with 538 additions and 106 deletions

View File

@ -82,8 +82,8 @@ func coldImport() {
// init cold importer deps // init cold importer deps
blockRepository := repositories.NewBlockRepository(&pgDB) blockRepository := repositories.NewBlockRepository(&pgDB)
receiptRepository := repositories.ReceiptRepository{DB: &pgDB} receiptRepository := repositories.ReceiptRepository{DB: &pgDB}
transactionconverter := cold_db.NewColdDbTransactionConverter() transactionConverter := cold_db.NewColdDbTransactionConverter()
blockConverter := vulcCommon.NewBlockConverter(transactionconverter) blockConverter := vulcCommon.NewBlockConverter(transactionConverter)
// init and execute cold importer // init and execute cold importer
coldImporter := cold_import.NewColdImporter(ethDB, blockRepository, receiptRepository, blockConverter) coldImporter := cold_import.NewColdImporter(ethDB, blockRepository, receiptRepository, blockConverter)

View File

@ -15,11 +15,16 @@
package cmd package cmd
import ( import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block" "github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/libraries/shared" "github.com/vulcanize/vulcanizedb/libraries/shared"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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" "log"
"time" "time"
) )
@ -49,14 +54,23 @@ Expects an ethereum node to be running and requires a .toml config file:
func watchERC20s() { func watchERC20s() {
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop() defer ticker.Stop()
blockchain := geth.NewBlockChain(ipc) rpcClient, err := rpc.Dial(ipc)
db, err := postgres.NewDB(databaseConfig, blockchain.Node()) 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 { if err != nil {
log.Fatal("Failed to initialize database.") log.Fatal("Failed to initialize database.")
} }
watcher := shared.Watcher{ watcher := shared.Watcher{
DB: *db, DB: *db,
Blockchain: blockchain, Blockchain: blockChain,
} }
watcher.AddTransformers(every_block.TransformerInitializers()) watcher.AddTransformers(every_block.TransformerInitializers())

View File

@ -15,11 +15,16 @@
package cmd package cmd
import ( import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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/pkg/history"
"github.com/vulcanize/vulcanizedb/utils" "github.com/vulcanize/vulcanizedb/utils"
"log" "log"
@ -63,7 +68,16 @@ func backFillAllHeaders(blockchain core.Blockchain, headerRepository datastore.H
func lightSync() { func lightSync() {
ticker := time.NewTicker(pollingInterval) ticker := time.NewTicker(pollingInterval)
defer ticker.Stop() 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 { if lastBlock == 0 {

View File

@ -21,11 +21,16 @@ import (
"log" "log"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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/pkg/history"
"github.com/vulcanize/vulcanizedb/utils" "github.com/vulcanize/vulcanizedb/utils"
) )
@ -72,9 +77,18 @@ func backFillAllBlocks(blockchain core.Blockchain, blockRepository datastore.Blo
func sync() { func sync() {
ticker := time.NewTicker(pollingInterval) ticker := time.NewTicker(pollingInterval)
defer ticker.Stop() 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 { if lastBlock == 0 {
log.Fatal("geth initial: state sync not finished") log.Fatal("geth initial: state sync not finished")
} }
@ -82,11 +96,11 @@ func sync() {
log.Fatal("starting block number > current block number") log.Fatal("starting block number > current block number")
} }
db := utils.LoadPostgres(databaseConfig, blockchain.Node()) db := utils.LoadPostgres(databaseConfig, blockChain.Node())
blockRepository := repositories.NewBlockRepository(&db) blockRepository := repositories.NewBlockRepository(&db)
validator := history.NewBlockValidator(blockchain, blockRepository, validationWindow) validator := history.NewBlockValidator(blockChain, blockRepository, validationWindow)
missingBlocksPopulated := make(chan int) missingBlocksPopulated := make(chan int)
go backFillAllBlocks(blockchain, blockRepository, missingBlocksPopulated, startingBlockNumber) go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
for { for {
select { select {
@ -94,7 +108,7 @@ func sync() {
window := validator.ValidateBlocks() window := validator.ValidateBlocks()
window.Log(os.Stdout) window.Log(os.Stdout)
case <-missingBlocksPopulated: case <-missingBlocksPopulated:
go backFillAllBlocks(blockchain, blockRepository, missingBlocksPopulated, startingBlockNumber) go backFillAllBlocks(blockChain, blockRepository, missingBlocksPopulated, startingBlockNumber)
} }
} }
} }

View File

@ -15,29 +15,51 @@
package every_block_test package every_block_test
import ( import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/examples/constants" "github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block" "github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/examples/mocks" "github.com/vulcanize/vulcanizedb/examples/mocks"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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"
"math/big" "math/big"
) )
var _ = Describe("ERC20 Fetcher", func() { var _ = Describe("ERC20 Fetcher", func() {
blockNumber := int64(5502914) blockNumber := int64(5502914)
infuraIPC := "https://mainnet.infura.io/J5Vd2fRtGsw0zZ0Ov3BL" infuraIPC := "https://mainnet.infura.io/J5Vd2fRtGsw0zZ0Ov3BL"
realBlockchain := geth.NewBlockChain(infuraIPC) var errorFetcher every_block.Fetcher
realFetcher := every_block.NewFetcher(realBlockchain) var realFetcher every_block.Fetcher
var testFetcher every_block.Fetcher
var fakeBlockchain *mocks.Blockchain
var testAbi string
var testContractAddress string
fakeBlockchain := &mocks.Blockchain{} BeforeEach(func() {
testFetcher := every_block.NewFetcher(fakeBlockchain) rpcClient, err := rpc.Dial(infuraIPC)
testAbi := "testAbi" Expect(err).NotTo(HaveOccurred())
testContractAddress := "testContractAddress" ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: infuraIPC,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
realFetcher = every_block.NewFetcher(realBlockChain)
fakeBlockchain = &mocks.Blockchain{}
testFetcher = every_block.NewFetcher(fakeBlockchain)
testAbi = "testAbi"
testContractAddress = "testContractAddress"
errorBlockchain := &mocks.FailureBlockchain{} errorBlockchain := &mocks.FailureBlockchain{}
errorFetcher := every_block.NewFetcher(errorBlockchain) errorFetcher = every_block.NewFetcher(errorBlockchain)
})
Describe("FetchSupplyOf", func() { Describe("FetchSupplyOf", func() {
It("fetches data from the blockchain with the correct arguments", func() { It("fetches data from the blockchain with the correct arguments", func() {

View File

@ -1,24 +1,49 @@
package integration package integration
import ( import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
var _ = Describe("Rewards calculations", func() { var _ = Describe("Rewards calculations", func() {
It("calculates a block reward for a real block", func() { It("calculates a block reward for a real block", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
block, err := blockchain.GetBlockByNumber(1071819) Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(block.Reward).To(Equal(5.31355)) Expect(block.Reward).To(Equal(5.31355))
}) })
It("calculates an uncle reward for a real block", func() { It("calculates an uncle reward for a real block", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
block, err := blockchain.GetBlockByNumber(1071819) Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(block.UnclesReward).To(Equal(6.875)) Expect(block.UnclesReward).To(Equal(6.875))
}) })

View File

@ -4,10 +4,15 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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/geth/testing" "github.com/vulcanize/vulcanizedb/pkg/geth/testing"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -27,42 +32,69 @@ var _ = Describe("Reading contracts", func() {
}, },
Index: 19, Index: 19,
Data: "0x0000000000000000000000000000000000000000000000000c7d713b49da0000"} Data: "0x0000000000000000000000000000000000000000000000000c7d713b49da0000"}
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
contract := testing.SampleContract() contract := testing.SampleContract()
logs, err := blockchain.GetLogs(contract, big.NewInt(4703824), nil) logs, err := blockChain.GetLogs(contract, big.NewInt(4703824), nil)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(len(logs)).To(Equal(3)) Expect(len(logs)).To(Equal(3))
Expect(logs[0]).To(Equal(expectedLogZero)) Expect(logs[0]).To(Equal(expectedLogZero))
}) })
It("returns and empty log array when no events for a given block / contract combo", func() { It("returns and empty log array when no events for a given block / contract combo", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
logs, err := blockchain.GetLogs(core.Contract{Hash: "x123"}, big.NewInt(4703824), nil) logs, err := blockChain.GetLogs(core.Contract{Hash: "x123"}, big.NewInt(4703824), nil)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(len(logs)).To(Equal(0)) Expect(len(logs)).To(Equal(0))
}) })
}) })
Describe("Fetching Contract data", func() { Describe("Fetching Contract data", func() {
It("returns the correct attribute for a real contract", func() { It("returns the correct attribute for a real contract", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
contract := testing.SampleContract() contract := testing.SampleContract()
var balance = new(big.Int) var balance = new(big.Int)
args := common.HexToHash("0xd26114cd6ee289accf82350c8d8487fedb8a0c07") args := common.HexToHash("0xd26114cd6ee289accf82350c8d8487fedb8a0c07")
err := blockchain.FetchContractData(contract.Abi, "0xd26114cd6ee289accf82350c8d8487fedb8a0c07", "balanceOf", args, &balance, 5167471) err = blockChain.FetchContractData(contract.Abi, "0xd26114cd6ee289accf82350c8d8487fedb8a0c07", "balanceOf", args, &balance, 5167471)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
expected := new(big.Int) expected := new(big.Int)
expected.SetString("10897295492887612977137", 10) expected.SetString("10897295492887612977137", 10)
Expect(balance).To(Equal(expected)) Expect(balance).To(Equal(expected))
}) })
}) })
}) })

View File

@ -1,40 +1,54 @@
package integration_test package integration_test
import ( import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory" "github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
"github.com/vulcanize/vulcanizedb/pkg/geth" "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/pkg/history"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
var _ = Describe("Reading from the Geth blockchain", func() { var _ = Describe("Reading from the Geth blockchain", func() {
var blockChain *geth.BlockChain
var blockchain *geth.BlockChain
var inMemory *inmemory.InMemory var inMemory *inmemory.InMemory
BeforeEach(func() { BeforeEach(func() {
blockchain = geth.NewBlockChain(test_config.InfuraClient.IPCPath) rpcClient, err := rpc.Dial(test_config.InfuraClient.IPCPath)
Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: test_config.InfuraClient.IPCPath,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain = geth.NewBlockChain(blockChainClient, node, transactionConverter)
inMemory = inmemory.NewInMemory() inMemory = inmemory.NewInMemory()
}) })
It("reads two blocks", func(done Done) { It("reads two blocks", func(done Done) {
blocks := &inmemory.BlockRepository{InMemory: inMemory} blocks := &inmemory.BlockRepository{InMemory: inMemory}
lastBlock := blockchain.LastBlock() lastBlock := blockChain.LastBlock()
queriedBlocks := []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6} queriedBlocks := []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6}
history.RetrieveAndUpdateBlocks(blockchain, blocks, queriedBlocks) history.RetrieveAndUpdateBlocks(blockChain, blocks, queriedBlocks)
Expect(blocks.BlockCount()).To(Equal(2)) Expect(blocks.BlockCount()).To(Equal(2))
close(done) close(done)
}, 30) }, 30)
It("retrieves the genesis block and first block", func(done Done) { It("retrieves the genesis block and first block", func(done Done) {
genesisBlock, err := blockchain.GetBlockByNumber(int64(0)) genesisBlock, err := blockChain.GetBlockByNumber(int64(0))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
firstBlock, err := blockchain.GetBlockByNumber(int64(1)) firstBlock, err := blockChain.GetBlockByNumber(int64(1))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
lastBlockNumber := blockchain.LastBlock() lastBlockNumber := blockChain.LastBlock()
Expect(genesisBlock.Number).To(Equal(int64(0))) Expect(genesisBlock.Number).To(Equal(int64(0)))
Expect(firstBlock.Number).To(Equal(int64(1))) Expect(firstBlock.Number).To(Equal(int64(1)))
@ -43,7 +57,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
}, 15) }, 15)
It("retrieves the node info", func(done Done) { It("retrieves the node info", func(done Done) {
node := blockchain.Node() node := blockChain.Node()
mainnetID := float64(1) mainnetID := float64(1)
Expect(node.GenesisBlock).ToNot(BeNil()) Expect(node.GenesisBlock).ToNot(BeNil())
@ -60,7 +74,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
var blocks []core.Block var blocks []core.Block
n := 10 n := 10
for i := 5327459; i > 5327459-n; i-- { for i := 5327459; i > 5327459-n; i-- {
block, err := blockchain.GetBlockByNumber(int64(i)) block, err := blockChain.GetBlockByNumber(int64(i))
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
blocks = append(blocks, block) blocks = append(blocks, block)
} }

16
pkg/core/client.go Normal file
View File

@ -0,0 +1,16 @@
package core
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
)
type Client interface {
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
}

5
pkg/fakes/data.go Normal file
View File

@ -0,0 +1,5 @@
package fakes
import "errors"
var FakeError = errors.New("failed")

View File

@ -6,7 +6,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
) )
type BlockChain struct { type MockBlockChain struct {
ContractReturnValue []byte ContractReturnValue []byte
WasToldToStop bool WasToldToStop bool
blocks map[int64]core.Block blocks map[int64]core.Block
@ -18,19 +18,19 @@ type BlockChain struct {
node core.Node node core.Node
} }
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) { func (blockChain *MockBlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) {
return blockChain.headers[blockNumber], nil return blockChain.headers[blockNumber], nil
} }
func (blockChain *BlockChain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error { func (blockChain *MockBlockChain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
panic("implement me") panic("implement me")
} }
func (blockChain *BlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) { func (blockChain *MockBlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
return blockChain.ContractReturnValue, nil return blockChain.ContractReturnValue, nil
} }
func (blockChain *BlockChain) LastBlock() *big.Int { func (blockChain *MockBlockChain) LastBlock() *big.Int {
var max int64 var max int64
for blockNumber := range blockChain.blocks { for blockNumber := range blockChain.blocks {
if blockNumber > max { if blockNumber > max {
@ -40,16 +40,16 @@ func (blockChain *BlockChain) LastBlock() *big.Int {
return big.NewInt(max) return big.NewInt(max)
} }
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlock *big.Int, endingBlock *big.Int) ([]core.Log, error) { func (blockChain *MockBlockChain) GetLogs(contract core.Contract, startingBlock *big.Int, endingBlock *big.Int) ([]core.Log, error) {
return blockChain.logs[contract.Hash], nil return blockChain.logs[contract.Hash], nil
} }
func (blockChain *BlockChain) Node() core.Node { func (blockChain *MockBlockChain) Node() core.Node {
return blockChain.node return blockChain.node
} }
func NewBlockchain(err error) *BlockChain { func NewMockBlockChain(err error) *MockBlockChain {
return &BlockChain{ return &MockBlockChain{
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),
@ -58,17 +58,17 @@ func NewBlockchain(err error) *BlockChain {
} }
} }
func NewBlockchainWithBlocks(blocks []core.Block) *BlockChain { func NewMockBlockChainWithBlocks(blocks []core.Block) *MockBlockChain {
blockNumberToBlocks := make(map[int64]core.Block) blockNumberToBlocks := make(map[int64]core.Block)
for _, block := range blocks { for _, block := range blocks {
blockNumberToBlocks[block.Number] = block blockNumberToBlocks[block.Number] = block
} }
return &BlockChain{ return &MockBlockChain{
blocks: blockNumberToBlocks, blocks: blockNumberToBlocks,
} }
} }
func NewBlockChainWithHeaders(headers []core.Header) *BlockChain { func NewMockBlockChainWithHeaders(headers []core.Header) *MockBlockChain {
// need to create blocks and headers so that LastBlock() will work in the mock // need to create blocks and headers so that LastBlock() will work in the mock
// no reason to implement LastBlock() separately for headers since it checks // no reason to implement LastBlock() separately for headers since it checks
// the last header in the Node's DB already // the last header in the Node's DB already
@ -78,20 +78,20 @@ func NewBlockChainWithHeaders(headers []core.Header) *BlockChain {
memoryBlocks[header.BlockNumber] = core.Block{Number: header.BlockNumber} memoryBlocks[header.BlockNumber] = core.Block{Number: header.BlockNumber}
memoryHeaders[header.BlockNumber] = header memoryHeaders[header.BlockNumber] = header
} }
return &BlockChain{ return &MockBlockChain{
blocks: memoryBlocks, blocks: memoryBlocks,
headers: memoryHeaders, headers: memoryHeaders,
} }
} }
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) { func (blockChain *MockBlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
if blockChain.err != nil { if blockChain.err != nil {
return core.Block{}, blockChain.err return core.Block{}, blockChain.err
} }
return blockChain.blocks[blockNumber], nil return blockChain.blocks[blockNumber], nil
} }
func (blockChain *BlockChain) AddBlock(block core.Block) { func (blockChain *MockBlockChain) AddBlock(block core.Block) {
blockChain.blocks[block.Number] = block blockChain.blocks[block.Number] = block
blockChain.blocksChannel <- block blockChain.blocksChannel <- block
} }

130
pkg/fakes/mock_client.go Normal file
View File

@ -0,0 +1,130 @@
package fakes
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/gomega"
)
type MockClient struct {
callContractErr error
callContractPassedContext context.Context
callContractPassedMsg ethereum.CallMsg
callContractPassedNumber *big.Int
callContractReturnBytes []byte
blockByNumberErr error
blockByNumberPassedContext context.Context
blockByNumberPassedNumber *big.Int
blockByNumberReturnBlock *types.Block
headerByNumberErr error
headerByNumberPassedContext context.Context
headerByNumberPassedNumber *big.Int
headerByNumberReturnHeader *types.Header
filterLogsErr error
filterLogsPassedContext context.Context
filterLogsPassedQuery ethereum.FilterQuery
filterLogsReturnLogs []types.Log
}
func NewMockClient() *MockClient {
return &MockClient{
callContractErr: nil,
callContractPassedContext: nil,
callContractPassedMsg: ethereum.CallMsg{},
callContractPassedNumber: nil,
callContractReturnBytes: nil,
blockByNumberErr: nil,
blockByNumberPassedContext: nil,
blockByNumberPassedNumber: nil,
blockByNumberReturnBlock: nil,
headerByNumberErr: nil,
headerByNumberPassedContext: nil,
headerByNumberPassedNumber: nil,
headerByNumberReturnHeader: nil,
filterLogsErr: nil,
filterLogsPassedContext: nil,
filterLogsPassedQuery: ethereum.FilterQuery{},
filterLogsReturnLogs: nil,
}
}
func (client *MockClient) SetCallContractErr(err error) {
client.callContractErr = err
}
func (client *MockClient) SetCallContractReturnBytes(returnBytes []byte) {
client.callContractReturnBytes = returnBytes
}
func (client *MockClient) SetBlockByNumberErr(err error) {
client.blockByNumberErr = err
}
func (client *MockClient) SetBlockByNumberReturnBlock(block *types.Block) {
client.blockByNumberReturnBlock = block
}
func (client *MockClient) SetHeaderByNumberErr(err error) {
client.headerByNumberErr = err
}
func (client *MockClient) SetHeaderByNumberReturnHeader(header *types.Header) {
client.headerByNumberReturnHeader = header
}
func (client *MockClient) SetFilterLogsErr(err error) {
client.filterLogsErr = err
}
func (client *MockClient) SetFilterLogsReturnLogs(logs []types.Log) {
client.filterLogsReturnLogs = logs
}
func (client *MockClient) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
client.callContractPassedContext = ctx
client.callContractPassedMsg = msg
client.callContractPassedNumber = blockNumber
return client.callContractReturnBytes, client.callContractErr
}
func (client *MockClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
client.blockByNumberPassedContext = ctx
client.blockByNumberPassedNumber = number
return client.blockByNumberReturnBlock, client.blockByNumberErr
}
func (client *MockClient) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
client.headerByNumberPassedContext = ctx
client.headerByNumberPassedNumber = number
return client.headerByNumberReturnHeader, client.headerByNumberErr
}
func (client *MockClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
client.filterLogsPassedContext = ctx
client.filterLogsPassedQuery = q
return client.filterLogsReturnLogs, client.filterLogsErr
}
func (client *MockClient) AssertCallContractCalledWith(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) {
Expect(client.callContractPassedContext).To(Equal(ctx))
Expect(client.callContractPassedMsg).To(Equal(msg))
Expect(client.callContractPassedNumber).To(Equal(blockNumber))
}
func (client *MockClient) AssertBlockByNumberCalledWith(ctx context.Context, number *big.Int) {
Expect(client.blockByNumberPassedContext).To(Equal(ctx))
Expect(client.blockByNumberPassedNumber).To(Equal(number))
}
func (client *MockClient) AssertHeaderByNumberCalledWith(ctx context.Context, number *big.Int) {
Expect(client.headerByNumberPassedContext).To(Equal(ctx))
Expect(client.headerByNumberPassedNumber).To(Equal(number))
}
func (client *MockClient) AssertFilterLogsCalledWith(ctx context.Context, q ethereum.FilterQuery) {
Expect(client.filterLogsPassedContext).To(Equal(ctx))
Expect(client.filterLogsPassedQuery).To(Equal(q))
}

View File

@ -1,44 +1,48 @@
package geth package geth
import ( import (
"log"
"math/big" "math/big"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common" vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common"
vulcRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
) )
type BlockChain struct { type BlockChain struct {
client *ethclient.Client client core.Client
blockConverter vulcCommon.BlockConverter blockConverter vulcCommon.BlockConverter
headerConverter vulcCommon.HeaderConverter headerConverter vulcCommon.HeaderConverter
node core.Node node core.Node
} }
func NewBlockChain(ipcPath string) *BlockChain { func NewBlockChain(client core.Client, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
rpcClient, err := rpc.Dial(ipcPath)
if err != nil {
log.Fatal(err)
}
client := ethclient.NewClient(rpcClient)
clientWrapper := node.ClientWrapper{ContextCaller: rpcClient, IPCPath: ipcPath}
transactionConverter := vulcRpc.NewRpcTransactionConverter(client)
return &BlockChain{ return &BlockChain{
client: client, client: client,
blockConverter: vulcCommon.NewBlockConverter(transactionConverter), blockConverter: vulcCommon.NewBlockConverter(converter),
headerConverter: vulcCommon.HeaderConverter{}, headerConverter: vulcCommon.HeaderConverter{},
node: node.MakeNode(clientWrapper), node: node,
} }
} }
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
gethBlock, err := blockChain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return block, err
}
return blockChain.blockConverter.ToCoreBlock(gethBlock)
}
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
gethHeader, err := blockChain.client.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return header, err
}
return blockChain.headerConverter.Convert(gethHeader)
}
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) { func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) {
if endingBlockNumber == nil { if endingBlockNumber == nil {
endingBlockNumber = startingBlockNumber endingBlockNumber = startingBlockNumber
@ -57,27 +61,11 @@ func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlockNumbe
return logs, nil return logs, nil
} }
func (blockChain *BlockChain) Node() core.Node {
return blockChain.node
}
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
gethBlock, err := blockChain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return block, err
}
return blockChain.blockConverter.ToCoreBlock(gethBlock)
}
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
gethHeader, err := blockChain.client.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
if err != nil {
return header, err
}
return blockChain.headerConverter.Convert(gethHeader)
}
func (blockChain *BlockChain) LastBlock() *big.Int { func (blockChain *BlockChain) LastBlock() *big.Int {
block, _ := blockChain.client.HeaderByNumber(context.Background(), nil) block, _ := blockChain.client.HeaderByNumber(context.Background(), nil)
return block.Number return block.Number
} }
func (blockChain *BlockChain) Node() core.Node {
return blockChain.node
}

125
pkg/geth/blockchain_test.go Normal file
View File

@ -0,0 +1,125 @@
package geth_test
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
vulcCore "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/converters/cold_db"
)
var _ = Describe("Geth blockchain", func() {
Describe("getting a block", func() {
It("fetches block from client", func() {
mockClient := fakes.NewMockClient()
mockClient.SetBlockByNumberReturnBlock(types.NewBlockWithHeader(&types.Header{}))
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
blockNumber := int64(100)
_, err := blockChain.GetBlockByNumber(blockNumber)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertBlockByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if client returns err", func() {
mockClient := fakes.NewMockClient()
mockClient.SetBlockByNumberErr(fakes.FakeError)
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetBlockByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("getting a header", func() {
It("fetches header from client", func() {
mockClient := fakes.NewMockClient()
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(blockNumber)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertHeaderByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if client returns err", func() {
mockClient := fakes.NewMockClient()
mockClient.SetHeaderByNumberErr(fakes.FakeError)
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("getting logs", func() {
It("fetches logs from client", func() {
mockClient := fakes.NewMockClient()
mockClient.SetFilterLogsReturnLogs([]types.Log{{}})
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
endingBlockNumber := big.NewInt(2)
_, err := blockChain.GetLogs(contract, startingBlockNumber, endingBlockNumber)
Expect(err).NotTo(HaveOccurred())
expectedQuery := ethereum.FilterQuery{
FromBlock: startingBlockNumber,
ToBlock: endingBlockNumber,
Addresses: []common.Address{common.HexToAddress(contract.Hash)},
}
mockClient.AssertFilterLogsCalledWith(context.Background(), expectedQuery)
})
It("returns err if client returns err", func() {
mockClient := fakes.NewMockClient()
mockClient.SetFilterLogsErr(fakes.FakeError)
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
endingBlockNumber := big.NewInt(2)
_, err := blockChain.GetLogs(contract, startingBlockNumber, endingBlockNumber)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("getting the most recent block number", func() {
It("fetches latest header from client", func() {
mockClient := fakes.NewMockClient()
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
node := vulcCore.Node{}
blockChain := geth.NewBlockChain(mockClient, node, cold_db.NewColdDbTransactionConverter())
result := blockChain.LastBlock()
mockClient.AssertHeaderByNumberCalledWith(context.Background(), nil)
Expect(result).To(Equal(big.NewInt(blockNumber)))
})
})
})

33
pkg/geth/client/client.go Normal file
View File

@ -0,0 +1,33 @@
package client
import (
"context"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
)
type Client struct {
client *ethclient.Client
}
func NewClient(client *ethclient.Client) Client {
return Client{client: client}
}
func (client Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return client.client.BlockByNumber(ctx, number)
}
func (client Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
return client.client.CallContract(ctx, msg, blockNumber)
}
func (client Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
return client.client.FilterLogs(ctx, q)
}
func (client Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return client.client.HeaderByNumber(ctx, number)
}

View File

@ -13,7 +13,7 @@ import (
var _ = Describe("Blocks validator", func() { var _ = Describe("Blocks validator", func() {
It("calls create or update for all blocks within the window", func() { It("calls create or update for all blocks within the window", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 4}, {Number: 4},
{Number: 5}, {Number: 5},
{Number: 6}, {Number: 6},
@ -31,7 +31,7 @@ var _ = Describe("Blocks validator", func() {
}) })
It("returns the number of largest block", func() { It("returns the number of largest block", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 1}, {Number: 1},
{Number: 2}, {Number: 2},
{Number: 3}, {Number: 3},

View File

@ -27,7 +27,7 @@ var _ = Describe("Header validator", func() {
Hash: newHash, Hash: newHash,
} }
headers := []core.Header{newHeader} headers := []core.Header{newHeader}
blockChain := fakes.NewBlockChainWithHeaders(headers) blockChain := fakes.NewMockBlockChainWithHeaders(headers)
validator := history.NewHeaderValidator(blockChain, headerRepository, 1) validator := history.NewHeaderValidator(blockChain, headerRepository, 1)
validator.ValidateHeaders() validator.ValidateHeaders()

View File

@ -25,7 +25,7 @@ var _ = Describe("Populating blocks", func() {
{Number: 1}, {Number: 1},
{Number: 2}, {Number: 2},
} }
blockchain := fakes.NewBlockchainWithBlocks(blocks) blockchain := fakes.NewMockBlockChainWithBlocks(blocks)
blockRepository.CreateOrUpdateBlock(core.Block{Number: 2}) blockRepository.CreateOrUpdateBlock(core.Block{Number: 2})
@ -37,7 +37,7 @@ var _ = Describe("Populating blocks", func() {
}) })
It("fills in the three missing blocks (Numbers: 5,8,10)", func() { It("fills in the three missing blocks (Numbers: 5,8,10)", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 4}, {Number: 4},
{Number: 5}, {Number: 5},
{Number: 6}, {Number: 6},
@ -76,7 +76,7 @@ var _ = Describe("Populating blocks", func() {
}) })
It("returns the number of blocks created", func() { It("returns the number of blocks created", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 4}, {Number: 4},
{Number: 5}, {Number: 5},
{Number: 6}, {Number: 6},
@ -90,7 +90,7 @@ var _ = Describe("Populating blocks", func() {
}) })
It("updates the repository with a range of blocks w/in the range ", func() { It("updates the repository with a range of blocks w/in the range ", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 1}, {Number: 1},
{Number: 2}, {Number: 2},
{Number: 3}, {Number: 3},
@ -104,7 +104,7 @@ var _ = Describe("Populating blocks", func() {
}) })
It("does not call repository create block when there is an error", func() { It("does not call repository create block when there is an error", func() {
blockchain := fakes.NewBlockchain(errors.New("error getting block")) blockchain := fakes.NewMockBlockChain(errors.New("error getting block"))
blocks := history.MakeRange(1, 10) blocks := history.MakeRange(1, 10)
history.RetrieveAndUpdateBlocks(blockchain, blockRepository, blocks) history.RetrieveAndUpdateBlocks(blockchain, blockRepository, blocks)
Expect(blockRepository.BlockCount()).To(Equal(0)) Expect(blockRepository.BlockCount()).To(Equal(0))

View File

@ -26,7 +26,7 @@ var _ = Describe("Populating headers", func() {
{BlockNumber: 1}, {BlockNumber: 1},
{BlockNumber: 2}, {BlockNumber: 2},
} }
blockChain := fakes.NewBlockChainWithHeaders(headers) blockChain := fakes.NewMockBlockChainWithHeaders(headers)
headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: 2}) headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: 2})
headersAdded := history.PopulateMissingHeaders(blockChain, headerRepository, 1) headersAdded := history.PopulateMissingHeaders(blockChain, headerRepository, 1)
@ -40,7 +40,7 @@ var _ = Describe("Populating headers", func() {
{BlockNumber: 1}, {BlockNumber: 1},
{BlockNumber: 2}, {BlockNumber: 2},
} }
blockChain := fakes.NewBlockChainWithHeaders(headers) blockChain := fakes.NewMockBlockChainWithHeaders(headers)
dbHeader, _ := headerRepository.GetHeader(1) dbHeader, _ := headerRepository.GetHeader(1)
Expect(dbHeader.BlockNumber).To(BeZero()) Expect(dbHeader.BlockNumber).To(BeZero())

View File

@ -13,7 +13,7 @@ import (
var _ = Describe("", func() { var _ = Describe("", func() {
It("creates a ValidationWindow equal to (HEAD-windowSize, HEAD)", func() { It("creates a ValidationWindow equal to (HEAD-windowSize, HEAD)", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ blockchain := fakes.NewMockBlockChainWithBlocks([]core.Block{
{Number: 1}, {Number: 1},
{Number: 2}, {Number: 2},
{Number: 3}, {Number: 3},