-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Functions] Offchain heartbeat support in Listener and OCR2 plugin
1. Add new API structs 2. Mark offchain requests in Listener 3. Introduce a simple OffchainTransmitter to pass responses from the plugin
- Loading branch information
Showing
9 changed files
with
308 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package functions | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Simple wrapper around a channel to transmit offchain reports between | ||
// OCR plugin and Gateway connector | ||
// | ||
//go:generate mockery --quiet --name OffchainTransmitter --output ./mocks/ --case=underscore | ||
type OffchainTransmitter interface { | ||
TransmitReport(ctx context.Context, report *OffchainResponse) error | ||
ReportChannel() chan *OffchainResponse | ||
} | ||
|
||
type offchainTransmitter struct { | ||
reportCh chan *OffchainResponse | ||
} | ||
|
||
func NewOffchainTransmitter(chanSize uint32) OffchainTransmitter { | ||
return &offchainTransmitter{ | ||
reportCh: make(chan *OffchainResponse, chanSize), | ||
} | ||
} | ||
|
||
func (t *offchainTransmitter) TransmitReport(ctx context.Context, report *OffchainResponse) error { | ||
select { | ||
case t.reportCh <- report: | ||
return nil | ||
case <-ctx.Done(): | ||
return errors.New("context cancelled") | ||
} | ||
} | ||
|
||
func (t *offchainTransmitter) ReportChannel() chan *OffchainResponse { | ||
return t.reportCh | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package functions_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/functions" | ||
) | ||
|
||
func TestOffchainTransmitter(t *testing.T) { | ||
t.Parallel() | ||
|
||
transmitter := functions.NewOffchainTransmitter(1) | ||
ch := transmitter.ReportChannel() | ||
report := &functions.OffchainResponse{RequestId: []byte("testID")} | ||
ctx := testutils.Context(t) | ||
|
||
require.NoError(t, transmitter.TransmitReport(ctx, report)) | ||
require.Equal(t, report, <-ch) | ||
|
||
require.NoError(t, transmitter.TransmitReport(ctx, report)) | ||
|
||
ctxTimeout, cancel := context.WithTimeout(ctx, time.Millisecond*20) | ||
defer cancel() | ||
// should not freeze | ||
require.Error(t, transmitter.TransmitReport(ctxTimeout, report)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.