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

Update application registry when chains change. #42

Merged
merged 8 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
16 changes: 13 additions & 3 deletions internal/apps_registry/cached_app_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,22 @@ 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
}

// Gateway operator may have updated chains staked
if !strings.EqualFold(strings.Join(networkApp1.Chains, ","), strings.Join(networkApp2.Chains, ",")) {
Copy link
Contributor

@Maxitosh Maxitosh May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nodiesBlade what if the order of Chain IDs is different for two applications?
Let's say:

  • networkApp1.Chains = ["111", "222", "333"]
  • networkApp2.Chains = ["222", "333", "111"]

I don't know if Chains are ordered before assigning to PoktApplication, but it is better not to rely on string concatenation equality.

Copy link
Contributor

@Maxitosh Maxitosh May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ worth adding a unit test for such a case.

Copy link
Collaborator Author

@nodiesBlade nodiesBlade May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the nature of blockchain and how hashing works, if the chains ordering change, i would argue the application is still technically different and we should recache it. and so that's why I took a little shortcut.

Though I'm glad you mentioned it as well, wouldn't mind adding the extra security. will push up some changes for it

Copy link
Contributor

@Maxitosh Maxitosh May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was talking only about the way Chains were compared.

Absolutely agree with what you say about Applications equivalency on different Chains order. No need to re-cache such.

return false
}

}

// Slices are equal
Expand Down
101 changes: 101 additions & 0 deletions internal/apps_registry/cached_app_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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",
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,
},
}
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)
}
})
}
}
Loading