Skip to content

Commit

Permalink
use io.Closer
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkouv committed Oct 4, 2024
1 parent ff81400 commit ed5508c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 28 deletions.
26 changes: 9 additions & 17 deletions core/capabilities/ccip/oraclecreator/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
chainsel "github.com/smartcontractkit/chain-selectors"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3confighelper"
"go.uber.org/multierr"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/ccipevm"
evmconfig "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm"
Expand All @@ -21,8 +22,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"

chainsel "github.com/smartcontractkit/chain-selectors"

"github.com/smartcontractkit/libocr/commontypes"
libocr3 "github.com/smartcontractkit/libocr/offchainreporting2plus"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
Expand Down Expand Up @@ -205,21 +204,14 @@ func (i *pluginOracleCreator) Create(donID uint32, config cctypes.OCR3ConfigWith
return nil, err
}

closeFunc := func() error {
errs := make([]error, 0)

for _, cr := range contractReaders {
errs = append(errs, cr.Close())
}

for _, cw := range chainWriters {
errs = append(errs, cw.Close())
}

return multierr.Combine(errs...)
closers := make([]io.Closer, 0, len(contractReaders)+len(chainWriters))
for _, cr := range contractReaders {
closers = append(closers, cr)
}

return newWrappedOracle(oracle, closeFunc), nil
for _, cw := range chainWriters {
closers = append(closers, cw)
}
return newWrappedOracle(oracle, closers), nil
}

func (i *pluginOracleCreator) createFactoryAndTransmitter(
Expand Down
22 changes: 11 additions & 11 deletions core/capabilities/ccip/oraclecreator/wrapped_oracle.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package oraclecreator

import (
"errors"
"fmt"
"io"

cctypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/types"

"go.uber.org/multierr"
)

// wrappedOracle is a wrapper for cctypes.CCIPOracle that allows custom actions on Oracle shutdown.
type wrappedOracle struct {
baseOracle cctypes.CCIPOracle

// afterCloseFunc will run after calling baseOracle.Close()
afterCloseFunc func() error
// closableResources will be closed after calling baseOracle.Close()
closableResources []io.Closer
}

func newWrappedOracle(baseOracle cctypes.CCIPOracle, afterCloseFunc func() error) cctypes.CCIPOracle {
func newWrappedOracle(baseOracle cctypes.CCIPOracle, closableResources []io.Closer) cctypes.CCIPOracle {
return &wrappedOracle{
baseOracle: baseOracle,
afterCloseFunc: afterCloseFunc,
baseOracle: baseOracle,
closableResources: closableResources,
}
}

Expand All @@ -34,11 +34,11 @@ func (o *wrappedOracle) Close() error {
errs = append(errs, fmt.Errorf("close base oracle: %w", err))
}

if o.afterCloseFunc != nil {
if err := o.afterCloseFunc(); err != nil {
errs = append(errs, fmt.Errorf("after close func: %w", err))
for _, closer := range o.closableResources {
if err := closer.Close(); err != nil {
errs = append(errs, fmt.Errorf("close resource: %w", err))
}
}

return multierr.Combine(errs...)
return errors.Join(errs...)
}
81 changes: 81 additions & 0 deletions core/capabilities/ccip/oraclecreator/wrapped_oracle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package oraclecreator

import (
"errors"
"io"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_wrappedOracle_Close(t *testing.T) {
tests := []struct {
name string
oracleErr error
closerErrors []error
expectedErr error
}{
{
name: "no errors",
expectedErr: nil,
},
{
name: "oracle error",
oracleErr: err1,
expectedErr: errors.New("close base oracle: err1"),
},
{
name: "oracle and closers errors",
oracleErr: err1,
closerErrors: []error{err2, nil, err3},
expectedErr: errors.New("close base oracle: err1\nclose resource: err2\nclose resource: err3"),
},
{
name: "closers only errors",
oracleErr: nil,
closerErrors: []error{err2, nil, err3},
expectedErr: errors.New("close resource: err2\nclose resource: err3"),
},
{
name: "no errors with closers",
closerErrors: []error{nil, nil, nil, nil},
expectedErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
closers := make([]io.Closer, 0, len(tt.closerErrors))
for _, err := range tt.closerErrors {
closers = append(closers, mockCloser{err: err})
}

o := newWrappedOracle(mockOracle{err: tt.oracleErr}, closers)

err := o.Close()
if err == nil && tt.expectedErr == nil {
assert.NoError(t, err)
return
}

assert.Error(t, err)
assert.Equal(t, tt.expectedErr.Error(), err.Error())
})
}
}

type mockCloser struct{ err error }

func (m mockCloser) Close() error { return m.err }

type mockOracle struct{ err error }

func (m mockOracle) Close() error { return m.err }

func (m mockOracle) Start() error { return m.err }

var (
err1 = errors.New("err1")
err2 = errors.New("err2")
err3 = errors.New("err3")
)

0 comments on commit ed5508c

Please sign in to comment.