Add topics type

This commit is contained in:
Matt Krump 2018-01-15 14:44:47 -06:00
parent 50f00b80c1
commit 9ee13e715d
8 changed files with 80 additions and 64 deletions

View File

@ -107,8 +107,8 @@ var _ = Describe("Reading contracts", func() {
expectedLogZero := core.Log{ expectedLogZero := core.Log{
BlockNumber: 4703824, BlockNumber: 4703824,
TxHash: "0xf896bfd1eb539d881a1a31102b78de9f25cd591bf1fe1924b86148c0b205fd5d", TxHash: "0xf896bfd1eb539d881a1a31102b78de9f25cd591bf1fe1924b86148c0b205fd5d",
Address: "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07", Address: "0xd26114cd6ee289accf82350c8d8487fedb8a0c07",
Topics: map[int]string{ Topics: core.Topics{
0: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 0: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
1: "0x000000000000000000000000fbb1b73c4f0bda4f67dca266ce6ef42f520fbb98", 1: "0x000000000000000000000000fbb1b73c4f0bda4f67dca266ce6ef42f520fbb98",
2: "0x000000000000000000000000d26114cd6ee289accf82350c8d8487fedb8a0c07", 2: "0x000000000000000000000000d26114cd6ee289accf82350c8d8487fedb8a0c07",

View File

@ -4,7 +4,8 @@ type Log struct {
BlockNumber int64 BlockNumber int64
TxHash string TxHash string
Address string Address string
Topics map[int]string Topics
Index int64 Index int64
Data string Data string
Removed bool
} }

3
pkg/core/topics.go Normal file
View File

@ -0,0 +1,3 @@
package core
type Topics [4]string

View File

@ -57,7 +57,7 @@ func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumbe
if err != nil { if err != nil {
return []core.Log{}, err return []core.Log{}, err
} }
logs := GethLogsToCoreLogs(gethLogs) logs := LogsToCoreLogs(gethLogs)
return logs, nil return logs, nil
} }

View File

@ -4,16 +4,31 @@ import (
"strings" "strings"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"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"
) )
func LogToCoreLog(gethLog types.Log) core.Log { func LogsToCoreLogs(gethLogs []types.Log) []core.Log {
topics := gethLog.Topics var logs []core.Log
var hexTopics = make(map[int]string) 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 { for i, topic := range topics {
hexTopics[i] = topic.Hex() hexTopics[i] = topic.Hex()
} }
return hexTopics
}
func LogToCoreLog(gethLog types.Log) core.Log {
topics := gethLog.Topics
hexTopics := MakeTopics(topics)
return core.Log{ return core.Log{
Address: strings.ToLower(gethLog.Address.Hex()), Address: strings.ToLower(gethLog.Address.Hex()),
@ -22,14 +37,6 @@ func LogToCoreLog(gethLog types.Log) core.Log {
TxHash: gethLog.TxHash.Hex(), TxHash: gethLog.TxHash.Hex(),
Index: int64(gethLog.Index), Index: int64(gethLog.Index),
Data: hexutil.Encode(gethLog.Data), 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
}

View File

@ -27,6 +27,7 @@ var _ = Describe("Conversion of GethLog to core.Log", func() {
common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"), common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"), common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615"),
}, },
Removed: true,
} }
expected := core.Log{ expected := core.Log{
@ -35,10 +36,11 @@ var _ = Describe("Conversion of GethLog to core.Log", func() {
Data: hexutil.Encode(gethLog.Data), Data: hexutil.Encode(gethLog.Data),
TxHash: gethLog.TxHash.Hex(), TxHash: gethLog.TxHash.Hex(),
Index: 2, Index: 2,
Topics: map[int]string{ Topics: core.Topics{
0: common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef").Hex(), 0: common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef").Hex(),
1: common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615").Hex(), 1: common.HexToHash("0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615").Hex(),
}, },
Removed: gethLog.Removed,
} }
coreLog := geth.LogToCoreLog(gethLog) 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.Address).To(Equal(expected.Address))
Expect(coreLog.BlockNumber).To(Equal(expected.BlockNumber)) Expect(coreLog.BlockNumber).To(Equal(expected.BlockNumber))
Expect(coreLog.Data).To(Equal(expected.Data)) Expect(coreLog.Data).To(Equal(expected.Data))
Expect(coreLog.Removed).To(Equal(expected.Removed))
Expect(coreLog.Index).To(Equal(expected.Index)) Expect(coreLog.Index).To(Equal(expected.Index))
Expect(coreLog.Topics[0]).To(Equal(expected.Topics[0])) Expect(coreLog.Topics[0]).To(Equal(expected.Topics[0]))
Expect(coreLog.Topics[1]).To(Equal(expected.Topics[1])) 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) expectedOne := geth.LogToCoreLog(gethLogOne)
expectedTwo := geth.LogToCoreLog(gethLogTwo) 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(len(coreLogs)).To(Equal(2))
Expect(coreLogs[0]).To(Equal(expectedOne)) Expect(coreLogs[0]).To(Equal(expectedOne))

View File

@ -62,35 +62,6 @@ func (repository Postgres) SetBlocksStatus(chainHead int64) {
cutoff) 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 { func (repository Postgres) FindLogs(address string, blockNumber int64) []core.Log {
logRows, _ := repository.Db.Query( logRows, _ := repository.Db.Query(
`SELECT block_number, `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 { func (repository Postgres) createReceipt(tx *sql.Tx, transactionId int, receipt core.Receipt) error {
//Not currently persisting log bloom filters //Not currently persisting log bloom filters
_, err := tx.Exec( var receiptId int
err := tx.QueryRow(
`INSERT INTO receipts `INSERT INTO receipts
(contract_address, tx_hash, cumulative_gas_used, gas_used, state_root, status, transaction_id) (contract_address, tx_hash, cumulative_gas_used, gas_used, state_root, status, transaction_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)`, VALUES ($1, $2, $3, $4, $5, $6, $7)
receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, transactionId) RETURNING id`,
receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, transactionId).Scan(&receiptId)
if err != nil { if err != nil {
return err return err
} }
return nil 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) { func loadReceipt(receiptsRow *sql.Row) (core.Receipt, error) {
var contractAddress string var contractAddress string
var txHash string var txHash string
@ -445,7 +447,7 @@ func (repository Postgres) loadLogs(logsRows *sql.Rows) []core.Log {
var txHash string var txHash string
var index int64 var index int64
var data string 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) logsRows.Scan(&blockNumber, &address, &txHash, &index, &topics[0], &topics[1], &topics[2], &topics[3], &data)
log := core.Log{ log := core.Log{
BlockNumber: blockNumber, BlockNumber: blockNumber,
@ -454,7 +456,6 @@ func (repository Postgres) loadLogs(logsRows *sql.Rows) []core.Log {
Index: index, Index: index,
Data: data, Data: data,
} }
log.Topics = make(map[int]string)
for i, topic := range topics { for i, topic := range topics {
log.Topics[i] = topic log.Topics[i] = topic
} }

View File

@ -399,7 +399,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 0, Index: 0,
Address: "x123", Address: "x123",
TxHash: "x456", TxHash: "x456",
Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, Topics: [4]string{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, }},
) )
@ -428,7 +428,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 0, Index: 0,
Address: "x123", Address: "x123",
TxHash: "x456", TxHash: "x456",
Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, Topics: [4]string{0: "x777", 1: "x888", 2: "x999"},
Data: "xABC", Data: "xABC",
}, },
}) })
@ -437,7 +437,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 0, Index: 0,
Address: "x123", Address: "x123",
TxHash: "x456", TxHash: "x456",
Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, Topics: [4]string{0: "x777", 1: "x888", 2: "x999"},
Data: "xXYZ", Data: "xXYZ",
}, },
}) })
@ -452,7 +452,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 0, Index: 0,
Address: "x123", Address: "x123",
TxHash: "x456", TxHash: "x456",
Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, Topics: [4]string{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, }},
) )
@ -461,7 +461,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 1, Index: 1,
Address: "x123", Address: "x123",
TxHash: "x789", TxHash: "x789",
Topics: map[int]string{0: "x111", 1: "x222", 2: "x333"}, Topics: [4]string{0: "x111", 1: "x222", 2: "x333"},
Data: "xdef", Data: "xdef",
}}, }},
) )
@ -470,7 +470,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Index: 0, Index: 0,
Address: "x123", Address: "x123",
TxHash: "x456", TxHash: "x456",
Topics: map[int]string{0: "x777", 1: "x888", 2: "x999"}, Topics: [4]string{0: "x777", 1: "x888", 2: "x999"},
Data: "xabc", Data: "xabc",
}}, }},
) )
@ -511,7 +511,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
BlockNumber: 4745407, BlockNumber: 4745407,
Data: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000645a68669900000000000000000000000000000000000000000000003397684ab5869b0000000000000000000000000000000000000000000000000000000000005a36053200000000000000000000000099041f808d598b782d5a3e498681c2452a31da08", Data: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000645a68669900000000000000000000000000000000000000000000003397684ab5869b0000000000000000000000000000000000000000000000000000000000005a36053200000000000000000000000099041f808d598b782d5a3e498681c2452a31da08",
Index: 86, Index: 86,
Topics: map[int]string{ Topics: core.Topics{
0: "0x5a68669900000000000000000000000000000000000000000000000000000000", 0: "0x5a68669900000000000000000000000000000000000000000000000000000000",
1: "0x000000000000000000000000d0148dad63f73ce6f1b6c607e3413dcf1ff5f030", 1: "0x000000000000000000000000d0148dad63f73ce6f1b6c607e3413dcf1ff5f030",
2: "0x00000000000000000000000000000000000000000000003397684ab5869b0000", 2: "0x00000000000000000000000000000000000000000000003397684ab5869b0000",
@ -523,7 +523,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
BlockNumber: 4745407, BlockNumber: 4745407,
Data: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000418178358", Data: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000418178358",
Index: 87, Index: 87,
Topics: map[int]string{ Topics: core.Topics{
0: "0x1817835800000000000000000000000000000000000000000000000000000000", 0: "0x1817835800000000000000000000000000000000000000000000000000000000",
1: "0x0000000000000000000000008a4774fe82c63484afef97ca8d89a6ea5e21f973", 1: "0x0000000000000000000000008a4774fe82c63484afef97ca8d89a6ea5e21f973",
2: "0x0000000000000000000000000000000000000000000000000000000000000000", 2: "0x0000000000000000000000000000000000000000000000000000000000000000",
@ -535,7 +535,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
BlockNumber: 4745407, BlockNumber: 4745407,
Data: "0x00000000000000000000000000000000000000000000003338f64c8423af4000", Data: "0x00000000000000000000000000000000000000000000003338f64c8423af4000",
Index: 88, Index: 88,
Topics: map[int]string{ Topics: core.Topics{
0: "0x296ba4ca62c6c21c95e828080cb8aec7481b71390585605300a8a76f9e95b527", 0: "0x296ba4ca62c6c21c95e828080cb8aec7481b71390585605300a8a76f9e95b527",
}, },
TxHash: "0x002c4799161d809b23f67884eb6598c9df5894929fe1a9ead97ca175d360f547", TxHash: "0x002c4799161d809b23f67884eb6598c9df5894929fe1a9ead97ca175d360f547",
@ -557,7 +557,8 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
} }
block := core.Block{Transactions: []core.Transaction{transaction}} block := core.Block{Transactions: []core.Transaction{transaction}}
repository.CreateOrUpdateBlock(block) err := repository.CreateOrUpdateBlock(block)
Expect(err).To(Not(HaveOccurred()))
retrievedLogs := repository.FindLogs("0x99041f808d598b782d5a3e498681c2452a31da08", 4745407) retrievedLogs := repository.FindLogs("0x99041f808d598b782d5a3e498681c2452a31da08", 4745407)
Expect(retrievedLogs).To(Not(BeZero())) Expect(retrievedLogs).To(Not(BeZero()))