Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/tools/cosmovisor/cosmo…
Browse files Browse the repository at this point in the history
…ssdk.io/log-1.5.0
  • Loading branch information
julienrbrt authored Nov 8, 2024
2 parents 055a2e3 + 78b5cd4 commit db536a2
Show file tree
Hide file tree
Showing 59 changed files with 592 additions and 625 deletions.
5 changes: 1 addition & 4 deletions collections/protocodec/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.2

require (
cosmossdk.io/collections v0.4.0
cosmossdk.io/core v0.11.1
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a
github.com/cosmos/gogoproto v1.7.0
github.com/google/go-cmp v0.6.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -50,6 +50,3 @@ require (
golang.org/x/text v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// TODO: remove this once core is tagged.
replace cosmossdk.io/core => ../../core
2 changes: 2 additions & 0 deletions collections/protocodec/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0=
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a h1:iN3KJqxjdsIxHDKOFjQ6GQpQNYwcqnhVR9jkuwU7bDA=
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/schema v0.3.0 h1:01lcaM4trhzZ1HQTfTV8z6Ma1GziOZ/YmdzBN3F720c=
cosmossdk.io/schema v0.3.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
Expand Down
14 changes: 14 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,25 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

## [v1.0.0-alpha.6](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv1.0.0-alpha.6)

### API Breaking Changes

* [#22435](https://github.com/cosmos/cosmos-sdk/pull/22435) Add `Version uint64` field to `store.Changeset` and update `Changeset` constructors to accept a `version uint64` as their first argument.

## [v1.0.0-alpha.5](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv1.0.0-alpha.5)

### Features

* [#22326](https://github.com/cosmos/cosmos-sdk/pull/22326) Introduce codec package in order to facilitate removal of Cosmos SDK dependency in modules.
* [*22267](https://github.com/cosmos/cosmos-sdk/pull/22267) Add `server.ConfigMap` and `server.ModuleConfigMap` to replace `server.DynamicConfig` in module configuration.

## [v1.0.0-alpha.4](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv1.0.0-alpha.4)

### Improvements

* [#22007](https://github.com/cosmos/cosmos-sdk/pull/22007) Improve handlers registration `DevX`.

## [v1.0.0-alpha.3](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv1.0.0-alpha.3)

### Features
Expand Down
8 changes: 5 additions & 3 deletions core/store/changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// Changeset is a list of changes to be written to disk
type Changeset struct {
Version uint64
Changes []StateChanges
}

Expand All @@ -29,11 +30,11 @@ type KVPair = struct {
Remove bool
}

func NewChangeset() *Changeset {
return &Changeset{}
func NewChangeset(version uint64) *Changeset {
return &Changeset{Version: version}
}

func NewChangesetWithPairs(pairs map[string]KVPairs) *Changeset {
func NewChangesetWithPairs(version uint64, pairs map[string]KVPairs) *Changeset {
changes := make([]StateChanges, len(pairs))
i := 0
for storeKey, kvPairs := range pairs {
Expand All @@ -44,6 +45,7 @@ func NewChangesetWithPairs(pairs map[string]KVPairs) *Changeset {
i++
}
return &Changeset{
Version: version,
Changes: changes,
}
}
Expand Down
234 changes: 234 additions & 0 deletions docs/architecture/adr-75-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
# ADR 75: V2 Upgrades

## Changelog

* 2023-11-07: Initial Draft

## Status

DRAFT

## Abstract

V2 is a reset in the Cosmos SDK architecture. It is a complete rewrite of the SDK, with a focus on modularity, extensibility, and performance. The V2 SDK breaks apart the core SDK into smaller modular components allowing users to pick and choose the components they need for their specific use case. This document outlines the changes and migration path for users of the V1 SDK.

## Context

The Cosmos SDK began in 2016, at this time the software was written with the direct use case of the Cosmos Hub.Since then we have seen the SDK evolve and grow, with new features and improvements being added over time. The SDK today is used by over 100 different projects, with more users joining the ecosystem every day. This has led to a number of challenges, including:

* The SDK is becoming increasingly complex, with many different components and dependencies.
* The SDK is becoming more difficult to maintain, with frequent breaking changes and compatibility issues.

V2 is a complete rewrite of the Cosmos SDK, with a focus on modularity, extensibility, and performance. The goal is to make the Cosmos SDK easier to use for the various use cases that we have seen emerging in the ecosystem.

```mermaid
graph TD
Z[CometBFT] --> A[Baseapp]
A[BaseApp] --> B[ABCI Methods]
A --> C[State Management]
A --> D[Transaction Processing]
A --> E[Query Handling]
B --> B1[InitChain]
B --> B2[Info]
B --> B3[CheckTx]
B --> B4[PrepareProposal]
B --> B5[ProcessProposal]
B --> B6[FinalizeBlock]
B --> B7[Commit]
C --> C1[Store]
C1 --> C2[Iavl]
D --> D1[runTx]
D1 --> D2[Module]
E --> E1[Query]
E1 --> D2[Module]
```

This is a high-level overview of Baseapp today. As we can see baseapp houses all the logic for the ABCI methods, state management, transaction processing, and query handling. This has led baseapp to be a very large monolith.

## Alternatives

The alternative to doing a rewrite is to spend more time cleaning up baseapp. This would not fix issues around forking the repository to make changes like we see today. Keeping the current codebase does not allow the project to progress and reduce the maintenance burden on the project.


## Decision

The Descision is to rewrite the core componenets (baseapp, server, store) of the SDK into smaller modules.

These components will be broken into separate go.mods. The modules consist of the following:

* Consensus
* STF (State Transition Function)
* Server/v2
* Store/v2
* Runtime/v2

The flow of all the components was designed to be as simple as possible, and the flow is as follows:

```mermaid
graph TD
subgraph Server
Mempool
E[Vote Extensions]
F[Prepare & Process Proposal]
Consensus
API[API]
end
Server <--> B["STF(State Transition Function)"]
B <--> C[Bank]
B <--> D[Auth]
B <--> G[Staking]
Server --> H[Storage]
H --> I[State Storage]
H --> J[State Commitment]
```

In the above diagram we do not mention runtime/v2 because it is the componend that is responsible for combining all the other components into a single application.

### Consensus

Consensus is the component that handles communication to the Consensus Engine (Networking & Consensus). The default implementation will be CometBFT, but other consensus engines can be used with v2. The goal of consensus is not to offer a consensus API to meet all needs, but a way to allow developers to swap out the server for a different consensus engine. An application developer should not assume that the cometbftserver will work with other consensus engines.

Consensus is the component that controls the interaction with the consensus engine, concurrency and execution models. For context, there are three execution models we have identified:

* **Immediate**:
* Immediate execution differs from what Cosmos SDK utilizes today. In CometBFT, consensus at height N is executed at height N+1.
* Immediate execution refers to coming to consensus at height N for the transactions in the same block.
* **Optimistic**
* Optimistic execution means different things to different applications. The way we imagine it working is that consensus may not be made on every block. Instead consensus is made after execution. This design favors a fast chain as it will not slow down for execution until the optimistic window may be exceeded.
* **Delayed**
* Delayed execution is the default execution model in CometBFT. Consensus is made after execution, but the execution may be delayed until the next block.

The consensus server is responsible for handling the execution model of the state machine. The state machine executes transactions when it is told, it is unaware of the execution model.

Since consensus servers can be swapped there are certain features features specific to consensus engines need to be implemented in the server. In the CometBFT server we have implemented the following features:

* Mempool
* Prepare & Process Proposal
* Vote Extensions
* Snapshots

If another consensus server would like to utilize the above features they can be copied or implemented in the server.

```mermaid
graph TD
subgraph Consensus
Mempool
E[Vote Extensions]
F[Prepare & Process Proposal]
end
Consensus <-->|ABCI| A[CometBFT]
```

:::Note
ABCI, Vote Extensions, and Prepare & Process Proposal are primitives of cometbft, V2 is not tied to these features, teams do not have to adhere to them if they implement their own consensus engine.
:::

### State Transition Function

The state transition function is the component that handles the execution of transactions. It is responsible for calling the correct message handler for the transaction. The state transition function is stateless, it is handed a read only view of state and returns state changes, the changes returned can be handled by consensus in anyway needed.

The state transition function interface is simple and meant to be as light weight as possible. This is the only interface that is required to be implemented by bespoke consensus engines.

```mermaid
graph TD
subgraph STF[State Transition Funciton]
BR --> DB
subgraph DB[DeliverBlock]
PB[PreBlock]
BB[BeginBlock]
DT["Deliver Transaction(s)"]
EB[EndBlock]
end
BR[BlockRequest]
Q[Query]
S[Simulate]
VT[Validate TX]
end
C -->|Decoded Transactions| STF
DBR --> STF
STF --> |ChangeSets, Events, Transaction Results| C2
C2 -->|Commit To DB| D
C[Consensus]
C2[Consensus]
D[DataBase]
DBR[DataBase Reader]
```

State Transition function interface:

```go
type StateTransitionFunction[T transaction.Tx] interface {
// DeliverBlock executes a block of transactions.
DeliverBlock(
ctx context.Context,
block *server.BlockRequest[T],
state store.ReaderMap,
) (blockResult *server.BlockResponse, newState store.WriterMap, err error)

// ValidateTx validates a transaction.
ValidateTx(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) server.TxResult

// Simulate executes a transaction in simulation mode.
Simulate(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) (server.TxResult, store.WriterMap)

// Query executes a query on the application.
Query(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
req transaction.Msg,
) (transaction.Msg, error)
}
```

## Consequences

The design of the node comes with a number of tradeoffs.

* Maintenance cost can be the same as existing Baseapp as handling many go.mods is a overhead.
* Modularity means different layers of abstractions, abstractions always have a cost.

### Backwards Compatibility

The state machine was made to not affect modules that are not using the state transition function. If a user would like to migrate to v2 they will need to migrate to `appmodule.Environment` from `sdk.Context`. `sdk.Context` is a struct which is a global in the state machine, this desing limits the concurrency.

V2 will have a breaking changes in regards to how CometBFT handles certain fields in ABCI. Previously, the Cosmos SDK paniced and recovered in the case of out of gas, providing an error to CometBFT which we do not return in the new design.

V2 only works with `Store/v2`, `IAVL V1` can be used with `Store/v2`. This allows chains to continue with existing databases. There will be a migration happening to convert the database to the separation of Storage and Commitment. Once the migration is completed the state machine will query information from the rawDB unless otherwise specified.

### Positive

* Ability to add new features to the SDK without forking the entire repository.
* Ability to create custom node configurations.
* Reduced maintenance cost burden.
* State machine is more performant.
* Isolated components allow for easier testing.
* Allow the team to delete a lot of code in `github.com/cosmos/cosmos-sdk`.

### Negative

* Modularity and abstractions can be a burden.


## Further Discussions

* After reducing the feature set of Cosmos SDK, we can more easily look into rewriting the core into rust. This is dependent on crosslang.

## References

* {reference link}
2 changes: 1 addition & 1 deletion runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ replace (

require (
cosmossdk.io/api v0.7.6
cosmossdk.io/core v1.0.0-alpha.5
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a
cosmossdk.io/depinject v1.1.0
cosmossdk.io/log v1.5.0
cosmossdk.io/schema v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions runtime/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fed
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1/go.mod h1:JTBMfyi+qAXUHumX+rcD2WIq9FNWmdcNh5MjBnSw0L0=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 h1:F78ecjvMtgd1aZ1Aj9cvBjURxVGCYvRM+OOy5eR+pjw=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
cosmossdk.io/core v1.0.0-alpha.5 h1:McjYXAQ6XcT20v2uHyH7PhoWH8V+mebzfVFqT3GinsI=
cosmossdk.io/core v1.0.0-alpha.5/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a h1:iN3KJqxjdsIxHDKOFjQ6GQpQNYwcqnhVR9jkuwU7bDA=
cosmossdk.io/core v1.0.0-alpha.5.0.20241108140525-43e28b43ad7a/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/depinject v1.1.0 h1:wLan7LG35VM7Yo6ov0jId3RHWCGRhe8E8bsuARorl5E=
cosmossdk.io/depinject v1.1.0/go.mod h1:kkI5H9jCGHeKeYWXTqYdruogYrEeWvBQCw1Pj4/eCFI=
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 h1:IQNdY2kB+k+1OM2DvqFG1+UgeU1JzZrWtwuWzI3ZfwA=
Expand Down
5 changes: 3 additions & 2 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/api"
"cosmossdk.io/server/v2/api/grpc/gogoreflection"
)

Expand Down Expand Up @@ -224,7 +223,9 @@ func (s *Server[T]) Stop(ctx context.Context) error {
}

s.logger.Info("stopping gRPC server...", "address", s.config.Address)
return api.DoUntilCtxExpired(ctx, s.grpcSrv.GracefulStop)
s.grpcSrv.GracefulStop()

return nil
}

// GetGRPCServer returns the underlying gRPC server.
Expand Down
10 changes: 4 additions & 6 deletions server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ const (
)

type Server[T transaction.Tx] struct {
logger log.Logger
router *http.ServeMux

logger log.Logger
router *http.ServeMux
httpServer *http.Server
config *Config
cfgOptions []CfgOption
}

func New[T transaction.Tx](
appManager appmanager.AppManager[T],
logger log.Logger,
appManager appmanager.AppManager[T],
cfg server.ConfigMap,
cfgOptions ...CfgOption,
) (*Server[T], error) {
srv := &Server[T]{
cfgOptions: cfgOptions,
logger: logger.With(log.ModuleKey, ServerName),
cfgOptions: cfgOptions,
router: http.NewServeMux(),
}

Expand Down Expand Up @@ -90,7 +89,6 @@ func (s *Server[T]) Stop(ctx context.Context) error {
}

s.logger.Info("stopping HTTP server")

return s.httpServer.Shutdown(ctx)
}

Expand Down
Loading

0 comments on commit db536a2

Please sign in to comment.