Skip to content

Commit

Permalink
Merge pull request #43 from datachainlab/eth_logs
Browse files Browse the repository at this point in the history
implements eth_logs
  • Loading branch information
toshihiko-okubo authored Nov 30, 2021
2 parents 684cf78 + 72bcf46 commit fc41ece
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 76 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ services:
iroha-node0:
container_name: iroha-node0
hostname: iroha-node0
image: ghcr.io/hyperledger/iroha-burrow:1.3.0-rc.2
image: ghcr.io/hyperledger/iroha-burrow:1.3.0
restart: always
environment:
- KEY=node0
- IROHA_POSTGRES_HOST=iroha-node0-postgres
Expand Down
20 changes: 4 additions & 16 deletions iroha-go/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ func CreatedTime(t uint64) PayLoadMetaOption {
}

func (c *commandClient) SendTransaction(ctx context.Context, tx *pb.Transaction) (string, error) {
reqCtx, cancel := context.WithTimeout(
ctx,
c.Timeout,
)
reqCtx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()

if _, err := c.client.Torii(reqCtx, tx, c.callOpts...); err != nil {
Expand All @@ -80,10 +77,7 @@ func (c *commandClient) SendTransaction(ctx context.Context, tx *pb.Transaction)
}

func (c *commandClient) SendBatchTransaction(ctx context.Context, txList *pb.TxList) ([]string, error) {
reqCtx, cancel := context.WithTimeout(
ctx,
c.Timeout,
)
reqCtx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()

if _, err := c.client.ListTorii(reqCtx, txList, c.callOpts...); err != nil {
Expand All @@ -105,10 +99,7 @@ func (c *commandClient) SendBatchTransaction(ctx context.Context, txList *pb.TxL
}

func (c *commandClient) TxStatus(ctx context.Context, txHash string) (*pb.ToriiResponse, error) {
reqCtx, cancel := context.WithTimeout(
ctx,
c.Timeout,
)
reqCtx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()

res, err := c.client.Status(reqCtx, &pb.TxStatusRequest{TxHash: txHash}, c.callOpts...)
Expand All @@ -120,10 +111,7 @@ func (c *commandClient) TxStatus(ctx context.Context, txHash string) (*pb.ToriiR
}

func (c *commandClient) TxStatusStream(ctx context.Context, txHash string) (*pb.ToriiResponse, error) {
reqCtx, cancel := context.WithTimeout(
ctx,
c.Timeout,
)
reqCtx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()

stream, err := c.client.StatusStream(reqCtx, &pb.TxStatusRequest{TxHash: txHash}, c.callOpts...)
Expand Down
7 changes: 2 additions & 5 deletions iroha-go/example/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Batch() {
batchTx1 := command.BuildTransaction(
command.BuildPayload(
[]*pb.Command{
command.CreateAsset(assetID, "test", 2),
command.CreateAsset(assetID, DomainId, 2),
},
command.CreatorAccountId(AdminAccountId),
),
Expand Down Expand Up @@ -55,10 +55,7 @@ func Batch() {
tx.Signatures = sigs
}

txHashList, err := commandClient.SendBatchTransaction(
context.Background(),
txList,
)
txHashList, err := commandClient.SendBatchTransaction(context.Background(), txList)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion iroha-go/example/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Tx() {
tx := command.BuildTransaction(
command.BuildPayload(
[]*pb.Command{
command.CreateAccount(accountID, "test", pubKey.Hex()),
command.CreateAccount(accountID, DomainId, pubKey.Hex()),
},
command.CreatorAccountId(AdminAccountId),
),
Expand Down
12 changes: 0 additions & 12 deletions web3-gateway/acm/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
type DB interface {
Add(account *Account) error
All() ([]*Account, error)
First() (*Account, error)
GetByIrohaAccountID(accountID string) (*Account, error)
GetByIrohaAddress(address string) (*Account, error)
GetByEthereumAddress(address string) (*Account, error)
Expand Down Expand Up @@ -56,17 +55,6 @@ func (m MemDB) All() ([]*Account, error) {
return accounts, nil
}

func (m MemDB) First() (*Account, error) {
// Create read-only transaction
txn := m.db.Txn(false)
raw, err := txn.First(MemDBAccountTable, MemDBAccountIndexId)
if err != nil {
return nil, err
}

return raw.(*Account), nil
}

func (m MemDB) GetByIrohaAccountID(accountID string) (*Account, error) {
txn := m.db.Txn(false)
raw, err := txn.First(MemDBAccountTable, MemDBAccountIndexId, accountID)
Expand Down
3 changes: 2 additions & 1 deletion web3-gateway/iroha/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type DBClient interface {
GetBurrowAccountKeyValueByAddressAndKey(address, key string) (*entity.BurrowAccountKeyValue, error)
GetEngineTransaction(txHash string) (*entity.EngineTransaction, error)
GetEngineReceipt(txHash string) (*entity.EngineReceipt, error)
GeEngineReceiptLogsByTxHash(txHash string) ([]*entity.EngineReceiptLog, error)
GetEngineReceiptLogsByTxHash(txHash string) ([]*entity.EngineReceiptLog, error)
GetEngineReceiptLogsByFilters(opts ...LogFilterOption) ([]*entity.EngineReceiptLog, error)
}

type TxReceiptLogFilter struct {
Expand Down
115 changes: 114 additions & 1 deletion web3-gateway/iroha/db/postgres/postgres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgres

import (
"bytes"
"database/sql"
"fmt"
"strings"
Expand Down Expand Up @@ -151,7 +152,7 @@ WHERE ec.tx_hash = $1
return &receipt, nil
}

func (c *postgresClient) GeEngineReceiptLogsByTxHash(txHash string) ([]*entity.EngineReceiptLog, error) {
func (c *postgresClient) GetEngineReceiptLogsByTxHash(txHash string) ([]*entity.EngineReceiptLog, error) {
txHash = strings.ToLower(x.RemovePrefix(txHash))

var logs []*entity.EngineReceiptLog
Expand Down Expand Up @@ -198,3 +199,115 @@ WHERE btlt.log_idx = $1

return logs, nil
}

func (c *postgresClient) GetEngineReceiptLogsByFilters(opts ...db.LogFilterOption) ([]*entity.EngineReceiptLog, error) {

filter := &db.TxReceiptLogFilter{
FromBlock: 0,
ToBlock: 0,
Address: "",
Topics: nil,
}

for _, opt := range opts {
opt(filter)
}

var conditions bytes.Buffer

clause := func() {
if conditions.Len() == 0 {
conditions.WriteString(" WHERE ")
} else {
conditions.WriteString(" AND ")
}
}

if filter.FromBlock > 0 {
clause()
conditions.WriteString(fmt.Sprintf("height>=%d", filter.FromBlock))
}

if filter.ToBlock > 0 {
clause()
conditions.WriteString(fmt.Sprintf("height<=%d", filter.ToBlock))
}

if len(filter.Address) > 0 {
clause()
address := x.RemovePrefix(filter.Address)
conditions.WriteString(fmt.Sprintf("address IN (LOWER('%s'), UPPER('%s'))", address, address))
}

if len(filter.Topics) > 0 {
clause()
var topicsStr string
for i, topic := range filter.Topics {
sep := ""
if i > 0 {
sep = ","
}
topicsStr = fmt.Sprintf("%s%s'%s'", topicsStr, sep, topic)
}
conditions.WriteString(fmt.Sprintf(`log_idx IN (
SELECT
DISTINCT btl.log_idx
FROM
burrow_tx_logs_topics AS btlt
INNER JOIN
burrow_tx_logs btl
ON
btlt.log_idx = btl.log_idx
WHERE
btlt.topic IN (%s)
)`, topicsStr))
}

queryLogs := fmt.Sprintf(`
SELECT
btl.log_idx as log_idx,
btl.call_id as call_id,
btl.address as address,
btl.data as data,
tp.creator_id,
tp.height as height,
tp.index as index,
tp.hash as tx_hash
FROM
burrow_tx_logs AS btl
INNER JOIN
engine_calls AS ec
ON
btl.call_id = ec.call_id
INNER JOIN
tx_positions AS tp
ON
tp.hash = ec.tx_hash
%s
ORDER BY log_idx`, conditions.String())

var logs []*entity.EngineReceiptLog
if err := c.db.Select(&logs, queryLogs); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}

queryTopics := `
SELECT
btlt.topic
FROM burrow_tx_logs_topics AS btlt
WHERE btlt.log_idx = $1
`

for _, log := range logs {
var topics []entity.EngineReceiptLogTopic
if err := c.db.Select(&topics, queryTopics, log.LogIdx); err != nil {
return nil, err
}
log.Topics = topics
}

return logs, nil
}
62 changes: 29 additions & 33 deletions web3-gateway/iroha/db/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ func TestClient(t *testing.T) {
}

logFilterOpts := []iroha.LogFilterOption{
iroha.FromBlockOption(0),
iroha.ToBlockOption(0),
iroha.AddressOption("7BAF96BBCA5E4469AEEC441A12128BCF719A1FF7"),
iroha.TopicsOption(
"6be3adec13978223a6a787c43ad17abbf22501bfc461466aeeac04a368ad479c",
"981503e0fe5bde28ee3eefdc476eb74e602bbb29f68150d095bfae68a6875178",
),
//iroha.FromBlockOption(0),
//iroha.ToBlockOption(0),
//iroha.AddressOption("708b5cd2603fa4f8fc3f3071598bf2080be16093"),
//iroha.TopicsOption(
// //"981503e0fe5bde28ee3eefdc476eb74e602bbb29f68150d095bfae68a6875178",
//),
}

for _, opt := range logFilterOpts {
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestClient(t *testing.T) {

if len(filter.Address) > 0 {
clause()
conditions.WriteString(fmt.Sprintf("callee='%s'", filter.Address))
conditions.WriteString(fmt.Sprintf("address IN (LOWER('%s'), UPPER('%s'))", filter.Address, filter.Address))
}

if len(filter.Topics) > 0 {
Expand All @@ -82,18 +81,29 @@ func TestClient(t *testing.T) {
}
topicsStr = fmt.Sprintf("%s%s'%s'", topicsStr, sep, topic)
}
conditions.WriteString(fmt.Sprintf("topic IN (%s)", topicsStr))
conditions.WriteString(fmt.Sprintf(`log_idx IN (
SELECT
DISTINCT btl.log_idx
FROM
burrow_tx_logs_topics AS btlt
INNER JOIN
burrow_tx_logs btl
ON
btlt.log_idx = btl.log_idx
WHERE
btlt.topic IN (%s)
)`, topicsStr))
}

query := fmt.Sprintf(`
SELECT
btl.log_idx,
btl.call_id,
btl.address,
btl.data,
btl.log_idx as log_idx,
btl.call_id as call_id,
btl.address as address,
btl.data as data,
tp.creator_id,
tp.height,
tp.index,
tp.height as height,
tp.index as index,
tp.hash as tx_hash
FROM
burrow_tx_logs AS btl
Expand All @@ -105,24 +115,10 @@ INNER JOIN
tx_positions AS tp
ON
tp.hash = ec.tx_hash
WHERE ec.tx_hash IN (
SELECT
DISTINCT(ec.tx_hash)
FROM
burrow_tx_logs_topics AS btlt
INNER JOIN
burrow_tx_logs btl
ON
btlt.log_idx = btl.log_idx
INNER JOIN
engine_calls AS ec
ON
btl.call_id = ec.call_id
INNER JOIN
tx_positions AS tp
ON
tp.hash = ec.tx_hash %s
)`, conditions.String())
%s
ORDER BY log_idx`, conditions.String())

log.Println(query)

var logs []*entity.EngineReceiptLog
if err = ddb.Select(&logs, query); err != nil {
Expand Down
Loading

0 comments on commit fc41ece

Please sign in to comment.