-
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.
test(systemtests): Add double signing test (#21115)
Co-authored-by: marbar3778 <[email protected]>
- Loading branch information
1 parent
43dd231
commit aee9803
Showing
4 changed files
with
129 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build system_test | ||
|
||
package systemtests | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/gjson" | ||
|
||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
) | ||
|
||
func TestValidatorDoubleSign(t *testing.T) { | ||
// Scenario: | ||
// given: a running chain | ||
// when: a second instance with the same val key signs a block | ||
// then: the validator is removed from the active set and jailed forever | ||
sut.ResetChain(t) | ||
cli := NewCLIWrapper(t, sut, verbose) | ||
sut.StartChain(t) | ||
|
||
// Check the validator is in the active set | ||
rsp := cli.CustomQuery("q", "staking", "validators") | ||
t.Log(rsp) | ||
nodePowerBefore := QueryCometValidatorPowerForNode(t, sut, 0) | ||
require.NotEmpty(t, nodePowerBefore) | ||
|
||
var validatorPubKey cryptotypes.PubKey | ||
newNode := sut.AddFullnode(t, "0.001stake", func(nodeNumber int, nodePath string) { | ||
valKeyFile := filepath.Join(WorkDir, nodePath, "config", "priv_validator_key.json") | ||
_ = os.Remove(valKeyFile) | ||
_, err := copyFile(filepath.Join(WorkDir, sut.nodePath(0), "config", "priv_validator_key.json"), valKeyFile) | ||
require.NoError(t, err) | ||
validatorPubKey = LoadValidatorPubKeyForNode(t, sut, nodeNumber) | ||
}) | ||
sut.AwaitNodeUp(t, fmt.Sprintf("http://%s:%d", newNode.IP, newNode.RPCPort)) | ||
|
||
// let's wait some blocks to have evidence and update persisted | ||
rpc := sut.RPCClient(t) | ||
pkBz := validatorPubKey.Bytes() | ||
for i := 0; i < 20; i++ { | ||
sut.AwaitNextBlock(t) | ||
if QueryCometValidatorPower(rpc, pkBz) == 0 { | ||
break | ||
} | ||
} | ||
sut.AwaitNextBlock(t) | ||
|
||
// then comet status updated | ||
nodePowerAfter := QueryCometValidatorPowerForNode(t, sut, 0) | ||
require.Empty(t, nodePowerAfter) | ||
|
||
// and sdk status updated | ||
byzantineOperatorAddr := cli.GetKeyAddrPrefix("node0", "val") | ||
rsp = cli.CustomQuery("q", "staking", "validator", byzantineOperatorAddr) | ||
assert.True(t, gjson.Get(rsp, "validator.jailed").Bool(), rsp) | ||
} |
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,46 @@ | ||
package systemtests | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/cometbft/cometbft/privval" | ||
"github.com/stretchr/testify/require" | ||
|
||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
) | ||
|
||
// LoadValidatorPubKeyForNode load validator nodes consensus pub key for given node number | ||
func LoadValidatorPubKeyForNode(t *testing.T, sut *SystemUnderTest, nodeNumber int) cryptotypes.PubKey { | ||
t.Helper() | ||
return LoadValidatorPubKey(t, filepath.Join(WorkDir, sut.nodePath(nodeNumber), "config", "priv_validator_key.json")) | ||
} | ||
|
||
// LoadValidatorPubKey load validator nodes consensus pub key from disk | ||
func LoadValidatorPubKey(t *testing.T, keyFile string) cryptotypes.PubKey { | ||
t.Helper() | ||
filePV := privval.LoadFilePVEmptyState(keyFile, "") | ||
pubKey, err := filePV.GetPubKey() | ||
require.NoError(t, err) | ||
valPubKey, err := cryptocodec.FromCmtPubKeyInterface(pubKey) | ||
require.NoError(t, err) | ||
return valPubKey | ||
} | ||
|
||
// QueryCometValidatorPowerForNode returns the validator's power from tendermint RPC endpoint. 0 when not found | ||
func QueryCometValidatorPowerForNode(t *testing.T, sut *SystemUnderTest, nodeNumber int) int64 { | ||
t.Helper() | ||
pubKebBz := LoadValidatorPubKeyForNode(t, sut, nodeNumber).Bytes() | ||
return QueryCometValidatorPower(sut.RPCClient(t), pubKebBz) | ||
} | ||
|
||
func QueryCometValidatorPower(c RPCClient, pubKebBz []byte) int64 { | ||
for _, v := range c.Validators() { | ||
if bytes.Equal(v.PubKey.Bytes(), pubKebBz) { | ||
return v.VotingPower | ||
} | ||
} | ||
return 0 | ||
} |
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