Skip to content

Commit

Permalink
Define new registry interface, implement new mock registry, update te…
Browse files Browse the repository at this point in the history
…st cases with mock registry instances
  • Loading branch information
ferglor committed Nov 10, 2023
1 parent 4194323 commit f79529d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/scripts/chaincli/handler/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (k *Keeper) Debug(ctx context.Context, args []string) {
lggr, _ := logger.NewLogger()
blockSub := &blockSubscriber{k.client}

_ = streams.NewStreamsLookup(packer, mercuryConfig, blockSub, keeperRegistry21, k.rpcClient, lggr)
_ = streams.NewStreamsLookup(packer, mercuryConfig, blockSub, keeperRegistry21, lggr)

streamsLookupErr, err := packer.DecodeStreamsLookupRequest(checkResult.PerformData)
if err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ type contextCaller interface {
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
}

type streamsRegistry interface {
GetUpkeepPrivilegeConfig(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error)
CheckCallback(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error)
}

type streams struct {
packer mercury.Packer
mercuryConfig mercury.MercuryConfigProvider
abi abi.ABI
blockSubscriber latestBlockProvider
registry *iregistry21.IKeeperRegistryMaster
registry streamsRegistry
lggr logger.Logger
v02Client mercury.MercuryClient
v03Client mercury.MercuryClient
Expand All @@ -56,7 +61,7 @@ func NewStreamsLookup(
packer mercury.Packer,
mercuryConfig mercury.MercuryConfigProvider,
blockSubscriber latestBlockProvider,
registry *iregistry21.IKeeperRegistryMaster,
registry streamsRegistry,
lggr logger.Logger) *streams {
httpClient := http.DefaultClient
return &streams{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ func (m *MockIKeeperRegistryMaster) GetUpkeepPrivilegeConfig(opts *bind.CallOpts
return args.Get(0).([]byte), args.Error(1)
}

type mockRegistry struct {
GetUpkeepPrivilegeConfigFn func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error)
CheckCallbackFn func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error)
}

func (r *mockRegistry) GetUpkeepPrivilegeConfig(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return r.GetUpkeepPrivilegeConfigFn(opts, upkeepId)
}

func (r *mockRegistry) CheckCallback(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return r.CheckCallbackFn(opts, id, values, extraData)
}

//// Your function using the registry
//func YourFunctionUsingRegistry(s *IKeeperRegistryMaster, opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
// upkeepPrivilegeConfigBytes, err := s.GetUpkeepPrivilegeConfig(opts, upkeepId)
Expand All @@ -155,7 +168,14 @@ func setupStreams(t *testing.T) *streams {
packer := encoding.NewAbiPacker()
mercuryConfig := new(MockMercuryConfigProvider)
blockSubscriber := new(MockBlockSubscriber)
registry := new(MockIKeeperRegistryMaster)
registry := &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return []byte{}, nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
}
//registry := &iregistry21.IKeeperRegistryMaster{}

streams := NewStreamsLookup(
Expand Down Expand Up @@ -289,32 +309,87 @@ func TestStreams_AllowedToUseMercury(t *testing.T) {
err error
state mercury.MercuryUpkeepState
reason mercury.MercuryUpkeepFailureReason
registry streamsRegistry
retryable bool
config []byte
}{
{
name: "success - allowed via cache",
cached: true,
allowed: true,
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return nil, nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "success - allowed via fetching privilege config",
allowed: true,
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return nil, nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "success - not allowed via cache",
cached: true,
allowed: false,
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return nil, nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "success - not allowed via fetching privilege config",
allowed: false,
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return nil, nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "failure - cannot unmarshal privilege config",
err: fmt.Errorf("failed to unmarshal privilege config: invalid character '\\x00' looking for beginning of value"),
state: mercury.MercuryUnmarshalError,
config: []byte{0, 1},
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return []byte(`{`), nil
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "failure - cannot get privilege config",
err: fmt.Errorf("failed to unmarshal privilege config: invalid character '\\x00' looking for beginning of value"),
state: mercury.MercuryUnmarshalError,
config: []byte{0, 1},
registry: &mockRegistry{
GetUpkeepPrivilegeConfigFn: func(opts *bind.CallOpts, upkeepId *big.Int) ([]byte, error) {
return nil, errors.New("unable to get upkeep privilege config")
},
CheckCallbackFn: func(opts *bind.CallOpts, id *big.Int, values [][]byte, extraData []byte) (iregistry21.CheckCallback, error) {
return iregistry21.CheckCallback{}, nil
},
},
},
{
name: "failure - flaky RPC",
Expand All @@ -334,6 +409,7 @@ func TestStreams_AllowedToUseMercury(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := setupStreams(t)
s.registry = tt.registry

client := new(evmClientMocks.Client)
s.client = client

Check failure on line 415 in core/services/ocr2/plugins/ocr2keeper/evm21/mercury/streams/streams_test.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_tests)

s.client undefined (type *streams has no field or method client)

Check failure on line 415 in core/services/ocr2/plugins/ocr2keeper/evm21/mercury/streams/streams_test.go

View workflow job for this annotation

GitHub Actions / lint

s.client undefined (type *streams has no field or method client)

Check failure on line 415 in core/services/ocr2/plugins/ocr2keeper/evm21/mercury/streams/streams_test.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_race_tests)

s.client undefined (type *streams has no field or method client)
Expand Down

0 comments on commit f79529d

Please sign in to comment.