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

91 lines
2.9 KiB
Go
Raw Normal View History

package geth
import (
"strings"
"log"
2018-01-06 20:31:53 +00:00
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum/common"
2017-11-02 21:43:07 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
2017-11-08 20:55:35 +00:00
"golang.org/x/net/context"
)
2017-11-08 20:55:35 +00:00
type GethClient interface {
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)
}
func ToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
transactions := convertTransactionsToCore(gethBlock, client)
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()),
GasLimit: gethBlock.GasLimit().Int64(),
GasUsed: gethBlock.GasUsed().Int64(),
2017-11-02 14:36:53 +00:00
Hash: gethBlock.Hash().Hex(),
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(),
Size: gethBlock.Size().Int64(),
Time: gethBlock.Time().Int64(),
Transactions: transactions,
2017-11-02 14:36:53 +00:00
UncleHash: gethBlock.UncleHash().Hex(),
}
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles())
coreBlock.UnclesReward = CalcUnclesReward(coreBlock, gethBlock.Uncles())
return coreBlock
}
func convertTransactionsToCore(gethBlock *types.Block, client GethClient) []core.Transaction {
transactions := make([]core.Transaction, 0)
for i, gethTransaction := range gethBlock.Transactions() {
from, err := client.TransactionSender(context.Background(), gethTransaction, gethBlock.Hash(), uint(i))
if err != nil {
log.Println(err)
}
transaction := transToCoreTrans(gethTransaction, &from)
transaction, err = appendReceiptToTransaction(client, transaction)
if err != nil {
log.Println(err)
}
transactions = append(transactions, transaction)
}
return transactions
}
func appendReceiptToTransaction(client GethClient, transaction core.Transaction) (core.Transaction, error) {
gethReceipt, err := client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
2018-01-08 20:19:42 +00:00
if err != nil {
log.Println(err)
return transaction, err
}
receipt := ReceiptToCoreReceipt(gethReceipt)
transaction.Receipt = receipt
return transaction, err
}
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(),
To: strings.ToLower(addressToHex(transaction.To())),
From: strings.ToLower(addressToHex(from)),
2017-11-08 20:55:35 +00:00
GasLimit: transaction.Gas().Int64(),
GasPrice: transaction.GasPrice().Int64(),
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 {
if to == nil {
return ""
} else {
return to.Hex()
}
}