Skip to content

Commit

Permalink
Merge pull request #11775 from smartcontractkit/rtinianov_eventFields
Browse files Browse the repository at this point in the history
Support indexed events
  • Loading branch information
nolag authored Jan 16, 2024
2 parents 6fdba4f + bce2e3b commit 5403f5e
Show file tree
Hide file tree
Showing 20 changed files with 1,050 additions and 206 deletions.
30 changes: 29 additions & 1 deletion contracts/src/v0.8/shared/test/ChainReaderTestContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct InnerTestStruct {

contract LatestValueHolder {
event Triggered(
int32 field,
int32 indexed field,
string differentField,
uint8 oracleId,
uint8[32] oracleIds,
Expand All @@ -34,6 +34,19 @@ contract LatestValueHolder {
MidLevelTestStruct nestedStruct
);

event TriggeredEventWithDynamicTopic(
string indexed fieldHash,
string field
);


// First topic is event hash
event TriggeredWithFourTopics(
int32 indexed field1,
int32 indexed field2,
int32 indexed field3
);

TestStruct[] private seen;
uint64[] private arr;

Expand Down Expand Up @@ -100,4 +113,19 @@ contract LatestValueHolder {
) public {
emit Triggered(field, differentField, oracleId, oracleIds, account, accounts, bigField, nestedStruct);
}

function TriggerEventWithDynamicTopic(
string calldata field
) public {
emit TriggeredEventWithDynamicTopic(field, field);
}

// first topic is the event signature
function TriggerWithFourTopics(
int32 field1,
int32 field2,
int32 field3
) public {
emit TriggeredWithFourTopics(field1, field2, field3);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ batch_blockhash_store: ../../contracts/solc/v0.8.6/BatchBlockhashStore/BatchBloc
batch_vrf_coordinator_v2: ../../contracts/solc/v0.8.6/BatchVRFCoordinatorV2/BatchVRFCoordinatorV2.abi ../../contracts/solc/v0.8.6/BatchVRFCoordinatorV2/BatchVRFCoordinatorV2.bin d0a54963260d8c1f1bbd984b758285e6027cfb5a7e42701bcb562ab123219332
batch_vrf_coordinator_v2plus: ../../contracts/solc/v0.8.6/BatchVRFCoordinatorV2Plus/BatchVRFCoordinatorV2Plus.abi ../../contracts/solc/v0.8.6/BatchVRFCoordinatorV2Plus/BatchVRFCoordinatorV2Plus.bin 7bb76ae241cf1b37b41920830b836cb99f1ad33efd7435ca2398ff6cd2fe5d48
blockhash_store: ../../contracts/solc/v0.8.6/BlockhashStore/BlockhashStore.abi ../../contracts/solc/v0.8.6/BlockhashStore/BlockhashStore.bin 12b0662f1636a341c8863bdec7a20f2ddd97c3a4fd1a7ae353fe316609face4e
chain_reader_example: ../../contracts/solc/v0.8.19/ChainReaderTestContract/LatestValueHolder.abi ../../contracts/solc/v0.8.19/ChainReaderTestContract/LatestValueHolder.bin 0df0a592be25f8635aa07038d92f03043421fd0dc8855f4750c1c9673d2c613b
chain_reader_example: ../../contracts/solc/v0.8.19/ChainReaderTestContract/LatestValueHolder.abi ../../contracts/solc/v0.8.19/ChainReaderTestContract/LatestValueHolder.bin 77b8d15609d34d553950e3170a86a39943437f63396289935da64c001efafa25
chain_specific_util_helper: ../../contracts/solc/v0.8.6/ChainSpecificUtilHelper/ChainSpecificUtilHelper.abi ../../contracts/solc/v0.8.6/ChainSpecificUtilHelper/ChainSpecificUtilHelper.bin 5f10664e31abc768f4a37901cae7a3bef90146180f97303e5a1bde5a08d84595
consumer_wrapper: ../../contracts/solc/v0.7/Consumer/Consumer.abi ../../contracts/solc/v0.7/Consumer/Consumer.bin 894d1cbd920dccbd36d92918c1037c6ded34f66f417ccb18ec3f33c64ef83ec5
cron_upkeep_factory_wrapper: ../../contracts/solc/v0.8.6/CronUpkeepFactory/CronUpkeepFactory.abi - dacb0f8cdf54ae9d2781c5e720fc314b32ed5e58eddccff512c75d6067292cd7
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chainlink-automation v1.0.1
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240114201223-0d13e4669960
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240115191717-1e2676fced3f
github.com/smartcontractkit/chainlink-vrf v0.0.0-20231120191722-fef03814f868
github.com/smartcontractkit/chainlink/v2 v2.0.0-00010101000000-000000000000
github.com/smartcontractkit/libocr v0.0.0-20231130143053-c5102a9c0fb7
Expand Down Expand Up @@ -244,7 +244,7 @@ require (
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 // indirect
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231222201016-da3f0a763f71 // indirect
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20231204152908-a6e3fe8ff2a1 // indirect
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240113162230-9b808fd915ee // indirect
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240110170252-c27581c17dc1 // indirect
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231222220348-c7d81beaf8fd // indirect
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231221191127-1f32389044ea // indirect
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1164,14 +1164,14 @@ github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 h1:T3lFWumv
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/chainlink-automation v1.0.1 h1:vVjBFq2Zsz21kPy1Pb0wpjF9zrbJX+zjXphDeeR4XZk=
github.com/smartcontractkit/chainlink-automation v1.0.1/go.mod h1:INSchkV3ntyDdlZKGWA030MPDpp6pbeuiRkRKYFCm2k=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240114201223-0d13e4669960 h1:JIWro3+Kx83V5R6RBPwdPffcKDuYlI1stJqjNYJCeQA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240114201223-0d13e4669960/go.mod h1:f+0ei9N4PlTJHu7pbGzEjTnBUr45syPdGFu5+31lS5Q=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240115191717-1e2676fced3f h1:7904h45vNBT+IVO7PMcucvNXSIS9ilf2cxf+RMYb7zs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240115191717-1e2676fced3f/go.mod h1:f+0ei9N4PlTJHu7pbGzEjTnBUr45syPdGFu5+31lS5Q=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231222201016-da3f0a763f71 h1:Ju0cxdrzGFwHGDPp16IzkOyX87LZ/kKDFG1A+VSEJHY=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231222201016-da3f0a763f71/go.mod h1:Ppv5X8MTUkkpKdb270dLefjio724vMkCWmSSaWo7CzI=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20231204152908-a6e3fe8ff2a1 h1:xYqRgZO0nMSO8CBCMR0r3WA+LZ4kNL8a6bnbyk/oBtQ=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20231204152908-a6e3fe8ff2a1/go.mod h1:GuPvyXryvbiUZIHmPeLBz4L+yJKeyGUjrDfd1KNne+o=
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240113162230-9b808fd915ee h1:EC8tcNKx3f6qYln5WD+xVhzz60PKPH+sgrbjzAm3xcw=
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240113162230-9b808fd915ee/go.mod h1:kXeFFq7kA+pmeG/A27wMi3geIRGn7G+r61v787ZyJtU=
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240110170252-c27581c17dc1 h1:eIwwTq9fvxt7z7JUZl06STwa5VrlYsZZbIjLhMeDD2k=
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240110170252-c27581c17dc1/go.mod h1:L2b9/3wYVUqPAeKG/SLG/T0VsMOJtg+ygw8vTmRDMGE=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231222220348-c7d81beaf8fd h1:9xSwDgRJDIfDw6171PQEyn5IQ1JKpaJnG5NX6KfCaHQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231222220348-c7d81beaf8fd/go.mod h1:kY435jBtHbyzhe+ImAxZ6G229uHbB0ablA+A0tJkDn8=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231221191127-1f32389044ea h1:WzMa0O6DEauMYMIjzS/T1JF8zvFDt4aG6EUTDlStaZo=
Expand Down
145 changes: 2 additions & 143 deletions core/services/relay/evm/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,155 +2,14 @@ package evm

import (
"context"
"fmt"
"strings"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"

commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"

evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
)

type readBinding interface {
GetLatestValue(ctx context.Context, params any) ([]byte, error)
GetLatestValue(ctx context.Context, params, returnVal any) error
Bind(binding commontypes.BoundContract) error
SetCodec(codec commontypes.Codec)
SetCodec(codec commontypes.RemoteCodec)
Register() error
Unregister() error
}

type methodBinding struct {
address common.Address
contractName string
method string
client evmclient.Client
codec commontypes.Codec
bound bool
}

var _ readBinding = &methodBinding{}

func (m *methodBinding) SetCodec(codec commontypes.Codec) {
m.codec = codec
}

func (m *methodBinding) Register() error {
return nil
}

func (m *methodBinding) Unregister() error {
return nil
}

func (m *methodBinding) GetLatestValue(ctx context.Context, params any) ([]byte, error) {
if !m.bound {
return nil, fmt.Errorf("%w: method not bound", commontypes.ErrInvalidType)
}

data, err := m.codec.Encode(ctx, params, wrapItemType(m.contractName, m.method, true))
if err != nil {
return nil, err
}

callMsg := ethereum.CallMsg{
To: &m.address,
From: m.address,
Data: data,
}

return m.client.CallContract(ctx, callMsg, nil)
}

func (m *methodBinding) Bind(binding commontypes.BoundContract) error {
m.address = common.HexToAddress(binding.Address)
m.bound = true
return nil
}

type eventBinding struct {
address common.Address
contractName string
eventName string
lp logpoller.LogPoller
hash common.Hash
codec commontypes.Codec
pending bool
subscribed bool
bound bool
}

func (e *eventBinding) SetCodec(codec commontypes.Codec) {
e.codec = codec
}

func (e *eventBinding) Register() error {
e.subscribed = true
if !e.bound {
return nil
}

if err := e.lp.RegisterFilter(logpoller.Filter{
Name: wrapItemType(e.contractName, e.eventName, false),
EventSigs: evmtypes.HashArray{e.hash},
Addresses: evmtypes.AddressArray{e.address},
}); err != nil {
return fmt.Errorf("%w: %w", commontypes.ErrInternal, err)
}
return nil
}

func (e *eventBinding) Unregister() error {
e.subscribed = false
if !e.bound {
return nil
}

if err := e.lp.UnregisterFilter(wrapItemType(e.contractName, e.eventName, false)); err != nil {
return fmt.Errorf("%w: %w", commontypes.ErrInternal, err)
}
return nil
}

var _ readBinding = &eventBinding{}

func (e *eventBinding) GetLatestValue(_ context.Context, _ any) ([]byte, error) {
if !e.bound {
return nil, fmt.Errorf("%w: event not bound", commontypes.ErrInvalidType)
}

confs := logpoller.Finalized
if e.pending {
confs = logpoller.Unconfirmed
}
log, err := e.lp.LatestLogByEventSigWithConfs(e.hash, e.address, confs)
if err != nil {
errStr := err.Error()
if strings.Contains(errStr, "not found") || strings.Contains(errStr, "no rows") {
return nil, fmt.Errorf("%w: %w", commontypes.ErrNotFound, err)
}
return nil, err
}

return log.Data, nil
}

func (e *eventBinding) Bind(binding commontypes.BoundContract) error {
wasSubscribed := e.subscribed
if wasSubscribed {
if err := e.Unregister(); err != nil {
return err
}
}
e.address = common.HexToAddress(binding.Address)
e.pending = binding.Pending
e.bound = true

if wasSubscribed {
return e.Register()
}
return nil
}
Loading

0 comments on commit 5403f5e

Please sign in to comment.