From c7bd6de7dae0642816dec85e04e4e4e2fbcb1857 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Wed, 8 Nov 2017 14:55:35 -0600 Subject: [PATCH] Added From field to transactions --- ...10262915_add_from_to_transactions.down.sql | 2 + ...1510262915_add_from_to_transactions.up.sql | 2 + db/schema.sql | 3 +- pkg/core/transaction.go | 1 + pkg/geth/geth_block_to_core_block.go | 38 +++++++++++-------- pkg/geth/geth_block_to_core_block_test.go | 22 ++++++++--- pkg/geth/geth_blockchain.go | 2 +- pkg/repositories/postgres.go | 12 +++--- pkg/repositories/repository_test.go | 3 ++ 9 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 db/migrations/1510262915_add_from_to_transactions.down.sql create mode 100644 db/migrations/1510262915_add_from_to_transactions.up.sql diff --git a/db/migrations/1510262915_add_from_to_transactions.down.sql b/db/migrations/1510262915_add_from_to_transactions.down.sql new file mode 100644 index 00000000..1f323136 --- /dev/null +++ b/db/migrations/1510262915_add_from_to_transactions.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE transactions + DROP COLUMN tx_from diff --git a/db/migrations/1510262915_add_from_to_transactions.up.sql b/db/migrations/1510262915_add_from_to_transactions.up.sql new file mode 100644 index 00000000..2222e866 --- /dev/null +++ b/db/migrations/1510262915_add_from_to_transactions.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE transactions + ADD COLUMN tx_from VARCHAR(66) diff --git a/db/schema.sql b/db/schema.sql index 2f6b40ab..e9b5e91e 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -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) ); diff --git a/pkg/core/transaction.go b/pkg/core/transaction.go index a0dfd1d2..8a46a2e2 100644 --- a/pkg/core/transaction.go +++ b/pkg/core/transaction.go @@ -5,6 +5,7 @@ type Transaction struct { Data []byte Nonce uint64 To string + From string GasLimit int64 GasPrice int64 Value int64 diff --git a/pkg/geth/geth_block_to_core_block.go b/pkg/geth/geth_block_to_core_block.go index 7544e2bc..3f50430e 100644 --- a/pkg/geth/geth_block_to_core_block.go +++ b/pkg/geth/geth_block_to_core_block.go @@ -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 { diff --git a/pkg/geth/geth_block_to_core_block_test.go b/pkg/geth/geth_block_to_core_block_test.go index 1b4adf73..494706de 100644 --- a/pkg/geth/geth_block_to_core_block_test.go +++ b/pkg/geth/geth_block_to_core_block_test.go @@ -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("")) diff --git a/pkg/geth/geth_blockchain.go b/pkg/geth/geth_blockchain.go index 177efe34..60ea34f8 100644 --- a/pkg/geth/geth_blockchain.go +++ b/pkg/geth/geth_blockchain.go @@ -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 { diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index 72cdd660..fb11e3f7 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -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, diff --git a/pkg/repositories/repository_test.go b/pkg/repositories/repository_test.go index 98e0fe28..65ace621 100644 --- a/pkg/repositories/repository_test.go +++ b/pkg/repositories/repository_test.go @@ -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))