Skip to content

Commit

Permalink
add ability to get aliases of a chain via admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
0xK4d1r committed Jan 19, 2021
1 parent 71d8dd7 commit 904d9fe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func (c *Client) AliasChain(chain, alias string) (bool, error) {
return res.Success, err
}

// GetAliasesOfChain ...
func (c *Client) GetAliasesOfChain(chain string) (*GetAliasesOfChainReply, error) {
res := &GetAliasesOfChainReply{}
err := c.requester.SendRequest("getAliasesOfChain", &AliasChainArgs{
Chain: chain,
}, res)
return res, err
}

// Stacktrace ...
func (c *Client) Stacktrace() (bool, error) {
res := &api.SuccessResponse{}
Expand Down
32 changes: 32 additions & 0 deletions api/admin/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package admin

import (
"errors"
"reflect"
"testing"

"github.com/ava-labs/avalanchego/api"
Expand Down Expand Up @@ -56,6 +57,9 @@ func (mc *mockClient) SendRequest(method string, params interface{}, reply inter
case *api.SuccessResponse:
response := mc.response.(api.SuccessResponse)
*p = response
case *GetAliasesOfChainReply:
response := mc.response.(*GetAliasesOfChainReply)
*p = *response
default:
panic("illegal type")
}
Expand Down Expand Up @@ -176,6 +180,34 @@ func TestAliasChain(t *testing.T) {
}
}

func TestGetAliasesOfChain(t *testing.T) {
t.Run("successful", func(t *testing.T) {
expectedReply := &GetAliasesOfChainReply{
Aliases: []string{"alias1", "alias2"},
}
mockClient := Client{requester: NewMockClient(expectedReply, nil)}

reply, err := mockClient.GetAliasesOfChain("chain")

if err != nil {
t.Fatalf("Unexepcted error: %s", err)
}
if !reflect.DeepEqual(expectedReply, reply) {
t.Fatalf("Expected response to be: %v, but found: %v", expectedReply, reply)
}
})

t.Run("failure", func(t *testing.T) {
mockClient := Client{requester: NewMockClient(&GetAliasesOfChainReply{}, errors.New("some error"))}

_, err := mockClient.GetAliasesOfChain("chain")

if err == nil {
t.Fatalf("Expected error but got no error.")
}
})
}

func TestStacktrace(t *testing.T) {
tests := GetSuccessResponseTests()

Expand Down
23 changes: 23 additions & 0 deletions api/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ func (service *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, reply *a
return service.httpServer.AddAliasesWithReadLock("bc/"+chainID.String(), "bc/"+args.Alias)
}

// GetAliasesOfChainArgs are the arguments for calling GetAliasesOfChain
type GetAliasesOfChainArgs struct {
Chain string `json:"chain"`
}

// GetAliasesOfChainArgs are the arguments for calling GetAliasesOfChain
type GetAliasesOfChainReply struct {
Aliases []string `json:"aliases"`
}

// GetAliasesOfChain returns the all aliases of a chain
func (service *Admin) GetAliasesOfChain(_ *http.Request, args *GetAliasesOfChainArgs, reply *GetAliasesOfChainReply) error {
service.log.Info("Admin: GetAliasesOfChain called with Chain: %s", args.Chain)

chainID, err := service.chainManager.Lookup(args.Chain)
if err != nil {
return err
}

reply.Aliases = service.chainManager.Aliases(chainID)
return nil
}

// Stacktrace returns the current global stacktrace
func (service *Admin) Stacktrace(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: Stacktrace called")
Expand Down

0 comments on commit 904d9fe

Please sign in to comment.