Added From field to transactions

This commit is contained in:
Matt Krump 2017-11-08 14:55:35 -06:00
parent 84205a21ea
commit c7bd6de7da
9 changed files with 57 additions and 28 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE transactions
DROP COLUMN tx_from

View File

@ -0,0 +1,2 @@
ALTER TABLE transactions
ADD COLUMN tx_from VARCHAR(66)

View File

@ -94,7 +94,8 @@ CREATE TABLE transactions (
tx_gaslimit numeric,
tx_gasprice numeric,
tx_value numeric,
block_id integer NOT NULL
block_id integer NOT NULL,
tx_from character varying(66)
);

View File

@ -5,6 +5,7 @@ type Transaction struct {
Data []byte
Nonce uint64
To string
From string
GasLimit int64
GasPrice int64
Value int64

View File

@ -5,26 +5,19 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"golang.org/x/net/context"
)
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction {
to := transaction.To()
toHex := convertTo(to)
return core.Transaction{
Hash: transaction.Hash().Hex(),
Data: transaction.Data(),
Nonce: transaction.Nonce(),
To: toHex,
GasLimit: transaction.Gas().Int64(),
GasPrice: transaction.GasPrice().Int64(),
Value: transaction.Value().Int64(),
}
type GethClient interface {
TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)
}
func GethBlockToCoreBlock(gethBlock *types.Block) core.Block {
func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
transactions := []core.Transaction{}
for _, gethTransaction := range gethBlock.Transactions() {
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
for i, gethTransaction := range gethBlock.Transactions() {
from, _ := client.TransactionSender(context.Background(), gethTransaction, gethBlock.Hash(), uint(i))
transaction := gethTransToCoreTrans(gethTransaction, &from)
transactions = append(transactions, transaction)
}
return core.Block{
Difficulty: gethBlock.Difficulty().Int64(),
@ -41,7 +34,20 @@ func GethBlockToCoreBlock(gethBlock *types.Block) core.Block {
}
}
func convertTo(to *common.Address) string {
func gethTransToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction {
return core.Transaction{
Hash: transaction.Hash().Hex(),
Data: transaction.Data(),
Nonce: transaction.Nonce(),
To: addressToHex(transaction.To()),
From: addressToHex(from),
GasLimit: transaction.Gas().Int64(),
GasPrice: transaction.GasPrice().Int64(),
Value: transaction.Value().Int64(),
}
}
func addressToHex(to *common.Address) string {
if to == nil {
return ""
} else {

View File

@ -3,6 +3,8 @@ package geth_test
import (
"math/big"
"context"
"github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
@ -11,6 +13,12 @@ import (
. "github.com/onsi/gomega"
)
type FakeGethClient struct{}
func (client *FakeGethClient) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
return common.HexToAddress("0x123"), nil
}
var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("converts basic Block metada", func() {
@ -32,7 +40,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
UncleHash: common.Hash{128},
}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
gethBlock := geth.GethBlockToCoreBlock(block)
client := &FakeGethClient{}
gethBlock := geth.GethBlockToCoreBlock(block, client)
Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64()))
Expect(gethBlock.GasLimit).To(Equal(gasLimit))
@ -50,8 +59,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("is empty", func() {
header := types.Header{}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
coreBlock := geth.GethBlockToCoreBlock(block)
client := &FakeGethClient{}
coreBlock := geth.GethBlockToCoreBlock(block, client)
Expect(len(coreBlock.Transactions)).To(Equal(0))
})
@ -66,13 +75,15 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
payload := []byte("1234")
gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
client := &FakeGethClient{}
gethBlock := types.NewBlock(&header, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{})
coreBlock := geth.GethBlockToCoreBlock(gethBlock)
coreBlock := geth.GethBlockToCoreBlock(gethBlock, client)
Expect(len(coreBlock.Transactions)).To(Equal(1))
coreTransaction := coreBlock.Transactions[0]
Expect(coreTransaction.Data).To(Equal(gethTransaction.Data()))
Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex()))
Expect(coreTransaction.From).To(Equal("0x0000000000000000000000000000000000000123"))
Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64()))
Expect(coreTransaction.GasPrice).To(Equal(gethTransaction.GasPrice().Int64()))
Expect(coreTransaction.Value).To(Equal(gethTransaction.Value().Int64()))
@ -82,8 +93,9 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("has an empty to field when transaction creates a new contract", func() {
gethTransaction := types.NewContractCreation(uint64(10000), big.NewInt(10), big.NewInt(5000), big.NewInt(3), []byte("1234"))
gethBlock := types.NewBlock(&types.Header{}, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{})
client := &FakeGethClient{}
coreBlock := geth.GethBlockToCoreBlock(gethBlock)
coreBlock := geth.GethBlockToCoreBlock(gethBlock, client)
coreTransaction := coreBlock.Transactions[0]
Expect(coreTransaction.To).To(Equal(""))

View File

@ -21,7 +21,7 @@ type GethBlockchain struct {
func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block {
gethBlock, _ := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
return GethBlockToCoreBlock(gethBlock)
return GethBlockToCoreBlock(gethBlock, blockchain.client)
}
func NewGethBlockchain(ipcPath string) *GethBlockchain {

View File

@ -99,9 +99,9 @@ func (repository Postgres) createTransactions(tx *sql.Tx, blockId int64, transac
for _, transaction := range transactions {
_, err := tx.Exec(
`INSERT INTO transactions
(block_id, tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.GasLimit, transaction.GasPrice, transaction.Value)
(block_id, tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, transaction.Value)
if err != nil {
return err
}
@ -138,20 +138,22 @@ func (repository Postgres) loadBlock(blockRows *sql.Rows) core.Block {
}
}
func (repository Postgres) loadTransactions(blockId int64) []core.Transaction {
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value FROM transactions`)
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions`)
var transactions []core.Transaction
for transactionRows.Next() {
var hash string
var nonce uint64
var to string
var from string
var gasLimit int64
var gasPrice int64
var value int64
transactionRows.Scan(&hash, &nonce, &to, &gasLimit, &gasPrice, &value)
transactionRows.Scan(&hash, &nonce, &to, &from, &gasLimit, &gasPrice, &value)
transaction := core.Transaction{
Hash: hash,
Nonce: nonce,
To: to,
From: from,
GasLimit: gasLimit,
GasPrice: gasPrice,
Value: value,

View File

@ -111,6 +111,7 @@ var _ = Describe("Repositories", func() {
gasPrice := int64(3)
nonce := uint64(10000)
to := "1234567890"
from := "0987654321"
value := int64(10)
transaction := core.Transaction{
Hash: "x1234",
@ -118,6 +119,7 @@ var _ = Describe("Repositories", func() {
GasLimit: gasLimit,
Nonce: nonce,
To: to,
From: from,
Value: value,
}
block := core.Block{
@ -132,6 +134,7 @@ var _ = Describe("Repositories", func() {
savedTransaction := savedBlock.Transactions[0]
Expect(savedTransaction.Hash).To(Equal(transaction.Hash))
Expect(savedTransaction.To).To(Equal(to))
Expect(savedTransaction.From).To(Equal(from))
Expect(savedTransaction.Nonce).To(Equal(nonce))
Expect(savedTransaction.GasLimit).To(Equal(gasLimit))
Expect(savedTransaction.GasPrice).To(Equal(gasPrice))