diff --git a/Dockerfile b/Dockerfile index 9499d46cf..0e9a894a4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,8 @@ RUN cd $REPO_PATH && \ make get_vendor_deps && \ make test_unit && \ make build_linux && \ - make install + make install && \ + make test_cli FROM alpine:3.7 diff --git a/Makefile b/Makefile index 97a6261d1..e8c566c3b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation' | grep -v '/prometheus' | grep -v '/clitest' | grep -v '/lcd' | grep -v '/protobuf') PACKAGES_MODULES=$(shell go list ./... | grep 'modules') -PACKAGES_TYPES=$(shell go list ./... | grep 'types') +PACKAGES_TYPES=$(shell go list ./... | grep 'irisnet/irishub/types') +PACKAGES_STORE=$(shell go list ./... | grep 'irisnet/irishub/store') PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation') all: get_tools get_vendor_deps install @@ -74,7 +75,7 @@ install: update_irislcd_swagger_docs echo_bech32_prefix go install $(INSTALL_FLAGS) $(BUILD_FLAGS) ./cmd/iriscli go install $(INSTALL_FLAGS) $(BUILD_FLAGS) ./cmd/irislcd go install $(INSTALL_FLAGS) $(BUILD_FLAGS) ./cmd/iristool - go install $(INSTALL_FLAGS) $(BUILD_FLAGS) ./cmd/newiris +# go install $(INSTALL_FLAGS) $(BUILD_FLAGS) ./cmd/newiris build_linux: update_irislcd_swagger_docs echo_bech32_prefix CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(BUILD_FLAGS) -o build/iris ./cmd/iris && \ @@ -100,6 +101,7 @@ test_unit: #@go test $(PACKAGES_NOSIMULATION) @go test $(PACKAGES_MODULES) @go test $(PACKAGES_TYPES) + @go test $(PACKAGES_STORE) test_cli: @go test -timeout 20m -count 1 -p 1 client/clitest/utils.go client/clitest/bank_test.go client/clitest/distribution_test.go client/clitest/gov_test.go client/clitest/iparam_test.go client/clitest/irismon_test.go client/clitest/record_test.go client/clitest/service_test.go client/clitest/stake_test.go diff --git a/app/app.go b/app/app.go index 92d9e1f5a..e4ab6a072 100644 --- a/app/app.go +++ b/app/app.go @@ -466,7 +466,8 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode) 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. var msgType string @@ -503,6 +504,7 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode) if !msgResult.IsOK() { logs = append(logs, fmt.Sprintf("Msg %d failed: %s", msgIdx, msgResult.Log)) code = msgResult.Code + codespace = msgResult.Codespace break } @@ -512,10 +514,11 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode) // 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, } diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 9d820da64..c782644da 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -50,7 +50,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 @@ -100,13 +99,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) } @@ -124,11 +119,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 { @@ -345,8 +335,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() @@ -355,8 +346,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" @@ -415,12 +407,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, } } @@ -478,6 +471,7 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { return abci.ResponseCheckTx{ Code: uint32(result.Code), + Codespace: string(result.Codespace), Data: result.Data, Log: result.Log, GasWanted: result.GasWanted, @@ -526,6 +520,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, @@ -545,7 +540,6 @@ func validateBasicTxMsgs(msgs []sdk.Msg) sdk.Error { // Validate the Msg. err := msg.ValidateBasic() if err != nil { - err = err.WithDefaultCodespace(sdk.CodespaceRoot) return err } } @@ -574,7 +568,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() @@ -601,6 +596,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 } @@ -610,10 +606,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, } diff --git a/client/clitest/utils.go b/client/clitest/utils.go index 909227e11..43f86e0a5 100644 --- a/client/clitest/utils.go +++ b/client/clitest/utils.go @@ -208,7 +208,8 @@ func copyFile(dstFile, srcFile string) error { // helper methods func initializeFixtures(t *testing.T) (chainID, servAddr, port string) { - tests.ExecuteT(t, fmt.Sprintf("iris --home=%s unsafe-reset-all", irisHome), "") + tests.ExecuteT(t, fmt.Sprintf("rm -rf %s ", irisHome), "") + //tests.ExecuteT(t, fmt.Sprintf("iris --home=%s unsafe-reset-all", irisHome), "") executeWrite(t, fmt.Sprintf("iriscli keys delete --home=%s foo", iriscliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("iriscli keys delete --home=%s bar", iriscliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("iriscli keys add --home=%s foo", iriscliHome), app.DefaultKeyPass) diff --git a/client/utils/rest.go b/client/utils/rest.go index 71c8f2f73..fe505fedc 100644 --- a/client/utils/rest.go +++ b/client/utils/rest.go @@ -37,7 +37,7 @@ type kvPair struct { TagValue string `json:"tag_value"` } type abciResult struct { - Code sdk.ABCICodeType `json:"code"` + Code sdk.CodeType `json:"code"` Data []byte `json:"data"` Log string `json:"log"` GasWanted int64 `json:"gas_wanted"` diff --git a/cmd/iristool/debug/hack.go b/cmd/iristool/debug/hack.go index a7ae13133..5e054c370 100644 --- a/cmd/iristool/debug/hack.go +++ b/cmd/iristool/debug/hack.go @@ -192,13 +192,13 @@ func NewIrisApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.paramsKeeper.Subspace(stake.DefaultParamspace), - app.RegisterCodespace(stake.DefaultCodespace), + stake.DefaultCodespace, ) app.slashingKeeper = slashing.NewKeeper( app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace), - app.RegisterCodespace(slashing.DefaultCodespace), + slashing.DefaultCodespace, ) app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection) app.upgradeKeeper = upgrade.NewKeeper(app.cdc, app.keyUpgrade, app.stakeKeeper) @@ -206,7 +206,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp app.cdc, app.keyGov, app.bankKeeper, app.stakeKeeper, - app.RegisterCodespace(gov.DefaultCodespace), + gov.DefaultCodespace, ) // register message routes app.Router(). diff --git a/modules/auth/ante_test.go b/modules/auth/ante_test.go index 76e5fb888..8e14f450d 100644 --- a/modules/auth/ante_test.go +++ b/modules/auth/ante_test.go @@ -42,7 +42,7 @@ func privAndAddr() (crypto.PrivKey, sdk.AccAddress) { func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool) { _, result, abort := anteHandler(ctx, tx, simulate) require.False(t, abort) - require.Equal(t, sdk.ABCICodeOK, result.Code) + require.Equal(t, sdk.CodeOK, result.Code) require.True(t, result.IsOK()) } @@ -50,8 +50,8 @@ func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool, code sdk.CodeType) { newCtx, result, abort := anteHandler(ctx, tx, simulate) require.True(t, abort) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, code), result.Code, - fmt.Sprintf("Expected %v, got %v", sdk.ToABCICode(sdk.CodespaceRoot, code), result)) + require.Equal(t, code, result.Code, fmt.Sprintf("Expected %v, got %v", code, result)) + require.Equal(t, sdk.CodespaceRoot, result.Codespace) if code == sdk.CodeOutOfGas { stdTx, ok := tx.(StdTx) diff --git a/modules/bank/errors.go b/modules/bank/errors.go index f21e36be3..807a635d7 100644 --- a/modules/bank/errors.go +++ b/modules/bank/errors.go @@ -7,7 +7,7 @@ import ( // Bank errors reserve 100 ~ 199. const ( - DefaultCodespace sdk.CodespaceType = 2 + DefaultCodespace sdk.CodespaceType = "bank" CodeInvalidInput sdk.CodeType = 101 CodeInvalidOutput sdk.CodeType = 102 diff --git a/modules/distribution/types/errors.go b/modules/distribution/types/errors.go index 88f9ea2d5..98e97166c 100644 --- a/modules/distribution/types/errors.go +++ b/modules/distribution/types/errors.go @@ -8,7 +8,7 @@ import ( type CodeType = sdk.CodeType const ( - DefaultCodespace sdk.CodespaceType = 6 + DefaultCodespace sdk.CodespaceType = "distr" CodeInvalidInput CodeType = 103 CodeNoDistributionInfo CodeType = 104 ) diff --git a/modules/distribution/types/msg.go b/modules/distribution/types/msg.go index eb940a06b..41b43ee15 100644 --- a/modules/distribution/types/msg.go +++ b/modules/distribution/types/msg.go @@ -17,7 +17,7 @@ var _, _ sdk.Msg = &MsgWithdrawDelegatorReward{}, &MsgWithdrawValidatorRewardsAl // msg struct for changing the withdraw address for a delegator (or validator self-delegation) type MsgSetWithdrawAddress struct { DelegatorAddr sdk.AccAddress `json:"delegator_addr"` - WithdrawAddr sdk.AccAddress `json:"delegator_addr"` + WithdrawAddr sdk.AccAddress `json:"withdraw_addr"` } func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) MsgSetWithdrawAddress { diff --git a/modules/gov/errors.go b/modules/gov/errors.go index f35572758..299047df2 100644 --- a/modules/gov/errors.go +++ b/modules/gov/errors.go @@ -8,7 +8,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 21 + DefaultCodespace sdk.CodespaceType = "gov" CodeUnknownProposal sdk.CodeType = 1 CodeInactiveProposal sdk.CodeType = 2 diff --git a/modules/gov/simulation/sim_test.go b/modules/gov/simulation/sim_test.go index 5617b4653..868c2cc1c 100644 --- a/modules/gov/simulation/sim_test.go +++ b/modules/gov/simulation/sim_test.go @@ -39,7 +39,7 @@ func TestGovWithRandomMessages(t *testing.T) { mapp.Cdc, govKey, bankKeeper, stakeKeeper, - mapp.RegisterCodespace(gov.DefaultCodespace), + gov.DefaultCodespace, ) mapp.Router().AddRoute("gov", []*sdk.KVStoreKey{govKey, mapp.KeyAccount, stakeKey, paramKey}, gov.NewHandler(govKeeper)) diff --git a/modules/gov/test_common.go b/modules/gov/test_common.go index 9b3200232..bfe71fbb6 100644 --- a/modules/gov/test_common.go +++ b/modules/gov/test_common.go @@ -34,7 +34,7 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, Keeper, stake.Keeper, mapp.Cdc, mapp.KeyStake, mapp.TkeyStake, mapp.BankKeeper, mapp.ParamsKeeper.Subspace(stake.DefaultParamspace), - mapp.RegisterCodespace(stake.DefaultCodespace)) + stake.DefaultCodespace) gk := NewKeeper(mapp.Cdc, keyGov, ck, sk, DefaultCodespace) mapp.Router().AddRoute("gov", []*sdk.KVStoreKey{keyGov}, NewHandler(gk)) diff --git a/modules/guardian/error.go b/modules/guardian/error.go index f796b1444..45a138afb 100644 --- a/modules/guardian/error.go +++ b/modules/guardian/error.go @@ -6,7 +6,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 25 + DefaultCodespace sdk.CodespaceType = "guardian" CodeProfilerExists sdk.CodeType = 100 CodeProfilerNotExists sdk.CodeType = 101 diff --git a/modules/mock/app_test.go b/modules/mock/app_test.go index 052e0bfc6..5282e41e7 100644 --- a/modules/mock/app_test.go +++ b/modules/mock/app_test.go @@ -143,7 +143,8 @@ func TestMsgSendWithAccounts(t *testing.T) { tx.Signatures[0].Sequence = 1 res := mapp.Deliver(tx) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnauthorized), res.Code, res.Log) + require.Equal(t, sdk.CodeUnauthorized, res.Code, res.Log) + require.Equal(t, sdk.CodespaceRoot, res.Codespace) // resigning the tx with the bumped sequence should work SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{sendMsg1, sendMsg2}, []int64{0}, []int64{1}, true, true, priv1) diff --git a/modules/mock/test_utils.go b/modules/mock/test_utils.go index a845d0fa2..e80469315 100644 --- a/modules/mock/test_utils.go +++ b/modules/mock/test_utils.go @@ -58,9 +58,9 @@ func CheckGenTx( res := app.Check(tx) if expPass { - require.Equal(t, sdk.ABCICodeOK, res.Code, res.Log) + require.Equal(t, sdk.CodeOK, res.Code, res.Log) } else { - require.NotEqual(t, sdk.ABCICodeOK, res.Code, res.Log) + require.NotEqual(t, sdk.CodeOK, res.Code, res.Log) } return res @@ -79,9 +79,9 @@ func SignCheckDeliver( res := app.Simulate(tx) if expSimPass { - require.Equal(t, sdk.ABCICodeOK, res.Code, res.Log) + require.Equal(t, sdk.CodeOK, res.Code, res.Log) } else { - require.NotEqual(t, sdk.ABCICodeOK, res.Code, res.Log) + require.NotEqual(t, sdk.CodeOK, res.Code, res.Log) } // Simulate a sending a transaction and committing a block @@ -89,9 +89,9 @@ func SignCheckDeliver( res = app.Deliver(tx) if expPass { - require.Equal(t, sdk.ABCICodeOK, res.Code, res.Log) + require.Equal(t, sdk.CodeOK, res.Code, res.Log) } else { - require.NotEqual(t, sdk.ABCICodeOK, res.Code, res.Log) + require.NotEqual(t, sdk.CodeOK, res.Code, res.Log) } app.EndBlock(abci.RequestEndBlock{}) diff --git a/modules/params/errors.go b/modules/params/errors.go index e4e09ce68..190b2b3c3 100644 --- a/modules/params/errors.go +++ b/modules/params/errors.go @@ -5,7 +5,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 20 + DefaultCodespace sdk.CodespaceType = "params" CodeInvalidMinDeposit sdk.CodeType = 100 CodeInvalidMinDepositDenom sdk.CodeType = 101 CodeInvalidMinDepositAmount sdk.CodeType = 102 diff --git a/modules/record/errors.go b/modules/record/errors.go index 09393d4d9..84f385aeb 100644 --- a/modules/record/errors.go +++ b/modules/record/errors.go @@ -8,7 +8,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 22 + DefaultCodespace sdk.CodespaceType = "record" CodeInvalidDataSize sdk.CodeType = 1 CodeInvalidFileDescription sdk.CodeType = 2 diff --git a/modules/record/test_common.go b/modules/record/test_common.go index a39ce9c73..d5d822d43 100644 --- a/modules/record/test_common.go +++ b/modules/record/test_common.go @@ -68,8 +68,8 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, Keeper, stake.Keeper, mapp.Cdc, mapp.KeyStake, mapp.TkeyStake, mapp.BankKeeper, mapp.ParamsKeeper.Subspace(stake.DefaultParamspace), - mapp.RegisterCodespace(stake.DefaultCodespace)) - rk := NewKeeper(mapp.Cdc, keyRecord, mapp.RegisterCodespace(DefaultCodespace)) + stake.DefaultCodespace) + rk := NewKeeper(mapp.Cdc, keyRecord, DefaultCodespace) mapp.Router().AddRoute("record", []*sdk.KVStoreKey{keyRecord}, NewHandler(rk)) diff --git a/modules/service/error.go b/modules/service/error.go index b7f80d3a3..26dca17ba 100644 --- a/modules/service/error.go +++ b/modules/service/error.go @@ -6,7 +6,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 23 + DefaultCodespace sdk.CodespaceType = "service" CodeInvalidDefiniton sdk.CodeType = 100 CodeInvalidBinding sdk.CodeType = 101 diff --git a/modules/service/test_common.go b/modules/service/test_common.go index a45652917..e1bb39c36 100644 --- a/modules/service/test_common.go +++ b/modules/service/test_common.go @@ -36,7 +36,7 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, Keeper, stake.Keeper, mapp.Cdc, mapp.KeyStake, mapp.TkeyStake, mapp.BankKeeper, mapp.ParamsKeeper.Subspace(stake.DefaultParamspace), - mapp.RegisterCodespace(stake.DefaultCodespace)) + stake.DefaultCodespace) ik := NewKeeper(mapp.Cdc, keyService, ck, gk, DefaultCodespace) mapp.Router().AddRoute("service", []*sdk.KVStoreKey{keyService}, NewHandler(ik)) diff --git a/modules/slashing/app_test.go b/modules/slashing/app_test.go index 63228020d..5c352f48d 100644 --- a/modules/slashing/app_test.go +++ b/modules/slashing/app_test.go @@ -30,8 +30,8 @@ func getMockApp(t *testing.T) (*mock.App, stake.Keeper, Keeper) { bankKeeper := bank.NewBaseKeeper(mApp.AccountKeeper) paramsKeeper := params.NewKeeper(mApp.Cdc, mApp.KeyParams, mApp.TkeyParams) - stakeKeeper := stake.NewKeeper(mApp.Cdc, mApp.KeyStake, mApp.TkeyStake, bankKeeper, paramsKeeper.Subspace(stake.DefaultParamspace), mApp.RegisterCodespace(stake.DefaultCodespace)) - keeper := NewKeeper(mApp.Cdc, keySlashing, stakeKeeper, paramsKeeper.Subspace(DefaultParamspace), mApp.RegisterCodespace(DefaultCodespace)) + stakeKeeper := stake.NewKeeper(mApp.Cdc, mApp.KeyStake, mApp.TkeyStake, bankKeeper, paramsKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace) + keeper := NewKeeper(mApp.Cdc, keySlashing, stakeKeeper, paramsKeeper.Subspace(DefaultParamspace), DefaultCodespace) mApp.Router().AddRoute("stake", []*sdk.KVStoreKey{mApp.KeyStake, mApp.KeyAccount, mApp.KeyParams}, stake.NewHandler(stakeKeeper)) mApp.Router().AddRoute("slashing", []*sdk.KVStoreKey{mApp.KeyStake, keySlashing, mApp.KeyParams}, NewHandler(keeper)) @@ -120,5 +120,6 @@ func TestSlashingMsgs(t *testing.T) { // unjail should fail with unknown validator res := mock.SignCheckDeliver(t, mapp.BaseApp, []sdk.Msg{unjailMsg}, []int64{0}, []int64{1}, false, false, priv1) - require.Equal(t, sdk.ToABCICode(DefaultCodespace, CodeValidatorNotJailed), res.Code) + require.EqualValues(t, CodeValidatorNotJailed, res.Code) + require.EqualValues(t, DefaultCodespace, res.Codespace) } diff --git a/modules/slashing/errors.go b/modules/slashing/errors.go index 29f25e72f..92efdb27e 100644 --- a/modules/slashing/errors.go +++ b/modules/slashing/errors.go @@ -10,7 +10,7 @@ type CodeType = sdk.CodeType const ( // Default slashing codespace - DefaultCodespace sdk.CodespaceType = 10 + DefaultCodespace sdk.CodespaceType = "slash" CodeInvalidValidator CodeType = 101 CodeValidatorJailed CodeType = 102 diff --git a/modules/slashing/handler_test.go b/modules/slashing/handler_test.go index 65cf7f16d..8e0ebff55 100644 --- a/modules/slashing/handler_test.go +++ b/modules/slashing/handler_test.go @@ -26,7 +26,8 @@ func TestCannotUnjailUnlessJailed(t *testing.T) { // assert non-jailed validator can't be unjailed got = slh(ctx, NewMsgUnjail(addr)) require.False(t, got.IsOK(), "allowed unjail of non-jailed validator") - require.Equal(t, sdk.ToABCICode(DefaultCodespace, CodeValidatorNotJailed), got.Code) + require.EqualValues(t, CodeValidatorNotJailed, got.Code) + require.EqualValues(t, DefaultCodespace, got.Codespace) } func TestJailedValidatorDelegations(t *testing.T) { diff --git a/modules/stake/app_test.go b/modules/stake/app_test.go index f360442a4..2ff60910c 100644 --- a/modules/stake/app_test.go +++ b/modules/stake/app_test.go @@ -22,7 +22,7 @@ func getMockApp(t *testing.T) (*mock.App, Keeper) { bankKeeper := bank.NewBaseKeeper(mApp.AccountKeeper) pk := params.NewKeeper(mApp.Cdc, mApp.KeyParams, mApp.TkeyParams) - keeper := NewKeeper(mApp.Cdc, mApp.KeyStake, mApp.TkeyStake, bankKeeper, pk.Subspace(DefaultParamspace), mApp.RegisterCodespace(DefaultCodespace)) + keeper := NewKeeper(mApp.Cdc, mApp.KeyStake, mApp.TkeyStake, bankKeeper, pk.Subspace(DefaultParamspace), DefaultCodespace) mApp.Router().AddRoute("stake", []*sdk.KVStoreKey{mApp.KeyStake, mApp.KeyAccount, mApp.KeyParams}, NewHandler(keeper)) mApp.SetEndBlocker(getEndBlocker(keeper)) diff --git a/modules/stake/types/errors.go b/modules/stake/types/errors.go index f044e6faf..a14f8476d 100644 --- a/modules/stake/types/errors.go +++ b/modules/stake/types/errors.go @@ -11,7 +11,7 @@ import ( type CodeType = sdk.CodeType const ( - DefaultCodespace sdk.CodespaceType = 4 + DefaultCodespace sdk.CodespaceType = "stake" CodeInvalidValidator CodeType = 101 CodeInvalidDelegation CodeType = 102 diff --git a/modules/upgrade/error.go b/modules/upgrade/error.go index 071cc24a8..5da453930 100644 --- a/modules/upgrade/error.go +++ b/modules/upgrade/error.go @@ -5,7 +5,7 @@ import ( ) const ( - DefaultCodespace sdk.CodespaceType = 24 + DefaultCodespace sdk.CodespaceType = "upgrade" CodeInvalidMsgType sdk.CodeType = 100 CodeUnSupportedMsgType sdk.CodeType = 101 diff --git a/store/rootmultistore_test.go b/store/rootmultistore_test.go index dd8dc65d8..88ed6f59d 100644 --- a/store/rootmultistore_test.go +++ b/store/rootmultistore_test.go @@ -202,34 +202,37 @@ func TestMultiStoreQuery(t *testing.T) { // Test bad path. query := abci.RequestQuery{Path: "/key", Data: k, Height: ver} qres := multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeUnknownRequest, qres.Code) + require.EqualValues(t, sdk.CodespaceRoot, qres.Codespace) query.Path = "h897fy32890rf63296r92" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeUnknownRequest, qres.Code) + require.EqualValues(t, sdk.CodespaceRoot, qres.Codespace) // Test invalid store name. query.Path = "/garbage/key" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeUnknownRequest, qres.Code) + require.EqualValues(t, sdk.CodespaceRoot, qres.Codespace) // Test valid query with data. query.Path = "/store1/key" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeOK, qres.Code) require.Equal(t, v, qres.Value) // Test valid but empty query. query.Path = "/store2/key" query.Prove = true qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeOK, qres.Code) require.Nil(t, qres.Value) // Test store2 data. query.Data = k2 qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.EqualValues(t, sdk.CodeOK, qres.Code) require.Equal(t, v2, qres.Value) } diff --git a/types/codespacer.go b/types/codespacer.go deleted file mode 100644 index 92025c1a6..000000000 --- a/types/codespacer.go +++ /dev/null @@ -1,35 +0,0 @@ -package types - -// Codespacer is a simple struct to track reserved codespaces -type Codespacer struct { - reserved map[CodespaceType]bool -} - -// NewCodespacer generates a new Codespacer with the starting codespace -func NewCodespacer() *Codespacer { - return &Codespacer{ - reserved: make(map[CodespaceType]bool), - } -} - -// RegisterNext reserves and returns the next available codespace, starting from a default, and panics if the maximum codespace is reached -func (c *Codespacer) RegisterNext(codespace CodespaceType) CodespaceType { - for { - if !c.reserved[codespace] { - c.reserved[codespace] = true - return codespace - } - codespace++ - if codespace == MaximumCodespace { - panic("Maximum codespace reached!") - } - } -} - -// RegisterOrPanic reserved a codespace or panics if it is unavailable -func (c *Codespacer) RegisterOrPanic(codespace CodespaceType) { - if c.reserved[codespace] { - panic("Cannot register codespace, already reserved") - } - c.reserved[codespace] = true -} diff --git a/types/codespacer_test.go b/types/codespacer_test.go deleted file mode 100644 index 7052aa92a..000000000 --- a/types/codespacer_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package types - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestRegisterNext(t *testing.T) { - codespacer := NewCodespacer() - // unregistered, allow - code1 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code1, CodespaceType(2)) - // registered, pick next - code2 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code2, CodespaceType(3)) - // pick next - code3 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code3, CodespaceType(4)) - // skip 1 - code4 := codespacer.RegisterNext(CodespaceType(6)) - require.Equal(t, code4, CodespaceType(6)) - code5 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code5, CodespaceType(5)) - code6 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code6, CodespaceType(7)) - // panic on maximum - defer func() { - r := recover() - require.NotNil(t, r, "Did not panic on maximum codespace") - }() - codespacer.RegisterNext(MaximumCodespace - 1) - codespacer.RegisterNext(MaximumCodespace - 1) -} - -func TestRegisterOrPanic(t *testing.T) { - codespacer := NewCodespacer() - // unregistered, allow - code1 := codespacer.RegisterNext(CodespaceType(2)) - require.Equal(t, code1, CodespaceType(2)) - // panic on duplicate - defer func() { - r := recover() - require.NotNil(t, r, "Did not panic on duplicate codespace") - }() - codespacer.RegisterOrPanic(CodespaceType(2)) -} diff --git a/types/errors.go b/types/errors.go index ef7785dd6..20d187cde 100644 --- a/types/errors.go +++ b/types/errors.go @@ -4,43 +4,27 @@ import ( "fmt" "strings" - "github.com/irisnet/irishub/codec" - cmn "github.com/tendermint/tendermint/libs/common" - + "github.com/cosmos/cosmos-sdk/codec" abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" ) -// ABCICodeType - combined codetype / codespace -type ABCICodeType uint32 - -// CodeType - code identifier within codespace -type CodeType uint16 +// CodeType - ABCI code identifier within codespace +type CodeType uint32 // CodespaceType - codespace identifier -type CodespaceType uint16 +type CodespaceType string // IsOK - is everything okay? -func (code ABCICodeType) IsOK() bool { - if code == ABCICodeOK { +func (code CodeType) IsOK() bool { + if code == CodeOK { return true } return false } -// get the abci code from the local code and codespace -func ToABCICode(space CodespaceType, code CodeType) ABCICodeType { - // TODO: Make Tendermint more aware of codespaces. - if space == CodespaceRoot && code == CodeOK { - return ABCICodeOK - } - return ABCICodeType((uint32(space) << 16) | uint32(code)) -} - // SDK error codes const ( - // ABCI error codes - ABCICodeOK ABCICodeType = 0 - // Base error codes CodeOK CodeType = 0 CodeInternal CodeType = 1 @@ -58,15 +42,13 @@ const ( CodeMemoTooLarge CodeType = 13 CodeInsufficientFee CodeType = 14 CodeOutOfService CodeType = 15 + CodeTooManySignatures CodeType = 16 // CodespaceRoot is a codespace for error codes in this file only. // Notice that 0 is an "unset" codespace, which can be overridden with // Error.WithDefaultCodespace(). - CodespaceUndefined CodespaceType = 0 - CodespaceRoot CodespaceType = 1 - - // Maximum reservable codespace (2^16 - 1) - MaximumCodespace CodespaceType = 65535 + CodespaceUndefined CodespaceType = "" + CodespaceRoot CodespaceType = "sdk" ) func unknownCodeMsg(code CodeType) string { @@ -106,6 +88,8 @@ func CodeToDefaultMsg(code CodeType) string { return "insufficient fee" case CodeOutOfService: return "out of service" + case CodeTooManySignatures: + return "maximum numer of signatures exceeded" default: return unknownCodeMsg(code) } @@ -158,6 +142,9 @@ func ErrMemoTooLarge(msg string) Error { func ErrInsufficientFee(msg string) Error { return newErrorWithRootCodespace(CodeInsufficientFee, msg) } +func ErrTooManySignatures(msg string) Error { + return newErrorWithRootCodespace(CodeTooManySignatures, msg) +} //---------------------------------------- // Error & sdkError @@ -182,7 +169,6 @@ type Error interface { Code() CodeType Codespace() CodespaceType ABCILog() string - ABCICode() ABCICodeType Result() Result QueryResult() abci.ResponseQuery } @@ -236,17 +222,12 @@ func (err *sdkError) TraceSDK(format string, args ...interface{}) Error { // Implements ABCIError. func (err *sdkError) Error() string { return fmt.Sprintf(`ERROR: -Codespace: %d +Codespace: %s Code: %d Message: %#v `, err.codespace, err.code, err.cmnError.Error()) } -// Implements ABCIError. -func (err *sdkError) ABCICode() ABCICodeType { - return ToABCICode(err.codespace, err.code) -} - // Implements Error. func (err *sdkError) Codespace() CodespaceType { return err.codespace @@ -264,7 +245,6 @@ func (err *sdkError) ABCILog() string { jsonErr := humanReadableError{ Codespace: err.codespace, Code: err.code, - ABCICode: err.ABCICode(), Message: errMsg, } bz, er := cdc.MarshalJSON(jsonErr) @@ -277,16 +257,18 @@ func (err *sdkError) ABCILog() string { func (err *sdkError) Result() Result { return Result{ - Code: err.ABCICode(), - Log: err.ABCILog(), + Code: err.Code(), + Codespace: err.Codespace(), + Log: err.ABCILog(), } } // QueryResult allows us to return sdk.Error.QueryResult() in query responses func (err *sdkError) QueryResult() abci.ResponseQuery { return abci.ResponseQuery{ - Code: uint32(err.ABCICode()), - Log: err.ABCILog(), + Code: uint32(err.Code()), + Codespace: string(err.Codespace()), + Log: err.ABCILog(), } } @@ -321,6 +303,5 @@ func mustGetMsgIndex(abciLog string) int { type humanReadableError struct { Codespace CodespaceType `json:"codespace"` Code CodeType `json:"code"` - ABCICode ABCICodeType `json:"abci_code"` Message string `json:"message"` } diff --git a/types/errors_test.go b/types/errors_test.go index 1d63e0990..8d4f88226 100644 --- a/types/errors_test.go +++ b/types/errors_test.go @@ -42,7 +42,7 @@ var errFns = []errFn{ } func TestCodeType(t *testing.T) { - require.True(t, ABCICodeOK.IsOK()) + require.True(t, CodeOK.IsOK()) for tcnum, c := range codeTypes { msg := CodeToDefaultMsg(c) @@ -59,12 +59,9 @@ func TestErrFn(t *testing.T) { codeType := codeTypes[i] require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", i) require.Equal(t, err.Codespace(), CodespaceRoot, "Err function expected to return proper codespace. tc #%d", i) - require.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType), "Err function expected to return proper ABCICode. tc #%d") - require.Equal(t, err.QueryResult().Code, uint32(err.ABCICode()), "Err function expected to return proper ABCICode from QueryResult. tc #%d") + require.Equal(t, err.QueryResult().Code, uint32(err.Code()), "Err function expected to return proper Code from QueryResult. tc #%d") require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d") } - - require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK)) } func TestAppendMsgToErr(t *testing.T) { diff --git a/types/result.go b/types/result.go index 63b8a6e18..20893a076 100644 --- a/types/result.go +++ b/types/result.go @@ -4,7 +4,10 @@ package types type Result struct { // Code is the response code, is stored back on the chain. - Code ABCICodeType + Code CodeType + + // Codespace is the string referring to the domain of an error + Codespace CodespaceType // Data is any data returned from the app. Data []byte diff --git a/types/result_test.go b/types/result_test.go index e0305932c..9765933d9 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -13,6 +13,6 @@ func TestResult(t *testing.T) { res.Data = []byte("data") require.True(t, res.IsOK()) - res.Code = ABCICodeType(1) + res.Code = CodeType(1) require.False(t, res.IsOK()) }