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
+29 -4
View File
@@ -1,24 +1,49 @@
package integration
import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"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"
)
var _ = Describe("Rewards calculations", func() {
It("calculates a block reward for a real block", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath)
block, err := blockchain.GetBlockByNumber(1071819)
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)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred())
Expect(block.Reward).To(Equal(5.31355))
})
It("calculates an uncle reward for a real block", func() {
blockchain := geth.NewBlockChain(test_config.InfuraClient.IPCPath)
block, err := blockchain.GetBlockByNumber(1071819)
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)
block, err := blockChain.GetBlockByNumber(1071819)
Expect(err).ToNot(HaveOccurred())
Expect(block.UnclesReward).To(Equal(6.875))
})
+41 -9
View File
@@ -4,10 +4,15 @@ import (
"math/big"
"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/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"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/test_config"
)
@@ -27,42 +32,69 @@ var _ = Describe("Reading contracts", func() {
},
Index: 19,
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()
logs, err := blockchain.GetLogs(contract, big.NewInt(4703824), nil)
logs, err := blockChain.GetLogs(contract, big.NewInt(4703824), nil)
Expect(err).To(BeNil())
Expect(len(logs)).To(Equal(3))
Expect(logs[0]).To(Equal(expectedLogZero))
})
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(len(logs)).To(Equal(0))
})
})
Describe("Fetching Contract data", 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()
var balance = new(big.Int)
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())
expected := new(big.Int)
expected.SetString("10897295492887612977137", 10)
Expect(balance).To(Equal(expected))
})
})
})
+24 -10
View File
@@ -1,40 +1,54 @@
package integration_test
import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
"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/test_config"
)
var _ = Describe("Reading from the Geth blockchain", func() {
var blockchain *geth.BlockChain
var blockChain *geth.BlockChain
var inMemory *inmemory.InMemory
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()
})
It("reads two blocks", func(done Done) {
blocks := &inmemory.BlockRepository{InMemory: inMemory}
lastBlock := blockchain.LastBlock()
lastBlock := blockChain.LastBlock()
queriedBlocks := []int64{lastBlock.Int64() - 5, lastBlock.Int64() - 6}
history.RetrieveAndUpdateBlocks(blockchain, blocks, queriedBlocks)
history.RetrieveAndUpdateBlocks(blockChain, blocks, queriedBlocks)
Expect(blocks.BlockCount()).To(Equal(2))
close(done)
}, 30)
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())
firstBlock, err := blockchain.GetBlockByNumber(int64(1))
firstBlock, err := blockChain.GetBlockByNumber(int64(1))
Expect(err).ToNot(HaveOccurred())
lastBlockNumber := blockchain.LastBlock()
lastBlockNumber := blockChain.LastBlock()
Expect(genesisBlock.Number).To(Equal(int64(0)))
Expect(firstBlock.Number).To(Equal(int64(1)))
@@ -43,7 +57,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
}, 15)
It("retrieves the node info", func(done Done) {
node := blockchain.Node()
node := blockChain.Node()
mainnetID := float64(1)
Expect(node.GenesisBlock).ToNot(BeNil())
@@ -60,7 +74,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
var blocks []core.Block
n := 10
for i := 5327459; i > 5327459-n; i-- {
block, err := blockchain.GetBlockByNumber(int64(i))
block, err := blockChain.GetBlockByNumber(int64(i))
Expect(err).ToNot(HaveOccurred())
blocks = append(blocks, block)
}