2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
package eth_test
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-22 03:43:06 +00:00
|
|
|
"errors"
|
2018-07-18 20:59:40 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
2018-12-12 16:01:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-20 22:15:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-12-12 16:01:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-01-23 06:55:26 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
vulcCore "github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Geth blockchain", func() {
|
2019-03-27 04:05:30 +00:00
|
|
|
var (
|
|
|
|
mockClient *fakes.MockEthClient
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain *eth.BlockChain
|
2019-03-27 04:05:30 +00:00
|
|
|
mockRpcClient *fakes.MockRpcClient
|
|
|
|
mockTransactionConverter *fakes.MockTransactionConverter
|
|
|
|
node vulcCore.Node
|
|
|
|
)
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
mockClient = fakes.NewMockEthClient()
|
2018-09-18 19:46:16 +00:00
|
|
|
mockRpcClient = fakes.NewMockRpcClient()
|
2019-03-27 04:05:30 +00:00
|
|
|
mockTransactionConverter = fakes.NewMockTransactionConverter()
|
2018-09-18 19:46:16 +00:00
|
|
|
node = vulcCore.Node{}
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain = eth.NewBlockChain(mockClient, mockRpcClient, node, mockTransactionConverter)
|
2018-08-07 15:51:34 +00:00
|
|
|
})
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
Describe("getting a block", func() {
|
2018-09-18 19:46:16 +00:00
|
|
|
It("fetches block from ethClient", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
mockClient.SetBlockByNumberReturnBlock(types.NewBlockWithHeader(&types.Header{}))
|
|
|
|
blockNumber := int64(100)
|
|
|
|
|
|
|
|
_, err := blockChain.GetBlockByNumber(blockNumber)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockClient.AssertBlockByNumberCalledWith(context.Background(), big.NewInt(blockNumber))
|
|
|
|
})
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
It("returns err if ethClient returns err", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
mockClient.SetBlockByNumberErr(fakes.FakeError)
|
|
|
|
|
|
|
|
_, err := blockChain.GetBlockByNumber(100)
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("getting a header", func() {
|
2018-09-18 19:46:16 +00:00
|
|
|
Describe("default/mainnet", func() {
|
|
|
|
It("fetches header from ethClient", func() {
|
|
|
|
blockNumber := int64(100)
|
|
|
|
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
_, err := blockChain.GetHeaderByNumber(blockNumber)
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
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))
|
|
|
|
})
|
2018-12-12 16:01:50 +00:00
|
|
|
|
|
|
|
It("fetches headers with multiple blocks", func() {
|
2019-05-07 22:24:10 +00:00
|
|
|
_, err := blockChain.GetHeadersByNumbers([]int64{100, 99})
|
2018-12-12 16:01:50 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockRpcClient.AssertBatchCalledWith("eth_getBlockByNumber", 2)
|
|
|
|
})
|
2018-07-18 20:59:40 +00:00
|
|
|
})
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
Describe("POA/Kovan", func() {
|
|
|
|
It("fetches header from rpcClient", func() {
|
|
|
|
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
|
2018-12-12 16:01:50 +00:00
|
|
|
blockNumber := hexutil.Big(*big.NewInt(100))
|
2018-09-20 22:15:06 +00:00
|
|
|
mockRpcClient.SetReturnPOAHeader(vulcCore.POAHeader{Number: &blockNumber})
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain = eth.NewBlockChain(mockClient, mockRpcClient, node, fakes.NewMockTransactionConverter())
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
_, err := blockChain.GetHeaderByNumber(100)
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
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)
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain = eth.NewBlockChain(mockClient, mockRpcClient, node, fakes.NewMockTransactionConverter())
|
2018-09-18 19:46:16 +00:00
|
|
|
|
|
|
|
_, err := blockChain.GetHeaderByNumber(100)
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
2018-09-20 22:15:06 +00:00
|
|
|
|
|
|
|
It("returns error if returned header is empty", func() {
|
|
|
|
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain = eth.NewBlockChain(mockClient, mockRpcClient, node, fakes.NewMockTransactionConverter())
|
2018-09-20 22:15:06 +00:00
|
|
|
|
|
|
|
_, err := blockChain.GetHeaderByNumber(100)
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
2019-10-28 11:30:24 +00:00
|
|
|
Expect(err).To(MatchError(eth.ErrEmptyHeader))
|
2018-09-20 22:15:06 +00:00
|
|
|
})
|
2018-12-12 16:01:50 +00:00
|
|
|
|
|
|
|
It("returns multiple headers with multiple blocknumbers", func() {
|
|
|
|
node.NetworkID = vulcCore.KOVAN_NETWORK_ID
|
|
|
|
blockNumber := hexutil.Big(*big.NewInt(100))
|
|
|
|
mockRpcClient.SetReturnPOAHeaders([]vulcCore.POAHeader{{Number: &blockNumber}})
|
|
|
|
|
2019-05-07 22:24:10 +00:00
|
|
|
_, err := blockChain.GetHeadersByNumbers([]int64{100, 99})
|
2018-12-12 16:01:50 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockRpcClient.AssertBatchCalledWith("eth_getBlockByNumber", 2)
|
|
|
|
})
|
2018-07-18 20:59:40 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-07 15:51:34 +00:00
|
|
|
Describe("getting logs with default FilterQuery", func() {
|
2018-09-18 19:46:16 +00:00
|
|
|
It("fetches logs from ethClient", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
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)
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
_, err := blockChain.GetFullSyncLogs(contract, startingBlockNumber, endingBlockNumber)
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
expectedQuery := ethereum.FilterQuery{
|
|
|
|
FromBlock: startingBlockNumber,
|
|
|
|
ToBlock: endingBlockNumber,
|
|
|
|
Addresses: []common.Address{common.HexToAddress(contract.Hash)},
|
|
|
|
}
|
|
|
|
mockClient.AssertFilterLogsCalledWith(context.Background(), expectedQuery)
|
|
|
|
})
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
It("returns err if ethClient returns err", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
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)
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
_, err := blockChain.GetFullSyncLogs(contract, startingBlockNumber, endingBlockNumber)
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("getting logs with a custom FilterQuery", func() {
|
2018-09-18 19:46:16 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
It("returns err if ethClient returns err", func() {
|
2018-08-07 15:51:34 +00:00
|
|
|
mockClient.SetFilterLogsErr(fakes.FakeError)
|
2018-07-18 20:59:40 +00:00
|
|
|
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-18 20:59:40 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
_, err := blockChain.GetEthLogsWithCustomQuery(query)
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
Describe("getting transactions", func() {
|
|
|
|
It("fetches transaction for each hash", func() {
|
|
|
|
_, err := blockChain.GetTransactions([]common.Hash{{}, {}})
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockRpcClient.AssertBatchCalledWith("eth_getTransactionByHash", 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("converts transaction indexes from hex to int", func() {
|
|
|
|
_, err := blockChain.GetTransactions([]common.Hash{{}, {}})
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockTransactionConverter.ConvertHeaderTransactionIndexToIntCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
Describe("getting the most recent block number", func() {
|
2018-09-18 19:46:16 +00:00
|
|
|
It("fetches latest header from ethClient", func() {
|
2018-07-18 20:59:40 +00:00
|
|
|
blockNumber := int64(100)
|
|
|
|
mockClient.SetHeaderByNumberReturnHeader(&types.Header{Number: big.NewInt(blockNumber)})
|
|
|
|
|
2019-02-20 11:01:19 +00:00
|
|
|
result, err := blockChain.LastBlock()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-07-18 20:59:40 +00:00
|
|
|
|
|
|
|
mockClient.AssertHeaderByNumberCalledWith(context.Background(), nil)
|
|
|
|
Expect(result).To(Equal(big.NewInt(blockNumber)))
|
|
|
|
})
|
|
|
|
})
|
2019-03-22 03:43:06 +00:00
|
|
|
|
|
|
|
Describe("getting an account balance", func() {
|
|
|
|
It("fetches the balance for a given account address at a given block height", func() {
|
|
|
|
balance := big.NewInt(100000)
|
|
|
|
mockClient.SetBalanceAt(balance)
|
|
|
|
|
|
|
|
result, err := blockChain.GetAccountBalance(common.HexToAddress("0x40"), big.NewInt(100))
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
mockClient.AssertBalanceAtCalled(context.Background(), common.HexToAddress("0x40"), big.NewInt(100))
|
|
|
|
Expect(result).To(Equal(balance))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("fails if the client returns an error", func() {
|
|
|
|
balance := big.NewInt(100000)
|
|
|
|
mockClient.SetBalanceAt(balance)
|
|
|
|
setErr := errors.New("testError")
|
|
|
|
mockClient.SetBalanceAtErr(setErr)
|
|
|
|
|
|
|
|
_, err := blockChain.GetAccountBalance(common.HexToAddress("0x40"), big.NewInt(100))
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(Equal(setErr))
|
|
|
|
})
|
|
|
|
})
|
2018-07-18 20:59:40 +00:00
|
|
|
})
|