-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ValidatorStore interface doesn't match StakingKeeper (backport #…
…17164) (#17283) Co-authored-by: Facundo Medica <[email protected]> Co-authored-by: Julien Robert <[email protected]>
- Loading branch information
1 parent
8375a67
commit 69db56b
Showing
9 changed files
with
174 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
protoio "github.com/cosmos/gogoproto/io" | ||
"github.com/cosmos/gogoproto/proto" | ||
"gotest.tools/v3/assert" | ||
|
||
"cosmossdk.io/math" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
ed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/testutil" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func TestValidateVoteExtensions(t *testing.T) { | ||
t.Parallel() | ||
f := initFixture(t) | ||
|
||
// enable vote extensions | ||
cp := simtestutil.DefaultConsensusParams | ||
cp.Abci = &cmtproto.ABCIParams{VoteExtensionsEnableHeight: 1} | ||
f.sdkCtx = f.sdkCtx.WithConsensusParams(*cp).WithBlockHeight(2) | ||
|
||
// setup the validators | ||
numVals := 3 | ||
privKeys := []cryptotypes.PrivKey{} | ||
for i := 0; i < numVals; i++ { | ||
privKeys = append(privKeys, ed25519.GenPrivKey()) | ||
} | ||
|
||
vals := []stakingtypes.Validator{} | ||
for _, v := range privKeys { | ||
valAddr := sdk.ValAddress(v.PubKey().Address()) | ||
simtestutil.AddTestAddrsFromPubKeys(f.bankKeeper, f.stakingKeeper, f.sdkCtx, []cryptotypes.PubKey{v.PubKey()}, math.NewInt(100000000000)) | ||
vals = append(vals, testutil.NewValidator(t, valAddr, v.PubKey())) | ||
} | ||
|
||
votes := []abci.ExtendedVoteInfo{} | ||
|
||
for i, v := range vals { | ||
v.Tokens = math.NewInt(1000000) | ||
v.Status = stakingtypes.Bonded | ||
assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, v)) | ||
assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.sdkCtx, v)) | ||
assert.NilError(t, f.stakingKeeper.SetNewValidatorByPowerIndex(f.sdkCtx, v)) | ||
_, err := f.stakingKeeper.Delegate(f.sdkCtx, sdk.AccAddress(privKeys[i].PubKey().Address()), v.Tokens, stakingtypes.Unbonded, v, true) | ||
assert.NilError(t, err) | ||
|
||
// each val produces a vote | ||
voteExt := []byte("something" + v.OperatorAddress) | ||
cve := cmtproto.CanonicalVoteExtension{ | ||
Extension: voteExt, | ||
Height: f.sdkCtx.BlockHeight() - 1, // the vote extension was signed in the previous height | ||
Round: 0, | ||
ChainId: "chain-id-123", | ||
} | ||
|
||
extSignBytes, err := mashalVoteExt(&cve) | ||
assert.NilError(t, err) | ||
|
||
sig, err := privKeys[i].Sign(extSignBytes) | ||
assert.NilError(t, err) | ||
|
||
ve := abci.ExtendedVoteInfo{ | ||
Validator: abci.Validator{ | ||
Address: v.GetOperator(), | ||
Power: v.ConsensusPower(sdk.DefaultPowerReduction), | ||
}, | ||
VoteExtension: voteExt, | ||
ExtensionSignature: sig, | ||
BlockIdFlag: cmtproto.BlockIDFlagCommit, | ||
} | ||
votes = append(votes, ve) | ||
} | ||
|
||
err := baseapp.ValidateVoteExtensions(f.sdkCtx, f.stakingKeeper, f.sdkCtx.BlockHeight(), "chain-id-123", abci.ExtendedCommitInfo{Round: 0, Votes: votes}) | ||
assert.NilError(t, err) | ||
} | ||
|
||
func mashalVoteExt(msg proto.Message) ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := protoio.NewDelimitedWriter(&buf).WriteMsg(msg); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
Oops, something went wrong.