Skip to content

Commit

Permalink
services/horizon/internal/ingest/processors: Dedupe participants dete…
Browse files Browse the repository at this point in the history
…rministically (stellar#5149)
  • Loading branch information
tamirms authored Jan 3, 2024
1 parent 51c1b15 commit 38f67b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package processors

import (
"context"
"sort"

"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/collections/set"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
Expand Down Expand Up @@ -73,16 +73,21 @@ func liquidityPoolsForTransaction(transaction ingest.LedgerTransaction) ([]strin
}

func dedupeStrings(in []string) []string {
set := set.Set[string]{}
for _, id := range in {
set.Add(id)
if len(in) <= 1 {
return in
}

out := make([]string, 0, len(in))
for id := range set {
out = append(out, id)
sort.Strings(in)
insert := 1
for cur := 1; cur < len(in); cur++ {
if in[cur] == in[cur-1] {
continue
}
if insert != cur {
in[insert] = in[cur]
}
insert++
}
return out
return in[:insert]
}

func liquidityPoolsForChanges(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"sort"

"github.com/guregu/null"

Expand Down Expand Up @@ -1034,16 +1035,25 @@ func (operation *transactionOperationWrapper) Participants() ([]xdr.AccountId, e
}

// dedupeParticipants remove any duplicate ids from `in`
func dedupeParticipants(in []xdr.AccountId) (out []xdr.AccountId) {
set := map[string]xdr.AccountId{}
for _, id := range in {
set[id.Address()] = id
func dedupeParticipants(in []xdr.AccountId) []xdr.AccountId {
if len(in) <= 1 {
return in
}

for _, id := range set {
out = append(out, id)
sort.Slice(in, func(i, j int) bool {
return in[i].Address() < in[j].Address()
})
insert := 1
for cur := 1; cur < len(in); cur++ {
if in[cur].Equals(in[cur-1]) {
continue
}
if insert != cur {
in[insert] = in[cur]
}
insert++
}
return
return in[:insert]

}

// OperationsParticipants returns a map with all participants per operation
Expand Down

0 comments on commit 38f67b9

Please sign in to comment.