Skip to content

Commit

Permalink
Codespaces as Strings (cosmos#2821)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and jaekwon committed Nov 16, 2018
1 parent ca2270c commit 8d6b092
Show file tree
Hide file tree
Showing 39 changed files with 119 additions and 210 deletions.
3 changes: 2 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ IMPROVEMENTS
- #2773 Require moniker to be provided on `gaiad init`.
- #2672 [Makefile] Updated for better Windows compatibility and ledger support logic, get_tools was rewritten as a cross-compatible Makefile.
- [#110](https://github.com/tendermint/devops/issues/110) Updated CircleCI job to trigger website build when cosmos docs are updated.

* SDK
- [x/mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization
- \#2821 Codespaces are now strings

* Tendermint
- #2796 Update to go-amino 0.14.1
Expand Down
42 changes: 19 additions & 23 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type BaseApp struct {
cms sdk.CommitMultiStore // Main (uncached) state
router Router // handle any kind of message
queryRouter QueryRouter // router for redirecting query calls
codespacer *sdk.Codespacer // handle module codespacing
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

anteHandler sdk.AnteHandler // ante handler for fee and auth
Expand Down Expand Up @@ -94,13 +93,9 @@ func NewBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecod
cms: store.NewCommitMultiStore(db),
router: NewRouter(),
queryRouter: NewQueryRouter(),
codespacer: sdk.NewCodespacer(),
txDecoder: txDecoder,
}

// Register the undefined & root codespaces, which should not be used by
// any modules.
app.codespacer.RegisterOrPanic(sdk.CodespaceRoot)
for _, option := range options {
option(app)
}
Expand All @@ -118,11 +113,6 @@ func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) {
app.cms.WithTracer(w)
}

// Register the next available codespace through the baseapp's codespacer, starting from a default
func (app *BaseApp) RegisterCodespace(codespace sdk.CodespaceType) sdk.CodespaceType {
return app.codespacer.RegisterNext(codespace)
}

// Mount IAVL stores to the provided keys in the BaseApp multistore
func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) {
for _, key := range keys {
Expand Down Expand Up @@ -329,8 +319,9 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
}
case "version":
return abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Value: []byte(version.GetVersion()),
Code: uint32(sdk.CodeOK),
Codespace: string(sdk.CodespaceRoot),
Value: []byte(version.GetVersion()),
}
default:
result = sdk.ErrUnknownRequest(fmt.Sprintf("Unknown query: %s", path)).Result()
Expand All @@ -339,8 +330,9 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
// Encode with json
value := codec.Cdc.MustMarshalBinaryLengthPrefixed(result)
return abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Value: value,
Code: uint32(sdk.CodeOK),
Codespace: string(sdk.CodespaceRoot),
Value: value,
}
}
msg := "Expected second parameter to be either simulate or version, neither was present"
Expand Down Expand Up @@ -400,12 +392,13 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res
resBytes, err := querier(ctx, path[2:], req)
if err != nil {
return abci.ResponseQuery{
Code: uint32(err.ABCICode()),
Log: err.ABCILog(),
Code: uint32(err.Code()),
Codespace: string(err.Codespace()),
Log: err.ABCILog(),
}
}
return abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Code: uint32(sdk.CodeOK),
Value: resBytes,
}
}
Expand Down Expand Up @@ -482,6 +475,7 @@ func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
// Tell the blockchain engine (i.e. Tendermint).
return abci.ResponseDeliverTx{
Code: uint32(result.Code),
Codespace: string(result.Codespace),
Data: result.Data,
Log: result.Log,
GasWanted: result.GasWanted,
Expand All @@ -501,7 +495,6 @@ func validateBasicTxMsgs(msgs []sdk.Msg) sdk.Error {
// Validate the Msg.
err := msg.ValidateBasic()
if err != nil {
err = err.WithDefaultCodespace(sdk.CodespaceRoot)
return err
}
}
Expand All @@ -526,7 +519,8 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re
logs := make([]string, 0, len(msgs))
var data []byte // NOTE: we just append them all (?!)
var tags sdk.Tags // also just append them all
var code sdk.ABCICodeType
var code sdk.CodeType
var codespace sdk.CodespaceType
for msgIdx, msg := range msgs {
// Match route.
msgRoute := msg.Route()
Expand All @@ -553,6 +547,7 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re
if !msgResult.IsOK() {
logs = append(logs, fmt.Sprintf("Msg %d failed: %s", msgIdx, msgResult.Log))
code = msgResult.Code
codespace = msgResult.Codespace
break
}

Expand All @@ -562,10 +557,11 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re

// Set the final gas values.
result = sdk.Result{
Code: code,
Data: data,
Log: strings.Join(logs, "\n"),
GasUsed: ctx.GasMeter().GasConsumed(),
Code: code,
Codespace: codespace,
Data: data,
Log: strings.Join(logs, "\n"),
GasUsed: ctx.GasMeter().GasConsumed(),
// TODO: FeeAmount/FeeDenom
Tags: tags,
}
Expand Down
18 changes: 12 additions & 6 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ func TestRunInvalidTransaction(t *testing.T) {
{
emptyTx := &txTest{}
err := app.Deliver(emptyTx)
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeInternal), err.Code)
require.EqualValues(t, sdk.CodeInternal, err.Code)
require.EqualValues(t, sdk.CodespaceRoot, err.Codespace)
}

// Transaction where ValidateBasic fails
Expand All @@ -701,7 +702,8 @@ func TestRunInvalidTransaction(t *testing.T) {
tx := testCase.tx
res := app.Deliver(tx)
if testCase.fail {
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeInvalidSequence), res.Code)
require.EqualValues(t, sdk.CodeInvalidSequence, res.Code)
require.EqualValues(t, sdk.CodespaceRoot, res.Codespace)
} else {
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
}
Expand All @@ -712,11 +714,13 @@ func TestRunInvalidTransaction(t *testing.T) {
{
unknownRouteTx := txTest{[]sdk.Msg{msgNoRoute{}}, 0}
err := app.Deliver(unknownRouteTx)
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), err.Code)
require.EqualValues(t, sdk.CodeUnknownRequest, err.Code)
require.EqualValues(t, sdk.CodespaceRoot, err.Codespace)

unknownRouteTx = txTest{[]sdk.Msg{msgCounter{}, msgNoRoute{}}, 0}
err = app.Deliver(unknownRouteTx)
require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), err.Code)
require.EqualValues(t, sdk.CodeUnknownRequest, err.Code)
require.EqualValues(t, sdk.CodespaceRoot, err.Codespace)
}

// Transaction with an unregistered message
Expand All @@ -732,7 +736,8 @@ func TestRunInvalidTransaction(t *testing.T) {
txBytes, err := newCdc.MarshalBinaryLengthPrefixed(tx)
require.NoError(t, err)
res := app.DeliverTx(txBytes)
require.EqualValues(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeTxDecode), res.Code)
require.EqualValues(t, sdk.CodeTxDecode, res.Code)
require.EqualValues(t, sdk.CodespaceRoot, res.Codespace)
}
}

Expand Down Expand Up @@ -819,7 +824,8 @@ func TestTxGasLimits(t *testing.T) {
if !tc.fail {
require.True(t, res.IsOK(), fmt.Sprintf("%d: %v, %v", i, tc, res))
} else {
require.Equal(t, res.Code, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOutOfGas), fmt.Sprintf("%d: %v, %v", i, tc, res))
require.Equal(t, sdk.CodeOutOfGas, res.Code, fmt.Sprintf("%d: %v, %v", i, tc, res))
require.Equal(t, sdk.CodespaceRoot, res.Codespace)
}
}
}
8 changes: 4 additions & 4 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.cdc,
app.keyStake, app.tkeyStake,
app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace),
app.RegisterCodespace(stake.DefaultCodespace),
stake.DefaultCodespace,
)
app.mintKeeper = mint.NewKeeper(app.cdc, app.keyMint,
app.paramsKeeper.Subspace(mint.DefaultParamspace),
Expand All @@ -124,19 +124,19 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.keyDistr,
app.paramsKeeper.Subspace(distr.DefaultParamspace),
app.bankKeeper, &stakeKeeper, app.feeCollectionKeeper,
app.RegisterCodespace(stake.DefaultCodespace),
distr.DefaultCodespace,
)
app.slashingKeeper = slashing.NewKeeper(
app.cdc,
app.keySlashing,
&stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace),
app.RegisterCodespace(slashing.DefaultCodespace),
slashing.DefaultCodespace,
)
app.govKeeper = gov.NewKeeper(
app.cdc,
app.keyGov,
app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamspace), app.bankKeeper, &stakeKeeper,
app.RegisterCodespace(gov.DefaultCodespace),
gov.DefaultCodespace,
)

// register the staking hooks
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
// add handlers
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace), app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace), app.RegisterCodespace(slashing.DefaultCodespace))
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace), slashing.DefaultCodespace)

// register message routes
app.Router().
Expand Down
7 changes: 4 additions & 3 deletions docs/_attic/sdk/core/examples/app1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

const (
app1Name = "App1"
app1Name = "App1"
bankCodespace = "BANK"
)

func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp {
Expand Down Expand Up @@ -107,7 +108,7 @@ func handleMsgSend(key *sdk.KVStoreKey) sdk.Handler {
if !ok {
// Create custom error message and return result
// Note: Using unreserved error codespace
return sdk.NewError(2, 1, "MsgSend is malformed").Result()
return sdk.NewError(bankCodespace, 1, "MsgSend is malformed").Result()
}

// Load the store.
Expand Down Expand Up @@ -137,7 +138,7 @@ func handleFrom(store sdk.KVStore, from sdk.AccAddress, amt sdk.Coins) sdk.Resul
accBytes := store.Get(from)
if accBytes == nil {
// Account was not added to store. Return the result of the error.
return sdk.NewError(2, 101, "Account not added to store").Result()
return sdk.NewError(bankCodespace, 101, "Account not added to store").Result()
}

// Unmarshal the JSON account bytes.
Expand Down
2 changes: 1 addition & 1 deletion docs/_attic/sdk/core/examples/app2.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func handleMsgIssue(keyIssue *sdk.KVStoreKey, keyAcc *sdk.KVStoreKey) sdk.Handle
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
issueMsg, ok := msg.(MsgIssue)
if !ok {
return sdk.NewError(2, 1, "MsgIssue is malformed").Result()
return sdk.NewError(bankCodespace, 1, "MsgIssue is malformed").Result()
}

// Retrieve stores
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
},
)
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, ibc.DefaultCodespace)

// register message routes
app.Router().
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/democoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {

// Add handlers.
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper)
app.coolKeeper = cool.NewKeeper(app.capKeyMainStore, app.bankKeeper, app.RegisterCodespace(cool.DefaultCodespace))
app.powKeeper = pow.NewKeeper(app.capKeyPowStore, pow.NewConfig("pow", int64(1)), app.bankKeeper, app.RegisterCodespace(pow.DefaultCodespace))
app.ibcMapper = ibc.NewMapper(app.cdc, app.capKeyIBCStore, app.RegisterCodespace(ibc.DefaultCodespace))
app.stakeKeeper = simplestake.NewKeeper(app.capKeyStakingStore, app.bankKeeper, app.RegisterCodespace(simplestake.DefaultCodespace))
app.coolKeeper = cool.NewKeeper(app.capKeyMainStore, app.bankKeeper, cool.DefaultCodespace)
app.powKeeper = pow.NewKeeper(app.capKeyPowStore, pow.NewConfig("pow", int64(1)), app.bankKeeper, pow.DefaultCodespace)
app.ibcMapper = ibc.NewMapper(app.cdc, app.capKeyIBCStore, ibc.DefaultCodespace)
app.stakeKeeper = simplestake.NewKeeper(app.capKeyStakingStore, app.bankKeeper, simplestake.DefaultCodespace)
app.Router().
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
AddRoute("cool", cool.NewHandler(app.coolKeeper)).
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/cool/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getMockApp(t *testing.T) *mock.App {
RegisterCodec(mapp.Cdc)
keyCool := sdk.NewKVStoreKey("cool")
bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper)
keeper := NewKeeper(keyCool, bankKeeper, mapp.RegisterCodespace(DefaultCodespace))
keeper := NewKeeper(keyCool, bankKeeper, DefaultCodespace)
mapp.Router().AddRoute("cool", NewHandler(keeper))

mapp.SetInitChainer(getInitChainer(mapp, keeper, "ice-cold"))
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/cool/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Cool errors reserve 400 ~ 499.
const (
DefaultCodespace sdk.CodespaceType = 6
DefaultCodespace sdk.CodespaceType = "cool"

// Cool module reserves error 400-499 lawl
CodeIncorrectCoolAnswer sdk.CodeType = 400
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/oracle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (keeper Keeper) Handle(h Handler, ctx sdk.Context, o Msg, codespace sdk.Cod
err := h(cctx, payload)
if err != nil {
return sdk.Result{
Code: sdk.ABCICodeOK,
Code: sdk.CodeOK,
Log: err.ABCILog(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/pow/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getMockApp(t *testing.T) *mock.App {
keyPOW := sdk.NewKVStoreKey("pow")
bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper)
config := Config{"pow", 1}
keeper := NewKeeper(keyPOW, config, bankKeeper, mapp.RegisterCodespace(DefaultCodespace))
keeper := NewKeeper(keyPOW, config, bankKeeper, DefaultCodespace)
mapp.Router().AddRoute("pow", keeper.Handler)

mapp.SetInitChainer(getInitChainer(mapp, keeper))
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/pow/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CodeType = sdk.CodeType

// POW errors reserve 200 ~ 299
const (
DefaultCodespace sdk.CodespaceType = 5
DefaultCodespace sdk.CodespaceType = "pow"
CodeInvalidDifficulty CodeType = 201
CodeNonexistentDifficulty CodeType = 202
CodeNonexistentReward CodeType = 203
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/democoin/x/simplestake/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// simple stake errors reserve 300 ~ 399.
const (
DefaultCodespace sdk.CodespaceType = 4
DefaultCodespace sdk.CodespaceType = moduleName

// simplestake errors reserve 300 - 399.
CodeEmptyValidator sdk.CodeType = 300
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/democoin/x/simplestake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func handleMsgBond() sdk.Result {
// Removed ValidatorSet from result because it does not get used.
// TODO: Implement correct bond/unbond handling
return sdk.Result{
Code: sdk.ABCICodeOK,
Code: sdk.CodeOK,
}
}

func handleMsgUnbond() sdk.Result {
return sdk.Result{
Code: sdk.ABCICodeOK,
Code: sdk.CodeOK,
}
}
Loading

0 comments on commit 8d6b092

Please sign in to comment.