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 cold_db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ColdDbTransactionConverter struct{}
|
|
|
|
|
|
|
|
func NewColdDbTransactionConverter() *ColdDbTransactionConverter {
|
|
|
|
return &ColdDbTransactionConverter{}
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (cdtc *ColdDbTransactionConverter) ConvertBlockTransactionsToCore(gethBlock *types.Block) ([]core.TransactionModel, error) {
|
2018-05-02 16:17:02 +00:00
|
|
|
var g errgroup.Group
|
2019-03-27 04:05:30 +00:00
|
|
|
coreTransactions := make([]core.TransactionModel, len(gethBlock.Transactions()))
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
for gethTransactionIndex, gethTransaction := range gethBlock.Transactions() {
|
|
|
|
transaction := gethTransaction
|
|
|
|
transactionIndex := uint(gethTransactionIndex)
|
|
|
|
g.Go(func() error {
|
|
|
|
signer := getSigner(transaction)
|
|
|
|
sender, err := signer.Sender(transaction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
coreTransaction := transToCoreTrans(transaction, &sender)
|
|
|
|
coreTransactions[transactionIndex] = coreTransaction
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return coreTransactions, err
|
|
|
|
}
|
|
|
|
return coreTransactions, nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (cdtc *ColdDbTransactionConverter) ConvertRpcTransactionsToModels(transactions []core.RpcTransaction) ([]core.TransactionModel, error) {
|
|
|
|
panic("converting transaction indexes to integer not supported for cold import")
|
|
|
|
}
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
func getSigner(tx *types.Transaction) types.Signer {
|
|
|
|
v, _, _ := tx.RawSignatureValues()
|
|
|
|
if v.Sign() != 0 && tx.Protected() {
|
|
|
|
return types.NewEIP155Signer(tx.ChainId())
|
|
|
|
}
|
|
|
|
return types.HomesteadSigner{}
|
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func transToCoreTrans(transaction *types.Transaction, from *common.Address) core.TransactionModel {
|
|
|
|
return core.TransactionModel{
|
2018-05-02 16:17:02 +00:00
|
|
|
Hash: transaction.Hash().Hex(),
|
|
|
|
Nonce: transaction.Nonce(),
|
|
|
|
To: strings.ToLower(addressToHex(transaction.To())),
|
|
|
|
From: strings.ToLower(addressToHex(from)),
|
|
|
|
GasLimit: transaction.Gas(),
|
|
|
|
GasPrice: transaction.GasPrice().Int64(),
|
|
|
|
Value: transaction.Value().String(),
|
2019-03-27 04:05:30 +00:00
|
|
|
Data: transaction.Data(),
|
2018-05-02 16:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addressToHex(to *common.Address) string {
|
|
|
|
if to == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return to.Hex()
|
|
|
|
}
|