From 351d315a4c8c514b91d776b4cef8459608ad67b9 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Thu, 28 Dec 2017 17:04:15 -0600 Subject: [PATCH] Add tx fields --- ...02281_add_data_input_to_transactions.down.sql | 2 ++ ...4502281_add_data_input_to_transactions.up.sql | 2 ++ db/schema.sql | 3 ++- pkg/core/transaction.go | 2 +- pkg/geth/geth_block_to_core_block.go | 3 ++- pkg/geth/geth_block_to_core_block_test.go | 5 +++-- pkg/repositories/postgres.go | 16 ++++++++++------ pkg/repositories/testing/helpers.go | 3 +++ 8 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 db/migrations/1514502281_add_data_input_to_transactions.down.sql create mode 100644 db/migrations/1514502281_add_data_input_to_transactions.up.sql diff --git a/db/migrations/1514502281_add_data_input_to_transactions.down.sql b/db/migrations/1514502281_add_data_input_to_transactions.down.sql new file mode 100644 index 00000000..cbaddf86 --- /dev/null +++ b/db/migrations/1514502281_add_data_input_to_transactions.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE transactions + DROP COLUMN tx_input_data; \ No newline at end of file diff --git a/db/migrations/1514502281_add_data_input_to_transactions.up.sql b/db/migrations/1514502281_add_data_input_to_transactions.up.sql new file mode 100644 index 00000000..963d0c75 --- /dev/null +++ b/db/migrations/1514502281_add_data_input_to_transactions.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE transactions + ADD COLUMN tx_input_data VARCHAR; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index b3eae30a..c72a368f 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -168,7 +168,8 @@ CREATE TABLE transactions ( tx_gasprice numeric, tx_value numeric, block_id integer NOT NULL, - tx_from character varying(66) + tx_from character varying(66), + tx_input_data character varying ); diff --git a/pkg/core/transaction.go b/pkg/core/transaction.go index 8a46a2e2..fe12291a 100644 --- a/pkg/core/transaction.go +++ b/pkg/core/transaction.go @@ -2,7 +2,7 @@ package core type Transaction struct { Hash string - Data []byte + Data string Nonce uint64 To string From string diff --git a/pkg/geth/geth_block_to_core_block.go b/pkg/geth/geth_block_to_core_block.go index ece19b56..f5d9b8e1 100644 --- a/pkg/geth/geth_block_to_core_block.go +++ b/pkg/geth/geth_block_to_core_block.go @@ -44,15 +44,16 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block } func gethTransToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction { + data := hexutil.Encode(transaction.Data()) return core.Transaction{ Hash: transaction.Hash().Hex(), - Data: transaction.Data(), Nonce: transaction.Nonce(), To: strings.ToLower(addressToHex(transaction.To())), From: strings.ToLower(addressToHex(from)), GasLimit: transaction.Gas().Int64(), GasPrice: transaction.GasPrice().Int64(), Value: transaction.Value().Int64(), + Data: data, } } diff --git a/pkg/geth/geth_block_to_core_block_test.go b/pkg/geth/geth_block_to_core_block_test.go index 5d551a6a..cc23f6e6 100644 --- a/pkg/geth/geth_block_to_core_block_test.go +++ b/pkg/geth/geth_block_to_core_block_test.go @@ -203,7 +203,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { amount := big.NewInt(10) gasLimit := big.NewInt(5000) gasPrice := big.NewInt(3) - payload := []byte("1234") + input := "0xf7d8c8830000000000000000000000000000000000000000000000000000000000037788000000000000000000000000000000000000000000000000000000000003bd14" + payload, _ := hexutil.Decode(input) gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload) @@ -214,7 +215,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { Expect(len(coreBlock.Transactions)).To(Equal(1)) coreTransaction := coreBlock.Transactions[0] - Expect(coreTransaction.Data).To(Equal(gethTransaction.Data())) + Expect(coreTransaction.Data).To(Equal(input)) Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex())) Expect(coreTransaction.From).To(Equal("0x0000000000000000000000000000000000000123")) Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64())) diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index c4b00e5b..440a0667 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -294,9 +294,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_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) + (block_id, tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value, tx_input_data) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, + blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, transaction.Value, transaction.Data) if err != nil { return err } @@ -332,7 +332,8 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) { tx_from, tx_gaslimit, tx_gasprice, - tx_value + tx_value, + tx_input_data FROM transactions WHERE block_id = $1 ORDER BY tx_hash`, blockId) @@ -392,8 +393,9 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr var from string var gasLimit int64 var gasPrice int64 + var inputData string var value int64 - transactionRows.Scan(&hash, &nonce, &to, &from, &gasLimit, &gasPrice, &value) + transactionRows.Scan(&hash, &nonce, &to, &from, &gasLimit, &gasPrice, &value, &inputData) transaction := core.Transaction{ Hash: hash, Nonce: nonce, @@ -402,6 +404,7 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr GasLimit: gasLimit, GasPrice: gasPrice, Value: value, + Data: inputData, } transactions = append(transactions, transaction) } @@ -416,7 +419,8 @@ func (repository Postgres) addTransactions(contract core.Contract) core.Contract tx_from, tx_gaslimit, tx_gasprice, - tx_value + tx_value, + tx_input_data FROM transactions WHERE tx_to = $1 ORDER BY block_id DESC`, contract.Hash) diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index ca7420db..e17d2be2 100644 --- a/pkg/repositories/testing/helpers.go +++ b/pkg/repositories/testing/helpers.go @@ -191,6 +191,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. to := "1234567890" from := "0987654321" value := int64(10) + inputData := "0xf7d8c8830000000000000000000000000000000000000000000000000000000000037788000000000000000000000000000000000000000000000000000000000003bd14" transaction := core.Transaction{ Hash: "x1234", GasPrice: gasPrice, @@ -199,6 +200,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. To: to, From: from, Value: value, + Data: inputData, } block := core.Block{ Number: 123, @@ -210,6 +212,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. savedBlock, _ := repository.FindBlockByNumber(123) Expect(len(savedBlock.Transactions)).To(Equal(1)) savedTransaction := savedBlock.Transactions[0] + Expect(savedTransaction.Data).To(Equal(transaction.Data)) Expect(savedTransaction.Hash).To(Equal(transaction.Hash)) Expect(savedTransaction.To).To(Equal(to)) Expect(savedTransaction.From).To(Equal(from))