-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
205 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package plugincommon | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
) | ||
|
||
// GetTransmissionSchedule returns a TransmissionSchedule for the provided oracles. | ||
// It uses the ChainSupport service to query which oracles support the destination chain. | ||
// It returns an error if no oracles support the destination chain. | ||
// Read more about TransmissionDelay at ocr3types.TransmissionSchedule | ||
// The transmissionDelayMultiplier is used in the following way: | ||
// | ||
// Assume that we have transmitters: [1, 3, 5] | ||
// And transmissionDelayMultiplier = 5s | ||
// Then the transmission delays will be: [5s, 10s, 15s] | ||
func GetTransmissionSchedule( | ||
chainSupport ChainSupport, | ||
allTheOracles []commontypes.OracleID, | ||
transmissionDelayMultiplier time.Duration, | ||
) (*ocr3types.TransmissionSchedule, error) { | ||
transmitters := make([]commontypes.OracleID, 0, len(allTheOracles)) | ||
for _, oracleID := range allTheOracles { | ||
supportsDestChain, err := chainSupport.SupportsDestChain(oracleID) | ||
if err != nil { | ||
return nil, fmt.Errorf("supports dest chain %d: %w", oracleID, err) | ||
} | ||
if supportsDestChain { | ||
transmitters = append(transmitters, oracleID) | ||
} | ||
} | ||
|
||
transmissionDelays := make([]time.Duration, len(transmitters)) | ||
|
||
for i := range transmissionDelays { | ||
transmissionDelays[i] = (transmissionDelayMultiplier) * time.Duration(i+1) | ||
} | ||
|
||
if len(transmitters) == 0 { | ||
return nil, fmt.Errorf("no transmitters") | ||
} | ||
|
||
if len(transmitters) != len(transmissionDelays) { | ||
return nil, fmt.Errorf("critical issue mismatched transmitters and transmission delays") | ||
} | ||
|
||
return &ocr3types.TransmissionSchedule{ | ||
Transmitters: transmitters, | ||
TransmissionDelays: transmissionDelays, | ||
}, nil | ||
} |
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,83 @@ | ||
package plugincommon_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/stretchr/testify/require" | ||
|
||
plugincommon2 "github.com/smartcontractkit/chainlink-ccip/internal/plugincommon" | ||
"github.com/smartcontractkit/chainlink-ccip/mocks/internal_/plugincommon" | ||
) | ||
|
||
func TestGetTransmissionSchedule(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
allTheOracles []commontypes.OracleID | ||
oraclesSupportingDest []commontypes.OracleID | ||
transmissionDelayMultiplier time.Duration | ||
expectedTransmitters []commontypes.OracleID | ||
expectedTransmissionDelays []time.Duration | ||
expectedError bool | ||
chainSupportReturnsError bool | ||
}{ | ||
{ | ||
name: "no oracles supporting dest leads to error", | ||
allTheOracles: []commontypes.OracleID{1, 2, 3}, | ||
oraclesSupportingDest: []commontypes.OracleID{}, | ||
transmissionDelayMultiplier: 5 * time.Second, | ||
expectedTransmitters: []commontypes.OracleID{}, | ||
expectedTransmissionDelays: []time.Duration{}, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "some transmitters supporting dest", | ||
allTheOracles: []commontypes.OracleID{1, 2, 3}, | ||
oraclesSupportingDest: []commontypes.OracleID{1, 3}, | ||
transmissionDelayMultiplier: 5 * time.Second, | ||
expectedTransmitters: []commontypes.OracleID{1, 3}, | ||
expectedTransmissionDelays: []time.Duration{5 * time.Second, 10 * time.Second}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "chainsupport returns error", | ||
allTheOracles: []commontypes.OracleID{1, 2, 3}, | ||
oraclesSupportingDest: []commontypes.OracleID{1, 3}, | ||
transmissionDelayMultiplier: 5 * time.Second, | ||
expectedError: true, | ||
chainSupportReturnsError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cs := plugincommon.NewMockChainSupport(t) | ||
destSupportingOraclesSet := mapset.NewSet(tc.oraclesSupportingDest...) | ||
for _, oracleID := range tc.allTheOracles { | ||
var err error | ||
if tc.chainSupportReturnsError { | ||
err = errors.New("some error") | ||
} | ||
cs.On("SupportsDestChain", oracleID). | ||
Return(destSupportingOraclesSet.Contains(oracleID), err).Maybe() | ||
} | ||
|
||
transmissionSchedule, err := plugincommon2.GetTransmissionSchedule( | ||
cs, | ||
tc.allTheOracles, | ||
tc.transmissionDelayMultiplier, | ||
) | ||
if tc.expectedError { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedTransmitters, transmissionSchedule.Transmitters) | ||
require.Equal(t, tc.expectedTransmissionDelays, transmissionSchedule.TransmissionDelays) | ||
}) | ||
} | ||
} |