Add tx fields

This commit is contained in:
Matt Krump 2017-12-28 17:04:15 -06:00
parent 7a09839c23
commit 351d315a4c
8 changed files with 25 additions and 11 deletions

View File

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

View File

@ -0,0 +1,2 @@
ALTER TABLE transactions
ADD COLUMN tx_input_data VARCHAR;

View File

@ -168,7 +168,8 @@ CREATE TABLE transactions (
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) tx_from character varying(66),
tx_input_data character varying
); );

View File

@ -2,7 +2,7 @@ package core
type Transaction struct { type Transaction struct {
Hash string Hash string
Data []byte Data string
Nonce uint64 Nonce uint64
To string To string
From string From string

View File

@ -44,15 +44,16 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block
} }
func gethTransToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction { func gethTransToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction {
data := hexutil.Encode(transaction.Data())
return core.Transaction{ return core.Transaction{
Hash: transaction.Hash().Hex(), Hash: transaction.Hash().Hex(),
Data: transaction.Data(),
Nonce: transaction.Nonce(), Nonce: transaction.Nonce(),
To: strings.ToLower(addressToHex(transaction.To())), To: strings.ToLower(addressToHex(transaction.To())),
From: strings.ToLower(addressToHex(from)), From: strings.ToLower(addressToHex(from)),
GasLimit: transaction.Gas().Int64(), GasLimit: transaction.Gas().Int64(),
GasPrice: transaction.GasPrice().Int64(), GasPrice: transaction.GasPrice().Int64(),
Value: transaction.Value().Int64(), Value: transaction.Value().Int64(),
Data: data,
} }
} }

View File

@ -203,7 +203,8 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
amount := big.NewInt(10) amount := big.NewInt(10)
gasLimit := big.NewInt(5000) gasLimit := big.NewInt(5000)
gasPrice := big.NewInt(3) gasPrice := big.NewInt(3)
payload := []byte("1234") input := "0xf7d8c8830000000000000000000000000000000000000000000000000000000000037788000000000000000000000000000000000000000000000000000000000003bd14"
payload, _ := hexutil.Decode(input)
gethTransaction := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, payload) 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)) 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(input))
Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex())) Expect(coreTransaction.To).To(Equal(gethTransaction.To().Hex()))
Expect(coreTransaction.From).To(Equal("0x0000000000000000000000000000000000000123")) Expect(coreTransaction.From).To(Equal("0x0000000000000000000000000000000000000123"))
Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64())) Expect(coreTransaction.GasLimit).To(Equal(gethTransaction.Gas().Int64()))

View File

@ -294,9 +294,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_from, tx_gaslimit, tx_gasprice, tx_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)`, 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) blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, transaction.Value, transaction.Data)
if err != nil { if err != nil {
return err return err
} }
@ -332,7 +332,8 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
tx_from, tx_from,
tx_gaslimit, tx_gaslimit,
tx_gasprice, tx_gasprice,
tx_value tx_value,
tx_input_data
FROM transactions FROM transactions
WHERE block_id = $1 WHERE block_id = $1
ORDER BY tx_hash`, blockId) ORDER BY tx_hash`, blockId)
@ -392,8 +393,9 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
var from string var from string
var gasLimit int64 var gasLimit int64
var gasPrice int64 var gasPrice int64
var inputData string
var value int64 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{ transaction := core.Transaction{
Hash: hash, Hash: hash,
Nonce: nonce, Nonce: nonce,
@ -402,6 +404,7 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
GasLimit: gasLimit, GasLimit: gasLimit,
GasPrice: gasPrice, GasPrice: gasPrice,
Value: value, Value: value,
Data: inputData,
} }
transactions = append(transactions, transaction) transactions = append(transactions, transaction)
} }
@ -416,7 +419,8 @@ func (repository Postgres) addTransactions(contract core.Contract) core.Contract
tx_from, tx_from,
tx_gaslimit, tx_gaslimit,
tx_gasprice, tx_gasprice,
tx_value tx_value,
tx_input_data
FROM transactions FROM transactions
WHERE tx_to = $1 WHERE tx_to = $1
ORDER BY block_id DESC`, contract.Hash) ORDER BY block_id DESC`, contract.Hash)

View File

@ -191,6 +191,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
to := "1234567890" to := "1234567890"
from := "0987654321" from := "0987654321"
value := int64(10) value := int64(10)
inputData := "0xf7d8c8830000000000000000000000000000000000000000000000000000000000037788000000000000000000000000000000000000000000000000000000000003bd14"
transaction := core.Transaction{ transaction := core.Transaction{
Hash: "x1234", Hash: "x1234",
GasPrice: gasPrice, GasPrice: gasPrice,
@ -199,6 +200,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
To: to, To: to,
From: from, From: from,
Value: value, Value: value,
Data: inputData,
} }
block := core.Block{ block := core.Block{
Number: 123, Number: 123,
@ -210,6 +212,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
savedBlock, _ := repository.FindBlockByNumber(123) savedBlock, _ := repository.FindBlockByNumber(123)
Expect(len(savedBlock.Transactions)).To(Equal(1)) Expect(len(savedBlock.Transactions)).To(Equal(1))
savedTransaction := savedBlock.Transactions[0] savedTransaction := savedBlock.Transactions[0]
Expect(savedTransaction.Data).To(Equal(transaction.Data))
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.From).To(Equal(from))