ipld-eth-server/pkg/geth/converters/rpc/transaction_converter.go

86 lines
2.5 KiB
Go
Raw Normal View History

2018-05-02 16:17:02 +00:00
package rpc
import (
2018-05-02 16:17:02 +00:00
"context"
"log"
2018-05-02 16:17:02 +00:00
"strings"
"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"
"golang.org/x/sync/errgroup"
2018-05-02 16:17:02 +00:00
"github.com/vulcanize/vulcanizedb/pkg/core"
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/geth/converters/common"
)
2018-05-02 16:17:02 +00:00
type RpcTransactionConverter struct {
client core.EthClient
2018-05-02 16:17:02 +00:00
}
func NewRpcTransactionConverter(client core.EthClient) *RpcTransactionConverter {
2018-05-02 16:17:02 +00:00
return &RpcTransactionConverter{client: client}
}
2018-05-02 16:17:02 +00:00
func (rtc *RpcTransactionConverter) ConvertTransactionsToCore(gethBlock *types.Block) ([]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 {
2018-05-02 16:17:02 +00:00
from, err := rtc.client.TransactionSender(context.Background(), transaction, gethBlock.Hash(), transactionIndex)
if err != nil {
log.Println("transaction sender: ", err)
return err
}
coreTransaction := transToCoreTrans(transaction, &from)
coreTransaction, err = rtc.appendReceiptToTransaction(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
}
return coreTransactions, nil
}
func (rtc *RpcTransactionConverter) appendReceiptToTransaction(transaction core.Transaction) (core.Transaction, error) {
gethReceipt, err := rtc.client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
2018-01-08 20:19:42 +00:00
if err != nil {
return transaction, err
}
2018-05-02 16:17:02 +00:00
receipt := vulcCommon.ToCoreReceipt(gethReceipt)
transaction.Receipt = receipt
return transaction, nil
}
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)),
GasLimit: transaction.Gas(),
2017-11-08 20:55:35 +00:00
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 ""
}
return to.Hex()
}