Skip to content

Commit

Permalink
Merge pull request #503 from ashish10677/setDelegation-hotfix
Browse files Browse the repository at this point in the history
Set delegation hotfix
  • Loading branch information
SkandaBhat authored Jan 21, 2022
2 parents 6782f86 + dd61af4 commit d0efb9e
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 167 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ $ ./razor stakerInfo --stakerId 2
If you are a staker you can accept delegation from delegators and charge a commission from them.

```
$ ./razor setDelegation --address <address> --status <true_or_false>
$ ./razor setDelegation --address <address> --status <true_or_false> --commission <commission_percent>
```

Example:

```
$ ./razor setDelegation --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c --status true
$ ./razor setDelegation --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c --status true -c 20
```

### Update Commission
Expand Down
1 change: 1 addition & 0 deletions cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type utilsCmdInterface interface {
Create(string, UtilsStruct) (accounts.Account, error)
claimBounty(types.Configurations, *ethclient.Client, types.RedeemBountyInput, UtilsStruct) (common.Hash, error)
GetAmountInSRZRs(*ethclient.Client, string, bindings.StructsStaker, *big.Int, UtilsStruct) (*big.Int, error)
ExecuteUpdateCommission(client *ethclient.Client, input types.UpdateCommissionInput, utilsStruct UtilsStruct) error
}

type cryptoInterface interface {
Expand Down
6 changes: 6 additions & 0 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ var claimBountyMock func(types.Configurations, *ethclient.Client, types.RedeemBo

var GetAmountInSRZRsMock func(*ethclient.Client, string, bindings.StructsStaker, *big.Int, UtilsStruct) (*big.Int, error)

var ExecuteUpdateCommissionMock func(*ethclient.Client, types.UpdateCommissionInput, UtilsStruct) error

var UnstakeMock func(types.Configurations, *ethclient.Client, types.UnstakeInput, UtilsStruct) (types.TransactionOptions, error)

var withdrawFundsMock func(*ethclient.Client, types.Account, types.Configurations, uint32, UtilsStruct) (common.Hash, error)
Expand Down Expand Up @@ -994,6 +996,10 @@ func (utilsCmdMock UtilsCmdMock) GetAmountInSRZRs(client *ethclient.Client, addr
return GetAmountInSRZRsMock(client, address, staker, amount, utilsStruct)
}

func (utilsCmdMock UtilsCmdMock) ExecuteUpdateCommission(client *ethclient.Client, input types.UpdateCommissionInput, utilsStruct UtilsStruct) error {
return ExecuteUpdateCommissionMock(client, input, utilsStruct)
}

func (blockManagerMock BlockManagerMock) ClaimBlockReward(client *ethclient.Client, opts *bind.TransactOpts) (*Types.Transaction, error) {
return ClaimBlockRewardMock(client, opts)
}
Expand Down
26 changes: 21 additions & 5 deletions cmd/setDelegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Example:
}

func (utilsStruct UtilsStruct) SetDelegation(flagSet *pflag.FlagSet) error {

config, err := utilsStruct.razorUtils.GetConfigData(utilsStruct)
if err != nil {
log.Error("Error in getting config")
Expand All @@ -54,7 +53,10 @@ func (utilsStruct UtilsStruct) SetDelegation(flagSet *pflag.FlagSet) error {
log.Error("Error in parsing status to boolean")
return err
}

commission, err := utilsStruct.flagSetUtils.GetUint8Commission(flagSet)
if err != nil {
return err
}
client := utilsStruct.razorUtils.ConnectToClient(config.Provider)

stakerId, err := utilsStruct.razorUtils.GetStakerId(client, address)
Expand All @@ -68,6 +70,18 @@ func (utilsStruct UtilsStruct) SetDelegation(flagSet *pflag.FlagSet) error {
log.Error("Error in fetching staker info")
return err
}
if commission != 0 {
err = utilsStruct.cmdUtils.ExecuteUpdateCommission(client, types.UpdateCommissionInput{
StakerId: stakerId,
Address: address,
Password: password,
Commission: commission,
Config: config,
}, utilsStruct)
if err != nil {
return err
}
}

txnOpts := types.TransactionOptions{
Client: client,
Expand Down Expand Up @@ -108,14 +122,16 @@ func init() {
rootCmd.AddCommand(setDelegationCmd)

var (
Status string
Address string
Password string
Status string
Address string
Password string
Commission uint8
)

setDelegationCmd.Flags().StringVarP(&Status, "status", "s", "true", "true for accepting delegation and false for not accepting")
setDelegationCmd.Flags().StringVarP(&Address, "address", "a", "", "your account address")
setDelegationCmd.Flags().StringVarP(&Password, "password", "", "", "password path to protect the keystore")
setDelegationCmd.Flags().Uint8VarP(&Commission, "commission", "c", 0, "commission")

addrErr := setDelegationCmd.MarkFlagRequired("address")
utils.CheckError("Address error: ", addrErr)
Expand Down
66 changes: 66 additions & 0 deletions cmd/setDelegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func TestSetDelegation(t *testing.T) {
stakerIdErr error
staker bindings.StructsStaker
stakerErr error
commission uint8
commissionErr error
SetDelegationAcceptanceTxn *Types.Transaction
SetDelegationAcceptanceErr error
hash common.Hash
WaitForBlockCompletionStatus int
executeUpdateCommissionErr error
}
tests := []struct {
name string
Expand Down Expand Up @@ -247,6 +250,61 @@ func TestSetDelegation(t *testing.T) {
},
wantErr: errors.New("SetDelegationAcceptance error"),
},
{
name: "Test 11: When there is an error in fetching commission",
args: args{
config: config,
password: "test",
address: "0x000000000000000000000000000000000000dea1",
status: "true",
parseStatus: true,
stakerId: 1,
staker: bindings.StructsStaker{
AcceptDelegation: false,
},
commissionErr: errors.New("error in fetching commission"),
SetDelegationAcceptanceTxn: &Types.Transaction{},
WaitForBlockCompletionStatus: 1,
},
wantErr: errors.New("error in fetching commission"),
},
{
name: "Test 12: When commission is non zero",
args: args{
config: config,
password: "test",
address: "0x000000000000000000000000000000000000dea1",
status: "true",
parseStatus: true,
stakerId: 1,
staker: bindings.StructsStaker{
AcceptDelegation: false,
},
commission: 12,
SetDelegationAcceptanceTxn: &Types.Transaction{},
WaitForBlockCompletionStatus: 1,
},
wantErr: nil,
},
{
name: "Test 13: When executeUpdateCommission throws an error",
args: args{
config: config,
password: "test",
address: "0x000000000000000000000000000000000000dea1",
status: "true",
parseStatus: true,
stakerId: 1,
staker: bindings.StructsStaker{
AcceptDelegation: false,
},
commission: 12,
executeUpdateCommissionErr: errors.New("error in updating commission"),
SetDelegationAcceptanceTxn: &Types.Transaction{},
WaitForBlockCompletionStatus: 1,
},
wantErr: errors.New("error in updating commission"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -266,6 +324,10 @@ func TestSetDelegation(t *testing.T) {
return tt.args.status, tt.args.statusErr
}

GetUint8CommissionMock = func(*pflag.FlagSet) (uint8, error) {
return tt.args.commission, tt.args.commissionErr
}

ParseBoolMock = func(string) (bool, error) {
return tt.args.parseStatus, tt.args.parseStatusErr
}
Expand Down Expand Up @@ -294,6 +356,10 @@ func TestSetDelegation(t *testing.T) {
return tt.args.hash
}

ExecuteUpdateCommissionMock = func(*ethclient.Client, types.UpdateCommissionInput, UtilsStruct) error {
return tt.args.executeUpdateCommissionErr
}

WaitForBlockCompletionMock = func(*ethclient.Client, string) int {
return tt.args.WaitForBlockCompletionStatus
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/struct-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@ func (cmdUtils UtilsCmd) GetAmountInSRZRs(client *ethclient.Client, address stri
return GetAmountInSRZRs(client, address, staker, amount, utilsStruct)
}

func (cmdUtils UtilsCmd) ExecuteUpdateCommission(client *ethclient.Client, input types.UpdateCommissionInput, utilsStruct UtilsStruct) error {
return ExecuteUpdateCommission(client, input, utilsStruct)
}

func (blockManagerUtils BlockManagerUtils) ClaimBlockReward(client *ethclient.Client, opts *bind.TransactOpts) (*Types.Transaction, error) {
blockManager := utils.UtilsInterface.GetBlockManager(client)
return blockManager.ClaimBlockReward(opts)
Expand Down
29 changes: 19 additions & 10 deletions cmd/updateCommission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/pflag"
"razor/core"
"razor/core/types"
Expand Down Expand Up @@ -54,7 +55,17 @@ func (utilsStruct UtilsStruct) UpdateCommission(flagSet *pflag.FlagSet) error {
return err
}

stakerInfo, err := utilsStruct.razorUtils.GetStaker(client, address, stakerId)
return utilsStruct.cmdUtils.ExecuteUpdateCommission(client, types.UpdateCommissionInput{
Commission: commission,
Config: config,
Address: address,
Password: password,
StakerId: stakerId,
}, utilsStruct)
}

func ExecuteUpdateCommission(client *ethclient.Client, input types.UpdateCommissionInput, utilsStruct UtilsStruct) error {
stakerInfo, err := utilsStruct.razorUtils.GetStaker(client, input.Address, input.StakerId)
if err != nil {
log.Error("Error in fetching staker info")
return err
Expand All @@ -65,7 +76,7 @@ func (utilsStruct UtilsStruct) UpdateCommission(flagSet *pflag.FlagSet) error {
return err
}

if commission == 0 || commission > maxCommission {
if input.Commission == 0 || input.Commission > maxCommission {
return errors.New("commission out of range")
}

Expand All @@ -82,22 +93,20 @@ func (utilsStruct UtilsStruct) UpdateCommission(flagSet *pflag.FlagSet) error {
if stakerInfo.EpochCommissionLastUpdated != 0 && (stakerInfo.EpochCommissionLastUpdated+uint32(epochLimitForUpdateCommission)) >= epoch {
return errors.New("invalid epoch for update")
}

txnOpts := types.TransactionOptions{
Client: client,
Password: password,
AccountAddress: address,
Password: input.Password,
AccountAddress: input.Address,
ChainId: core.ChainId,
Config: config,
Config: input.Config,
ContractAddress: core.StakeManagerAddress,
ABI: bindings.StakeManagerABI,
MethodName: "updateCommission",
Parameters: []interface{}{commission},
Parameters: []interface{}{input.Commission},
}

updateCommissionTxnOpts := utilsStruct.razorUtils.GetTxnOpts(txnOpts)
log.Infof("Setting the commission value of Staker %d to %d%%", stakerId, commission)
txn, err := utilsStruct.stakeManagerUtils.UpdateCommission(client, updateCommissionTxnOpts, commission)
log.Infof("Setting the commission value of Staker %d to %d%%", input.StakerId, input.Commission)
txn, err := utilsStruct.stakeManagerUtils.UpdateCommission(client, updateCommissionTxnOpts, input.Commission)
if err != nil {
log.Error("Error in setting commission")
return err
Expand Down
Loading

0 comments on commit d0efb9e

Please sign in to comment.