Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted validator related structures into consensus/polybft/validator package #1493

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/server"
Expand Down Expand Up @@ -202,7 +203,7 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
}
}

validatorMetadata := make([]*polybft.ValidatorMetadata, len(initialValidators))
validatorMetadata := make([]*validator.ValidatorMetadata, len(initialValidators))

for i, validator := range initialValidators {
// create validator metadata instance
Expand Down Expand Up @@ -420,8 +421,8 @@ func (p *genesisParams) deployContracts(totalStake *big.Int,
}

// generateExtraDataPolyBft populates Extra with specific fields required for polybft consensus protocol
func generateExtraDataPolyBft(validators []*polybft.ValidatorMetadata) ([]byte, error) {
delta := &polybft.ValidatorSetDelta{
func generateExtraDataPolyBft(validators []*validator.ValidatorMetadata) ([]byte, error) {
delta := &validator.ValidatorSetDelta{
Added: validators,
Removed: bitmap.Bitmap{},
}
Expand All @@ -433,7 +434,7 @@ func generateExtraDataPolyBft(validators []*polybft.ValidatorMetadata) ([]byte,

// getValidatorAccounts gathers validator accounts info either from CLI or from provided local storage
func (p *genesisParams) getValidatorAccounts(
premineBalances map[types.Address]*premineInfo) ([]*polybft.Validator, error) {
premineBalances map[types.Address]*premineInfo) ([]*validator.GenesisValidator, error) {
// populate validators premine info
stakeMap := make(map[types.Address]*premineInfo, len(p.stakes))

Expand All @@ -447,9 +448,9 @@ func (p *genesisParams) getValidatorAccounts(
}

if len(p.validators) > 0 {
validators := make([]*polybft.Validator, len(p.validators))
for i, validator := range p.validators {
parts := strings.Split(validator, ":")
validators := make([]*validator.GenesisValidator, len(p.validators))
for i, val := range p.validators {
parts := strings.Split(val, ":")
if len(parts) != 3 {
return nil, fmt.Errorf("expected 4 parts provided in the following format "+
"<P2P multi address:ECDSA address:public BLS key>, but got %d part(s)",
Expand All @@ -471,7 +472,7 @@ func (p *genesisParams) getValidatorAccounts(
}

addr := types.StringToAddress(trimmedAddress)
validators[i] = &polybft.Validator{
validators[i] = &validator.GenesisValidator{
MultiAddr: parts[0],
Address: addr,
BlsKey: trimmedBLSKey,
Expand Down
8 changes: 4 additions & 4 deletions command/genesis/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/secrets/helper"
Expand Down Expand Up @@ -176,13 +176,13 @@ func GetValidatorKeyFiles(rootDir, filePrefix string) ([]string, error) {
}

// ReadValidatorsByPrefix reads validators secrets on a given root directory and with given folder prefix
func ReadValidatorsByPrefix(dir, prefix string) ([]*polybft.Validator, error) {
func ReadValidatorsByPrefix(dir, prefix string) ([]*validator.GenesisValidator, error) {
validatorKeyFiles, err := GetValidatorKeyFiles(dir, prefix)
if err != nil {
return nil, err
}

validators := make([]*polybft.Validator, len(validatorKeyFiles))
validators := make([]*validator.GenesisValidator, len(validatorKeyFiles))

for i, file := range validatorKeyFiles {
path := filepath.Join(dir, file)
Expand All @@ -192,7 +192,7 @@ func ReadValidatorsByPrefix(dir, prefix string) ([]*polybft.Validator, error) {
return nil, err
}

validators[i] = &polybft.Validator{
validators[i] = &validator.GenesisValidator{
Address: types.Address(account.Ecdsa.Address()),
BlsPrivateKey: account.Bls,
BlsKey: hex.EncodeToString(account.Bls.PublicKey().Marshal()),
Expand Down
21 changes: 11 additions & 10 deletions command/rootchain/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -302,7 +303,7 @@ func runCommand(cmd *cobra.Command, _ []string) {

// deployContracts deploys and initializes rootchain smart contracts
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
initialValidators []*polybft.Validator, cmdCtx context.Context) (*polybft.RootchainConfig, int64, error) {
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (*polybft.RootchainConfig, int64, error) {
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client))
if err != nil {
return nil, 0, fmt.Errorf("failed to initialize tx relayer: %w", err)
Expand Down Expand Up @@ -600,7 +601,7 @@ func initializeCheckpointManager(
cmdOutput command.OutputFormatter,
txRelayer txrelayer.TxRelayer,
chainID int64,
validators []*polybft.Validator,
validators []*validator.GenesisValidator,
rootchainCfg *polybft.RootchainConfig,
deployerKey ethgo.Key) error {
validatorSet, err := validatorSetToABISlice(cmdOutput, validators)
Expand Down Expand Up @@ -817,27 +818,27 @@ func sendTransaction(txRelayer txrelayer.TxRelayer, addr ethgo.Address, input []
// validatorSetToABISlice converts given validators to generic map
// which is used for ABI encoding validator set being sent to the rootchain contract
func validatorSetToABISlice(o command.OutputFormatter,
validators []*polybft.Validator) ([]*contractsapi.Validator, error) {
accSet := make(polybft.AccountSet, len(validators))
validators []*validator.GenesisValidator) ([]*contractsapi.Validator, error) {
accSet := make(validator.AccountSet, len(validators))

if _, err := o.Write([]byte(fmt.Sprintf("%s [VALIDATORS]\n", contractsDeploymentTitle))); err != nil {
return nil, err
}

for i, validator := range validators {
if _, err := o.Write([]byte(fmt.Sprintf("%v\n", validator))); err != nil {
for i, val := range validators {
if _, err := o.Write([]byte(fmt.Sprintf("%v\n", val))); err != nil {
return nil, err
}

blsKey, err := validator.UnmarshalBLSPublicKey()
blsKey, err := val.UnmarshalBLSPublicKey()
if err != nil {
return nil, err
}

accSet[i] = &polybft.ValidatorMetadata{
Address: validator.Address,
accSet[i] = &validator.ValidatorMetadata{
Address: val.Address,
BlsKey: blsKey,
VotingPower: new(big.Int).Set(validator.Stake),
VotingPower: new(big.Int).Set(val.Stake),
}
}

Expand Down
4 changes: 2 additions & 2 deletions command/rootchain/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/types"
)

Expand All @@ -38,7 +38,7 @@ func TestDeployContracts_NoPanics(t *testing.T) {
outputter := command.InitializeOutputter(GetCommand())

require.NotPanics(t, func() {
_, _, err = deployContracts(outputter, client, []*polybft.Validator{}, context.Background())
_, _, err = deployContracts(outputter, client, []*validator.GenesisValidator{}, context.Background())
})
require.NoError(t, err)
}
5 changes: 3 additions & 2 deletions consensus/polybft/checkpoint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/merkle-tree"
Expand Down Expand Up @@ -196,7 +197,7 @@ func (c *checkpointManager) encodeAndSendCheckpoint(txn *ethgo.Transaction,
header *types.Header, extra *Extra, isEndOfEpoch bool) error {
c.logger.Debug("send checkpoint txn...", "block number", header.Number)

nextEpochValidators := AccountSet{}
nextEpochValidators := validator.AccountSet{}

if isEndOfEpoch {
var err error
Expand Down Expand Up @@ -232,7 +233,7 @@ func (c *checkpointManager) encodeAndSendCheckpoint(txn *ethgo.Transaction,

// abiEncodeCheckpointBlock encodes checkpoint data into ABI format for a given header
func (c *checkpointManager) abiEncodeCheckpointBlock(blockNumber uint64, blockHash types.Hash, extra *Extra,
nextValidators AccountSet) ([]byte, error) {
nextValidators validator.AccountSet) ([]byte, error) {
aggs, err := bls.UnmarshalSignature(extra.Committed.AggregatedSignature)
if err != nil {
return nil, err
Expand Down
23 changes: 12 additions & 11 deletions consensus/polybft/checkpoint_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand All @@ -36,8 +37,8 @@ func TestCheckpointManager_SubmitCheckpoint(t *testing.T) {

var aliases = []string{"A", "B", "C", "D", "E"}

validators := newTestValidatorsWithAliases(t, aliases)
validatorsMetadata := validators.getPublicIdentities()
validators := validator.NewTestValidatorsWithAliases(t, aliases)
validatorsMetadata := validators.GetPublicIdentities()
txRelayerMock := newDummyTxRelayer(t)
txRelayerMock.On("Call", mock.Anything, mock.Anything, mock.Anything).
Return("2", error(nil)).
Expand All @@ -59,9 +60,9 @@ func TestCheckpointManager_SubmitCheckpoint(t *testing.T) {
signatures bls.Signatures
)

validators.iterAcct(aliases, func(t *testValidator) {
validators.IterAcct(aliases, func(t *validator.TestValidator) {
bitmap.Set(idx)
signatures = append(signatures, t.mustSign(dummyMsg, bls.DomainCheckpointManager))
signatures = append(signatures, t.MustSign(dummyMsg, bls.DomainCheckpointManager))
idx++
})

Expand Down Expand Up @@ -96,7 +97,7 @@ func TestCheckpointManager_SubmitCheckpoint(t *testing.T) {
blockchainMock := new(blockchainMock)
blockchainMock.On("GetHeaderByNumber", mock.Anything).Return(headersMap.getHeader)

validatorAcc := validators.getValidator("A")
validatorAcc := validators.GetValidator("A")
c := &checkpointManager{
key: wallet.NewEcdsaSigner(validatorAcc.Key()),
rootChainRelayer: txRelayerMock,
Expand All @@ -122,8 +123,8 @@ func TestCheckpointManager_abiEncodeCheckpointBlock(t *testing.T) {

const epochSize = uint64(10)

currentValidators := newTestValidatorsWithAliases(t, []string{"A", "B", "C", "D"})
nextValidators := newTestValidatorsWithAliases(t, []string{"E", "F", "G", "H"})
currentValidators := validator.NewTestValidatorsWithAliases(t, []string{"A", "B", "C", "D"})
nextValidators := validator.NewTestValidatorsWithAliases(t, []string{"E", "F", "G", "H"})
header := &types.Header{Number: 50}
checkpoint := &CheckpointData{
BlockRound: 1,
Expand All @@ -138,8 +139,8 @@ func TestCheckpointManager_abiEncodeCheckpointBlock(t *testing.T) {

var signatures bls.Signatures

currentValidators.iterAcct(nil, func(v *testValidator) {
signatures = append(signatures, v.mustSign(proposalHash, bls.DomainCheckpointManager))
currentValidators.IterAcct(nil, func(v *validator.TestValidator) {
signatures = append(signatures, v.MustSign(proposalHash, bls.DomainCheckpointManager))
bmp.Set(i)
i++
})
Expand All @@ -156,14 +157,14 @@ func TestCheckpointManager_abiEncodeCheckpointBlock(t *testing.T) {
header.ComputeHash()

backendMock := new(polybftBackendMock)
backendMock.On("GetValidators", mock.Anything, mock.Anything).Return(currentValidators.getPublicIdentities())
backendMock.On("GetValidators", mock.Anything, mock.Anything).Return(currentValidators.GetPublicIdentities())

c := &checkpointManager{
blockchain: &blockchainMock{},
consensusBackend: backendMock,
logger: hclog.NewNullLogger(),
}
checkpointDataEncoded, err := c.abiEncodeCheckpointBlock(header.Number, header.Hash, extra, nextValidators.getPublicIdentities())
checkpointDataEncoded, err := c.abiEncodeCheckpointBlock(header.Number, header.Hash, extra, nextValidators.GetPublicIdentities())
require.NoError(t, err)

submit := &contractsapi.SubmitCheckpointManagerFn{}
Expand Down
11 changes: 6 additions & 5 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/txrelayer"
Expand Down Expand Up @@ -54,7 +55,7 @@ type epochMetadata struct {
FirstBlockInEpoch uint64

// Validators is the set of validators for the epoch
Validators AccountSet
Validators validator.AccountSet
}

type guardedDataDTO struct {
Expand Down Expand Up @@ -359,7 +360,7 @@ func (c *consensusRuntime) FSM() error {
isEndOfSprint := slash || c.isFixedSizeOfSprintMet(pendingBlockNumber, epoch)
isEndOfEpoch := slash || c.isFixedSizeOfEpochMet(pendingBlockNumber, epoch)

valSet := NewValidatorSet(epoch.Validators, c.logger)
valSet := validator.NewValidatorSet(epoch.Validators, c.logger)

exitRootHash, err := c.checkpointManager.BuildEventRoot(epoch.Number)
if err != nil {
Expand Down Expand Up @@ -474,7 +475,7 @@ func (c *consensusRuntime) restartEpoch(header *types.Header) (*epochMetadata, e
SystemState: systemState,
NewEpochID: epochNumber,
FirstBlockOfEpoch: firstBlockInEpoch,
ValidatorSet: NewValidatorSet(validatorSet, c.logger),
ValidatorSet: validator.NewValidatorSet(validatorSet, c.logger),
}

if err := c.stateSyncManager.PostEpoch(reqObj); err != nil {
Expand Down Expand Up @@ -504,7 +505,7 @@ func (c *consensusRuntime) calculateCommitEpochInput(
epochID := epoch.Number
totalBlocks := int64(0)

getSealersForBlock := func(blockExtra *Extra, validators AccountSet) error {
getSealersForBlock := func(blockExtra *Extra, validators validator.AccountSet) error {
signers, err := validators.GetFilteredValidators(blockExtra.Parent.Bitmap)
if err != nil {
return err
Expand Down Expand Up @@ -1002,7 +1003,7 @@ func (c *consensusRuntime) getFirstBlockOfEpoch(epochNumber uint64, latestHeader

// getSealersForBlock checks who sealed a given block and updates the counter
func getSealersForBlock(sealersCounter map[types.Address]uint64,
blockExtra *Extra, validators AccountSet) error {
blockExtra *Extra, validators validator.AccountSet) error {
signers, err := validators.GetFilteredValidators(blockExtra.Parent.Bitmap)
if err != nil {
return err
Expand Down
Loading