ipld-eth-server/pkg/geth/blockchain_test.go

194 lines
6.3 KiB
Go
Raw Normal View History

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"
"github.com/ethereum/go-ethereum/common/hexutil"
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() {
2018-08-07 15:51:34 +00:00
var mockClient *fakes.MockEthClient
var mockRpcClient *fakes.MockRpcClient
var node vulcCore.Node
2018-08-07 15:51:34 +00:00
var blockChain *geth.BlockChain
BeforeEach(func() {
mockClient = fakes.NewMockEthClient()
mockRpcClient = fakes.NewMockRpcClient()
node = vulcCore.Node{}
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
2018-08-07 15:51:34 +00:00
})
Describe("getting a block", func() {
It("fetches block from ethClient", func() {
mockClient.SetBlockByNumberReturnBlock(types.NewBlockWithHeader(&types.Header{}))
blockNumber := int64(100)
_, err := blockChain.GetBlockByNumber(blockNumber)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertBlockByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if ethClient returns err", func() {
mockClient.SetBlockByNumberErr(fakes.FakeError)
_, err := blockChain.GetBlockByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("getting a header", func() {
Describe("default/mainnet", func() {
It("fetches header from ethClient", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
_, err := blockChain.GetHeaderByNumber(blockNumber)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertHeaderByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
})
It("returns err if ethClient returns err", func() {
mockClient.SetHeaderByNumberErr(fakes.FakeError)
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("POA/Kovan", func() {
It("fetches header from rpcClient", func() {
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
blockNumber := hexutil.Big(*big.NewInt(123))
mockRpcClient.SetReturnPOAHeader(vulcCore.POAHeader{Number: &blockNumber})
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).NotTo(HaveOccurred())
mockRpcClient.AssertCallContextCalledWith(context.Background(), &vulcCore.POAHeader{}, "eth_getBlockByNumber")
})
It("returns err if rpcClient returns err", func() {
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
mockRpcClient.SetCallContextErr(fakes.FakeError)
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
It("returns error if returned header is empty", func() {
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
blockChain = geth.NewBlockChain(mockClient, mockRpcClient, node, cold_db.NewColdDbTransactionConverter())
_, err := blockChain.GetHeaderByNumber(100)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(geth.ErrEmptyHeader))
})
})
})
2018-08-07 15:51:34 +00:00
Describe("getting logs with default FilterQuery", func() {
It("fetches logs from ethClient", func() {
mockClient.SetFilterLogsReturnLogs([]types.Log{{}})
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 ethClient returns err", func() {
mockClient.SetFilterLogsErr(fakes.FakeError)
2018-08-07 15:51:34 +00:00
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 logs with a custom FilterQuery", func() {
It("fetches logs from ethClient", func() {
2018-08-07 15:51:34 +00:00
mockClient.SetFilterLogsReturnLogs([]types.Log{{}})
address := common.HexToAddress("0x")
startingBlockNumber := big.NewInt(1)
endingBlockNumber := big.NewInt(2)
topic := common.HexToHash("0x")
query := ethereum.FilterQuery{
FromBlock: startingBlockNumber,
ToBlock: endingBlockNumber,
Addresses: []common.Address{address},
Topics: [][]common.Hash{{topic}},
}
2018-07-24 16:38:49 +00:00
2018-08-07 15:51:34 +00:00
_, err := blockChain.GetEthLogsWithCustomQuery(query)
Expect(err).NotTo(HaveOccurred())
mockClient.AssertFilterLogsCalledWith(context.Background(), query)
})
It("returns err if ethClient returns err", func() {
2018-08-07 15:51:34 +00:00
mockClient.SetFilterLogsErr(fakes.FakeError)
contract := vulcCore.Contract{Hash: common.BytesToHash([]byte{1, 2, 3, 4, 5}).Hex()}
startingBlockNumber := big.NewInt(1)
endingBlockNumber := big.NewInt(2)
2018-07-24 16:38:49 +00:00
query := ethereum.FilterQuery{
FromBlock: startingBlockNumber,
ToBlock: endingBlockNumber,
Addresses: []common.Address{common.HexToAddress(contract.Hash)},
Topics: nil,
}
2018-07-24 16:38:49 +00:00
_, err := blockChain.GetEthLogsWithCustomQuery(query)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
})
Describe("getting the most recent block number", func() {
It("fetches latest header from ethClient", func() {
blockNumber := int64(100)
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
result := blockChain.LastBlock()
mockClient.AssertHeaderByNumberCalledWith(context.Background(), nil)
Expect(result).To(Equal(big.NewInt(blockNumber)))
})
})
})