|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Core |
| 6 | + |
| 7 | +Core is package which specifies the interfaces for core components of the Cosmos SDK. Other |
| 8 | +packages in the SDK implement these interfaces to provide the core functionality. This design |
| 9 | +provides modularity and flexibility to the SDK, allowing developers to swap out implementations |
| 10 | +of core components as needed. As such it is often referred to as the Core API. |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +The `Environment` struct is a core component of the Cosmos SDK. It provides access to the core |
| 15 | +services of the SDK, such as the KVStore, EventManager, and Logger. The `Environment` struct is |
| 16 | +passed to modules and other components of the SDK to provide access to these services. |
| 17 | + |
| 18 | +```go reference |
| 19 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/appmodule/v2/environment.go#L16-L29 |
| 20 | +``` |
| 21 | + |
| 22 | +Historically the SDK has used an [sdk.Context](02-context.md) to pass around services and data. |
| 23 | +`Environment` is a newer construct that is intended to replace an `sdk.Context` in many cases. |
| 24 | +`sdk.Context` will be deprecated in the future on the same timeline as [Baseapp](00-baseapp.md). |
| 25 | + |
| 26 | +## Branch Service |
| 27 | + |
| 28 | +The [BranchService](https://pkg.go.dev/cosmossdk.io/core/branch#Service.Execute) provides an |
| 29 | +interface to execute arbitrary code in a branched store. This is useful for executing code |
| 30 | +that needs to make changes to the store, but may need to be rolled back if an error occurs. |
| 31 | +Below is a contrived example based on the `x/epoch` module's BeginBlocker logic. |
| 32 | + |
| 33 | +```go |
| 34 | +func (k Keeper) BeginBlocker(ctx context.Context) error { |
| 35 | + err := k.EpochInfo.Walk( |
| 36 | + // ... |
| 37 | + ctx, |
| 38 | + nil, |
| 39 | + func(key string, epochInfo types.EpochInfo) (stop bool, err error) { |
| 40 | + // ... |
| 41 | + if err := k.BranchService.Execute(ctx, func(ctx context.Context) error { |
| 42 | + return k.AfterEpochEnd(ctx, epochInfo.Identifier, epochInfo.CurrentEpoch) |
| 43 | + }); err != nil { |
| 44 | + return true, err |
| 45 | + } |
| 46 | + }) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Note that calls to `BranchService.Execute` are atomic and cannot share state with each other |
| 51 | +except when the transaction is successful. If successful, the changes made to the store will be |
| 52 | +committed. If an error occurs, the changes will be rolled back. |
| 53 | + |
| 54 | +## Event Service |
| 55 | + |
| 56 | +The Event Service returns a handle to an [EventManager ](https://pkg.go.dev/cosmossdk.io/[email protected]/event#Manager) |
| 57 | +which can be used to emit events. For information on how to emit events and their meaning |
| 58 | +in the SDK see the [Events](08-events.md) document. |
| 59 | + |
| 60 | +Note that core's `EventManager` API is a subset of the EventManager API described above; the |
| 61 | +latter will be deprecated and removed in the future. Roughly speaking legacy `EmitTypeEvent` |
| 62 | +maps to `Emit` and legacy `EmitEvent` maps to `EmitKV`. |
| 63 | + |
| 64 | +```go reference |
| 65 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/event/service.go#L18-L29 |
| 66 | +``` |
| 67 | + |
| 68 | +## Gas Service |
| 69 | + |
| 70 | +The gas service encapsulates both gas configuration and a gas meter. Gas consumption is largely |
| 71 | +handled at the framework level for transaction processing and state access but modules can |
| 72 | +choose to use the gas service directly if needed. |
| 73 | + |
| 74 | +```go reference |
| 75 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/gas/service.go#L26-L54 |
| 76 | +``` |
| 77 | + |
| 78 | +## Header Service |
| 79 | + |
| 80 | +The header service provides access to the current block header. This is useful for modules that |
| 81 | +need to access the block header fields like `Time` and `Height` during transaction processing. |
| 82 | + |
| 83 | +```go reference |
| 84 | +https://github.com/cosmos/cosmos-sdk/blob/a3729c1ad6ba2fb46f879ec7ea67c3afc02e9859/core/header/service.go#L11-L23 |
| 85 | +``` |
| 86 | + |
| 87 | +### Custom Header Service |
| 88 | + |
| 89 | +Core's service oriented architecture (SOA) allows for chain developers to define a custom |
| 90 | +implementation of the `HeaderService` interface. This would involve creating a new struct that |
| 91 | +satisfies `HeaderService` but composes additional logic on top. An example of where this would |
| 92 | +happen (when using depinject is shown below). Note this example is taken from `runtime/v2` but |
| 93 | +could easily be adapted to `runtime/v1` (the default runtime 0.52). This same pattern can be |
| 94 | +replicated for any core service. |
| 95 | + |
| 96 | +```go reference |
| 97 | +https://github.com/cosmos/cosmos-sdk/blob/489aaae40234f1015a7bbcfa9384a89dc8de8153/runtime/v2/module.go#L262-L288 |
| 98 | +``` |
| 99 | + |
| 100 | +These bindings are applied to the `depinject` container in simapp/v2 as shown below. |
| 101 | + |
| 102 | +```go reference |
| 103 | +https://github.com/cosmos/cosmos-sdk/blob/489aaae40234f1015a7bbcfa9384a89dc8de8153/simapp/v2/app_di.go#L72-L74 |
| 104 | +``` |
| 105 | + |
| 106 | +## Query and Message Router Service |
| 107 | + |
| 108 | +Both the query and message router services are implementation of the same interface, `router.Service`. |
| 109 | + |
| 110 | +```go reference |
| 111 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/router/service.go#L11-L16 |
| 112 | +``` |
| 113 | + |
| 114 | +Both are exposed to modules so that arbitrary messages and queries can be routed to the |
| 115 | +appropriate handler. This powerful abstraction allows module developers to fully decouple |
| 116 | +modules from each other by using only the proto message for dispatching. This is particularly |
| 117 | +useful for modules like `x/accounts` which require a dynamic dispatch mechanism in order to |
| 118 | +function. |
| 119 | + |
| 120 | +## TransactionService |
| 121 | + |
| 122 | +```go reference |
| 123 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/transaction/service.go#L21-L25 |
| 124 | +``` |
| 125 | + |
| 126 | +The transaction service provides access to the execution mode a state machine transaction is |
| 127 | +running in, which may be one of `Check`, `Recheck`, `Simulate` or `Finalize`. The SDK primarily |
| 128 | +uses these flags in ante handlers to skip certain checks while in `Check` or `Simulate` modes, |
| 129 | +but module developers may find uses for them as well. |
| 130 | + |
| 131 | +## KVStore Service |
| 132 | + |
| 133 | +```go reference |
| 134 | +https://github.com/cosmos/cosmos-sdk/blob/core/v1.0.0-alpha.4/core/store/service.go#L5-L11 |
| 135 | +``` |
| 136 | + |
| 137 | +The KVStore service abstracts access to, and creation of, key-value stores. Most use cases will |
| 138 | +be backed by a merkle-tree store, but developers can provide their own implementations if |
| 139 | +needed. In the case of the `KVStoreService` implementation provided in `Environment`, module |
| 140 | +developers should understand that calling `OpenKVStore` will return a store already scoped to |
| 141 | +the module's prefix. The wiring for this scoping is specified in `runtime`. |
0 commit comments