bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 34쪽
urn a continuous vesting account
}
else if ga.EndTime != 0 {
// return a delayed vesting account
}
else {
// invalid genesis vesting account provided
panic()
}
}
return bacc
}
```
## Examples
### Simple
Given a continuous vesting account with 10 vesting coins.
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
OV = 10
DF = 0
DV = 0
BC = 10
V = 10
V' = 0
```
1. Immediately receives 1 coin
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
BC = 11
```
2. Time passes, 2 coins vest
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
V = 8
V' = 2
```
3. Delegates 4 coins to validator A
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DV = 4
BC = 7
```
4. Sends 3 coins
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
BC = 4
```
5. More time passes, 2 more coins vest
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
V = 6
V' = 4
```
6. Sends 2 coins. At this point the account cannot send anymore until further
coins vest or it receives additional coins. It can still however, delegate.
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
BC = 2
```
### Slashing
Same initial starting conditions as the simple example.
1. Time passes, 5 coins vest
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
V = 5
V' = 5
```
2. Delegate 5 coins to validator A
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DV = 5
BC = 5
```
3. Delegate 5 coins to validator B
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DF = 5
BC = 0
```
4. Validator A gets slashed by 50%, making the delegation to A now worth 2.5 coins
5. Undelegate from validator A (2.5 coins)
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DF = 5 - 2.5 = 2.5
BC = 0 + 2.5 = 2.5
```
6. Undelegate from validator B (5 coins). The account at this point can only
send 2.5 coins unless it receives more coins or until more coins vest.
It can still however, delegate.
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DV = 5 - 2.5 = 2.5
DF = 2.5 - 2.5 = 0
BC = 2.5 + 5 = 7.5
```
Notice how we have an excess amount of `DV`.
### Periodic Vesting
A vesting account is created where 100 tokens will be released over 1 year, with
1/4 of tokens vesting each quarter. The vesting schedule would be as follows:
```yaml theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
Periods:
- amount: 25stake, length: 7884000
- amount: 25stake, length: 7884000
- amount: 25stake, length: 7884000
- amount: 25stake, length: 7884000
```
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
OV = 100
DF = 0
DV = 0
BC = 100
V = 100
V' = 0
```
1. Immediately receives 1 coin
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
BC = 101
```
2. Vesting period 1 passes, 25 coins vest
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
V = 75
V' = 25
```
3. During vesting period 2, 5 coins are transferred and 5 coins are delegated
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
DV = 5
BC = 91
```
4. Vesting period 2 passes, 25 coins vest
```text theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
V = 50
V' = 50
```
## Glossary
* OriginalVesting: The amount of coins (per denomination) that are initially
part of a vesting account. These coins are set at genesis.
* StartTime: The BFT time at which a vesting account starts to vest.
* EndTime: The BFT time at which a vesting account is fully vested.
* DelegatedFree: The tracked amount of coins (per denomination) that are
delegated from a vesting account that have been fully vested at time of delegation.
* DelegatedVesting: The tracked amount of coins (per denomination) that are
delegated from a vesting account that were vesting at time of delegation.
* ContinuousVestingAccount: A vesting account implementation that vests coins
linearly over time.
* DelayedVestingAccount: A vesting account implementation that only fully vests
all coins at a given time.
* PeriodicVestingAccount: A vesting account implementation that vests coins
according to a custom vesting schedule.
* PermanentLockedAccount: It does not ever release coins, locking them indefinitely.
Coins in this account can still be used for delegating and for governance votes even while locked.
## CLI
A user can query and interact with the `vesting` module using the CLI.
### Transactions
The `tx` commands allow users to interact with the `vesting` module.
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd tx vesting --help
```
#### create-periodic-vesting-account
The `create-periodic-vesting-account` command creates a new vesting account funded with an allocation of tokens, where a sequence of coins and period length in seconds. Periods are sequential, in that the duration of a period only starts at the end of the previous period. The duration of the first period starts upon account creation.
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd tx vesting create-periodic-vesting-account [to_address] [periods_json_file] [flags]
```
Example:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd tx vesting create-periodic-vesting-account cosmos1.. periods.json
```
#### create-vesting-account
The `create-vesting-account` command creates a new vesting account funded with an allocation of tokens. The account can either be a delayed or continuous vesting account, which is determined by the '--delayed' flag. All vesting accouts created will have their start time set by the committed block's time. The end\_time must be provided as a UNIX epoch timestamp.
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd tx vesting create-vesting-account [to_address] [amount] [end_time] [flags]
```
Example:
```bash theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
simd tx vesting create-vesting-account cosmos1.. 100stake 2592000
```
# x/authz
Source: https://docs.cosmos.network/sdk/latest/modules/authz/README
## Abstract
`x/authz` is an implementation of a Cosmos SDK module, per [ADR 30](https://github.com/cosmos/cosmos-sdk/blob/release/v0.55.x/docs/architecture/adr-030-authz-module.md), that allows
granting arbitrary privileges from one account (the granter) to another account (the grantee). Authorizations must be granted for a particular Msg service method one by one using an implementation of the `Authorization` interface.
## Contents
* [Concepts](#concepts)
* [Authorization and Grant](#authorization-and-grant)
* [Built-in Authorizations](#built-in-authorizations)
* [Gas](#gas)
* [State](#state)
* [Grant](#grant)
* [GrantQueue](#grantqueue)
* [Messages](#messages)
* [MsgGrant](#msggrant)
* [MsgRevoke](#msgrevoke)
* [MsgExec](#msgexec)
* [Events](#events)
* [Client](#client)
* [CLI](#cli)
* [gRPC](#grpc)
* [REST](#rest)
## Concepts
### Authorization and Grant
The `x/authz` module defines interfaces and messages grant authorizations to perform actions
on behalf of one account to other accounts. The design is defined in the [ADR 030](https://github.com/cosmos/cosmos-sdk/blob/release/v0.55.x/docs/architecture/adr-030-authz-module.md).
A *grant* is an allowance to execute a Msg by the grantee on behalf of the granter.
Authorization is an interface that must be implemented by a concrete authorization logic to validate and execute grants. Authorizations are extensible and can be defined for any Msg service method even outside of the module where the Msg method is defined. See the `SendAuthorization` example in the next section for more details.
**Note:** The authz module is different from the [auth (authentication)](/sdk/latest/modules/auth/auth/) module that is responsible for specifying the base transaction and account types.
```go expandable theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
package authz
import (
"github.com/cosmos/gogoproto/proto"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Authorization represents the interface of various Authorization types implemented
// by other modules.
type Authorization interface {
proto.Message
// MsgTypeURL returns the fully-qualified Msg service method URL (as described in ADR 031),
// which will process and accept or reject a request.
MsgTypeURL()
string
// Accept determines whether this grant permits the provided sdk.Msg to be performed,
// and if so provides an upgraded authorization instance.
Accept(ctx sdk.Context, msg sdk.Msg) (AcceptResponse, error)
// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
ValidateBasic()
error
}
// AcceptResponse instruments the controller of an authz message if the request is accepted
// and if it should be updated or deleted.
type AcceptResponse struct {
// If Accept=true, the controller can accept and authorization and handle the update.
Accept bool
// If Delete=true, the controller must delete the authorization object and release
// storage resources.
Delete bool
// Controller, who is calling Authorization.Accept must check if `Updated != nil`. If yes,
// it must use the updated version and handle the update on the storage level.
Updated Authorization
}
```
### Built-in Authorizations
The Cosmos SDK `x/authz` module comes with following authorization types:
#### GenericAuthorization
`GenericAuthorization` implements the `Authorization` interface that gives unrestricted permission to execute the provided Msg on behalf of granter's account.
```protobuf theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
// Reference: https://github.com/cosmos/cosmos-sdk/blob/release/v0.55.x/proto/cosmos/authz/v1beta1/authz.proto#L13-L21
```
```go expandable theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
package authz
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ Authorization = &GenericAuthorization{
}
// NewGenericAuthorization creates a new GenericAuthorization object.
func NewGenericAuthorization(msgTypeURL string) *GenericAuthorization {
return &GenericAuthorization{
Msg: msgTypeURL,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a GenericAuthorization)
MsgTypeURL()
string {
return a.Msg
}
// Accept implements Authorization.Accept.
func (a GenericAuthorization)
Accept(ctx sdk.Context, msg sdk.Msg) (AcceptResponse, error) {
return AcceptResponse{
Accept: true
}, nil
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a GenericAuthorization)
ValidateBasic()
error {
return nil
}
```
* `msg` stores Msg type URL.
#### SendAuthorization
`SendAuthorization` implements the `Authorization` interface for the `cosmos.bank.v1beta1.MsgSend` Msg.
* It takes a (positive) `SpendLimit` that specifies the maximum amount of tokens the grantee can spend. The `SpendLimit` is updated as the tokens are spent.
* It takes an (optional) `AllowList` that specifies to which addresses a grantee can send token.
```protobuf theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
// Reference: https://github.com/cosmos/cosmos-sdk/blob/release/v0.55.x/proto/cosmos/bank/v1beta1/authz.proto#L11-L29
```
```go expandable theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
// TODO: Revisit this once we have proper gas fee framework.
// Ref: https://github.com/cosmos/cosmos-sdk/issues/9054
// Ref: https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(10)
var _ authz.Authorization = &SendAuthorization{
}
// NewSendAuthorization creates a new SendAuthorization object.
func NewSendAuthorization(spendLimit sdk.Coins, allowed []sdk.AccAddress) *SendAuthorization {
return &SendAuthorization{
AllowList: toBech32Addresses(allowed),
SpendLimit: spendLimit,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a SendAuthorization)
MsgTypeURL()
string {
return sdk.MsgTypeURL(&MsgSend{
})
}
// Accept implements Authorization.Accept.
func (a SendAuthorization)
Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
mSend, ok := msg.(*MsgSend)
if !ok {
return authz.AcceptResponse{
}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
toAddr := mSend.ToAddress
limitLeft, isNegative := a.SpendLimit.SafeSub(mSend.Amount...)
if isNegative {
return authz.AcceptResponse{
}, sdkerrors.ErrInsufficientFunds.Wrapf("requested amount is more than spend limit")
}
if limitLeft.IsZero() {
return authz.AcceptResponse{
Accept: true,
Delete: true
}, nil
}
isAddrExists := false
allowedList := a.GetAllowList()
for _, addr := range allowedList {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "send authorization")
if addr == toAddr {
isAddrExists = true
break
}
}
if len(allowedList) > 0 && !isAddrExists {
return authz.AcceptResponse{
}, sdkerrors.ErrUnauthorized.Wrapf("cannot send to %s address", toAddr)
}
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &SendAuthorization{
SpendLimit: limitLeft,
AllowList: allowedList
}}, nil
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a SendAuthorization)
ValidateBasic()
error {
if a.SpendLimit == nil {
return sdkerrors.ErrInvalidCoins.Wrap("spend limit cannot be nil")
}
if !a.SpendLimit.IsAllPositive() {
return sdkerrors.ErrInvalidCoins.Wrapf("spend limit must be positive")
}
found := make(map[string]bool, 0)
for i := 0; i < len(a.AllowList); i++ {
if found[a.AllowList[i]] {
return ErrDuplicateEntry
}
found[a.AllowList[i]] = true
}
return nil
}
func toBech32Addresses(allowed []sdk.AccAddress) []string {
if len(allowed) == 0 {
return nil
}
allowedAddrs := make([]string, len(allowed))
for i, addr := range allowed {
allowedAddrs[i] = addr.String()
}
return allowedAddrs
}
```
* `spend_limit` keeps track of how many coins are left in the authorization.
* `allow_list` specifies an optional list of addresses to whom the grantee can send tokens on behalf of the granter.
#### StakeAuthorization
`StakeAuthorization` implements the `Authorization` interface for messages in the [staking module](/sdk/latest/modules/staking). It takes an `AuthorizationType` to specify whether you want to authorise delegating, undelegating, redelegating, or cancelling an unbonding delegation (i.e. these have to be authorised separately). It also takes an optional `MaxTokens` that keeps track of a limit to the amount of tokens that can be delegated/undelegated/redelegated. If left empty, the amount is unlimited. Additionally, this Msg takes an `AllowList` or a `DenyList`, which allows you to select which validators you allow or deny grantees to stake with.
```protobuf theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
// Reference: https://github.com/cosmos/cosmos-sdk/blob/release/v0.55.x/proto/cosmos/staking/v1beta1/authz.proto#L10-L33
```
```go expandable theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
// TODO: Revisit this once we have proper gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(10)
var _ authz.Authorization = &StakeAuthorization{
}
// NewStakeAuthorization creates a new StakeAuthorization object.
func NewStakeAuthorization(allowed []sdk.ValAddress, denied []sdk.ValAddress, authzType AuthorizationType, amount *sdk.Coin) (*StakeAuthorization, error) {
allowedValidators, deniedValidators, err := validateAllowAndDenyValidators(allowed, denied)
if err != nil {
return nil, err
}
a := StakeAuthorization{
}
if allowedValidators != nil {
a.Validators = &StakeAuthorization_AllowList{
AllowList: &StakeAuthorization_Validators{
Address: allowedValidators
}}
}
else {
a.Validators = &StakeAuthorization_DenyList{
DenyList: &StakeAuthorization_Validators{
Address: deniedValidators
}}
}
if amount != nil {
a.MaxTokens = amount
}
a.AuthorizationType = authzType
return &a, nil
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a StakeAuthorization)
MsgTypeURL()
string {
authzType, err := normalizeAuthzType(a.AuthorizationType)
if err != nil {
panic(err)
}
return authzType
}
func (a StakeAuthorization)
ValidateBasic()
error {
if a.MaxTokens != nil && a.MaxTokens.IsNegative() {
return sdkerrors.Wrapf(authz.ErrNegativeMaxTokens, "negative coin amount: %v", a.MaxTokens)
}
if a.AuthorizationType == AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED {
return authz.ErrUnknownAuthorizationType
}
return nil
}
// Accept implements Authorization.Accept.
func (a StakeAuthorization)
Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
var validatorAddress string
var amount sdk.Coin
switch msg := msg.(type) {
case *MsgDelegate:
validatorAddress = msg.ValidatorAddress
amount = msg.Amount
case *MsgUndelegate:
validatorAddress = msg.ValidatorAddress
amount = msg.Amount
case *MsgBeginRedelegate:
validatorAddress = msg.ValidatorDstAddress
amount = msg.Amount
default:
return authz.AcceptResponse{
}, sdkerrors.ErrInvalidRequest.Wrap("unknown msg type")
}
isValidatorExists := false
allowedList := a.GetAllowList().GetAddress()
for _, validator := range allowedList {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
isValidatorExists = true
break
}
}
denyList := a.GetDenyList().GetAddress()
for _, validator := range denyList {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
return authz.AcceptResponse{
}, sdkerrors.ErrUnauthorized.Wrapf("cannot delegate/undelegate to %s validator", validator)
}
}
if len(allowedList) > 0 && !isValidatorExists {
return authz.AcceptResponse{
}, sdkerrors.ErrUnauthorized.Wrapf("cannot delegate/undelegate to %s validator", validatorAddress)
}
if a.MaxTokens == nil {
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &StakeAuthorization{
Validators: a.GetValidators(),
AuthorizationType: a.GetAuthorizationType()
},
}, nil
}
limitLeft, err := a.MaxTokens.SafeSub(amount)
if err != nil {
return authz.AcceptResponse{
}, err
}
if limitLeft.IsZero() {
return authz.AcceptResponse{
Accept: true,
Delete: true
}, nil
}
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &StakeAuthorization{
Validators: a.GetValidators(),
AuthorizationType: a.GetAuthorizationType(),
MaxTokens: &limitLeft
},
}, nil
}
func validateAllowAndDenyValidators(allowed []sdk.ValAddress, denied []sdk.ValAddress) ([]string, []string, error) {
if len(allowed) == 0 && len(denied) == 0 {
return nil, nil, sdkerrors.ErrIn프로젝트로 돌아가기