Skip to content

Commit

Permalink
-move chain query/update to private.
Browse files Browse the repository at this point in the history
-Config for hot reload
-change hot reload interval from 5 seconds to 1 minute
-Manual reload via rpc and hot reload should not be used together.(if hot reload is enabled turn off rpc manual reload route)
  • Loading branch information
Otto V authored and oten91 committed Apr 6, 2022
1 parent c7e6324 commit a762dc1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func start(cmd *cobra.Command, args []string) {
genesisType = app.TestnetGenesisType
}
tmNode := app.InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL, keybase, genesisType, useCache)
go rpc.StartRPC(app.GlobalConfig.PocketConfig.RPCPort, app.GlobalConfig.PocketConfig.RPCTimeout, simulateRelay, profileApp, allBlockTxs)
go rpc.StartRPC(app.GlobalConfig.PocketConfig.RPCPort, app.GlobalConfig.PocketConfig.RPCTimeout, simulateRelay, profileApp, allBlockTxs, app.GlobalConfig.PocketConfig.ChainsHotReload)
// trap kill signals (2,3,15,9)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel,
Expand Down
25 changes: 15 additions & 10 deletions app/cmd/rpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,22 @@ func SecondUpgrade(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
}

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
value := r.URL.Query().Get("authtoken")
if value == app.AuthToken.Value {
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)
} else {
WriteErrorResponse(w, 401, "wrong authtoken "+value)
}
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}

func NodeParams(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand Down
10 changes: 7 additions & 3 deletions app/cmd/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

var APIVersion = app.AppVersion

func StartRPC(port string, timeout int64, simulation, debug, allBlockTxs bool) {
func StartRPC(port string, timeout int64, simulation, debug, allBlockTxs, hotReloadChains bool) {
routes := GetRoutes()
if simulation {
simRoute := Route{Name: "SimulateRequest", Method: "POST", Path: "/v1/client/sim", HandlerFunc: SimRequest}
Expand All @@ -45,6 +45,11 @@ func StartRPC(port string, timeout int64, simulation, debug, allBlockTxs bool) {
routes = append(routes, Route{Name: "QueryAllBlockTxs", Method: "POST", Path: "/v1/query/allblocktxs", HandlerFunc: AllBlockTxs})
}

//if hot reload is not enabled, enable manual reload.
if !hotReloadChains {
routes = append(routes, Route{Name: "UpdateChains", Method: "POST", Path: "/v1/private/updatechains", HandlerFunc: UpdateChains})
}

srv := &http.Server{
ReadTimeout: 30 * time.Second,
ReadHeaderTimeout: 20 * time.Second,
Expand Down Expand Up @@ -115,8 +120,7 @@ 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},
Route{Name: "QueryChains", Method: "POST", Path: "/v1/private/chains", HandlerFunc: Chains},
}
return routes
}
Expand Down
6 changes: 4 additions & 2 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL string, keyba
InitKeyfiles()
// get hosted blockchains
chains := NewHostedChains(false)
// hot reload chains
HotReloadChains(chains)
if GlobalConfig.PocketConfig.ChainsHotReload {
// hot reload chains
HotReloadChains(chains)
}
// create logger
logger := InitLogger()
// init cache
Expand Down
8 changes: 4 additions & 4 deletions app/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ 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) QueryHostedChains() (res map[string]pocketTypes.HostedBlockchain, err error) {
return app.pocketKeeper.GetHostedBlockchains().M, nil
}

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

func (app PocketCoreApp) QuerySigningInfo(height int64, addr string) (res nodesTypes.ValidatorSigningInfo, err error) {
Expand Down
3 changes: 3 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type PocketConfig struct {
DisableTxEvents bool `json:"disable_tx_events"`
Cache bool `json:"-"`
IavlCacheSize int64 `json:"iavl_cache_size"`
ChainsHotReload bool `json:"chains_hot_reload"`
}

type Config struct {
Expand Down Expand Up @@ -95,6 +96,7 @@ const (
DefaultRelayErrors = true
AuthFileName = "auth.json"
DefaultIavlCacheSize = 5000000
DefaultChainHotReload = false
)

func DefaultConfig(dataDir string) Config {
Expand Down Expand Up @@ -126,6 +128,7 @@ func DefaultConfig(dataDir string) Config {
RelayErrors: DefaultRelayErrors,
DisableTxEvents: DefaultRPCDisableTransactionEvents,
IavlCacheSize: DefaultIavlCacheSize,
ChainsHotReload: DefaultChainHotReload,
},
}
c.TendermintConfig.LevelDBOptions = config.DefaultLevelDBOpts()
Expand Down
1 change: 0 additions & 1 deletion x/pocketcore/keeper/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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
Expand Down

0 comments on commit a762dc1

Please sign in to comment.