Merge pull request #100 from ava-labs/backwards-compatible-api

Backwards compatible api
This commit is contained in:
Stephen Buttolph 2020-06-23 17:48:36 -04:00 committed by GitHub
commit 3609a73073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 2 deletions

View File

@ -10,35 +10,129 @@ import (
"github.com/ava-labs/gecko/api" "github.com/ava-labs/gecko/api"
"github.com/ava-labs/gecko/chains" "github.com/ava-labs/gecko/chains"
"github.com/ava-labs/gecko/genesis"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/network" "github.com/ava-labs/gecko/network"
"github.com/ava-labs/gecko/snow/engine/common" "github.com/ava-labs/gecko/snow/engine/common"
"github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/logging"
"github.com/ava-labs/gecko/version"
cjson "github.com/ava-labs/gecko/utils/json" cjson "github.com/ava-labs/gecko/utils/json"
) )
// Admin is the API service for node admin management // Admin is the API service for node admin management
type Admin struct { type Admin struct {
version version.Version
nodeID ids.ShortID
networkID uint32
log logging.Logger log logging.Logger
networking network.Network
performance Performance performance Performance
chainManager chains.Manager chainManager chains.Manager
httpServer *api.Server httpServer *api.Server
} }
// NewService returns a new admin API service // NewService returns a new admin API service
func NewService(log logging.Logger, chainManager chains.Manager, peers network.Network, httpServer *api.Server) *common.HTTPHandler { func NewService(version version.Version, nodeID ids.ShortID, networkID uint32, log logging.Logger, chainManager chains.Manager, peers network.Network, httpServer *api.Server) *common.HTTPHandler {
newServer := rpc.NewServer() newServer := rpc.NewServer()
codec := cjson.NewCodec() codec := cjson.NewCodec()
newServer.RegisterCodec(codec, "application/json") newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8") newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
newServer.RegisterService(&Admin{ newServer.RegisterService(&Admin{
version: version,
nodeID: nodeID,
networkID: networkID,
log: log, log: log,
chainManager: chainManager, chainManager: chainManager,
networking: peers,
httpServer: httpServer, httpServer: httpServer,
}, "admin") }, "admin")
return &common.HTTPHandler{Handler: newServer} return &common.HTTPHandler{Handler: newServer}
} }
// GetNodeVersionReply are the results from calling GetNodeVersion
type GetNodeVersionReply struct {
Version string `json:"version"`
}
// GetNodeVersion returns the version this node is running
func (service *Admin) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error {
service.log.Info("Admin: GetNodeVersion called")
reply.Version = service.version.String()
return nil
}
// GetNodeIDReply are the results from calling GetNodeID
type GetNodeIDReply struct {
NodeID ids.ShortID `json:"nodeID"`
}
// GetNodeID returns the node ID of this node
func (service *Admin) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error {
service.log.Info("Admin: GetNodeID called")
reply.NodeID = service.nodeID
return nil
}
// GetNetworkIDReply are the results from calling GetNetworkID
type GetNetworkIDReply struct {
NetworkID cjson.Uint32 `json:"networkID"`
}
// GetNetworkID returns the network ID this node is running on
func (service *Admin) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error {
service.log.Info("Admin: GetNetworkID called")
reply.NetworkID = cjson.Uint32(service.networkID)
return nil
}
// GetNetworkNameReply is the result from calling GetNetworkName
type GetNetworkNameReply struct {
NetworkName string `json:"networkName"`
}
// GetNetworkName returns the network name this node is running on
func (service *Admin) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error {
service.log.Info("Admin: GetNetworkName called")
reply.NetworkName = genesis.NetworkName(service.networkID)
return nil
}
// GetBlockchainIDArgs are the arguments for calling GetBlockchainID
type GetBlockchainIDArgs struct {
Alias string `json:"alias"`
}
// GetBlockchainIDReply are the results from calling GetBlockchainID
type GetBlockchainIDReply struct {
BlockchainID string `json:"blockchainID"`
}
// GetBlockchainID returns the blockchain ID that resolves the alias that was supplied
func (service *Admin) GetBlockchainID(_ *http.Request, args *GetBlockchainIDArgs, reply *GetBlockchainIDReply) error {
service.log.Info("Admin: GetBlockchainID called")
bID, err := service.chainManager.Lookup(args.Alias)
reply.BlockchainID = bID.String()
return err
}
// PeersReply are the results from calling Peers
type PeersReply struct {
Peers []network.PeerID `json:"peers"`
}
// Peers returns the list of current validators
func (service *Admin) Peers(_ *http.Request, _ *struct{}, reply *PeersReply) error {
service.log.Info("Admin: Peers called")
reply.Peers = service.networking.Peers()
return nil
}
// StartCPUProfilerReply are the results from calling StartCPUProfiler // StartCPUProfilerReply are the results from calling StartCPUProfiler
type StartCPUProfilerReply struct { type StartCPUProfilerReply struct {
Success bool `json:"success"` Success bool `json:"success"`

View File

@ -462,7 +462,7 @@ func (n *Node) initMetricsAPI() {
func (n *Node) initAdminAPI() { func (n *Node) initAdminAPI() {
if n.Config.AdminAPIEnabled { if n.Config.AdminAPIEnabled {
n.Log.Info("initializing Admin API") n.Log.Info("initializing Admin API")
service := admin.NewService(n.Log, n.chainManager, n.Net, &n.APIServer) service := admin.NewService(Version, n.ID, n.Config.NetworkID, n.Log, n.chainManager, n.Net, &n.APIServer)
n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog) n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog)
} }
} }