Skip to content

Commit

Permalink
[fix] Fix the issue regarding voting power not taking effect (#65)
Browse files Browse the repository at this point in the history
* fix the issue #58 regarding voting power not taking effect

* change prepareDelegation to be compatible with PR61

* update ibc-go to v7.4.0 to fix the Vulnerability GO-2024-2694

* change the epoch configuration to hour when start a local node through the local_node.sh

* using shfmt to fix the shell lint error
  • Loading branch information
TimmyExogenous authored May 29, 2024
1 parent 1b8f41f commit 82b2509
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
14 changes: 14 additions & 0 deletions local_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ if [[ $overwrite == "y" || $overwrite == "Y" ]]; then
# x/dogfood
jq '.app_state["dogfood"]["initial_val_set"][0]["public_key"]="0xf0f6919e522c5b97db2c8255bff743f9dfddd7ad9fc37cb0c1670b480d0f9914"' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
jq '.app_state["dogfood"]["initial_val_set"][0]["power"]="5000"' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"
# change the epoch to an hour when starting a local node, which facilitates the testing.
jq '.app_state["dogfood"]["params"]["epoch_identifier"]="hour"' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"

# x/epochs
HOUR_EPOCH='{
"identifier": "hour",
"start_time": "0001-01-01T00:00:00Z",
"duration": "3600s",
"current_epoch": "0",
"current_epoch_start_time": "0001-01-01T00:00:00Z",
"epoch_counting_started": false,
"current_epoch_start_height": "0"
}'
jq --argjson newEpoch "$HOUR_EPOCH" '.app_state["epochs"].epochs += [$newEpoch]' "$GENESIS" >"$TMP_GENESIS" && mv "$TMP_GENESIS" "$GENESIS"

if [[ $1 == "pending" ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
Expand Down
2 changes: 1 addition & 1 deletion testutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (suite *BaseTestSuite) SetupWithGenesisValSet(genAccs []authtypes.GenesisAc
Power: 1,
},
{
PublicKey: hexutil.Encode(pubKey.Bytes()),
PublicKey: hexutil.Encode(pubKey2.Bytes()),
Power: 1,
},
},
Expand Down
15 changes: 9 additions & 6 deletions x/operator/keeper/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,23 @@ func (suite *OperatorTestSuite) prepareDeposit(assetAddr common.Address, amount
suite.NoError(err)
}

func (suite *OperatorTestSuite) prepareDelegation(assetAddr common.Address, amount sdkmath.Int) {
func (suite *OperatorTestSuite) prepareDelegation(isDelegation bool, assetAddr common.Address, amount sdkmath.Int) {
suite.delegationAmount = amount
// delegate to operator
delegationParam := &delegationtype.DelegationOrUndelegationParams{
param := &delegationtype.DelegationOrUndelegationParams{
ClientChainLzID: suite.clientChainLzID,
Action: assetstypes.DelegateTo,
AssetsAddress: assetAddr[:],
OperatorAddress: suite.operatorAddr,
StakerAddress: suite.Address[:],
OpAmount: amount,
LzNonce: 0,
TxHash: common.HexToHash("0x24c4a315d757249c12a7a1d7b6fb96261d49deee26f06a3e1787d008b445c3ac"),
}
err := suite.App.DelegationKeeper.DelegateTo(suite.Ctx, delegationParam)
var err error
if isDelegation {
err = suite.App.DelegationKeeper.DelegateTo(suite.Ctx, param)
} else {
err = suite.App.DelegationKeeper.UndelegateFrom(suite.Ctx, param)
}
suite.NoError(err)
}

Expand All @@ -83,7 +86,7 @@ func (suite *OperatorTestSuite) prepare() {
delegationAmount := sdkmath.NewInt(50)
suite.prepareOperator()
suite.prepareDeposit(usdtAddress, depositAmount)
suite.prepareDelegation(usdtAddress, delegationAmount)
suite.prepareDelegation(true, usdtAddress, delegationAmount)
}

func (suite *OperatorTestSuite) CheckState(expectedState *StateForCheck) {
Expand Down
2 changes: 1 addition & 1 deletion x/operator/keeper/usd_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (k Keeper) GetAvgDelegatedValue(
}
ret := make([]int64, 0)
for _, operator := range operators {
usdValue, err := k.GetOperatorUSDValue(ctx, operator.String(), avsAddr)
usdValue, err := k.GetOperatorUSDValue(ctx, avsAddr, operator.String())
if err != nil {
return nil, err
}
Expand Down
53 changes: 52 additions & 1 deletion x/operator/keeper/usd_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (suite *OperatorTestSuite) TestAVSUSDValue() {
usdcPrice, err := suite.App.OperatorKeeper.OracleInterface().GetSpecifiedAssetsPrice(suite.Ctx, suite.assetID)
suite.NoError(err)
delegatedAmount := sdkmath.NewIntWithDecimal(8, 7)
suite.prepareDelegation(usdcAddr, delegatedAmount)
suite.prepareDelegation(true, usdcAddr, delegatedAmount)

// updating the new voting power
suite.NoError(err)
Expand All @@ -99,3 +99,54 @@ func (suite *OperatorTestSuite) TestAVSUSDValue() {
suite.NoError(err)
suite.Equal(expectedUSDvalue, operatorUSDValue)
}

func (suite *OperatorTestSuite) TestVotingPowerForDogFood() {
initialPower := int64(1)
initialAVSUSDValue := sdkmath.LegacyNewDec(2)
initialOperatorUSDValue := sdkmath.LegacyNewDec(1)
addPower := 1
addUSDValue := sdkmath.LegacyNewDec(1)

validators := suite.App.StakingKeeper.GetAllExocoreValidators(suite.Ctx)
for _, validator := range validators {
_, isFound := suite.App.StakingKeeper.GetValidatorByConsAddr(suite.Ctx, validator.Address)
suite.True(isFound)
suite.Equal(initialPower, validator.Power)
}

operators, _ := suite.App.OperatorKeeper.GetActiveOperatorsForChainID(suite.Ctx, suite.Ctx.ChainID())
allAssets, err := suite.App.AssetsKeeper.GetAllStakingAssetsInfo(suite.Ctx)
suite.NoError(err)
suite.Equal(1, len(allAssets))
var asset assetstype.AssetInfo
for _, value := range allAssets {
asset = *value.AssetBasicInfo
}

assetAddr := common.HexToAddress(asset.Address)
depositAmount := sdkmath.NewIntWithDecimal(2, int(asset.Decimals))
delegationAmount := sdkmath.NewIntWithDecimal(int64(addPower), int(asset.Decimals))
suite.prepareDeposit(assetAddr, depositAmount)
suite.operatorAddr = operators[0]
suite.prepareDelegation(true, assetAddr, delegationAmount)

suite.App.OperatorKeeper.EndBlock(suite.Ctx, abci.RequestEndBlock{})
avsUSDValue, err := suite.App.OperatorKeeper.GetAVSUSDValue(suite.Ctx, suite.Ctx.ChainID())
suite.NoError(err)
suite.Equal(initialAVSUSDValue.Add(addUSDValue), avsUSDValue)
operatorUSDValue, err := suite.App.OperatorKeeper.GetOperatorUSDValue(suite.Ctx, suite.Ctx.ChainID(), suite.operatorAddr.String())
suite.NoError(err)
suite.Equal(initialOperatorUSDValue.Add(addUSDValue), operatorUSDValue)

found, consensusKey, err := suite.App.OperatorKeeper.GetOperatorConsKeyForChainID(suite.Ctx, suite.operatorAddr, suite.Ctx.ChainID())
suite.NoError(err)
suite.True(found)

suite.App.StakingKeeper.MarkEpochEnd(suite.Ctx)
validatorUpdates := suite.App.StakingKeeper.EndBlock(suite.Ctx)
suite.Equal(1, len(validatorUpdates))
for _, update := range validatorUpdates {
suite.Equal(*consensusKey, update.PubKey)
suite.Equal(initialPower+int64(addPower), update.Power)
}
}

0 comments on commit 82b2509

Please sign in to comment.