Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ func decodeOcr3Config(pbCfg *capabilitiespb.OCR3Config) ocrtypes.ContractConfig
}
}

func transmitterAccountToBytes(account ocrtypes.Account) ([]byte, error) {
Comment thread
ilija42 marked this conversation as resolved.
Comment thread
cawthorne marked this conversation as resolved.
decoded, err := hex.DecodeString(string(account))
if err == nil {
return decoded, nil
}
return []byte(account), nil
}
Comment thread
cawthorne marked this conversation as resolved.

func (cr *capabilitiesRegistryClient) Get(ctx context.Context, ID string) (capabilities.BaseCapability, error) {
req := &pb.GetRequest{
Id: ID,
Expand Down Expand Up @@ -512,9 +520,9 @@ func (c *capabilitiesRegistryServer) ConfigForCapability(ctx context.Context, re
}
transmitters := make([][]byte, len(cfg.Transmitters))
for i, t := range cfg.Transmitters {
transmitters[i], err = hex.DecodeString(string(t))
transmitters[i], err = transmitterAccountToBytes(t)
if err != nil {
return nil, fmt.Errorf("failed to decode transmitter: %w", err)
return nil, err
}
}
ccp.Ocr3Configs[key] = &capabilitiespb.OCR3Config{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package capability

import (
"context"
"encoding/hex"
"errors"
"sync"
"testing"
Expand Down Expand Up @@ -586,6 +587,97 @@ func TestCapabilitiesRegistry_ConfigForCapability_WithOcr3AndOracleFactoryConfig
assert.Equal(t, expectedCapConfig, capConf)
}

func TestTransmitterAccountToBytes(t *testing.T) {
t.Run("decodes_plain_hex", func(t *testing.T) {
gotUpper, err := transmitterAccountToBytes(ocrtypes.Account("00ff"))
require.NoError(t, err)
require.Equal(t, []byte{0x00, 0xff}, gotUpper)
})

t.Run("falls_back_to_raw_bytes_for_prefixed_printable_account", func(t *testing.T) {
raw := []byte("0xABcd")
got, err := transmitterAccountToBytes(ocrtypes.Account(string(raw)))
require.NoError(t, err)
require.Equal(t, raw, got)
})

t.Run("falls_back_to_raw_bytes_for_non_printable_account_even_with_0x_prefix", func(t *testing.T) {
raw := []byte{'0', 'x', 0xff, 'A'}
got, err := transmitterAccountToBytes(ocrtypes.Account(string(raw)))
require.NoError(t, err)
require.Equal(t, raw, got)
})

t.Run("falls_back_to_raw_bytes_for_realistic_aptos_account_bytes_without_0x_prefix", func(t *testing.T) {
raw, err := hex.DecodeString("26c93635e9af3ce8ba977ba6c3e4bc84b1cbfbeffe850a603ef0a7251aecbd55")
require.NoError(t, err)
require.Len(t, raw, 32)

// Aptos account bytes are raw binary here, not printable ASCII hex. The
// helper must preserve them unchanged rather than attempting to decode the
// string form as hex.
got, err := transmitterAccountToBytes(ocrtypes.Account(string(raw)))
require.NoError(t, err)
require.Equal(t, raw, got)
})
}
Comment thread
cawthorne marked this conversation as resolved.

func TestCapabilitiesRegistry_ConfigForCapability_NormalizesRawAndHexTransmitters(t *testing.T) {
stopCh := make(chan struct{})
logger := logger.Test(t)
reg := mocks.NewCapabilitiesRegistry(t)

pluginName := "registry-test"
client, server := plugin.TestPluginGRPCConn(
t,
true,
map[string]plugin.Plugin{
pluginName: &testRegistryPlugin{
impl: reg,
brokerExt: &net.BrokerExt{
BrokerConfig: net.BrokerConfig{
StopCh: stopCh,
Logger: logger,
},
},
},
},
)

defer client.Close()
defer server.Stop()

regClient, err := client.Dispense(pluginName)
require.NoError(t, err)

rc, ok := regClient.(*capabilitiesRegistryClient)
require.True(t, ok)

capID := "ocr-cap@1.0.0"
donID := uint32(1)
rawTransmitter := []byte{'0', 'x', 0xff, 'A'}

serverCapConfig := capabilities.CapabilityConfiguration{
Ocr3Configs: map[string]ocrtypes.ContractConfig{
"__default__": {
ConfigCount: 5,
Signers: []ocrtypes.OnchainPublicKey{{0x01, 0x02}},
Transmitters: []ocrtypes.Account{"abcd", ocrtypes.Account(string(rawTransmitter)), "123e"},
F: 1,
OnchainConfig: []byte{0x10, 0x20},
OffchainConfigVersion: 2,
OffchainConfig: []byte{0x30, 0x40},
},
},
}
reg.On("ConfigForCapability", mock.Anything, capID, donID).Once().Return(serverCapConfig, nil)

capConf, err := rc.ConfigForCapability(t.Context(), capID, donID)
require.NoError(t, err)
require.Contains(t, capConf.Ocr3Configs, "__default__")
assert.Equal(t, []ocrtypes.Account{"abcd", "3078ff41", "123e"}, capConf.Ocr3Configs["__default__"].Transmitters)
}

func TestCapabilitiesRegistry_DONsForCapability(t *testing.T) {
stopCh := make(chan struct{})
logger := logger.Test(t)
Expand Down
Loading