2017-11-02 19:37:07 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
2017-12-20 20:06:22 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-01-03 17:23:43 +00:00
|
|
|
"log"
|
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-11-02 21:43:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-02-02 21:53:16 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2017-11-08 20:55:35 +00:00
|
|
|
"golang.org/x/net/context"
|
2018-03-27 21:06:12 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-11-02 19:37:07 +00:00
|
|
|
)
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
type Client interface {
|
2017-11-08 20:55:35 +00:00
|
|
|
TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)
|
2017-12-27 23:51:17 +00:00
|
|
|
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
|
2017-11-02 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
func ToCoreBlock(gethBlock *types.Block, client Client) (core.Block, error) {
|
|
|
|
transactions, err := convertTransactionsToCore(gethBlock, client)
|
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
coreBlock := core.Block{
|
2017-11-02 14:36:53 +00:00
|
|
|
Difficulty: gethBlock.Difficulty().Int64(),
|
2017-12-28 16:06:13 +00:00
|
|
|
ExtraData: hexutil.Encode(gethBlock.Extra()),
|
2018-03-07 21:29:21 +00:00
|
|
|
GasLimit: gethBlock.GasLimit(),
|
|
|
|
GasUsed: gethBlock.GasUsed(),
|
2017-11-02 14:36:53 +00:00
|
|
|
Hash: gethBlock.Hash().Hex(),
|
2018-01-08 21:59:47 +00:00
|
|
|
Miner: strings.ToLower(gethBlock.Coinbase().Hex()),
|
2017-11-02 21:43:07 +00:00
|
|
|
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
|
2017-11-02 14:36:53 +00:00
|
|
|
Number: gethBlock.Number().Int64(),
|
|
|
|
ParentHash: gethBlock.ParentHash().Hex(),
|
2018-03-07 21:29:21 +00:00
|
|
|
Size: gethBlock.Size().String(),
|
2017-11-02 19:37:07 +00:00
|
|
|
Time: gethBlock.Time().Int64(),
|
|
|
|
Transactions: transactions,
|
2017-11-02 14:36:53 +00:00
|
|
|
UncleHash: gethBlock.UncleHash().Hex(),
|
2017-11-02 19:37:07 +00:00
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles())
|
|
|
|
coreBlock.UnclesReward = CalcUnclesReward(coreBlock, gethBlock.Uncles())
|
2018-03-27 21:06:12 +00:00
|
|
|
return coreBlock, nil
|
2017-11-02 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
func convertTransactionsToCore(gethBlock *types.Block, client Client) ([]core.Transaction, error) {
|
|
|
|
var g errgroup.Group
|
|
|
|
coreTransactions := make([]core.Transaction, len(gethBlock.Transactions()))
|
|
|
|
|
|
|
|
for gethTransactionIndex, gethTransaction := range gethBlock.Transactions() {
|
|
|
|
//https://golang.org/doc/faq#closures_and_goroutines
|
|
|
|
transaction := gethTransaction
|
|
|
|
transactionIndex := uint(gethTransactionIndex)
|
|
|
|
g.Go(func() error {
|
|
|
|
from, err := client.TransactionSender(context.Background(), transaction, gethBlock.Hash(), transactionIndex)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("transaction sender: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
coreTransaction := transToCoreTrans(transaction, &from)
|
|
|
|
coreTransaction, err = appendReceiptToTransaction(client, coreTransaction)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("receipt: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
coreTransactions[transactionIndex] = coreTransaction
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
log.Println("transactions: ", err)
|
|
|
|
return coreTransactions, err
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
2018-03-27 21:06:12 +00:00
|
|
|
return coreTransactions, nil
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
func appendReceiptToTransaction(client Client, transaction core.Transaction) (core.Transaction, error) {
|
2018-01-03 17:23:43 +00:00
|
|
|
gethReceipt, err := client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
|
2018-01-08 20:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return transaction, err
|
|
|
|
}
|
2018-01-05 17:55:00 +00:00
|
|
|
receipt := ReceiptToCoreReceipt(gethReceipt)
|
2018-01-03 17:23:43 +00:00
|
|
|
transaction.Receipt = receipt
|
2018-03-27 21:06:12 +00:00
|
|
|
return transaction, nil
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func transToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction {
|
2017-12-28 23:04:15 +00:00
|
|
|
data := hexutil.Encode(transaction.Data())
|
2017-11-08 20:55:35 +00:00
|
|
|
return core.Transaction{
|
|
|
|
Hash: transaction.Hash().Hex(),
|
|
|
|
Nonce: transaction.Nonce(),
|
2017-12-20 20:06:22 +00:00
|
|
|
To: strings.ToLower(addressToHex(transaction.To())),
|
|
|
|
From: strings.ToLower(addressToHex(from)),
|
2018-03-07 21:29:21 +00:00
|
|
|
GasLimit: transaction.Gas(),
|
2017-11-08 20:55:35 +00:00
|
|
|
GasPrice: transaction.GasPrice().Int64(),
|
2018-01-16 20:25:33 +00:00
|
|
|
Value: transaction.Value().String(),
|
2017-12-28 23:04:15 +00:00
|
|
|
Data: data,
|
2017-11-08 20:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addressToHex(to *common.Address) string {
|
2017-11-02 19:37:07 +00:00
|
|
|
if to == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
return to.Hex()
|
2017-11-02 19:37:07 +00:00
|
|
|
}
|