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

Bump font weight docs website and fix link #5391

Merged
merged 254 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
254 commits
Select commit Hold shift + click to select a range
4a86564
working
gamarin2 May 24, 2019
a9e303c
querier
gamarin2 May 28, 2019
0913fb6
working
gamarin2 Jun 3, 2019
167104c
merg
gamarin2 Jun 3, 2019
6346e8b
workiiiing
gamarin2 Jun 5, 2019
23f8f78
finish
gamarin2 Jun 5, 2019
9c2fd4e
add dep and makefile
gamarin2 Jun 5, 2019
9baaed7
Apply suggestions from code review
gamarin2 Jun 5, 2019
45f5654
typo
gamarin2 Jun 5, 2019
265ed8e
typo
gamarin2 Jun 5, 2019
0a83404
Apply suggestions from code review
gamarin2 Jun 6, 2019
5f883fd
refactor for new module interface
gamarin2 Jun 10, 2019
b06fafc
pull
gamarin2 Jun 10, 2019
dde15ae
karoly review
gamarin2 Jun 11, 2019
da2514f
Apply suggestions from code review
gamarin2 Jun 11, 2019
56c3f5b
encoding
gamarin2 Jun 11, 2019
4f7c6b4
working on baseapp doc
gamarin2 Jun 17, 2019
e37310f
baseapp work
gamarin2 Jun 27, 2019
92a0db9
reorg
gamarin2 Jun 27, 2019
94664fe
almost there
gamarin2 Jul 5, 2019
2624fec
finish first draft
gamarin2 Jul 5, 2019
9aedc16
Merge branch 'master' into gamarin/baseapp
gamarin2 Jul 5, 2019
3496625
remove old files
gamarin2 Jul 5, 2019
c90c997
module doc start
gamarin2 Jul 10, 2019
1239fcb
finish intro
gamarin2 Jul 11, 2019
a2e24e8
working
gamarin2 Jul 15, 2019
0f1b61d
workinnn
gamarin2 Jul 18, 2019
c5bb538
add transactions into core
glozow Jul 18, 2019
3c7d388
hans comments
glozow Jul 25, 2019
41e922c
add transactions into core
glozow Jul 18, 2019
37a2f11
final edits
glozow Aug 8, 2019
c3623e9
working
gamarin2 Aug 8, 2019
1e70aaf
gautier comments
glozow Aug 10, 2019
e8f59d2
merge
gamarin2 Aug 20, 2019
81cf8e6
clean
gamarin2 Aug 20, 2019
a6788cd
working
gamarin2 Aug 20, 2019
16d78da
consolidate intro
gamarin2 May 20, 2019
66dd675
querier
gamarin2 May 28, 2019
6d1df2e
workiiiing
gamarin2 Jun 5, 2019
afbaba5
refactor for new module interface
gamarin2 Jun 10, 2019
a039341
karoly review
gamarin2 Jun 11, 2019
dc97fb5
working on baseapp doc
gamarin2 Jun 17, 2019
24570fe
baseapp work
gamarin2 Jun 27, 2019
2c412ec
reorg
gamarin2 Jun 27, 2019
f783f03
almost there
gamarin2 Jul 5, 2019
653ada2
finish first draft
gamarin2 Jul 5, 2019
0fc3c9a
remove old files
gamarin2 Jul 5, 2019
7b1f376
finish intro
gamarin2 Jul 11, 2019
023c31e
workinnn
gamarin2 Jul 18, 2019
033af57
initial commit after rebase
glozow Jul 20, 2019
f342f3a
query-lifecycle and started modules-interfaces
glozow Jul 20, 2019
f766dc2
query-lifecycle first draft done
glozow Jul 21, 2019
45fb5f1
module interfaces first draft
glozow Jul 23, 2019
a77f607
rest and intro skeletons
glozow Jul 26, 2019
0284332
rest and intro done
glozow Jul 26, 2019
54ced86
small edits and links
glozow Jul 26, 2019
e3d819e
comments
glozow Jul 31, 2019
4f040c4
revisions
glozow Aug 2, 2019
0be9410
cli.md comments
glozow Aug 12, 2019
62e1da2
comments
glozow Aug 14, 2019
8faeacb
minor edits
glozow Aug 14, 2019
936afcc
better flow for query lifecycle
glozow Aug 20, 2019
93a9fab
add transactions into core
glozow Jul 18, 2019
54ee067
hans comments
glozow Jul 25, 2019
de0aebf
add transactions into core
glozow Jul 18, 2019
e6123bd
merge
glozow Aug 20, 2019
207132c
checkout master-docs files
glozow Aug 21, 2019
cf60c92
deleted some
glozow Aug 21, 2019
8f9c36c
remove modules readme
glozow Aug 21, 2019
38c9ab9
cli.md comments
glozow Aug 28, 2019
19fee81
comments
glozow Aug 28, 2019
c685c34
module-interfaces comments
glozow Aug 28, 2019
cef7b4c
Merge PR #4857: Add Context concept doc
glozow Aug 28, 2019
d96abfd
working
gamarin2 Sep 9, 2019
f38fd48
working
gamarin2 Sep 9, 2019
c7ddc70
finish messages and queries
gamarin2 Sep 10, 2019
f7b6fc3
handler
gamarin2 Sep 10, 2019
8b1ac72
querier
gamarin2 Sep 10, 2019
6f39d98
last comments!
glozow Sep 10, 2019
02bfe22
punctuation
glozow Sep 10, 2019
5162d2e
querier2
gamarin2 Sep 10, 2019
a33f0db
Merge pull request #4704 from cosmos/gloria/transactions-doc
gamarin2 Sep 10, 2019
25fdbf2
consolidate intro
gamarin2 May 20, 2019
a6bd71e
querier
gamarin2 May 28, 2019
2101a75
workiiiing
gamarin2 Jun 5, 2019
8ee9c4a
refactor for new module interface
gamarin2 Jun 10, 2019
cefe24d
karoly review
gamarin2 Jun 11, 2019
c3bdae6
working on baseapp doc
gamarin2 Jun 17, 2019
919ced4
baseapp work
gamarin2 Jun 27, 2019
a8a06b1
reorg
gamarin2 Jun 27, 2019
6bfd175
almost there
gamarin2 Jul 5, 2019
8c6c26b
finish first draft
gamarin2 Jul 5, 2019
96d0441
remove old files
gamarin2 Jul 5, 2019
082ef4c
finish intro
gamarin2 Jul 11, 2019
00e88d8
workinnn
gamarin2 Jul 18, 2019
996e47d
initial commit after rebase
glozow Jul 20, 2019
5b10675
query-lifecycle and started modules-interfaces
glozow Jul 20, 2019
b6e4e17
query-lifecycle first draft done
glozow Jul 21, 2019
a708cc2
module interfaces first draft
glozow Jul 23, 2019
9affd0f
rest and intro skeletons
glozow Jul 26, 2019
cf13f00
rest and intro done
glozow Jul 26, 2019
d64ce4c
small edits and links
glozow Jul 26, 2019
cab3c2e
comments
glozow Jul 31, 2019
2ec8d94
revisions
glozow Aug 2, 2019
f53347f
cli.md comments
glozow Aug 12, 2019
d861ad7
comments
glozow Aug 14, 2019
a154691
minor edits
glozow Aug 14, 2019
3c7d671
better flow for query lifecycle
glozow Aug 20, 2019
463bffa
checkout master-docs files
glozow Aug 21, 2019
5b24d33
deleted some
glozow Aug 21, 2019
e96b166
remove modules readme
glozow Aug 21, 2019
0444f02
cli.md comments
glozow Aug 28, 2019
70fd35c
comments
glozow Aug 28, 2019
ee08a01
module-interfaces comments
glozow Aug 28, 2019
6343e60
Merge branch 'gloria/interfaces-docs' of github.com:cosmos/cosmos-sdk…
jackzampolin Sep 11, 2019
b4d1657
keeper
gamarin2 Sep 12, 2019
190c5d3
genesis
gamarin2 Sep 12, 2019
d2a2d91
finish
gamarin2 Sep 13, 2019
f3d307a
Merge branch 'master-docs' into gamarin/building-modules
gamarin2 Sep 13, 2019
c09597f
Apply suggestions from code review
gamarin2 Sep 16, 2019
e20c6d3
hans review
gamarin2 Sep 16, 2019
628ad72
Update docs/core/baseapp.md
gamarin2 Sep 16, 2019
9fa66e4
working
gamarin2 Sep 17, 2019
6914e0d
last comment
glozow Sep 17, 2019
47c9a28
Merge pull request #4755 from cosmos/gloria/interfaces-docs
gamarin2 Sep 17, 2019
036f418
workin
gamarin2 Sep 17, 2019
7392275
Merge branch 'master-docs' into gamarin/finish-docs
gamarin2 Sep 17, 2019
01e79c8
Apply suggestions from code review
fedekunze Oct 1, 2019
c151b31
encoding and node
gamarin2 Oct 4, 2019
5ef23b0
almost finish store
gamarin2 Oct 11, 2019
a8c51b7
merge
gamarin2 Oct 11, 2019
bc6afa6
merge
gamarin2 Oct 11, 2019
c44e476
Merge branch 'gamarin/building-modules' of https://github.com/cosmos/…
gamarin2 Oct 11, 2019
1d5d907
merge
gamarin2 Oct 11, 2019
7af02f8
finish docs
gamarin2 Oct 11, 2019
2b419e9
fixes
gamarin2 Oct 11, 2019
d3a168a
Merge pull request #5037 from cosmos/gamarin/building-modules
gamarin2 Oct 11, 2019
6ee28c4
fede comments + permalinks
gamarin2 Oct 18, 2019
e13a4bc
hans review
gamarin2 Oct 21, 2019
1cc5c19
add more permalinks
gamarin2 Oct 23, 2019
6d67876
Merge pull request #5186 from cosmos/gamarin/finish-docs
gamarin2 Oct 23, 2019
0d4e30c
update docs theme version (#5239)
fadeev Oct 24, 2019
6291948
merge master
gamarin2 Oct 27, 2019
fb63c07
R4R: Docs Cleanup (#5246)
gamarin2 Oct 28, 2019
834e14b
fix links
gamarin2 Nov 2, 2019
2b418b5
remove dup
gamarin2 Nov 2, 2019
4521f3c
remove dup
gamarin2 Nov 2, 2019
b97e615
remove dup
gamarin2 Nov 2, 2019
f1cc432
remove dup
gamarin2 Nov 2, 2019
f1ea40c
remove dup
gamarin2 Nov 2, 2019
5e4d9e9
fix links
gamarin2 Nov 2, 2019
0103f64
add subscribe events
gamarin2 Nov 5, 2019
72b821a
refine rest
gamarin2 Nov 5, 2019
3e55e78
index page
fadeev Nov 7, 2019
1b4ce52
sidebar
fadeev Nov 7, 2019
f0286af
theme version
fadeev Nov 7, 2019
1216369
theme version
fadeev Nov 7, 2019
1427288
testing netlify
fadeev Nov 7, 2019
fa048b8
theme version
fadeev Nov 7, 2019
9b32df7
tooltip example
fadeev Nov 8, 2019
f958a9b
version
fadeev Nov 9, 2019
eef7f62
testing code embedding
fadeev Nov 10, 2019
699d1f1
reverting back
fadeev Nov 10, 2019
885b2f8
theme version
fadeev Nov 10, 2019
dcdb101
version
fadeev Nov 10, 2019
e0b216f
version
fadeev Nov 10, 2019
3716b05
version
fadeev Nov 11, 2019
30dae64
readme and version
fadeev Nov 12, 2019
483d597
cleanup
gamarin2 Nov 13, 2019
77fc987
Merge branch 'master-docs' into docs-theme-latest
gamarin2 Nov 13, 2019
deffa90
redo app anatomy
gamarin2 Nov 13, 2019
e71cc65
Merge branch 'master-docs' into docs-theme-latest
gamarin2 Nov 13, 2019
aae2d9f
modules readme, theme version
fadeev Nov 13, 2019
14623ca
theme version
fadeev Nov 13, 2019
604e414
fix modules list
fadeev Nov 13, 2019
9505e33
theme version
fadeev Nov 14, 2019
50d8538
new snippets
gamarin2 Nov 15, 2019
dd2fc20
Merge branch 'master-docs' into docs-theme-latest
gamarin2 Nov 15, 2019
47d3115
modules readme
fadeev Nov 16, 2019
2d3d229
Merge branch 'docs-theme-latest' of https://github.com/cosmos/cosmos-…
fadeev Nov 16, 2019
d088829
update docs readme
gamarin2 Nov 18, 2019
6de6087
modify synopsis
gamarin2 Nov 18, 2019
7d62116
version
fadeev Nov 19, 2019
376c8dd
Merge branch 'master-docs' into docs-theme-latest
fadeev Nov 19, 2019
67bcf80
fix yaml
fadeev Nov 19, 2019
990a4d4
version
fadeev Nov 19, 2019
dbedb92
version
fadeev Nov 19, 2019
150566f
version
fadeev Nov 19, 2019
1a8278f
version
fadeev Nov 19, 2019
29956aa
version
fadeev Nov 20, 2019
5a4d439
version
fadeev Nov 21, 2019
cc57ed4
version
fadeev Nov 21, 2019
132aa27
version
fadeev Nov 21, 2019
d152ffe
version
fadeev Nov 22, 2019
90b184c
version
fadeev Nov 23, 2019
6aabc5a
add hide banner
gamarin2 Nov 25, 2019
01cd19b
version
fadeev Nov 25, 2019
2fa42e1
version
fadeev Nov 28, 2019
2a6cd2b
Merge branch 'master-docs' into docs-theme-latest
fadeev Dec 2, 2019
0ada884
version
fadeev Dec 2, 2019
d63bf84
small fixes
gamarin2 Dec 2, 2019
9dd99f1
modules readme, version
fadeev Dec 6, 2019
f388057
remove hotkeys dep, version
fadeev Dec 6, 2019
097d05e
version
fadeev Dec 6, 2019
a7d5523
version
fadeev Dec 6, 2019
f4e319d
version
fadeev Dec 6, 2019
a664b8d
version
fadeev Dec 8, 2019
786e719
version
fadeev Dec 9, 2019
c1c13a3
version
fadeev Dec 9, 2019
efe58c9
version
fadeev Dec 9, 2019
e3d62d8
slight notice
gamarin2 Dec 9, 2019
55dcc49
fix links and hide
gamarin2 Dec 9, 2019
565101d
permalinks
gamarin2 Dec 9, 2019
f7a7f26
small clean
gamarin2 Dec 9, 2019
ebf8fbf
Merge branch 'master-docs' into docs-theme-latest
gamarin2 Dec 9, 2019
40a56dd
merge
gamarin2 Dec 9, 2019
c03aaee
version
fadeev Dec 9, 2019
1fc0701
Merge branch 'docs-theme-latest' of https://github.com/cosmos/cosmos-…
fadeev Dec 9, 2019
d70bfa5
resolve conflicts, add google analytics
fadeev Dec 9, 2019
c705394
fix merge remants
gamarin2 Dec 9, 2019
2004b3f
version
fadeev Dec 10, 2019
cfba835
changelog 1/2
gamarin2 Dec 10, 2019
d34481b
Changelog: docs UI
fadeev Dec 10, 2019
2a52470
version
fadeev Dec 10, 2019
0ca6a8b
remove merge conflicts
fadeev Dec 10, 2019
1031cc4
remove merge conflicts
fadeev Dec 10, 2019
d6027cf
Code: Update link for Contributing to the docs to docs_readme
fadeev Dec 11, 2019
7f73d49
HTML/CSS: Update layout of homepage footer to match new layout in Figma
fadeev Dec 11, 2019
a0a89a6
version
fadeev Dec 11, 2019
10b625b
Merge branch 'master' into master-docs
gamarin2 Dec 11, 2019
69af384
final modifs
gamarin2 Dec 11, 2019
4d9fff0
merge
gamarin2 Dec 11, 2019
93252f2
modules, version
fadeev Dec 11, 2019
d7d0198
Merge branch 'docs-theme-latest' of https://github.com/cosmos/cosmos-…
fadeev Dec 11, 2019
53f2cff
modules readme
fadeev Dec 11, 2019
81ee608
link to module list from homepage
fadeev Dec 11, 2019
4f9bd1c
version
fadeev Dec 11, 2019
e11a866
building modules link
fadeev Dec 11, 2019
7a7cb12
version
fadeev Dec 11, 2019
211040f
version
fadeev Dec 11, 2019
c4898f5
fonts
fadeev Dec 11, 2019
68f6c4f
version
fadeev Dec 11, 2019
f775771
version
fadeev Dec 11, 2019
9897728
fix link
gamarin2 Dec 11, 2019
287307a
merge conflicts
fadeev Dec 11, 2019
05d8b28
fix package.json
fadeev Dec 11, 2019
e5cc921
Merge branch 'master' into docs-theme-latest
alexanderbez Dec 11, 2019
8b57449
links in explore sdk section
fadeev Dec 11, 2019
05b0d86
Merge branch 'docs-theme-latest' of https://github.com/cosmos/cosmos-…
fadeev Dec 11, 2019
19e85ed
core concepts
fadeev Dec 11, 2019
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
Prev Previous commit
Next Next commit
encoding and node
  • Loading branch information
gamarin2 committed Oct 4, 2019
commit c151b31a2bbcfca01473013550c25ea00039f884
147 changes: 136 additions & 11 deletions docs/basics/accounts-fees-gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ This document describes the in-built accounts system of the Cosmos SDK, as well

- [Accounts](#accounts)
+ [KeyBase](#keybase)
+ [Addresses](#addresses)
+ [Signatures](#signatures)
+ [Addresses and Pubkeys](#addresses-and-pubkeys)
* [Pubkeys](#pubkeys)
* [Addresses](#addresses)
- [Fees and Gas](#fees-and-gas)
+ [AnteHandler](#antehandler)
+ [Gas](#gas)
+ [Gas Meter](#gas-meter)
+ [Block Gas Meter](#block-gas-meter)
* [Main Gas Meter](#main-gas-meter)
* [Block Gas Meter](#block-gas-meter)
+ [AnteHandler](#antehandler)

## Accounts

Expand Down Expand Up @@ -122,7 +123,7 @@ type Keybase interface {

The default implementation of `Keybase` of the Cosmos SDK is [`dbKeybase`](https://github.com/cosmos/cosmos-sdk/blob/master/crypto/keys/keybase.go). A few notes on the `Keybase` methods as implemented in `dbKeybase`:

- `Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error)` strictly deals with the signature of the `message` bytes. Some preliminary work should be done beforehand to prepare and encode the `message` into a canonical `[]byte` form. See an example of `message` preparation from the `auth` module](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/types/txbuilder.go#L177-L207).
- `Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error)` strictly deals with the signature of the `message` bytes. Some preliminary work should be done beforehand to prepare and encode the `message` into a canonical `[]byte` form. See an example of `message` preparation from the `auth` module](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/types/txbuilder.go#L177-L207). Note that signature verification is not implemented in the SDK by default. It is deferred to the [`anteHandler`](#antehandler).
- `CreateMnemonic(name string, language Language, passwd string, algo SigningAlgo) (info Info, seed string, err error)` creates a new mnemonic and prints it in the logs, but it **does not persist it on disk**.
- `CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (Info, error)` creates a new account based on the [`bip44 path`](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) and persists it on disk (note that the `PrivKey` is [encrypted with a passphrase before being persisted](https://github.com/cosmos/cosmos-sdk/blob/master/crypto/keys/mintkey/mintkey.go), it is **never stored unencrypted**). In the context of this method, the `account` and `address` parameters refer to the segment of the BIP44 derivation path (e.g. `0`, `1`, `2`, ...) used to derive the `PrivKey` and `PubKey` from the mnemonic (note that given the same mnemonic and `account`, the same `PrivKey` will be generated, and given the same `account` and `address`, the same `PubKey` and `Address` will be generated). Finally, note that the `CreateAccount` method derives keys and addresses using `secp256k1` as implemented in the [Tendermint library](https://github.com/tendermint/tendermint/blob/master/crypto/secp256k1). As a result, it only works for creating account keys and addresses, not consensus keys. See [`Addresses`](#addresses) for more.

Expand Down Expand Up @@ -150,16 +151,140 @@ With the `lazyKeybase`, it is possible for the [command-line interface](../inter

- Addresses and Keys for **accounts**, which identify users (e.g. the sender of a `message`). They are derived using the **`secp256k1`** curve.
- Addresses and Keys for **validator operators**, which identify the operators of validators. They are derived using the **`secp256k1`** curve.
- Addresses and Keys for **consensus nodes**, which identify the validator nodes participating in consensus. They are derived using the **`ed25519`** curve.
- Addresses and Keys for **consensus nodes**, which identify the validator nodes participating in consensus. They are derived using the **`ed25519`** curve.

| | Address bech32 Prefix | Pubkey bech32 Prefix | Curve | Address byte length | Pubkey byte length |
|--------------------|-----------------------|----------------------|-------------|---------------------|--------------------|
| Accounts | cosmos | cosmospub | `secp256k1` | `20` | `33` |
| Validator Operator | cosmosvaloper | cosmosvaloperpub | `secp256k1` | `20` | `33` |
| Consensus Nodes | cosmosvalcons | cosmosvalconspub | `ed25519` | `20` | `32` |

#### PubKeys

`PubKey`s used in the Cosmos SDK follow the [`Pubkey` interface defined in [tendermint's `crypto` package](https://github.com/tendermint/tendermint/blob/master/crypto/crypto.go#L22-L27):


```go
type PubKey interface {
Address() Address
Bytes() []byte
VerifyBytes(msg []byte, sig []byte) bool
Equals(PubKey) bool
}
```

For `secp256k1` keys, the actual implementation can be found [here](https://github.com/tendermint/tendermint/blob/master/crypto/secp256k1/secp256k1.go#L140). For `ed25519` keys, it can be found [here](https://github.com/tendermint/tendermint/blob/master/crypto/ed25519/ed25519.go#L135).

Note that in the Cosmos SDK, `Pubkeys` are not manipulated in their raw form. Instead, they are double encoded using [`Amino`](../core/encoding.md#amino) and [`bech32`](https://en.bitcoin.it/wiki/Bech32). In the SDK is done by first calling the `Bytes()` method on the raw `Pubkey` (which applies amino encoding), and then the `ConvertAndEncode` method of `bech32`. You can check out the implementation [here](https://github.com/cosmos/cosmos-sdk/blob/master/types/address.go#L579-L729).

#### Addresses

The Cosmos SDK comes by default with 3 types of addresses:

### Signatures
- `AccAddress` for accounts.
- `ValAddress` for validator operators.
- `ConsAddress` for validator nodes.

Each of these address types are an alias for an hex-encoded `[]byte` array of length 20. Here is the standard way to obtain an address `aa` from a `Pubkey pub`:

```go
aa := sdk.AccAddress(pub.Address().Bytes())
```

These addresses implement the [`Address` interface](https://github.com/cosmos/cosmos-sdk/blob/master/types/address.go#L72-L80):

```go
type Address interface {
Equals(Address) bool
Empty() bool
Marshal() ([]byte, error)
MarshalJSON() ([]byte, error)
Bytes() []byte
String() string
Format(s fmt.State, verb rune)
}
```

Of note, the `Marhsal()` and `Bytes()` method both return the same raw `[]byte` form of the address, the former being needed for Protobuff compatibility. Also, the `String()` method is used to return the `bech32` encoded form of the address, which should be the only address format with which end-user interract. See an example [here](https://github.com/cosmos/cosmos-sdk/blob/master/types/address.go#L230-L243).

## Fees and Gas

### AnteHandler
In the Cosmos SDK, `gas` is a special unit that is used to track the consumption of resources during execution. `gas` is typically consumed whenever read and writes are made to the store, but it can also be consumed if expensive computation needs to be done. It serves two main purposes:

### Gas
- Make sure blocks are not consuming too many resources and will be finalized. This is implemented by default in the SDK via the [block gas meter](#block-gas-meter).
- Prevent spam and abuse from end-user. To this end, `gas` consumed during [`message`](../building-modules/messages-and-queries.md#messages) execution is typically priced, resulting in a `fee` (`fees = gas * gas-prices`). This fee generally has to be paid by the sender of the `message`. Note that the SDK does not enforce `gas` pricing by default, as there may be other ways to prevent spam (e.g. bandwidth schemes). Still, most applications will implement `fee` mechanisms to prevent spam. This is done via the [`AnteHandler`](#antehandler).

### Gas Meter

### Block Gas Meter
In the Cosmos SDK, `gas` is a simple alias for `uint64`, and is managed by an object called a *gas meter*. Gas meters implement the [`GasMeter` interface](https://github.com/cosmos/cosmos-sdk/blob/master/store/types/gas.go#L32-L44):

```go
type GasMeter interface {
GasConsumed() Gas
GasConsumedToLimit() Gas
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
IsPastLimit() bool
IsOutOfGas() bool
}
```

where:

- `GasConsumed()` returns the amount of gas that was consumed by the gas meter instance.
- `GasConsumedToLimit()` returns the amount of gas that was consumed by gas meter instance, or the limit if it is reached.
- `Limit()` returns the limit of the gas meter instance. `0` if the gas meter is infinite.
- `ConsumeGas(amount Gas, descriptor string)` consumes the amount of `gas` provided. If the `gas` overflows, it panics with the `descriptor` message. If the gas meter is not infinite, it panics if `gas` consumed goes above the limit.
- `IsPastLimit()` returns `true` if the amount of gas consumed by the gas meter instance is strictly above the limit, `false` otherwise.
- `IsOutOfGas()` returns `true` if the amount of gas consumed by the gas meter instance is above or equal to the limit, `false` otherwise.

The gas meter is generally held in [`ctx`](../core/context.md), and consuming gas is done with the following pattern:

```go
ctx.GasMeter().ConsumeGas(amount, "description")
```

By default, the Cosmos SDK makes use of two different gas meters, the [main gas meter](#main-gas-metter[) and the [block gas meter](#block-gas-meter).

#### Main Gas Meter

`ctx.GasMeter()` is the main gas meter of the application. The main gas meter is initialized in `BeginBlock` via `setDeliverState`, and then tracks gas consumption during execution sequences that lead to state-transitions, i.e. those originally triggered by [`BeginBlock`](../core/baseapp.md#beginblock), [`DeliverTx`](../core/baseapp.md#delivertx) and [`EndBlock`](../core/baseapp.md#endblock). At the beginning of each `DeliverTx`, the main gas meter **must be set to 0** in the [`AnteHandler`](#antehandler), so that it can track gas comsumption per-transaction.

Gas comsumption can be done manually, generally by the module developer in the [`BeginBlocker`, `EndBlocker`](../building-modules/beginblock-endblock.md) or [`handler`](../building-modules/handler.md), but most of the time it is done automatically whenever there is a read or write to the store. This automatic gas consumption logic is implemented in a special store called [`GasKv`](../core/store.md#gaskv-store).

#### Block Gas Meter

`ctx.BlockGasMeter()` is the gas meter used to track gas consumption per block and make sure it does not go above a certain limit. A new instance of the `BlockGasMeter` is created each time [`BeginBlock`](../core/baseapp.go#beginblock) is called. The `BlockGasMeter` is finite, and the limit of gas per block is defined in the application's consensus parameters.

When a new [transaction](../core/transaction.md) is being processed via `DeliverTx`, the current value of `BlockGasMeter` is checked to see if it is above the limit. If it is, `DeliverTx` returns immediately. This can happen even with the first transaction in a block, as `BeginBlock` itself can consume gas. If not, the transaction is processed normally. At the end of `DeliverTx`, the gas tracked by `ctx.BlockGasMeter()` is increased by the amount consumed to process the transaction:

```go
ctx.BlockGasMeter().ConsumeGas(
ctx.GasMeter().GasConsumedToLimit(),
"block gas meter",
)
```

### AnteHandler

The `AnteHandler` is a special `handler` that is run for every transaction during `CheckTx` and `DeliverTx`, before the `handler` of each `message` in the transaction. `AnteHandler` have a different signature than `handler`s:

```go
// AnteHandler authenticates transactions, before their internal messages are handled.
// If newCtx.IsZero(), ctx is used instead.
type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, result Result, abort bool)
```

Being a `handler`, the `anteHandler` is not implemented in the core SDK but in a module. This gives the possibility to developers to choose the version of `AnteHandler` that fits their application's needs. That said, most applications today use the default implementation defined in the [`auth` module](https://github.com/cosmos/cosmos-sdk/tree/master/x/auth). Here is what the `anteHandler` is intended to do in a normal Cosmos SDK application:

- Verify that the transaction are of the correct type. Transaction types are defined in the module that implements the `anteHandler`, and they follow the [transaction interface](https://github.com/cosmos/cosmos-sdk/blob/master/types/tx_msg.go#L34-L41). This enables developers to play with various types for the transaction of their application. In the default `auth` module, the standard transaction type is [`StdTx`](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/types/stdtx.go#L23-L28).
- Verify signatures for each [`message`](../building-modules/messages-and-queries.md#messages) contained in the transaction. Each `message` should be signed by one or multiple sender(s), and these signatures must be verified in the `anteHandler`.
- During `CheckTx`, verify that the gas prices provided with the transaction is greater than the local `min-gas-prices` (as a reminder, gas-prices can be deducted from the following equation: `fees = gas * gas-prices`). `min-gas-prices` is a parameter local to each full-node and used during `CheckTx` to discard transactions that do not provide a minimum amount of fees. This ensure that the mempool cannot be spammed with garbage transactions.
- Verify that the sender of the transaction has enough funds to cover for the `fees`. When the end-user generates a transaction, they must indicate 2 of the 3 following parameters (the third one being implicit): `fees`, `gas` and `gas-prices`. This signals how much they are willing to pay for nodes to execute their transaction. The provided `gas` value is stored in a parameter called `GasWanted` for later use.
- Set `newCtx.GasMeter` to 0, with a limit of `GasWanted`. **This step is extremely important**, as it not only makes sure the transaction cannot consume infinite gas, but also that `ctx.GasMeter` is reset in-between each `DeliverTx` (`ctx` is set to `newCtx` after `anteHandler` is run).

As explained above, the `anteHandler` returns a maximum limit of `gas` the transaction can consume during execution called `GasWanted`. The actual amount consumed in the end is denominated `GasUsed`, and we must therefore have `GasUsed =< GasWanted`. Both `GasWanted` and `GasUsed` are relayed to the underlying consensus engine when [`DeliverTx`](../core/baseapp.md#delivertx) returns.

## Next

Learn about [baseapp](../core/baseapp.md).
2 changes: 1 addition & 1 deletion docs/basics/app-anatomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ In general, the core of the state-machine is defined in a file called `app.go`.

The first thing defined in `app.go` is the `type` of the application. It is generally comprised of the following parts:

- **A reference to [`baseapp`](../core/baseapp.md).** The custom application defined in `app.go` is an extension of the `baseapp` type. `baseapp` implements most of the core logic for the application, including all the [ABCI methods](https://tendermint.com/docs/spec/abci/abci.html#overview) and the routing logic. When a transaction is relayed by Tendermint to the application, the latter uses `baseapp`'s methods to route them to the appropriate module.
- **A reference to [`baseapp`](../core/baseapp.md).** The custom application defined in `app.go` is an extension of the `baseapp` type. When a transaction is relayed by Tendermint to the application, the latter uses `baseapp`'s methods to route them to the appropriate module. `baseapp` implements most of the core logic for the application, including all the [ABCI methods](https://tendermint.com/docs/spec/abci/abci.html#overview) and the routing logic. Since `baseapp` implements these ABCI methods and `app` extends `baseapp`, it means that `app` satisfies the [`abci.Application` interface](https://github.com/tendermint/tendermint/blob/master/abci/types/application.go#L11-L26), which is a must for the [full-node](../core/node.md) to work.
- **A list of store keys**. The [store](../core/store.md), which contains the entire state, is implemented as a multistore (i.e. a store of stores) in the Cosmos SDK. Each module uses one or multiple stores in the multistore to persist their part of the state. These stores can be accessed with specific keys that are declared in the `app` type. These keys, along with the `keepers`, are at the heart of the [object-capabilities model](../intro/ocap.md) of the Cosmos SDK.
- **A list of module's `keepers`.** Each module defines an abstraction called [`keeper`](../building-modules/keeper.md), which handles reads and writes for this module's store(s). The `keeper`'s methods of one module can be called from other modules (if authorized), which is why they are declared in the application's type and exported as interfaces to other modules so that they are only allowed to access the authorized functions.
- **A reference to a [`codec`](../core/encoding.md).** The Cosmos SDK gives developers the freedom to choose the encoding framework for their application. The application's `codec` is used to serialize and deserialize data structures in order to store them, as stores can only persist `[]bytes`. The `codec` must be deterministic. The default codec is [amino](./amino.md).
Expand Down
2 changes: 1 addition & 1 deletion docs/building-modules/beginblock-endblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The actual implementation of `BeginBlocker` and `EndBlocker` are very similar to
- If needed, they use the `keeper` and `ctx` to trigger state-transitions.
- If needed, they can emit [`events`](../core/events.md) via the `ctx`'s `EventManager`.

A specificity of the `EndBlocker` is that it can return validator updates to the underlying consensus engine in the form of an [`[]abci.ValidatorUpdates`](https://tendermint.com/docs/app-dev/abci-spec.html#validatorupdate).
A specificity of the `EndBlocker` is that it can return validator updates to the underlying consensus engine in the form of an [`[]abci.ValidatorUpdates`](https://tendermint.com/docs/app-dev/abci-spec.html#validatorupdate). This is the preferred way to implement custom validator changes.

For more, see an [example implementation of `BeginBlocker` from the `distr` module](https://github.com/cosmos/cosmos-sdk/blob/master/x/distribution/abci.go) and an [example implementation of `EndBlocker`]( https://github.com/cosmos/cosmos-sdk/blob/master/x/staking/handler.go#L44)

Expand Down
Loading