Skip to content

Commit

Permalink
Scope native and checked type conversion to a single struct and add t…
Browse files Browse the repository at this point in the history
…ests to verify that NewAt is safe and fuzz tests for sanity around it too
  • Loading branch information
nolag committed Jan 16, 2024
1 parent 5403f5e commit e1f837f
Show file tree
Hide file tree
Showing 25 changed files with 797 additions and 589 deletions.
2 changes: 1 addition & 1 deletion 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.20240115191717-1e2676fced3f
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240116201354-23cd46ccbbe5
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
4 changes: 2 additions & 2 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,8 @@ 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.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-common v0.1.7-0.20240116201354-23cd46ccbbe5 h1:o36okzBAbWhlxKrIeb66Iw/U7Uy3j4FsPGQREb06SUo=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240116201354-23cd46ccbbe5/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=
Expand Down
30 changes: 14 additions & 16 deletions core/services/relay/evm/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewChainReaderService(lggr logger.Logger, lp logpoller.LogPoller, chain leg
lp: lp,
client: chain.Client(),
contractBindings: contractBindings{},
parsed: &parsedTypes{encoderDefs: map[string]*codecEntry{}, decoderDefs: map[string]*codecEntry{}},
parsed: &parsedTypes{encoderDefs: map[string]types.CodecEntry{}, decoderDefs: map[string]types.CodecEntry{}},
}

var err error
Expand Down Expand Up @@ -208,15 +208,15 @@ func (cr *chainReader) addEvent(contractName, eventName string, a abi.ABI, chain
}

func (cr *chainReader) getEventInput(def types.ChainReaderDefinition, contractName, eventName string) (
*codecEntry, codec.Modifier, error) {
types.CodecEntry, codec.Modifier, error) {
inputInfo := cr.parsed.encoderDefs[wrapItemType(contractName, eventName, true)]
inMod, err := def.InputModifications.ToModifier(evmDecoderHooks...)
if err != nil {
return nil, nil, err
}

// initialize the modification
if _, err = inMod.RetypeForOffChain(reflect.PointerTo(inputInfo.checkedType), ""); err != nil {
if _, err = inMod.RetypeForOffChain(reflect.PointerTo(inputInfo.CheckedType()), ""); err != nil {
return nil, nil, err
}

Expand All @@ -234,41 +234,39 @@ func verifyEventInputsUsed(chainReaderDefinition types.ChainReaderDefinition, in

func (cr *chainReader) addEncoderDef(contractName, methodName string, args abi.Arguments, prefix []byte, chainReaderDefinition types.ChainReaderDefinition) error {
// ABI.Pack prepends the method.ID to the encodings, we'll need the encoder to do the same.
input := &codecEntry{Args: args, encodingPrefix: prefix}

if err := input.Init(); err != nil {
inputMod, err := chainReaderDefinition.InputModifications.ToModifier(evmDecoderHooks...)
if err != nil {
return err
}
input := types.NewCodecEntry(args, prefix, inputMod)

inputMod, err := chainReaderDefinition.InputModifications.ToModifier(evmDecoderHooks...)
if err != nil {
if err := input.Init(); err != nil {
return err
}
input.mod = inputMod

cr.parsed.encoderDefs[wrapItemType(contractName, methodName, true)] = input
return nil
}

func (cr *chainReader) addDecoderDef(contractName, methodName string, outputs abi.Arguments, def types.ChainReaderDefinition) error {
output := &codecEntry{Args: outputs}
mod, err := def.OutputModifications.ToModifier(evmDecoderHooks...)
if err != nil {
return err
}
output.mod = mod
output := types.NewCodecEntry(outputs, nil, mod)
cr.parsed.decoderDefs[wrapItemType(contractName, methodName, false)] = output
return output.Init()
}

func setupEventInput(event abi.Event, def types.ChainReaderDefinition) ([]abi.Argument, *codecEntry, map[string]bool) {
func setupEventInput(event abi.Event, def types.ChainReaderDefinition) ([]abi.Argument, types.CodecEntry, map[string]bool) {
topicFieldDefs := map[string]bool{}
for _, value := range def.EventInputFields {
capFirstValue := abi.ToCamelCase(value)
topicFieldDefs[capFirstValue] = true
}

filterArgs := make([]abi.Argument, 0, maxTopicFields)
info := &codecEntry{}
filterArgs := make([]abi.Argument, 0, types.MaxTopicFields)
inputArgs := make([]abi.Argument, 0, len(event.Inputs))
indexArgNames := map[string]bool{}

for _, input := range event.Inputs {
Expand All @@ -286,9 +284,9 @@ func setupEventInput(event abi.Event, def types.ChainReaderDefinition) ([]abi.Ar
filterArgs = append(filterArgs, inputUnindexed)
}

info.Args = append(info.Args, input)
inputArgs = append(inputArgs, input)
indexArgNames[abi.ToCamelCase(input.Name)] = true
}

return filterArgs, info, indexArgNames
return filterArgs, types.NewCodecEntry(inputArgs, nil, nil), indexArgNames
}
10 changes: 5 additions & 5 deletions core/services/relay/evm/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var evmDecoderHooks = []mapstructure.DecodeHookFunc{decodeAccountHook, codec.Big
// eg: rename FooBar -> Bar, not foo_bar_ to Bar if the name on-chain is foo_bar_
func NewCodec(conf types.CodecConfig) (commontypes.RemoteCodec, error) {
parsed := &parsedTypes{
encoderDefs: map[string]*codecEntry{},
decoderDefs: map[string]*codecEntry{},
encoderDefs: map[string]types.CodecEntry{},
decoderDefs: map[string]types.CodecEntry{},
}

for k, v := range conf.ChainCodecConfigs {
Expand All @@ -51,7 +51,7 @@ func NewCodec(conf types.CodecConfig) (commontypes.RemoteCodec, error) {
return nil, err
}

item := &codecEntry{Args: args, mod: mod}
item := types.NewCodecEntry(args, nil, mod)
if err = item.Init(); err != nil {
return nil, err
}
Expand All @@ -70,7 +70,7 @@ type evmCodec struct {
}

func (c *evmCodec) CreateType(itemType string, forEncoding bool) (any, error) {
var itemTypes map[string]*codecEntry
var itemTypes map[string]types.CodecEntry
if forEncoding {
itemTypes = c.encoderDefs
} else {
Expand All @@ -82,7 +82,7 @@ func (c *evmCodec) CreateType(itemType string, forEncoding bool) (any, error) {
return nil, fmt.Errorf("%w: cannot find type name %s", commontypes.ErrInvalidType, itemType)
}

return reflect.New(def.checkedType).Interface(), nil
return reflect.New(def.CheckedType()).Interface(), nil
}

var bigIntType = reflect.TypeOf((*big.Int)(nil))
Expand Down
219 changes: 0 additions & 219 deletions core/services/relay/evm/codec_entry_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions core/services/relay/evm/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestCodec(t *testing.T) {
actual, err := c.GetMaxEncodingSize(testutils.Context(t), anyN, sizeItemType)
assert.NoError(t, err)

expected, err := evm.GetMaxSize(anyN, parseDefs(t)[sizeItemType])
expected, err := types.GetMaxSize(anyN, parseDefs(t)[sizeItemType])
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
Expand All @@ -41,7 +41,7 @@ func TestCodec(t *testing.T) {
actual, err := c.GetMaxDecodingSize(testutils.Context(t), anyN, sizeItemType)
assert.NoError(t, err)

expected, err := evm.GetMaxSize(anyN, parseDefs(t)[sizeItemType])
expected, err := types.GetMaxSize(anyN, parseDefs(t)[sizeItemType])
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
Expand Down
Loading

0 comments on commit e1f837f

Please sign in to comment.