From 9ee13e715dbe2b427032b5ce128ccd621a0fa647 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Mon, 15 Jan 2018 14:44:47 -0600 Subject: [PATCH] Add topics type --- integration_test/contract_test.go | 4 +- pkg/core/log.go | 7 +-- pkg/core/topics.go | 3 ++ pkg/geth/blockchain.go | 2 +- pkg/geth/log_to_core_log.go | 31 ++++++++----- pkg/geth/log_to_core_log_test.go | 7 ++- pkg/repositories/postgres.go | 69 +++++++++++++++-------------- pkg/repositories/testing/helpers.go | 21 ++++----- 8 files changed, 80 insertions(+), 64 deletions(-) create mode 100644 pkg/core/topics.go diff --git a/integration_test/contract_test.go b/integration_test/contract_test.go index 6d0af83f..33784973 100644 --- a/integration_test/contract_test.go +++ b/integration_test/contract_test.go @@ -107,8 +107,8 @@ var _ = Describe("Reading contracts", func() { expectedLogZero := core.Log{ BlockNumber: 4703824, TxHash: "0xf896bfd1eb539d881a1a31102b78de9f25cd591bf1fe1924b86148c0b205fd5d", - Address: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07", - Topics: map[int]string{ + Address: "0xd26114cd6ee289accf82350c8d8487fedb8a0c07", + Topics: core.Topics{ 0: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 1: "0x000000000000000000000000fbb1b73c4f0bda4f67dca266ce6ef42f520fbb98", 2: "0x000000000000000000000000d26114cd6ee289accf82350c8d8487fedb8a0c07", diff --git a/pkg/core/log.go b/pkg/core/log.go index 9688b46c..c96817f9 100644 --- a/pkg/core/log.go +++ b/pkg/core/log.go @@ -4,7 +4,8 @@ type Log struct { BlockNumber int64 TxHash string Address string - Topics map[int]string - Index int64 - Data string + Topics + Index int64 + Data string + Removed bool } diff --git a/pkg/core/topics.go b/pkg/core/topics.go new file mode 100644 index 00000000..2f1acb9d --- /dev/null +++ b/pkg/core/topics.go @@ -0,0 +1,3 @@ +package core + +type Topics [4]string diff --git a/pkg/geth/blockchain.go b/pkg/geth/blockchain.go index c519706f..aad367d3 100644 --- a/pkg/geth/blockchain.go +++ b/pkg/geth/blockchain.go @@ -57,7 +57,7 @@ func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumbe if err != nil { return []core.Log{}, err } - logs := GethLogsToCoreLogs(gethLogs) + logs := LogsToCoreLogs(gethLogs) return logs, nil } diff --git a/pkg/geth/log_to_core_log.go b/pkg/geth/log_to_core_log.go index bce6fd4c..f3bc3534 100644 --- a/pkg/geth/log_to_core_log.go +++ b/pkg/geth/log_to_core_log.go @@ -4,16 +4,31 @@ import ( "strings" "github.com/8thlight/vulcanizedb/pkg/core" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" ) -func LogToCoreLog(gethLog types.Log) core.Log { - topics := gethLog.Topics - var hexTopics = make(map[int]string) +func LogsToCoreLogs(gethLogs []types.Log) []core.Log { + var logs []core.Log + for _, log := range gethLogs { + log := LogToCoreLog(log) + logs = append(logs, log) + } + return logs +} + +func MakeTopics(topics []common.Hash) core.Topics { + var hexTopics core.Topics for i, topic := range topics { hexTopics[i] = topic.Hex() } + return hexTopics +} + +func LogToCoreLog(gethLog types.Log) core.Log { + topics := gethLog.Topics + hexTopics := MakeTopics(topics) return core.Log{ Address: strings.ToLower(gethLog.Address.Hex()), @@ -22,14 +37,6 @@ func LogToCoreLog(gethLog types.Log) core.Log { TxHash: gethLog.TxHash.Hex(), Index: int64(gethLog.Index), Data: hexutil.Encode(gethLog.Data), + Removed: gethLog.Removed, } } - -func GethLogsToCoreLogs(gethLogs []types.Log) []core.Log { - var logs []core.Log - for _, log := range gethLogs { - log := LogToCoreLog(log) - logs = append(logs, log) - } - return logs -} diff --git a/pkg/geth/log_to_core_log_test.go b/pkg/geth/log_to_core_log_test.go index ad15e826..a470b91c 100644 --- a/pkg/geth/log_to_core_log_test.go +++ b/pkg/geth/log_to_core_log_test.go @@ -27,6 +27,7 @@ var _ = Describe("Conversion of GethLog to core.Log", func() { common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"), }, + Removed: true, } expected := core.Log{ @@ -35,10 +36,11 @@ var _ = Describe("Conversion of GethLog to core.Log", func() { Data: hexutil.Encode(gethLog.Data), TxHash: gethLog.TxHash.Hex(), Index: 2, - Topics: map[int]string{ + Topics: core.Topics{ 0: common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef").Hex(), 1: common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615").Hex(), }, + Removed: gethLog.Removed, } coreLog := geth.LogToCoreLog(gethLog) @@ -46,6 +48,7 @@ var _ = Describe("Conversion of GethLog to core.Log", func() { Expect(coreLog.Address).To(Equal(expected.Address)) Expect(coreLog.BlockNumber).To(Equal(expected.BlockNumber)) Expect(coreLog.Data).To(Equal(expected.Data)) + Expect(coreLog.Removed).To(Equal(expected.Removed)) Expect(coreLog.Index).To(Equal(expected.Index)) Expect(coreLog.Topics[0]).To(Equal(expected.Topics[0])) Expect(coreLog.Topics[1]).To(Equal(expected.Topics[1])) @@ -84,7 +87,7 @@ var _ = Describe("Conversion of GethLog to core.Log", func() { expectedOne := geth.LogToCoreLog(gethLogOne) expectedTwo := geth.LogToCoreLog(gethLogTwo) - coreLogs := geth.GethLogsToCoreLogs([]types.Log{gethLogOne, gethLogTwo}) + coreLogs := geth.LogsToCoreLogs([]types.Log{gethLogOne, gethLogTwo}) Expect(len(coreLogs)).To(Equal(2)) Expect(coreLogs[0]).To(Equal(expectedOne)) diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index d7d82402..5461fe53 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -62,35 +62,6 @@ func (repository Postgres) SetBlocksStatus(chainHead int64) { cutoff) } -func (repository Postgres) CreateLogs(logs []core.Log) error { - tx, _ := repository.Db.BeginTx(context.Background(), nil) - for _, tlog := range logs { - _, err := tx.Exec( - `INSERT INTO logs (block_number, address, tx_hash, index, topic0, topic1, topic2, topic3, data) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) - ON CONFLICT (index, block_number) - DO UPDATE - SET block_number = $1, - address = $2, - tx_hash = $3, - index = $4, - topic0 = $5, - topic1 = $6, - topic2 = $7, - topic3 = $8, - data = $9 - `, - tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, - ) - if err != nil { - tx.Rollback() - return ErrDBInsertFailed - } - } - tx.Commit() - return nil -} - func (repository Postgres) FindLogs(address string, blockNumber int64) []core.Log { logRows, _ := repository.Db.Query( `SELECT block_number, @@ -353,17 +324,48 @@ func (repository Postgres) createTransaction(tx *sql.Tx, blockId int64, transact func (repository Postgres) createReceipt(tx *sql.Tx, transactionId int, receipt core.Receipt) error { //Not currently persisting log bloom filters - _, err := tx.Exec( + var receiptId int + err := tx.QueryRow( `INSERT INTO receipts (contract_address, tx_hash, cumulative_gas_used, gas_used, state_root, status, transaction_id) - VALUES ($1, $2, $3, $4, $5, $6, $7)`, - receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, transactionId) + VALUES ($1, $2, $3, $4, $5, $6, $7) + RETURNING id`, + receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, transactionId).Scan(&receiptId) if err != nil { return err } return nil } +func (repository Postgres) CreateLogs(logs []core.Log) error { + tx, _ := repository.Db.BeginTx(context.Background(), nil) + for _, tlog := range logs { + _, err := tx.Exec( + `INSERT INTO logs (block_number, address, tx_hash, index, topic0, topic1, topic2, topic3, data) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + ON CONFLICT (index, block_number) + DO UPDATE + SET block_number = $1, + address = $2, + tx_hash = $3, + index = $4, + topic0 = $5, + topic1 = $6, + topic2 = $7, + topic3 = $8, + data = $9 + `, + tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, + ) + if err != nil { + tx.Rollback() + return ErrDBInsertFailed + } + } + tx.Commit() + return nil +} + func loadReceipt(receiptsRow *sql.Row) (core.Receipt, error) { var contractAddress string var txHash string @@ -445,7 +447,7 @@ func (repository Postgres) loadLogs(logsRows *sql.Rows) []core.Log { var txHash string var index int64 var data string - topics := make([]string, 4) + var topics core.Topics logsRows.Scan(&blockNumber, &address, &txHash, &index, &topics[0], &topics[1], &topics[2], &topics[3], &data) log := core.Log{ BlockNumber: blockNumber, @@ -454,7 +456,6 @@ func (repository Postgres) loadLogs(logsRows *sql.Rows) []core.Log { Index: index, Data: data, } - log.Topics = make(map[int]string) for i, topic := range topics { log.Topics[i] = topic } diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index 2ce90b3d..251d043d 100644 --- a/pkg/repositories/testing/helpers.go +++ b/pkg/repositories/testing/helpers.go @@ -399,7 +399,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 0, Address: "x123", TxHash: "x456", - Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, + Topics: [4]string{0: "x777", 1: "x888", 2: "x999"}, Data: "xabc", }}, ) @@ -428,7 +428,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 0, Address: "x123", TxHash: "x456", - Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, + Topics: [4]string{0: "x777", 1: "x888", 2: "x999"}, Data: "xABC", }, }) @@ -437,7 +437,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 0, Address: "x123", TxHash: "x456", - Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, + Topics: [4]string{0: "x777", 1: "x888", 2: "x999"}, Data: "xXYZ", }, }) @@ -452,7 +452,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 0, Address: "x123", TxHash: "x456", - Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, + Topics: [4]string{0: "x777", 1: "x888", 2: "x999"}, Data: "xabc", }}, ) @@ -461,7 +461,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 1, Address: "x123", TxHash: "x789", - Topics: map[int]string{0: "x111", 1: "x222", 2: "x333"}, + Topics: [4]string{0: "x111", 1: "x222", 2: "x333"}, Data: "xdef", }}, ) @@ -470,7 +470,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Index: 0, Address: "x123", TxHash: "x456", - Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, + Topics: [4]string{0: "x777", 1: "x888", 2: "x999"}, Data: "xabc", }}, ) @@ -511,7 +511,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. BlockNumber: 4745407, Data: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000645a68669900000000000000000000000000000000000000000000003397684ab5869b0000000000000000000000000000000000000000000000000000000000005a36053200000000000000000000000099041f808d598b782d5a3e498681c2452a31da08", Index: 86, - Topics: map[int]string{ + Topics: core.Topics{ 0: "0x5a68669900000000000000000000000000000000000000000000000000000000", 1: "0x000000000000000000000000d0148dad63f73ce6f1b6c607e3413dcf1ff5f030", 2: "0x00000000000000000000000000000000000000000000003397684ab5869b0000", @@ -523,7 +523,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. BlockNumber: 4745407, Data: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000418178358", Index: 87, - Topics: map[int]string{ + Topics: core.Topics{ 0: "0x1817835800000000000000000000000000000000000000000000000000000000", 1: "0x0000000000000000000000008a4774fe82c63484afef97ca8d89a6ea5e21f973", 2: "0x0000000000000000000000000000000000000000000000000000000000000000", @@ -535,7 +535,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. BlockNumber: 4745407, Data: "0x00000000000000000000000000000000000000000000003338f64c8423af4000", Index: 88, - Topics: map[int]string{ + Topics: core.Topics{ 0: "0x296ba4ca62c6c21c95e828080cb8aec7481b71390585605300a8a76f9e95b527", }, TxHash: "0x002c4799161d809b23f67884eb6598c9df5894929fe1a9ead97ca175d360f547", @@ -557,7 +557,8 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. } block := core.Block{Transactions: []core.Transaction{transaction}} - repository.CreateOrUpdateBlock(block) + err := repository.CreateOrUpdateBlock(block) + Expect(err).To(Not(HaveOccurred())) retrievedLogs := repository.FindLogs("0x99041f808d598b782d5a3e498681c2452a31da08", 4745407) Expect(retrievedLogs).To(Not(BeZero()))