Skip to content

Commit 8a2991d

Browse files
committed
analyzer/runtime: Use events to detect rofl related txs
1 parent 5766b61 commit 8a2991d

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

.changelog/1053.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
analyzer/runtime: Use events to detect rofl related txs

analyzer/runtime/extract.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,18 @@ type TxError struct {
9494
type EventBody interface{}
9595

9696
type EventData struct {
97-
TxIndex *int // nil for non-tx events
98-
TxHash *string // nil for non-tx events
99-
TxEthHash *string // nil for non-evm-tx events
100-
EventIdx int // Unique event index within the block.
101-
Type apiTypes.RuntimeEventType
102-
Body EventBody
103-
WithScope ScopedSdkEvent
104-
EvmLogName *string
105-
EvmLogSignature *ethCommon.Hash
106-
EvmLogParams []*apiTypes.EvmAbiParam
107-
RelatedAddresses map[apiTypes.Address]struct{}
97+
TxIndex *int // nil for non-tx events
98+
TxHash *string // nil for non-tx events
99+
TxEthHash *string // nil for non-evm-tx events
100+
EventIdx int // Unique event index within the block.
101+
Type apiTypes.RuntimeEventType
102+
Body EventBody
103+
WithScope ScopedSdkEvent
104+
EvmLogName *string
105+
EvmLogSignature *ethCommon.Hash
106+
EvmLogParams []*apiTypes.EvmAbiParam
107+
RelatedAddresses map[apiTypes.Address]struct{}
108+
RelatedRoflAddresses map[nodeapi.AppID]struct{}
108109
// EVM logs are emitted by contracts. We usually detect contracts via transactions,
109110
// but in some edge cases, we can only detect them via emitted events.
110111
// This happens when a contract is created via an internal call and only interacted with
@@ -288,7 +289,6 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
288289
blockTransactionData.RawResult = cbor.Marshal(txr.Result)
289290
blockTransactionData.RelatedAccountAddresses = map[apiTypes.Address]struct{}{}
290291
blockTransactionData.RelatedRoflAddresses = map[nodeapi.AppID]struct{}{}
291-
var isRoflCreate bool
292292
tx, err := uncategorized.OpenUtxNoVerify(&txr.Tx, minGasPrice)
293293
if err != nil {
294294
logger.Error("error decoding tx, skipping tx-specific analysis",
@@ -568,13 +568,10 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
568568
},
569569
RoflCreate: func(body *rofl.Create) error {
570570
blockTransactionData.Body = body
571-
// blockTransactionData.RelatedRoflAddresses[] = struct{}{} // We don't have the ID yet here, we need to get it from the event.
572-
isRoflCreate = true
573571
return nil
574572
},
575573
RoflUpdate: func(body *rofl.Update) error {
576574
blockTransactionData.Body = body
577-
blockTransactionData.RelatedRoflAddresses[body.ID] = struct{}{}
578575
admin, err := addresses.FromSdkAddress(body.Admin)
579576
if err != nil {
580577
logger.Warn("failed to convert admin address to native address", "err", err)
@@ -585,14 +582,10 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
585582
},
586583
RoflRemove: func(body *rofl.Remove) error {
587584
blockTransactionData.Body = body
588-
blockTransactionData.RelatedRoflAddresses[body.ID] = struct{}{}
589585
return nil
590586
},
591587
RoflRegister: func(body *rofl.Register) error {
592588
blockTransactionData.Body = body
593-
// RoflRegister transactions are not tracked as "related" to any ROFL address,
594-
// since these will already be tracked as the "instance transactions" of the ROFL instance
595-
// which is registering.
596589
return nil
597590
},
598591
RoflMarketProviderCreate: func(body *roflmarket.ProviderCreate) error {
@@ -701,14 +694,11 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
701694
blockTransactionData.RelatedAccountAddresses[addr] = struct{}{}
702695
}
703696

704-
// For rofl.Create we need to get the App ID from the event so that we
705-
// can mark the transaction related ROFL address.
706-
if isRoflCreate {
707-
for _, event := range extractedEvents {
708-
if event.WithScope.Rofl != nil && event.WithScope.Rofl.AppCreated != nil {
709-
blockTransactionData.RelatedRoflAddresses[event.WithScope.Rofl.AppCreated.ID] = struct{}{}
710-
}
711-
}
697+
// Register related ROFL addresses found in the events for the transaction as well.
698+
// We only track these addresses via events, (not via VisitCall) because this is more
699+
// general as it also tracks subcalls.
700+
for appID := range event.RelatedRoflAddresses {
701+
blockTransactionData.RelatedRoflAddresses[appID] = struct{}{}
712702
}
713703
}
714704
}
@@ -1577,6 +1567,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
15771567
Type: apiTypes.RuntimeEventTypeRoflAppCreated,
15781568
Body: event.AppCreated,
15791569
WithScope: ScopedSdkEvent{Rofl: event},
1570+
RelatedRoflAddresses: map[nodeapi.AppID]struct{}{
1571+
event.AppCreated.ID: {},
1572+
},
15801573
}
15811574
extractedEvents = append(extractedEvents, &eventData)
15821575
}
@@ -1587,6 +1580,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
15871580
Type: apiTypes.RuntimeEventTypeRoflAppRemoved,
15881581
Body: event.AppRemoved,
15891582
WithScope: ScopedSdkEvent{Rofl: event},
1583+
RelatedRoflAddresses: map[nodeapi.AppID]struct{}{
1584+
event.AppRemoved.ID: {},
1585+
},
15901586
}
15911587
extractedEvents = append(extractedEvents, &eventData)
15921588
}
@@ -1597,6 +1593,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
15971593
Type: apiTypes.RuntimeEventTypeRoflAppUpdated,
15981594
Body: event.AppUpdated,
15991595
WithScope: ScopedSdkEvent{Rofl: event},
1596+
RelatedRoflAddresses: map[nodeapi.AppID]struct{}{
1597+
event.AppUpdated.ID: {},
1598+
},
16001599
}
16011600
extractedEvents = append(extractedEvents, &eventData)
16021601
}
@@ -1607,6 +1606,12 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
16071606
Type: apiTypes.RuntimeEventTypeRoflInstanceRegistered,
16081607
Body: event.InstanceRegistered,
16091608
WithScope: ScopedSdkEvent{Rofl: event},
1609+
// RoflRegister transactions are not tracked as "related" to any ROFL address,
1610+
// since these will already be tracked as the "instance transactions" of the ROFL instance
1611+
// which is registering.
1612+
// RelatedRoflAddresses: map[nodeapi.AppID]struct{}{
1613+
// event.InstanceRegistered.AppID: {},
1614+
// },
16101615
}
16111616
extractedEvents = append(extractedEvents, &eventData)
16121617
}

0 commit comments

Comments
 (0)