forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/capability: simulations (cosmos#6062)
* x/capability: simulations * update logs * validate genesis state * InitGenesis and ExportGenesis functions * update validation func * fix import-export sim * remove nondeterminism from capability genesis * Update x/capability/types/genesis.go * Update x/capability/types/genesis.go * fix tests * fix merge * consistency updates * try fix nondeterminism * fix conditional * Fix random index logic * lint * lint Co-authored-by: Aditya Sripal <[email protected]> Co-authored-by: Aleksandr Bezobchuk <[email protected]> Co-authored-by: Alexander Bezobchuk <[email protected]>
- Loading branch information
1 parent
7143d09
commit 930802e
Showing
13 changed files
with
477 additions
and
41 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
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,41 @@ | ||
package capability | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// InitGenesis initializes the capability module's state from a provided genesis | ||
// state. | ||
func InitGenesis(ctx sdk.Context, k Keeper, genState GenesisState) { | ||
k.SetIndex(ctx, genState.Index) | ||
|
||
// set owners for each index and initialize capability | ||
for _, genOwner := range genState.Owners { | ||
k.SetOwners(ctx, genOwner.Index, genOwner.Owners) | ||
k.InitializeCapability(ctx, genOwner.Index, genOwner.Owners) | ||
} | ||
} | ||
|
||
// ExportGenesis returns the capability module's exported genesis. | ||
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState { | ||
index := k.GetLatestIndex(ctx) | ||
owners := []GenesisOwners{} | ||
|
||
for i := uint64(1); i < index; i++ { | ||
capabilityOwners, ok := k.GetOwners(ctx, i) | ||
if !ok || len(capabilityOwners.Owners) == 0 { | ||
continue | ||
} | ||
|
||
genOwner := GenesisOwners{ | ||
Index: i, | ||
Owners: capabilityOwners, | ||
} | ||
owners = append(owners, genOwner) | ||
} | ||
|
||
return GenesisState{ | ||
Index: index, | ||
Owners: owners, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package simulation | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
tmkv "github.com/tendermint/tendermint/libs/kv" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/capability/types" | ||
) | ||
|
||
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's | ||
// Value to the corresponding capaility type. | ||
func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB tmkv.Pair) string { | ||
return func(kvA, kvB tmkv.Pair) string { | ||
switch { | ||
case bytes.Equal(kvA.Key, types.KeyIndex): | ||
idxA := sdk.BigEndianToUint64(kvA.Value) | ||
idxB := sdk.BigEndianToUint64(kvB.Value) | ||
return fmt.Sprintf("Index A: %d\nIndex B: %d\n", idxA, idxB) | ||
|
||
case bytes.HasPrefix(kvA.Key, types.KeyPrefixIndexCapability): | ||
var capOwnersA, capOwnersB types.CapabilityOwners | ||
cdc.MustUnmarshalBinaryBare(kvA.Value, &capOwnersA) | ||
cdc.MustUnmarshalBinaryBare(kvB.Value, &capOwnersB) | ||
return fmt.Sprintf("CapabilityOwners A: %v\nCapabilityOwners B: %v\n", capOwnersA, capOwnersB) | ||
|
||
default: | ||
panic(fmt.Sprintf("invalid %s key prefix %X (%s)", types.ModuleName, kvA.Key, string(kvA.Key))) | ||
} | ||
} | ||
} |
Oops, something went wrong.