Skip to content

Commit

Permalink
Change validator info command
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Apr 24, 2023
1 parent 8585cf5 commit 2fe5c0b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 56 deletions.
3 changes: 2 additions & 1 deletion command/polybft/polybft_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/0xPolygon/polygon-edge/command/rootchain/registration"
"github.com/0xPolygon/polygon-edge/command/rootchain/staking"
"github.com/0xPolygon/polygon-edge/command/rootchain/supernet"
"github.com/0xPolygon/polygon-edge/command/rootchain/validators"
"github.com/0xPolygon/polygon-edge/command/rootchain/whitelist"
"github.com/0xPolygon/polygon-edge/command/sidechain/unstaking"
"github.com/0xPolygon/polygon-edge/command/sidechain/validators"

"github.com/0xPolygon/polygon-edge/command/sidechain/withdraw"
"github.com/spf13/cobra"
Expand All @@ -21,6 +21,7 @@ func GetCommand() *cobra.Command {
polybftCmd.AddCommand(
unstaking.GetCommand(),
withdraw.GetCommand(),
// rootchain (supernet manager) command that queries validator info
validators.GetCommand(),
// rootchain (supernet manager) whitelist validator
whitelist.GetCommand(),
Expand Down
47 changes: 46 additions & 1 deletion command/rootchain/helper/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package helper

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"

"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
polybftWallet "github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/txrelayer"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/umbracle/ethgo"
Expand Down Expand Up @@ -109,3 +114,43 @@ func GetECDSAKey(privateKey, accountDir, accountConfig string) (ethgo.Key, error

return polybftWallet.GetEcdsaFromSecret(secretsManager)
}

// GetValidatorInfo queries SupernetManager smart contract on root
// and retrieves validator info for given address
func GetValidatorInfo(validatorAddr, supernetManagerAddr ethgo.Address,
txRelayer txrelayer.TxRelayer) (*polybft.ValidatorInfo, error) {
getValidatorMethod := contractsapi.CustomSupernetManager.Abi.GetMethod("validators")

encode, err := getValidatorMethod.Encode([]interface{}{validatorAddr})
if err != nil {
return nil, err
}

response, err := txRelayer.Call(ethgo.Address(contracts.SystemCaller),
supernetManagerAddr, encode)
if err != nil {
return nil, err
}

byteResponse, err := hex.DecodeHex(response)
if err != nil {
return nil, fmt.Errorf("unable to decode hex response, %w", err)
}

decoded, err := getValidatorMethod.Outputs.Decode(byteResponse)
if err != nil {
return nil, err
}

decodedOutputsMap, ok := decoded.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("could not convert decoded outputs to map")
}

return &polybft.ValidatorInfo{
Address: validatorAddr.Address(),
Stake: decodedOutputsMap["stake"].(*big.Int), //nolint:forcetypeassert
Active: decodedOutputsMap["isActive"].(bool), //nolint:forcetypeassert
Whitelisted: decodedOutputsMap["isWhitelisted"].(bool), //nolint:forcetypeassert
}, nil
}
44 changes: 44 additions & 0 deletions command/rootchain/validators/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package validators

import (
"bytes"
"fmt"

"github.com/0xPolygon/polygon-edge/command/helper"
sidechainHelper "github.com/0xPolygon/polygon-edge/command/sidechain"
)

type validatorInfoParams struct {
accountDir string
accountConfig string
jsonRPC string
supernetManagerAddress string
}

func (v *validatorInfoParams) validateFlags() error {
return sidechainHelper.ValidateSecretFlags(v.accountDir, v.accountConfig)
}

type validatorsInfoResult struct {
address string
stake uint64
active bool
whitelisted bool
}

func (vr validatorsInfoResult) GetOutput() string {
var buffer bytes.Buffer

buffer.WriteString("\n[VALIDATOR INFO]\n")

vals := make([]string, 4)
vals[0] = fmt.Sprintf("Validator Address|%s", vr.address)
vals[1] = fmt.Sprintf("Stake|%v", vr.stake)
vals[2] = fmt.Sprintf("Is Whitelisted|%v", vr.whitelisted)
vals[3] = fmt.Sprintf("Is Active|%v", vr.active)

buffer.WriteString(helper.FormatKV(vals))
buffer.WriteString("\n")

return buffer.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
rootHelper "github.com/0xPolygon/polygon-edge/command/rootchain/helper"
sidechainHelper "github.com/0xPolygon/polygon-edge/command/sidechain"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
)

var (
Expand Down Expand Up @@ -44,6 +47,13 @@ func setFlags(cmd *cobra.Command) {
polybftsecrets.AccountConfigFlagDesc,
)

cmd.Flags().StringVar(
&params.supernetManagerAddress,
rootHelper.SupernetManagerFlag,
"",
rootHelper.SupernetManagerFlagDesc,
)

cmd.MarkFlagsMutuallyExclusive(polybftsecrets.AccountDirFlag, polybftsecrets.AccountConfigFlag)
}

Expand All @@ -68,19 +78,18 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

validatorAddr := validatorAccount.Ecdsa.Address()
supernetManagerAddr := ethgo.Address(types.StringToAddress(params.supernetManagerAddress))

validatorInfo, err := sidechainHelper.GetValidatorInfo(validatorAddr, txRelayer)
validatorInfo, err := rootHelper.GetValidatorInfo(validatorAddr, supernetManagerAddr, txRelayer)
if err != nil {
return fmt.Errorf("failed to get validator info for %s: %w", validatorAddr, err)
}

outputter.WriteCommandResult(&validatorsInfoResult{
address: validatorInfo.Address.String(),
stake: validatorInfo.Stake.Uint64(),
totalStake: validatorInfo.TotalStake.Uint64(),
commission: validatorInfo.Commission.Uint64(),
withdrawableRewards: validatorInfo.WithdrawableRewards.Uint64(),
active: validatorInfo.Active,
address: validatorInfo.Address.String(),
stake: validatorInfo.Stake.Uint64(),
active: validatorInfo.Active,
whitelisted: validatorInfo.Whitelisted,
})

return nil
Expand Down
3 changes: 3 additions & 0 deletions command/sidechain/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func GetAccountFromDir(accountDir string) (*wallet.Account, error) {
return GetAccount(accountDir, "")
}

//nolint:godox

// GetValidatorInfo queries ChildValidatorSet smart contract and retrieves validator info for given address
// TODO - @goran-ethernal depricate this function once we change e2e tests
func GetValidatorInfo(validatorAddr ethgo.Address, txRelayer txrelayer.TxRelayer) (*polybft.ValidatorInfo, error) {
getValidatorMethod := contractsapi.ChildValidatorSet.Abi.GetMethod("getValidator")

Expand Down
47 changes: 0 additions & 47 deletions command/sidechain/validators/params.go

This file was deleted.

6 changes: 6 additions & 0 deletions consensus/polybft/system_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import (
"github.com/umbracle/ethgo/contract"
)

//nolint:godox

// ValidatorInfo is data transfer object which holds validator information,
// provided by smart contract
// TODO - @goran-ethernal deprecate this struct once we change e2e tests
// we will instead use the contractsapi generated stub once we remove old
// ChildValidatorSet contract and its stubs
type ValidatorInfo struct {
Address ethgo.Address
Stake *big.Int
TotalStake *big.Int
Commission *big.Int
WithdrawableRewards *big.Int
Active bool
Whitelisted bool
}

// SystemState is an interface to interact with the consensus system contracts in the chain
Expand Down

0 comments on commit 2fe5c0b

Please sign in to comment.