Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions aclmapping/bank/mappings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package aclbankmapping

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkacltypes "github.com/cosmos/cosmos-sdk/types/accesscontrol"
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
acltypes "github.com/cosmos/cosmos-sdk/x/accesscontrol/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
utils "github.com/sei-protocol/sei-chain/aclmapping/utils"
)

var ErrorInvalidMsgType = fmt.Errorf("invalid message received for bank module")

func GetBankDepedencyGenerator() aclkeeper.DependencyGeneratorMap {
dependencyGeneratorMap := make(aclkeeper.DependencyGeneratorMap)

// dex place orders
placeOrdersKey := acltypes.GenerateMessageKey(&banktypes.MsgSend{})
dependencyGeneratorMap[placeOrdersKey] = MsgSendDependencyGenerator

return dependencyGeneratorMap
}

// TODO:: we can make resource types more granular (e.g KV_PARAM or KV_BANK_BALANCE)
func MsgSendDependencyGenerator(keeper aclkeeper.Keeper, ctx sdk.Context, msg sdk.Msg) ([]sdkacltypes.AccessOperation, error) {
msgSend, ok := msg.(*banktypes.MsgSend)
if !ok {
return []sdkacltypes.AccessOperation{}, ErrorInvalidMsgType
}

accessOperations := []sdkacltypes.AccessOperation{
// MsgSend also checks if the coin denom is enabled, but the information is from the params.
// Changing the param would require a gov proposal, which is synchrounos by default

// Checks balance of sender
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.BANK, msgSend.FromAddress),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having the identifier template be per module, we should create more resource types to further segment the population. For example, under KV, we can have resource type KV_BANK, and then KV_BANK_BALANCE and KV_BANK_PARAMS, and then for the specific account we could have another resource type called KV_BANK_BALANCE_ACCOUNT. This way its much easier to have static dependencies that are granular enough to still allow for parallelization, and then the static read for this would be resource KV_BANK_BALANCE whereas the dynamic resource would be KV_BANK_BALANCE_ACCOUNT with the account identifier. We can represent this hierarchy in the resource hierarchy tree in resource.go in sei-cosmos.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrandonWeng lets leave a TODO at the top of the generator function to make the resources more granular down the road

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yessir, thanks! As discussed offline - we want to unblock the work first and this can be something we address later once we get the concurrency working

},
// Reduce the amount from the sender's balance
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.BANK, msgSend.FromAddress),
},

// Checks balance for receiver
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.BANK, msgSend.ToAddress),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.BANK, msgSend.ToAddress),
},

// Tries to create the reciever's account if it doesn't exist
{
AccessType: sdkacltypes.AccessType_READ,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.AUTH, msgSend.ToAddress),
},
{
AccessType: sdkacltypes.AccessType_WRITE,
ResourceType: sdkacltypes.ResourceType_KV,
IdentifierTemplate: utils.GetIdentifierTemplatePerModule(utils.AUTH, msgSend.ToAddress),
},

// Last Operation should always be a commit
{
ResourceType: sdkacltypes.ResourceType_ANY,
AccessType: sdkacltypes.AccessType_COMMIT,
IdentifierTemplate: utils.DefaultIDTemplate,
},
}
return accessOperations, nil
}
2 changes: 2 additions & 0 deletions aclmapping/dependency_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aclmapping
import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
aclbankmapping "github.com/sei-protocol/sei-chain/aclmapping/bank"
acldexmapping "github.com/sei-protocol/sei-chain/aclmapping/dex"
aclwasmmapping "github.com/sei-protocol/sei-chain/aclmapping/wasm"
)
Expand All @@ -19,6 +20,7 @@ func (customDepGen CustomDependencyGenerator) GetCustomDependencyGenerators() ac
dependencyGeneratorMap := make(aclkeeper.DependencyGeneratorMap)

dependencyGeneratorMap.Merge(acldexmapping.GetDexDependencyGenerators())
dependencyGeneratorMap.Merge(aclbankmapping.GetBankDepedencyGenerator())
wasmDependencyGenerators := aclwasmmapping.NewWasmDependencyGenerator(customDepGen.WasmKeeper)
dependencyGeneratorMap.Merge(wasmDependencyGenerators.GetWasmDependencyGenerators())

Expand Down
13 changes: 13 additions & 0 deletions aclmapping/utils/identifier_templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import "fmt"

const (
BANK = "bank"
AUTH = "auth"
DefaultIDTemplate = "*"
)

func GetIdentifierTemplatePerModule(module string, identifier string) string {
return fmt.Sprintf("%s/%s", module, identifier)
}