Skip to content

Commit

Permalink
Log event provider (#9583)
Browse files Browse the repository at this point in the history
* log event provider (wip)

* fixes 2.1 integration

* log event provider + buffer

* log event provider: integration test

* adding a temp copy from ocr2keepers

* increase timeout to stablize test

TODO: figure this out w/o timeouts at all

* use log struct for check data

* move log event provider int test into evm21

* extract 2.1 int tests

* aligned default config

should fix backfill test instability

* fixes and tests

* lint

* extract logprovider package

* extract log packer

* stablize test

* apply lookback buffer after rate limiting

* stablize tests

* log struct packing
  • Loading branch information
amirylm authored Jul 6, 2023
1 parent cff29c0 commit 1ec921f
Show file tree
Hide file tree
Showing 17 changed files with 2,265 additions and 55 deletions.
10 changes: 3 additions & 7 deletions core/services/ocr2/plugins/ocr2keeper/evm21/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func (rp *evmRegistryPackerV2_1) UnpackCheckResult(key ocr2keepers.UpkeepKey, ra
rawPerformData := *abi.ConvertType(out[1], new([]byte)).(*[]byte)
result.FailureReason = *abi.ConvertType(out[2], new(uint8)).(*uint8)
result.GasUsed = *abi.ConvertType(out[3], new(*big.Int)).(**big.Int)
result.FastGasWei = *abi.ConvertType(out[4], new(*big.Int)).(**big.Int)
result.LinkNative = *abi.ConvertType(out[5], new(*big.Int)).(**big.Int)
result.ExecuteGas = *abi.ConvertType(out[4], new(uint32)).(*uint32)
result.FastGasWei = *abi.ConvertType(out[5], new(*big.Int)).(**big.Int)
result.LinkNative = *abi.ConvertType(out[6], new(*big.Int)).(**big.Int)

if !upkeepNeeded {
result.Eligible = false
Expand All @@ -84,11 +85,6 @@ func (rp *evmRegistryPackerV2_1) UnpackCheckResult(key ocr2keepers.UpkeepKey, ra
result.PerformData = rawPerformData
}

// This is a default placeholder which is used since we do not get the execute gas
// from checkUpkeep result. This field is overwritten later from the execute gas
// we have for an upkeep in memory. TODO (AUTO-1482): Refactor this
result.ExecuteGas = 5_000_000

return result, nil
}

Expand Down
8 changes: 4 additions & 4 deletions core/services/ocr2/plugins/ocr2keeper/evm21/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestUnpackTransmitTxInputErrors(t *testing.T) {
abi, err := abi.JSON(strings.NewReader(iregistry21.IKeeperRegistryMasterABI))
assert.Nil(t, err)

packer := &evmRegistryPackerV2_1{abi: abi}
packer := NewEvmRegistryPackerV2_1(abi)
_, err = packer.UnpackTransmitTxInput(hexutil.MustDecode(test.RawData))
assert.NotNil(t, err)
})
Expand All @@ -57,7 +57,7 @@ func TestUnpackPerformResult(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
packer := &evmRegistryPackerV2_1{abi: registryABI}
packer := NewEvmRegistryPackerV2_1(registryABI)
rs, err := packer.UnpackPerformResult(test.RawData)
assert.Nil(t, err)
assert.True(t, rs)
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestUnpackCheckCallbackResult(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
packer := &evmRegistryPackerV2_1{abi: registryABI}
packer := NewEvmRegistryPackerV2_1(registryABI)
needed, pd, failureReason, gasUsed, err := packer.UnpackCheckCallbackResult(test.CallbackResp)

if test.ErrorString != "" {
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestUnpackLogTriggerConfig(t *testing.T) {
},
}

packer := &evmRegistryPackerV2_1{abi: keeperRegistryABI}
packer := NewEvmRegistryPackerV2_1(keeperRegistryABI)

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func setupEVMRegistry(t *testing.T) *EvmRegistry {
registry: mockRegistry,
abi: keeperRegistryABI,
active: make(map[string]activeUpkeep),
packer: &evmRegistryPackerV2_1{abi: keeperRegistryABI},
packer: NewEvmRegistryPackerV2_1(keeperRegistryABI),
headFunc: func(ocr2keepers.BlockKey) {},
chLog: make(chan logpoller.Log, 1000),
mercury: &MercuryConfig{
Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ocr2keeper/evm21/log_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type LogProvider struct {
}

func LogProviderFilterName(addr common.Address) string {
return logpoller.FilterName("OCR2KeeperRegistry - LogProvider", addr)
return logpoller.FilterName("KeepersRegistry LogProvider", addr)
}

func NewLogProvider(
Expand All @@ -60,7 +60,7 @@ func NewLogProvider(
return nil, err
}

abi, err := abi.JSON(strings.NewReader(iregistry21.IKeeperRegistryMasterABI))
keeperABI, err := abi.JSON(strings.NewReader(iregistry21.IKeeperRegistryMasterABI))
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrABINotParsable, err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func NewLogProvider(
lookbackBlocks: lookbackBlocks,
registry: contract,
client: client,
packer: NewEvmRegistryPackerV2_1(abi),
packer: NewEvmRegistryPackerV2_1(keeperABI),
txCheckBlockCache: pluginutils.NewCache[string](time.Hour),
cacheCleaner: pluginutils.NewIntervalCacheCleaner[string](time.Minute),
}, nil
Expand Down
43 changes: 43 additions & 0 deletions core/services/ocr2/plugins/ocr2keeper/evm21/logprovider/abi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package logprovider

import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/i_log_automation"
)

type LogDataPacker interface {
PackLogData(log logpoller.Log) ([]byte, error)
}

type logEventsPacker struct {
abi abi.ABI
}

func NewLogEventsPacker(logDataABI abi.ABI) *logEventsPacker {
return &logEventsPacker{abi: logDataABI}
}

func (p *logEventsPacker) PackLogData(log logpoller.Log) ([]byte, error) {
topics := [][32]byte{}
for _, topic := range log.GetTopics() {
topics = append(topics, topic)
}
b, err := p.abi.Pack("checkLog", &i_log_automation.Log{
Index: big.NewInt(log.LogIndex),
TxIndex: big.NewInt(0), // TODO
TxHash: log.TxHash,
BlockNumber: big.NewInt(log.BlockNumber),
BlockHash: log.BlockHash,
Source: log.Address,
Topics: topics,
Data: log.Data,
})
if err != nil {
return nil, err
}
return b[4:], nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package logprovider

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math/big"

ocr2keepers "github.com/smartcontractkit/ocr2keepers/pkg"
)

// TODO: remove this file, these types should come from ocr2keepers

type ConfiguredUpkeep struct {
// ID uniquely identifies the upkeep
ID ocr2keepers.UpkeepIdentifier
// Type is the event type required to initiate the upkeep
Type int
// Config is configuration data specific to the type
Config interface{}
}

type UpkeepPayload struct {
// ID uniquely identifies the upkeep payload
ID string
// Upkeep is all the information that identifies the upkeep
Upkeep ConfiguredUpkeep
// CheckData is the data used to check the upkeep
CheckData []byte
// Trigger is the event that triggered the upkeep to be checked
Trigger Trigger
}

func NewUpkeepPayload(uid *big.Int, tp int, trigger Trigger, checkData []byte) UpkeepPayload {
p := UpkeepPayload{
Upkeep: ConfiguredUpkeep{
ID: ocr2keepers.UpkeepIdentifier(uid.Bytes()),
Type: tp,
},
Trigger: trigger,
CheckData: checkData,
}
p.ID = p.GenerateID()
return p
}

func (p UpkeepPayload) GenerateID() string {
id := fmt.Sprintf("%s:%s", p.Upkeep.ID, p.Trigger)
idh := sha256.Sum256([]byte(id))
return hex.EncodeToString(idh[:])
}

type Trigger struct {
// BlockNumber is the block number of the corresponding block
BlockNumber int64
// BlockHash is the block hash of the corresponding block
BlockHash string
// Extension is the extensions data that can differ between triggers.
// e.g. for tx hash and log id for log triggers.
Extension interface{}
}

func NewTrigger(blockNumber int64, blockHash string, extension interface{}) Trigger {
return Trigger{
BlockNumber: blockNumber,
BlockHash: blockHash,
Extension: extension,
}
}

func (t Trigger) String() string {
return fmt.Sprintf("%d:%s:%+v", t.BlockNumber, t.BlockHash, t.Extension)
}
Loading

0 comments on commit 1ec921f

Please sign in to comment.