Merge pull request #67 from 8thlight/from_complete

Added From field to transactions
This commit is contained in:
Matt K 2017-11-13 10:31:57 -06:00 committed by GitHub
commit 74b51a6e05
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_gaslimit numeric,
tx_gasprice numeric, tx_gasprice numeric,
tx_value 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 Data []byte
Nonce uint64 Nonce uint64
To string To string
From string
GasLimit int64 GasLimit int64
GasPrice int64 GasPrice int64
Value int64 Value int64

View File

@ -5,26 +5,19 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"golang.org/x/net/context"
) )
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction { type GethClient interface {
to := transaction.To() TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)
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(),
}
} }
func GethBlockToCoreBlock(gethBlock *types.Block) core.Block { func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
transactions := []core.Transaction{} transactions := []core.Transaction{}
for _, gethTransaction := range gethBlock.Transactions() { for i, gethTransaction := range gethBlock.Transactions() {
transactions = append(transactions, gethTransToCoreTrans(gethTransaction)) from, _ := client.TransactionSender(context.Background(), gethTransaction, gethBlock.Hash(), uint(i))
transaction := gethTransToCoreTrans(gethTransaction, &from)
transactions = append(transactions, transaction)
} }
return core.Block{ return core.Block{
Difficulty: gethBlock.Difficulty().Int64(), 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 { if to == nil {
return "" return ""
} else { } else {

View File

@ -3,6 +3,8 @@ package geth_test
import ( import (
"math/big" "math/big"
"context"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -11,6 +13,12 @@ import (
. "github.com/onsi/gomega" . "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() { var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("converts basic Block metada", func() { It("converts basic Block metada", func() {
@ -32,7 +40,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
UncleHash: common.Hash{128}, UncleHash: common.Hash{128},
} }
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{}) 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.Difficulty).To(Equal(difficulty.Int64()))
Expect(gethBlock.GasLimit).To(Equal(gasLimit)) Expect(gethBlock.GasLimit).To(Equal(gasLimit))
@ -50,8 +59,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("is empty", func() { It("is empty", func() {
header := types.Header{} header := types.Header{}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{}) block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
client := &FakeGethClient{}
coreBlock := geth.GethBlockToCoreBlock(block) coreBlock := geth.GethBlockToCoreBlock(block, client)
Expect(len(coreBlock.Transactions)).To(Equal(0)) Expect(len(coreBlock.Transactions)).To(Equal(0))
}) })
@ -66,13 +75,15 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
payload := []byte("1234") payload := []byte("1234")
gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload) gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload)
client := &FakeGethClient{}
gethBlock := types.NewBlock(&header, []*types.Transaction{gethTransaction}, []*types.Header{}, []*types.Receipt{}) 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)) Expect(len(coreBlock.Transactions)).To(Equal(1))
coreTransaction := coreBlock.Transactions[0] coreTransaction := coreBlock.Transactions[0]
Expect(coreTransaction.Data).To(Equal(gethTransaction.Data())) Expect(coreTransaction.Data).To(Equal(gethTransaction.Data()))
Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex())) Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex()))
Expect(coreTransaction.From).To(Equal("0x0000000000000000000000000000000000000123"))
Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64())) Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64()))
Expect(coreTransaction.GasPrice).To(Equal(gethTransaction.GasPrice().Int64())) Expect(coreTransaction.GasPrice).To(Equal(gethTransaction.GasPrice().Int64()))
Expect(coreTransaction.Value).To(Equal(gethTransaction.Value().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() { 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")) 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{}) 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] coreTransaction := coreBlock.Transactions[0]
Expect(coreTransaction.To).To(Equal("")) Expect(coreTransaction.To).To(Equal(""))

View File

@ -21,7 +21,7 @@ type GethBlockchain struct {
func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block { func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block {
gethBlock, _ := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber)) gethBlock, _ := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
return GethBlockToCoreBlock(gethBlock) return GethBlockToCoreBlock(gethBlock, blockchain.client)
} }
func NewGethBlockchain(ipcPath string) *GethBlockchain { 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 { for _, transaction := range transactions {
_, err := tx.Exec( _, err := tx.Exec(
`INSERT INTO transactions `INSERT INTO transactions
(block_id, tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_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)`, VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.GasLimit, transaction.GasPrice, transaction.Value) blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, transaction.Value)
if err != nil { if err != nil {
return err return err
} }
@ -138,20 +138,22 @@ func (repository Postgres) loadBlock(blockRows *sql.Rows) core.Block {
} }
} }
func (repository Postgres) loadTransactions(blockId int64) []core.Transaction { 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 var transactions []core.Transaction
for transactionRows.Next() { for transactionRows.Next() {
var hash string var hash string
var nonce uint64 var nonce uint64
var to string var to string
var from string
var gasLimit int64 var gasLimit int64
var gasPrice int64 var gasPrice int64
var value int64 var value int64
transactionRows.Scan(&hash, &nonce, &to, &gasLimit, &gasPrice, &value) transactionRows.Scan(&hash, &nonce, &to, &from, &gasLimit, &gasPrice, &value)
transaction := core.Transaction{ transaction := core.Transaction{
Hash: hash, Hash: hash,
Nonce: nonce, Nonce: nonce,
To: to, To: to,
From: from,
GasLimit: gasLimit, GasLimit: gasLimit,
GasPrice: gasPrice, GasPrice: gasPrice,
Value: value, Value: value,

View File

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