github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/docs/core/encoding.md (about) 1 <!-- 2 order: 6 3 synopsis: The `codec` is used everywhere in the Cosmos SDK to encode and decode structs and interfaces. The specific codec used in the Cosmos SDK is called `go-amino` 4 --> 5 6 # Encoding 7 8 ## Pre-requisite Readings {hide} 9 10 - [Anatomy of an SDK application](../basics/app-anatomy.md) {prereq} 11 12 ## Encoding 13 14 Every Cosmos SDK application exposes a global `codec` to marshal/unmarshal structs and interfaces in order to store and/or transfer them. As of now, the `codec` used in the Cosmos SDK is [go-amino](https://github.com/tendermint/go-amino), which possesses the following important properties: 15 16 - Interface support. 17 - Deterministic encoding of value (which is required considering that blockchains are deterministic replicated state-machines). 18 - Upgradeable schemas. 19 20 The application's `codec` is typically initialized in the [application's constructor function](../basics/app-anatomy.md#constructor-function), where it is also passed to each of the application's modules via the [basic manager](../building-modules/module-manager.md#basic-manager). 21 22 Among other things, the `codec` is used by module's [`keeper`s](../building-modules/keeper.md) to marshal objects into `[]byte` before storing them in the module's [`KVStore`](./store.md#kvstore), or to unmarshal them from `[]byte` when retrieving them: 23 24 ```go 25 // typical pattern to marshal an object to []byte before storing it 26 bz := keeper.cdc.MustMarshalBinaryBare(object) 27 28 //typical pattern to unmarshal an object from []byte when retrieving it 29 keeper.cdc.MustUnmarshalBinaryBare(bz, &object) 30 ``` 31 32 Alternatively, it is possible to use `MustMarshalBinaryLengthPrefixed`/`MustUnmarshalBinaryLengthPrefixed` instead of `MustMarshalBinaryBare`/`MustUnmarshalBinaryBare` for the same encoding prefixed by a `uvarint` encoding of the object to encode. 33 34 Another important use of the `codec` is the encoding and decoding of [transactions](./transactions.md). Transactions are defined at the Cosmos SDK level, but passed to the underlying consensus engine in order to be relayed to other peers. Since the underlying consensus engine is agnostic to the application, it only accepts transactions in the form of `[]byte`. The encoding is done by an object called `TxEncoder` and the decoding by an object called `TxDecoder`. 35 36 +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/types/tx_msg.go#L45-L49 37 38 A standard implementation of both these objects can be found in the [`auth` module](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth): 39 40 +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/x/auth/types/stdtx.go#L241-L266 41 42 ## Next {hide} 43 44 Learn about [events](./events.md) {hide}