Skip to content

Commit

Permalink
rpc update chains call
Browse files Browse the repository at this point in the history
rpc hosted blockchain call
  • Loading branch information
Otto V authored and oten91 committed Apr 6, 2022
1 parent a51251d commit c7e6324
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/cmd/cli/queryUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var (
GetSupplyPath,
GetAllParamsPath,
GetParamPath,
GetStopPath string
GetStopPath,
GetQueryChains string
)

func init() {
Expand Down Expand Up @@ -100,6 +101,8 @@ func init() {
GetParamPath = route.Path
case "Stop":
GetStopPath = route.Path
case "QueryChains":
GetQueryChains = route.Path
default:
continue
}
Expand Down
34 changes: 34 additions & 0 deletions app/cmd/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
nodesTypes "github.com/pokt-network/pocket-core/x/nodes/types"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -86,6 +87,39 @@ func Relay(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}

// UpdateChains
func UpdateChains(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
value := r.URL.Query().Get("authtoken")
if value == app.AuthToken.Value {
var hostedChainsSlice []types.HostedBlockchain
if err := PopModel(w, r, ps, &hostedChainsSlice); err != nil {
WriteErrorResponse(w, 400, err.Error())
return
}
m := make(map[string]types.HostedBlockchain)
for _, chain := range hostedChainsSlice {
if err := nodesTypes.ValidateNetworkIdentifier(chain.ID); err != nil {
WriteErrorResponse(w, 400, fmt.Sprintf("invalid ID: %s in network identifier in json", chain.ID))
return
}
m[chain.ID] = chain
}
result, err := app.PCA.SetHostedChains(m)
if err != nil {
WriteErrorResponse(w, 400, err.Error())
} else {
j, er := json.Marshal(result)
if er != nil {
WriteErrorResponse(w, 400, er.Error())
return
}
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}
} else {
WriteErrorResponse(w, 401, "wrong authtoken "+value)
}
}

// Stop
func Stop(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
value := r.URL.Query().Get("authtoken")
Expand Down
14 changes: 14 additions & 0 deletions app/cmd/rpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,20 @@ func SecondUpgrade(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}

func Chains(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
res, err := app.PCA.QueryHostedChains()
if err != nil {
WriteErrorResponse(w, 400, err.Error())
return
}
j, err := app.Codec().MarshalJSON(res)
if err != nil {
WriteErrorResponse(w, 400, err.Error())
return
}
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}

func NodeParams(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var params = HeightParams{Height: 0}
if err := PopModel(w, r, ps, &params); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions app/cmd/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func GetRoutes() Routes {
Route{Name: "QueryTX", Method: "POST", Path: "/v1/query/tx", HandlerFunc: Tx},
Route{Name: "QueryUpgrade", Method: "POST", Path: "/v1/query/upgrade", HandlerFunc: Upgrade},
Route{Name: "QuerySigningInfo", Method: "POST", Path: "/v1/query/signinginfo", HandlerFunc: SigningInfo},
Route{Name: "QueryChains", Method: "POST", Path: "/v1/query/chains", HandlerFunc: Chains},
Route{Name: "UpdateChains", Method: "POST", Path: "/v1/private/updatechains", HandlerFunc: UpdateChains},
}
return routes
}
Expand Down
2 changes: 1 addition & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func getTMClient() client.Client {
func HotReloadChains(chains *types.HostedBlockchains) {
go func() {
for {
time.Sleep(time.Second * 5)
time.Sleep(time.Minute * 1)
// create the chains path
var chainsPath = GlobalConfig.PocketConfig.DataDir + FS + sdk.ConfigDirName + FS + GlobalConfig.PocketConfig.ChainsName
// if file exists open, else create and open
Expand Down
8 changes: 8 additions & 0 deletions app/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func (app PocketCoreApp) QueryNodeParams(height int64) (res nodesTypes.Params, e
return app.nodesKeeper.GetParams(ctx), nil
}

func (app PocketCoreApp) QueryHostedChains() (res *pocketTypes.HostedBlockchains, err error) {
return app.pocketKeeper.GetHostedBlockchains(), nil
}

func (app PocketCoreApp) SetHostedChains(req map[string]pocketTypes.HostedBlockchain) (res *pocketTypes.HostedBlockchains, err error) {
return app.pocketKeeper.SetHostedBlockchains(req), nil
}

func (app PocketCoreApp) QuerySigningInfo(height int64, addr string) (res nodesTypes.ValidatorSigningInfo, err error) {
a, err := sdk.AddressFromHex(addr)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions x/pocketcore/keeper/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ import (
func (k Keeper) GetHostedBlockchains() *pc.HostedBlockchains {
return k.hostedBlockchains
}

// "GetHostedBlockchains" returns the non native chains hosted locally on this node
func (k Keeper) SetHostedBlockchains(m map[string]pc.HostedBlockchain) *pc.HostedBlockchains {
k.hostedBlockchains.L.Lock()
k.hostedBlockchains.M = m
k.hostedBlockchains.L.Unlock()
return k.hostedBlockchains
}

0 comments on commit c7e6324

Please sign in to comment.