Skip to content
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

Problem: Account projection balance is relying on Cosmos real time data #383

Open
calvinaco opened this issue Apr 17, 2021 · 3 comments
Open
Assignees

Comments

@calvinaco
Copy link
Collaborator

Should use GenesisCreated and AccountTransfer event to construct the balance

@calvinaco calvinaco added this to the v1.0.0 releases milestone May 19, 2021
@davcrypto davcrypto self-assigned this Aug 18, 2021
@davcrypto
Copy link
Contributor

Tired to get the balance via events in txs results but looks like some fee are not included for failed tx (but deducted from wallet)

ref: cosmos/cosmos-sdk#10618

@davcrypto
Copy link
Contributor

davcrypto commented Dec 28, 2021

Account Usable Balance

Altering go deeper into cosmos-sdk and take some experimental implementation. Usable balance is proved can be only invoke inside the bank module and every invocation emit events. As a result, this balance can be successfully accumulated by combining the following means base on v0.44.5 cosmos-sdk

Genesis block account balance

All initial balance stated inside genesis block

{
  "jsonrpc": "2.0",
  "id": -1,
  "result": {
    "genesis": {
      ...
      "app_state": {
        ...
        "bank": {
          "params": {
            "send_enabled": [],
            "default_send_enabled": true
          },
          "balances": [
            {
              "address": "tcro1s4ggq2zuzvwg5k8vnx2xfwtdm4cz6wtnfl4xx7",
              "coins": [
                {
                  "denom": "basetcro",
                  "amount": "840000000000000000"
                }
              ]
            },
            {
              "address": "tcro14fzksz5h72et4ssjtqpwsmhz6ysk6r4ngtg6zj",
              "coins": [
                {
                  "denom": "basetcro",
                  "amount": "840000000000000000"
                }
              ]
            },
            {
              "address": "tcro163tv59yzgeqcap8lrsa2r4zk580h8ddrpzvf4w",
              "coins": [
                {
                  "denom": "basetcro",
                  "amount": "840000000000000000"
                }
              ]
            }
          ],
          ...
        },
        ...
    }
  }
}

Accumulate balance by events emitted

Begin Block Events

Event emitted in here would not have any execution in term of the bank module. We can skip this part for usable balance.

Block Events

Event emitted would include all actually actions taken for every tx inside the blocks. Events related to usable balance would be:

  • coin_spent
    • Balance spent for actions
  • coin_received
    • Balance received for actions
  • coinbase
    • Balance minted
  • burn
    • Balance burned

End Block Events

Event emitted would include all the actually actions after txs was executed inside the blocks. Things like release unbonded tokens. Events related to usable balance would be:

  • coin_spent
    • Balance spent for actions
  • coin_received
    • Balance received for actions
  • coinbase
    • Balance minted
  • burn
    • Balance burned

Fee paid from failed transactions

No event would be emitted if the transaction was failed. However, the transaction fee is actually paid. Normally, paid fee would emitted in a coin_spent event. But for failed transaction case, we will need to detect it separately since the event would not be there.

Fee payer

Usually, the first signer is responsible for paying the fees. But there might exceptional case due to FeeGrant module. In order to get the actual fee payer from block result, we can take the fee property inside block data and refer to the description in below reference.

// Fee includes the amount of coins paid in fees and the maximum
// gas to be used by the transaction. The ratio yields an effective "gasprice",
// which must be above some miminum to be accepted into the mempool.
type Fee struct {
	// amount is the amount of coins to be paid as a fee
	Amount github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,1,rep,name=amount,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"amount"`
	// gas_limit is the maximum gas that can be used in transaction processing
	// before an out of gas error occurs
	GasLimit uint64 `protobuf:"varint,2,opt,name=gas_limit,json=gasLimit,proto3" json:"gas_limit,omitempty"`
	// if unset, the first signer is responsible for paying the fees. If set, the specified account must pay the fees.
	// the payer must be a tx signer (and thus have signed this field in AuthInfo).
	// setting this field does *not* change the ordering of required signers for the transaction.
	Payer string `protobuf:"bytes,3,opt,name=payer,proto3" json:"payer,omitempty"`
	// if set, the fee payer (either the first signer or the value of the payer field) requests that a fee grant be used
	// to pay fees instead of the fee payer's own balance. If an appropriate fee grant does not exist or the chain does
	// not support fee grants, this will fail
	Granter string `protobuf:"bytes,4,opt,name=granter,proto3" json:"granter,omitempty"`
}

Fee payer reference: https://github.com/cosmos/cosmos-sdk/blob/master/types/tx/tx.pb.go#L778

@davcrypto
Copy link
Contributor

Thanks @calvinaco pointed out that these coin-related events on existed after cosmos SDK v0.43. Regarding on this, we may need to do via components on outer layer to trace the traffic.

Below are trigger points for those coin activities on cosmos SDK

coin_spent:

  • x/staking/keeper/delegation.go L757
    • CompleteUnbonding
  • x/gov/keeper/deposit.go L57
    • DeleteDeposits
  • x/staking/keeper/slash.go L24
    • Slash/burnBondedTokens
    • Slash/burnNotBondedTokens
  • x/bank/keeper/msg_server.go L73
    • MultiSend
  • x/auth/vesting/msg_server.go
    • CreateVestingAccount
  • x/bank/keeper/msg_server.go L26
    • Send
  • x/distribution/keeper/keeper.go L85
    • WithdrawDelegationRewards
  • x/distribution/keeper/fee_pool.go L10
    • DistributeFromFeePool
  • x/distribution/keeper/hooks.go L26
    • AfterValidatorRemoved
  • x/distribution/keeper/keeper.go L116
    • WithdrawValidatorCommission
  • x/gov/keeper/deposit.go L170
    • RefundDeposits
  • x/distribution/keeper/allocation.go L16
    • AllocateTokens
  • x/mint/keeper/keeper.go L108
    • AddCollectedFees
  • x/staking/keeper/delegation.go L547
    • notBondedTokensToBonded
    • bondedTokensToNotBonded
  • x/staking/keeper/val_state_change.go L112
    • notBondedTokensToBonded
    • bondedTokensToNotBonded
  • x/staking/keeper/delegation.go L725
    • Undelegate
  • x/auth/ante/fee.go L129
    • DeductFees
  • x/crisis/keeper/msg_server.go L12
    • VerifyInvariant
  • x/distribution/keeper/msg_server.go L125
    • FundCommunityPool
  • x/gov/keeper/msg_server.go L27
    • SubmitProposal
  • x/gov/keeper/msg_server.go L122
    • Deposit
  • x/staking/keeper/msg_server.go L248
    • BeginRedelegate
  • x/staking/keeper/msg_server.go L30
    • CreateValidator
  • x/staking/keeper/msg_server.go L189
    • Delegate

coin_receipt:

  • x/staking/keeper/msg_server.go L248
    • BeginRedelegate
  • x/staking/keeper/msg_server.go L30
    • CreateValidator
  • x/staking/keeper/msg_server.go L189
    • Delegate
  • x/staking/keeper/delegation.go L757
    • CompleteUnbonding
  • x/mint/keeper/keeper.go L97
    • MintCoins
  • x/bank/keeper/msg_server.go L73
    • MultiSend
  • x/auth/vesting/msg_server.go
    • CreateVestingAccount
  • x/bank/keeper/msg_server.go L26
    • Send
  • x/distribution/keeper/keeper.go L85
    • WithdrawDelegationRewards
  • x/distribution/keeper/fee_pool.go L10
    • DistributeFromFeePool
  • x/distribution/keeper/hooks.go L26
    • AfterValidatorRemoved
  • x/distribution/keeper/keeper.go L116
    • WithdrawValidatorCommission
  • x/gov/keeper/deposit.go L170
    • RefundDeposits
  • x/distribution/keeper/allocation.go L16
    • AllocateTokens
  • x/mint/keeper/keeper.go L108
    • AddCollectedFees
  • x/staking/keeper/delegation.go L547
    • notBondedTokensToBonded
    • bondedTokensToNotBonded
  • x/staking/keeper/val_state_change.go L112
    • notBondedTokensToBonded
    • bondedTokensToNotBonded
  • x/staking/keeper/delegation.go L725
    • Undelegate
  • x/auth/ante/fee.go L129
    • DeductFees
  • x/crisis/keeper/msg_server.go L12
    • VerifyInvariant
  • x/distribution/keeper/msg_server.go L125
    • FundCommunityPool
  • x/gov/keeper/msg_server.go L27
    • SubmitProposal
  • x/gov/keeper/msg_server.go L122
    • Deposit

coinbase:

  • x/mint/keeper/keeper.go L97
    • MintCoins

burn:

  • x/gov/keeper/deposit.go L57
    • DeleteDeposits
  • x/staking/keeper/slash.go L24
    • Slash/burnBondedTokens
    • Slash/burnNotBondedTokens

Plus all the IBC activities trigger point on ibc-go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants