forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IBFT] Introduce quorum calculation switch (0xPolygon#549)
* This PR introduces a flag for specifying the block number at which to use a QuorumOptimal calculation for a valid number of votes in a particular ValidatorSet. (see PR 0xPolygon#513 and issue 0xPolygon#547 )
- Loading branch information
Showing
10 changed files
with
325 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package quorum | ||
|
||
import ( | ||
"fmt" | ||
"github.com/0xPolygon/polygon-edge/command" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func GetCommand() *cobra.Command { | ||
ibftQuorumCmd := &cobra.Command{ | ||
Use: "quorum", | ||
Short: "Specify the block number after which quorum optimal will be used for reaching consensus", | ||
PreRunE: runPreRun, | ||
Run: runCommand, | ||
} | ||
|
||
setFlags(ibftQuorumCmd) | ||
setRequiredFlags(ibftQuorumCmd) | ||
|
||
return ibftQuorumCmd | ||
} | ||
|
||
func setFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringVar( | ||
¶ms.genesisPath, | ||
chainFlag, | ||
fmt.Sprintf("./%s", command.DefaultGenesisFileName), | ||
"the genesis file to update", | ||
) | ||
|
||
cmd.Flags().Uint64Var( | ||
¶ms.from, | ||
fromFlag, | ||
0, | ||
"the height to switch the quorum calculation", | ||
) | ||
} | ||
|
||
func setRequiredFlags(cmd *cobra.Command) { | ||
for _, requiredFlag := range params.getRequiredFlags() { | ||
_ = cmd.MarkFlagRequired(requiredFlag) | ||
} | ||
} | ||
|
||
func runPreRun(_ *cobra.Command, _ []string) error { | ||
return params.initRawParams() | ||
} | ||
|
||
func runCommand(cmd *cobra.Command, _ []string) { | ||
outputter := command.InitializeOutputter(cmd) | ||
defer outputter.WriteOutput() | ||
|
||
if err := params.updateGenesisConfig(); err != nil { | ||
outputter.SetError(err) | ||
|
||
return | ||
} | ||
|
||
if err := params.overrideGenesisConfig(); err != nil { | ||
outputter.SetError(err) | ||
|
||
return | ||
} | ||
|
||
outputter.SetCommandResult(params.getResult()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package quorum | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/0xPolygon/polygon-edge/chain" | ||
"github.com/0xPolygon/polygon-edge/command" | ||
"github.com/0xPolygon/polygon-edge/command/helper" | ||
"github.com/0xPolygon/polygon-edge/helper/common" | ||
"os" | ||
) | ||
|
||
const ( | ||
fromFlag = "from" | ||
chainFlag = "chain" | ||
) | ||
|
||
var ( | ||
params = &quorumParams{} | ||
) | ||
|
||
type quorumParams struct { | ||
genesisConfig *chain.Chain | ||
from uint64 | ||
genesisPath string | ||
} | ||
|
||
func (p *quorumParams) initChain() error { | ||
cc, err := chain.Import(p.genesisPath) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed to load chain config from %s: %w", | ||
p.genesisPath, | ||
err, | ||
) | ||
} | ||
|
||
p.genesisConfig = cc | ||
|
||
return nil | ||
} | ||
|
||
func (p *quorumParams) initRawParams() error { | ||
return p.initChain() | ||
} | ||
|
||
func (p *quorumParams) getRequiredFlags() []string { | ||
return []string{ | ||
fromFlag, | ||
} | ||
} | ||
|
||
func (p *quorumParams) updateGenesisConfig() error { | ||
return appendIBFTQuorum( | ||
p.genesisConfig, | ||
p.from, | ||
) | ||
} | ||
|
||
func (p *quorumParams) overrideGenesisConfig() error { | ||
// Remove the current genesis configuration from disk | ||
if err := os.Remove(p.genesisPath); err != nil { | ||
return err | ||
} | ||
|
||
// Save the new genesis configuration | ||
if err := helper.WriteGenesisConfigToDisk( | ||
p.genesisConfig, | ||
p.genesisPath, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *quorumParams) getResult() command.CommandResult { | ||
return &IBFTQuorumResult{ | ||
Chain: p.genesisPath, | ||
From: common.JSONNumber{Value: p.from}, | ||
} | ||
} | ||
|
||
func appendIBFTQuorum( | ||
cc *chain.Chain, | ||
from uint64, | ||
) error { | ||
ibftConfig, ok := cc.Params.Engine["ibft"].(map[string]interface{}) | ||
if !ok { | ||
return errors.New(`"ibft" setting doesn't exist in "engine" of genesis.json'`) | ||
} | ||
|
||
ibftConfig["quorumSizeBlockNum"] = from | ||
|
||
cc.Params.Engine["ibft"] = ibftConfig | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package quorum | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/0xPolygon/polygon-edge/command/helper" | ||
"github.com/0xPolygon/polygon-edge/helper/common" | ||
) | ||
|
||
type IBFTQuorumResult struct { | ||
Chain string `json:"chain"` | ||
From common.JSONNumber `json:"from"` | ||
} | ||
|
||
func (r *IBFTQuorumResult) GetOutput() string { | ||
var buffer bytes.Buffer | ||
|
||
buffer.WriteString("\n[NEW IBFT QUORUM START]\n") | ||
|
||
outputs := []string{ | ||
fmt.Sprintf("Chain|%s", r.Chain), | ||
fmt.Sprintf("From|%d", r.From.Value), | ||
} | ||
|
||
buffer.WriteString(helper.FormatKV(outputs)) | ||
buffer.WriteString("\n") | ||
|
||
return buffer.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.