2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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() {
|
2018-07-20 16:37:46 +00:00
|
|
|
mockClient := fakes.NewMockEthClient()
|
2018-07-18 20:59:40 +00:00
|
|
|
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)))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|