Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Update application registry when chains change. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
nodiesBlade authored May 27, 2024
1 parent fd302f2 commit 5f1aad1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
30 changes: 26 additions & 4 deletions internal/apps_registry/cached_app_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pokt-network/gateway-server/pkg/pokt/pokt_v0"
pokt "github.com/pokt-network/gateway-server/pkg/pokt/pokt_v0/models"
"go.uber.org/zap"
"reflect"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -171,14 +172,35 @@ func arePoktApplicationSignersEqual(slice1, slice2 []*models.PoktApplicationSign
return sortedSlice2[i].NetworkApp.Address < sortedSlice2[j].NetworkApp.Address
})

// Now that slices are sorted, check if address keys are same.
// Now that slices are sorted, check if address keys and chains are same.
for i := range slice1 {
// Check if any field is different
if !strings.EqualFold(sortedSlice1[i].NetworkApp.Address, sortedSlice2[i].NetworkApp.Address) {

networkApp1 := sortedSlice1[i].NetworkApp
networkApp2 := sortedSlice2[i].NetworkApp

// If address are not same, gateway operator added or remove some application
if !strings.EqualFold(networkApp1.Address, networkApp2.Address) {
return false
}

// Copy network chains and sort them.
// Applications are equal if they have same chains but different ordering
chains1Copy := append([]string{}, networkApp1.Chains...)
chains2Copy := append([]string{}, networkApp2.Chains...)
sort.Strings(chains1Copy)
sort.Strings(chains2Copy)

if len(chains1Copy) != len(chains2Copy) {
return false
}

// Gateway operator may have updated chains staked
if !reflect.DeepEqual(chains1Copy, chains2Copy) {
return false
}

}

// Slices are equal
// Applications are equal
return true
}
121 changes: 121 additions & 0 deletions internal/apps_registry/cached_app_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package apps_registry

import (
"github.com/pokt-network/gateway-server/internal/apps_registry/models"
pokt_models "github.com/pokt-network/gateway-server/pkg/pokt/pokt_v0/models"
"testing"
)

func Test_arePoktApplicationSignersEqual(t *testing.T) {
type args struct {
slice1 []*models.PoktApplicationSigner
slice2 []*models.PoktApplicationSigner
}
tests := []struct {
name string
args args
want bool
}{
{
name: "different length",
args: args{
slice1: []*models.PoktApplicationSigner{},
slice2: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
},
want: false,
},
{
name: "different address",
args: args{
slice1: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "1234",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
slice2: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
},
want: false,
},
{
name: "different chains",
args: args{
slice1: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "1234",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
slice2: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
},
want: false,
},
{
name: "same apps with exact chains",
args: args{
slice1: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
slice2: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "123"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
},
want: true,
},
{
name: "same apps with same chains different ordering",
args: args{
slice1: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "1234", "1235"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
slice2: []*models.PoktApplicationSigner{{NetworkApp: &pokt_models.PoktApplication{
Address: "123",
Chains: []string{"123", "1235", "1234"},
PublicKey: "",
Status: 0,
MaxRelays: 0,
}}},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := arePoktApplicationSignersEqual(tt.args.slice1, tt.args.slice2); got != tt.want {
t.Errorf("arePoktApplicationSignersEqual() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5f1aad1

Please sign in to comment.