Skip to content

Commit

Permalink
fix(x/tally): outlier list is an inverted error list in case of conse…
Browse files Browse the repository at this point in the history
…nsus in error
  • Loading branch information
hacheigriega committed Jan 10, 2025
1 parent a59f7f5 commit 60b8c2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions x/tally/keeper/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type FilterResult struct {
GasUsed uint64 // gas used by filter
}

func errorCount(errors []bool) int {
// countErrors returns the number of errors in a given error list.
func countErrors(errors []bool) int {
count := 0
for _, err := range errors {
if err {
Expand All @@ -33,6 +34,15 @@ func errorCount(errors []bool) int {
return count
}

// invertErrors returns an inversion of a given error list.
func invertErrors(errors []bool) []bool {
inverted := make([]bool, len(errors))
for i, err := range errors {
inverted[i] = !err
}
return inverted
}

// BuildFilter builds a filter based on the requestor-provided input.
func (k Keeper) BuildFilter(ctx sdk.Context, filterInput string, replicationFactor uint16) (types.Filter, error) {
input, err := base64.StdEncoding.DecodeString(filterInput)
Expand Down Expand Up @@ -99,9 +109,9 @@ func ApplyFilter(filter types.Filter, reveals []types.RevealBody) (FilterResult,
outliers, consensus := filter.ApplyFilter(reveals, result.Errors)

switch {
case errorCount(result.Errors)*3 > len(reveals)*2:
case countErrors(result.Errors)*3 > len(reveals)*2:
result.Consensus = true
result.Outliers = outliers
result.Outliers = invertErrors(result.Errors)
return result, types.ErrConsensusInError
case !consensus:
result.Consensus = false
Expand Down
4 changes: 2 additions & 2 deletions x/tally/keeper/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestFilter(t *testing.T) {
{
name: "Mode filter - >2/3 bad exit codes",
tallyInputAsHex: "01000000000000000D242E726573756C742E74657874", // json_path = $.result.text
outliers: []bool{true, true, true, true, false, true},
outliers: []bool{false, false, false, false, true, false},
reveals: []types.RevealBody{
{ExitCode: 1, Reveal: `{"high_level_prop1":"ignore this", "result": {"text": "A", "number": 0}}`},
{ExitCode: 1, Reveal: `{"makes_this_json":"ignore this", "result": {"text": "A", "number": 10}}`},
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestFilter(t *testing.T) {
{
name: "Mode filter - >2/3 bad exit codes",
tallyInputAsHex: "01000000000000000D242E726573756C742E74657874", // json_path = $.result.text
outliers: []bool{true, false, true, true, true, true},
outliers: []bool{false, true, false, false, false, false},
reveals: []types.RevealBody{
{
ExitCode: 1,
Expand Down

0 comments on commit 60b8c2f

Please sign in to comment.