Skip to content

Commit

Permalink
fix: fix active (#80)
Browse files Browse the repository at this point in the history
* fix: fix active

* chore: add tests for converter

* chore: fixed linting
  • Loading branch information
freak12techno authored Oct 4, 2024
1 parent 2819ec4 commit b08a64f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package converter

import (
"encoding/hex"
"fmt"
"main/pkg/types"

Expand Down Expand Up @@ -76,7 +75,7 @@ func (c *Converter) ValidatorFromCosmosValidator(
Identity: validator.Description.Identity,
Website: validator.Description.Website,
Commission: commission,
ConsensusAddressHex: hex.EncodeToString(addr),
ConsensusAddressHex: fmt.Sprintf("%X", addr),
ConsensusAddressValcons: sdkTypes.ConsAddress(addr).String(),
OperatorAddress: validator.OperatorAddress,
Jailed: validator.Jailed,
Expand All @@ -92,5 +91,5 @@ func (c *Converter) MustSetValidatorConsumerConsensusAddr(validator *types.Valid
}

validator.ConsensusAddressValcons = consAddress.String()
validator.ConsensusAddressHex = fmt.Sprintf("%x", consAddress)
validator.ConsensusAddressHex = fmt.Sprintf("%X", consAddress)
}
78 changes: 78 additions & 0 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package converter

import (
"main/pkg/types"
"testing"
"time"

"cosmossdk.io/math"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
slashingTypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/assert"
)

func TestConverterFromCosmosValidator(t *testing.T) {
t.Parallel()

converter := NewConverter()
src := stakingTypes.Validator{
OperatorAddress: "cosmosvaloper1qphf0ferqcch0jca9hlqfm3x0eds3dpkcvpafp",
ConsensusPubkey: &codecTypes.Any{
TypeUrl: "/cosmos.crypto.ed25519.PubKey",
Value: []byte{10, 32, 190, 133, 104, 92, 29, 0, 175, 54, 121, 236, 216, 25, 131, 32, 33, 175, 6, 180, 153, 86, 155, 62, 40, 222, 169, 61, 30, 109, 2, 88, 60, 247},
},
Jailed: false,
Status: stakingTypes.Unbonded,
Tokens: math.NewInt(1386400),
DelegatorShares: math.LegacyNewDec(1386400.000000000000000000),
Description: stakingTypes.Description{
Moniker: "moniker",
Identity: "identity",
Website: "website",
SecurityContact: "contact",
Details: "details",
},
UnbondingHeight: 0,
UnbondingTime: time.Now(),
Commission: stakingTypes.Commission{
CommissionRates: stakingTypes.CommissionRates{
Rate: math.LegacyMustNewDecFromStr("0.1"),
MaxRate: math.LegacyMustNewDecFromStr("0.2"),
MaxChangeRate: math.LegacyMustNewDecFromStr("0.01"),
},
UpdateTime: time.Now(),
},
UnbondingOnHoldRefCount: 0,
}

val := converter.ValidatorFromCosmosValidator(src, &slashingTypes.ValidatorSigningInfo{
Tombstoned: false,
MissedBlocksCounter: 10,
})

assert.Equal(t, &types.Validator{
Moniker: "moniker",
Description: "details",
Identity: "identity",
SecurityContact: "contact",
Website: "website",
ConsensusAddressHex: "E5464CB88318A98724BFFE9E0C59129A2B35F11E",
ConsensusAddressValcons: "cosmosvalcons1u4ryewyrrz5cwf9ll60qckgjng4ntug726d6vf",
OperatorAddress: "cosmosvaloper1qphf0ferqcch0jca9hlqfm3x0eds3dpkcvpafp",
Commission: 0.1,
Jailed: false,
SigningInfo: &types.SigningInfo{
MissedBlocksCounter: 10,
Tombstoned: false,
},
VotingPower: math.LegacyNewDec(1386400.000000000000000000),
VotingPowerPercent: 0,
CumulativeVotingPowerPercent: 0,
Rank: 0,
}, val)

converter.MustSetValidatorConsumerConsensusAddr(val, "cosmosvalcons16vj0vma5lzcfsdfnkl7rrk9tfke9ut5yesmumm")
assert.Equal(t, "D324F66FB4F8B0983533B7FC31D8AB4DB25E2E84", val.ConsensusAddressHex)
assert.Equal(t, "cosmosvalcons16vj0vma5lzcfsdfnkl7rrk9tfke9ut5yesmumm", val.ConsensusAddressValcons)
}

0 comments on commit b08a64f

Please sign in to comment.