Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions consensus/oracle/consensus_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func CalculateOutcomeForObservations(
observations []*valuespb.Value,
consensusDescriptor *sdk.ConsensusDescriptor,
defaultValue *valuespb.Value,
minObservations int,
f int,
) (*valuespb.Value, error) {
filtered, _, err := filterObservations(observations, minObservations)
filtered, _, err := filterObservations(observations, f+1)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -456,35 +455,30 @@ func filterObservations(observationProtos []*valuespb.Value, minObservations int
return nil, nil, fmt.Errorf("insufficient observations (%d) to meet minimum (%d)", len(observationProtos), minObservations)
}

var dominantType reflect.Type
var highestCount int
var highestCountEqual bool

observationsByType := map[reflect.Type][]*valuespb.Value{}

for _, observation := range observationProtos {
if observation.Value == nil {
continue
}

tpe := reflect.TypeOf(observation.Value)
observationsByType[tpe] = append(observationsByType[tpe], observation)
count := len(observationsByType[tpe])
if count > highestCount {
highestCount = count
dominantType = tpe
highestCountEqual = false
} else if count == highestCount {
highestCountEqual = true
}
}

if highestCount < minObservations {
return nil, nil, fmt.Errorf("no single type met the minimum observation threshold of %d", minObservations)
var dominantType reflect.Type
for tpe, obsOfType := range observationsByType {
if len(obsOfType) >= minObservations {
if dominantType == nil {
dominantType = tpe
} else {
// More than one type meets the threshold
return nil, nil, ErrMultipleValuesMetThreshold
}
}
}

if highestCountEqual {
return nil, nil, ErrMultipleValuesMetThreshold
if dominantType == nil {
return nil, nil, ErrNoValuesMetThreshold
}

return observationsByType[dominantType], dominantType, nil
Expand Down
27 changes: 19 additions & 8 deletions consensus/oracle/consensus_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
observations []*valuespb.Value
descriptor *sdk.ConsensusDescriptor
defaultValue *valuespb.Value
minObs int
f int
expectedOutcome *valuespb.Value
expectedError error
Expand All @@ -43,7 +42,7 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_MEDIAN,
},
},
minObs: 3,
f: 2,
expectedOutcome: nil,
expectedError: errors.New("insufficient observations"),
},
Expand All @@ -61,7 +60,7 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_MEDIAN,
},
},
minObs: 5,
f: 4,
expectedOutcome: values.Proto(values.NewInt64(30)),
expectedError: nil,
},
Expand All @@ -80,13 +79,28 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_IDENTICAL,
},
},
minObs: 5,
f: 3,
expectedOutcome: values.Proto(values.NewInt64(42)),
expectedError: nil,
},
{
name: "median: mixed types, one dominant (int64) - handled by filtering",
name: "median: mixed types, two eligible types (int64, float64) - error returned",
f: 1,
observations: []*valuespb.Value{
values.Proto(values.NewInt64(10)), values.Proto(values.NewFloat64(1.0)),
values.Proto(values.NewInt64(20)), values.Proto(values.NewFloat64(2.0)),
values.Proto(values.NewInt64(30)), values.Proto(values.NewInt64(40)),
},
descriptor: &sdk.ConsensusDescriptor{
Descriptor_: &sdk.ConsensusDescriptor_Aggregation{
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_MEDIAN,
},
},
expectedError: ErrMultipleValuesMetThreshold,
},
{
name: "median: mixed types, one eligible type (int64) - handled by filtering",
f: 2,
observations: []*valuespb.Value{
values.Proto(values.NewInt64(10)), values.Proto(values.NewFloat64(1.0)),
values.Proto(values.NewInt64(20)), values.Proto(values.NewFloat64(2.0)),
Expand Down Expand Up @@ -155,7 +169,6 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
mustWrap(s{Val: 50, OtherField: "common", PrefixSlice: []int64{1, 2, 6}, Nest: s1{Val: 102}, SuffixSlice: []int64{42, 2, 3}}),
},
descriptor: cre.ConsensusAggregationFromTags[s]().Descriptor(),
minObs: 5,
f: 2,
expectedOutcome: mustWrap(s{
Val: 30,
Expand Down Expand Up @@ -201,7 +214,6 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
Aggregation: sdk.AggregationType_AGGREGATION_TYPE_UNSPECIFIED,
},
},
minObs: 1,
expectedOutcome: nil,
expectedError: errors.New("unknown aggregation type"),
},
Expand All @@ -214,7 +226,6 @@ func Test_CalculateOutcomeForObservations(t *testing.T) {
tc.observations,
tc.descriptor,
tc.defaultValue,
tc.minObs,
tc.f,
)

Expand Down
Loading