Skip to content

Commit

Permalink
clean code + storecodecompat
Browse files Browse the repository at this point in the history
  • Loading branch information
trevormil committed Jan 10, 2024
1 parent 62ec422 commit ae1ae28
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion proto/badges/address_lists.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ option go_package = "github.com/bitbadges/bitbadgeschain/x/badges/types";
/*
An AddressList is a list of addresses that are referenced by an ID.
The list may include only the specified addresses, or it may include all addresses but
the specified addresses (depending on if allowlist is true (whitelist) or false (blacklist)).
the specified addresses (depending on if allowlist is true (allowlist) or false (blocklist)).
AddressLists are used for defining approvals, as well as being a simplified badge interface.
*/
Expand Down
2 changes: 1 addition & 1 deletion proto/badges/transfers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ message MerkleChallenge {
// The expected length of the Merkle path for verification. Used to prevent Merkle path truncation attacks.
string expectedProofLength = 2 [(gogoproto.customtype) = "Uint", (gogoproto.nullable) = false];

// If true, we will override the user's leaf for their proof with their creator address. Used for whitelist trees where all leaves are valid Cosmos addresses.
// If true, we will override the user's leaf for their proof with their creator address. Used for allowlist trees where all leaves are valid Cosmos addresses.
bool useCreatorAddressAsLeaf = 3;

// The maximum number of times each leaf can be used. Must be 1 if useCreatorAddressAsLeaf is false to prevent replay attacks.
Expand Down
33 changes: 25 additions & 8 deletions proto/wasmx/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ option go_package = "github.com/bitbadges/bitbadgeschain/x/wasmx/types";
// Msg defines the wasmx Msg service.
service Msg {
rpc ExecuteContractCompat(MsgExecuteContractCompat) returns (MsgExecuteContractCompatResponse);
rpc StoreCodeCompat(MsgStoreCodeCompat) returns (MsgStoreCodeCompatResponse);
}

// MsgExecuteContractCompat submits the given message data to a smart contract, compatible with EIP712
message MsgExecuteContractCompat {
// Sender is the that actor that signed the messages
string sender = 1;
// Contract is the address of the smart contract
string contract = 2;
// Msg json encoded message to be passed to the contract
string msg = 3;
// Funds coins that are transferred to the contract on execution
string funds = 4;
// Sender is the that actor that signed the messages
string sender = 1;
// Contract is the address of the smart contract
string contract = 2;
// Msg json encoded message to be passed to the contract
string msg = 3;
// Funds coins that are transferred to the contract on execution
string funds = 4;
}

// MsgExecuteContractCompatResponse returns execution result data.
message MsgExecuteContractCompatResponse {
// Data contains bytes to returned from the contract
bytes data = 1;
}

// MsgStoreCodeCompat submits the given message data to a smart contract, compatible with EIP712
message MsgStoreCodeCompat {
// Sender is the that actor that signed the messages
string sender = 1;
// WasmByteCode is the raw wasm bytes
string hexWasmByteCode = 2;
}

// MsgStoreCodeCompatResponse returns store result data.
message MsgStoreCodeCompatResponse {
// CodeID is the reference to the stored WASM code
string codeId = 1 [(gogoproto.customtype) = "Uint", (gogoproto.nullable) = false];
// Checksum is the sha256 hash of the stored code
bytes checksum = 2;
}
4 changes: 2 additions & 2 deletions x/badges/keeper/approved_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type UserApprovalsToCheck struct {
func (k Keeper) DeductUserOutgoingApprovals(ctx sdk.Context, overallTransferBalances []*types.Balance, collection *types.BadgeCollection, userBalance *types.UserBalanceStore, badgeIds []*types.UintRange, times []*types.UintRange, from string, to string, requester string, amount sdkmath.Uint, solutions []*types.MerkleProof, challengeIdsIncremented *[]string, trackerIdsIncremented *[]string, prioritizedApprovals []*types.ApprovalIdentifierDetails, onlyCheckPrioritized bool) error {
currApprovals := userBalance.OutgoingApprovals
if userBalance.AutoApproveSelfInitiatedOutgoingTransfers {
currApprovals = AppendDefaultForOutgoing(currApprovals, from)
currApprovals = AppendSelfInitiatedOutgoingApproval(currApprovals, from)
}

//Little hack to reuse the same function for all transfer objects (we cast everything to a collection transfer)
Expand All @@ -37,7 +37,7 @@ func (k Keeper) DeductUserOutgoingApprovals(ctx sdk.Context, overallTransferBala
func (k Keeper) DeductUserIncomingApprovals(ctx sdk.Context, overallTransferBalances []*types.Balance, collection *types.BadgeCollection, userBalance *types.UserBalanceStore, badgeIds []*types.UintRange, times []*types.UintRange, from string, to string, requester string, amount sdkmath.Uint, solutions []*types.MerkleProof, challengeIdsIncremented *[]string, trackerIdsIncremented *[]string, prioritizedApprovals []*types.ApprovalIdentifierDetails, onlyCheckPrioritized bool) error {
currApprovals := userBalance.IncomingApprovals
if userBalance.AutoApproveSelfInitiatedIncomingTransfers {
currApprovals = AppendDefaultForIncoming(currApprovals, to)
currApprovals = AppendSelfInitiatedIncomingApproval(currApprovals, to)
}

//Little hack to reuse the same function for all transfer objects (we cast everything to a collection transfer)
Expand Down
2 changes: 1 addition & 1 deletion x/badges/keeper/badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (k Keeper) CreateBadges(ctx sdk.Context, collection *types.BadgeCollection,
}
}

err = k.CheckBalancesActionPermission(ctx, detailsToCheck, collection.CollectionPermissions.CanCreateMoreBadges, "can create more badges")
err = k.CheckIfBalancesActionPermissionPermits(ctx, detailsToCheck, collection.CollectionPermissions.CanCreateMoreBadges, "can create more badges")
if err != nil {
return &types.BadgeCollection{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/badges/keeper/challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) AssertValidSolutionForEveryChallenge(ctx sdk.Context, collection
hasValidSolution := false
errStr := ""
if challenge.UseCreatorAddressAsLeaf {
errStr = "does not satisfy whitelist"
errStr = "does not satisfy allowlist"
} else {
errStr = "invalid code / password"
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func CheckMerklePath(leaf string, expectedRoot string, aunts []*types.MerklePath

func GetLeafIndex(aunts []*types.MerklePathItem) sdkmath.Uint {
leafIndex := sdkmath.NewUint(1)
//iterate through msg.WhitelistProof.Aunts backwards
//iterate through msg.AllowlistProof.Aunts backwards
for i := len(aunts) - 1; i >= 0; i-- {
aunt := aunts[i]
onRight := aunt.OnRight
Expand Down
4 changes: 2 additions & 2 deletions x/badges/keeper/integration_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func AssertUintsEqual(suite *TestSuite, expected sdkmath.Uint, actual sdkmath.Ui
}

func AssertUintRangesEqual(suite *TestSuite, expected []*types.UintRange, actual []*types.UintRange) {
remainingOne, _ := types.RemoveUintRangeFromUintRange(actual, expected)
remainingTwo, _ := types.RemoveUintRangeFromUintRange(expected, actual)
remainingOne, _ := types.RemoveUintRangesFromUintRanges(actual, expected)
remainingTwo, _ := types.RemoveUintRangesFromUintRanges(expected, actual)
suite.Require().Equal(len(remainingOne), 0, "UintRanges not equal %s %s", expected, actual)
suite.Require().Equal(len(remainingTwo), 0, "UintRanges not equal %s %s", expected, actual)
}
Expand Down
1 change: 0 additions & 1 deletion x/badges/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var (
UserBalanceKey = []byte{0x02}
NextCollectionIdKey = []byte{0x03}
UsedClaimChallengeKey = []byte{0x04}
WhitelistIndexKey = []byte{0x05}
AddressListKey = []byte{0x06}
ApprovalTrackerKey = []byte{0x07}

Expand Down
2 changes: 1 addition & 1 deletion x/badges/keeper/msg_server_delete_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k msgServer) DeleteCollection(goCtx context.Context, msg *types.MsgDeleteC
}

//Check deleted permission is valid for current time
err = k.CheckActionPermission(ctx, collection.CollectionPermissions.CanDeleteCollection, "can delete collection")
err = k.CheckIfActionPermissionPermits(ctx, collection.CollectionPermissions.CanDeleteCollection, "can delete collection")
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/badges/keeper/msg_server_update_user_approved_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (k msgServer) UpdateUserApprovals(goCtx context.Context, msg *types.MsgUpda

if msg.UpdateAutoApproveSelfInitiatedIncomingTransfers && userBalance.AutoApproveSelfInitiatedIncomingTransfers != msg.AutoApproveSelfInitiatedIncomingTransfers {
//Check permission is valid for current time
err = k.CheckActionPermission(ctx, userBalance.UserPermissions.CanUpdateAutoApproveSelfInitiatedIncomingTransfers, "can update auto approve self initiated incoming transfers")
err = k.CheckIfActionPermissionPermits(ctx, userBalance.UserPermissions.CanUpdateAutoApproveSelfInitiatedIncomingTransfers, "can update auto approve self initiated incoming transfers")
if err != nil {
return nil, err
}
Expand All @@ -71,7 +71,7 @@ func (k msgServer) UpdateUserApprovals(goCtx context.Context, msg *types.MsgUpda

if msg.UpdateAutoApproveSelfInitiatedOutgoingTransfers && userBalance.AutoApproveSelfInitiatedOutgoingTransfers != msg.AutoApproveSelfInitiatedOutgoingTransfers {
//Check permission is valid for current time
err = k.CheckActionPermission(ctx, userBalance.UserPermissions.CanUpdateAutoApproveSelfInitiatedOutgoingTransfers, "can update auto approve self initiated outgoing transfers")
err = k.CheckIfActionPermissionPermits(ctx, userBalance.UserPermissions.CanUpdateAutoApproveSelfInitiatedOutgoingTransfers, "can update auto approve self initiated outgoing transfers")
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/badges/keeper/transfers_expanders.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// By default, we approve all transfers if to === initiatedBy
func AppendDefaultForIncoming(currApprovals []*types.UserIncomingApproval, userAddress string) []*types.UserIncomingApproval {
func AppendSelfInitiatedIncomingApproval(currApprovals []*types.UserIncomingApproval, userAddress string) []*types.UserIncomingApproval {
currApprovals = append([]*types.UserIncomingApproval{
{
FromListId: "AllWithMint", //everyone
Expand Down Expand Up @@ -37,7 +37,7 @@ func AppendDefaultForIncoming(currApprovals []*types.UserIncomingApproval, userA
}

// By default, we approve all transfers if from === initiatedBy
func AppendDefaultForOutgoing(currApprovals []*types.UserOutgoingApproval, userAddress string) []*types.UserOutgoingApproval {
func AppendSelfInitiatedOutgoingApproval(currApprovals []*types.UserOutgoingApproval, userAddress string) []*types.UserOutgoingApproval {
//prepend it
currApprovals = append([]*types.UserOutgoingApproval{
{
Expand Down
14 changes: 7 additions & 7 deletions x/badges/keeper/update_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func CheckNotForbidden(ctx sdk.Context, permission *types.UniversalPermissionDet
return nil
}

func (k Keeper) CheckActionPermission(ctx sdk.Context, permissions []*types.ActionPermission, permissionStr string) error {
func (k Keeper) CheckIfActionPermissionPermits(ctx sdk.Context, permissions []*types.ActionPermission, permissionStr string) error {
castedPermissions, err := k.CastActionPermissionToUniversalPermission(permissions)
if err != nil {
return err
Expand All @@ -131,7 +131,7 @@ func (k Keeper) CheckActionPermission(ctx sdk.Context, permissions []*types.Acti
return nil
}

func (k Keeper) CheckTimedUpdatePermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.TimedUpdatePermission, permissionStr string) error {
func (k Keeper) CheckIfTimedUpdatePermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.TimedUpdatePermission, permissionStr string) error {
castedPermissions, err := k.CastTimedUpdatePermissionToUniversalPermission(permissions)
if err != nil {
return err
Expand All @@ -142,7 +142,7 @@ func (k Keeper) CheckTimedUpdatePermission(ctx sdk.Context, detailsToCheck []*ty
return CheckNotForbiddenForAllOverlaps(ctx, permissionDetails, detailsToCheck, permissionStr)
}

func (k Keeper) CheckBalancesActionPermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.BalancesActionPermission, permissionStr string) error {
func (k Keeper) CheckIfBalancesActionPermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.BalancesActionPermission, permissionStr string) error {
castedPermissions, err := k.CastBalancesActionPermissionToUniversalPermission(permissions)
if err != nil {
return err
Expand All @@ -153,7 +153,7 @@ func (k Keeper) CheckBalancesActionPermission(ctx sdk.Context, detailsToCheck []
return CheckNotForbiddenForAllOverlaps(ctx, permissionDetails, detailsToCheck, permissionStr)
}

func (k Keeper) CheckTimedUpdateWithBadgeIdsPermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.TimedUpdateWithBadgeIdsPermission, permissionStr string) error {
func (k Keeper) CheckIfTimedUpdateWithBadgeIdsPermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.TimedUpdateWithBadgeIdsPermission, permissionStr string) error {
castedPermissions, err := k.CastTimedUpdateWithBadgeIdsPermissionToUniversalPermission(permissions)
if err != nil {
return err
Expand All @@ -164,7 +164,7 @@ func (k Keeper) CheckTimedUpdateWithBadgeIdsPermission(ctx sdk.Context, detailsT
return CheckNotForbiddenForAllOverlaps(ctx, permissionDetails, detailsToCheck, permissionStr)
}

func (k Keeper) CheckCollectionApprovalPermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.CollectionApprovalPermission, permissionStr string) error {
func (k Keeper) CheckIfCollectionApprovalPermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.CollectionApprovalPermission, permissionStr string) error {
castedPermissions, err := k.CastCollectionApprovalPermissionToUniversalPermission(ctx, permissions)
if err != nil {
return err
Expand All @@ -175,7 +175,7 @@ func (k Keeper) CheckCollectionApprovalPermission(ctx sdk.Context, detailsToChec
return CheckNotForbiddenForAllOverlaps(ctx, permissionDetails, detailsToCheck, permissionStr)
}

func (k Keeper) CheckUserOutgoingApprovalPermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.UserOutgoingApprovalPermission, permissionStr string) error {
func (k Keeper) CheckIfUserOutgoingApprovalPermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.UserOutgoingApprovalPermission, permissionStr string) error {
castedPermissions, err := k.CastUserOutgoingApprovalPermissionToUniversalPermission(ctx, permissions)
if err != nil {
return err
Expand All @@ -186,7 +186,7 @@ func (k Keeper) CheckUserOutgoingApprovalPermission(ctx sdk.Context, detailsToCh
return CheckNotForbiddenForAllOverlaps(ctx, permissionDetails, detailsToCheck, permissionStr)
}

func (k Keeper) CheckUserIncomingApprovalPermission(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.UserIncomingApprovalPermission, permissionStr string) error {
func (k Keeper) CheckIfUserIncomingApprovalPermissionPermits(ctx sdk.Context, detailsToCheck []*types.UniversalPermissionDetails, permissions []*types.UserIncomingApprovalPermission, permissionStr string) error {
castedPermissions, err := k.CastUserIncomingApprovalPermissionToUniversalPermission(ctx, permissions)
if err != nil {
return err
Expand Down
20 changes: 10 additions & 10 deletions x/badges/keeper/update_checks_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (k Keeper) ValidateCollectionApprovalsUpdate(ctx sdk.Context, collection *t
return err
}

err = k.CheckCollectionApprovalPermission(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
err = k.CheckIfCollectionApprovalPermissionPermits(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
if err != nil {
return err
}
Expand All @@ -295,7 +295,7 @@ func (k Keeper) ValidateUserOutgoingApprovalsUpdate(ctx sdk.Context, collection
return err
}

err = k.CheckUserOutgoingApprovalPermission(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
err = k.CheckIfUserOutgoingApprovalPermissionPermits(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
if err != nil {
return err
}
Expand All @@ -312,7 +312,7 @@ func (k Keeper) ValidateUserIncomingApprovalsUpdate(ctx sdk.Context, collection
return err
}

err = k.CheckUserIncomingApprovalPermission(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
err = k.CheckIfUserIncomingApprovalPermissionPermits(ctx, detailsToCheck, CanUpdateCollectionApprovals, "update collection approved transfers")
if err != nil {
return err
}
Expand Down Expand Up @@ -366,7 +366,7 @@ func (k Keeper) ValidateBadgeMetadataUpdate(ctx sdk.Context, oldBadgeMetadata []
return err
}

err = k.CheckTimedUpdateWithBadgeIdsPermission(ctx, detailsToCheck, canUpdateBadgeMetadata, "update badge metadata")
err = k.CheckIfTimedUpdateWithBadgeIdsPermissionPermits(ctx, detailsToCheck, canUpdateBadgeMetadata, "update badge metadata")
if err != nil {
return err
}
Expand Down Expand Up @@ -399,7 +399,7 @@ func (k Keeper) ValidateCollectionMetadataUpdate(ctx sdk.Context, oldCollectionM
return err
}

err = k.CheckTimedUpdatePermission(ctx, detailsToCheck, canUpdateCollectionMetadata, "update collection metadata")
err = k.CheckIfTimedUpdatePermissionPermits(ctx, detailsToCheck, canUpdateCollectionMetadata, "update collection metadata")
if err != nil {
return err
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func (k Keeper) ValidateOffChainBalancesMetadataUpdate(ctx sdk.Context, collecti
return err
}

err = k.CheckTimedUpdatePermission(ctx, detailsToCheck, canUpdateOffChainBalancesMetadata, "update off chain balances metadata")
err = k.CheckIfTimedUpdatePermissionPermits(ctx, detailsToCheck, canUpdateOffChainBalancesMetadata, "update off chain balances metadata")
if err != nil {
return err
}
Expand Down Expand Up @@ -487,7 +487,7 @@ func (k Keeper) ValidateManagerUpdate(ctx sdk.Context, oldManager []*types.Manag
return err
}

if err = k.CheckTimedUpdatePermission(ctx, updatedTimelineTimes, canUpdateManager, "update manager"); err != nil {
if err = k.CheckIfTimedUpdatePermissionPermits(ctx, updatedTimelineTimes, canUpdateManager, "update manager"); err != nil {
return err
}

Expand All @@ -506,7 +506,7 @@ func (k Keeper) ValidateCustomDataUpdate(ctx sdk.Context, oldCustomData []*types
return err
}

if err = k.CheckTimedUpdatePermission(ctx, updatedTimelineTimes, canUpdateCustomData, "update custom data"); err != nil {
if err = k.CheckIfTimedUpdatePermissionPermits(ctx, updatedTimelineTimes, canUpdateCustomData, "update custom data"); err != nil {
return err
}

Expand Down Expand Up @@ -544,7 +544,7 @@ func (k Keeper) ValidateStandardsUpdate(ctx sdk.Context, oldStandards []*types.S
return err
}

if err = k.CheckTimedUpdatePermission(ctx, updatedTimelineTimes, canUpdateStandards, "update standards"); err != nil {
if err = k.CheckIfTimedUpdatePermissionPermits(ctx, updatedTimelineTimes, canUpdateStandards, "update standards"); err != nil {
return err
}

Expand All @@ -563,7 +563,7 @@ func (k Keeper) ValidateIsArchivedUpdate(ctx sdk.Context, oldIsArchived []*types
return err
}

if err = k.CheckTimedUpdatePermission(ctx, updatedTimelineTimes, canUpdateIsArchived, "update is archived"); err != nil {
if err = k.CheckIfTimedUpdatePermissionPermits(ctx, updatedTimelineTimes, canUpdateIsArchived, "update is archived"); err != nil {
return err
}

Expand Down
Loading

0 comments on commit ae1ae28

Please sign in to comment.