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
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2017-10-23 18:58:33 +00:00
|
|
|
import (
|
2018-09-12 14:10:34 +00:00
|
|
|
"errors"
|
2019-03-27 04:05:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2018-07-17 21:23:07 +00:00
|
|
|
"math/big"
|
2019-01-23 06:37:26 +00:00
|
|
|
"strconv"
|
2018-01-15 21:27:45 +00:00
|
|
|
|
2017-12-11 21:08:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-18 19:46:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-08-07 15:51:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-05-02 16:17:02 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
|
|
|
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/eth/converters/common"
|
2017-10-23 18:58:33 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-09-20 22:15:06 +00:00
|
|
|
var ErrEmptyHeader = errors.New("empty header returned over RPC")
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
const MAX_BATCH_SIZE = 100
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
type BlockChain struct {
|
2019-03-27 04:05:30 +00:00
|
|
|
blockConverter vulcCommon.BlockConverter
|
|
|
|
ethClient core.EthClient
|
|
|
|
headerConverter vulcCommon.HeaderConverter
|
|
|
|
node core.Node
|
2019-10-18 16:16:19 +00:00
|
|
|
rpcClient core.RPCClient
|
2019-03-27 04:05:30 +00:00
|
|
|
transactionConverter vulcCommon.TransactionConverter
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func NewBlockChain(ethClient core.EthClient, rpcClient core.RPCClient, node core.Node, converter vulcCommon.TransactionConverter) *BlockChain {
|
2018-07-17 21:23:07 +00:00
|
|
|
return &BlockChain{
|
2019-03-27 04:05:30 +00:00
|
|
|
blockConverter: vulcCommon.NewBlockConverter(converter),
|
|
|
|
ethClient: ethClient,
|
|
|
|
headerConverter: vulcCommon.HeaderConverter{},
|
|
|
|
node: node,
|
|
|
|
rpcClient: rpcClient,
|
|
|
|
transactionConverter: converter,
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (block core.Block, err error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
gethBlock, err := blockChain.ethClient.BlockByNumber(context.Background(), big.NewInt(blockNumber))
|
2018-07-18 20:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return block, err
|
|
|
|
}
|
|
|
|
return blockChain.blockConverter.ToCoreBlock(gethBlock)
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (blockChain *BlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
|
|
|
|
gethLogs, err := blockChain.ethClient.FilterLogs(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
return []types.Log{}, err
|
|
|
|
}
|
|
|
|
return gethLogs, nil
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (header core.Header, err error) {
|
2018-09-18 19:46:16 +00:00
|
|
|
if blockChain.node.NetworkID == core.KOVAN_NETWORK_ID {
|
|
|
|
return blockChain.getPOAHeader(blockNumber)
|
|
|
|
}
|
|
|
|
return blockChain.getPOWHeader(blockNumber)
|
|
|
|
}
|
|
|
|
|
2019-05-07 22:24:10 +00:00
|
|
|
func (blockChain *BlockChain) GetHeadersByNumbers(blockNumbers []int64) (header []core.Header, err error) {
|
2018-12-12 16:01:50 +00:00
|
|
|
if blockChain.node.NetworkID == core.KOVAN_NETWORK_ID {
|
|
|
|
return blockChain.getPOAHeaders(blockNumbers)
|
|
|
|
}
|
|
|
|
return blockChain.getPOWHeaders(blockNumbers)
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
func (blockChain *BlockChain) GetFullSyncLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.FullSyncLog, error) {
|
2019-03-27 04:05:30 +00:00
|
|
|
if endingBlockNumber == nil {
|
|
|
|
endingBlockNumber = startingBlockNumber
|
|
|
|
}
|
|
|
|
contractAddress := common.HexToAddress(contract.Hash)
|
|
|
|
fc := ethereum.FilterQuery{
|
|
|
|
FromBlock: startingBlockNumber,
|
|
|
|
ToBlock: endingBlockNumber,
|
|
|
|
Addresses: []common.Address{contractAddress},
|
|
|
|
Topics: nil,
|
|
|
|
}
|
|
|
|
gethLogs, err := blockChain.GetEthLogsWithCustomQuery(fc)
|
2018-07-18 20:59:40 +00:00
|
|
|
if err != nil {
|
2019-08-06 21:57:08 +00:00
|
|
|
return []core.FullSyncLog{}, err
|
2018-07-18 20:59:40 +00:00
|
|
|
}
|
2019-08-06 21:57:08 +00:00
|
|
|
logs := vulcCommon.ToFullSyncLogs(gethLogs)
|
2019-03-27 04:05:30 +00:00
|
|
|
return logs, nil
|
2018-07-18 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (blockChain *BlockChain) GetTransactions(transactionHashes []common.Hash) ([]core.TransactionModel, error) {
|
|
|
|
numTransactions := len(transactionHashes)
|
2019-01-21 20:52:37 +00:00
|
|
|
var batch []client.BatchElem
|
2019-10-18 16:16:19 +00:00
|
|
|
transactions := make([]core.RPCTransaction, numTransactions)
|
2018-12-12 16:01:50 +00:00
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
for index, transactionHash := range transactionHashes {
|
2019-01-21 20:52:37 +00:00
|
|
|
batchElem := client.BatchElem{
|
2019-03-27 04:05:30 +00:00
|
|
|
Method: "eth_getTransactionByHash",
|
|
|
|
Result: &transactions[index],
|
|
|
|
Args: []interface{}{transactionHash},
|
2018-12-12 16:01:50 +00:00
|
|
|
}
|
|
|
|
batch = append(batch, batchElem)
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
rpcErr := blockChain.rpcClient.BatchCall(batch)
|
|
|
|
if rpcErr != nil {
|
|
|
|
return []core.TransactionModel{}, rpcErr
|
2018-12-12 16:01:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
return blockChain.transactionConverter.ConvertRPCTransactionsToModels(transactions)
|
2019-03-27 04:05:30 +00:00
|
|
|
}
|
2018-12-12 16:01:50 +00:00
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (blockChain *BlockChain) LastBlock() (*big.Int, error) {
|
|
|
|
block, err := blockChain.ethClient.HeaderByNumber(context.Background(), nil)
|
2019-07-18 07:21:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return big.NewInt(0), err
|
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
return block.Number, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *BlockChain) Node() core.Node {
|
|
|
|
return blockChain.node
|
2018-12-12 16:01:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:46:16 +00:00
|
|
|
func (blockChain *BlockChain) getPOAHeader(blockNumber int64) (header core.Header, err error) {
|
|
|
|
var POAHeader core.POAHeader
|
|
|
|
blockNumberArg := hexutil.EncodeBig(big.NewInt(blockNumber))
|
|
|
|
includeTransactions := false
|
|
|
|
err = blockChain.rpcClient.CallContext(context.Background(), &POAHeader, "eth_getBlockByNumber", blockNumberArg, includeTransactions)
|
|
|
|
if err != nil {
|
|
|
|
return header, err
|
|
|
|
}
|
2018-09-20 22:15:06 +00:00
|
|
|
if POAHeader.Number == nil {
|
|
|
|
return header, ErrEmptyHeader
|
|
|
|
}
|
2018-09-18 19:46:16 +00:00
|
|
|
return blockChain.headerConverter.Convert(&types.Header{
|
|
|
|
ParentHash: POAHeader.ParentHash,
|
|
|
|
UncleHash: POAHeader.UncleHash,
|
|
|
|
Coinbase: POAHeader.Coinbase,
|
|
|
|
Root: POAHeader.Root,
|
|
|
|
TxHash: POAHeader.TxHash,
|
|
|
|
ReceiptHash: POAHeader.ReceiptHash,
|
|
|
|
Bloom: POAHeader.Bloom,
|
|
|
|
Difficulty: POAHeader.Difficulty.ToInt(),
|
|
|
|
Number: POAHeader.Number.ToInt(),
|
|
|
|
GasLimit: uint64(POAHeader.GasLimit),
|
|
|
|
GasUsed: uint64(POAHeader.GasUsed),
|
2019-07-15 22:08:40 +00:00
|
|
|
Time: uint64(POAHeader.Time),
|
2018-09-18 19:46:16 +00:00
|
|
|
Extra: POAHeader.Extra,
|
2019-02-14 15:03:57 +00:00
|
|
|
}, POAHeader.Hash.String()), nil
|
2018-09-18 19:46:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
func (blockChain *BlockChain) getPOAHeaders(blockNumbers []int64) (headers []core.Header, err error) {
|
|
|
|
|
2019-01-21 20:52:37 +00:00
|
|
|
var batch []client.BatchElem
|
2018-12-12 16:01:50 +00:00
|
|
|
var POAHeaders [MAX_BATCH_SIZE]core.POAHeader
|
|
|
|
includeTransactions := false
|
|
|
|
|
|
|
|
for index, blockNumber := range blockNumbers {
|
|
|
|
|
|
|
|
if index >= MAX_BATCH_SIZE {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
blockNumberArg := hexutil.EncodeBig(big.NewInt(blockNumber))
|
|
|
|
|
2019-01-21 20:52:37 +00:00
|
|
|
batchElem := client.BatchElem{
|
2018-12-12 16:01:50 +00:00
|
|
|
Method: "eth_getBlockByNumber",
|
|
|
|
Result: &POAHeaders[index],
|
|
|
|
Args: []interface{}{blockNumberArg, includeTransactions},
|
|
|
|
}
|
|
|
|
|
|
|
|
batch = append(batch, batchElem)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = blockChain.rpcClient.BatchCall(batch)
|
|
|
|
if err != nil {
|
|
|
|
return headers, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 22:34:20 +00:00
|
|
|
for _, POAHeader := range POAHeaders {
|
2018-12-12 16:01:50 +00:00
|
|
|
var header core.Header
|
|
|
|
//Header.Number of the newest block will return nil.
|
|
|
|
if _, err := strconv.ParseUint(POAHeader.Number.ToInt().String(), 16, 64); err == nil {
|
2019-02-14 15:03:57 +00:00
|
|
|
header = blockChain.headerConverter.Convert(&types.Header{
|
2018-12-12 16:01:50 +00:00
|
|
|
ParentHash: POAHeader.ParentHash,
|
|
|
|
UncleHash: POAHeader.UncleHash,
|
|
|
|
Coinbase: POAHeader.Coinbase,
|
|
|
|
Root: POAHeader.Root,
|
|
|
|
TxHash: POAHeader.TxHash,
|
|
|
|
ReceiptHash: POAHeader.ReceiptHash,
|
|
|
|
Bloom: POAHeader.Bloom,
|
|
|
|
Difficulty: POAHeader.Difficulty.ToInt(),
|
|
|
|
Number: POAHeader.Number.ToInt(),
|
|
|
|
GasLimit: uint64(POAHeader.GasLimit),
|
|
|
|
GasUsed: uint64(POAHeader.GasUsed),
|
2019-07-15 22:08:40 +00:00
|
|
|
Time: uint64(POAHeader.Time),
|
2018-12-12 16:01:50 +00:00
|
|
|
Extra: POAHeader.Extra,
|
|
|
|
}, POAHeader.Hash.String())
|
|
|
|
|
|
|
|
headers = append(headers, header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers, err
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (blockChain *BlockChain) getPOWHeader(blockNumber int64) (header core.Header, err error) {
|
|
|
|
gethHeader, err := blockChain.ethClient.HeaderByNumber(context.Background(), big.NewInt(blockNumber))
|
2017-12-11 21:08:00 +00:00
|
|
|
if err != nil {
|
2019-03-27 04:05:30 +00:00
|
|
|
return header, err
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
return blockChain.headerConverter.Convert(gethHeader, gethHeader.Hash().String()), nil
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (blockChain *BlockChain) getPOWHeaders(blockNumbers []int64) (headers []core.Header, err error) {
|
|
|
|
var batch []client.BatchElem
|
|
|
|
var POWHeaders [MAX_BATCH_SIZE]types.Header
|
|
|
|
includeTransactions := false
|
|
|
|
|
|
|
|
for index, blockNumber := range blockNumbers {
|
|
|
|
|
|
|
|
if index >= MAX_BATCH_SIZE {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
blockNumberArg := hexutil.EncodeBig(big.NewInt(blockNumber))
|
|
|
|
|
|
|
|
batchElem := client.BatchElem{
|
|
|
|
Method: "eth_getBlockByNumber",
|
|
|
|
Result: &POWHeaders[index],
|
|
|
|
Args: []interface{}{blockNumberArg, includeTransactions},
|
|
|
|
}
|
|
|
|
|
|
|
|
batch = append(batch, batchElem)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = blockChain.rpcClient.BatchCall(batch)
|
2018-08-07 15:51:34 +00:00
|
|
|
if err != nil {
|
2019-03-27 04:05:30 +00:00
|
|
|
return headers, err
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
for _, POWHeader := range POWHeaders {
|
|
|
|
if POWHeader.Number != nil {
|
|
|
|
header := blockChain.headerConverter.Convert(&POWHeader, POWHeader.Hash().String())
|
|
|
|
headers = append(headers, header)
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 20:59:40 +00:00
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
return headers, err
|
2018-07-18 20:59:40 +00:00
|
|
|
}
|
2019-03-22 03:43:06 +00:00
|
|
|
|
|
|
|
func (blockChain *BlockChain) GetAccountBalance(address common.Address, blockNumber *big.Int) (*big.Int, error) {
|
|
|
|
return blockChain.ethClient.BalanceAt(context.Background(), address, blockNumber)
|
|
|
|
}
|