Skip to content

Commit

Permalink
feat: added importEndpoints command
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Nov 22, 2024
1 parent b657079 commit 2aa58b2
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ Password:

_Before staking on Razor Network, please ensure your account has sFUEL and RAZOR. For testnet RAZOR, please contact us on Discord._

### Import Endpoints

You can import the endpoints mentioned in `endpoints.json` from the repository to your `$HOME/.razor/endpoints.json` file using the `importEndpoints` command.
This multiple providers along with the user input provider are sorted according to the best performance and the best provider is chosen by the RPC manager which will be used to make the RPC calls.

razor cli

```
$ ./razor importEndpoints
```

docker

```
docker exec -it razor-go razor importEndpoints
```

### Stake

If you have a minimum of 1000 razors in your account, you can stake those using the addStake command.
Expand Down
47 changes: 47 additions & 0 deletions cmd/importEndpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"encoding/json"
"github.com/spf13/cobra"
"os"
"path/filepath"
"razor/core"
"razor/utils"
)

// importEndpointsCmd represents the importEndpoints command
var importEndpointsCmd = &cobra.Command{
Use: "importEndpoints",
Short: "imports the list of endpoints locally",
Long: `Imports the list of endpoints to $HOME/.rzr directory allowing the user to access/edit it easily.
Example:
./razor importEndpoints `,
Run: initialiseImportEndpoints,
}

func initialiseImportEndpoints(cmd *cobra.Command, args []string) {
cmdUtils.ExecuteImportEndpoints()
}

// ExecuteImportEndpoints imports the list of endpoints from core/endpoints.go to $HOME/.razor directory locally.
func (*UtilsStruct) ExecuteImportEndpoints() {
defaultPath, err := pathUtils.GetDefaultPath()
utils.CheckError("Error in getting default path: ", err)

// Define the target path for endpoints.json
destFilePath := filepath.Join(defaultPath, "endpoints.json")

// Serialize the default endpoints to JSON
endpointsData, err := json.MarshalIndent(core.DefaultEndpoints, "", " ")
utils.CheckError("Error in serializing the endpoints: %w", err)

// Write the JSON to the destination file
err = os.WriteFile(destFilePath, endpointsData, 0644)
utils.CheckError("Error in writing endpoints.json: %w", err)

log.Infof("Default endpoints successfully imported to %s", destFilePath)
}

func init() {
rootCmd.AddCommand(importEndpointsCmd)
}
52 changes: 52 additions & 0 deletions cmd/importEndpoints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"razor/core"
"testing"
)

func TestExecuteImportEndpoints_WithMockedPath(t *testing.T) {
// Create a temporary directory to mock the default path
tempDir, err := os.MkdirTemp("", "test_default_path")
assert.NoError(t, err, "Temporary directory should be created")
defer os.RemoveAll(tempDir) // Clean up after the test

SetUpMockInterfaces()

pathMock.On("GetDefaultPath").Return(tempDir, nil)

// Call ExecuteImportEndpoints
utils := &UtilsStruct{}
utils.ExecuteImportEndpoints()

// Verify the file was created in the mocked directory
destFilePath := filepath.Join(tempDir, "endpoints.json")
_, err = os.Stat(destFilePath)
assert.NoError(t, err, "endpoints.json should be created in the mocked directory")

// Verify the content of the file matches core.DefaultEndpoints
data, err := os.ReadFile(destFilePath)
assert.NoError(t, err, "File should be readable")

var importedEndpoints []string
err = json.Unmarshal(data, &importedEndpoints)
assert.NoError(t, err, "JSON should be valid")
assert.Equal(t, core.DefaultEndpoints, importedEndpoints, "Imported endpoints should match the defaults")

// Delete the file after verification
err = os.Remove(destFilePath)
assert.NoError(t, err, "endpoints.json should be deleted successfully")

// Verify logs
log.Infof("Default endpoints successfully imported to %s", destFilePath)
pathMock.AssertExpectations(t)

// Confirm the file no longer exists
_, err = os.Stat(destFilePath)
assert.Error(t, err, "endpoints.json should not exist after deletion")
assert.True(t, os.IsNotExist(err), "Error should indicate file does not exist")
}
1 change: 1 addition & 0 deletions cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type UtilsCmdInterface interface {
CheckToDoResetDispute(rpcParameters RPC.RPCParameters, txnOpts *bind.TransactOpts, epoch uint32, sortedValues []*big.Int)
InitJobAndCollectionCache(rpcParameters RPC.RPCParameters) (*cache.JobsCache, *cache.CollectionsCache, *big.Int, error)
BatchGetStakeSnapshotCalls(rpcParameters RPC.RPCParameters, epoch uint32, numberOfStakers uint32) ([]*big.Int, error)
ExecuteImportEndpoints()
}

type TransactionInterface interface {
Expand Down
5 changes: 5 additions & 0 deletions cmd/mocks/utils_cmd_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions core/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core

var DefaultEndpoints = []string{
"https://andromeda02.skale.prod.chorus1.net:10072",
"https://skale-figment-9.skale.figment.io:10008",
"https://skale-figment-2.skale.figment.io:10072",
"https://node03.skale.prod.chorus1.net:10072",
"https://skale.tuku.dev:10136",
"https://signia-node-8.skale.bdnodes.net:10072",
"https://skale.infstones.io:10136",
"https://skale-figment-8.skale.figment.io:10200",
"https://node05.skale.prod.chorus1.net:10136",
"https://block-node-5.skale.bdnodes.net:10008",
"https://skale-figment-11.skale.figment.io:10072",
"https://skale-jupiter-2.skale.figment.io:10072",
"https://signia-node-5.skale.bdnodes.net:10328",
"https://block-node-8.skale.bdnodes.net:10008",
"https://skale-mainnet-1a.stakin-nodes.com:10072",
"https://skale2.anonstake.com:10008",
}

0 comments on commit 2aa58b2

Please sign in to comment.