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

EVM-720 Implement json output for all commands #1724

Merged
merged 11 commits into from
Jul 24, 2023
2 changes: 1 addition & 1 deletion command/json_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (jo *jsonOutput) WriteOutput() {

// WriteCommandResult implements OutputFormatter interface
func (jo *jsonOutput) WriteCommandResult(result CommandResult) {
_, _ = fmt.Fprintln(os.Stdout, result.GetOutput())
_, _ = fmt.Fprintln(os.Stdout, marshalJSONToString(result))
}

// WriteOutput implements OutputFormatter plus io.Writer interfaces
Expand Down
31 changes: 17 additions & 14 deletions command/rootchain/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}
}

rootchainCfg, supernetID, err := deployContracts(outputter, client,
rootchainCfg, supernetID, commandResults, err := deployContracts(outputter, client,
chainConfig.Params.ChainID, consensusCfg.InitialValidatorSet, cmd.Context())
if err != nil {
outputter.SetError(fmt.Errorf("failed to deploy rootchain contracts: %w", err))
Expand Down Expand Up @@ -378,31 +378,33 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

outputter.SetCommandResult(&helper.MessageResult{
outputter.WriteCommandResult(&helper.MessageResult{
Message: fmt.Sprintf("%s finished. All contracts are successfully deployed and initialized.",
contractsDeploymentTitle),
})
outputter.SetCommandResult(command.Results(commandResults))
Stefan-Ethernal marked this conversation as resolved.
Show resolved Hide resolved
}

// deployContracts deploys and initializes rootchain smart contracts
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client, chainID int64,
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (*polybft.RootchainConfig, int64, error) {
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (
*polybft.RootchainConfig, int64, []command.CommandResult, error) {
jelacamarko marked this conversation as resolved.
Show resolved Hide resolved
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client), txrelayer.WithWriter(outputter))
if err != nil {
return nil, 0, fmt.Errorf("failed to initialize tx relayer: %w", err)
return nil, 0, nil, fmt.Errorf("failed to initialize tx relayer: %w", err)
}

deployerKey, err := helper.DecodePrivateKey(params.deployerKey)
if err != nil {
return nil, 0, fmt.Errorf("failed to initialize deployer key: %w", err)
return nil, 0, nil, fmt.Errorf("failed to initialize deployer key: %w", err)
}

if params.isTestMode {
deployerAddr := deployerKey.Address()
txn := &ethgo.Transaction{To: &deployerAddr, Value: ethgo.Ether(1)}

if _, err = txRelayer.SendTransactionLocal(txn); err != nil {
return nil, 0, err
return nil, 0, nil, err
}
}

Expand All @@ -425,7 +427,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
// use existing root chain ERC20 token
if err := populateExistingTokenAddr(client.Eth(),
params.rootERC20TokenAddr, rootERC20Name, rootchainConfig); err != nil {
return nil, 0, err
return nil, 0, nil, err
}
} else {
// deploy MockERC20 as a root chain root native token
Expand Down Expand Up @@ -501,6 +503,7 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,

g, ctx := errgroup.WithContext(cmdCtx)
results := make([]*deployContractResult, len(allContracts))
commandResults := make([]command.CommandResult, len(allContracts))

for i, contract := range allContracts {
i := i
Expand Down Expand Up @@ -546,18 +549,18 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
}
Stefan-Ethernal marked this conversation as resolved.
Show resolved Hide resolved
}

return nil, 0, err
return nil, 0, nil, err
}

for _, result := range results {
for i, result := range results {
populatorFn, ok := metadataPopulatorMap[result.Name]
if !ok {
return nil, 0, fmt.Errorf("rootchain metadata populator not registered for contract '%s'", result.Name)
return nil, 0, nil, fmt.Errorf("rootchain metadata populator not registered for contract '%s'", result.Name)
}

populatorFn(rootchainConfig, result.Address)

outputter.WriteCommandResult(result)
commandResults[i] = result
}

g, ctx = errgroup.WithContext(cmdCtx)
Expand All @@ -581,16 +584,16 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,
}

if err := g.Wait(); err != nil {
return nil, 0, err
return nil, 0, nil, err
}

// register supernets manager on stake manager
supernetID, err := registerChainOnStakeManager(txRelayer, rootchainConfig, deployerKey)
if err != nil {
return nil, 0, err
return nil, 0, nil, err
}

return rootchainConfig, supernetID, nil
return rootchainConfig, supernetID, commandResults, nil
}

// populateExistingTokenAddr checks whether given token is deployed on the provided address.
Expand Down
2 changes: 1 addition & 1 deletion command/rootchain/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestDeployContracts_NoPanics(t *testing.T) {
}

require.NotPanics(t, func() {
_, _, err = deployContracts(outputter, client, 1, []*validator.GenesisValidator{}, context.Background())
_, _, _, err = deployContracts(outputter, client, 1, []*validator.GenesisValidator{}, context.Background())
})
require.NoError(t, err)
}
10 changes: 5 additions & 5 deletions consensus/polybft/system_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
// ValidatorInfo is data transfer object which holds validator information,
// provided by smart contract
type ValidatorInfo struct {
Address ethgo.Address
Stake *big.Int
WithdrawableRewards *big.Int
IsActive bool
IsWhitelisted bool
Address ethgo.Address `json:"address"`
Stake *big.Int `json:"stake"`
WithdrawableRewards *big.Int `json:"withdrawableRewards"`
IsActive bool `json:"isActive"`
IsWhitelisted bool `json:"isWhitelisted"`
}

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