forked from cerc-io/ipld-eth-server
updates for light sync transactions
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package rpc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestRpc(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Rpc Suite")
|
||||
}
|
||||
@@ -19,11 +19,13 @@ package rpc
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
@@ -35,29 +37,75 @@ type RpcTransactionConverter struct {
|
||||
client core.EthClient
|
||||
}
|
||||
|
||||
// raw transaction data, required for generating RLP
|
||||
type transactionData struct {
|
||||
AccountNonce uint64
|
||||
Price *big.Int
|
||||
GasLimit uint64
|
||||
Recipient *common.Address `rlp:"nil"` // nil means contract creation
|
||||
Amount *big.Int
|
||||
Payload []byte
|
||||
V *big.Int
|
||||
R *big.Int
|
||||
S *big.Int
|
||||
}
|
||||
|
||||
func NewRpcTransactionConverter(client core.EthClient) *RpcTransactionConverter {
|
||||
return &RpcTransactionConverter{client: client}
|
||||
}
|
||||
|
||||
func (rtc *RpcTransactionConverter) ConvertTransactionsToCore(gethBlock *types.Block) ([]core.Transaction, error) {
|
||||
func (converter *RpcTransactionConverter) ConvertRpcTransactionsToModels(transactions []core.RpcTransaction) ([]core.TransactionModel, error) {
|
||||
var results []core.TransactionModel
|
||||
for _, transaction := range transactions {
|
||||
txData, convertErr := getTransactionData(transaction)
|
||||
if convertErr != nil {
|
||||
return nil, convertErr
|
||||
}
|
||||
txRLP, rlpErr := getTransactionRLP(txData)
|
||||
if rlpErr != nil {
|
||||
return nil, rlpErr
|
||||
}
|
||||
txIndex, txIndexErr := hexToBigInt(transaction.TransactionIndex)
|
||||
if txIndexErr != nil {
|
||||
return nil, txIndexErr
|
||||
}
|
||||
transactionModel := core.TransactionModel{
|
||||
Data: transaction.Payload,
|
||||
From: transaction.From,
|
||||
GasLimit: txData.GasLimit,
|
||||
GasPrice: txData.Price.Int64(),
|
||||
Hash: transaction.Hash,
|
||||
Nonce: txData.AccountNonce,
|
||||
Raw: txRLP,
|
||||
// NOTE: Light Sync transactions don't include receipt; would require separate RPC call
|
||||
To: transaction.Recipient,
|
||||
TxIndex: txIndex.Int64(),
|
||||
Value: txData.Amount.String(),
|
||||
}
|
||||
results = append(results, transactionModel)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (converter *RpcTransactionConverter) ConvertBlockTransactionsToCore(gethBlock *types.Block) ([]core.TransactionModel, error) {
|
||||
var g errgroup.Group
|
||||
coreTransactions := make([]core.Transaction, len(gethBlock.Transactions()))
|
||||
coreTransactions := make([]core.TransactionModel, 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 := rtc.client.TransactionSender(context.Background(), transaction, gethBlock.Hash(), transactionIndex)
|
||||
from, err := converter.client.TransactionSender(context.Background(), transaction, gethBlock.Hash(), transactionIndex)
|
||||
if err != nil {
|
||||
log.Println("transaction sender: ", err)
|
||||
return err
|
||||
}
|
||||
coreTransaction, convertErr := transToCoreTrans(transaction, &from, int64(gethTransactionIndex))
|
||||
coreTransaction, convertErr := convertGethTransactionToModel(transaction, &from, int64(gethTransactionIndex))
|
||||
if convertErr != nil {
|
||||
return convertErr
|
||||
}
|
||||
coreTransaction, err = rtc.appendReceiptToTransaction(coreTransaction)
|
||||
coreTransaction, err = converter.appendReceiptToTransaction(coreTransaction)
|
||||
if err != nil {
|
||||
log.Println("receipt: ", err)
|
||||
return err
|
||||
@@ -73,7 +121,7 @@ func (rtc *RpcTransactionConverter) ConvertTransactionsToCore(gethBlock *types.B
|
||||
return coreTransactions, nil
|
||||
}
|
||||
|
||||
func (rtc *RpcTransactionConverter) appendReceiptToTransaction(transaction core.Transaction) (core.Transaction, error) {
|
||||
func (rtc *RpcTransactionConverter) appendReceiptToTransaction(transaction core.TransactionModel) (core.TransactionModel, error) {
|
||||
gethReceipt, err := rtc.client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
|
||||
if err != nil {
|
||||
return transaction, err
|
||||
@@ -83,15 +131,14 @@ func (rtc *RpcTransactionConverter) appendReceiptToTransaction(transaction core.
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func transToCoreTrans(transaction *types.Transaction, from *common.Address, transactionIndex int64) (core.Transaction, error) {
|
||||
data := hexutil.Encode(transaction.Data())
|
||||
var raw bytes.Buffer
|
||||
func convertGethTransactionToModel(transaction *types.Transaction, from *common.Address, transactionIndex int64) (core.TransactionModel, error) {
|
||||
raw := bytes.Buffer{}
|
||||
encodeErr := transaction.EncodeRLP(&raw)
|
||||
if encodeErr != nil {
|
||||
return core.Transaction{}, encodeErr
|
||||
return core.TransactionModel{}, encodeErr
|
||||
}
|
||||
return core.Transaction{
|
||||
Data: data,
|
||||
return core.TransactionModel{
|
||||
Data: transaction.Data(),
|
||||
From: strings.ToLower(addressToHex(from)),
|
||||
GasLimit: transaction.Gas(),
|
||||
GasPrice: transaction.GasPrice().Int64(),
|
||||
@@ -104,9 +151,70 @@ func transToCoreTrans(transaction *types.Transaction, from *common.Address, tran
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getTransactionData(transaction core.RpcTransaction) (transactionData, error) {
|
||||
nonce, nonceErr := hexToBigInt(transaction.Nonce)
|
||||
if nonceErr != nil {
|
||||
return transactionData{}, nonceErr
|
||||
}
|
||||
gasPrice, gasPriceErr := hexToBigInt(transaction.GasPrice)
|
||||
if gasPriceErr != nil {
|
||||
return transactionData{}, gasPriceErr
|
||||
}
|
||||
gasLimit, gasLimitErr := hexToBigInt(transaction.GasLimit)
|
||||
if gasLimitErr != nil {
|
||||
return transactionData{}, gasLimitErr
|
||||
}
|
||||
recipient := common.HexToAddress(transaction.Recipient)
|
||||
amount, amountErr := hexToBigInt(transaction.Amount)
|
||||
if amountErr != nil {
|
||||
return transactionData{}, amountErr
|
||||
}
|
||||
v, vErr := hexToBigInt(transaction.V)
|
||||
if vErr != nil {
|
||||
return transactionData{}, vErr
|
||||
}
|
||||
r, rErr := hexToBigInt(transaction.R)
|
||||
if rErr != nil {
|
||||
return transactionData{}, rErr
|
||||
}
|
||||
s, sErr := hexToBigInt(transaction.S)
|
||||
if sErr != nil {
|
||||
return transactionData{}, sErr
|
||||
}
|
||||
return transactionData{
|
||||
AccountNonce: nonce.Uint64(),
|
||||
Price: gasPrice,
|
||||
GasLimit: gasLimit.Uint64(),
|
||||
Recipient: &recipient,
|
||||
Amount: amount,
|
||||
Payload: transaction.Payload,
|
||||
V: v,
|
||||
R: r,
|
||||
S: s,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getTransactionRLP(txData transactionData) ([]byte, error) {
|
||||
transactionRlp := bytes.Buffer{}
|
||||
encodeErr := rlp.Encode(&transactionRlp, txData)
|
||||
if encodeErr != nil {
|
||||
return nil, encodeErr
|
||||
}
|
||||
return transactionRlp.Bytes(), nil
|
||||
}
|
||||
|
||||
func addressToHex(to *common.Address) string {
|
||||
if to == nil {
|
||||
return ""
|
||||
}
|
||||
return to.Hex()
|
||||
}
|
||||
|
||||
func hexToBigInt(hex string) (*big.Int, error) {
|
||||
result := big.NewInt(0)
|
||||
_, scanErr := fmt.Sscan(hex, result)
|
||||
if scanErr != nil {
|
||||
return nil, scanErr
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package rpc_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
||||
)
|
||||
|
||||
var _ = Describe("RPC transaction converter", func() {
|
||||
It("converts hex fields to integers", func() {
|
||||
converter := rpc.RpcTransactionConverter{}
|
||||
rpcTransaction := getFakeRpcTransaction("0x1")
|
||||
|
||||
transactionModels, err := converter.ConvertRpcTransactionsToModels([]core.RpcTransaction{rpcTransaction})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(transactionModels)).To(Equal(1))
|
||||
Expect(transactionModels[0].GasLimit).To(Equal(uint64(1)))
|
||||
Expect(transactionModels[0].GasPrice).To(Equal(int64(1)))
|
||||
Expect(transactionModels[0].Nonce).To(Equal(uint64(1)))
|
||||
Expect(transactionModels[0].TxIndex).To(Equal(int64(1)))
|
||||
Expect(transactionModels[0].Value).To(Equal("1"))
|
||||
})
|
||||
|
||||
It("returns error if invalid hex cannot be converted", func() {
|
||||
converter := rpc.RpcTransactionConverter{}
|
||||
invalidTransaction := getFakeRpcTransaction("invalid")
|
||||
|
||||
_, err := converter.ConvertRpcTransactionsToModels([]core.RpcTransaction{invalidTransaction})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("copies RPC transaction hash, from, and to values to model", func() {
|
||||
converter := rpc.RpcTransactionConverter{}
|
||||
rpcTransaction := getFakeRpcTransaction("0x1")
|
||||
|
||||
transactionModels, err := converter.ConvertRpcTransactionsToModels([]core.RpcTransaction{rpcTransaction})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(transactionModels)).To(Equal(1))
|
||||
Expect(transactionModels[0].Hash).To(Equal(rpcTransaction.Hash))
|
||||
Expect(transactionModels[0].From).To(Equal(rpcTransaction.From))
|
||||
Expect(transactionModels[0].To).To(Equal(rpcTransaction.Recipient))
|
||||
})
|
||||
|
||||
XIt("derives transaction RLP", func() {
|
||||
// actual transaction: https://kovan.etherscan.io/tx/0x73aefdf70fc5650e0dd82affbb59d107f12dfabc50a78625b434ea68b7a69ee6
|
||||
// actual RLP hex: 0x2926af093b6b72e3f10089bde6da0f99b0d4e13354f6f37c8334efc9d7e99a47
|
||||
|
||||
})
|
||||
|
||||
It("does not include transaction receipt", func() {
|
||||
converter := rpc.RpcTransactionConverter{}
|
||||
rpcTransaction := getFakeRpcTransaction("0x1")
|
||||
|
||||
transactionModels, err := converter.ConvertRpcTransactionsToModels([]core.RpcTransaction{rpcTransaction})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(transactionModels)).To(Equal(1))
|
||||
Expect(transactionModels[0].Receipt).To(Equal(core.Receipt{}))
|
||||
})
|
||||
})
|
||||
|
||||
func getFakeRpcTransaction(hex string) core.RpcTransaction {
|
||||
return core.RpcTransaction{
|
||||
Hash: "0x2",
|
||||
Amount: hex,
|
||||
GasLimit: hex,
|
||||
GasPrice: hex,
|
||||
Nonce: hex,
|
||||
From: fakes.FakeAddress.Hex(),
|
||||
Recipient: fakes.FakeAddress.Hex(),
|
||||
V: "0x2",
|
||||
R: "0x2",
|
||||
S: "0x2",
|
||||
Payload: nil,
|
||||
TransactionIndex: hex,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user