Skip to content

Commit

Permalink
feat: implement simapp
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed May 17, 2024
1 parent ba89b76 commit d20e28c
Show file tree
Hide file tree
Showing 15 changed files with 4,233 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.authority
.idea
build
coverage.out
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
.PHONY: proto-format proto-lint proto-gen format lint test-unit
all: proto-all format lint test
.PHONY: proto-format proto-lint proto-gen format lint test-unit build
all: proto-all format lint test build

###############################################################################
### Build ###
###############################################################################

build:
@echo "🤖 Building simd..."
@cd simapp && make build 1> /dev/null
@echo "✅ Completed build!"

###############################################################################
### Formatting & Linting ###
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/golangci/golangci-lint v1.57.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240429193739-8cf5692501f6
google.golang.org/grpc v1.63.2
Expand Down Expand Up @@ -257,7 +258,6 @@ require (
github.com/sourcegraph/go-diff v0.7.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.22

use (
.
simapp
)
1,351 changes: 1,351 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
alias authorityd=./simapp/build/simd

for arg in "$@"
do
case $arg in
-r|--reset)
rm -rf .authority
shift
;;
esac
done

if ! [ -f .authority/data/priv_validator_state.json ]; then
authorityd init validator --chain-id "authority-1" --home .authority &> /dev/null

authorityd keys add validator --home .authority --keyring-backend test &> /dev/null
authorityd genesis add-genesis-account validator 1000000ustake --home .authority --keyring-backend test
OWNER=$(authorityd keys add owner --home .authority --keyring-backend test --output json | jq .address)
authorityd genesis add-genesis-account owner 2500000uusdc --home .authority --keyring-backend test
PENDING_OWNER=$(authorityd keys add pending-owner --home .authority --keyring-backend test --output json | jq .address)
authorityd genesis add-genesis-account pending-owner 2500000uusdc --home .authority --keyring-backend test

TEMP=.authority/genesis.json
touch $TEMP && jq '.app_state.authority.owner = '$OWNER'' .authority/config/genesis.json > $TEMP && mv $TEMP .authority/config/genesis.json
touch $TEMP && jq '.app_state.staking.params.bond_denom = "ustake"' .authority/config/genesis.json > $TEMP && mv $TEMP .authority/config/genesis.json

authorityd genesis gentx validator 1000000ustake --chain-id "authority-1" --home .authority --keyring-backend test &> /dev/null
authorityd genesis collect-gentxs --home .authority &> /dev/null

sed -i '' 's/timeout_commit = "5s"/timeout_commit = "1s"/g' .authority/config/config.toml
fi

authorityd start --home .authority
27 changes: 27 additions & 0 deletions simapp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
COMMIT := $(shell git log -1 --format='%H')

ifeq (,$(VERSION))
VERSION := $(shell git describe --exact-match 2>/dev/null)
ifeq (,$(VERSION))
ifeq ($(shell git status --porcelain),)
VERSION := $(BRANCH)
else
VERSION := $(BRANCH)-dirty
endif
endif
endif

ldflags := $(LDFLAGS)
ldflags += -X github.com/cosmos/cosmos-sdk/version.Name=simapp \
-X github.com/cosmos/cosmos-sdk/version.AppName=simd \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT)
ldflags := $(strip $(ldflags))

BUILD_FLAGS := -ldflags '$(ldflags)'

build:
@go build -mod=readonly $(BUILD_FLAGS) -o $(PWD)/build/ ./...

.PHONY: build
173 changes: 173 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package simapp

import (
_ "embed"
"io"
"os"
"path/filepath"

"cosmossdk.io/core/appconfig"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

authoritykeeper "github.com/noble-assets/authority/x/authority/keeper"

_ "cosmossdk.io/api/cosmos/tx/config/v1" // import for side effects
_ "cosmossdk.io/x/upgrade" // import for side effects
_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side effects
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import for side effects
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side effects
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side effects
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side effects
_ "github.com/noble-assets/authority/x/authority" // import for side effects
)

var DefaultNodeHome string

//go:embed app.yaml
var AppConfigYAML []byte

var (
_ runtime.AppI = (*SimApp)(nil)
_ servertypes.Application = (*SimApp)(nil)
)

// SimApp extends an ABCI application, but with most of its parameters exported.
// They are exported for convenience in creating helper functions, as object
// capabilities aren't needed for testing.
type SimApp struct {
*runtime.App
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry

// Cosmos SDK Modules
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
ConsensusKeeper consensuskeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
// Custom Modules
AuthorityKeeper *authoritykeeper.Keeper
}

func init() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}

DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
}

// AppConfig returns the default app config.
func AppConfig() depinject.Config {
return depinject.Configs(
appconfig.LoadYAML(AppConfigYAML),
depinject.Supply(
// supply custom module basics
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
},
),
)
}

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) (*SimApp, error) {
var (
app = &SimApp{}
appBuilder *runtime.AppBuilder
)

if err := depinject.Inject(
depinject.Configs(
AppConfig(),
depinject.Supply(
logger,
appOpts,
),
),
&appBuilder,
&app.appCodec,
&app.legacyAmino,
&app.txConfig,
&app.interfaceRegistry,
// Cosmos SDK Modules
&app.AccountKeeper,
&app.BankKeeper,
&app.ConsensusKeeper,
&app.StakingKeeper,
&app.UpgradeKeeper,
// Custom Modules
&app.AuthorityKeeper,
); err != nil {
return nil, err
}

app.App = appBuilder.Build(db, traceStore, baseAppOptions...)

if err := app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
return nil, err
}

if err := app.Load(loadLatest); err != nil {
return nil, err
}

return app, nil
}

func (app *SimApp) LegacyAmino() *codec.LegacyAmino {
return app.legacyAmino
}

func (app *SimApp) SimulationManager() *module.SimulationManager {
return nil
}

func (app *SimApp) GetKey(storeKey string) *storetypes.KVStoreKey {
key, _ := app.UnsafeFindStoreKey(storeKey).(*storetypes.KVStoreKey)
return key
}

func (app *SimApp) GetMemKey(memKey string) *storetypes.MemoryStoreKey {
key, _ := app.UnsafeFindStoreKey(memKey).(*storetypes.MemoryStoreKey)
return key
}

func (app *SimApp) kvStoreKeys() map[string]*storetypes.KVStoreKey {
keys := make(map[string]*storetypes.KVStoreKey)
for _, k := range app.GetStoreKeys() {
if kv, ok := k.(*storetypes.KVStoreKey); ok {
keys[kv.Name()] = kv
}
}

return keys
}
51 changes: 51 additions & 0 deletions simapp/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
modules:
- name: runtime
config:
"@type": cosmos.app.runtime.v1alpha1.Module
app_name: SimApp
pre_blockers: [ upgrade ]
begin_blockers: [ staking ]
end_blockers: [ staking ]
init_genesis: [ auth, bank, staking, genutil, upgrade, authority ]
override_store_keys:
- module_name: auth
kv_store_key: acc
- name: auth
config:
"@type": cosmos.auth.module.v1.Module
bech32_prefix: noble
module_account_permissions:
- account: fee_collector
- account: bonded_tokens_pool
permissions: [ burner, staking ]
- account: not_bonded_tokens_pool
permissions: [ burner, staking ]
authority: authority # Utilize our custom x/authority module.
- name: bank
config:
"@type": cosmos.bank.module.v1.Module
blocked_module_accounts_override:
[ auth, distribution, bonded_tokens_pool, not_bonded_tokens_pool ]
authority: authority # Utilize our custom x/authority module.
- name: consensus
config:
"@type": cosmos.consensus.module.v1.Module
authority: authority # Utilize our custom x/authority module.
- name: genutil
config:
"@type": cosmos.genutil.module.v1.Module
- name: staking
config:
"@type": cosmos.staking.module.v1.Module
authority: authority # Utilize our custom x/authority module.
- name: tx
config:
"@type": cosmos.tx.config.v1.Config
- name: upgrade
config:
"@type": cosmos.upgrade.module.v1.Module
authority: authority # Utilize our custom x/authority module.

- name: authority
config:
"@type": noble.authority.module.v1.Module
53 changes: 53 additions & 0 deletions simapp/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package simapp

import (
"encoding/json"
"fmt"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
)

// ExportAppStateAndValidators exports the state of the application for a genesis file.
func (app *SimApp) ExportAppStateAndValidators(
forZeroHeight bool,
jailAllowedAddrs []string,
_ []string,
) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight()})

// We export at last height + 1, because that's the height at which
// CometBFT will start InitChain.
height := app.LastBlockHeight() + 1
if forZeroHeight {
height = 0
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState, err := app.ModuleManager.ExportGenesis(ctx, app.appCodec)
if err != nil {
return servertypes.ExportedApp{}, fmt.Errorf("failed to export genesis state: %w", err)
}

appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, err
}

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature, which will be deprecated in favour of export at a block height
func (app *SimApp) prepForZeroHeightGenesis(_ sdk.Context, _ []string) {
panic("unimplemented")
}
Loading

0 comments on commit d20e28c

Please sign in to comment.