Skip to content

Commit

Permalink
correct issues where log events would not run correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Nov 1, 2023
1 parent 0a8a950 commit 094a966
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
91 changes: 91 additions & 0 deletions tools/simulator/plans/only_log_trigger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"node": {
"totalNodeCount": 4,
"maxNodeServiceWorkers": 100,
"maxNodeServiceQueueSize": 1000
},
"p2pNetwork": {
"avgLatency": "100ms"
},
"rpc": {
"maxBlockDelay": 600,
"averageLatency": 300,
"errorRate": 0.02,
"rateLimitThreshold": 1000
},
"blocks": {
"genesisBlock": 128943862,
"blockCadence": "1s",
"durationInBlocks": 60,
"endPadding": 20
},
"events": [
{
"type": "ocr3config",
"triggerBlockNumber": 128943863,
"comment": "initial ocr config (valid)",
"maxFaultyNodes": 1,
"encodedOffchainConfig": "{\"version\":\"v3\",\"performLockoutWindow\":100000,\"targetProbability\":\"0.999\",\"targetInRounds\":4,\"minConfirmations\":1,\"gasLimitPerReport\":1000000,\"gasOverheadPerUpkeep\":300000,\"maxUpkeepBatchSize\":10}",
"maxRoundsPerEpoch": 7,
"deltaProgress": "10s",
"deltaResend": "10s",
"deltaInitial": "300ms",
"deltaRound": "1100ms",
"deltaGrace": "300ms",
"deltaCertifiedCommitRequest": "200ms",
"deltaStage": "20s",
"maxQueryTime": "50ms",
"maxObservationTime": "100ms",
"maxShouldAcceptTime": "50ms",
"maxShouldTransmitTime": "50ms"
},
{
"type": "generateUpkeeps",
"triggerBlockNumber": 128943862,
"comment": "single log triggered upkeep",
"count": 1,
"startID": 300,
"eligibilityFunc": "always",
"upkeepType": "logTrigger",
"logTriggeredBy": "test_trigger_event"
},
{
"type": "generateUpkeeps",
"triggerBlockNumber": 128943864,
"comment": "5 log triggered upkeeps",
"count": 5,
"startID": 400,
"eligibilityFunc": "always",
"upkeepType": "logTrigger",
"logTriggeredBy": "test_trigger_event"
},
{
"type": "logTrigger",
"triggerBlockNumber": 128943872,
"comment": "trigger 10 blocks after trigger upkeep created",
"triggerValue": "test_trigger_event"
},
{
"type": "generateUpkeeps",
"triggerBlockNumber": 128943878,
"comment": "7 log triggered upkeeps",
"count": 7,
"startID": 500,
"eligibilityFunc": "always",
"upkeepType": "logTrigger",
"logTriggeredBy": "test_trigger_event"
},
{
"type": "logTrigger",
"triggerBlockNumber": 128943882,
"comment": "trigger 10 blocks after trigger upkeep created",
"triggerValue": "test_trigger_event"
},
{
"type": "logTrigger",
"triggerBlockNumber": 128943892,
"comment": "trigger 10 blocks after trigger upkeep created",
"triggerValue": "test_trigger_event"
}
]
}
2 changes: 2 additions & 0 deletions tools/simulator/simulate/chain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func generateBasicSimulatedUpkeeps(event config.GenerateUpkeepEvent, alwaysEligi
UpkeepID: newUpkeepID(id.Bytes(), pluginTriggerType),
AlwaysEligible: alwaysEligible,
EligibleAt: make([]*big.Int, 0),
TriggeredBy: event.LogTriggeredBy,
}

generated = append(generated, simulated)
Expand Down Expand Up @@ -110,6 +111,7 @@ func generateEligibilityFuncSimulatedUpkeeps(event config.GenerateUpkeepEvent, s
UpkeepID: newUpkeepID(id.Bytes(), pluginTriggerType),
AlwaysEligible: false,
EligibleAt: make([]*big.Int, 0),
TriggeredBy: event.LogTriggeredBy,
}

var genesis *big.Int
Expand Down
17 changes: 13 additions & 4 deletions tools/simulator/simulate/loader/logtrigger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package loader

import (
"crypto/sha256"
"sync"
"time"

"github.com/smartcontractkit/ocr2keepers/tools/simulator/config"
"github.com/smartcontractkit/ocr2keepers/tools/simulator/simulate/chain"
Expand All @@ -14,7 +16,7 @@ type LogTriggerLoader struct {

// internal state values
mu sync.RWMutex
triggers map[string][]interface{}
triggers map[string][]chain.Log
}

// NewLogTriggerLoader ...
Expand All @@ -24,13 +26,13 @@ func NewLogTriggerLoader(plan config.SimulationPlan, progress ProgressTelemetry)
return nil, err
}

events := make(map[string][]interface{})
events := make(map[string][]chain.Log)
for _, logEvt := range logs {
trigger := logEvt.TriggerAt

existing, ok := events[trigger.String()]
if !ok {
existing = []interface{}{}
existing = []chain.Log{}
}

events[trigger.String()] = append(existing, chain.Log{
Expand All @@ -57,7 +59,14 @@ func (ltl *LogTriggerLoader) Load(block *chain.Block) {
defer ltl.mu.RUnlock()

if events, ok := ltl.triggers[block.Number.String()]; ok {
block.Transactions = append(block.Transactions, events...)
for _, event := range events {
event.BlockNumber = block.Number
event.BlockHash = block.Hash
event.Idx = uint32(len(block.Transactions))
event.TxHash = sha256.Sum256([]byte(time.Now().Format(time.RFC3339Nano)))

block.Transactions = append(block.Transactions, event)
}

if ltl.progress != nil {
ltl.progress.Increment(emitLogNamespace, int64(len(events)))
Expand Down
7 changes: 4 additions & 3 deletions tools/simulator/simulate/loader/ocr3transmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ func calculateExpectedPerformEvents(plan config.SimulationPlan) (int64, error) {
}

for _, upkeep := range upkeeps {
if upkeep.Type == chain.ConditionalType {
switch upkeep.Type {
case chain.ConditionalType:
count += int64(len(upkeep.EligibleAt))
} else if upkeep.Type == chain.LogTriggerType {
case chain.LogTriggerType:
for _, log := range logs {
if logTriggersUpkeep(log, upkeep) {
count++
Expand All @@ -162,7 +163,7 @@ func calculateExpectedPerformEvents(plan config.SimulationPlan) (int64, error) {
}

func logTriggersUpkeep(log chain.SimulatedLog, upkeep chain.SimulatedUpkeep) bool {
if log.TriggerValue == upkeep.TriggeredBy {
if log.TriggerAt.Cmp(upkeep.CreateInBlock) >= 0 && log.TriggerValue == upkeep.TriggeredBy {
if upkeep.AlwaysEligible {
return true
} else {
Expand Down
10 changes: 6 additions & 4 deletions tools/simulator/simulate/loader/upkeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type UpkeepConfigLoader struct {

// internal state values
mu sync.RWMutex
create map[string][]interface{}
create map[string][]chain.UpkeepCreatedTransaction
}

// NewUpkeepConfigLoader ...
Expand All @@ -32,11 +32,11 @@ func NewUpkeepConfigLoader(plan config.SimulationPlan, progress ProgressTelemetr
return nil, err
}

create := make(map[string][]interface{})
create := make(map[string][]chain.UpkeepCreatedTransaction)
for _, upkeep := range allUpkeeps {
evts, ok := create[upkeep.CreateInBlock.String()]
if !ok {
evts = []interface{}{}
evts = []chain.UpkeepCreatedTransaction{}
}

create[upkeep.CreateInBlock.String()] = append(evts, chain.UpkeepCreatedTransaction{
Expand All @@ -63,7 +63,9 @@ func (ucl *UpkeepConfigLoader) Load(block *chain.Block) {
defer ucl.mu.RUnlock()

if events, ok := ucl.create[block.Number.String()]; ok {
block.Transactions = append(block.Transactions, events...)
for _, event := range events {
block.Transactions = append(block.Transactions, event)
}

if ucl.progress != nil {
ucl.progress.Increment(CreateUpkeepNamespace, int64(len(events)))
Expand Down

0 comments on commit 094a966

Please sign in to comment.