-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Ports key assignment to the driver on the PSS feature branch #1628
Merged
p-offtermatt
merged 2 commits into
feat/partial-set-security
from
ph/key-assignment-pss-feat
Feb 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -15,10 +15,13 @@ import ( | |
"github.com/kylelemons/godebug/pretty" | ||
"github.com/stretchr/testify/require" | ||
|
||
sdktypes "github.com/cosmos/cosmos-sdk/types" | ||
|
||
cmttypes "github.com/cometbft/cometbft/types" | ||
|
||
tmencoding "github.com/cometbft/cometbft/crypto/encoding" | ||
"github.com/cosmos/interchain-security/v4/testutil/integration" | ||
|
||
sdktypes "github.com/cosmos/cosmos-sdk/types" | ||
|
||
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types" | ||
) | ||
|
||
|
@@ -69,6 +72,7 @@ func TestMBT(t *testing.T) { | |
t.Logf("Number of sent packets: %v", stats.numSentPackets) | ||
t.Logf("Number of blocks: %v", stats.numBlocks) | ||
t.Logf("Number of transactions: %v", stats.numTxs) | ||
t.Logf("Number of key assignments: %v", stats.numKeyAssignments) | ||
t.Logf("Average summed block time delta passed per trace: %v", stats.totalBlockTimePassedPerTrace/time.Duration(numTraces)) | ||
} | ||
|
||
|
@@ -117,6 +121,21 @@ func RunItfTrace(t *testing.T, path string) { | |
|
||
t.Log("Chains are: ", chains) | ||
|
||
// generate keys that can be assigned on consumers, according to the ConsumerAddresses in the trace | ||
consumerAddressesExpr := params["ConsumerAddresses"].Value.(itf.ListExprType) | ||
|
||
_, _, consumerPrivVals, err := integration.CreateValidators(len(consumerAddressesExpr)) | ||
require.NoError(t, err, "Error creating consumer signers") | ||
|
||
consumerAddrNamesToPrivVals := make(map[string]cmttypes.PrivValidator, len(consumerAddressesExpr)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment here on the differences between real address, model cons addresses, etc? |
||
realAddrsToModelConsAddrs := make(map[string]string, len(consumerAddressesExpr)) | ||
i := 0 | ||
for address, privVal := range consumerPrivVals { | ||
consumerAddrNamesToPrivVals[consumerAddressesExpr[i].Value.(string)] = privVal | ||
realAddrsToModelConsAddrs[address] = consumerAddressesExpr[i].Value.(string) | ||
i++ | ||
} | ||
|
||
// create params struct | ||
vscTimeout := time.Duration(params["VscTimeout"].Value.(int64)) * time.Second | ||
|
||
|
@@ -145,6 +164,15 @@ func RunItfTrace(t *testing.T, path string) { | |
valSet, addressMap, signers, err := CreateValSet(initialValSet) | ||
require.NoError(t, err, "Error creating validator set") | ||
|
||
// get the set of signers for consumers: the validator signers, plus signers for the assignable addresses | ||
consumerSigners := make(map[string]cmttypes.PrivValidator, 0) | ||
for consAddr, consPrivVal := range consumerPrivVals { | ||
consumerSigners[consAddr] = consPrivVal | ||
} | ||
for consAddr, signer := range signers { | ||
consumerSigners[consAddr] = signer | ||
} | ||
|
||
// get a slice of validators in the right order | ||
nodes := make([]*cmttypes.Validator, len(valNames)) | ||
for i, valName := range valNames { | ||
|
@@ -211,6 +239,10 @@ func RunItfTrace(t *testing.T, path string) { | |
// and then increment the rest of the time | ||
runningConsumersBefore := driver.runningConsumers() | ||
driver.endAndBeginBlock("provider", 1*time.Nanosecond) | ||
for _, consumer := range driver.runningConsumers() { | ||
UpdateProviderClientOnConsumer(t, driver, consumer.ChainId) | ||
} | ||
|
||
driver.endAndBeginBlock("provider", time.Duration(timeAdvancement)*time.Second-1*time.Nanosecond) | ||
runningConsumersAfter := driver.runningConsumers() | ||
|
||
|
@@ -243,7 +275,7 @@ func RunItfTrace(t *testing.T, path string) { | |
consumer.Value.(string), | ||
modelParams, | ||
driver.providerChain().Vals, | ||
signers, | ||
consumerSigners, | ||
nodes, | ||
valNames, | ||
driver.providerChain(), | ||
|
@@ -268,11 +300,8 @@ func RunItfTrace(t *testing.T, path string) { | |
if len(consumersToStart) > 0 && consumer.ChainId == consumersToStart[len(consumersToStart)-1].Value.(string) { | ||
continue | ||
} | ||
consumerChainId := consumer.ChainId | ||
|
||
driver.path(ChainId(consumerChainId)).AddClientHeader(PROVIDER, driver.providerHeader()) | ||
err := driver.path(ChainId(consumerChainId)).UpdateClient(consumerChainId, false) | ||
require.True(t, err == nil, "Error updating client from %v on provider: %v", consumerChainId, err) | ||
UpdateProviderClientOnConsumer(t, driver, consumer.ChainId) | ||
} | ||
|
||
case "EndAndBeginBlockForConsumer": | ||
|
@@ -286,13 +315,12 @@ func RunItfTrace(t *testing.T, path string) { | |
_ = headerBefore | ||
|
||
driver.endAndBeginBlock(ChainId(consumerChain), 1*time.Nanosecond) | ||
UpdateConsumerClientOnProvider(t, driver, consumerChain) | ||
|
||
driver.endAndBeginBlock(ChainId(consumerChain), time.Duration(timeAdvancement)*time.Second-1*time.Nanosecond) | ||
|
||
// update the client on the provider | ||
consumerHeader := driver.chain(ChainId(consumerChain)).LastHeader | ||
driver.path(ChainId(consumerChain)).AddClientHeader(consumerChain, consumerHeader) | ||
err := driver.path(ChainId(consumerChain)).UpdateClient(PROVIDER, false) | ||
require.True(t, err == nil, "Error updating client from %v on provider: %v", consumerChain, err) | ||
UpdateConsumerClientOnProvider(t, driver, consumerChain) | ||
|
||
case "DeliverVscPacket": | ||
consumerChain := lastAction["consumerChain"].Value.(string) | ||
|
@@ -328,8 +356,26 @@ func RunItfTrace(t *testing.T, path string) { | |
expectError = false | ||
driver.DeliverPacketFromConsumer(ChainId(consumerChain), expectError) | ||
} | ||
default: | ||
case "KeyAssignment": | ||
consumerChain := lastAction["consumerChain"].Value.(string) | ||
node := lastAction["validator"].Value.(string) | ||
consumerAddr := lastAction["consumerAddr"].Value.(string) | ||
|
||
t.Log("KeyAssignment", consumerChain, node, consumerAddr) | ||
stats.numKeyAssignments++ | ||
|
||
valIndex := getIndexOfString(node, valNames) | ||
assignedPrivVal := consumerAddrNamesToPrivVals[consumerAddr] | ||
assignedKey, err := assignedPrivVal.GetPubKey() | ||
require.NoError(t, err, "Error getting pubkey") | ||
|
||
protoPubKey, err := tmencoding.PubKeyToProto(assignedKey) | ||
require.NoError(t, err, "Error converting pubkey to proto") | ||
|
||
error := driver.AssignKey(ChainId(consumerChain), int64(valIndex), protoPubKey) | ||
require.NoError(t, error, "Error assigning key") | ||
|
||
default: | ||
log.Fatalf("Error loading trace file %s, step %v: do not know action type %s", | ||
path, index, actionKind) | ||
} | ||
|
@@ -364,7 +410,7 @@ func RunItfTrace(t *testing.T, path string) { | |
require.Equal(t, modelRunningConsumers, actualRunningConsumers, "Running consumers do not match") | ||
|
||
// check validator sets - provider current validator set should be the one from the staking keeper | ||
CompareValidatorSets(t, driver, currentModelState, actualRunningConsumers) | ||
CompareValidatorSets(t, driver, currentModelState, actualRunningConsumers, realAddrsToModelConsAddrs) | ||
|
||
// check times - sanity check that the block times match the ones from the model | ||
CompareTimes(driver, actualRunningConsumers, currentModelState, timeOffset) | ||
|
@@ -383,7 +429,27 @@ func RunItfTrace(t *testing.T, path string) { | |
t.Log("🟢 Trace is ok!") | ||
} | ||
|
||
func CompareValidatorSets(t *testing.T, driver *Driver, currentModelState map[string]itf.Expr, consumers []string) { | ||
func UpdateProviderClientOnConsumer(t *testing.T, driver *Driver, consumerChainId string) { | ||
driver.path(ChainId(consumerChainId)).AddClientHeader(PROVIDER, driver.providerHeader()) | ||
err := driver.path(ChainId(consumerChainId)).UpdateClient(consumerChainId, false) | ||
require.True(t, err == nil, "Error updating client from %v on provider: %v", consumerChainId, err) | ||
} | ||
|
||
func UpdateConsumerClientOnProvider(t *testing.T, driver *Driver, consumerChain string) { | ||
consumerHeader := driver.chain(ChainId(consumerChain)).LastHeader | ||
driver.path(ChainId(consumerChain)).AddClientHeader(consumerChain, consumerHeader) | ||
err := driver.path(ChainId(consumerChain)).UpdateClient(PROVIDER, false) | ||
require.True(t, err == nil, "Error updating client from %v on provider: %v", consumerChain, err) | ||
} | ||
|
||
func CompareValidatorSets( | ||
t *testing.T, | ||
driver *Driver, | ||
currentModelState map[string]itf.Expr, | ||
consumers []string, | ||
// a map from real addresses to the names of those consumer addresses in the model | ||
keyAddrsToModelConsAddrName map[string]string, | ||
) { | ||
t.Helper() | ||
modelValSet := ValidatorSet(currentModelState, "provider") | ||
|
||
|
@@ -407,23 +473,28 @@ func CompareValidatorSets(t *testing.T, driver *Driver, currentModelState map[st | |
pubkey, err := val.ConsPubKey() | ||
require.NoError(t, err, "Error getting pubkey") | ||
|
||
consAddr := providertypes.NewConsumerConsAddress(sdktypes.ConsAddress(pubkey.Address().Bytes())) | ||
consAddrModelName, ok := keyAddrsToModelConsAddrName[pubkey.Address().String()] | ||
if ok { // the node has a key assigned, use the name of the consumer address in the model | ||
consumerCurValSet[consAddrModelName] = val.Power | ||
} else { // the node doesn't have a key assigned yet, get the validator moniker | ||
consAddr := providertypes.NewConsumerConsAddress(sdktypes.ConsAddress(pubkey.Address().Bytes())) | ||
|
||
// the consumer vals right now are CrossChainValidators, for which we don't know their mnemonic | ||
// so we need to find the mnemonic of the consumer val now to enter it by name in the map | ||
// the consumer vals right now are CrossChainValidators, for which we don't know their mnemonic | ||
// so we need to find the mnemonic of the consumer val now to enter it by name in the map | ||
|
||
// get the address on the provider that corresponds to the consumer address | ||
providerConsAddr, found := driver.providerKeeper().GetValidatorByConsumerAddr(driver.providerCtx(), consumer, consAddr) | ||
if !found { | ||
providerConsAddr = providertypes.NewProviderConsAddress(consAddr.Address) | ||
} | ||
// get the address on the provider that corresponds to the consumer address | ||
providerConsAddr, found := driver.providerKeeper().GetValidatorByConsumerAddr(driver.providerCtx(), consumer, consAddr) | ||
if !found { | ||
providerConsAddr = providertypes.NewProviderConsAddress(consAddr.Address) | ||
} | ||
|
||
// get the validator for that address on the provider | ||
providerVal, found := driver.providerStakingKeeper().GetValidatorByConsAddr(driver.providerCtx(), providerConsAddr.Address) | ||
require.True(t, found, "Error getting provider validator") | ||
// get the validator for that address on the provider | ||
providerVal, found := driver.providerStakingKeeper().GetValidatorByConsAddr(driver.providerCtx(), providerConsAddr.Address) | ||
require.True(t, found, "Error getting provider validator") | ||
|
||
// use the moniker of that validator | ||
consumerCurValSet[providerVal.GetMoniker()] = val.Power | ||
// use the moniker of that validator | ||
consumerCurValSet[providerVal.GetMoniker()] = val.Power | ||
} | ||
} | ||
require.NoError(t, CompareValSet(modelValSet, consumerCurValSet), "Validator sets do not match for consumer %v", consumer) | ||
} | ||
|
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 |
---|---|---|
|
@@ -16,4 +16,6 @@ type Stats struct { | |
numTxs int | ||
|
||
totalBlockTimePassedPerTrace time.Duration | ||
|
||
numKeyAssignments int | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename
value
topubKey
or something similar.