-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
types.go
199 lines (165 loc) · 5.61 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package types
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/lib/pq"
"gopkg.in/guregu/null.v4"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
)
type ChainReaderConfig struct {
// Contracts key is contract name
Contracts map[string]ChainContractReader `json:"contracts" toml:"contracts"`
}
type CodecConfig struct {
// Configs key is the type's name for the codec
Configs map[string]ChainCodecConfig `json:"configs" toml:"configs"`
}
type ChainCodecConfig struct {
TypeABI string `json:"typeAbi" toml:"typeABI"`
ModifierConfigs codec.ModifiersConfig `toml:"modifierConfigs,omitempty"`
}
type ChainContractReader struct {
ContractABI string `json:"contractABI" toml:"contractABI"`
// key is genericName from config
Configs map[string]*ChainReaderDefinition `json:"configs" toml:"configs"`
}
type ChainReaderDefinition chainReaderDefinitionFields
// chainReaderDefinitionFields has the fields for ChainReaderDefinition but no methods.
// This is necessary because package json recognizes the text encoding methods used for TOML,
// and would infinitely recurse on itself.
type chainReaderDefinitionFields struct {
CacheEnabled bool `json:"cacheEnabled,omitempty"`
// chain specific contract method name or event type.
ChainSpecificName string `json:"chainSpecificName"`
ReadType ReadType `json:"readType,omitempty"`
InputModifications codec.ModifiersConfig `json:"inputModifications,omitempty"`
OutputModifications codec.ModifiersConfig `json:"outputModifications,omitempty"`
// EventInputFields allows you to choose which indexed fields are expected from the input
EventInputFields []string `json:"eventInputFields,omitempty"`
}
func (d *ChainReaderDefinition) MarshalText() ([]byte, error) {
var b bytes.Buffer
e := json.NewEncoder(&b)
e.SetIndent("", " ")
if err := e.Encode((*chainReaderDefinitionFields)(d)); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (d *ChainReaderDefinition) UnmarshalText(b []byte) error {
return json.Unmarshal(b, (*chainReaderDefinitionFields)(d))
}
type ReadType int
const (
Method ReadType = iota
Event
)
func (r ReadType) String() string {
switch r {
case Method:
return "method"
case Event:
return "event"
}
return fmt.Sprintf("ReadType(%d)", r)
}
func (r ReadType) MarshalText() ([]byte, error) {
return []byte(r.String()), nil
}
func (r *ReadType) UnmarshalText(text []byte) error {
switch string(text) {
case "method":
*r = Method
return nil
case "event":
*r = Event
return nil
}
return fmt.Errorf("unrecognized ReadType: %s", string(text))
}
type RelayConfig struct {
ChainID *big.Big `json:"chainID"`
FromBlock uint64 `json:"fromBlock"`
EffectiveTransmitterID null.String `json:"effectiveTransmitterID"`
ConfigContractAddress *common.Address `json:"configContractAddress"`
ChainReader *ChainReaderConfig `json:"chainReader"`
Codec *CodecConfig `json:"codec"`
// Contract-specific
SendingKeys pq.StringArray `json:"sendingKeys"`
// Mercury-specific
FeedID *common.Hash `json:"feedID"`
}
var ErrBadRelayConfig = errors.New("bad relay config")
type RelayOpts struct {
// TODO BCF-2508 -- should anyone ever get the raw config bytes that are embedded in args? if not,
// make this private and wrap the arg fields with funcs on RelayOpts
types.RelayArgs
c *RelayConfig
}
func NewRelayOpts(args types.RelayArgs) *RelayOpts {
return &RelayOpts{
RelayArgs: args,
c: nil, // lazy initialization
}
}
func (o *RelayOpts) RelayConfig() (RelayConfig, error) {
var empty RelayConfig
//TODO this should be done once and the error should be cached
if o.c == nil {
var c RelayConfig
err := json.Unmarshal(o.RelayArgs.RelayConfig, &c)
if err != nil {
return empty, fmt.Errorf("%w: failed to deserialize relay config: %w", ErrBadRelayConfig, err)
}
o.c = &c
}
return *o.c, nil
}
type ConfigPoller interface {
ocrtypes.ContractConfigTracker
Start()
Close() error
Replay(ctx context.Context, fromBlock int64) error
}
// TODO(FUN-668): Migrate this fully into types.FunctionsProvider
type FunctionsProvider interface {
types.FunctionsProvider
LogPollerWrapper() LogPollerWrapper
}
type OracleRequest struct {
RequestId [32]byte
RequestingContract common.Address
RequestInitiator common.Address
SubscriptionId uint64
SubscriptionOwner common.Address
Data []byte
DataVersion uint16
Flags [32]byte
CallbackGasLimit uint64
TxHash common.Hash
CoordinatorContract common.Address
OnchainMetadata []byte
}
type OracleResponse struct {
RequestId [32]byte
}
type RouteUpdateSubscriber interface {
UpdateRoutes(ctx context.Context, activeCoordinator common.Address, proposedCoordinator common.Address) error
}
// A LogPoller wrapper that understands router proxy contracts
//
//go:generate mockery --quiet --name LogPollerWrapper --output ./mocks/ --case=underscore
type LogPollerWrapper interface {
services.Service
LatestEvents(ctx context.Context) ([]OracleRequest, []OracleResponse, error)
// TODO (FUN-668): Remove from the LOOP interface and only use internally within the EVM relayer
SubscribeToUpdates(ctx context.Context, name string, subscriber RouteUpdateSubscriber)
}