From 4ba05391798480d0faea58ab1734b8bbdee7cfe7 Mon Sep 17 00:00:00 2001 From: Tudor Malene Date: Thu, 26 Sep 2024 11:17:26 +0100 Subject: [PATCH] improve debug_logs (#2061) --- go/common/tracers/debug_logs.go | 45 +++++++++++++++----------- go/enclave/storage/enclavedb/events.go | 11 +++---- go/enclave/storage/events_storage.go | 15 +++++++-- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/go/common/tracers/debug_logs.go b/go/common/tracers/debug_logs.go index 176a573e6c..ec4814c83d 100644 --- a/go/common/tracers/debug_logs.go +++ b/go/common/tracers/debug_logs.go @@ -11,10 +11,13 @@ import ( // DebugLogs are the logs returned when using the DebugGetLogs endpoint type DebugLogs struct { - RelAddress1 *gethcommon.Address `json:"relAddress1"` - RelAddress2 *gethcommon.Address `json:"relAddress2"` - RelAddress3 *gethcommon.Address `json:"relAddress3"` - LifecycleEvent bool `json:"lifecycleEvent"` + RelAddress1 *gethcommon.Address `json:"relAddress1"` + RelAddress2 *gethcommon.Address `json:"relAddress2"` + RelAddress3 *gethcommon.Address `json:"relAddress3"` + ConfigPublic bool `json:"configPublic"` + AutoPublic bool `json:"autoPublic"` + AutoContract bool `json:"autoContract"` + TransparentContract bool `json:"transparentContract"` gethtypes.Log } @@ -23,19 +26,22 @@ type DebugLogs struct { // this holds a copy of the gethtypes.Log log marshaller func (l DebugLogs) MarshalJSON() ([]byte, error) { return json.Marshal(struct { - Address string `json:"address" gencodec:"required"` - Topics []gethcommon.Hash `json:"topics" gencodec:"required"` - Data hexutil.Bytes `json:"data" gencodec:"required"` - BlockNumber uint64 `json:"blockNumber"` - TxHash gethcommon.Hash `json:"transactionHash" gencodec:"required"` - TxIndex uint `json:"transactionIndex"` - BlockHash gethcommon.Hash `json:"blockHash"` - Index uint `json:"logIndex"` - Removed bool `json:"removed"` - LifecycleEvent bool `json:"lifecycleEvent"` - RelAddress1 *gethcommon.Address `json:"relAddress1"` - RelAddress2 *gethcommon.Address `json:"relAddress2"` - RelAddress3 *gethcommon.Address `json:"relAddress3"` + Address string `json:"address" gencodec:"required"` + Topics []gethcommon.Hash `json:"topics" gencodec:"required"` + Data hexutil.Bytes `json:"data" gencodec:"required"` + BlockNumber uint64 `json:"blockNumber"` + TxHash gethcommon.Hash `json:"transactionHash" gencodec:"required"` + TxIndex uint `json:"transactionIndex"` + BlockHash gethcommon.Hash `json:"blockHash"` + Index uint `json:"logIndex"` + Removed bool `json:"removed"` + ConfigPublic bool `json:"configPublic"` + AutoPublic bool `json:"autoPublic"` + AutoContract bool `json:"autoContract"` + TransparentContract bool `json:"transparentContract"` + RelAddress1 *gethcommon.Address `json:"relAddress1"` + RelAddress2 *gethcommon.Address `json:"relAddress2"` + RelAddress3 *gethcommon.Address `json:"relAddress3"` }{ l.Address.Hex(), l.Topics, @@ -46,7 +52,10 @@ func (l DebugLogs) MarshalJSON() ([]byte, error) { l.BlockHash, l.Index, l.Removed, - l.LifecycleEvent, + l.ConfigPublic, + l.AutoPublic, + l.AutoContract, + l.TransparentContract, l.RelAddress1, l.RelAddress2, l.RelAddress3, diff --git a/go/enclave/storage/enclavedb/events.go b/go/enclave/storage/enclavedb/events.go index a471f8f94f..65b36d002e 100644 --- a/go/enclave/storage/enclavedb/events.go +++ b/go/enclave/storage/enclavedb/events.go @@ -163,7 +163,7 @@ func DebugGetLogs(ctx context.Context, db *sql.DB, txHash common.TxHash) ([]*tra var queryParams []any // todo - should we return the config here? - query := "select eoa1.address, eoa2.address, eoa3.address, et.config_public, et.auto_public, et.event_sig, t1.topic, t2.topic, t3.topic, datablob, b.hash, b.height, tx.hash, tx.idx, log_idx, c.address " + + query := "select eoa1.address, eoa2.address, eoa3.address, et.config_public, et.auto_public, et.event_sig, t1.topic, t2.topic, t3.topic, datablob, b.hash, b.height, tx.hash, tx.idx, log_idx, c.address, c.auto_visibility, c.transparent " + baseEventsJoin + " AND tx.hash = ? " @@ -182,18 +182,16 @@ func DebugGetLogs(ctx context.Context, db *sql.DB, txHash common.TxHash) ([]*tra Log: types.Log{ Topics: []gethcommon.Hash{}, }, - LifecycleEvent: false, } var t0, t1, t2, t3 sql.NullString var relAddress1, relAddress2, relAddress3 []byte - var config_public, autoPublic bool err = rows.Scan( &relAddress1, &relAddress2, &relAddress3, - &config_public, - &autoPublic, + &l.ConfigPublic, + &l.AutoPublic, &t0, &t1, &t2, &t3, &l.Data, &l.BlockHash, @@ -202,8 +200,9 @@ func DebugGetLogs(ctx context.Context, db *sql.DB, txHash common.TxHash) ([]*tra &l.TxIndex, &l.Index, &l.Address, + &l.AutoContract, + &l.TransparentContract, ) - l.LifecycleEvent = config_public || autoPublic if err != nil { return nil, fmt.Errorf("could not load log entry from db: %w", err) } diff --git a/go/enclave/storage/events_storage.go b/go/enclave/storage/events_storage.go index deef851cdb..6dc75ff5c8 100644 --- a/go/enclave/storage/events_storage.go +++ b/go/enclave/storage/events_storage.go @@ -139,6 +139,18 @@ func (es *eventsStorage) storeEventLog(ctx context.Context, dbTX *sql.Tx, execTx return fmt.Errorf("could not write event log. Cause: %w", err) } + // event types that were not configured explicitly can be "Public events" as well. + // based on the topics, this logic determines whether the event type has any relevant addresses + // this is called only the first time an event is emitted + err = es.setAutoVisibilityWhenEventFirstEmitted(ctx, dbTX, eventType, topicIds) + if err != nil { + return fmt.Errorf("could not update the auto visibility. Cause: %w", err) + } + + return nil +} + +func (es *eventsStorage) setAutoVisibilityWhenEventFirstEmitted(ctx context.Context, dbTX *sql.Tx, eventType *enclavedb.EventType, topicIds []*uint64) error { if !eventType.ConfigPublic && eventType.AutoVisibility && eventType.AutoPublic == nil { isPublic := true for _, topicId := range topicIds { @@ -154,12 +166,11 @@ func (es *eventsStorage) storeEventLog(ctx context.Context, dbTX *sql.Tx, execTx } } // for private events with autovisibility, the first time we need to determine whether they are public - err = enclavedb.UpdateEventTypeAutoPublic(ctx, dbTX, eventType.Id, isPublic) + err := enclavedb.UpdateEventTypeAutoPublic(ctx, dbTX, eventType.Id, isPublic) if err != nil { return fmt.Errorf("could not update event type. cause: %w", err) } } - return nil }