-
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.
Preperation for register unregister execution capability (#15760)
* common bump * move aggregator * message cache to own package * lint * mod tidy * lint * generate * revert dispatcher revert * dispatcher mock
- Loading branch information
Showing
30 changed files
with
287 additions
and
284 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package aggregation | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
|
||
commoncap "github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb" | ||
remotetypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types" | ||
) | ||
|
||
// Default MODE Aggregator needs a configurable number of identical responses for aggregation to succeed | ||
type defaultModeAggregator struct { | ||
minIdenticalResponses uint32 | ||
} | ||
|
||
var _ remotetypes.Aggregator = &defaultModeAggregator{} | ||
|
||
func NewDefaultModeAggregator(minIdenticalResponses uint32) *defaultModeAggregator { | ||
return &defaultModeAggregator{ | ||
minIdenticalResponses: minIdenticalResponses, | ||
} | ||
} | ||
|
||
func (a *defaultModeAggregator) Aggregate(_ string, responses [][]byte) (commoncap.TriggerResponse, error) { | ||
found, err := AggregateModeRaw(responses, a.minIdenticalResponses) | ||
if err != nil { | ||
return commoncap.TriggerResponse{}, fmt.Errorf("failed to aggregate responses, err: %w", err) | ||
} | ||
|
||
unmarshaled, err := pb.UnmarshalTriggerResponse(found) | ||
if err != nil { | ||
return commoncap.TriggerResponse{}, fmt.Errorf("failed to unmarshal aggregated responses, err: %w", err) | ||
} | ||
return unmarshaled, nil | ||
} | ||
|
||
func AggregateModeRaw(elemList [][]byte, minIdenticalResponses uint32) ([]byte, error) { | ||
hashToCount := make(map[string]uint32) | ||
var found []byte | ||
for _, elem := range elemList { | ||
hasher := sha256.New() | ||
hasher.Write(elem) | ||
sha := hex.EncodeToString(hasher.Sum(nil)) | ||
hashToCount[sha]++ | ||
if hashToCount[sha] >= minIdenticalResponses { | ||
found = elem | ||
// update in case we find another elem with an even higher count | ||
minIdenticalResponses = hashToCount[sha] | ||
} | ||
} | ||
if found == nil { | ||
return nil, errors.New("not enough identical responses found") | ||
} | ||
return found, 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,51 @@ | ||
package aggregation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
commoncap "github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb" | ||
"github.com/smartcontractkit/chainlink-common/pkg/values" | ||
) | ||
|
||
var ( | ||
triggerEvent1 = map[string]any{"event": "triggerEvent1"} | ||
triggerEvent2 = map[string]any{"event": "triggerEvent2"} | ||
) | ||
|
||
func TestDefaultModeAggregator_Aggregate(t *testing.T) { | ||
val, err := values.NewMap(triggerEvent1) | ||
require.NoError(t, err) | ||
capResponse1 := commoncap.TriggerResponse{ | ||
Event: commoncap.TriggerEvent{ | ||
Outputs: val, | ||
}, | ||
Err: nil, | ||
} | ||
marshaled1, err := pb.MarshalTriggerResponse(capResponse1) | ||
require.NoError(t, err) | ||
|
||
val2, err := values.NewMap(triggerEvent2) | ||
require.NoError(t, err) | ||
capResponse2 := commoncap.TriggerResponse{ | ||
Event: commoncap.TriggerEvent{ | ||
Outputs: val2, | ||
}, | ||
Err: nil, | ||
} | ||
marshaled2, err := pb.MarshalTriggerResponse(capResponse2) | ||
require.NoError(t, err) | ||
|
||
agg := NewDefaultModeAggregator(2) | ||
_, err = agg.Aggregate("", [][]byte{marshaled1}) | ||
require.Error(t, err) | ||
|
||
_, err = agg.Aggregate("", [][]byte{marshaled1, marshaled2}) | ||
require.Error(t, err) | ||
|
||
res, err := agg.Aggregate("", [][]byte{marshaled1, marshaled2, marshaled1}) | ||
require.NoError(t, err) | ||
require.Equal(t, res, capResponse1) | ||
} |
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
Oops, something went wrong.