Skip to content

Commit

Permalink
Change parameters for bor producer selection algorithm (#1208)
Browse files Browse the repository at this point in the history
* bor,helper: update seed and voting powers for producer selection

* bor: minor fixes

* bor: fix GetNextSpanSeed for span-id<2

* helper: (temp) set HF height for testing

* bor: (temp) change default params

* bor: add logs + fix query for next span

* bor: modify last sprint producer case

* bor: (temp) add logs for debugging

* bor,helper: modify approach to get seed

* bor: some cleanup

* bor: use pointer receivers + add some debug logs

* bor,common: fix storage of seed producer

* app,bor,helper: add UTs

* Reduce validators collusion chance

* Additional tests

* If no unique author found, return first one that is different from last author

* Change settings for testing

* Prevent taking seed in two consecutive spans from same author when there are other authors

* Enable at zero height for testing

* Add more logging

* When proposing span 1, take block 1

* Test for proposing span 1

* Fix tests

* Refactor

* Update params

* bor,helper: address TODOs

* bor: add comments for getBorBlockForSpanSeed

* helper: (temp) set HF height for testing

* bor,helper: set HF name and height for amoy and minor error logging changes in querier

* bor: fix lint

---------

Co-authored-by: Angel Valkov <[email protected]>
  • Loading branch information
Raneet10 and avalkov authored Nov 27, 2024
1 parent 7ab304c commit f6c0261
Show file tree
Hide file tree
Showing 17 changed files with 1,128 additions and 143 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func NewHeimdallApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
common.DefaultCodespace,
app.ChainKeeper,
app.StakingKeeper,
app.caller,
&app.caller,
)

app.ClerkKeeper = clerk.NewKeeper(
Expand Down
35 changes: 31 additions & 4 deletions bor/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,28 @@ func GetSpanList(cdc *codec.Codec) *cobra.Command {

// GetNextSpanSeed implements the next span seed.
func GetNextSpanSeed(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "next-span-seed",
Args: cobra.NoArgs,
Short: "show the next span seed",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
spanIDStr := viper.GetString(FlagSpanId)
if spanIDStr == "" {
return fmt.Errorf("span id cannot be empty")
}

spanID, err := strconv.ParseUint(spanIDStr, 10, 64)
if err != nil {
return err
}

seedQueryParams, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(spanID))
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), seedQueryParams)
if err != nil {

fmt.Println("Error while fetching the span seed")
Expand All @@ -247,6 +261,14 @@ func GetNextSpanSeed(cdc *codec.Codec) *cobra.Command {

},
}

cmd.Flags().String(FlagSpanId, "", "--span-id=<span-id>")

if err := cmd.MarkFlagRequired(FlagSpanId); err != nil {
cliLogger.Error("GetNextSpanSeed | MarkFlagRequired | FlagSpanId", "Error", err)
}

return cmd
}

// PostSendProposeSpanTx send propose span transaction
Expand Down Expand Up @@ -309,7 +331,12 @@ func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
return err
}

res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
seedQueryParams, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(spanID))
if err != nil {
return err
}

res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), seedQueryParams)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion bor/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func PostSendProposeSpanTx(cdc *codec.Codec) *cobra.Command {
return err
}

res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
seedQueryParams, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(spanID))
if err != nil {
return err
}

res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), seedQueryParams)
if err != nil {
return err
}
Expand Down
40 changes: 35 additions & 5 deletions bor/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,21 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bor/span/{id}", spanHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bor/latest-span", latestSpanHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bor/prepare-next-span", prepareNextSpanHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bor/next-span-seed", fetchNextSpanSeedHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bor/next-span-seed/{id}", fetchNextSpanSeedHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bor/params", paramsHandlerFn(cliCtx)).Methods("GET")
}

// swagger:route GET /bor/next-span-seed bor borNextSpanSeed
//swagger:parameters borCurrentSpanById
type borCurrentSpanById struct {

//Id number of the span
//required:true
//type:integer
//in:path
Id int `json:"id"`
}

// swagger:route GET /bor/next-span-seed/{id} bor borCurrentSpanById
// It returns the seed for the next span
// responses:
// 200: borNextSpanSeedResponse
Expand All @@ -153,7 +163,19 @@ func fetchNextSpanSeedHandlerFn(
return
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
vars := mux.Vars(r)

spanID, ok := rest.ParseUint64OrReturnBadRequest(w, vars["id"])
if !ok {
return
}

seedQueryParams, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(spanID))
if err != nil {
return
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), seedQueryParams)
if err != nil {
RestLogger.Error("Error while fetching next span seed ", "Error", err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand All @@ -165,7 +187,7 @@ func fetchNextSpanSeedHandlerFn(

// error if span seed found
if !hmRest.ReturnNotFoundIfNoContent(w, res, "NextSpanSeed not found") {
RestLogger.Error("NextSpanSeed not found ", "Error", err.Error())
RestLogger.Error("NextSpanSeed not found ", "Error", err)
return
}

Expand Down Expand Up @@ -458,8 +480,16 @@ func prepareNextSpanHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
// Fetching SelectedProducers
//

nextProducerBytes, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextProducers), nil)
query, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(spanID))
if err != nil {
fmt.Println("error while marshalling: ", err)
hmRest.WriteErrorResponse(w, http.StatusNoContent, errors.New("unable to marshal JSON").Error())
return
}

nextProducerBytes, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextProducers), query)
if err != nil {
fmt.Println("error while querying next producers: ", err)
hmRest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
Expand Down
7 changes: 6 additions & 1 deletion bor/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ func postProposeSpanHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

seedQueryParams, err := cliCtx.Codec.MarshalJSON(types.NewQuerySpanParams(req.ID))
if err != nil {
return
}

// fetch seed
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), nil)
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryNextSpanSeed), seedQueryParams)
if err != nil {
RestLogger.Error("Error while fetching next span seed ", "Error", err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand Down
3 changes: 3 additions & 0 deletions bor/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bor

var RollbackVotingPowers = rollbackVotingPowers
Loading

0 comments on commit f6c0261

Please sign in to comment.