Skip to content
Merged
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
1 change: 1 addition & 0 deletions nodes/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ go.work
dev.env
prod.env
test.env
.venv

11 changes: 11 additions & 0 deletions nodes/node_balancer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ For Web3 providers `access_id` and `data_source` could be specified in headers
```bash
/usr/local/go/bin/go test -run ^TestCleanInactiveClientNodes$ github.com/bugout-dev/moonstream/nodes/node_balancer/cmd/nodebalancer -v -count=1
```

## Migrations

To run migration:

```bash
python migrations/migrations.py run --key 20230522 \
--token-current-owner "$NB_CONTROLLER_TOKEN" \
--token-new-owner "$MOONSTREAM_ADMIN_OR_OTHER_CONTROLLER" \
--new-application-id "$MOONSTREAM_APPLICATION_ID"
```
49 changes: 49 additions & 0 deletions nodes/node_balancer/cmd/nodebalancer/access_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"testing"
"time"
)

func accessCacheSetupSuit(t *testing.T) func(t *testing.T) {
t.Log("Setup suit")

CreateAccessCache()

return func(t *testing.T) {
t.Log("Teardown suit")
}
}

func TestAddAccessToCache(t *testing.T) {
teardownSuit := accessCacheSetupSuit(t)
defer teardownSuit(t)

tsNow := time.Now().Unix()

var cases = []struct {
prop ClientAccess
expected bool
}{
{
prop: ClientAccess{ClientResourceData: ClientResourceData{AccessID: "7378e2b2-b6ac-4738-bf34-fe39aa0d19e9"}},
expected: true,
},
{
prop: ClientAccess{ClientResourceData: ClientResourceData{AccessID: "000000000000000000000000000000000000"}},
expected: false,
},
{
prop: ClientAccess{ClientResourceData: ClientResourceData{Name: "name-1"}},
expected: false,
},
}
for _, c := range cases {
accessId := c.prop.ClientResourceData.AccessID
accessCache.AddAccessToCache(c.prop, tsNow)
if accessCache.isAccessIdInCache(accessId) != c.expected {
t.Logf("Access %s not found in access cache", accessId)
t.Fatal()
}
}
}
12 changes: 7 additions & 5 deletions nodes/node_balancer/cmd/nodebalancer/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,21 @@ func cli() {
PeriodStartTs: time.Now().Unix(),
MaxCallsPerPeriod: stateCLI.MaxCallsPerPeriodFlag,
CallsPerPeriod: 0,

Type: BUGOUT_RESOURCE_TYPE_NODEBALANCER_ACCESS,
}
_, err := bugoutClient.Brood.FindUser(
NB_CONTROLLER_TOKEN,
map[string]string{
"user_id": proposedClientResourceData.UserID,
"application_id": NB_APPLICATION_ID,
"application_id": MOONSTREAM_APPLICATION_ID,
},
)
if err != nil {
fmt.Printf("User does not exists, err: %v\n", err)
os.Exit(1)
}
resource, err := bugoutClient.Brood.CreateResource(NB_CONTROLLER_TOKEN, NB_APPLICATION_ID, proposedClientResourceData)
resource, err := bugoutClient.Brood.CreateResource(NB_CONTROLLER_TOKEN, MOONSTREAM_APPLICATION_ID, proposedClientResourceData)
if err != nil {
fmt.Printf("Unable to create user access, err: %v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -302,7 +304,7 @@ func cli() {
}
resources, err := bugoutClient.Brood.GetResources(
NB_CONTROLLER_TOKEN,
NB_APPLICATION_ID,
MOONSTREAM_APPLICATION_ID,
queryParameters,
)
if err != nil {
Expand Down Expand Up @@ -406,7 +408,7 @@ func cli() {
}
resources, err := bugoutClient.Brood.GetResources(
NB_CONTROLLER_TOKEN,
NB_APPLICATION_ID,
MOONSTREAM_APPLICATION_ID,
queryParameters,
)
if err != nil {
Expand Down Expand Up @@ -464,7 +466,7 @@ func cli() {
}
resources, err := bugoutClient.Brood.GetResources(
NB_CONTROLLER_TOKEN,
NB_APPLICATION_ID,
MOONSTREAM_APPLICATION_ID,
queryParameters,
)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions nodes/node_balancer/cmd/nodebalancer/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var (
type ClientAccess struct {
ResourceID string

authorizationToken string

ClientResourceData ClientResourceData

LastAccessTs int64
Expand All @@ -36,6 +38,8 @@ type ClientResourceData struct {
PeriodStartTs int64 `json:"period_start_ts"`
MaxCallsPerPeriod int64 `json:"max_calls_per_period"`
CallsPerPeriod int64 `json:"calls_per_period"`

Type string `json:"type"`
}

// CheckClientCallPeriodLimits returns true if limit of call requests per period is exceeded
Expand Down
9 changes: 4 additions & 5 deletions nodes/node_balancer/cmd/nodebalancer/clients_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO(kompotkot): Re-write tests for client
package main

import (
Expand All @@ -7,7 +6,7 @@ import (
"time"
)

func setupSuit(t *testing.T) func(t *testing.T) {
func clientsSetupSuit(t *testing.T) func(t *testing.T) {
t.Log("Setup suit")

supportedBlockchains = map[string]bool{"ethereum": true}
Expand All @@ -18,7 +17,7 @@ func setupSuit(t *testing.T) func(t *testing.T) {
}

func TestAddClientNode(t *testing.T) {
teardownSuit := setupSuit(t)
teardownSuit := clientsSetupSuit(t)
defer teardownSuit(t)

var cases = []struct {
Expand All @@ -44,7 +43,7 @@ func TestAddClientNode(t *testing.T) {
}

func TestGetClientNode(t *testing.T) {
teardownSuit := setupSuit(t)
teardownSuit := clientsSetupSuit(t)
defer teardownSuit(t)

ts := time.Now().Unix()
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestGetClientNode(t *testing.T) {
}

func TestCleanInactiveClientNodes(t *testing.T) {
teardownSuit := setupSuit(t)
teardownSuit := clientsSetupSuit(t)
defer teardownSuit(t)

ts := time.Now().Unix()
Expand Down
8 changes: 7 additions & 1 deletion nodes/node_balancer/cmd/nodebalancer/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (

// Bugout and application configuration
BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5
NB_APPLICATION_ID = os.Getenv("NB_APPLICATION_ID")
MOONSTREAM_APPLICATION_ID = os.Getenv("MOONSTREAM_APPLICATION_ID")
NB_CONTROLLER_TOKEN = os.Getenv("NB_CONTROLLER_TOKEN")
NB_CONTROLLER_ACCESS_ID = os.Getenv("NB_CONTROLLER_ACCESS_ID")

Expand All @@ -46,6 +46,12 @@ var (

// Humbug configuration
HUMBUG_REPORTER_NB_TOKEN = os.Getenv("HUMBUG_REPORTER_NB_TOKEN")

// Moonstream resources types
BUGOUT_RESOURCE_TYPE_NODEBALANCER_ACCESS = "nodebalancer-access"
DEFAULT_AUTOGENERATED_USER_PERMISSIONS = []string{"read", "update", "delete"}
DEFAULT_AUTOGENERATED_PERIOD_DURATION = int64(86400)
DEFAULT_AUTOGENERATED_MAX_CALLS_PER_PERIOD = int64(1000)
)

func CheckEnvVarSet() {
Expand Down
Loading