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/>.
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
package rpc
|
2017-11-02 19:37:07 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-19 19:11:26 +00:00
|
|
|
"bytes"
|
2018-05-02 16:17:02 +00:00
|
|
|
"context"
|
2019-03-27 04:05:30 +00:00
|
|
|
"fmt"
|
2018-01-03 17:23:43 +00:00
|
|
|
"log"
|
2019-03-27 04:05:30 +00:00
|
|
|
"math/big"
|
2018-05-02 16:17:02 +00:00
|
|
|
"strings"
|
2018-01-03 17:23:43 +00:00
|
|
|
|
2019-05-17 04:15:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-03-27 17:26:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-03-27 21:06:12 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2019-10-28 11:30:24 +00:00
|
|
|
vulcCommon "github.com/vulcanize/vulcanizedb/pkg/eth/converters/common"
|
2017-11-02 19:37:07 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
type RPCTransactionConverter struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
client core.EthClient
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func NewRPCTransactionConverter(client core.EthClient) *RPCTransactionConverter {
|
|
|
|
return &RPCTransactionConverter{client: client}
|
2017-11-02 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (converter *RPCTransactionConverter) ConvertRPCTransactionsToModels(transactions []core.RPCTransaction) ([]core.TransactionModel, error) {
|
2019-03-27 04:05:30 +00:00
|
|
|
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{
|
2019-03-27 18:49:44 +00:00
|
|
|
Data: txData.Payload,
|
2019-03-27 04:05:30 +00:00
|
|
|
From: transaction.From,
|
|
|
|
GasLimit: txData.GasLimit,
|
|
|
|
GasPrice: txData.Price.Int64(),
|
|
|
|
Hash: transaction.Hash,
|
|
|
|
Nonce: txData.AccountNonce,
|
|
|
|
Raw: txRLP,
|
2019-05-01 06:02:30 +00:00
|
|
|
// NOTE: Header Sync transactions don't include receipt; would require separate RPC call
|
2019-03-27 04:05:30 +00:00
|
|
|
To: transaction.Recipient,
|
|
|
|
TxIndex: txIndex.Int64(),
|
|
|
|
Value: txData.Amount.String(),
|
|
|
|
}
|
|
|
|
results = append(results, transactionModel)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (converter *RPCTransactionConverter) ConvertBlockTransactionsToCore(gethBlock *types.Block) ([]core.TransactionModel, error) {
|
2018-03-27 21:06:12 +00:00
|
|
|
var g errgroup.Group
|
2019-03-27 04:05:30 +00:00
|
|
|
coreTransactions := make([]core.TransactionModel, len(gethBlock.Transactions()))
|
2018-03-27 21:06:12 +00:00
|
|
|
|
|
|
|
for gethTransactionIndex, gethTransaction := range gethBlock.Transactions() {
|
|
|
|
//https://golang.org/doc/faq#closures_and_goroutines
|
|
|
|
transaction := gethTransaction
|
|
|
|
transactionIndex := uint(gethTransactionIndex)
|
|
|
|
g.Go(func() error {
|
2019-03-27 04:05:30 +00:00
|
|
|
from, err := converter.client.TransactionSender(context.Background(), transaction, gethBlock.Hash(), transactionIndex)
|
2018-03-27 21:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("transaction sender: ", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
coreTransaction, convertErr := convertGethTransactionToModel(transaction, &from, int64(gethTransactionIndex))
|
2019-03-19 19:11:26 +00:00
|
|
|
if convertErr != nil {
|
|
|
|
return convertErr
|
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
coreTransaction, err = converter.appendReceiptToTransaction(coreTransaction)
|
2018-03-27 21:06:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func (converter *RPCTransactionConverter) appendReceiptToTransaction(transaction core.TransactionModel) (core.TransactionModel, error) {
|
|
|
|
gethReceipt, err := converter.client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
|
2018-01-08 20:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return transaction, err
|
|
|
|
}
|
2019-03-21 19:52:24 +00:00
|
|
|
receipt, err := vulcCommon.ToCoreReceipt(gethReceipt)
|
|
|
|
if err != nil {
|
|
|
|
return transaction, err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func convertGethTransactionToModel(transaction *types.Transaction, from *common.Address, transactionIndex int64) (core.TransactionModel, error) {
|
|
|
|
raw := bytes.Buffer{}
|
2019-03-19 19:11:26 +00:00
|
|
|
encodeErr := transaction.EncodeRLP(&raw)
|
|
|
|
if encodeErr != nil {
|
2019-03-27 04:05:30 +00:00
|
|
|
return core.TransactionModel{}, encodeErr
|
2019-03-19 19:11:26 +00:00
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
return core.TransactionModel{
|
|
|
|
Data: transaction.Data(),
|
2017-12-20 20:06:22 +00:00
|
|
|
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(),
|
2019-03-19 19:11:26 +00:00
|
|
|
Hash: transaction.Hash().Hex(),
|
|
|
|
Nonce: transaction.Nonce(),
|
|
|
|
Raw: raw.Bytes(),
|
|
|
|
To: strings.ToLower(addressToHex(transaction.To())),
|
|
|
|
TxIndex: transactionIndex,
|
2018-01-16 20:25:33 +00:00
|
|
|
Value: transaction.Value().String(),
|
2019-03-19 19:11:26 +00:00
|
|
|
}, nil
|
2017-11-08 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func getTransactionData(transaction core.RPCTransaction) (transactionData, error) {
|
2019-03-27 04:05:30 +00:00
|
|
|
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,
|
2019-03-27 18:49:44 +00:00
|
|
|
Payload: hexutil.MustDecode(transaction.Payload),
|
2019-03-27 04:05:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-03-27 04:05:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|